大久保弘崇
ex10.5
最終更新:
hirotakaohkubo
-
view
10.16
instance ArrowCircuit StreamMap where
delay b :: SM (Cons b)
著者のPPTにあるとおりだが、
のような実行関数でcounterを評価すると無限ループに陥るのは、
zipStream, unzipStreamあたりのパターンに~を付けてlazy matchにすると直った。
zipStream, unzipStreamあたりのパターンに~を付けてlazy matchにすると直った。
ex1016 = appSMtake counter st 11 where
st = repli 5 False (Cons True st)
repli 0 _ rest = rest
repli k v rest = v `Cons` (repli (k-1) v rest)
*Main> ex1016
[0,1,2,3,4,0,1,2,3,4,5]
10.17
これを解くには8章を理解しているといいらしい。
10.18
newtype StateT s a i o = STT (a (s,i) (s,o))
instance Arrow a => Arrow (StateT s a) where
pure f = STT (pure (id `x` f))
STT f >>> STT g = STT (f >>> g)
first (STT f) = STT (pure unassoc >>> first f >>> pure assoc)
型しか合わせていないので正しいかはよくわからない。
proc構文からの書換えではなく、非変換子版を持ち上げる方向での解答。
proc構文からの書換えではなく、非変換子版を持ち上げる方向での解答。
10.19
proc構文を使うものは、非変換子版のλ関数をprocに置き換えることで作れる(!)
newtype AutoFunctor a i o = AF (a i (o, AutoFunctor a i o))
instance Arrow a => Category (AutoFunctor a) where
id = arr id
AF g . AF f = AF (proc b -> do { (c,f') <- f -< b; (d,g') <- g -< c; idA -< (d, g'.f') })
instance Arrow a => Arrow (AutoFunctor a) where
arr f = AF (proc b -> idA -< (f b, arr f))
first (AF f) = AF (proc (b,d) -> do { (c,g) <- f -< b ; idA -< ((c,d), first g) })
元になる定義と見比べるとその恐ろしさがわかるだろう。
instance Category Auto where
id = arr id
A g . A f = A (\b -> let { (c,f') = f b; (d, g') = g c; } in (d, g' . f'))
instance Arrow Auto where
arr f = A (\b -> (f b, arr f))
first (A f) = A (\(b,d) -> let { (c,f') = f b; } in ((c,d), first f'))