Arrow
Monadの進化形みたいなもの(適当)
Monad m -- m a :: *, m :: * -> *
Arrow a -- a b c :: *, a :: * -> * -> *
ArrowはArrowApplyを満たしてはじめてMonadと等価だそうです.
ライブラリ
netwire
Wire型
instance
記法として使いそうなのだけ.ArrowApplyは満たしていない.
以下の省略を用いる.本当はdataに制約つけるのは良くないらしい.
(Monoid e, Monad m) => type WireP = Wire e m
Categoryっぽく直列につなぐ.
instance Category WireP where
id :: WireP a a
(.) :: WireP b c -> WireP a b -> WireP a c
Alternativeで並列に.
instance Functor (WireP a) where
fmap :: (b -> c) -> WireP a b -> WireP a c
instance Functor (WireP a) => Applicative (WireP a) where
pure :: b -> WireP a b
(<*>) :: WireP a (b -> c) -> WireP a b -> WireP a c
(<$>) :: (b -> c) -> WireP a b -> WireP a c -- (<$>) = fmap
instance Applicative (WireP a) => Alternative (WireP a) where -- Monoid e はここ.
empty :: WireP a b
(<|>) :: WireP a b -> WireP a b -> WireP a b
some, many :: WireP a b -> WireP a [b]
timeとかを数字のように扱える.
instance Num b => Num (WireP a b) where
(+), (-), (*) :: WireP a b -> WireP a b -> WireP a b
negate, abs, signum :: WireP a b -> WireP a b
fromInteger :: Integer -> WireP a b
instance (Num (WireP a b), Fractional b) => Fractional (WireP a b) where
(/) :: WireP a b -> WireP a b -> WireP a b
recip :: WireP a b -> WireP a b
fromRational :: Rational -> WireP a b
instance (Fractional (WireP a b), Floating b) => Floating (WireP a b) where
-- pi, exp, sqrt, log, (**), logBase
-- sin, tan, cos, asin, atan, acos, sinh, tanh, cosh, asinh, atanh, acosh
OverloadedStrings拡張で使う.
instance IsString b => IsString (WireP a b) where
fromString :: String -> WireP a b
他にRead, Monoid, あとベクトル関係.
メモ
Arrowの構文
wire :: WireP Input Output
wire = proc input -> do
output <- somewire -< input
rec
loop <- delay etc -< loop
returnA -< output
recも書いてあるけど使いこなせる気がしない
(未検証)変数っぽいのは全て<- -<の外側に置く.こんなんとかダメ
bad = proc input -> do
var <- testwire -< ()
output <- argwire $ var -< input
...
ArrowApplyがあれば-<<なんかでできるみたいだけどnetwireは不可.
「Monadに内部状態を加えたもの」だけならMonadFixで多分十分.DoRecとか
Arrowは「計算」そのものを高度に抽象化したものなので(多分)
Arrow 入力 出力
なんて型になる.
最終更新:2012年12月26日 07:24