Arc Forumnew | comments | leaders | submitlogin
Yield = exceptions + non-determinism (okmij.org)
2 points by akkartik 5245 days ago | 4 comments


1 point by akkartik 5245 days ago | link

Throwing this here in case somebody who knows more haskell or category theory than me is moved to summarize it.

-----

3 points by rocketnia 5244 days ago | link

I have limited experience with this, but I'll take my best shot.

In Haskell, programs are built up out of pure functions (duh ^_^ ). A computation with side effects is represented as a data structure, a monad, which can be traversed just like any data structure. Executing a monad is just traversing it in a way that simulates the world model the monad is designed for.

Monad traversers come in various forms. The Haskell runtime itself traverses the IO monad. Oftentimes only a single traverser is used for any given kind of monad, like runState for the State monad. It's also useful to implement a new monad by translating it to an existing one and relying on that one's traversal to do most of the work (and in fact, this is pretty much necessary for any computation which needs to do things only the Haskell runtime's IO monad traverser can do).

The monad model supports many kinds of side effects, including nonlocal returns/exceptions (where the traverser stops traversing early) and nondeterminism (where the traverser has multiple choices for how to continue). A generator's "yield" operator is an example of an impure operation which not only suspends the calculation early, but also gives the calling code the option of whether or not to continue it. It can be implemented in a monad which combines those two techniques, and that's what they do here.

-----

3 points by akkartik 5244 days ago | link

Thanks!

-----

1 point by rocketnia 5242 days ago | link

Thanks indeed! ^_^ I've gotta thank myself for my own comment, 'cause suddenly I think I understand how to take various side-effect-like concepts I've been interested in and actually turn them into Haskell monads. XD Choose-your-own-adventure monads, FSM monads, and static single-assignment monads, here I come, maybe....

-----