Skip to content

Instantly share code, notes, and snippets.

@elliotpotts
Created December 12, 2016 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotpotts/e31f05b44e64cd9d50c062955cabeb36 to your computer and use it in GitHub Desktop.
Save elliotpotts/e31f05b44e64cd9d50c062955cabeb36 to your computer and use it in GitHub Desktop.
data FreeA : (Type -> Type) -> Type -> Type where
Ap : f a -> FreeA f (a -> b) -> FreeA f b
Pure : a -> FreeA f a
implementation Functor (FreeA f) where
map f (Pure x) = Pure (f x)
map f (Ap x y) = Ap x ((f .) <$> y) -- y <*> x
implementation Applicative (FreeA f) where
pure = Pure
(Pure f) <*> y = map f y
(Ap x y) <*> z = assert_total $ Ap x (flip <$> y <*> z)
data ListF : Type -> Type -> Type where
Empty : ListF b a
Cons : b -> a -> ListF b a
liftAp : f a -> FreeA f a
liftAp x = Ap x (Pure id)
empty : FreeA (ListF a) ()
empty = liftAp Empty
cons : b -> FreeA (ListF b) ()
cons x = liftAp (Cons x ())
foo : FreeA (ListF Int) Int
foo = [| id empty (cons (+1)) |]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment