I've seen the term Free Monad pop up every now and then for some time, but everyone just seems to use/discuss them without giving an explanation of what they are. So: what are free monads? (I'd say I'm familiar with monads and the Haskell basics,…
Monads can do many amazing, crazy things. They can create variables which hold a superposition of values. They can allow you to access data from the future before you compute it. They can allow you to write destructive updates, but not really. And…
I've been playing with Cofree, and can't quite grok it.
For example, I want to play with Cofree [] Num in ghci and can't quite get any interesting examples.
For example, if I construct a Cofree type:
let a = 1 :< [2, 3]
I would expect extract a ==…
We know from the category theory that not all endofunctors in Set admit a free monad. The canonical counterexample is the powerset functor.
But Haskell can turn any functor into a free monad.
data Free f a = Pure a | Free (f (Free f a))
instance…
I'm working on a project that, amongst other things, involves a database access layer. Pretty normal, really. In a previous project, a collaborator encouraged me to use the Free Monads concept for a database layer and so I did. Now I'm trying to…
I was reading http://www.haskellforall.com/2013/06/from-zero-to-cooperative-threads-in-33.html where an abstract syntax tree is derived as the free monad of a functor representing a set of instructions. I noticed that the free monad Free is not much…
Are there any rules of thumb for when to use continuation-passing style vs codensity vs reflection without remorse when creating monads in Haskell?
As an example, I'm going to use a simple coroutine monad. If you've never seen this before, you might…
Supposedly, all monads can be expressed using Free (if this isn't true, what is a counter-example and why)? How can the continuation monad or its corresponding transformer be expressed using Free or FreeT - what would be the corresponding functor?…
The documentation for Free says:
A number of common monads arise as free monads,
Given data Empty a, Free Empty is isomorphic to the Identity monad.
Free Maybe can be used to model a partiality monad where each layer represents running the…
I think I've come up with an interesting "zippy" Applicative instance for Free.
data FreeMonad f a = Free (f (FreeMonad f a))
| Return a
instance Functor f => Functor (FreeMonad f) where
fmap f (Return x) = Return (f x)
…
Can there be mtl-like mechanism for monad transformers created by FreeT / ProgramT ?
My understanding of the history is as follows. Once upon a time monad transformer was invented. Then people started to stack monad transformers one on other, then…
Given a free monad DSL such as:
data FooF x = Foo String x
| Bar Int x
deriving (Functor)
type Foo = Free FooF
And a random interpreter for Foo:
printFoo :: Foo -> IO ()
printFoo (Free (Foo s n)) = print s >> printFoo n
printFoo…
http://hackage.haskell.org/package/free in Control.Monad.Free.Free allows one to get access to the "free monad" for any given Functor. It does not, however, have a MonadFix instance. Is this because such an instance cannot be written, or was it…
I'm learning about the Free monad in Scala, and I've put together a simple example of algebra that I can lift into a Free monad using cats.
Here's my algebra
sealed trait ConsultationOp[A]
object consultation {
case class Create(c: Consultation)…
The streaming package offers a zipsWith function
zipsWith
:: (Monad m, Functor h)
=> (forall x y. f x -> g y -> h (x, y))
-> Stream f m r -> Stream g m r -> Stream h m r
and a slightly more streamlined version,
zipsWith'
:: Monad m
=>…