Skip to content

Instantly share code, notes, and snippets.

module state =
type StateM<'s,'a> = abstract Apply : 's -> ('s -> 'a -> 'r) -> 'r
let mkStateM (f : 's -> 's * 'a) : StateM<'s,'a> =
{ new StateM< 's , 'a > with member __.Apply s k = match f s with (s,a) -> k s a }
let runStateM (m : StateM<'s,'a>) : 's -> 's * 'a = fun s ->
m.Apply s (fun s a -> (s,a))
// defined directly to avoid the value restriction.
@Saizan
Saizan / tramp.fs
Created March 20, 2021 10:22
F# trampolining monad
module tramp
// we want to use an existential type, but F# makes that complicated so obj it is.
type Tree = | Bind of Tree * (obj -> Tree)
| Delay of (unit -> Tree)
| Leaf of obj
type FnStack<'a,'b> = | End of ('a -> 'b)
| Cons of ('a -> Tree) * FnStack<obj,'b>