[Haskell-cafe] monads with take-out options

Jonathan Cast jonathanccast at fastmail.fm
Mon Nov 24 17:30:03 EST 2008


On Mon, 2008-11-24 at 14:06 -0800, Greg Meredith wrote:
> Haskellians,

> Some monads come with take-out options, e.g.
>       * List
>       * Set
> In the sense that if unit : A -> List A is given by unit a = [a], then
> taking the head of a list can be used to retrieve values from inside
> the monad.

> Some monads do not come with take-out options, IO being a notorious
> example.

> Some monads, like Maybe, sit on the fence about take-out. They'll
> provide it when it's available.

It might be pointed out that List and Set are also in this region.  In
fact, Maybe is better, in this regard, since you know, if fromJust
succeeds, that it will only have once value to return.  head might find
one value to return, no values, or even multiple values.

A better example of a monad that always has a left inverse to return is
((,) w), where w is a monoid.  In this case,

    snd . return = id :: a -> a

as desired (we always have the left inverses

    join . return = id :: m a -> m a

where join a = a >>= id).

> Now, are there references for a theory of monads and take-out options?
> For example, it seems that all sensible notions of containers have
> take-out.

Sounds reasonable.  Foldable gives you something:

  foldr const undefined

will pull out the last value visited by foldr, and agrees with head at [].

> Can we make the leap and define a container as a monad with a notion
> of take-out?

If you want.  I'd rather define a container to be Traversable; it
doesn't exclude anything interesting (that I'm aware of), and is mostly
more powerful.

> Has this been done?

Are you familiar at all with Foldable
(http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html#t%3AFoldable) and Traversable (http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Traversable.html#t%3ATraversable)

jcc




More information about the Haskell-Cafe mailing list