What is the difference between these three monad transformers?
What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
List is also a monad. We have seen that the Maybe type constructor is a monad for building computations which may fail to return a value. You may be surprised to know that another common Haskell type constructor, [] (for building lists), is also a monad.
monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems.
Well, first of all ListT
is not a true monad transformer. It disobeys the associativity law for certain underlying monads. It is also pretty slow, as is the monadic interface to lists in general. It is built on actual lists internally.
LogicT
is probably the best choice for list-like monad transformers. It not only implements a proper monad transformer, but also some very useful combinators for fair list products.
ChoiceT
is my own work. It is basically just a CPSed version of LogicT
and is inspired by both LogicT
and the ChoiceT
from monadLib. It's very fast, often outperforming (non-transformed) lists, but the types may be scary and you are bound to the result type, which may be in your way sometimes.
Conclusion: If you're serious, use LogicT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With