aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/language/compiler/synthesis/structure.lux
blob: e3351158e10e825b5d9c2d8c42b3c936aacb9a15 (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
(.module:
  lux
  (lux [io]
       (control [monad #+ do]
                pipe)
       (data [bool "bool/" Equivalence<Bool>]
             [product]
             [error]
             (collection [list]))
       (language ["///." compiler]
                 [".L" analysis]
                 ["//" synthesis #+ Synthesis]
                 (synthesis [".S" expression])
                 [".L" extension])
       ["r" math/random "r/" Monad<Random>]
       test)
  [//primitive])

(context: "Variants"
  (<| (times +100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% +10) (n/+ +2))))
         tagA (|> r.nat (:: @ map (n/% size)))
         memberA //primitive.primitive]
        ($_ seq
            (test "Can synthesize variants."
                  (|> (analysisL.sum-analysis size tagA memberA)
                      (expressionS.synthesizer extensionL.empty)
                      (///compiler.run //.init)
                      (case> (#error.Success (#//.Structure (#//.Variant [leftsS right?S valueS])))
                             (let [tagS (if right?S (inc leftsS) leftsS)]
                               (and (n/= tagA tagS)
                                    (|> tagS (n/= (dec size)) (bool/= right?S))
                                    (//primitive.corresponds? memberA valueS)))
                             
                             _
                             false)))
            ))))

(context: "Tuples"
  (<| (times +100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% +10) (n/max +2))))
         membersA (r.list size //primitive.primitive)]
        ($_ seq
            (test "Can synthesize tuple."
                  (|> (analysisL.product-analysis membersA)
                      (expressionS.synthesizer extensionL.empty)
                      (///compiler.run //.init)
                      (case> (#error.Success (#//.Structure (#//.Tuple membersS)))
                             (and (n/= size (list.size membersS))
                                  (list.every? (product.uncurry //primitive.corresponds?)
                                               (list.zip2 membersA membersS)))

                             _
                             false)))
            ))))