Created
March 6, 2018 13:16
-
-
Save keigoi/a49d2ff7662b2773feb173c06484ae03 to your computer and use it in GitHub Desktop.
metaocaml monad
This file contains 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 M : sig | |
type 'a t | |
val return : 'a code -> 'a t | |
val (>>=) : 'a t -> ('a code -> 'b t) -> 'b t | |
val go : unit t -> unit code | |
end = struct | |
type 'a t = ('a -> unit) code -> unit code | |
let return = fun a k -> .< .~k .~a >. | |
let (>>=) m f = fun k -> .< .~(m .< fun x -> .~(f .< x >. k) >. ) >. | |
let go : unit t -> unit code = fun m -> m .< fun () -> () >. | |
end | |
open M | |
let m = | |
return .< 10 >. >>= fun x -> | |
return .< 20 >. >>= fun y -> | |
return .< print_int (.~x + .~y) >. | |
;; | |
Runcode.run (go m);; | |
(* module M : sig *) | |
(* type ('p, 'q, 'a) t *) | |
(* val return : 'a code -> ('p, 'p, 'a) t *) | |
(* val (>>=) : ('p, 'q, 'a) t -> ('a code -> ('q, 'r, 'b) t) -> ('p, 'r, 'b) t *) | |
(* end = struct *) | |
(* type ('p,'q,'a) t = ('a -> 'p) code -> 'q code *) | |
(* let return = fun a k -> .< .~k .~a >. *) | |
(* let (>>=) m f = fun k -> .< .~(m .< fun x -> .~(f .< x >. k) >. ) >. *) | |
(* end *) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment