Skip to content

Commit

Permalink
🚚 Move domain-related modules into a package
Browse files Browse the repository at this point in the history
This improves modularity, as well as preprocessing only the
domain-related code via PPX. PPX does not support effect syntax yet, so
the code using the effect syntax should not be PPX-rewritten.
  • Loading branch information
Zeta611 committed Dec 20, 2024
1 parent 40b7ee7 commit f75b8de
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 83 deletions.
1 change: 1 addition & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
profile = default
wrap-comments = true
ocaml-version = 5.3
3 changes: 2 additions & 1 deletion bin/js/main.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open! Base
open React_trace
open Lib_domains

let position (lexbuf : Lexing.lexbuf) : string =
let open Lexing in
Expand Down Expand Up @@ -36,6 +37,6 @@ let () =
prog
in
if Logs.err_count () > 0 then Error "error" else Ok recording)
|> (function Ok s -> s | Error s -> s)
|> ( function Ok s -> s | Error s -> s )
|> Js.string
end)
1 change: 1 addition & 0 deletions bin/js/string_recorder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open! Base
open Stdlib.Effect
open Stdlib.Effect.Deep
open React_trace
open Lib_domains
open Concrete_domains
open Interp_effects
include Recorder_intf
Expand Down
11 changes: 6 additions & 5 deletions bin/native/main.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open! Base
open Stdio
open React_trace
open Lib_domains

let print_position (outx : Out_channel.t) (lexbuf : Lexing.lexbuf) : unit =
let open Lexing in
Expand Down Expand Up @@ -60,11 +61,11 @@ let () =
Arg.parse speclist (fun x -> filename := x) usage_msg;
if String.is_empty !filename then Arg.usage speclist usage_msg
else if !opt_parse_js then failwith "Not implemented"
(* (let js_syntax, _ = Js_syntax.parse !filename in *)
(* print_endline (Js_syntax.show js_syntax); *)
(* let prog = Js_syntax.convert js_syntax in *)
(* Sexp.pp_hum Stdlib.Format.std_formatter (Syntax.Prog.sexp_of_t prog)) *)
else (
(* (let js_syntax, _ = Js_syntax.parse !filename in *)
(* print_endline (Js_syntax.show js_syntax); *)
(* let prog = Js_syntax.convert js_syntax in *)
(* Sexp.pp_hum Stdlib.Format.std_formatter (Syntax.Prog.sexp_of_t prog)) *)
else (
Fmt_tty.setup_std_outputs ();
Logs.set_reporter (Logs_fmt.reporter ());
Logs.set_level (Some !opt_verbosity);
Expand Down
1 change: 1 addition & 0 deletions bin/native/report_box_recorder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Stdlib.Effect
open Stdlib.Effect.Deep
open React_trace
open Interp_effects
open Lib_domains
open Concrete_domains
include Recorder_intf
module B = PrintBox
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions lib/domains/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(library
(name lib_domains)
(preprocess
(pps ppx_jane))
(libraries base logs ppx_jane))

(ocamllex lexer)

(menhir
(modules parser)
(flags --dump))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 46 additions & 6 deletions lib/syntax.ml → lib/domains/syntax.ml
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
open! Base

module Map_key (T : sig
type t

val sexp_of_t : t -> Sexp.t
val t_of_sexp : Sexp.t -> t
val of_string : string -> t
val to_string : t -> string
val ( <= ) : t -> t -> bool
val ( >= ) : t -> t -> bool
val ( = ) : t -> t -> bool
val ( < ) : t -> t -> bool
val ( > ) : t -> t -> bool
val ( <> ) : t -> t -> bool
val compare : t -> t -> int
val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int
val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t
val clamp : t -> min:t -> max:t -> t Or_error.t

type comparator_witness

val comparator : (t, comparator_witness) Comparator.t
val hash : t -> int
val equal : t -> t -> bool
end) =
struct
include T

module Map = struct
open Map
include M (T)

let empty = empty (module T)
let sexp_of_t sexp_of_v t = Map.sexp_of_m__t (module T) sexp_of_v t
end
end

module Id = struct
include Util.Map_key (String)
include Map_key (String)

let unit = "()"
end

module Label = struct
include Util.Map_key (Int)
include Map_key (Int)
end

module Expr = struct
Expand Down Expand Up @@ -62,15 +102,15 @@ module Expr = struct
let rec hook_free (expr : some_expr) : hook_free t option =
let (Ex { desc; loc }) = expr in
let mk = mk ~loc in
let open Option.Let_syntax in
let ( let* ) x f = Option.bind x ~f in
match desc with
| Let ({ body; _ } as e) ->
let%bind body = hook_free (Ex body) in
let* body = hook_free (Ex body) in
Some (mk (Let { e with body }))
| Stt _ | Eff _ -> None
| Seq (e1, e2) ->
let%bind e1 = hook_free (Ex e1) in
let%bind e2 = hook_free (Ex e2) in
let* e1 = hook_free (Ex e1) in
let* e2 = hook_free (Ex e2) in
Some (mk (Seq (e1, e2)))
| (Const _ as e)
| (Var _ as e)
Expand Down
10 changes: 1 addition & 9 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
(library
(name react_trace)
(preprocess
(pps ppx_jane))
(libraries base logs flow_parser ppx_jane))

(ocamllex lexer)

(menhir
(modules parser)
(flags --dump))
(libraries base logs flow_parser lib_domains))
1 change: 1 addition & 0 deletions lib/interp.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open! Base
open Stdlib.Effect
open Stdlib.Effect.Deep
open Lib_domains
open Syntax
open Concrete_domains
open Interp_effects
Expand Down
43 changes: 21 additions & 22 deletions lib/interp_effects.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open! Base
open Stdlib.Effect
open Lib_domains
open Syntax
open Concrete_domains

Expand All @@ -8,43 +8,42 @@ exception Type_error
exception Invalid_phase

(* path and phase effects *)
type _ Stdlib.Effect.t += Rd_pt : Path.t t | Rd_ph : phase t
type _ eff += Rd_pt : Path.t eff | Rd_ph : phase eff

(* environmental effects *)
type _ Stdlib.Effect.t +=
| Rd_env : Env.t t
| In_env : Env.t -> (('b -> 'a) -> 'b -> 'a) t
type _ eff +=
| Rd_env : Env.t eff
| In_env : Env.t -> (('b -> 'a) -> 'b -> 'a) eff

(* memory effects *)
type _ Stdlib.Effect.t +=
| Alloc_addr : obj -> Addr.t t
| Lookup_addr : Addr.t -> obj t
| Update_addr : Addr.t * obj -> unit t
type _ eff +=
| Alloc_addr : obj -> Addr.t eff
| Lookup_addr : Addr.t -> obj eff
| Update_addr : Addr.t * obj -> unit eff

(* tree memory effects in eval/eval_mult *)
type _ Stdlib.Effect.t +=
| Lookup_st : Path.t * Label.t -> (value * Job_q.t) t
| Update_st : (Path.t * Label.t * (value * Job_q.t)) -> unit t
| Get_dec : Path.t -> decision t
| Set_dec : Path.t * decision -> unit t
| Enq_eff : Path.t * clos -> unit t
type _ eff +=
| Lookup_st : Path.t * Label.t -> (value * Job_q.t) eff
| Update_st : (Path.t * Label.t * (value * Job_q.t)) -> unit eff
| Get_dec : Path.t -> decision eff
| Set_dec : Path.t * decision -> unit eff
| Enq_eff : Path.t * clos -> unit eff

(* tree memory effects in render *)
type _ Stdlib.Effect.t +=
| Alloc_pt : Path.t t
| Lookup_ent : Path.t -> entry t
| Update_ent : Path.t * entry -> unit t
type _ eff +=
| Alloc_pt : Path.t eff
| Lookup_ent : Path.t -> entry eff
| Update_ent : Path.t * entry -> unit eff

type checkpoint =
| Retry_start of (int * Path.t)
| Render_check of Path.t
| Render_finish of Path.t
| Effects_finish of Path.t

type _ Stdlib.Effect.t +=
| Checkpoint : { msg : string; checkpoint : checkpoint } -> unit t
type _ eff += Checkpoint : { msg : string; checkpoint : checkpoint } -> unit eff

(* For testing nontermination *)
type _ Stdlib.Effect.t += Re_render_limit : int t
type _ eff += Re_render_limit : int eff

exception Too_many_re_renders
1 change: 1 addition & 0 deletions lib/js_syntax.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open! Base
open Lib_domains

exception NotImplemented
exception Unreachable
Expand Down
1 change: 1 addition & 0 deletions lib/logger.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open! Base
open Lib_domains
open Syntax
open Concrete_domains

Expand Down
40 changes: 0 additions & 40 deletions lib/util.ml
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
open! Base

module Map_key (T : sig
type t

val sexp_of_t : t -> Sexp.t
val t_of_sexp : Sexp.t -> t
val of_string : string -> t
val to_string : t -> string
val ( <= ) : t -> t -> bool
val ( >= ) : t -> t -> bool
val ( = ) : t -> t -> bool
val ( < ) : t -> t -> bool
val ( > ) : t -> t -> bool
val ( <> ) : t -> t -> bool
val compare : t -> t -> int
val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int
val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t
val clamp : t -> min:t -> max:t -> t Or_error.t

type comparator_witness

val comparator : (t, comparator_witness) Comparator.t
val hash : t -> int
val equal : t -> t -> bool
end) =
struct
include T

module Map = struct
open Map
include M (T)

let empty = empty (module T)
let sexp_of_t sexp_of_v t = Map.sexp_of_m__t (module T) sexp_of_v t
end
end

let pad_or_truncate (lst : 'a list) ~(len : int) : 'a option list =
let open List in
let l = length lst in
Expand Down

0 comments on commit f75b8de

Please sign in to comment.