aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/tool/compiler/phase/analysis/structure.lux
blob: 156965a55aad971281728dab96db5bb9e36f97da (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
298
299
300
301
302
303
(.module:
  [lux #*
   [abstract ["." monad (#+ do)]]
   [data
    ["%" text/format (#+ format)]
    ["." name]]
   ["r" math/random (#+ Random) ("#@." monad)]
   ["_" test (#+ Test)]
   [control
    pipe]
   [data
    ["." bit ("#@." equivalence)]
    ["." error]
    ["." product]
    ["." maybe]
    ["." text]
    [collection
     ["." list ("#@." functor)]
     ["." set]]]
   ["." type
    ["." check]]
   [macro
    ["." code]]]
  [//
   ["_." primitive]]
  {1
   ["." /
    ["/#" //
     ["#." module]
     ["#." type]
     ["/#" //
      ["/#" //
       ["#." analysis (#+ Analysis Variant Tag Operation)]]]]]})

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

                 _
                 <on-error>)))]

  [check-succeeds true  false]
  [check-fails    false true]
  )

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

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

             _
             false)))

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

(def: (check-variant module tags expectedT variantT tag analysis)
  (-> Text (List //module.Tag) Type Type Tag (Operation Analysis) Bit)
  (|> analysis
      (with-tags module tags variantT)
      (//type.with-type expectedT)
      (///.run _primitive.state)
      (case> (^ (#error.Success [_ (////analysis.variant variant)]))
             (check-sum' tag (list.size tags) variant)

             _
             false)))

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

(def: (check-record module tags expectedT recordT size analysis)
  (-> Text (List //module.Tag) Type Type Nat (Operation Analysis) Bit)
  (|> analysis
      (with-tags module tags recordT)
      (//type.with-type expectedT)
      (///.run _primitive.state)
      (case> (#error.Success [_ productA])
             (correct-size? size productA)

             _
             false)))

(def: sum
  (do r.monad
    [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))]]
    (<| (_.context (%.name (name-of /.sum)))
        ($_ _.and
            (_.test "Can analyse."
                    (check-sum variantT choice size
                               (/.sum _primitive.phase choice valueC)))
            (_.test "Can analyse through bound type-vars."
                    (|> (do ///.monad
                          [[_ varT] (//type.with-env check.var)
                           _ (//type.with-env
                               (check.check varT variantT))]
                          (//type.with-type varT
                            (/.sum _primitive.phase choice valueC)))
                        (///.run _primitive.state)
                        (case> (^ (#error.Success (////analysis.variant variant)))
                               (check-sum' choice size variant)

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

(def: product
  (do r.monad
    [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))]]
    (<| (_.context (%.name (name-of /.product)))
        ($_ _.and
            (_.test "Can analyse."
                    (|> (//type.with-type tupleT
                          (/.product _primitive.phase (list@map product.right primitives)))
                        (///.run _primitive.state)
                        (case> (#error.Success tupleA)
                               (correct-size? size tupleA)

                               _
                               false)))
            (_.test "Can infer."
                    (|> (//type.with-inference
                          (/.product _primitive.phase (list@map product.right primitives)))
                        (///.run _primitive.state)
                        (case> (#error.Success [_type tupleA])
                               (and (check.checks? tupleT _type)
                                    (correct-size? size tupleA))

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

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

(def: variant
  (do r.monad
    [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 [with-name (|>> (#.Named [module-name type-name]))
           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))
           monoT (type.variant primitivesT)
           polyT (|> (type.variant (list.concat (list (list.take choice primitivesT)
                                                      (list varT)
                                                      (list.drop (inc choice) primitivesT))))
                     (type.univ-q 1))
           choice-tag (maybe.assume (list.nth choice tags))
           other-choice-tag (maybe.assume (list.nth other-choice tags))]]
    (<| (_.context (%.name (name-of /.tagged-sum)))
        ($_ _.and
            (_.test "Can infer."
                    (|> (/.tagged-sum _primitive.phase [module-name choice-tag] choiceC)
                        (check-variant module-name tags
                                       monoT (with-name monoT)
                                       choice)))
            (_.test "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
                                       polyT (with-name polyT)
                                       other-choice)))
            (_.test "Can specialize."
                    (|> (//type.with-type monoT
                          (/.tagged-sum _primitive.phase [module-name other-choice-tag] other-choiceC))
                        (check-variant module-name tags
                                       monoT (with-name polyT)
                                       other-choice)))
            (_.test "Specialization when type-vars get bound."
                    (|> (/.tagged-sum _primitive.phase [module-name choice-tag] choiceC)
                        (check-variant module-name tags
                                       monoT (with-name polyT)
                                       choice)))
            ))))

(def: record
  (do r.monad
    [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)
           monoT (#.Named [module-name type-name] (type.tuple primitivesT))
           recordC (list.zip2 tagsC primitivesC)
           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]))]]
    (<| (_.context (%.name (name-of /.record)))
        (_.test "Can infer."
                (|> (/.record _primitive.phase recordC)
                    (check-record module-name tags monoT monoT size))))))

(def: #export test
  Test
  (<| (_.context (name.module (name-of /._)))
      ($_ _.and
          ..sum
          ..product
          ..variant
          ..record
          )))