Skip to content

Instantly share code, notes, and snippets.

@elnygren
Created March 6, 2019 15:02
Show Gist options
  • Save elnygren/623f24c5cd7d2023eca8cee5c063f765 to your computer and use it in GitHub Desktop.
Save elnygren/623f24c5cd7d2023eca8cee5c063f765 to your computer and use it in GitHub Desktop.
ReasonML monad proof
/* monad */
type box('t) = Foo('t);
let return = (x) => Foo(x);
let bind = (b, f) => switch b {
| Foo(v) => f(v)
};
/* proof helpers */
let a = 1;
let m = Foo(a);
let f = (x) => Foo(x + 1);
let g = (x) => Foo(x * 2);
/* proof */
let left = bind(return(a), f) == f(a);
let right = bind(m, return) == m;
let assoc = (m |> bind(_, f) |> bind(_, g)) == (m |> bind(_, x => bind(f(x), g)));
/* box is a monad */
assert(left==right==assoc==true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment