Skip to content

Instantly share code, notes, and snippets.

@masaeedu
Created May 4, 2020 08:25
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 masaeedu/e9c228eb758a6d1f52568f133df304d0 to your computer and use it in GitHub Desktop.
Save masaeedu/e9c228eb758a6d1f52568f133df304d0 to your computer and use it in GitHub Desktop.
Free things
{-# LANGUAGE ImpredicativeTypes #-}
module Whatever where
import Data.Profunctor
newtype ForgetP p a b = ForgetP { runForgetP :: p a b }
deriving Profunctor
class (forall p. Profunctor p => thing (t p)) => Free thing t
where
fwd :: (Profunctor p, thing q) => (p :-> ForgetP q) -> t p :-> q
bwd :: (Profunctor p, thing q) => (t p :-> q) -> p :-> ForgetP q
data TheFree thing p a b = TheFree { runFree :: forall q. (Profunctor q, thing q) => (p :-> ForgetP q) -> q a b }
instance Profunctor p => Profunctor (TheFree thing p)
where
dimap f g (TheFree x) = TheFree $ \cb -> dimap f g $ x cb
instance Profunctor p => Strong (TheFree Strong p)
where
first' (TheFree p) = TheFree $ \cb -> first' $ p cb
instance Free Strong (TheFree Strong)
where
fwd cb (TheFree p) = p cb
bwd cb p = ForgetP $ cb $ TheFree $ runForgetP . ($ p)
instance Profunctor p => Costrong (TheFree Costrong p)
where
unfirst (TheFree p) = TheFree $ \cb -> unfirst $ p cb
instance Free Costrong (TheFree Costrong)
where
fwd cb (TheFree p) = p cb
bwd cb p = ForgetP $ cb $ TheFree $ runForgetP . ($ p)
instance Profunctor p => Choice (TheFree Choice p)
where
left' (TheFree p) = TheFree $ \cb -> left' $ p cb
instance Free Choice (TheFree Choice)
where
fwd cb (TheFree p) = p cb
bwd cb p = ForgetP $ cb $ TheFree $ runForgetP . ($ p)
instance Profunctor p => Cochoice (TheFree Cochoice p)
where
unleft (TheFree p) = TheFree $ \cb -> unleft $ p cb
instance Free Cochoice (TheFree Cochoice)
where
fwd cb (TheFree p) = p cb
bwd cb p = ForgetP $ cb $ TheFree $ runForgetP . ($ p)
-- ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment