Skip to content

Instantly share code, notes, and snippets.

@myuon
Created December 2, 2014 06:41
Show Gist options
  • Save myuon/91384ae5785c92226131 to your computer and use it in GitHub Desktop.
Save myuon/91384ae5785c92226131 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative
import Control.Arrow
import qualified Control.Category as C
import Control.Monad
data Process m a b = Process { runProcess :: a -> m (b, Process m a b) }
deriving (Functor)
instance (Applicative f) => Applicative (Process f a) where
pure b = go where
go = Process $ \_ -> pure (b, go)
f0 <*> a0 = Process $ go f0 a0 where
go mf ma a = (\(f,mf') (x,mx') -> (f x, Process $ go mf' mx'))
<$> runProcess mf a
<*> runProcess ma a
instance (Monad m) => C.Category (Process m) where
id = arr id
pbc . pab = go pab pbc where
go f g = Process $ \a -> do
(b, f') <- runProcess f a
(c, g') <- runProcess g b
return (c, go f' g')
instance (Monad m) => Arrow (Process m) where
arr f = go where
go = Process $ \b -> return (f b, go)
first m = go where
go = Process $ \(b,d) -> return . ((\c -> (c,d)) *** first) =<< runProcess m b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment