lux/control/effect

Algebraic effects.

Types

Eff

A Free Monad implementation for algebraic effects.

(type: (Eff F a)
  (#Pure a)
  (#Effect (F (Eff F a))))

Handler

A way to interpret effects into arbitrary monads.

(sig: (Handler E M)
  (: (lux/control/monad;Monad M)
     monad)
   (: (All [c] (-> (E c) (M c)))
     handle))

Macros

doE

## An alternative to the 'do' macro for monads.
(with-handler Handler<EffABC,IO>
  (doE Functor<EffABC>
    [a (lift fieldA)
     b (lift fieldB)
     c (lift fieldC)]
    (wrap ($_ n.+ a b c))))

effect:

## Define effects by specifying which operations and constants a handler must provide.
(effect: #export EffA
  (opA [Nat Text] Bool)
  (fieldA Nat))

## In this case, 'opA' will be a function (-> Nat Text Bool).
## 'fieldA' will be a value provided by a handler.

handler:

## Define effect handlers by implementing the operations and values of an effect.
(handler: _
  (=> EffA [IO Monad<IO>])
  (def: (opA length sample)
    (:: Monad<IO> wrap (n.< length
                            (size sample))))

  (def: fieldA (:: Monad<IO> wrap +10)))

## Since a name for the handler was not specified, 'handler:' will generate the name as Handler<EffA,IO>.

lift

## A way to (automatically) lift effectful fields and operations from simple effects into the larger space of composite effects.
(with-handler Handler<EffABC,IO>
  (doE Functor<EffABC>
    [a (lift fieldA)
     b (lift fieldB)
     c (lift fieldC)]
    (wrap ($_ n.+ a b c))))

|E

## A way to combine smaller effect into a larger effect.
(type: EffABC (|E EffA EffB EffC))

|F

## A way to combine smaller effect functors into a larger functor.
(def: Functor<EffABC>
  (Functor EffABC)
  (|F Functor<EffA> Functor<EffB> Functor<EffC>))

|H

## A way to combine smaller effect handlers into a larger handler.
(def: Handler<EffABC,IO>
  (Handler EffABC lux/codata/io;IO)
  (|H lux/codata/io;Monad<IO>
      Handler<EffA,IO> Handler<EffB,IO> Handler<EffC,IO>))

Structs

(Applicative<Eff> dsl)

(All [a] (-> (lux/control/functor;Functor a) (lux/control/applicative;Applicative (Eff a))))

(Functor<Eff> dsl)

(All [a] (-> (lux/control/functor;Functor a) (lux/control/functor;Functor (Eff a))))

(Monad<Eff> dsl)

(All [a] (-> (lux/control/functor;Functor a) (lux/control/monad;Monad (Eff a))))

Values

(with-handler handler body)

Handles an effectful computation with the given handler to produce a monadic value.

(All [a b c] (-> (Handler a b) (Eff a c) (b c)))