summaryrefslogtreecommitdiff
path: root/Reactor/Moore.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Reactor/Moore.hs')
-rw-r--r--Reactor/Moore.hs34
1 files changed, 34 insertions, 0 deletions
diff --git a/Reactor/Moore.hs b/Reactor/Moore.hs
new file mode 100644
index 0000000..5995cb2
--- /dev/null
+++ b/Reactor/Moore.hs
@@ -0,0 +1,34 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+module Reactor.Moore
+ ( Moore(..)
+ ) where
+
+import Control.Applicative
+import Control.Comonad
+import Data.Functor.Apply
+import Data.Typeable
+
+data Moore i o = Moore { step :: i -> Moore i o, current :: o }
+ deriving Typeable
+
+instance Functor (Moore i) where
+ fmap g (Moore f o) = Moore (fmap g . f) (g o)
+ b <$ _ = pure b
+
+instance Extend (Moore i) where
+ duplicate m = Moore (duplicate . step m) m
+ extend g m = Moore (extend g . step m) (g m)
+
+instance Comonad (Moore i) where
+ extract (Moore _ o) = o
+
+instance Apply (Moore i) where
+ Moore ff f <.> Moore fa a = Moore (\i -> ff i <.> fa i) (f a)
+ a <. _ = a
+ _ .> b = b
+
+instance Applicative (Moore i) where
+ pure o = m where m = Moore (const m) o
+ (<*>) = (<.>)
+ (<* ) = (<. )
+ ( *>) = ( .>)