Skip to content

Instantly share code, notes, and snippets.

@shhyou
Created April 22, 2021 22:41
Show Gist options
  • Save shhyou/d4376e3829e37ac73e53c1264a98635a to your computer and use it in GitHub Desktop.
Save shhyou/d4376e3829e37ac73e53c1264a98635a to your computer and use it in GitHub Desktop.
import Control.Applicative
import Control.Monad
main = putStrLn "Hello, World!"
data Void
data Cont r a = Cont ((a -> r) -> r)
unCont (Cont m) = m
instance Functor (Cont r) where
fmap f (Cont ma) = Cont (\k -> ma (\a -> k (f a)))
instance Applicative (Cont r) where
pure a = Cont (\k -> k a)
(Cont mf) <*> (Cont ma) = Cont (\k -> mf (\f -> ma (\a -> k (f a))))
instance Monad (Cont r) where
(Cont ma) >>= amb = Cont (\k -> ma (\a -> unCont (amb a) k))
exc0 :: Cont r (Either a (a -> r))
exc0 = Cont (\k -> k (Right (\a -> k (Left a))))
exc :: Cont r (Either a (a -> Cont r b))
exc = Cont (\k -> k (Right (\a -> Cont (\k' -> k (Left a)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment