aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/type.lux
blob: b4796911aae124e71e92f548b6de8bb8f4f306c7 (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
(.module:
  [lux #*
   [control
    ["M" monad (#+ do Monad)]
    pipe]
   [data
    ["." maybe]
    [text
     format]
    [collection
     ["." list]]]
   [math
    ["r" random]]
   ["&" type]]
  lux/test)

## [Utils]
(def: #export gen-short
  (r.Random Text)
  (do r.monad
    [size (|> r.nat (:: @ map (n/% 10)))]
    (r.unicode size)))

(def: #export gen-name
  (r.Random Name)
  (r.and gen-short gen-short))

(def: #export gen-type
  (r.Random Type)
  (let [(^open "R/.") r.monad]
    (r.rec (function (_ gen-type)
             (let [pairG (r.and gen-type gen-type)
                   idG r.nat
                   quantifiedG (r.and (R/wrap (list)) gen-type)]
               ($_ r.or
                   (r.and gen-short (R/wrap (list)))
                   pairG
                   pairG
                   pairG
                   idG
                   idG
                   idG
                   quantifiedG
                   quantifiedG
                   pairG
                   (r.and gen-name gen-type)
                   ))))))

## [Tests]
(context: "Types"
  (<| (times 100)
      (do @
        [sample gen-type]
        (test "Every type is equal to itself."
              (:: &.equivalence = sample sample)))))

(context: "Type application"
  (test "Can apply quantified types (universal and existential quantification)."
        (and (maybe.default #0
                            (do maybe.monad
                              [partial (&.apply (list Bit) Ann)
                               full (&.apply (list Int) partial)]
                              (wrap (:: &.equivalence = full (#.Product Bit Int)))))
             (|> (&.apply (list Bit) Text)
                 (case> #.None #1 _ #0)))))

(context: "Naming"
  (let [base (#.Named ["" "a"] (#.Product Bit Int))
        aliased (#.Named ["" "c"]
                         (#.Named ["" "b"]
                                  base))]
    ($_ seq
        (test "Can remove aliases from an already-named type."
              (:: &.equivalence =
                  base
                  (&.un-alias aliased)))

        (test "Can remove all names from a type."
              (and (not (:: &.equivalence =
                            base
                            (&.un-name aliased)))
                   (:: &.equivalence =
                       (&.un-name base)
                       (&.un-name aliased)))))))

(context: "Type construction [structs]"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (n/% 3)))
         members (|> gen-type
                     (r.filter (function (_ type)
                                 (case type
                                   (^or (#.Sum _) (#.Product _))
                                   #0

                                   _
                                   #1)))
                     (list.repeat size)
                     (M.seq @))
         #let [(^open "&/.") &.equivalence
               (^open "L/.") (list.equivalence &.equivalence)]]
        (with-expansions
          [<struct-tests> (do-template [<desc> <ctor> <dtor> <unit>]
                            [(test (format "Can build and tear-down " <desc> " types.")
                                   (let [flat (|> members <ctor> <dtor>)]
                                     (or (L/= members flat)
                                         (and (L/= (list) members)
                                              (L/= (list <unit>) flat)))))]

                            ["variant" &.variant &.flatten-variant Nothing]
                            ["tuple"   &.tuple   &.flatten-tuple   Any]
                            )]
          ($_ seq
              <struct-tests>
              )))))

(context: "Type construction [parameterized]"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (n/% 3)))
         members (M.seq @ (list.repeat size gen-type))
         extra (|> gen-type
                   (r.filter (function (_ type)
                               (case type
                                 (^or (#.Function _) (#.Apply _))
                                 #0

                                 _
                                 #1))))
         #let [(^open "&/.") &.equivalence
               (^open "L/.") (list.equivalence &.equivalence)]]
        ($_ seq
            (test "Can build and tear-down function types."
                  (let [[inputs output] (|> (&.function members extra) &.flatten-function)]
                    (and (L/= members inputs)
                         (&/= extra output))))

            (test "Can build and tear-down application types."
                  (let [[tfunc tparams] (|> extra (&.application members) &.flatten-application)]
                    (n/= (list.size members) (list.size tparams))))
            ))))

(context: "Type construction [higher order]"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (n/% 3)))
         extra (|> gen-type
                   (r.filter (function (_ type)
                               (case type
                                 (^or (#.UnivQ _) (#.ExQ _))
                                 #0

                                 _
                                 #1))))
         #let [(^open "&/.") &.equivalence]]
        (with-expansions
          [<quant-tests> (do-template [<desc> <ctor> <dtor>]
                           [(test (format "Can build and tear-down " <desc> " types.")
                                  (let [[flat-size flat-body] (|> extra (<ctor> size) <dtor>)]
                                    (and (n/= size flat-size)
                                         (&/= extra flat-body))))]

                           ["universally-quantified"   &.univ-q &.flatten-univ-q]
                           ["existentially-quantified" &.ex-q   &.flatten-ex-q]
                           )]
          ($_ seq
              <quant-tests>
              )))))