20

I heard this term a few times, but I still don't know what exactly is a so-called "Freer Monad". The name makes me think about Free Monads, but I don't see how they are actually related. There is some library I found on hackage: http://hackage.haskell.org/package/freer, but the example out there didn't help me a lot.

I don't understand the idea at all, and therefore I don't see any good usecases for them. I also wonder what advantages they provide over free monads and classic mtl stacks.

8
  • Did you visit the links in the readme?
    – Bergi
    Sep 2, 2019 at 14:53
  • More examples are on the related paper.
    – chi
    Sep 2, 2019 at 14:53
  • 16
    In short: a free monad turns any functor into a monad. A freer monad turns anything with kind Type -> Type into a monad, whether that thing is a functor or not.
    – chepner
    Sep 2, 2019 at 15:48
  • 1
    Importantly, freer is used to provide an effect system as a library - a potential replacement for MTL style monad transformers and classes which are great but required N^2 instances and a full set of N new instances for every new monad. Sep 2, 2019 at 18:34
  • 1
    Freer Monads are an alternative to Monad transformer stacks. Effects are defined as data which could have multiple "interpretations". An interpretation performs the task and converts the Effect to the Freer monad. For example, a ReadText effect could read text from memory, file, database etc depending on the interpretation being used. This makes testing effortless. This also decouples business logic from implementation details neatly. Also different effects compose together beautifully when implemented with Freer monads. Jul 7, 2020 at 23:54

1 Answer 1

5

I know this is an old thread, but i thought I'd answer it anyway just in case

what [...] is a so-called "Freer Monad"

according to the original paper Freer Monads, More Extensible Effects a "Freer Monad" is essentially a Free Monad without the necessary Functor constraint of a Free Monad.

A free monad is basically the essence of the monadic structure; the "smallest" thing that is still a monad. A very nice practial explanation approach can be found in this article. This article also shows that the "normal" free monad needs a Functor constraint.

However, it is often quite tedious adding the functor constraint in every function (and sometimes maybe even weird to implement), and as it turns out, by "moving the functor functionality" to an argument for the Impure constructor so that the implementing side can alter the type of the output itself (so without a general functor), it is possible to get rid of this constraint. This is done by using GADTs: (example from the Freer Monads paper)

data Free f a = Pure a
              | Impure (f (Free f a))

instance Functor f => Monad (Free f) where 

becomes

data FFree f a where
    Pure :: a → FFree f a
    Impure :: f x → (x → FFree f a) → FFree f a

instance Monad (FFree f) where 
    [...]
    Impure fx k’ >>= k = Impure fx (k’ >>> k)

This basically lets the later implementation choose how to perform the fmap operation fixed [pun not intended] to the appropriate "output/wrapped type". So the fundamental difference is essentially usability and generality.

As there was some confusion: FFree is the Freer monad and corresponds to Eff in the package freer-simple.

good usecases for them

Freer monads, just as well as Free monads lend themselves for constructing DSLs.

consider for example a type

data Lang r where
    LReturn     :: Var -> Lang Int
    LPrint      :: IntExpr -> Lang ()
    LAssign     :: Var -> IntExpr -> Lang ()
    LRead       :: Var -> Lang Int

this tells me that there are a couple of operations to be performed in Lang: return x print x assign x y read y.

We use GADTs here so that we can also specify what output the individual actions are going to have. This comes in quite handy if we write functions in our DSL, because their output can be tpechecked.

adding some convenience functions (that can acutally be derived):

lReturn :: Member Lang effs
        => Var -> Eff effs Int
lReturn = send . LReturn

lPrint  :: Member Lang effs
        => IntExpr -> Eff effs ()
lPrint  = send . LPrint

lAssign :: Member Lang effs
        => Var -> IntExpr -> Eff effs ()
lAssign v  i = send $ LAssign v i

lRead   :: Member Lang effs
        => Var -> Eff effs Int
lRead   = send . LRead

(this is already written using freer)

now we can use them like this: (assuming that IntExpr contains Variables and Ints)

someFunctionPrintingAnInt = do
    lAssign (Var "a") (IE_Int 12)
    lPrint (IE_Var $ Var "a")

these functions now enable you to have a DSL that can be interpreted in different ways. All needed for this is an interpreter with a specific type for effs (which is ~~ a type level list of freer monad "instances)

so freer takes the idea of the freer monads and packs it into an effect system.

this interpreter could look something like this:

runLangPure :: Eff '[Lang] Int -> Either () Int -- [StateMap]
runLangPure program = fst . fst $
    run (runWriter (runState empty (runError (reinterpret3 go program))))
  where
    go :: Lang v -> Eff '[Error (), State StateMap, Writer [String]] v
    go (LReturn var) = get >>= go (Eval stmt) >>= tell . []
    go (LPrint expr) = do
        store <- get
        value <- evalM expr
        tell [show value]
    go (LAssign var expr) = do
        value <- evalM expr
        --modify state (change var) 
    go (LRead var) = do
        strValue <- getLine
        get >>= insert var (stringToInt strValue)

the run... part specifies the initial "state" of the monads. the go part is the interpreter itself, interpreting the different possible actions.

Note that one can use the functions get and tell in the same do block even though they are part of different monads, which brings us to

I also wonder what advantages do they provide over free monads and classic mtl stacks.

the implementation allows to use monadic actions of different parts of the "monad stack" without lifting.

About the implementation:

To understand this, we look at it from a high level of abstraction: the auxiliary functions of our DSL are send to Eff effs where it is required that Member Lang effs.

So the Member constraint is just a way of declaing that Lang is in the type-level list effs in Member Lang effs. (basically typelevel elem)

The Eff monad has the functionality to "ask" the Members of the type level list of monads whether they can handle the current value (remeber, the operations are just values that are intrepreted subsequently). if so their intrepretation is executed, if not, the question is handed off to the next monad in the list.

This becomes more intuitive and understandable when spending some time in the freer-simple code base.

9
  • in Impure fx k’ >>= k = Impure fx (k’ >> k), isn't it that k’ :: (x → FFree f a) which means the >> in (k’ >> k) belongs to the functions monad, where the left argument is just ignored? (undefined >> id) 1 returns 1. what am I missing?
    – Will Ness
    Jul 15, 2021 at 12:25
  • also, you use Member and Eff without introducing them. what are they? when you say freer, do you mean freer? Hoogle only finds Member and Eff in the package freer-simple it seems.
    – Will Ness
    Jul 15, 2021 at 12:33
  • 1
    Member is part of polysemy, extensible-effects and freer-simple this is just a way to express that Lang is in the type-level list effs in Member Lang effs. this is used to be polymorphic about the monadic constraints; e.g. you can embed something intended to be intrepreted in [State Store] also in [State Store, Writer String] Jul 15, 2021 at 12:43
  • 1
    Eff is part of extensible-effects and freer-simple. it is essentially the Freer monad named FFree above Jul 15, 2021 at 12:45
  • 2
    I am glad you answered this, even if was almost 2 years. The explanation is clear and informative, thank you for it! I am marking it as accepted, but I'd happily see other interpretations.
    – radrow
    Jul 15, 2021 at 18:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.