Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the free monads vs mtl debate?

Tags:

haskell

monads

I have seen this post which gives sort of an abstract characterization of what free monads are. I also understand what Monad Transformers are, and I understand (to some extent) why they could be useful.

I am not aware of what free monads are used for or what monad transformer libraries are. I have also heard about the mtl vs free monad debate, but I am not sure what it is, as I cannot find any discussion on the internet about this.

Can somebody explain what is this controversy about?

like image 276
Agnishom Chattopadhyay Avatar asked May 26 '18 05:05

Agnishom Chattopadhyay


1 Answers

Probably they in fact mean Freer monad (paper, package) which are, as far as I understand, basically same thing as monad transformers with slightly another interface and having some some part of their implementation shared.

It has only one monadic type Eff r v, where r is a magical type which, as far as I understand, is a heterogenous list of stored data. To add a new transformer, you only need to define its core logic, and you don't have to define any new instances.

This is, for example, how much is needed to define State (the code is copypasted from package and subject to its license, BSD-3-Clause):

data State s v where
  Get :: State s s
  Put :: !s -> State s ()

get :: Member (State s) r => Eff r s
get = send Get

put :: Member (State s) r => s -> Eff r ()
put s = send (Put s)

runState :: Eff (State s ': r) w -> s -> Eff r (w,s)
runState (Val x) s = return (x,s)
runState (E u q) s = case decomp u of
  Right Get      -> runState (qApp q s) s
  Right (Put s') -> runState (qApp q ()) s'
  Left  u'       -> E u' (tsingleton (\x -> runState (qApp q x) s))

I'm not sure if this way offers any practical advantage over monad transformers, as they are already written.

like image 142
max630 Avatar answered Oct 13 '22 03:10

max630