aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/meta/type.lux
blob: 9f753b1d0349878d2be2b5e82e6de483c02e66a5 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
(.require
 [library
  [lux (.except symbol)
   ["_" test (.only Test)]
   [abstract
    ["[0]" monad (.only do)]
    [\\specification
     ["$[0]" equivalence]]]
   [control
    ["<>" parser]
    ["[0]" pipe]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception]]
   [data
    ["[0]" bit (.use "[1]#[0]" equivalence)]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list]
     ["[0]" array]]]
   [macro
    ["^" pattern]
    ["[0]" code (.use "[1]#[0]" equivalence)]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number
     ["n" nat]]]
   [meta
    ["[0]" symbol (.use "[1]#[0]" equivalence)]]]]
 ["[0]" \\parser]
 [\\library
  ["[0]" / (.use "[1]#[0]" equivalence)]]
 ["[0]" /
  ["[1][0]" primitive]
  ["[1][0]" check]
  ["[1][0]" dynamic]
  ["[1][0]" implicit]
  ["[1][0]" quotient]
  ["[1][0]" refinement]
  ["[1][0]" resource]
  ["[1][0]" unit]
  ["[1][0]" variance]])

(def !expect
  (template (_ <pattern> <value>)
    [(case <value>
       <pattern>
       true
       
       _
       false)]))

(def primitive
  (Random Type)
  (|> (random.alpha_numeric 1)
      (at random.monad each (function (_ name)
                              {.#Primitive name (list)}))))

(def test|matches
  Test
  (<| (_.for [\\parser.types_do_not_match])
      (do [! random.monad]
        [expected ..primitive
         dummy (random.only (|>> (/#= expected) not)
                            ..primitive)])
      (all _.and
           (_.coverage [\\parser.exactly]
             (and (|> (\\parser.result (\\parser.exactly expected) expected)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.exactly expected) dummy)
                      (!expect (^.multi {try.#Failure error}
                                        (exception.match? \\parser.types_do_not_match error))))))
           (_.coverage [\\parser.sub]
             (and (|> (\\parser.result (\\parser.sub expected) expected)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.sub Any) expected)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.sub expected) Nothing)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.sub expected) dummy)
                      (!expect (^.multi {try.#Failure error}
                                        (exception.match? \\parser.types_do_not_match error))))))
           (_.coverage [\\parser.super]
             (and (|> (\\parser.result (\\parser.super expected) expected)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.super expected) Any)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.super Nothing) expected)
                      (!expect {try.#Success []}))
                  (|> (\\parser.result (\\parser.super expected) dummy)
                      (!expect (^.multi {try.#Failure error}
                                        (exception.match? \\parser.types_do_not_match error))))))
           )))

(def test|aggregate
  Test
  (do [! random.monad]
    [expected_left ..primitive
     expected_middle ..primitive
     expected_right ..primitive]
    (`` (all _.and
             (~~ (with_template [<parser> <exception> <good_constructor> <bad_constructor>]
                   [(_.coverage [<parser> <exception>]
                      (and (|> (\\parser.result (<parser> (all <>.and \\parser.any \\parser.any \\parser.any))
                                                (<good_constructor> (list expected_left expected_middle expected_right)))
                               (!expect (^.multi {try.#Success [actual_left actual_middle actual_right]}
                                                 (and (/#= expected_left actual_left)
                                                      (/#= expected_middle actual_middle)
                                                      (/#= expected_right actual_right)))))
                           (|> (\\parser.result (<parser> (all <>.and \\parser.any \\parser.any \\parser.any))
                                                (<bad_constructor> (list expected_left expected_middle expected_right)))
                               (!expect (^.multi {try.#Failure error}
                                                 (exception.match? <exception> error))))))]

                   [\\parser.variant \\parser.not_variant /.variant /.tuple]
                   [\\parser.tuple \\parser.not_tuple /.tuple /.variant]
                   ))

             (_.coverage [\\parser.function \\parser.not_function]
               (and (|> (\\parser.result (\\parser.function (all <>.and \\parser.any \\parser.any) \\parser.any)
                                         (/.function (list expected_left expected_middle) expected_right))
                        (!expect (^.multi {try.#Success [[actual_left actual_middle] actual_right]}
                                          (and (/#= expected_left actual_left)
                                               (/#= expected_middle actual_middle)
                                               (/#= expected_right actual_right)))))
                    (|> (\\parser.result (\\parser.function (all <>.and \\parser.any \\parser.any) \\parser.any)
                                         (/.variant (list expected_left expected_middle expected_right)))
                        (!expect (^.multi {try.#Failure error}
                                          (exception.match? \\parser.not_function error))))))
             (_.coverage [\\parser.applied \\parser.not_application]
               (and (|> (\\parser.result (\\parser.applied (all <>.and \\parser.any \\parser.any \\parser.any))
                                         (/.application (list expected_middle expected_right) expected_left))
                        (!expect (^.multi {try.#Success [actual_left actual_middle actual_right]}
                                          (and (/#= expected_left actual_left)
                                               (/#= expected_middle actual_middle)
                                               (/#= expected_right actual_right)))))
                    (|> (\\parser.result (\\parser.applied (all <>.and \\parser.any \\parser.any \\parser.any))
                                         (/.variant (list expected_left expected_middle expected_right)))
                        (!expect (^.multi {try.#Failure error}
                                          (exception.match? \\parser.not_application error))))))
             ))))

(def test|parameter
  Test
  (do random.monad
    [quantification ..primitive
     argument ..primitive
     not_parameter ..primitive
     parameter random.nat]
    (all _.and
         (_.coverage [\\parser.not_parameter]
           (|> (\\parser.result \\parser.parameter not_parameter)
               (!expect (^.multi {try.#Failure error}
                                 (exception.match? \\parser.not_parameter error)))))
         (_.coverage [\\parser.unknown_parameter]
           (|> (\\parser.result \\parser.parameter {.#Parameter parameter})
               (!expect (^.multi {try.#Failure error}
                                 (exception.match? \\parser.unknown_parameter error)))))
         (_.coverage [\\parser.with_extension]
           (|> (\\parser.result (<| (\\parser.with_extension quantification)
                                    (\\parser.with_extension argument)
                                    \\parser.any)
                                not_parameter)
               (!expect (^.multi {try.#Success [quantification::binding argument::binding actual]}
                                 (same? not_parameter actual)))))
         (_.coverage [\\parser.parameter]
           (|> (\\parser.result (<| (\\parser.with_extension quantification)
                                    (\\parser.with_extension argument)
                                    \\parser.parameter)
                                {.#Parameter 0})
               (!expect {try.#Success [quantification::binding argument::binding _]})))
         (_.coverage [\\parser.argument]
           (let [argument? (is (-> Nat Nat Bit)
                               (function (_ @ expected)
                                 (|> (\\parser.result (<| (\\parser.with_extension quantification)
                                                          (\\parser.with_extension argument)
                                                          (\\parser.with_extension quantification)
                                                          (\\parser.with_extension argument)
                                                          (do <>.monad
                                                            [env \\parser.env
                                                             _ \\parser.any]
                                                            (in (\\parser.argument env @))))
                                                      not_parameter)
                                     (!expect (^.multi {try.#Success [_ _ _ _ actual]}
                                                       (n.= expected actual))))))]
             (and (argument? 0 2)
                  (argument? 1 3)
                  (argument? 2 0))))
         (_.coverage [\\parser.wrong_parameter]
           (|> (\\parser.result (<| (\\parser.with_extension quantification)
                                    (\\parser.with_extension argument)
                                    (\\parser.this_parameter 1))
                                {.#Parameter 0})
               (!expect (^.multi {try.#Failure error}
                                 (exception.match? \\parser.wrong_parameter error)))))
         (_.coverage [\\parser.this_parameter]
           (|> (\\parser.result (<| (\\parser.with_extension quantification)
                                    (\\parser.with_extension argument)
                                    (\\parser.this_parameter 0))
                                {.#Parameter 0})
               (!expect {try.#Success [quantification::binding argument::binding _]})))
         )))

(def test|polymorphic
  Test
  (do [! random.monad]
    [not_polymorphic ..primitive
     expected_inputs (at ! each (|>> (n.% 10) ++) random.nat)]
    (all _.and
         (_.coverage [\\parser.not_polymorphic]
           (and (|> (\\parser.result (\\parser.polymorphic \\parser.any)
                                     not_polymorphic)
                    (!expect (^.multi {try.#Failure error}
                                      (exception.match? \\parser.not_polymorphic error))))
                (|> (\\parser.result (\\parser.polymorphic \\parser.any)
                                     (/.univ_q 0 not_polymorphic))
                    (!expect (^.multi {try.#Failure error}
                                      (exception.match? \\parser.not_polymorphic error))))))
         (_.coverage [\\parser.polymorphic]
           (|> (\\parser.result (\\parser.polymorphic \\parser.any)
                                (/.univ_q expected_inputs not_polymorphic))
               (!expect (^.multi {try.#Success [g!poly actual_inputs bodyT]}
                                 (and (n.= expected_inputs (list.size actual_inputs))
                                      (same? not_polymorphic bodyT))))))
         )))

(def test|recursive
  Test
  (do random.monad
    [expected ..primitive]
    (all _.and
         (_.coverage [\\parser.recursive]
           (|> (.type_literal (Rec @ expected))
               (\\parser.result (\\parser.recursive \\parser.any))
               (!expect (^.multi {try.#Success [@self actual]}
                                 (/#= expected actual)))))
         (_.coverage [\\parser.recursive_self]
           (|> (.type_literal (Rec @ @))
               (\\parser.result (\\parser.recursive \\parser.recursive_self))
               (!expect (^.multi {try.#Success [@expected @actual]}
                                 (same? @expected @actual)))))
         (_.coverage [\\parser.recursive_call]
           (|> (.type_literal (All (self input) (self input)))
               (\\parser.result (\\parser.polymorphic \\parser.recursive_call))
               (!expect {try.#Success [@self inputs ???]})))
         (_.coverage [\\parser.not_recursive]
           (and (|> expected
                    (\\parser.result (\\parser.recursive \\parser.any))
                    (!expect (^.multi {try.#Failure error}
                                      (exception.match? \\parser.not_recursive error))))
                (|> expected
                    (\\parser.result \\parser.recursive_self)
                    (!expect (^.multi {try.#Failure error}
                                      (exception.match? \\parser.not_recursive error))))))
         )))

(def \\parser
  Test
  (<| (_.covering \\parser._)
      (_.for [\\parser.Parser])
      (all _.and
           (do [! random.monad]
             [expected ..primitive]
             (_.coverage [\\parser.result \\parser.any]
               (|> (\\parser.result \\parser.any expected)
                   (!expect (^.multi {try.#Success actual}
                                     (/#= expected actual))))))
           (do [! random.monad]
             [expected ..primitive]
             (_.coverage [\\parser.next \\parser.unconsumed_input]
               (and (|> (\\parser.result (do <>.monad
                                           [actual \\parser.next
                                            _ \\parser.any]
                                           (in actual))
                                         expected)
                        (!expect (^.multi {try.#Success actual}
                                          (/#= expected actual))))
                    (|> (\\parser.result \\parser.next expected)
                        (!expect (^.multi {try.#Failure error}
                                          (exception.match? \\parser.unconsumed_input error)))))))
           (do [! random.monad]
             [expected ..primitive]
             (_.coverage [\\parser.empty_input]
               (`` (and (~~ (with_template [<parser>]
                              [(|> (\\parser.result (do <>.monad
                                                      [_ \\parser.any]
                                                      <parser>)
                                                    expected)
                                   (!expect (^.multi {try.#Failure error}
                                                     (exception.match? \\parser.empty_input error))))]

                              [\\parser.any]
                              [\\parser.next]
                              ))))))
           (do [! random.monad]
             [expected ..primitive]
             (_.coverage [\\parser.Env \\parser.env \\parser.fresh]
               (|> (\\parser.result (do <>.monad
                                      [env \\parser.env
                                       _ \\parser.any]
                                      (in env))
                                    expected)
                   (!expect (^.multi {try.#Success environment}
                                     (same? \\parser.fresh environment))))))
           (do [! random.monad]
             [expected ..primitive
              dummy (random.only (|>> (/#= expected) not)
                                 ..primitive)]
             (_.coverage [\\parser.local]
               (|> (\\parser.result (do <>.monad
                                      [_ \\parser.any]
                                      (\\parser.local (list expected)
                                                      \\parser.any))
                                    dummy)
                   (!expect (^.multi {try.#Success actual}
                                     (/#= expected actual))))))
           (do [! random.monad]
             [expected random.nat]
             (_.coverage [\\parser.existential \\parser.not_existential]
               (|> (\\parser.result \\parser.existential
                                    {.#Ex expected})
                   (!expect (^.multi {try.#Success actual}
                                     (n.= expected actual))))))
           (do [! random.monad]
             [expected_name (random.and (random.alpha_numeric 1)
                                        (random.alpha_numeric 1))
              expected_type ..primitive]
             (_.coverage [\\parser.named \\parser.not_named]
               (|> (\\parser.result \\parser.named
                                    {.#Named expected_name expected_type})
                   (!expect (^.multi {try.#Success [actual_name actual_type]}
                                     (and (symbol#= expected_name actual_name)
                                          (/#= expected_type actual_type)))))))
           ..test|aggregate
           ..test|matches
           ..test|parameter
           ..test|polymorphic
           ..test|recursive
           )))

(def short
  (Random Text)
  (do [! random.monad]
    [size (|> random.nat (at ! each (n.% 10)))]
    (random.unicode size)))

(def symbol
  (Random Symbol)
  (random.and ..short ..short))

(def (random' parameters)
  (-> Nat (Random Type))
  (random.rec
   (function (_ again)
     (let [pairG (random.and again again)
           un_parameterized (is (Random Type)
                                (all random.either
                                     (random#each (|>> {.#Primitive}) (random.and ..short (random.list 0 again)))
                                     (random#each (|>> {.#Primitive}) (random.and ..short (random.list 1 again)))
                                     (random#each (|>> {.#Primitive}) (random.and ..short (random.list 2 again)))
                                     (random#each (|>> {.#Sum}) pairG)
                                     (random#each (|>> {.#Product}) pairG)
                                     (random#each (|>> {.#Function}) pairG)
                                     ))]
       (case parameters
         0 un_parameterized
         _ (|> random.nat
               (random#each (|>> (n.% parameters) {.#Parameter}))
               (random.either un_parameterized)))))))

(def .public (random parameters)
  (-> Nat (Random Type))
  (all random.either
       (random#each (/.univ_q parameters) (random' parameters))
       (random#each (/.ex_q parameters) (random' parameters))
       ))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec /.equivalence (..random 0)))
           
           (do [! random.monad]
             [anonymousT (random.only (|>> (pipe.case {.#Named _ _} false
                                             _ true))
                                      (..random 0))
              symbol/0 ..symbol
              symbol/1 ..symbol
              .let [namedT {.#Named symbol/0 anonymousT}
                    aliasedT {.#Named symbol/1 namedT}]]
             (all _.and
                  (_.coverage [/.de_aliased]
                    (at /.equivalence = namedT (/.de_aliased aliasedT)))
                  (_.coverage [/.anonymous]
                    (at /.equivalence = anonymousT (/.anonymous aliasedT)))))
           (do [! random.monad]
             [size (|> random.nat (at ! each (n.% 3)))
              members (|> (..random 0)
                          (random.only (function (_ type)
                                         (case type
                                           (^.or {.#Sum _} {.#Product _})
                                           #0

                                           _
                                           #1)))
                          (list.repeated size)
                          (monad.all !))
              .let [(open "/#[0]") /.equivalence
                    (open "list#[0]") (list.equivalence /.equivalence)]]
             (`` (all _.and
                      (~~ (with_template [<ctor> <dtor> <unit>]
                            [(_.coverage [<ctor> <dtor>]
                               (let [flat (|> members <ctor> <dtor>)]
                                 (or (list#= members flat)
                                     (and (list#= (list) members)
                                          (list#= (list <unit>) flat)))))]

                            [/.variant /.flat_variant Nothing]
                            [/.tuple   /.flat_tuple   Any]
                            ))
                      )))
           (_.coverage [/.applied]
             (and (<| (maybe.else #0)
                      (do maybe.monad
                        [partial (/.applied (list Bit) Ann)
                         full (/.applied (list Int) partial)]
                        (in (at /.equivalence = full {.#Product Bit Int}))))
                  (|> (/.applied (list Bit) Text)
                      (pipe.case {.#None} #1 _ #0))))
           (do [! random.monad]
             [size (|> random.nat (at ! each (n.% 3)))
              members (monad.all ! (list.repeated size (..random 0)))
              extra (|> (..random 0)
                        (random.only (function (_ type)
                                       (case type
                                         (^.or {.#Function _} {.#Apply _})
                                         #0

                                         _
                                         #1))))
              .let [(open "/#[0]") /.equivalence
                    (open "list#[0]") (list.equivalence /.equivalence)]]
             (all _.and
                  (_.coverage [/.function /.flat_function]
                    (let [[inputs output] (|> (/.function members extra) /.flat_function)]
                      (and (list#= members inputs)
                           (/#= extra output))))
                  (_.coverage [/.application /.flat_application]
                    (let [[tfunc tparams] (|> extra (/.application members) /.flat_application)]
                      (n.= (list.size members) (list.size tparams))))
                  ))
           (do [! random.monad]
             [size (|> random.nat (at ! each (|>> (n.% 3) ++)))
              body_type (|> (..random 0)
                            (random.only (function (_ type)
                                           (case type
                                             (^.or {.#UnivQ _} {.#ExQ _})
                                             #0

                                             _
                                             #1))))
              .let [(open "/#[0]") /.equivalence]]
             (`` (all _.and
                      (~~ (with_template [<ctor> <dtor>]
                            [(_.coverage [<ctor> <dtor>]
                               (let [[flat_size flat_body] (|> body_type (<ctor> size) <dtor>)]
                                 (and (n.= size flat_size)
                                      (/#= body_type flat_body))))]

                            [/.univ_q /.flat_univ_q]
                            [/.ex_q   /.flat_ex_q]
                            ))
                      (_.coverage [/.quantified?]
                        (and (not (/.quantified? body_type))
                             (|> body_type (/.univ_q size) /.quantified?)
                             (|> body_type (/.ex_q size) /.quantified?)))
                      )))
           (do [! random.monad]
             [depth (|> random.nat (at ! each (|>> (n.% 3) ++)))
              element_type (|> (..random 0)
                               (random.only (function (_ type)
                                              (case type
                                                (pattern {.#Primitive name (list element_type)})
                                                (not (text#= array.type_name name))

                                                _
                                                #1))))
              .let [(open "/#[0]") /.equivalence]]
             (all _.and
                  (_.coverage [/.array /.flat_array]
                    (let [[flat_depth flat_element] (|> element_type (/.array depth) /.flat_array)]
                      (and (n.= depth flat_depth)
                           (/#= element_type flat_element))))
                  (_.coverage [/.array?]
                    (and (not (/.array? element_type))
                         (/.array? (/.array depth element_type))))
                  ))
           (_.coverage [/.by_example]
             (let [example (is (Maybe Nat)
                               {.#None})]
               (/#= (.type_literal (List Nat))
                    (/.by_example [a]
                      (is (Maybe a)
                          example)
                      (List a)))))
           (do random.monad
             [sample random.nat]
             (_.coverage [/.log!]
               (exec
                 (/.log! sample)
                 true)))
           (do random.monad
             [left random.nat
              right (random.lower_case 1)
              .let [left,right [left right]]]
             (_.coverage [/.as]
               (|> left,right
                   (/.as [l r] (And l r) (Or l r))
                   (/.as [l r] (Or l r) (And l r))
                   (same? left,right))))
           (do random.monad
             [expected random.nat]
             (_.coverage [/.sharing]
               (n.= expected
                    (/.sharing [a]
                      (is (I64 a)
                          expected)
                      (is (I64 a)
                          (.i64 expected))))))
           (do random.monad
             [expected_left random.nat
              expected_right random.nat]
             (_.coverage [/.let]
               (let [[actual_left actual_right]
                     (is (/.let [side /.Nat]
                           [side side])
                         [expected_left expected_right])]
                 (and (same? expected_left actual_left)
                      (same? expected_right actual_right)))))
           (do random.monad
             [.let [(open "/#[0]") /.equivalence]
              left (..random 0)
              right (..random 0)]
             (all _.and
                  (_.coverage [/.code]
                    (bit#= (/#= left right)
                           (code#= (/.code left) (/.code right))))
                  (_.coverage [/.format]
                    (bit#= (/#= left right)
                           (text#= (/.format left) (/.format right))))
                  ))

           ..\\parser

           /primitive.test
           /check.test
           /dynamic.test
           /implicit.test
           /quotient.test
           /refinement.test
           /resource.test
           /unit.test
           /variance.test
           )))