MyNixOS website logo
Description

Monads with suspension and arbitrary-spot reentry.

This library implements a monad transformer for suspendable computations, similar and related to free comonads. It allows to write continuation-based web frameworks, command line applications and similar interfaces, where you want to reenter a computation at arbitrary spots.

The ContinueT monad transformer

Quickstart tutorial

This monad transformer allows you to set continuation spots in a computation and reenter it at any of those spots possibly causing different behavior:

label name = continue_ (M.singleton name ())

myComp = do
    label "first"
    liftIO (putStrLn "Hello")
    label "second"
    liftIO (putStrLn "World!")

This computation gives you two spots for reentry, "first" and "second". If you reenter at "first", the whole computation will be run again. If you reenter at "second", only the second putStrLn computation will be run again.

Advanced reentering

The most general way to reenter is to perform a computation when reentering:

addCont (Right False) . M.singleton "abc" $ do
    liftIO (putStrLn "Reentered at spot abc.")
    return True

In the primary run this computation will just return False, but it will also register a continuation spot. When you reenter the computation using that spot, it will return True instead.

Suspension

A ContinueT monad also allows you to suspend a computation. This basically means that the computation is aborted at some spot to be reentered somewhere else (or at the same spot) later. You can do this with the empty combinator as well as the suspend function. Examples TODO.

ContinueT explained

ContinueT takes four arguments:

newtype ContinueT e f m a

To form a monad, e has to be a monoid, f has to be a Plus functor (as defined by the semigroupoids package) and m has to be a monad.

The reentry functor

The most important parameter is the reentry functor f. It represents the type for maps of reentry points. Example:

ContinueT e (Map String) m a

A computation of this type may register reentry points indexed by strings. All registered reentry points are combined according to the functor's Plus instance.

The suspension monoid

Computations may suspend, in which case control is returned to a controller, which may be the application loop or the (<|>) combinator:

c1 <|> c2

This computation runs both c1 and c2 (note: even if neither suspends) and returns the result of the left-most computation that did not suspend.

When a computation suspends it may do so with a value of type e that might communicate the reason for suspending. This type must be a monoid, because multiple branches of a (<|>) composition may suspend, in which case the suspension values are combined according to the Monoid instance of e.

A reasonable suspension monoid is Last SomeException, and since this will likely be a very common choice this library defines a shorthand for it:

type LastEx = Last SomeException
Metadata

Version

0.2.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows