[Haskell-cafe] free vs. operational vs. free-operational

Nickolay Kudasov nickolay.kudasov at gmail.com
Sat Nov 30 22:33:03 UTC 2013


>
> ​​
> Well, that they are heavily used in practice does not mean that they are
> actually useful in practice. The thing is that as soon as your functor is a
> monad, I don't think you really need the Free type anymore -- your
> instruction type is already a monad.


I don't​​ use that myself, so I leave this for others to answer. But you
should note that `Free m` is not the same as `m`: e.g. if `m` is a
probability monad `newtype P a = P [(Double, a)]`, then `Free P` gives you
much more: the whole tree of probabilities (not only probs of final
results), so one could traverse that tree. So I believe `Free m` is rather
useful (as is deriving instances for `Free m` the way it is).

(But I think it will be impossible for MonadCont).


It is. See https://github.com/ekmett/free/pull/33 for FreeT. FT has the
instance in HEAD already.

Almost, but not quite. The key qualification is "while still allowing
> pattern matching".


You're​​ right. But I think it is unnecessary for a library user to pattern
match on F's structure. It is pattern matching on supplied functor that
matters. And that ability is not lost.

To summarize, I currently don't see what 'free' offers that the
> 'operational' package can't do equally well with only 11 exported symbols.


As far as I can tell, while with operational you can certainly do more
things, free provides more things for free (these "baked algebraic laws").
free also provides some other interesting things, like iterative (co)monad
trasformers, cofree comonads and free applicatives/alternatives (which are
out of operational/free common area).

That all said, I don't feel myself concerned/experienced enough to state
that one package should be preferred to another.

Best,
Nick


2013/12/1 Heinrich Apfelmus <apfelmus at quantentunnel.de>

> Nickolay Kudasov wrote:
>
>> ​In the 'free' approach, I find it unpleasant that some laws are
>>> automatic from the functor type, while others have to be ensured by the
>>> interpreter. That's why 'operational' offers only one way to implement
>>> monads: everything has to be done in the interpreter.
>>>
>>>> As far as I know these instances are heavily used in practice, though they
>> are inconvenient in a way. Perhaps they could be moved in a separate
>> module. On the other hand one could use `FreeT` which derives instances in
>> a different manner.
>>
>
> Well, that they are heavily used in practice does not mean that they are
> actually useful in practice. The thing is that as soon as your functor is a
> monad, I don't think you really need the Free type anymore -- your
> instruction type is already a monad.
>
>
>  If you look at the transformer version  Control.Monad.Trans.Free , you
>>> will see that there are no MonadState instances -- as expected, because
>>> you
>>> have to specify the interaction of effects.
>>>
>>
>> Some instances​​ are present in HEAD [1], just not on hackage yet. Some
>> other instances (MonadCont [2], MonadWriter [3]) are waiting for Edward
>> Kmett's approval.
>>
>
> Ah, I see now. Yes, it is possible for all classes that don't have control
> operations. (But I think it will be impossible for MonadCont).
>
> And looking at my 'operational' package, it appears that  ProgramT already
> includes the desired  MonadState  and  MonadIO  instances! In other words,
> 'operational' had always included proper support for monad transformer
> stacks.
>
> (The  MonadReader  class has a control operation, local , but looking at
> the source for  FreeT , it appears that it can actually be lifted. Amazing!)
>
>  Note that `Free` does not have "the true" set of mtl instances. While
>> these
>> instances (derived from underlying functor) are heavily used in practice
>> for `Free`, `FreeT` suggests deriving instances from the transformed monad
>> (not underlying functor). It turns out the latter can be done for the most
>> part of MonadX instances (MonadWriter instance is somewhat questionable).
>> See some comments in [4].
>>
>> That said, as we saw, Free can give you some laws automatically. However,
>>
>>> this also has a drawback: Program has an optimization that Free can never
>>> have. Namely, Program gives you a (>>=) that can be used in a
>>> left-associative way (Think (((x ++ y) ++ z) ++ w) ) while still allowing
>>> pattern matching.
>>>
>>
>> As far as I can tell, this corresponds to church encoded versions of
>> `Free`
>> and `FreeT`, namely `F` and `FT`​​.
>> This is possible due to the work "Asymptotic Improvement of Computations
>> over Free Monads" by Janis Voightländer [5] and based on Edward Kmett's
>> "Free
>> Monads for Less" series of articles [6,7]. `F` is on hackage already and
>> `FT` is in HEAD.
>>
>
> Almost, but not quite. The key qualification is "while still allowing
> pattern matching". The church encoding is akin to difference lists: you get
> O(1) (++), but now  head  and  tail  are  O(n) .
>
> In contrast, Program represents lists of instructions in a way similar to
>
>    data List a = Nil | One a | Concat (List a) (List a)
>
> This gives you O(1) append and head / tail if used in an ephemeral
> fashion. (It is actually possible to turn this into a genuine O(1) head and
> tail, but it's not worth it.) You can't do this optimization in Free.
>
>
> To summarize, I currently don't see what 'free' offers that the
> 'operational' package can't do equally well with only 11 exported symbols.
>
>
> Best regards,
> Heinrich Apfelmus
>
> --
> http://apfelmus.nfshost.com
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131201/d3a95bb1/attachment.html>


More information about the Haskell-Cafe mailing list