package ppx_let

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ppx_let_expander.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
open Base
open Ppxlib
open Ast_builder.Default

module List = struct
  include List

  let reduce_exn l ~f =
    match l with
    | [] -> invalid_arg "List.reduce_exn"
    | hd :: tl -> fold_left tl ~init:hd ~f
  ;;
end

module Extension_name = struct
  type t =
    | Sub
    | Sub_open
    | Bind
    | Bind_open
    | Bindn
    | Bindn_open
    | Map
    | Map_open
    | Mapn
    | Mapn_open

  let operator_name = function
    | Sub | Sub_open -> "sub"
    | Bind | Bind_open | Bindn | Bindn_open -> "bind"
    | Map | Map_open | Mapn | Mapn_open -> "map"
  ;;

  let to_string = function
    | Sub -> "sub"
    | Sub_open -> "sub_open"
    | Bind -> "bind"
    | Bindn -> "bindn"
    | Bindn_open -> "bindn_open"
    | Bind_open -> "bind_open"
    | Map -> "map"
    | Map_open -> "map_open"
    | Mapn -> "mapn"
    | Mapn_open -> "mapn_open"
  ;;
end

let let_syntax = "Let_syntax"

let let_syntax ~modul : Longident.t =
  match modul with
  | None -> Lident let_syntax
  | Some id -> Ldot (Ldot (id.txt, let_syntax), let_syntax)
;;

let open_on_rhs ~loc ~modul =
  pmod_ident ~loc (Located.mk ~loc (Longident.Ldot (let_syntax ~modul, "Open_on_rhs")))
;;

let eoperator ~loc ~modul func =
  let lid : Longident.t = Ldot (let_syntax ~modul, func) in
  pexp_ident ~loc (Located.mk ~loc lid)
;;

let expand_with_tmp_vars ~loc bindings expr ~f =
  match bindings with
  | [ _ ] -> f ~loc bindings expr
  | _ ->
    let tmp_vars =
      List.map bindings ~f:(fun _ -> gen_symbol ~prefix:"__let_syntax" ())
    in
    let s_rhs_tmp_var (* s/rhs/tmp_var *) =
      List.map2_exn bindings tmp_vars ~f:(fun vb var ->
        let loc = { vb.pvb_expr.pexp_loc with loc_ghost = true } in
        { vb with pvb_expr = evar ~loc var })
    in
    let s_lhs_tmp_var (* s/lhs/tmp_var *) =
      List.map2_exn bindings tmp_vars ~f:(fun vb var ->
        let loc = { vb.pvb_pat.ppat_loc with loc_ghost = true } in
        { vb with
          pvb_pat = pvar ~loc var
        ; pvb_loc = { vb.pvb_loc with loc_ghost = true }
        })
    in
    pexp_let ~loc Nonrecursive s_lhs_tmp_var (f ~loc s_rhs_tmp_var expr)
;;

let expand_with_op_n_function ~loc ~modul ~op_name bindings expr =
  let n = List.length bindings in
  let operator =
    match n with
    | 1 -> eoperator ~loc ~modul op_name
    | n -> eoperator ~loc ~modul (Printf.sprintf "%s%d" op_name n)
  in
  let bindings_args =
    bindings |> List.map ~f:(fun { pvb_expr; _ } -> Nolabel, pvb_expr)
  in
  let func =
    List.fold_right bindings ~init:expr ~f:(fun { pvb_pat; _ } lower ->
      pexp_fun ~loc Nolabel None pvb_pat lower)
  in
  let args = List.append bindings_args [ Labelled "f", func ] in
  pexp_apply ~loc operator args
;;

let bind_apply ~loc ~modul extension_name ~arg ~fn =
  pexp_apply
    ~loc
    (eoperator ~loc ~modul (Extension_name.operator_name extension_name))
    [ Nolabel, arg; Labelled "f", fn ]
;;

let maybe_open extension_name ~to_open:module_to_open expr =
  let loc = { expr.pexp_loc with loc_ghost = true } in
  match (extension_name : Extension_name.t) with
  | Sub | Bind | Map | Mapn | Bindn -> expr
  | Sub_open | Bind_open | Bindn_open | Map_open | Mapn_open ->
    pexp_open ~loc (open_infos ~loc ~override:Override ~expr:(module_to_open ~loc)) expr
;;

let assert_bindings_length ~loc ~extension_name ~bindings =
  match extension_name, List.length bindings with
  | _, 0 -> invalid_arg "expand_let: list of bindings must be non-empty"
  | (Extension_name.Sub | Sub_open), n when n > 1 ->
    (* let%sub doesn't allow more than one binding *)
    Location.raise_errorf ~loc "let%%sub cannot be used with 'and'"
  | _ -> ()
;;

let expand_let extension_name ~loc ~modul bindings body =
  assert_bindings_length ~loc ~extension_name ~bindings;
  (* Build expression [both E1 (both E2 (both ...))] *)
  let nested_boths =
    let rev_boths = List.rev_map bindings ~f:(fun vb -> vb.pvb_expr) in
    List.reduce_exn rev_boths ~f:(fun acc e ->
      let loc = { e.pexp_loc with loc_ghost = true } in
      eapply ~loc (eoperator ~loc ~modul "both") [ e; acc ])
  in
  (* Build pattern [(P1, (P2, ...))] *)
  let nested_patterns =
    let rev_patts = List.rev_map bindings ~f:(fun vb -> vb.pvb_pat) in
    List.reduce_exn rev_patts ~f:(fun acc p ->
      let loc = { p.ppat_loc with loc_ghost = true } in
      ppat_tuple ~loc [ p; acc ])
  in
  bind_apply
    ~loc
    ~modul
    extension_name
    ~arg:nested_boths
    ~fn:(pexp_fun ~loc Nolabel None nested_patterns body)
;;

let expand_match extension_name ~loc ~modul expr cases =
  bind_apply
    ~loc
    ~modul
    extension_name
    ~arg:(maybe_open extension_name ~to_open:(open_on_rhs ~modul) expr)
    ~fn:(pexp_function ~loc cases)
;;

let expand_if extension_name ~loc expr then_ else_ =
  expand_match
    extension_name
    ~loc
    expr
    [ case ~lhs:(pbool ~loc true) ~guard:None ~rhs:then_
    ; case ~lhs:(pbool ~loc false) ~guard:None ~rhs:else_
    ]
;;

let expand_while ~loc ~modul ~cond ~body =
  let loop_name = gen_symbol ~prefix:"__let_syntax_loop" () in
  let ploop = pvar ~loc loop_name in
  let eloop = evar ~loc loop_name in
  let loop_call = pexp_apply ~loc eloop [ Nolabel, eunit ~loc ] in
  let loop_body =
    let then_ = bind_apply ~loc ~modul Bind ~arg:body ~fn:eloop in
    let else_ =
      pexp_apply ~loc (eoperator ~loc ~modul "return") [ Nolabel, eunit ~loc ]
    in
    expand_if ~modul Bind ~loc cond then_ else_
  in
  let loop_func = pexp_fun ~loc Nolabel None (punit ~loc) loop_body in
  pexp_let ~loc Recursive [ value_binding ~loc ~pat:ploop ~expr:loop_func ] loop_call
;;

let expand ~modul extension_name expr =
  let loc = { expr.pexp_loc with loc_ghost = true } in
  let expansion =
    match expr.pexp_desc with
    | Pexp_let (Nonrecursive, bindings, expr) ->
      let bindings =
        List.map bindings ~f:(fun vb ->
          let pvb_pat =
            (* Temporary hack tentatively detecting that the parser
               has expanded `let x : t = e` into `let x : t = (e : t)`.

               For reference, here is the relevant part of the parser:
               https://github.com/ocaml/ocaml/blob/4.07/parsing/parser.mly#L1628 *)
            match vb.pvb_pat.ppat_desc, vb.pvb_expr.pexp_desc with
            | ( Ppat_constraint (p, { ptyp_desc = Ptyp_poly ([], t1); _ })
              , Pexp_constraint (_, t2) )
              when phys_equal t1 t2 || Poly.equal t1 t2 -> p
            | _ -> vb.pvb_pat
          in
          { vb with
            pvb_pat
          ; pvb_expr =
              maybe_open extension_name ~to_open:(open_on_rhs ~modul) vb.pvb_expr
          })
      in
      (match extension_name with
       | Mapn | Bindn | Mapn_open | Bindn_open ->
         expand_with_tmp_vars
           ~loc
           bindings
           expr
           ~f:
             (expand_with_op_n_function
                ~modul
                ~op_name:(Extension_name.operator_name extension_name))
       | Sub | Sub_open | Bind | Bind_open | Map | Map_open ->
         expand_with_tmp_vars ~loc bindings expr ~f:(expand_let extension_name ~modul))
    | Pexp_let (Recursive, _, _) ->
      Location.raise_errorf
        ~loc
        "'let%%%s' may not be recursive"
        (Extension_name.to_string extension_name)
    | Pexp_match (expr, cases) ->
      (match extension_name with
       | Sub | Sub_open -> Location.raise_errorf ~loc "match%%sub is not supported"
       | _ -> expand_match extension_name ~loc ~modul expr cases)
    | Pexp_ifthenelse (expr, then_, else_) ->
      let else_ =
        match else_ with
        | Some else_ -> else_
        | None ->
          Location.raise_errorf
            ~loc
            "'if%%%s' must include an else branch"
            (Extension_name.to_string extension_name)
      in
      (match extension_name with
       | Sub | Sub_open -> Location.raise_errorf ~loc "if%%sub is not supported"
       | _ -> expand_if extension_name ~loc ~modul expr then_ else_)
    | Pexp_while (cond, body) ->
      (match (extension_name : Extension_name.t) with
       | Map | Map_open | Mapn | Mapn_open ->
         Location.raise_errorf
           ~loc
           "while%%map is not supported. use while%%bind instead."
       | Bindn | Bindn_open ->
         Location.raise_errorf
           ~loc
           "while%%bindn is not supported. use while%%bind instead."
       | Sub | Sub_open -> Location.raise_errorf ~loc "while%%sub is not supported"
       | Bind | Bind_open -> expand_while ~loc ~modul ~cond ~body)
    | _ ->
      Location.raise_errorf
        ~loc
        "'%%%s' can only be used with 'let', 'match', 'while', and 'if'"
        (Extension_name.to_string extension_name)
  in
  { expansion with pexp_attributes = expr.pexp_attributes @ expansion.pexp_attributes }
;;
OCaml

Innovation. Community. Security.