aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/type.lux
blob: f129f1c5ae63dcf4b93fb0155f8e5668f802d608 (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
(.module:
  [lux (#- type)
   ["%" data/text/format (#+ format)]
   ["M" abstract/monad (#+ do)]
   ["r" math/random (#+ Random)]
   ["_" test (#+ Test)]
   [control
    pipe]
   [data
    ["." maybe]
    [number
     ["n" nat]]
    [collection
     ["." list]]]]
  {1
   ["." / ("#@." equivalence)]}
  ["." / #_
   ["#." check]
   ["#." dynamic]
   ["#." implicit]
   ["#." resource]])

(def: short
  (r.Random Text)
  (do r.monad
    [size (|> r.nat (:: @ map (n.% 10)))]
    (r.unicode size)))

(def: name
  (r.Random Name)
  (r.and ..short ..short))

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

(def: #export test
  Test
  (<| (_.context (%.name (name-of /._)))
      ($_ _.and
          (do r.monad
            [sample ..type]
            (_.test "Every type is equal to itself."
                    (:: /.equivalence = sample sample)))
          (_.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))))
          (let [base (#.Named ["" "a"] (#.Product Bit Int))
                aliased (#.Named ["" "c"]
                                 (#.Named ["" "b"]
                                          base))]
            ($_ _.and
                (_.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))))))
          (do r.monad
            [size (|> r.nat (:: @ map (n.% 3)))
             members (|> ..type
                         (r.filter (function (_ type)
                                     (case type
                                       (^or (#.Sum _) (#.Product _))
                                       #0

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

                          ["variant" /.variant /.flatten-variant Nothing]
                          ["tuple"   /.tuple   /.flatten-tuple   Any]
                          ))
                    )))
          (do r.monad
            [size (|> r.nat (:: @ map (n.% 3)))
             members (M.seq @ (list.repeat size ..type))
             extra (|> ..type
                       (r.filter (function (_ type)
                                   (case type
                                     (^or (#.Function _) (#.Apply _))
                                     #0

                                     _
                                     #1))))
             #let [(^open "/@.") /.equivalence
                   (^open "list@.") (list.equivalence /.equivalence)]]
            ($_ _.and
                (_.test "Can build and tear-down function types."
                        (let [[inputs output] (|> (/.function members extra) /.flatten-function)]
                          (and (list@= 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))))
                ))
          (do r.monad
            [size (|> r.nat (:: @ map (n.% 3)))
             extra (|> ..type
                       (r.filter (function (_ type)
                                   (case type
                                     (^or (#.UnivQ _) (#.ExQ _))
                                     #0

                                     _
                                     #1))))
             #let [(^open "/@.") /.equivalence]]
            (`` ($_ _.and
                    (~~ (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]
                          ))
                    )))
          (_.test (%.name (name-of /.:by-example))
                  (let [example (: (Maybe Nat)
                                   #.None)]
                    (/@= (.type (List Nat))
                         (/.:by-example [a]
                                        {(Maybe a) example}
                                        (List a)))))

          /check.test
          /dynamic.test
          /implicit.test
          /resource.test
          )))