1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
(*
Copyright (c) 2015, KC Sivaramakrishnan <sk826@cl.cam.ac.uk>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
(* Monadic Reflection :
http://www.cs.ioc.ee/mpc-amast06/msfp/filinski-slides.pdf *)
open Effect
open Effect.Deep
(* The monad signature *)
module type MONAD = sig
type +_ t
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end
(* Build reify and reflect operations for any monad *)
module RR (M : MONAD) : sig
val reify : (unit -> 'a) -> 'a M.t
val reflect : 'a M.t -> 'a
end = struct
type _ Effect.t += E : 'a M.t -> 'a Effect.t
let reify f =
match_with
f
()
{ retc = (fun x -> M.return x)
; exnc = raise
; effc =
(fun (type a) (e : a Effect.t) ->
match e with
| E m -> Some (fun k -> M.bind m (continue k))
| _ -> None)
}
let reflect m = perform (E m)
end
(* The state monad *)
module State = struct
type 'a t = int -> int * 'a
let return v s = s, v
let bind m k s =
let s, a = m s in
k a s
let get s = s, s
let put s _ = s, ()
let run s ~init = s init
end
(* Reify and reflect for State *)
module StateR = RR (State)
(* val put : int -> unit State.t *)
let put v = StateR.reflect (State.put v)
(* val get : unit -> int State.t *)
let get () = StateR.reflect State.get
(* val run_state : (unit -> 'a) -> init:int -> 'a *)
let run_state f ~init =
let final, v = State.run (StateR.reify f) ~init in
Printf.printf "Final state: %d\n" final;
v
(* The exception monad *)
module Exception = struct
type 'a t =
| Ok of 'a
| Exn of exn
let return v = Ok v
let bind m k =
match m with
| Ok v -> k v
| Exn e -> Exn e
let raise exn = Exn exn
let run m ~catch =
match m with
| Ok v -> v
| Exn e -> catch e
end
(* Reify and reflect for Exception *)
module ExceptionR = RR (Exception)
(* val raise : exn -> 'a *)
let raise e = ExceptionR.reflect (Exception.raise e)
(* val run_exception : (unit -> 'a) -> catch:(exn -> 'a) -> 'a *)
let run_exception m ~catch = Exception.run (ExceptionR.reify m) ~catch
(* Using the state monad *)
let state_example () =
let initial = get () in
Printf.printf "Initial state: %d\n" initial;
put 10;
assert (get () = 10);
put (get () + 1);
assert (get () = 11);
put 12;
`Initial initial, `Final (get ())
(* Using the exception monad *)
let exception_example () =
Printf.printf "Raising an exception\n";
raise (Failure "An error!") |> ignore;
Printf.printf "This shouldn't be displayed\n"
(* Using both exceptions and state *)
let combined_example () =
Printf.printf "Initial state: %d\n" (get ());
put 100;
raise (Failure "An error!") |> ignore;
put 200
let print_exception e = Printf.printf "Exception: %s\n" (Printexc.to_string e)
let%expect_test _ =
run_state ~init:10 state_example |> ignore;
[%expect {|
Initial state: 10
Final state: 12 |}];
run_exception ~catch:print_exception exception_example;
[%expect {|
Raising an exception
Exception: Failure("An error!") |}];
(run_exception ~catch:print_exception
@@ fun () -> run_state ~init:10 @@ fun () -> combined_example ());
[%expect {|
Initial state: 10
Exception: Failure("An error!") |}];
(run_state ~init:10
@@ fun () -> run_exception ~catch:print_exception @@ fun () -> combined_example ());
[%expect
{|
Initial state: 10
Exception: Failure("An error!")
Final state: 100 |}]
|