Questions tagged [free-monad]

Free monads give a general way of turning functors into monads. They are useful for many tree-like structures and domain specific languages.

Free monads give a general way of turning functors into monads. They are useful for many tree-like structures and domain specific languages.


Free monads on hackage: free

Some useful StackOverflow questions:

Posts about free monads in Haskell:

Posts about free monads in Scala:

161 questions
420
votes
7 answers

What are free monads?

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,…
David
  • 8,275
  • 5
  • 26
  • 36
72
votes
6 answers

The Pause monad

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…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
42
votes
1 answer

What are some motivating examples for Cofree CoMonad in Haskell?

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 ==…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
38
votes
2 answers

Does the free monad always exist?

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…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
38
votes
1 answer

When would I want to use a Free Monad + Interpreter pattern?

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…
Savanni D'Gerinel
  • 2,379
  • 17
  • 27
37
votes
2 answers

Difference between free monads and fixpoints of functors?

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…
Daniel
  • 26,899
  • 12
  • 60
  • 88
34
votes
1 answer

when to use CPS vs codensity vs reflection without remorse in Haskell

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…
illabout
  • 3,517
  • 1
  • 18
  • 39
27
votes
4 answers

How can the continuation monad be expressed using the free monad?

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?…
Petr
  • 62,528
  • 13
  • 153
  • 317
27
votes
4 answers

What monads can be expressed as Free over some 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…
Petr
  • 62,528
  • 13
  • 153
  • 317
23
votes
3 answers

Are free monads also zippily applicative?

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) …
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
22
votes
1 answer

Monad Stack Penetration Classes with Free/Operational Monad Transformers?

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…
nushio
  • 753
  • 3
  • 12
21
votes
5 answers

Is it possible to extend free monad interpreters?

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…
fho
  • 6,787
  • 26
  • 71
21
votes
3 answers

Is it possible to implement MonadFix for `Free`?

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…
singpolyma
  • 10,999
  • 5
  • 47
  • 71
20
votes
1 answer

Stacking monadic effects in a Free Monad in Scala

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)…
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
19
votes
2 answers

Zipping free monad transformers

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 =>…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
1
2 3
10 11