aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/meta/type/check.lux
blob: 18aef928a09b61ab742e3e5ac032ec3b58685b17 (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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
(.require
 [library
  [lux (.except symbol type)
   [abstract
    ["[0]" monad (.only do)]
    ["[0]" functor
     ["[1]T" \\test (.only Injection Comparison)]]
    ["[0]" apply
     ["[1]T" \\test]]
    [\\specification
     ["$[0]" monad]]]
   [control
    ["[0]" pipe]
    ["[0]" function]
    ["[0]" try]
    ["[0]" exception]]
   [data
    ["[0]" bit (.use "[1]#[0]" equivalence)]
    ["[0]" product]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor monoid)]
     ["[0]" set]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number
     ["n" nat]]]
   [meta
    [macro
     ["^" pattern]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   ["/[1]" // (.use "[1]#[0]" equivalence)]]])

... TODO: Remove the following 3 definitions ASAP. //.type already exists...
(def short
  (Random Text)
  (random.unicode 10))

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

(def (type' num_vars)
  (-> Nat (Random Type))
  (random.rec
   (function (_ again)
     (let [pairG (random.and again again)
           quantifiedG (random.and (random#in (list)) (type' (++ num_vars)))
           random_pair (random.either (random.either (random#each (|>> {.#Sum}) pairG)
                                                     (random#each (|>> {.#Product}) pairG))
                                      (random.either (random#each (|>> {.#Function}) pairG)
                                                     (random#each (|>> {.#Apply}) pairG)))
           random_id (let [random_id (random.either (random#each (|>> {.#Var}) random.nat)
                                                    (random#each (|>> {.#Ex}) random.nat))]
                       (when num_vars
                         0 random_id
                         _ (random.either (random#each (|>> (n.% num_vars) (n.* 2) ++ {.#Parameter}) random.nat)
                                          random_id)))
           random_quantified (random.either (random#each (|>> {.#UnivQ}) quantifiedG)
                                            (random#each (|>> {.#ExQ}) quantifiedG))]
       (all random.either
            (random#each (|>> {.#Nominal}) (random.and ..short (random#in (list))))
            random_pair
            random_id
            random_quantified
            (random#each (|>> {.#Named}) (random.and ..symbol (type' 0)))
            )))))

(def type
  (Random Type)
  (..type' 0))

(def (valid_type? type)
  (-> Type Bit)
  (when type
    {.#Nominal name params}
    (list.every? valid_type? params)
    
    {.#Ex id}
    true

    (^.with_template [<tag>]
      [{<tag> left right}
       (and (valid_type? left)
            (valid_type? right))])
    ([.#Sum] [.#Product] [.#Function])

    {.#Named name type'}
    (valid_type? type')

    _
    false))

(def injection
  (Injection (All (_ a) (/.Check a)))
  (of /.monad in))

(def comparison
  (Comparison (All (_ a) (/.Check a)))
  (function (_ == left right)
    (when [(/.result /.fresh_context left) (/.result /.fresh_context right)]
      [{try.#Success left} {try.#Success right}]
      (== left right)

      _
      false)))

(def polymorphism
  Test
  (all _.and
       (_.for [/.functor]
              (functorT.spec ..injection ..comparison /.functor))
       (_.for [/.apply]
              (applyT.spec ..injection ..comparison /.apply))
       (_.for [/.monad]
              ($monad.spec ..injection ..comparison /.monad))
       ))

(def (nominal_type parameters)
  (-> Nat (Random Type))
  (do random.monad
    [nominal (random.upper_cased 3)
     parameters (random.list parameters (nominal_type (-- parameters)))]
    (in {.#Nominal nominal parameters})))

(def clean_type
  (Random Type)
  (nominal_type 2))

(exception.def yolo)

(def error_handling
  Test
  (do random.monad
    [left ..clean_type
     right ..clean_type
     ex random.nat]
    (all _.and
         (do random.monad
           [expected (random.upper_cased 10)]
           (_.coverage [/.failure]
             (when (/.result /.fresh_context
                             (is (/.Check Any)
                                 (/.failure expected)))
               {try.#Success _} false
               {try.#Failure actual} (same? expected actual))))
         (do random.monad
           [expected (random.upper_cased 10)]
           (_.coverage [/.assertion]
             (and (when (/.result /.fresh_context
                                  (is (/.Check Any)
                                      (/.assertion expected true)))
                    {try.#Success _} true
                    {try.#Failure actual} false)
                  (when (/.result /.fresh_context (/.assertion expected false))
                    {try.#Success _} false
                    {try.#Failure actual} (same? expected actual)))))
         (_.coverage [/.except]
           (when (/.result /.fresh_context
                           (is (/.Check Any)
                               (/.except ..yolo [])))
             {try.#Success _} false
             {try.#Failure error} (exception.match? ..yolo error)))
         (let [scenario (is (-> (-> Text Bit) Type Type Bit)
                            (function (_ ? <left> <right>)
                              (and (|> (/.check <left> <right>)
                                       (is (/.Check Any))
                                       (/.result /.fresh_context)
                                       (pipe.when {try.#Failure error} (? error)
                                         {try.#Success _} false))
                                   (|> (/.check <right> <left>)
                                       (is (/.Check Any))
                                       (/.result /.fresh_context)
                                       (pipe.when {try.#Failure error} (? error)
                                         {try.#Success _} false)))))]
           (all _.and
                (_.coverage [/.type_check_failed]
                  (let [scenario (scenario (exception.match? /.type_check_failed))]
                    (and (scenario (Tuple left right) left)
                         (scenario (Tuple left right) (Or left right))
                         (scenario (Tuple left right) (-> left right))
                         (scenario (Tuple left right) {.#Ex ex})

                         (scenario (Or left right) left)
                         (scenario (Or left right) (-> left right))
                         (scenario (Or left right) {.#Ex ex})

                         (scenario (-> left right) left)
                         (scenario (-> left right) {.#Ex ex})

                         (scenario {.#Ex ex} left)
                         )))
                (_.coverage [/.invalid_type_application]
                  (let [scenario (scenario (text.contains? (the exception.#label /.invalid_type_application)))]
                    (scenario {.#Apply left right} left)))))
         )))

(def var
  Test
  (<| (_.for [/.Var])
      (all _.and
           (_.coverage [/.var]
             (when (/.result /.fresh_context
                             (do /.monad
                               [[var_id var_type] /.var]
                               (in (//#= var_type {.#Var var_id}))))
               {try.#Success verdict} verdict
               {try.#Failure error} false))
           (do random.monad
             [nominal (random.upper_cased 10)]
             (_.coverage [/.bind]
               (when (/.result /.fresh_context
                               (do /.monad
                                 [[var_id var_type] /.var
                                  _ (/.bind {.#Nominal nominal (list)}
                                            var_id)]
                                 (in true)))
                 {try.#Success _} true
                 {try.#Failure error} false)))
           (do random.monad
             [nominal (random.upper_cased 10)]
             (_.coverage [/.bound?]
               (and (|> (do /.monad
                          [[var_id var_type] /.var
                           pre (/.bound? var_id)
                           _ (/.bind {.#Nominal nominal (list)}
                                     var_id)
                           post (/.bound? var_id)]
                          (in (and (not pre)
                                   post)))
                        (/.result /.fresh_context)
                        (try.else false))
                    (|> (do /.monad
                          [[var_id var/0] /.var
                           pre (/.bound? var_id)
                           [_ var/1] /.var
                           _ (/.check var/0 var/1)
                           post (/.bound? var_id)]
                          (in (and (not pre)
                                   (not post))))
                        (/.result /.fresh_context)
                        (try.else false)))))
           (do random.monad
             [nominal (random.upper_cased 10)]
             (_.coverage [/.cannot_rebind_var]
               (when (/.result /.fresh_context
                               (do /.monad
                                 [[var_id var_type] /.var
                                  _ (/.bind {.#Nominal nominal (list)}
                                            var_id)]
                                 (/.bind {.#Nominal nominal (list)}
                                         var_id)))
                 {try.#Success _}
                 false
                 
                 {try.#Failure error}
                 (exception.match? /.cannot_rebind_var error))))
           (do random.monad
             [nominal (random.upper_cased 10)
              var_id random.nat]
             (_.coverage [/.unknown_type_var]
               (when (/.result /.fresh_context
                               (/.bind {.#Nominal nominal (list)}
                                       var_id))
                 {try.#Success _}
                 false
                 
                 {try.#Failure error}
                 (exception.match? /.unknown_type_var error))))
           (do random.monad
             [nominal (random.upper_cased 10)
              .let [expected {.#Nominal nominal (list)}]]
             (_.coverage [/.peek]
               (and (|> (do /.monad
                          [[var_id var_type] /.var]
                          (/.peek var_id))
                        (/.result /.fresh_context)
                        (pipe.when {try.#Success {.#None}} true
                          _ false))
                    (|> (do /.monad
                          [[var_id var/0] /.var
                           [_ var/1] /.var
                           _ (/.check var/0 var/1)]
                          (/.peek var_id))
                        (/.result /.fresh_context)
                        (pipe.when {try.#Success {.#None}} true
                          _ false))
                    (|> (do /.monad
                          [[var_id var_type] /.var
                           _ (/.bind expected var_id)]
                          (/.peek var_id))
                        (/.result /.fresh_context)
                        (pipe.when {try.#Success {.#Some actual}}
                          (same? expected actual)
                          
                          _
                          false)))))
           (do random.monad
             [nominal (random.upper_cased 10)
              .let [expected {.#Nominal nominal (list)}]]
             (_.coverage [/.read]
               (when (/.result /.fresh_context
                               (do /.monad
                                 [[var_id var_type] /.var
                                  _ (/.bind expected var_id)]
                                 (/.read var_id)))
                 {try.#Success actual}
                 (same? expected actual)
                 
                 _
                 false)))
           (do random.monad
             [nominal (random.upper_cased 10)
              .let [expected {.#Nominal nominal (list)}]]
             (_.coverage [/.unbound_type_var]
               (when (/.result /.fresh_context
                               (do /.monad
                                 [[var_id var_type] /.var]
                                 (/.read var_id)))
                 {try.#Failure error}
                 (exception.match? /.unbound_type_var error)

                 _
                 false)))
           )))

(def context
  Test
  (all _.and
       (_.coverage [/.fresh_context]
         (and (n.= 0 (the .#var_counter /.fresh_context))
              (n.= 0 (the .#ex_counter /.fresh_context))
              (list.empty? (the .#var_bindings /.fresh_context))))
       (_.coverage [/.context]
         (and (when (/.result /.fresh_context /.context)
                {try.#Success actual}
                (same? /.fresh_context actual)
                
                {try.#Failure error}
                false)
              (when (/.result /.fresh_context
                              (do /.monad
                                [_ /.var]
                                /.context))
                {try.#Success actual}
                (and (n.= 1 (the .#var_counter actual))
                     (n.= 0 (the .#ex_counter actual))
                     (n.= 1 (list.size (the .#var_bindings actual))))
                
                {try.#Failure error}
                false)))
       (_.coverage [/.existential]
         (when (/.result /.fresh_context
                         (do /.monad
                           [_ /.existential]
                           /.context))
           {try.#Success actual}
           (and (n.= 0 (the .#var_counter actual))
                (n.= 1 (the .#ex_counter actual))
                (n.= 0 (list.size (the .#var_bindings actual))))
           
           {try.#Failure error}
           false))
       ))

(def succeeds?
  (All (_ a) (-> (/.Check a) Bit))
  (|>> (/.result /.fresh_context)
       (pipe.when {try.#Success _}
         true

         {try.#Failure error}
         false)))

(def fails?
  (All (_ a) (-> (/.Check a) Bit))
  (|>> ..succeeds?
       not))

(def nominal
  (Random Type)
  (do random.monad
    [name (random.upper_cased 10)]
    (in {.#Nominal name (list)})))

(def (non_twins = random)
  (All (_ a) (-> (-> a a Bit) (Random a) (Random [a a])))
  (do random.monad
    [left random
     right (random.only (|>> (= left) not) random)]
    (in [left right])))

(.type Super
  (Ex (_ sub) [Text sub]))

(.type Sub
  (Super Bit))

(def (handles_nominal_types! name/0 name/1 parameter/0 parameter/1)
  (-> Text Text Type Type Bit)
  (let [names_matter!
        (and (..succeeds? (/.check {.#Nominal name/0 (list)}
                                   {.#Nominal name/0 (list)}))
             (..fails? (/.check {.#Nominal name/0 (list)}
                                {.#Nominal name/1 (list)})))

        parameters_matter!
        (and (..succeeds? (/.check {.#Nominal name/0 (list parameter/0)}
                                   {.#Nominal name/0 (list parameter/0)}))
             (..fails? (/.check {.#Nominal name/0 (list parameter/0)}
                                {.#Nominal name/0 (list parameter/1)})))

        covariant_parameters!
        (and (..succeeds? (/.check {.#Nominal name/0 (list Super)}
                                   {.#Nominal name/0 (list Sub)}))
             (..fails? (/.check {.#Nominal name/0 (list Sub)}
                                {.#Nominal name/0 (list Super)})))]
    (and names_matter!
         parameters_matter!
         covariant_parameters!)))

(with_template [<assertion> <combinator>]
  [(def (<assertion> name/0 name/1)
     (-> Text Text Bit)
     (let [pair/0 {<combinator> {.#Nominal name/0 (list)} {.#Nominal name/0 (list)}}
           pair/1 {<combinator> {.#Nominal name/1 (list)} {.#Nominal name/1 (list)}}
           
           invariant!
           (and (..succeeds? (/.check pair/0 pair/0))
                (..fails? (/.check pair/0 pair/1)))

           super_pair {<combinator> Super Super}
           sub_pair {<combinator> Sub Sub}
           
           covariant!
           (and (..succeeds? (/.check super_pair sub_pair))
                (..fails? (/.check sub_pair super_pair)))]
       (and invariant!
            covariant!)))]

  [handles_products! .#Product]
  [handles_sums! .#Sum]
  )

(def (handles_function_variance! nominal)
  (-> Type Bit)
  (let [functions_have_contravariant_inputs!
        (..succeeds? (/.check {.#Function Sub nominal} {.#Function Super nominal}))
        
        functions_have_covariant_outputs!
        (..succeeds? (/.check {.#Function nominal Super} {.#Function nominal Sub}))]
    (and functions_have_contravariant_inputs!
         functions_have_covariant_outputs!)))

(def (verdict check)
  (All (_ _) (-> (/.Check _) (/.Check Bit)))
  (function (_ context)
    {try.#Success [context (when (check context)
                             {try.#Success _}
                             true

                             {try.#Failure _}
                             false)]}))

(def (build_ring tail_size)
  (-> Nat (/.Check [Type (List Type) Type]))
  (do [! /.monad]
    [[id/head var/head] /.var
     var/tail+ (monad.each ! (function (_ _)
                               (do !
                                 [[id/T var/tail] /.var]
                                 (in var/tail)))
                           (list.repeated tail_size /.var))
     var/last (monad.mix ! (function (_ var/next var/prev)
                             (do !
                               [_ (/.check var/prev var/next)]
                               (in var/next)))
                         var/head
                         var/tail+)
     _ (/.check var/last var/head)]
    (in [var/head var/tail+ var/last])))

(def (handles_var_rings! tail_size nominal/0 nominal/1)
  (-> Nat Type Type Bit)
  (let [can_create_rings_of_variables!
        (succeeds? (..build_ring tail_size))

        can_bind_rings_of_variables!
        (succeeds? (do [! /.monad]
                     [[var/head var/tail+ var/last] (..build_ring tail_size)
                      _ (/.check var/head nominal/0)
                      failures (monad.each ! (|>> (/.check nominal/1) ..verdict) (list.partial var/head var/tail+))
                      successes (monad.each ! (|>> (/.check nominal/0) ..verdict) (list.partial var/head var/tail+))]
                     (/.assertion "" (and (list.every? (bit#= false) failures)
                                          (list.every? (bit#= true) successes)))))

        can_merge_multiple_rings_of_variables!
        (succeeds? (do [! /.monad]
                     [[var/head/0 var/tail+/0 var/last/0] (..build_ring tail_size)
                      [var/head/1 var/tail+/1 var/last/1] (..build_ring tail_size)
                      _ (/.check var/head/0 var/head/1)
                      _ (/.check var/head/0 nominal/0)
                      .let [all_variables (list#composite (list.partial var/head/0 var/tail+/0)
                                                          (list.partial var/head/1 var/tail+/1))]
                      failures (monad.each ! (|>> (/.check nominal/1) ..verdict) all_variables)
                      successes (monad.each ! (|>> (/.check nominal/0) ..verdict) all_variables)]
                     (/.assertion "" (and (list.every? (bit#= false) failures)
                                          (list.every? (bit#= true) successes)))))]
    (and can_create_rings_of_variables!
         can_bind_rings_of_variables!
         can_merge_multiple_rings_of_variables!)))

(def (handles_vars! nominal)
  (-> Type Bit)
  (let [vars_check_against_themselves!
        (succeeds? (do /.monad
                     [[id var] /.var]
                     (/.check var var)))

        can_bind_vars_by_checking_against_them!
        (and (succeeds? (do /.monad
                          [[id var] /.var]
                          (/.check var nominal)))
             (succeeds? (do /.monad
                          [[id var] /.var]
                          (/.check nominal var))))

        cannot_rebind!
        (fails? (do /.monad
                  [[id var] /.var
                   _ (/.check var nominal)]
                  (/.check var ..Sub)))

        bound_vars_check_against_their_bound_types!
        (and (succeeds? (do /.monad
                          [[id var] /.var
                           _ (/.check var nominal)]
                          (/.check nominal var)))
             (succeeds? (do /.monad
                          [[id var] /.var
                           _ (/.check var ..Super)]
                          (/.check var ..Sub)))
             (succeeds? (do /.monad
                          [[id var] /.var
                           _ (/.check var ..Sub)]
                          (/.check ..Super var)))

             (fails? (do /.monad
                       [[id var] /.var
                        _ (/.check var ..Super)]
                       (/.check ..Sub var)))
             (fails? (do /.monad
                       [[id var] /.var
                        _ (/.check var ..Sub)]
                       (/.check var ..Super))))]
    (and vars_check_against_themselves!
         can_bind_vars_by_checking_against_them!
         cannot_rebind!
         bound_vars_check_against_their_bound_types!)))

(def handles_existentials!
  Bit
  (let [existentials_always_match_themselves!
        (..succeeds? (do /.monad
                       [[_ single] /.existential]
                       (/.check single single)))

        existentials_never_match_each_other!
        (..fails? (do /.monad
                    [[_ left] /.existential
                     [_ right] /.existential]
                    (/.check left right)))]
    (and existentials_always_match_themselves!
         existentials_never_match_each_other!)))

(def (handles_quantification! nominal)
  (-> Type Bit)
  (let [universals_satisfy_themselves!
        (..succeeds? (/.check (.type_literal (All (_ a) (Maybe a)))
                              (.type_literal (All (_ a) (Maybe a)))))

        existentials_satisfy_themselves!
        (..succeeds? (/.check (.type_literal (Ex (_ a) (Maybe a)))
                              (.type_literal (Ex (_ a) (Maybe a)))))

        universals_satisfy_particulars!
        (..succeeds? (/.check (.type_literal (Maybe nominal))
                              (.type_literal (All (_ a) (Maybe a)))))

        particulars_do_not_satisfy_universals!
        (..fails? (/.check (.type_literal (All (_ a) (Maybe a)))
                           (.type_literal (Maybe nominal))))

        particulars_satisfy_existentials!
        (..succeeds? (/.check (.type_literal (Ex (_ a) (Maybe a)))
                              (.type_literal (Maybe nominal))))

        existentials_do_not_satisfy_particulars!
        (..fails? (/.check (.type_literal (Maybe nominal))
                           (.type_literal (Ex (_ a) (Maybe a)))))]
    (and universals_satisfy_themselves!
         existentials_satisfy_themselves!

         universals_satisfy_particulars!
         particulars_do_not_satisfy_universals!
         
         particulars_satisfy_existentials!
         existentials_do_not_satisfy_particulars!
         )))

(def (handles_ultimates! nominal)
  (-> Type Bit)
  (let [any_is_the_ultimate_super_type!
        (and (..succeeds? (/.check Any nominal))
             (..fails? (/.check nominal Any)))

        nothing_is_the_ultimate_sub_type!
        (and (..succeeds? (/.check nominal Nothing))
             (..fails? (/.check Nothing nominal)))

        ultimates_check_themselves!
        (and (..succeeds? (/.check Any Any))
             (..succeeds? (/.check Nothing Nothing)))]
    (and any_is_the_ultimate_super_type!
         nothing_is_the_ultimate_sub_type!
         ultimates_check_themselves!)))

(def (names_do_not_affect_types! left_name right_name nominal)
  (-> Symbol Symbol Type Bit)
  (and (..succeeds? (/.check {.#Named left_name Any} nominal))
       (..succeeds? (/.check Any {.#Named right_name nominal}))
       (..succeeds? (/.check {.#Named left_name Any} {.#Named right_name nominal}))))

... TODO: Test all the crazy corner cases from /.check_apply
(def (handles_application! nominal/0 nominal/1)
  (-> Type Type Bit)
  (let [types_flow_through!
        (and (..succeeds? (/.check (.type_literal ((All (_ a) a) nominal/0))
                                   nominal/0))
             (..succeeds? (/.check nominal/0
                                   (.type_literal ((All (_ a) a) nominal/0))))

             (..succeeds? (/.check (.type_literal ((Ex (_ a) a) nominal/0))
                                   nominal/0))
             (..succeeds? (/.check nominal/0
                                   (.type_literal ((Ex (_ a) a) nominal/0)))))

        multiple_parameters!
        (and (..succeeds? (/.check (.type_literal ((All (_ a b) [a b]) nominal/0 nominal/1))
                                   (.type_literal [nominal/0 nominal/1])))
             (..succeeds? (/.check (.type_literal [nominal/0 nominal/1])
                                   (.type_literal ((All (_ a b) [a b]) nominal/0 nominal/1))))
             
             (..succeeds? (/.check (.type_literal ((Ex (_ a b) [a b]) nominal/0 nominal/1))
                                   (.type_literal [nominal/0 nominal/1])))
             (..succeeds? (/.check (.type_literal [nominal/0 nominal/1])
                                   (.type_literal ((Ex (_ a b) [a b]) nominal/0 nominal/1)))))]
    (and types_flow_through!
         multiple_parameters!)))

(def check
  Test
  (do [! random.monad]
    [nominal ..nominal
     [name/0 name/1] (..non_twins text#= (random.upper_cased 10))
     [parameter/0 parameter/1] (..non_twins //#= ..nominal)
     left_name ..symbol
     right_name ..symbol
     ring_tail_size (of ! each (n.% 10) random.nat)]
    (_.coverage [/.check]
      (and (..handles_nominal_types! name/0 name/1 parameter/0 parameter/1)
           (..handles_products! name/0 name/1)
           (..handles_sums! name/0 name/1)
           (..handles_function_variance! nominal)
           (..handles_vars! nominal)
           (..handles_var_rings! ring_tail_size parameter/0 parameter/1)
           ..handles_existentials!
           (..handles_quantification! nominal)
           (..handles_ultimates! nominal)
           (..handles_application! parameter/0 parameter/1)
           (..names_do_not_affect_types! left_name right_name nominal)
           ))))

(def dirty_type
  (Random (-> Type Type))
  (random.rec
   (function (_ dirty_type)
     (`` (all random.either
              (random#each (function (_ id)
                             (function.constant {.#Ex id}))
                           random.nat)
              (do random.monad
                [module (random.upper_cased 10)
                 short (random.upper_cased 10)
                 anonymousT dirty_type]
                (in (function (_ holeT)
                      {.#Named [module short] (anonymousT holeT)})))
              (,, (with_template [<tag>]
                    [(do random.monad
                       [leftT dirty_type
                        rightT dirty_type]
                       (in (function (_ holeT)
                             {<tag> (leftT holeT) (rightT holeT)})))]

                    [.#Sum]
                    [.#Product]
                    [.#Function]
                    [.#Apply]
                    ))
              (do [! random.monad]
                [name (random.upper_cased 10)
                 parameterT dirty_type]
                (in (function (_ holeT)
                      {.#Nominal name (list (parameterT holeT))})))
              (,, (with_template [<tag>]
                    [(do [! random.monad]
                       [funcT dirty_type
                        argT dirty_type
                        body random.nat]
                       (in (function (_ holeT)
                             {<tag> (list (funcT holeT) (argT holeT))
                                    {.#Parameter body}})))]

                    [.#UnivQ]
                    [.#ExQ]
                    ))
              )))))

(def clean
  Test
  (do random.monad
    [type_shape ..dirty_type]
    (_.coverage [/.clean]
      (and (|> (do /.monad
                 [[var_id varT] /.var
                  cleanedT (/.clean (list) (type_shape varT))]
                 (in (//#= (type_shape varT)
                           cleanedT)))
               (/.result /.fresh_context)
               (try.else false))
           (|> (do /.monad
                 [[var_id varT] /.var
                  [_ replacementT] /.existential
                  _ (/.check varT replacementT)
                  cleanedT (/.clean (list) (type_shape varT))]
                 (in (//#= (type_shape replacementT)
                           cleanedT)))
               (/.result /.fresh_context)
               (try.else false))
           ))))

(def for_subsumption|ultimate
  (Random Bit)
  (do random.monad
    [example ..clean_type]
    (in (and (/.subsumes? .Any example)
             (not (/.subsumes? example .Any))
             
             (/.subsumes? example .Nothing)
             (not (/.subsumes? .Nothing example))
             ))))

(def for_subsumption|nominal
  (Random Bit)
  (do random.monad
    [nominal (random.upper_cased 10)
     example ..clean_type]
    (in (and (/.subsumes? {.#Nominal nominal (list)}
                          {.#Nominal nominal (list)})
             (/.subsumes? {.#Nominal nominal (list .Any)}
                          {.#Nominal nominal (list example)})
             (not (/.subsumes? {.#Nominal nominal (list example)}
                               {.#Nominal nominal (list .Any)}))
             (/.subsumes? {.#Nominal nominal (list example)}
                          {.#Nominal nominal (list .Nothing)})
             (not (/.subsumes? {.#Nominal nominal (list .Nothing)}
                               {.#Nominal nominal (list example)}))
             ))))

(def for_subsumption|sum
  (Random Bit)
  (do random.monad
    [left ..clean_type
     right ..clean_type]
    (in (and (/.subsumes? {.#Sum .Any .Any}
                          {.#Sum left right})
             (not (/.subsumes? {.#Sum left right}
                               {.#Sum .Any .Any}))
             (/.subsumes? {.#Sum left right}
                          {.#Sum .Nothing .Nothing})
             (not (/.subsumes? {.#Sum .Nothing .Nothing}
                               {.#Sum left right}))
             ))))

(def for_subsumption|product
  (Random Bit)
  (do random.monad
    [left ..clean_type
     right ..clean_type]
    (in (and (/.subsumes? {.#Product .Any .Any}
                          {.#Product left right})
             (not (/.subsumes? {.#Product left right}
                               {.#Product .Any .Any}))
             (/.subsumes? {.#Product left right}
                          {.#Product .Nothing .Nothing})
             (not (/.subsumes? {.#Product .Nothing .Nothing}
                               {.#Product left right}))
             ))))

(def for_subsumption|function
  (Random Bit)
  (do random.monad
    [left ..clean_type
     right ..clean_type]
    (in (and (/.subsumes? {.#Function .Nothing .Any}
                          {.#Function left right})
             (not (/.subsumes? {.#Function left right}
                               {.#Function .Nothing .Any}))
             (not (/.subsumes? {.#Function .Any .Nothing}
                               {.#Function left right}))
             ))))

(with_template [<tag> <name>]
  [(def <name>
     (Random Bit)
     (do random.monad
       [id random.nat
        example ..clean_type]
       (in (not (or (/.subsumes? {<tag> id} example)
                    (/.subsumes? example {<tag> id}))))))]

  [.#Var for_subsumption|variable]
  [.#Ex for_subsumption|existential]
  )

(def for_subsumption|quantification+application
  (Random Bit)
  (do random.monad
    [example ..clean_type]
    (in (and (and (/.subsumes? (.type_literal (List example)) (.type_literal (All (_ a) (List a))))
                  (not (/.subsumes? (.type_literal (All (_ a) (List a))) (.type_literal (List example)))))
             (and (/.subsumes? (.type_literal (Ex (_ a) (List a))) (.type_literal (List example)))
                  (not (/.subsumes? (.type_literal (List example)) (.type_literal (Ex (_ a) (List a))))))))))

(def for_subsumption|named
  (Random Bit)
  (do random.monad
    [module (random.upper_cased 10)
     short (random.upper_cased 10)
     example ..clean_type]
    (in (and (/.subsumes? {.#Named [module short] example}
                          example)
             (/.subsumes? example
                          {.#Named [module short] example})
             ))))

(def for_subsumption
  Test
  (do random.monad
    [for_subsumption|ultimate ..for_subsumption|ultimate
     for_subsumption|nominal ..for_subsumption|nominal
     for_subsumption|sum ..for_subsumption|sum
     for_subsumption|product ..for_subsumption|product
     for_subsumption|function ..for_subsumption|function
     for_subsumption|variable ..for_subsumption|variable
     for_subsumption|existential ..for_subsumption|existential
     for_subsumption|quantification+application ..for_subsumption|quantification+application
     for_subsumption|named ..for_subsumption|named]
    (_.coverage [/.subsumes?]
      (and for_subsumption|ultimate
           for_subsumption|nominal
           for_subsumption|sum
           for_subsumption|product
           for_subsumption|function
           for_subsumption|variable
           for_subsumption|existential
           for_subsumption|quantification+application
           for_subsumption|named
           ))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Check])
      (all _.and
           ..polymorphism
           (do random.monad
             [expected random.nat]
             (_.coverage [/.result]
               (when (/.result /.fresh_context
                               (of /.monad in expected))
                 {try.#Success actual} (same? expected actual)
                 {try.#Failure error} false)))
           ..error_handling
           ..var
           ..context
           ..check
           ..clean
           ..for_subsumption
           )))