Skip to content

Instantly share code, notes, and snippets.

@keigoi
Created March 6, 2018 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keigoi/a49d2ff7662b2773feb173c06484ae03 to your computer and use it in GitHub Desktop.
Save keigoi/a49d2ff7662b2773feb173c06484ae03 to your computer and use it in GitHub Desktop.
metaocaml monad
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