Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom printers for unhandled effects #19

Merged
merged 6 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Reader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sig
val read : unit -> env
val scope : (env -> env) -> (unit -> 'a) -> 'a
val run : env:env -> (unit -> 'a) -> 'a
val register_printer : ([`Read] -> string option) -> unit
end

module Make (P : Param) =
Expand All @@ -30,4 +31,8 @@ struct
| _ -> None }

let scope f c = run ~env:(f @@ read ()) c

let register_printer f = Printexc.register_printer @@ function
| Effect.Unhandled Read -> f `Read
| _ -> None
end
3 changes: 3 additions & 0 deletions src/Reader.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ sig

val run : env:env -> (unit -> 'a) -> 'a
(** [run t] runs the thunk [t] which may perform reading effects. *)

(**/**)
val register_printer : ([`Read] -> string option) -> unit
end

module Make (P : Param) : S with type env = P.env
Expand Down
5 changes: 5 additions & 0 deletions src/Sequencer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sig

val yield : elt -> unit
val run : (unit -> unit) -> elt Seq.t
val register_printer : ([`Yield of elt] -> string option) -> unit
end

module Make (P : Param) =
Expand All @@ -27,4 +28,8 @@ struct
| Yield x -> Option.some @@ fun (k : (a, _) continuation) ->
Seq.Cons (x, continue k)
| _ -> None }

let register_printer f = Printexc.register_printer @@ function
| Effect.Unhandled (Yield elt) -> f (`Yield elt)
| _ -> None
end
3 changes: 3 additions & 0 deletions src/Sequencer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ sig

val run : (unit -> unit) -> elt Seq.t
(** [run t] runs the thunk [t] which may perform sequencing effects. *)

(**/**)
val register_printer : ([`Yield of elt] -> string option) -> unit
end

module Make (P : Param) : S with type elt = P.elt
Expand Down
6 changes: 6 additions & 0 deletions src/State.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sig
val set : state -> unit
val modify : (state -> state) -> unit
val run : init:state -> (unit -> 'a) -> 'a
val register_printer : ([`Get | `Set of state] -> string option) -> unit
end

module Make (P : Param) =
Expand All @@ -37,4 +38,9 @@ struct
| _ -> None }

let modify f = set @@ f @@ get ()

let register_printer f = Printexc.register_printer @@ function
| Effect.Unhandled Get -> f `Get
| Effect.Unhandled (Set state) -> f (`Set state)
| _ -> None
end
3 changes: 3 additions & 0 deletions src/State.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ sig

val run : init:state -> (unit -> 'a) -> 'a
(** [run t] runs the thunk [t] which may perform state effects. *)

(**/**)
val register_printer : ([`Get | `Set of state] -> string option) -> unit
end

module Make (P : Param) : S with type state = P.state
Expand Down
7 changes: 7 additions & 0 deletions src/UniqueID.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sig
val retrieve : id -> elt
val export : unit -> elt Seq.t
val run : ?init:elt Seq.t -> (unit -> 'a) -> 'a
val register_printer : ([`Register of elt | `Retrieve of id | `Export] -> string option) -> unit
end

module Make (P : Param) =
Expand Down Expand Up @@ -66,4 +67,10 @@ struct
| Export -> Option.some @@ fun (k : (a, _) continuation) ->
continue k @@ Seq.map snd @@ M.to_seq @@ Eff.get ()
| _ -> None }

let register_printer f = Printexc.register_printer @@ function
| Effect.Unhandled (Insert elt) -> f (`Register elt)
| Effect.Unhandled (Select id) -> f (`Retrieve id)
| Effect.Unhandled Export -> f `Export
| _ -> None
end
3 changes: 3 additions & 0 deletions src/UniqueID.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ sig

@param init The initial storage, which should be the output of some previous {!val:export}.
*)

(**/**)
val register_printer : ([`Register of elt | `Retrieve of id | `Export] -> string option) -> unit
end

module Make (P : Param) : S with type elt = P.elt
Expand Down
1 change: 1 addition & 0 deletions test/TestReader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct
let read () = U.perform ReaderMonad.read
let scope f m = U.perform @@ ReaderMonad.scope f @@ U.run m
let run ~env f = U.run f env
let register_printer _ = ()
end

type cmd = ReadAndPrint | Scope of (int -> int) * prog
Expand Down
1 change: 1 addition & 0 deletions test/TestSequencer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct
type elt = int
let yield x = U.perform (SequencerMonad.yield x)
let run f = output_to_seq @@ snd @@ U.run f
let register_printer _ = ()
end

type cmd = Yield of int
Expand Down
1 change: 1 addition & 0 deletions test/TestState.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct
let set s = U.perform @@ StateMonad.set s
let modify f = U.perform @@ StateMonad.modify f
let run ~init f = fst @@ U.run f init
let register_printer _ = ()
end

type cmd = Set of int | GetAndPrint | Mod of (int -> int)
Expand Down