Skip to content

HeinrichApfelmus/operational

Repository files navigation

Hackage

Operational is a tiny library for implementing monads by specifying the primitive instructions and their operational semantics. The monad laws will hold automatically. It can also be used to define monad transformers where the lifting laws hold automatically.

Using operational semantics simplifies the implementation of monads with tricky control flow, such as:

  • web applications in sequential style
  • games with a uniform interface for human and AI players, and automatic replay
  • fast parser monads
  • monadic DSLs of any kind
  • ...

Any monad and monad transformer can be implemented in this fashion.

The library is based on the article The Operational Monad Tutorial, published in Issue 15 of The Monad.Reader.

Example

For example, imagine that you want to write a web application where the user is guided through a sequence of tasks ("wizard"). To structure your application, you can use a custom monad that supports an instruction askUserInput :: CustomMonad UserInput. This command sends a web form to the user and returns a result when he submits the form. However, you don't want your server to block while waiting for the user, so you have to suspend the computation and resume it at some later point. Sounds tricky to implement? This library makes it easy:

The idea is to identify a set of primitive instructions and to specify their operational semantics. Then, the library makes sure that the monad laws hold automatically. In the web application example, the primitive instruction would be AskUserInput.

The above example is implemented in WebSessionState.lhs.

Documentation

More documentation and examples are included in the doc/ folder.

Related

Sources and inspiration for this library include

Related packages included

About

Implement monads by specifying instructions and their desired operational semantics.

Topics

Resources

License

Stars

Watchers

Forks