Skip to content

Commit

Permalink
♻️ The effect syntax strikes back
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeta611 committed Dec 21, 2024
1 parent f75b8de commit 2e44e19
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 284 deletions.
4 changes: 2 additions & 2 deletions bin/js/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
(name main)
(modes js)
(preprocess
(pps ppx_jane js_of_ocaml-ppx))
(libraries react_trace base logs logs.fmt fmt fmt.tty js_of_ocaml))
(pps js_of_ocaml-ppx))
(libraries react_trace string_recorder base logs.fmt fmt.tty js_of_ocaml))
4 changes: 2 additions & 2 deletions bin/js/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ let () =
Js.export_all
(object%js
method run (fuel : int) program_str =
(let open Result.Let_syntax in
let%bind prog = parse_program_str program_str in
(let ( let* ) x f = Result.bind x ~f in
let* prog = parse_program_str program_str in
let Interp.{ recording; _ } =
Interp.run
?fuel:(if fuel < 1 then None else Some fuel)
Expand Down
69 changes: 0 additions & 69 deletions bin/js/string_recorder.ml

This file was deleted.

3 changes: 3 additions & 0 deletions bin/js/string_recorder/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name string_recorder)
(libraries react_trace base))
57 changes: 57 additions & 0 deletions bin/js/string_recorder/string_recorder.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
open! Base
open Stdlib.Effect
open Stdlib.Effect.Deep
open React_trace
open Lib_domains
open Concrete_domains
open Interp_effects
include Recorder_intf

type recording = string

let emp_recording = "= Recording =\n"

let event_h (type a b) (f : a -> b) (x : a) :
recording:recording -> b * recording =
match f x with
| v -> fun ~recording -> (v, recording)
| effect Update_st (path, label, (v, q)), k ->
fun ~recording ->
let () = perform (Update_st (path, label, (v, q))) in
let recording =
recording
^ Printf.sprintf "[path %s] Update state %d -> %s\n"
(Sexp.to_string (Path.sexp_of_t path))
label
(Sexp.to_string (sexp_of_value v))
in
continue k () ~recording
| effect Set_dec (path, dec), k ->
fun ~recording ->
let () = perform (Set_dec (path, dec)) in
let recording =
recording
^ Printf.sprintf "[path %s] Set decision %s\n"
(Sexp.to_string (Path.sexp_of_t path))
(Sexp.to_string (sexp_of_decision dec))
in
continue k () ~recording
| effect Enq_eff (path, clos), k ->
fun ~recording ->
let () = perform (Enq_eff (path, clos)) in
let recording =
recording
^ Printf.sprintf "[path %s] Enqueue effect\n"
(Sexp.to_string (Path.sexp_of_t path))
in
continue k () ~recording
| effect Alloc_pt, k ->
fun ~recording ->
let path = perform Alloc_pt in
let recording =
recording
^ Printf.sprintf "Allocate path %s\n"
(Sexp.to_string (Path.sexp_of_t path))
in
continue k path ~recording
| effect Checkpoint _, k -> fun ~recording -> continue k () ~recording
File renamed without changes.
6 changes: 1 addition & 5 deletions bin/native/dune
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
(executable
(public_name react_trace)
(name main)
(preprocess
(pps ppx_jane))
(libraries
react_trace
report_box_recorder
base
logs
logs.fmt
fmt
fmt.tty
printbox
printbox-text))
3 changes: 3 additions & 0 deletions bin/native/report_box_recorder/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name report_box_recorder)
(libraries react_trace base printbox))
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,12 @@ let get_path_from_checkpoint = function

let emp_recording = []

let event_h =
{
retc = (fun v ~recording -> (v, recording));
exnc = raise;
effc =
(fun (type a) (eff : a t) ->
match eff with
| Checkpoint { msg; checkpoint } ->
Some
(fun (k : (a, _) continuation) ~(recording : recording) ->
let pt = get_path_from_checkpoint checkpoint in
let box = path pt in
continue k () ~recording:((msg, box) :: recording))
| _ -> None);
}
let event_h (type a b) (f : a -> b) (x : a) :
recording:recording -> b * recording =
match f x with
| v -> fun ~recording -> (v, recording)
| effect Checkpoint { msg; checkpoint }, k ->
fun ~recording ->
let pt = get_path_from_checkpoint checkpoint in
let box = path pt in
continue k () ~recording:((msg, box) :: recording)
File renamed without changes.
54 changes: 21 additions & 33 deletions lib/default_recorder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,24 @@ type recording = unit

let emp_recording = ()

let event_h =
{
retc = (fun v ~recording -> (v, recording));
exnc = raise;
effc =
(fun (type a) (eff : a t) ->
match eff with
| Update_st (path, label, (v, q)) ->
Some
(fun (k : (a, _) continuation) ~(recording : recording) ->
let () = perform (Update_st (path, label, (v, q))) in
continue k () ~recording)
| Set_dec (path, dec) ->
Some
(fun (k : (a, _) continuation) ~(recording : recording) ->
let () = perform (Set_dec (path, dec)) in
continue k () ~recording)
| Enq_eff (path, clos) ->
Some
(fun (k : (a, _) continuation) ~(recording : recording) ->
let () = perform (Enq_eff (path, clos)) in
continue k () ~recording)
| Alloc_pt ->
Some
(fun (k : (a, _) continuation) ~(recording : recording) ->
let path = perform Alloc_pt in
continue k path ~recording)
| Checkpoint _ ->
Some
(fun (k : (a, _) continuation) ~(recording : recording) ->
continue k () ~recording)
| _ -> None);
}
let event_h (type a b) (f : a -> b) (x : a) :
recording:recording -> b * recording =
match f x with
| v -> fun ~recording -> (v, recording)
| effect Update_st (path, label, (v, q)), k ->
fun ~recording ->
let () = perform (Update_st (path, label, (v, q))) in
continue k () ~recording
| effect Set_dec (path, dec), k ->
fun ~recording ->
let () = perform (Set_dec (path, dec)) in
continue k () ~recording
| effect Enq_eff (path, clos), k ->
fun ~recording ->
let () = perform (Enq_eff (path, clos)) in
continue k () ~recording
| effect Alloc_pt, k ->
fun ~recording ->
let path = perform Alloc_pt in
continue k path ~recording
| effect Checkpoint _, k -> fun ~recording -> continue k () ~recording
Loading

0 comments on commit 2e44e19

Please sign in to comment.