Created
March 6, 2019 15:02
-
-
Save elnygren/623f24c5cd7d2023eca8cee5c063f765 to your computer and use it in GitHub Desktop.
ReasonML monad proof
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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