aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/meta/compiler/language/lux/analysis/inference.lux
blob: b9749422be053334e025dd8b452ff48e0fd436df (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" maybe (.use "[1]#[0]" functor)]
    ["[0]" try (.only Try) (.use "[1]#[0]" functor)]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" product]
    ["[0]" bit (.use "[1]#[0]" equivalence)]
    ["[0]" text]
    [collection
     ["[0]" list (.use "[1]#[0]" monad)]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number
     ["n" nat]]]
   [meta
    ["[0]" code]
    ["[0]" macro]
    ["[0]" symbol
     ["$[1]" \\test]]
    ["[0]" configuration
     ["$[1]" \\test]]
    ["[0]" type (.use "[1]#[0]" equivalence)
     ["[0]" check (.only Check)]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   ["/[1]" // (.only)
    [evaluation (.only Eval)]
    ["[1][0]" macro]
    ["[1][0]" type]
    ["[1][0]" module]
    ["[1][0]" complex]
    [//
     [phase
      ["[2][0]" analysis]
      ["[2][0]" extension (.only)
       ["[1]/[0]" analysis
        ["[1]" lux]]]]
     [///
      ["[2][0]" phase (.use "[1]#[0]" monad)]
      [meta
       ["[0]" archive]]]]]]])

(def (eval archive type term)
  Eval
  (/phase#in []))

(def (expander macro inputs state)
  //macro.Expander
  {try.#Success ((macro.function macro) inputs state)})

(def random_state
  (Random Lux)
  (do random.monad
    [version random.nat
     host (random.lower_cased 1)
     configuration ($configuration.random 5)]
    (in (//.state (//.info version host configuration)))))

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

(def analysis
  //.Phase
  (/analysis.phase ..expander))

(def (fails? exception try)
  (All (_ e a) (-> (Exception e) (Try a) Bit))
  (when try
    {try.#Success _}
    false
    
    {try.#Failure error}
    (text.contains? (the exception.#label exception) error)))

(def .public simple_parameter
  (Random [Type Code])
  (`` (all random.either
           (,, (with_template [<type> <random> <code>]
                 [(random#each (|>> <code> [<type>]) <random>)]

                 [.Bit random.bit code.bit]
                 [.Nat random.nat code.nat]
                 [.Int random.int code.int]
                 [.Rev random.rev code.rev]
                 [.Frac random.frac code.frac]
                 [.Text (random.lower_cased 1) code.text]
                 ))
           )))

(def test|general
  Test
  (do [! random.monad]
    [lux ..random_state
     .let [state [/extension.#bundle (/extension/analysis.bundle ..eval)
                  /extension.#state lux]]
     expected ..nominal
     name ($symbol.random 1 1)
     [type/0 term/0] ..simple_parameter
     arity (of ! each (n.% 10) random.nat)
     nats (random.list arity random.nat)]
    (all _.and
         (_.coverage [/.general]
           (and (|> (/.general archive.empty ..analysis expected (list))
                    (//type.expecting expected)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase.result state)
                    (try#each (|>> product.left (type#= expected)))
                    (try.else false))
                (|> (/.general archive.empty ..analysis
                               (type.function (list.repeated arity .Nat) expected)
                               (list#each code.nat nats))
                    (//type.expecting expected)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase.result state)
                    (try#each (function (_ [actual analysis/*])
                                (and (type#= expected actual)
                                     (of (list.equivalence //.equivalence) =
                                         (list#each (|>> //.nat) nats)
                                         analysis/*))))
                    (try.else false))
                (|> (/.general archive.empty ..analysis
                               (type.literal (-> type/0 expected))
                               (list term/0))
                    (//type.expecting expected)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase.result state)
                    (try#each (|>> product.left (type#= expected)))
                    (try.else false))
                (|> (/.general archive.empty ..analysis
                               (type.literal {.#Named name (-> type/0 expected)})
                               (list term/0))
                    (//type.expecting expected)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase.result state)
                    (try#each (|>> product.left (type#= expected)))
                    (try.else false))
                (|> (/.general archive.empty ..analysis
                               (type.literal (All (_ a) (-> a a)))
                               (list term/0))
                    (//type.expecting type/0)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase#each (|>> product.left (check.clean (list)) //type.check))
                    /phase#conjoint
                    (/phase.result state)
                    (try#each (type#= type/0))
                    (try.else false))
                (|> (/.general archive.empty ..analysis
                               (type.literal ((All (_ a) (-> a a)) type/0))
                               (list term/0))
                    (//type.expecting type/0)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase.result state)
                    (try#each (|>> product.left (type#= type/0)))
                    (try.else false))
                (|> (do /phase.monad
                      [[@var varT] (//type.check check.var)
                       _ (//type.check (check.check varT (type.literal (-> type/0 expected))))]
                      (/.general archive.empty ..analysis varT (list term/0)))
                    (//type.expecting expected)
                    (//module.with 0 (product.left name))
                    (/phase#each product.right)
                    (/phase#each (|>> product.left (check.clean (list)) //type.check))
                    /phase#conjoint
                    (/phase.result state)
                    (try#each (type#= expected))
                    (try.else false))
                (|> (/.general archive.empty ..analysis
                               (type.literal (Ex (_ a) (-> a a)))
                               (list (` (.error# ""))))
                    //type.inferring
                    (//module.with 0 (product.left name))
                    (/phase#each (|>> product.right product.left (check.clean (list)) //type.check))
                    /phase#conjoint
                    (/phase.result state)
                    (try#each //type.existential?)
                    (try.else false))
                ))
         (_.coverage [/.cannot_infer]
           (and (|> (/.general archive.empty ..analysis expected (list term/0))
                    (//type.expecting expected)
                    (/phase.result state)
                    (..fails? /.cannot_infer))
                (|> (do /phase.monad
                      [[@var varT] (//type.check check.var)]
                      (/.general archive.empty ..analysis varT (list term/0)))
                    (//type.expecting expected)
                    (/phase.result state)
                    (..fails? /.cannot_infer))))
         (_.coverage [/.cannot_infer_argument]
           (|> (/.general archive.empty ..analysis
                          (type.literal (-> expected expected))
                          (list term/0))
               (//type.expecting expected)
               (//module.with 0 (product.left name))
               (/phase.result state)
               (..fails? /.cannot_infer_argument)))
         )))

(def test|variant
  Test
  (do [! random.monad]
    [lux ..random_state
     .let [state [/extension.#bundle (/extension/analysis.bundle ..eval)
                  /extension.#state lux]]
     name ($symbol.random 1 1)
     arity (of ! each (|>> (n.% 5) (n.+ 2)) random.nat)
     [type/0 term/0] ..simple_parameter
     [type/1 term/1] (random.only (|>> product.left (same? type/0) not)
                                  ..simple_parameter)
     types/*,terms/* (random.list arity ..simple_parameter)
     tag (of ! each (n.% arity) random.nat)
     .let [[lefts right?] (//complex.choice arity tag)]
     arbitrary_right? random.bit]
    (all _.and
         (_.coverage [/.variant]
           (let [variantT (type.variant (list#each product.left types/*,terms/*))
                 [tagT tagC] (|> types/*,terms/*
                                 (list.item tag)
                                 (maybe.else [Any (' [])]))
                 variant?' (is (-> Type (Maybe Type) Nat Bit Code Bit)
                               (function (_ variant inferred lefts right? term)
                                 (|> (do /phase.monad
                                       [inferT (/.variant lefts right? variant)
                                        [_ [it _]] (|> (/.general archive.empty ..analysis inferT (list term))
                                                       //type.inferring)]
                                       (when inferred
                                         {.#Some inferred}
                                         (//type.check
                                          (do check.monad
                                            [_ (check.check inferred it)
                                             _ (check.check it inferred)]
                                            (in true)))
                                         
                                         {.#None}
                                         (in true)))
                                     (//module.with 0 (product.left name))
                                     (/phase#each product.right)
                                     (/phase.result state)
                                     (try.else false))))
                 variant? (is (-> Type Nat Bit Code Bit)
                              (function (_ type lefts right? term)
                                (variant?' type {.#Some type} lefts right? term)))

                 can_match_case!
                 (variant? variantT lefts right? tagC)

                 names_do_not_matter!
                 (variant? {.#Named name variantT} lefts right? tagC)

                 cases_independent_of_parameters_conform_to_anything!
                 (variant? (type.literal (Maybe type/0)) 0 #0 (' []))

                 cases_dependent_on_parameters_are_tettered_to_those_parameters!
                 (and (variant? (type.literal (Maybe type/0)) 0 #1 term/0)
                      (not (variant? (type.literal (Maybe type/0)) 0 #1 term/1)))

                 only_bottom_conforms_to_tags_outside_of_range!
                 (`` (and (,, (with_template [<verdict> <term>]
                                [(bit#= <verdict> (variant? variantT arity arbitrary_right? <term>))]

                                [#0 term/0]
                                [#1 (` (.error# ""))]))))

                 can_handle_universal_quantification!
                 (and (variant?' (type.literal (All (_ a) (Maybe a)))
                                 {.#Some Maybe}
                                 0 #0 (' []))
                      (variant?' (type.literal (All (_ a) (Maybe a)))
                                 {.#Some (type.literal (Maybe type/0))}
                                 0 #1 term/0)
                      (not (variant?' (type.literal (All (_ a) (Maybe a)))
                                      {.#Some Maybe}
                                      0 #1 term/0)))

                 existential_types_do_not_affect_independent_cases!
                 (variant?' (type.literal (Ex (_ a) (Maybe a)))
                            {.#None}
                            0 #0 (' []))

                 existential_types_affect_dependent_cases!
                 (`` (and (,, (with_template [<verdict> <term>]
                                [(bit#= <verdict> (variant?' (type.literal (Ex (_ a) (Maybe a))) {.#None} 0 #1 <term>))]

                                [#0 term/0]
                                [#1 (` (.error# ""))]))))]
             (and can_match_case!
                  names_do_not_matter!

                  cases_independent_of_parameters_conform_to_anything!
                  cases_dependent_on_parameters_are_tettered_to_those_parameters!

                  only_bottom_conforms_to_tags_outside_of_range!

                  can_handle_universal_quantification!

                  existential_types_do_not_affect_independent_cases!
                  existential_types_affect_dependent_cases!
                  )))
         (_.coverage [/.not_a_variant]
           (let [[tagT tagC] (|> types/*,terms/*
                                 (list.item tag)
                                 (maybe.else [Any (' [])]))]
             (|> (/.variant lefts right? tagT)
                 (/phase.result state)
                 (..fails? /.not_a_variant))))
         )))

(def test|record
  Test
  (do [! random.monad]
    [lux ..random_state
     .let [state [/extension.#bundle (/extension/analysis.bundle ..eval)
                  /extension.#state lux]]
     name ($symbol.random 1 1)
     arity (of ! each (|>> (n.% 5) (n.+ 2)) random.nat)
     [type/0 term/0] ..simple_parameter
     [type/1 term/1] (random.only (|>> product.left (same? type/0) not)
                                  ..simple_parameter)
     types/*,terms/* (random.list arity ..simple_parameter)
     .let [record? (is (-> Type (Maybe Type) Nat (List Code) Bit)
                       (function (_ record expected arity terms)
                         (|> (do /phase.monad
                               [inference (/.record arity record)
                                [_ [it _]] (|> (/.general archive.empty ..analysis inference terms)
                                               //type.inferring)]
                               (when expected
                                 {.#Some expected}
                                 (//type.check
                                  (do check.monad
                                    [_ (check.check expected it)
                                     _ (check.check it expected)]
                                    (in true)))
                                 
                                 {.#None}
                                 (in true)))
                             (//module.with 0 (product.left name))
                             (/phase#each product.right)
                             (/phase.result state)
                             (try.else false))))
           record (type.tuple (list#each product.left types/*,terms/*))
           terms (list#each product.right types/*,terms/*)]]
    (all _.and
         (_.coverage [/.record]
           (let [can_infer_record!
                 (record? record {.#None} arity terms)

                 names_do_not_matter!
                 (record? {.#Named name record} {.#None} arity terms)

                 can_handle_universal_quantification!
                 (and (record? (All (_ a) (Tuple type/0 a))
                               {.#Some (Tuple type/0 type/1)}
                               2 (list term/0 term/1))
                      (record? (All (_ a) (Tuple a type/0))
                               {.#Some (Tuple type/1 type/0)}
                               2 (list term/1 term/0)))

                 can_handle_existential_quantification!
                 (and (not (record? (Ex (_ a) (Tuple type/0 a))
                                    {.#Some (Tuple type/0 type/1)}
                                    2 (list term/0 term/1)))
                      (record? (Ex (_ a) (Tuple type/0 a))
                               {.#None}
                               2 (list term/0 (` (.error# ""))))
                      (not (record? (Ex (_ a) (Tuple a type/0))
                                    {.#Some (Tuple type/1 type/0)}
                                    2 (list term/1 term/0)))
                      (record? (Ex (_ a) (Tuple a type/0))
                               {.#None}
                               2 (list (` (.error# "")) term/0)))]
             (and can_infer_record!
                  names_do_not_matter!
                  can_handle_universal_quantification!
                  can_handle_existential_quantification!
                  )))
         (_.coverage [/.not_a_record]
           (|> (/.record arity type/0)
               (/phase.result state)
               (..fails? /.not_a_record)))
         )))

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [lux ..random_state
         .let [state [/extension.#bundle (/extension/analysis.bundle ..eval)
                      /extension.#state lux]]
         [type/0 term/0] ..simple_parameter
         [type/1 term/1] (random.only (|>> product.left (same? type/0) not)
                                      ..simple_parameter)
         lefts (of ! each (n.% 10) random.nat)
         right? random.bit]
        (all _.and
             ..test|general
             ..test|variant
             ..test|record
             (_.coverage [/.invalid_type_application]
               (and (|> (/.general archive.empty ..analysis (type.literal (type/0 type/1)) (list term/0))
                        (/phase.result state)
                        (..fails? /.invalid_type_application))
                    (|> (/.variant lefts right? (type.literal (type/0 type/1)))
                        (/phase.result state)
                        (..fails? /.invalid_type_application))
                    (|> (/.record lefts (type.literal (type/0 type/1)))
                        (/phase.result state)
                        (..fails? /.invalid_type_application))))
             ))))