Last active
December 17, 2019 17:43
-
-
Save mlms13/4b32e83740166d97cf2b7a00519252ae to your computer and use it in GitHub Desktop.
Understanding that module example from the PR
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
module type Monad = | |
sig | |
type 'a t | |
val map : ('a -> 'b) -> 'a t -> 'b t | |
val apply : ('a -> 'b) t -> 'a t -> 'b t | |
val pure : 'a -> 'a t | |
val bind : 'a t -> ('a -> 'b t) -> 'b t | |
end | |
module Option : Monad with type 'a t = 'a option = struct | |
type 'a t = 'a option | |
let map f = | |
function | |
| Some v -> Some (f v) | |
| None -> None | |
let apply optf opta = | |
match (optf, opta) with | |
| (Some f , Some a) -> Some (f a) | |
| _ -> None | |
let pure x = Some x | |
let bind opt f = | |
match opt with | |
| Some x -> f x | |
| None -> None | |
end | |
module type Join = functor (M : Monad) -> sig | |
val v : 'a M.t -> 'b M.t -> ('a* 'b) M.t | |
end | |
module Join = functor (M : Monad) -> struct | |
let v x y = M.bind x (fun x -> M.map (fun y -> (x, y)) y) | |
end | |
let join: (module Join) = (module Join) | |
let join_option x y = | |
let (module Join) = join in | |
let module M = Join(Option) in | |
M.v x y | |
let _ = join_option (Some 3) (Some "a") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment