-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: use effect syntax from OCaml 5.3 #33
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,35 +11,25 @@ end | |
|
|
||
| module Make (State : Sigs.Type) = | ||
| struct | ||
| type _ Effect.t += | ||
| | Get : State.t Effect.t | ||
| | Set : State.t -> unit Effect.t | ||
| type _ eff += | ||
| | Get : State.t eff | ||
| | Set : State.t -> unit eff | ||
|
|
||
| let get () = Effect.perform Get | ||
| let set st = Effect.perform (Set st) | ||
|
|
||
| let run ~(init:State.t) f = | ||
| let open Effect.Deep in | ||
| let st = ref init in | ||
| try_with f () | ||
| { effc = fun (type a) (eff : a Effect.t) -> | ||
| match eff with | ||
| | Get -> Option.some @@ fun (k : (a, _) continuation) -> | ||
| continue k !st | ||
| | Set v -> Option.some @@ fun (k : (a, _) continuation) -> | ||
| st := v; continue k () | ||
| | _ -> None } | ||
| try f () with | ||
| | effect Get, k -> continue k !st | ||
| | effect Set v, k -> st := v; continue k () | ||
|
|
||
| let try_with ?(get=get) ?(set=set) f = | ||
| let open Effect.Deep in | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonsterling Yes. See ocaml/ocaml#13511 for my Thought on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that is a very nice Thought.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonsterling It's also why we have these two helper functions: https://github.com/RedPRL/algaeff/blob/main/src/Fun.ml It will be great if the compiler can do it directly, avoiding the overhead of creating stupid closures. |
||
| try_with f () | ||
| { effc = fun (type a) (eff : a Effect.t) -> | ||
| match eff with | ||
| | Get -> Option.some @@ fun (k : (a, _) continuation) -> | ||
| continue k (get ()) | ||
| | Set v -> Option.some @@ fun (k : (a, _) continuation) -> | ||
| set v; continue k () | ||
| | _ -> None } | ||
| try f () with | ||
| | effect Get, k -> continue k (get ()) | ||
| | effect Set v, k -> set v; continue k () | ||
|
|
||
| let modify f = set @@ f @@ get () | ||
|
|
||
|
|
||