Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
Last active November 11, 2015 02:23
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 parsonsmatt/1a58af69792c48d2b308 to your computer and use it in GitHub Desktop.
Save parsonsmatt/1a58af69792c48d2b308 to your computer and use it in GitHub Desktop.
to free or not to free
-- so I can either have a convention on returning a bare bool value
-- out of this strangeness.
data AndF k = forall f. (AndF :<: f) => And (Free f Bool) (Free f Bool) (Bool -> k)
testProgram :: (AndF :<: f) => Free f Bool
testProgram = do
asdf <- true .&& false
hmmm <- false .&& true
wat <- pure asdf .&& pure hmmm
return asdf
-- or, alternatively, I can return a value in the expression language,
-- which makes it easier to use the thing but complicates the type signatures
-- (and how do I get it out I guess??)
data AndF k = forall f. (AndF :<: f) => And (Free f Bool) (Free f Bool) (Free f Bool -> k)
testProgram :: (AndF :<: f) => Free f (Free f Bool)
testProgram = do
asdf <- true .&& false
hmmm <- false .&& true
wat <- asdf .&& hmmm
return asdf
-- boilerplate stuff:
instance Functor AndF where
fmap f (And a b k) = And a b (f . k)
(.&&) :: (AndF :<: f) => Free f Bool -> Free f Bool -> Free f Bool
a .&& b = Free . inj $ And a b Pure
true :: Functor f => Free f Bool
true = Pure True
false :: Functor f => Free f Bool
false = Pure False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment