Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Haskell ListT, LogicT and ChoiceT

What is the difference between these three monad transformers?

  • ListT
  • LogicT
  • ChoiceT
like image 307
Mohan Avatar asked Jan 02 '13 16:01

Mohan


People also ask

What is a Monad Haskell?

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.

Is list a Monad Haskell?

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.

Why are monads useful?

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.


1 Answers

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.

like image 119
ertes Avatar answered Sep 29 '22 12:09

ertes