(* Base monad type, to be used throughout *) (* $Id$ *) type ('p,'v) monad = 's -> ('s -> 'v -> 'w) -> 'w constraint 'p = let ret (a :'v) : ('p,'v) monad = fun s k -> k s a let (let!) (m : ('p,'v) monad) (f : 'v -> ('p,'u) monad) : ('p,'u) monad = fun s k -> m s (fun s' b -> f b s' k) let k0 _ v = v (* Initial continuation -- for `reset' and `run' *) (* running our monad; need to pass the init state *) let runM m = fun s0 -> m s0 k0 (* The monad has 2 parts: the continuation and the state. For the state part, we only use 2 morphisms *) let fetch s k = k s s and store v _ k = k v () (* sequentially runs monads a and b, passing modified state along *) let liftM2 f a b = let! v1 = a in let! v2 = b in ret (f v1 v2) (* identity functions *) let id x = x let idM x = ret x