aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/compiler/default/phase/analysis/structure.lux
blob: 186c961e9ce3cd6838d03e3e3435ad0833c1dc46 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
(.module:
  [lux #*
   [control
    [monad (#+ do)]
    pipe]
   [data
    ["." bit ("#;." equivalence)]
    ["e" error]
    ["." product]
    ["." maybe]
    ["." text]
    [collection
     ["." list ("#;." functor)]
     ["." set]]]
   [math
    ["r" random]]
   ["." type ("#;." equivalence)
    ["." check]]
   [macro
    ["." code]]
   [compiler
    [default
     ["." init]
     ["." phase
      ["." analysis (#+ Analysis Variant Tag Operation)
       ["." module]
       [".A" type]
       ["/" structure]
       ["." expression]]
      [extension
       [".E" analysis]]]]]
   test]
  [//
   ["_." primitive]])

(template [<name> <on-success> <on-error>]
  [(def: #export <name>
     (All [a] (-> (Operation a) Bit))
     (|>> (phase.run _primitive.state)
          (case> (#e.Success _)
                 <on-success>

                 _
                 <on-error>)))]

  [check-succeeds #1 #0]
  [check-fails    #0 #1]
  )

(def: (check-sum' size tag variant)
  (-> Nat Tag (Variant Analysis) Bit)
  (let [variant-tag (if (get@ #analysis.right? variant)
                      (inc (get@ #analysis.lefts variant))
                      (get@ #analysis.lefts variant))]
    (|> size dec (n/= tag)
        (bit;= (get@ #analysis.right? variant))
        (and (n/= tag variant-tag)))))

(def: (check-sum type size tag analysis)
  (-> Type Nat Tag (Operation Analysis) Bit)
  (|> analysis
      (typeA.with-type type)
      (phase.run _primitive.state)
      (case> (^ (#e.Success (analysis.variant variant)))
             (check-sum' size tag variant)

             _
             #0)))

(def: (tagged module tags type)
  (All [a] (-> Text (List module.Tag) Type (Operation a) (Operation [Module a])))
  (|>> (do phase.monad
         [_ (module.declare-tags tags #0 type)])
       (module.with-module 0 module)))

(def: (check-variant module tags type size tag analysis)
  (-> Text (List module.Tag) Type Nat Tag (Operation Analysis) Bit)
  (|> analysis
      (tagged module tags type)
      (typeA.with-type type)
      (phase.run _primitive.state)
      (case> (^ (#e.Success [_ (analysis.variant variant)]))
             (check-sum' size tag variant)

             _
             #0)))

(def: (right-size? size)
  (-> Nat (-> Analysis Bit))
  (|>> (case> (^ (analysis.tuple elems))
              (|> elems
                  list.size
                  (n/= size))
              
              _
              false)))

(def: (check-record-inference module tags type size analysis)
  (-> Text (List module.Tag) Type Nat (Operation [Type Analysis]) Bit)
  (|> analysis
      (tagged module tags type)
      (phase.run _primitive.state)
      (case> (#e.Success [_ productT productA])
             (and (type;= type productT)
                  (right-size? size productA))

             _
             #0)))

(context: "Sums"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% 10) (n/max 2))))
         choice (|> r.nat (:: @ map (n/% size)))
         primitives (r.list size _primitive.primitive)
         +choice (|> r.nat (:: @ map (n/% (inc size))))
         [_ +valueC] _primitive.primitive
         #let [variantT (type.variant (list;map product.left primitives))
               [valueT valueC] (maybe.assume (list.nth choice primitives))
               +size (inc size)
               +primitives (list.concat (list (list.take choice primitives)
                                              (list [(#.Parameter 1) +valueC])
                                              (list.drop choice primitives)))
               [+valueT +valueC] (maybe.assume (list.nth +choice +primitives))
               +variantT (type.variant (list;map product.left +primitives))]]
        ($_ seq
            (test "Can analyse sum."
                  (check-sum variantT size choice
                             (/.sum _primitive.phase choice valueC)))
            (test "Can analyse sum through bound type-vars."
                  (|> (do phase.monad
                        [[_ varT] (typeA.with-env check.var)
                         _ (typeA.with-env
                             (check.check varT variantT))]
                        (typeA.with-type varT
                          (/.sum _primitive.phase choice valueC)))
                      (phase.run _primitive.state)
                      (case> (^ (#e.Success (analysis.variant variant)))
                             (check-sum' size choice variant)

                             _
                             #0)))
            (test "Cannot analyse sum through unbound type-vars."
                  (|> (do phase.monad
                        [[_ varT] (typeA.with-env check.var)]
                        (typeA.with-type varT
                          (/.sum _primitive.phase choice valueC)))
                      check-fails))
            (test "Can analyse sum through existential quantification."
                  (|> (typeA.with-type (type.ex-q 1 +variantT)
                        (/.sum _primitive.phase +choice +valueC))
                      check-succeeds))
            (test "Can analyse sum through universal quantification."
                  (let [check-outcome (if (not (n/= choice +choice))
                                        check-succeeds
                                        check-fails)]
                    (|> (typeA.with-type (type.univ-q 1 +variantT)
                          (/.sum _primitive.phase +choice +valueC))
                        check-outcome)))
            ))))

(context: "Products"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% 10) (n/max 2))))
         primitives (r.list size _primitive.primitive)
         choice (|> r.nat (:: @ map (n/% size)))
         [_ +valueC] _primitive.primitive
         #let [tupleT (type.tuple (list;map product.left primitives))
               [singletonT singletonC] (|> primitives (list.nth choice) maybe.assume)
               +primitives (list.concat (list (list.take choice primitives)
                                              (list [(#.Parameter 1) +valueC])
                                              (list.drop choice primitives)))
               +tupleT (type.tuple (list;map product.left +primitives))]]
        ($_ seq
            (test "Can analyse product."
                  (|> (typeA.with-type tupleT
                        (/.product _primitive.phase (list;map product.right primitives)))
                      (phase.run _primitive.state)
                      (case> (#e.Success tupleA)
                             (right-size? size tupleA)

                             _
                             #0)))
            (test "Can infer product."
                  (|> (typeA.with-inference
                        (/.product _primitive.phase (list;map product.right primitives)))
                      (phase.run _primitive.state)
                      (case> (#e.Success [_type tupleA])
                             (and (type;= tupleT _type)
                                  (right-size? size tupleA))

                             _
                             #0)))
            (test "Can analyse pseudo-product (singleton tuple)"
                  (|> (typeA.with-type singletonT
                        (_primitive.phase (` [(~ singletonC)])))
                      check-succeeds))
            (test "Can analyse product through bound type-vars."
                  (|> (do phase.monad
                        [[_ varT] (typeA.with-env check.var)
                         _ (typeA.with-env
                             (check.check varT (type.tuple (list;map product.left primitives))))]
                        (typeA.with-type varT
                          (/.product _primitive.phase (list;map product.right primitives))))
                      (phase.run _primitive.state)
                      (case> (#e.Success tupleA)
                             (right-size? size tupleA)

                             _
                             #0)))
            (test "Can analyse product through existential quantification."
                  (|> (typeA.with-type (type.ex-q 1 +tupleT)
                        (/.product _primitive.phase (list;map product.right +primitives)))
                      check-succeeds))
            (test "Cannot analyse product through universal quantification."
                  (|> (typeA.with-type (type.univ-q 1 +tupleT)
                        (/.product _primitive.phase (list;map product.right +primitives)))
                      check-fails))
            ))))

(context: "Tagged Sums"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% 10) (n/max 2))))
         tags (|> (r.set text.hash size (r.unicode 5)) (:: @ map set.to-list))
         choice (|> r.nat (:: @ map (n/% size)))
         other-choice (|> r.nat (:: @ map (n/% size)) (r.filter (|>> (n/= choice) not)))
         primitives (r.list size _primitive.primitive)
         module-name (r.unicode 5)
         type-name (r.unicode 5)
         #let [varT (#.Parameter 1)
               primitivesT (list;map product.left primitives)
               [choiceT choiceC] (maybe.assume (list.nth choice primitives))
               [other-choiceT other-choiceC] (maybe.assume (list.nth other-choice primitives))
               variantT (type.variant primitivesT)
               namedT (#.Named [module-name type-name] variantT)
               named-polyT (|> (type.variant (list.concat (list (list.take choice primitivesT)
                                                                (list varT)
                                                                (list.drop (inc choice) primitivesT))))
                               (type.univ-q 1)
                               (#.Named [module-name type-name]))
               choice-tag (maybe.assume (list.nth choice tags))
               other-choice-tag (maybe.assume (list.nth other-choice tags))]]
        ($_ seq
            (test "Can infer tagged sum."
                  (|> (/.tagged-sum _primitive.phase [module-name choice-tag] choiceC)
                      (check-variant module-name tags namedT choice size)))
            (test "Tagged sums specialize when type-vars get bound."
                  (|> (/.tagged-sum _primitive.phase [module-name choice-tag] choiceC)
                      (check-variant module-name tags named-polyT choice size)))
            (test "Tagged sum inference retains universal quantification when type-vars are not bound."
                  (|> (/.tagged-sum _primitive.phase [module-name other-choice-tag] other-choiceC)
                      (check-variant module-name tags named-polyT other-choice size)))
            (test "Can specialize generic tagged sums."
                  (|> (typeA.with-type variantT
                        (/.tagged-sum _primitive.phase [module-name other-choice-tag] other-choiceC))
                      (check-variant module-name tags named-polyT other-choice size)))
            ))))

(context: "Records"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% 10) (n/max 2))))
         tags (|> (r.set text.hash size (r.unicode 5)) (:: @ map set.to-list))
         primitives (r.list size _primitive.primitive)
         module-name (r.unicode 5)
         type-name (r.unicode 5)
         choice (|> r.nat (:: @ map (n/% size)))
         #let [varT (#.Parameter 1)
               tagsC (list;map (|>> [module-name] code.tag) tags)
               primitivesT (list;map product.left primitives)
               primitivesC (list;map product.right primitives)
               tupleT (type.tuple primitivesT)
               namedT (#.Named [module-name type-name] tupleT)
               recordC (list.zip2 tagsC primitivesC)
               named-polyT (|> (type.tuple (list.concat (list (list.take choice primitivesT)
                                                              (list varT)
                                                              (list.drop (inc choice) primitivesT))))
                               (type.univ-q 1)
                               (#.Named [module-name type-name]))]]
        ($_ seq
            (test "Can infer record."
                  (|> (typeA.with-inference
                        (/.record _primitive.phase recordC))
                      (check-record-inference module-name tags namedT size)))
            (test "Records specialize when type-vars get bound."
                  (|> (typeA.with-inference
                        (/.record _primitive.phase recordC))
                      (check-record-inference module-name tags named-polyT size)))
            (test "Can specialize generic records."
                  (|> (do phase.monad
                        [recordA (typeA.with-type tupleT
                                   (/.record _primitive.phase recordC))]
                        (wrap [tupleT recordA]))
                      (check-record-inference module-name tags named-polyT size)))
            ))))