Skip to content

Instantly share code, notes, and snippets.

@bohde
Created May 18, 2015 01:52
Show Gist options
  • Save bohde/b56d7addc6464af5ac87 to your computer and use it in GitHub Desktop.
Save bohde/b56d7addc6464af5ac87 to your computer and use it in GitHub Desktop.
module Fork where
newtype Fork' f a = Fork' { runFork :: forall r. (a -> r) ->
(f r -> r) ->
(forall b. Fork' f b -> r -> r) -> r } deriving Functor
instance Applicative (Fork' f) where
pure a = Fork' (\kh _ _ -> kh a)
Fork' f <*> Fork' g = Fork' (\kh ka kf -> f (\a -> g (\b -> kh (a b)) ka kf) ka kf)
instance Monad (Fork' f) where
return = pure
Fork' m >>= f = Fork' (\kh ka kf -> m (\a -> runFork (f a) kh ka kf) ka kf)
liftF' :: Functor f => f a -> Fork' f a
liftF' f = Fork' (\kh ka _ -> ka (fmap kh f))
fork' :: Functor f => Fork' f a -> Fork' f ()
fork' f = Fork' (\kh _ kf -> kf f (kh ()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment