Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Algaeff.Unmonad
SourceEffects for any monad.
This is a general construction that uses effects to construct monadic expressions. Here is an alternative implementation of State
using the standard state monad:
module StateMonad =
struct
type state = int
type 'a t = state -> 'a * state
let ret x s = x, s
let bind m f s = let x, s = m s in f x s
let get s = s, s
let set s _ = (), s
let modify f s = (), f s
end
module StateUnmonad =
struct
type state = int
module U = Algaeff.Unmonad.Make (StateMonad)
let get () = U.perform StateMonad.get
let set s = U.perform @@ StateMonad.set s
let modify f = U.perform @@ StateMonad.modify f
let run ~init f = fst @@ U.run f init
end
Note that continuations in OCaml are one-shot, so the list monad will not work; it will quickly lead to the runtime error that the continuation is resumed twice. Also, monads do not mix well with exceptions, and thus the bind
operation should not raise an exception unless it encounters a truly unrecoverable fatal error. Raising an exception within bind
will skip the continuation, and thus potentially skipping exception handlers within the continuation. Those handlers might be crucial for properly releasing acquired resources.