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-monad
Filter by
Sorted by
Tagged with
0 votes
0 answers
163 views

Printing free state monad as Haskell code

I've read articles about extending simple-reflect to functions and about representing the State monad as a free monad (here), so now I'd like to try to print some state monads as executable Haskell ...
1 vote
2 answers
94 views

Why this free monad example is failing?

I'm trying to understand how Free monads work in Haskell, and for that I've been trying to make an example work. My code was based on the answer here by Philip JF. Here is the example: data Free f a = ...
1 vote
0 answers
84 views

Free Monads in Julia Programming

I'm learning about Free Monads and I was wondering how one could implement it in the Julia programming language. I've seen the implementation in Haskell, but I'm having a hard time porting it to Julia....
18 votes
2 answers
2k views

Visualizing the Free Monad

I think I have rough idea of what the free monad is, but I would like to have a better way to visualize it. It makes sense that free magmas are just binary trees because that's as "general" as you ...
424 votes
7 answers
69k views

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'...
3 votes
1 answer
182 views

Is it possible to compose multiple interpreters for my Free Monad scala app

So i started to learn free monadic approach in my code style recently, and i hope i get the main idea of it. We have our own dsl to build a program, then we pass a compiler to a real world effect such ...
23 votes
3 answers
812 views

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 (...
2 votes
2 answers
229 views

How to interleave a free monadic DSL with state, but interpret state mid-program?

I have a situation where I want to interleave a free monadic DSL with state, but interpret the state mid-program. Below is a simplified example with only logging and state. More generally the problem ...
2 votes
1 answer
206 views

The identity monad as a free monad

The functor of the identity monad can be defined as: data Identity a = Identity a Because this monad is free, an alternative definition is the following: data Term f a = Pure a | Impure (f (Term f a))...
1 vote
1 answer
232 views

Indexed monads for state machines

I decided to experiment with indexed monads to create a type-safe state machine. That means Haskell will refuse to compile illegal state transitions. My questions are. I created a Door type with ...
1 vote
0 answers
177 views

How to map and flatmap a composition of cats free monad and ZIO

guys. I am currently trying to build a ZIO based application. Question: Is there any way to map and flatmap a composition of cats free monad and ZIO like the example below? // Suppose we have a free ...
1 vote
2 answers
161 views

Understanding the use of bind in free monad

I'm trying to get how free monads are working. During this I get into the monad instance of Free, which is: data Free f a = Pure a | Free (f (Free f a)) instance (Functor f) => Monad (Free f) ...
10 votes
1 answer
322 views

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 ...
9 votes
1 answer
527 views

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 ...
4 votes
1 answer
267 views

How to compose Free Monads

data Console a = PutStrLn String a | GetLine (String -> a) deriving (Functor) type ConsoleM = Free Console runConsole :: Console (IO a) -> IO a runConsole cmd = case cmd of (...
22 votes
1 answer
666 views

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 ...
1 vote
1 answer
115 views

How can I polymorphically interpret an Arrow-like GADT DSL?

TL;DR: How do I write interpreters (run :: Lang a b -> whatever) for my Lang GADT? I have a DSL: data Lang a b where F :: Lang x y G :: Lang x y I want to build programs like: prog :: Lang a ...
2 votes
1 answer
80 views

Using Either in a Free monad

I'm trying to use an Either as the result of an algebra using Free monad, like the following code ///// Command sealed trait Command[A] type Result[A] = Either[Exception, A] object Command {...
1 vote
1 answer
125 views

How to assign a value from the IO monad to a RankNType qualified constructor

(UPDATED) I have made an interface using a Free Monad to a generic data store. I want to place the specific interpreter (:: DataStore a -> IO a) chosen by the user at run time into a state monad ...
0 votes
1 answer
700 views

How to write a DSL based on free monad?

I'm playing with free monads in Haskell and I got stuck with defining the functions that lift the functor constructors in the Free monad. I have the AppF functor as a sum of several functors, each ...
2 votes
0 answers
128 views

Is there a way to derive Lift for "IO a" in Haskell?

For awhile now, whenever people mention the performance hit of free monad + interpreter pattern, I've had this vague idea in my head that: "Why not just use Template Haskell to evaluate your ...
15 votes
1 answer
350 views

Structurally enforced Free Alternative, without left distributivity

There is a nice Free Alternative in the great free package, which lifts a Functor to a left-distributive Alternative. That is, the claim is that: runAlt :: Alternative g => (forall x. f x -> g ...
18 votes
1 answer
348 views

Generate optparse-applicative parser from free alternative functor

Consider the following type signatures: data Foo x = Foo { name :: String , reader :: String -> x } instance Functor Foo where fmap f (Foo n r) = Foo n $ f . r Now I show a natural ...
6 votes
1 answer
184 views

Control.MonadPlus.Free without unnecessary distribution

I'm trying to use a free monad to build an EDSL for constructing AND/OR decision trees like Prolog, with >>= mapped to AND, and mplus mapped to OR. I want to be able to describe something like ...
18 votes
2 answers
343 views

What was wrong with Control.MonadPlus.Free?

The free MonadPlus defined as data Free f a = Pure a | Free (f (Free f a)) | Plus [Free f a] was removed in free 4.6 with the following remark (changelog): Removed Control.MonadPlus.Free. Use ...
16 votes
1 answer
655 views

Is there a Codensity MonadPlus that asymptotically optimizes a sequence of MonadPlus operations?

Recently there was a question about the relation between DList <-> [] versus Codensity <-> Free. This made me think whether there is such a thing for MonadPlus. The Codensity monad improves the ...
9 votes
1 answer
276 views

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 ...
2 votes
1 answer
171 views

Implementing a lexer using the Free Monad

I am thinking about a use case of the free monad which would be a simple lexing DSL. So far I came up with some primitive operations: data LexF r where POP :: (Char -> r) -> LexF r PEEK :: (...
1 vote
1 answer
559 views

Using the Free Monad in Functional Domain Design

I'm quite new to functional programming. However, I read about the Free Monad, and I'm trying to use it in a toy project. In this project, I model the stock's portfolio domain. As suggested in many ...
13 votes
1 answer
282 views

How to get the “inflexible semantics of monad transformers” using extensible effects?

Consider the following example. newtype TooBig = TooBig Int deriving Show choose :: MonadPlus m => [a] -> m a choose = msum . map return ex1 :: (MonadPlus m, MonadError TooBig m) => m Int ...
37 votes
2 answers
4k views

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 ...
3 votes
2 answers
215 views

How can I merge effect interpreters when using a library like freer-simple?

I am playing around with freer-simple and trying to work out how to combine effects. I have an algebra to represent a simple file system and user invoked failure as follows: data FileSystem r where ...
2 votes
2 answers
287 views

Freer-Simple Freer Monads How do I Unify IO Exception Handling with Error Effect

I am using freer-simple to write a super simple DSL. All it does is read a file. I have one rule regarding file names, they cannot contain the letter x. Any attempt to open a file with the letter x in ...
5 votes
2 answers
647 views

Parsing to Free Monads

Say I have the following free monad: data ExampleF a = Foo Int a | Bar String (Int -> a) deriving Functor type Example = Free ExampleF -- this is the free monad want to discuss I know how ...
2 votes
2 answers
288 views

How to make a free monad interpreter recursive?

In a Free monad interpreter using iterT, I'd like to have an internal state, but I'm not sure how to do so since the iterT function provides the continuation f pre-loaded with the recursive call, as I ...
2 votes
1 answer
201 views

Converting a monadic value to an IO in Polysemy

I am trying to build an automated feature testing suite using webdriver and polysemy in Haskell. I've gotten as far as defining proper effects and interpreting them into a webdriver WD monad, but now ...
4 votes
1 answer
294 views

Extracting multiple IO exceptions into polysemy

Let's say I have some very complex set of computations of the form computation :: IO a that I cannot modify, due to it being from some library code or other reasons. Let's also say I want to provide ...
1 vote
1 answer
100 views

How to add two Algebras in one interpreter in scala using natural transformation

Suppose I have two algebras: sealed trait OneAlgebra[A] case class create() extends OneAlgebra[Unit] case class update() extends OneAlgebra[Unit] sealed trait TwoAlgebra[A] case class add() ...
3 votes
1 answer
164 views

Where is that functor which free monad is supposed to be based on

I'm trying to comprehend the idea of free monad, and I'm stumped on the first sentence of cats's docs stating that A free monad is a construction which allows you to build a monad from any ...
12 votes
1 answer
886 views

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 ...
2 votes
1 answer
67 views

Is it possible to optimise a Free Monad program before execution?

I've recently picked up the Free Monad pattern using cats in an attempt to create a DSL which can be "simplified" before execution. For example, let's say I create a language for interacting with ...
6 votes
2 answers
232 views

Converting this FreeT (explicitly recursive data type) function to work on FT (church encoding)

I'm using the FreeT type from the free library to write this function which "runs" an underlying StateT: runStateFree :: (Functor f, Monad m) => s -> FreeT f (StateT s m) a ->...
2 votes
1 answer
52 views

How to specify the type introduced in a match of class with independent type parameter

I followed the talk given by Runar Bjarnason about free monads: http://functionaltalks.org/2014/11/23/runar-oli-bjarnason-free-monad/ I have the following piece of code from his initial example (I ...
13 votes
1 answer
2k views

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 (...
14 votes
2 answers
740 views

The fixed point functors of Free and Cofree

To make that clear, I'm not talking about how the free monad looks a lot like a fixpoint combinator applied to a functor, i.e. how Free f is basically a fixed point of f. (Not that this isn't ...
8 votes
1 answer
256 views

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 ...
4 votes
1 answer
893 views

Are free monads just monads with interfaces?

I've been reading some materials on free monads and I don't really think I'm much closer to implementation but I think I am closer to understanding what they are! Given a lot of the above resources, ...
7 votes
2 answers
431 views

Analog of free monads for Profunctors

We can define data Free f a = Pure a | Free (f (Free f a)) and so have Functor f => Monad (Free f). If we define data T f a b = R a | S b | T (f a (T f a b)) have we some analogous M so Profunctor ...
2 votes
0 answers
112 views

Confuse about free monad lifting and interpreter, example of binary tree dsl implement

i'm following the article stackless scala with free monads, and try to write my own code. the function and common data type always confused me while lifting sealed trait Free[F[_], A] { import ...
11 votes
4 answers
2k views

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 ...