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 2 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
4 changes: 4 additions & 0 deletions src/Reader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ struct
| _ -> None }

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

let register_printer ?read () = Printexc.register_printer @@ function
| Effect.Unhandled Read -> read
| _ -> None
end
2 changes: 2 additions & 0 deletions src/Reader.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ sig

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

val register_printer : ?read:string -> unit -> unit
favonia marked this conversation as resolved.
Show resolved Hide resolved
end

module Make (P : Param) : S with type env = P.env
Expand Down
4 changes: 4 additions & 0 deletions src/Sequencer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ struct
| Yield x -> Option.some @@ fun (k : (a, _) continuation) ->
Seq.Cons (x, continue k)
| _ -> None }

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

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

val register_printer : ?yield:(elt -> string) -> unit -> unit
favonia marked this conversation as resolved.
Show resolved Hide resolved
end

module Make (P : Param) : S with type elt = P.elt
Expand Down
5 changes: 5 additions & 0 deletions src/State.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ struct
| _ -> None }

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

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

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

val register_printer : ?get:string -> ?set:(state -> string) -> unit -> unit
favonia marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -66,4 +66,11 @@ struct
| Export -> Option.some @@ fun (k : (a, _) continuation) ->
continue k @@ Seq.map snd @@ M.to_seq @@ Eff.get ()
| _ -> None }

let register_printer ?register ?retrieve ?export () = Printexc.register_printer @@ function
| Effect.Unhandled (Insert elt) -> Option.map (fun f -> f elt) register
| Effect.Unhandled (Select id) -> Option.map (fun f -> f id) retrieve
| Effect.Unhandled Export -> export
| _ -> None

mmcqd marked this conversation as resolved.
Show resolved Hide resolved
end
2 changes: 2 additions & 0 deletions src/UniqueID.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ sig

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

val register_printer : ?register:(elt -> string) -> ?retrieve:(id -> string) -> ?export:string -> unit -> 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 ?read:_ () = ()
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 ?yield:_ () = ()
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 ?get:_ ?set:_ () = ()
end

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