File: test.ml

package info (click to toggle)
ppxlib 0.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,804 kB
  • sloc: ml: 66,587; sh: 103; makefile: 40; python: 36
file content (198 lines) | stat: -rw-r--r-- 5,162 bytes parent folder | download
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
open Ppxlib

(* This file contains tests to ensure that [Ast_builder.value_binding] properly
   translates the given [pattern] and [expression] pair into the correct
   [pattern], [expression] and [value_constraint] triple. *)


(* ------- Test Setup -------- *)

#install_printer Pp_ast.Default.structure_item;;
#install_printer Pp_ast.Default.expression;;
#install_printer Pp_ast.Default.pattern;;

let loc = Location.none
[%%ignore]

(* --------- Simple case, no translation --------- *)

let pat = [%pat? f]
let expr = [%expr fun x -> x + 1]
[%%ignore]

let vb =
  let open Ast_builder.Default in
  pstr_value ~loc Nonrecursive [value_binding ~pat ~expr ~loc]

[%%expect{|
val vb : structure_item =
  Pstr_value
    ( Nonrecursive
    , [ { pvb_pat = Ppat_var "f"
        ; pvb_expr =
            Pexp_function
              ( [ { pparam_loc = __loc
                  ; pparam_desc = Pparam_val ( Nolabel, None, Ppat_var "x")
                  }
                ]
              , None
              , Pfunction_body
                  (Pexp_apply
                     ( Pexp_ident (Lident "+")
                     , [ ( Nolabel, Pexp_ident (Lident "x"))
                       ; ( Nolabel
                         , Pexp_constant (Pconst_integer ( "1", None))
                         )
                       ]
                     ))
              )
        ; pvb_constraint = None
        ; pvb_attributes = __attrs
        ; pvb_loc = __loc
        }
      ]
    )
|}]

(* As expected here, the [pvb_constraint] field is none, the pattern and
   expression are used as is. *)

(* --------- No var Ppat_constraint to pvb_constraint --------- *)

let pat = [%pat? (x : int)]
let expr = [%expr 12]
[%%ignore]

let vb =
  let open Ast_builder.Default in
  pstr_value ~loc Nonrecursive [value_binding ~pat ~expr ~loc]

[%%expect{|
val vb : structure_item =
  Pstr_value
    ( Nonrecursive
    , [ { pvb_pat = Ppat_var "x"
        ; pvb_expr = Pexp_constant (Pconst_integer ( "12", None))
        ; pvb_constraint =
            Some
              (Pvc_constraint
                 { locally_abstract_univars = []
                 ; typ = Ptyp_constr ( Lident "int", [])
                 })
        ; pvb_attributes = __attrs
        ; pvb_loc = __loc
        }
      ]
    )
|}]

(* --------- poly Ppat_constraint to pvb_constraint --------- *)

let pat =
  Ast_builder.Default.ppat_constraint ~loc
    [%pat? f]
    (Ast_builder.Default.ptyp_poly ~loc
       [ Loc.make ~loc "a" ]
       [%type: 'a -> unit])

let expr = [%expr fun x -> unit]

[%%ignore]

let vb =
  let open Ast_builder.Default in
  pstr_value ~loc Nonrecursive [value_binding ~pat ~expr ~loc]

[%%expect{|
val vb : structure_item =
  Pstr_value
    ( Nonrecursive
    , [ { pvb_pat = Ppat_var "f"
        ; pvb_expr =
            Pexp_function
              ( [ { pparam_loc = __loc
                  ; pparam_desc = Pparam_val ( Nolabel, None, Ppat_var "x")
                  }
                ]
              , None
              , Pfunction_body (Pexp_ident (Lident "unit"))
              )
        ; pvb_constraint =
            Some
              (Pvc_constraint
                 { locally_abstract_univars = []
                 ; typ =
                     Ptyp_poly
                       ( [ "a"]
                       , Ptyp_arrow
                           ( Nolabel
                           , Ptyp_var "a"
                           , Ptyp_constr ( Lident "unit", [])
                           )
                       )
                 })
        ; pvb_attributes = __attrs
        ; pvb_loc = __loc
        }
      ]
    )
|}]

(* --------- desugared locally abstract univars to pvb_constraint --------- *)

let pat =
  Ast_builder.Default.ppat_constraint ~loc
    [%pat? f]
    (Ast_builder.Default.ptyp_poly ~loc
       [ Loc.make ~loc "a" ]
       [%type: 'a -> unit])

let expr = [%expr fun (type a) -> (fun _ -> unit : a -> unit)]

[%%ignore]

let vb =
  let open Ast_builder.Default in
  pstr_value ~loc Nonrecursive [value_binding ~pat ~expr ~loc]

[%%expect{|
val vb : structure_item =
  Pstr_value
    ( Nonrecursive
    , [ { pvb_pat = Ppat_var "f"
        ; pvb_expr =
            Pexp_function
              ( [ { pparam_loc = __loc
                  ; pparam_desc = Pparam_val ( Nolabel, None, Ppat_any)
                  }
                ]
              , None
              , Pfunction_body (Pexp_ident (Lident "unit"))
              )
        ; pvb_constraint =
            Some
              (Pvc_constraint
                 { locally_abstract_univars = [ "a"]
                 ; typ =
                     Ptyp_arrow
                       ( Nolabel
                       , Ptyp_constr ( Lident "a", [])
                       , Ptyp_constr ( Lident "unit", [])
                       )
                 })
        ; pvb_attributes = __attrs
        ; pvb_loc = __loc
        }
      ]
    )
|}]

(* As expected here, the matching constraint from the pattern and expression or
   recombined into a single value constraint with locally abstract univars set
   correctly. *)

(* --------- coercion to pvb_constraint --------- *)

(*TODO*)
[%%expect{|
|}]