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
11
votes
3 answers

Applicative vs. monadic combinators and the free monad in Scalaz

A couple of weeks ago Dragisa Krsmanovic asked a question here about how to use the free monad in Scalaz 7 to avoid stack overflows in this situation (I've adapted his code a bit): import scalaz._, Scalaz._ def setS(i: Int): State[List[Int], Unit]…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
10
votes
1 answer

Did this construction of free(freer?) monad works?

In the past 2 years, I was interested in using free monad to helping me to solve practical software engineering problem. And came up my own construction of free monad using some elementary category theory. {-# LANGUAGE RankNTypes #-} import…
10
votes
1 answer

Is IO a Free Monad?

In blog posts and examples by Mark Seemann, I got a first glimpse of free monads as a way to structure the boundary between pure code and IO code. My basic understanding is that a free monad lets you build a program (an abstract syntax tree - AST)…
Ulrich Schuster
  • 1,670
  • 15
  • 24
10
votes
1 answer

Understanding the Fix datatype in Haskell

In this article about the Free Monads in Haskell we are given a Toy datatype defined by: data Toy b next = Output b next | Bell next | Done Fix is defined as follows: data Fix f = Fix (f (Fix f)) Which allows to nest Toy expressions by…
Jesuspc
  • 1,664
  • 10
  • 25
10
votes
4 answers

How do I use the Church encoding for Free Monads?

I've been using the Free datatype in Control.Monad.Free from the free package. Now I'm trying to convert it to use F in Control.Monad.Free.Church but can't figure out how to map the functions. For example, a simple pattern matching function using…
Anupam Jain
  • 7,851
  • 2
  • 39
  • 74
10
votes
2 answers

How do I implement Reader using free monads?

Ok, so I have figured out how to implement Reader (and ReaderT, not shown) using the operational package: {-# LANGUAGE GADTs, ScopedTypeVariables #-} import Control.Monad.Operational data ReaderI r a where Ask :: ReaderI r r type Reader r a =…
Luis Casillas
  • 29,802
  • 7
  • 49
  • 102
9
votes
1 answer

The list monad is not a free monad but …

On page 12 of One Monad to Prove Them All, it is written that "a prominent example [of container] is the list data type. A list can be represented by the length of the list and a function mapping positions within the list". At first I thought that…
Bob
  • 1,713
  • 10
  • 23
9
votes
2 answers

Injecting Indexed Functor into Functor Coproduct

I'm trying to work with an indexed free monad (Oleg Kiselyov has an intro). I also want that free monad to be built from a from a coproduct of functors a la Data Types a la carte. However, I'm having trouble getting the coproduct injection type…
JesseC
  • 123
  • 5
9
votes
1 answer

How to encode actions that take monadic arguments with free (or freer) monads?

Most monadic functions take pure arguments and return a monadic value. But there are a few that need also monadic arguments, for example: mplus :: (MonadPlus m) => m a -> m a -> m a finally :: IO a -> IO b -> IO a forkIO :: m () -> m ThreadId --…
Petr
  • 62,528
  • 13
  • 153
  • 317
9
votes
1 answer

Syntax trees: free monad + Bound.Scope

I'm attempting to define an abstract syntax type using ekmett's libraries bound and free. I have something working, which I can strip down to the following minimal example: {-# LANGUAGE DeriveFunctor #-} import Bound import Control.Monad.Free type…
Andy Morris
  • 720
  • 3
  • 10
9
votes
1 answer

Proving the Functor laws for free monads; am I doing it right?

I'm having a bit of a hard time understanding how to prove the Functor and Monad laws for free monads. First off, let me put up the definitions I'm using: data Free f a = Pure a | Free (f (Free f a)) instance Functor f => Functor (Free f) where …
Luis Casillas
  • 29,802
  • 7
  • 49
  • 102
9
votes
2 answers

Applying Semantics to Free Monads

I am trying to abstract the pattern of applying a certain semantics to a free monad over some functor. The running example I am using to motivate this is applying updates to an entity in a game. So I import a few libraries and define a few example…
Vic Smith
  • 3,477
  • 1
  • 18
  • 29
8
votes
1 answer

Defining Free Bind in a way that is compatible with the Free Monad

So we have the free monad: (encoding may vary, but they're all the same) data Free f a = Pure a | Impure (f (Free f a)) instance Functor f => Monad (Free f) where pure = Pure Pure x >>= f = f x Impure x >>= f = Impure…
Justin L.
  • 13,510
  • 5
  • 48
  • 83
8
votes
1 answer

How are free objects constructed?

So I understand that a free object is defined as being the left-hand side of an adjunction. But how does that lead you to the Haskell definition of such objects? More concretely: given a "forgetful functor" from the category of monads to the…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
8
votes
1 answer

Logging using the free monad

This question is related to this article The idea is to define a DSL for manipulating files in the cloud, and define a composition of interpreters that take care of the different aspects, such as communication with the REST interface and…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
1 2
3
10 11