Debian Codegraph @

expander/lifted.ml

raw symbols SCIP
1 open! Base
2 open Ppxlib
3 open Ast_builder.Default
4
5 type 'a t =
6 { value_bindings : value_binding list
7 ; body : 'a
8 }
9
10 include Monad.Make (struct
11 type nonrec 'a t = 'a t
12
13 let return body = { value_bindings = []; body }
14
15 let bind a ~f =
16 let b = f a.body in
17 { value_bindings = a.value_bindings @ b.value_bindings; body = b.body }
18 ;;
19
20 let map = `Define_using_bind
21 end)
22
23 let create ~loc ~prefix ~ty rhs =
24 let name = gen_symbol ~prefix () in
25 let lhs = pvar ~loc name in
26 let body = evar ~loc name in
27 let ty, rhs, body =
28 if Helpers.is_value_expression rhs
29 then ty, rhs, body
30 else (
31 (* Thunkify the value to evaluate when referred to. *)
32 let ty = [%type: Stdlib.Unit.t -> [%t ty]] in
33 let rhs = [%expr fun () -> [%e rhs]] in
34 let body = [%expr [%e body] ()] in
35 ty, rhs, body)
36 in
37 { value_bindings = [ value_binding ~loc ~pat:(ppat_constraint ~loc lhs ty) ~expr:rhs ]
38 ; body
39 }
40 ;;
41
42 let let_bind_user_expressions { value_bindings; body } ~loc =
43 if List.is_empty value_bindings
44 then body
45 else pexp_let ~loc Nonrecursive value_bindings body
46 ;;