[Haskell-cafe] MTL vs Free-monads, what are your experiences

Christopher Allen cma at bitemyapp.com
Wed Oct 19 20:53:22 UTC 2016


Oh fair enough, I usually call the partially concreted or lensy style
"mtl-style" and then call MonadReader/MonadState "mtl library". I lean
to the former and usually use the latter if it discharges something I
need right then and there. I've only used Free for something I needed
to aggressively simulate/mock, and even then, some production uses of
Free have gotten defenestrated in favor of something more ordinary.

On Wed, Oct 19, 2016 at 3:47 PM, Richard Wallace
<rwallace at thewallacepack.net> wrote:
> I would call the approach in quine a lensy-mtl style. It's ok as far as it
> goes, but since you are using concrete environment values it isn't great if
> you want to do testing of things like database code without having a "real"
> backend hooked up. The typical approach then is to create your own
> type-class and instances
>
>   class MyBackend where ...
>
>   instance (MonadReader r m, HasDb r) => MyBackend m where ...
>
>   instance (MonadState s m, HasTestState s) => MyBackend m where ...
>
> Of course, now our problem is that our module with this abstraction depends
> on the module with the db and the test state. Unless we create orphan
> instances, which I prefer to avoid. This is one area where I like the Free
> monad approach more because the interpreter can be built and composed with
> other interpreters in completely separate modules or packages because they
> are just values.
>
> Rich
>
> PS for the record, I don't strongly prefer the mtl style or the free monad
> style, I think they each have good qualities and bad and which one I choose
> tends to depend on other factors.
>
> On Wed, Oct 19, 2016 at 10:26 AM, Christopher Allen <cma at bitemyapp.com>
> wrote:
>>
>> It's not really more direct. It's an unordered collection of effects
>> you can use. IME it's a less efficient mtl-style, but YMMV.
>>
>> Taking an example from a PureScript tutorial:
>>
>> func :: Eff (console :: CONSOLE, random :: RANDOM) Unit
>>
>> Can just as easily be:
>>
>> func :: (MonadConsole m, MonadGimmeRandom m) => m ()
>>
>> (mangled name so it doesn't overlap with a real class)
>>
>> There are other differences, but they haven't amounted to much for me yet.
>>
>> Kmett's Quine has a good example of some homespun mtl-style:
>> https://github.com/ekmett/quine
>>
>> On Wed, Oct 19, 2016 at 12:17 PM, Will Yager <will.yager at gmail.com> wrote:
>> > Can anyone comment on the use of Purescript-style effect monads as
>> > compared to MTL and Free? While I have not used them in practice, they seem
>> > to express the "intent" of monad composition a bit more directly than the
>> > approaches we use in Haskell.
>> >
>> > Cheers,
>> > Will
>> > _______________________________________________
>> > Haskell-Cafe mailing list
>> > To (un)subscribe, modify options or view archives go to:
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> > Only members subscribed via the mailman list are allowed to post.
>>
>>
>>
>> --
>> Chris Allen
>> Currently working on http://haskellbook.com
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>
>



-- 
Chris Allen
Currently working on http://haskellbook.com


More information about the Haskell-Cafe mailing list