Skip to content

Instantly share code, notes, and snippets.

@Kakadu
Created February 17, 2020 23:07
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 Kakadu/ef3772171330dcf07f635cd408841a61 to your computer and use it in GitHub Desktop.
Save Kakadu/ef3772171330dcf07f635cd408841a61 to your computer and use it in GitHub Desktop.
OCaml reader monad demo ( >= 4.09)
module Reader = struct
type ('r, 'a) t = { reader : 'r -> 'a }
let run_reader { reader } = reader
let reader f = { reader = f }
let return x = { reader = (fun _ -> x) }
let (>>=) x f = reader @@ fun r -> run_reader (f @@ run_reader x r) r
let ask = reader (fun x -> x)
let asks = reader
module Syntax = struct
(* let (and+ ) o1 o2 = product o1 o2*)
let (let* ) x f = x >>= f
(* let (let+ ) x f = x map f x*)
end
end
open Reader
let example2 context : string =
let open Syntax in
let greet : string -> (string,string) Reader.t = fun name ->
let* greeting = ask in
return @@ Printf.sprintf "%s, %s" greeting name
in
let fin : string -> (string,string) Reader.t = fun input ->
let* is_hello = asks ((=) "hello") in
return @@ Printf.sprintf "%s%s" input (if is_hello then "!" else ".")
in
run_reader (greet "James" >>= fin) context
let () = print_endline @@ example2 "Hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment