aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/tool/compiler/phase/analysis/case.lux
blob: 6f5a324cd817c4b5ba8b6020cb702be8221be07a (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
(.module:
  [lux #*
   [abstract ["." monad (#+ do)]]
   [data
    text/format
    ["." name ("#@." equivalence)]]
   ["r" math/random (#+ Random) ("#@." monad)]
   ["_" test (#+ Test)]
   [control
    pipe]
   [data
    ["." product]
    ["." maybe]
    ["." text ("#@." equivalence)]
    [collection
     ["." list ("#@." monad)]
     ["." set]]]
   ["." type
    ["." check]]
   [macro
    ["." code]]]
  [//
   ["_." primitive]
   ["_." structure]]
  {1
   ["." /
    ["/#" //
     ["#." module]
     ["#." type]
     ["/#" //
      ["/#" //
       ["#." analysis (#+ Analysis Variant Tag Operation)]]]]]})

(def: (exhaustive-weaving branchings)
  (-> (List (List Code)) (List (List Code)))
  (case branchings
    #.Nil
    #.Nil

    (#.Cons head+ #.Nil)
    (list@map (|>> list) head+)

    (#.Cons head+ tail++)
    (do list.monad
      [tail+ (exhaustive-weaving tail++)
       head head+]
      (wrap (#.Cons head tail+)))))

(def: #export (exhaustive-branches allow-literals? variantTC inputC)
  (-> Bit (List [Code Code]) Code (Random (List Code)))
  (case inputC
    [_ (#.Bit _)]
    (r@wrap (list (' #0) (' #1)))

    (^template [<tag> <gen> <wrapper>]
      [_ (<tag> _)]
      (if allow-literals?
        (do r.monad
          [?sample (r.maybe <gen>)]
          (case ?sample
            (#.Some sample)
            (do @
              [else (exhaustive-branches allow-literals? variantTC inputC)]
              (wrap (list& (<wrapper> sample) else)))

            #.None
            (wrap (list (' _)))))
        (r@wrap (list (' _)))))
    ([#.Nat  r.nat         code.nat]
     [#.Int  r.int         code.int]
     [#.Rev  r.rev         code.rev]
     [#.Frac r.frac        code.frac]
     [#.Text (r.unicode 5) code.text])
    
    (^ [_ (#.Tuple (list))])
    (r@wrap (list (' [])))

    [_ (#.Tuple members)]
    (do r.monad
      [member-wise-patterns (monad.map @ (exhaustive-branches allow-literals? variantTC) members)]
      (wrap (|> member-wise-patterns
                exhaustive-weaving
                (list@map code.tuple))))

    (^ [_ (#.Record (list))])
    (r@wrap (list (' {})))

    [_ (#.Record kvs)]
    (do r.monad
      [#let [ks (list@map product.left kvs)
             vs (list@map product.right kvs)]
       member-wise-patterns (monad.map @ (exhaustive-branches allow-literals? variantTC) vs)]
      (wrap (|> member-wise-patterns
                exhaustive-weaving
                (list@map (|>> (list.zip2 ks) code.record)))))

    (^ [_ (#.Form (list [_ (#.Tag _)] _))])
    (do r.monad
      [bundles (monad.map @
                          (function (_ [_tag _code])
                            (do @
                              [v-branches (exhaustive-branches allow-literals? variantTC _code)]
                              (wrap (list@map (function (_ pattern) (` ((~ _tag) (~ pattern))))
                                              v-branches))))
                          variantTC)]
      (wrap (list@join bundles)))

    _
    (r@wrap (list))
    ))

(def: #export (input variant-tags record-tags primitivesC)
  (-> (List Code) (List Code) (List Code) (Random Code))
  (r.rec
   (function (_ input)
     ($_ r.either
         (r@map product.right _primitive.primitive)
         (do r.monad
           [choice (|> r.nat (:: @ map (n/% (list.size variant-tags))))
            #let [choiceT (maybe.assume (list.nth choice variant-tags))
                  choiceC (maybe.assume (list.nth choice primitivesC))]]
           (wrap (` ((~ choiceT) (~ choiceC)))))
         (do r.monad
           [size (|> r.nat (:: @ map (n/% 3)))
            elems (r.list size input)]
           (wrap (code.tuple elems)))
         (r@wrap (code.record (list.zip2 record-tags primitivesC)))
         ))))

(def: (branch body pattern)
  (-> Code Code [Code Code])
  [pattern body])

(def: #export test
  (<| (_.context (name.module (name-of /._)))
      (do r.monad
        [module-name (r.unicode 5)
         variant-name (r.unicode 5)
         record-name (|> (r.unicode 5) (r.filter (|>> (text@= variant-name) not)))
         size (|> r.nat (:: @ map (|>> (n/% 10) (n/max 2))))
         variant-tags (|> (r.set text.hash size (r.unicode 5)) (:: @ map set.to-list))
         record-tags (|> (r.set text.hash size (r.unicode 5)) (:: @ map set.to-list))
         primitivesTC (r.list size _primitive.primitive)
         #let [primitivesT (list@map product.left primitivesTC)
               primitivesC (list@map product.right primitivesTC)
               code-tag (|>> [module-name] code.tag)
               variant-tags+ (list@map code-tag variant-tags)
               record-tags+ (list@map code-tag record-tags)
               variantTC (list.zip2 variant-tags+ primitivesC)]
         inputC (input variant-tags+ record-tags+ primitivesC)
         [outputT outputC] (r.filter (|>> product.left (is? Any) not)
                                     _primitive.primitive)
         #let [analyse-pm (|>> (/.case _primitive.phase inputC)
                               (//type.with-type outputT)
                               ////analysis.with-scope
                               (do ///.monad
                                 [_ (//module.declare-tags variant-tags false
                                                           (#.Named [module-name variant-name]
                                                                    (type.variant primitivesT)))
                                  _ (//module.declare-tags record-tags false
                                                           (#.Named [module-name record-name]
                                                                    (type.tuple primitivesT)))])
                               (//module.with-module 0 module-name))]
         exhaustive-patterns (exhaustive-branches true variantTC inputC)
         #let [exhaustive-branchesC (list@map (branch outputC)
                                              exhaustive-patterns)]]
        ($_ _.and
            (_.test "Will reject empty pattern-matching (no branches)."
                    (|> (analyse-pm (list))
                        _structure.check-fails))
            (_.test "Can analyse exhaustive pattern-matching."
                    (|> (analyse-pm exhaustive-branchesC)
                        _structure.check-succeeds))
            (let [non-exhaustive-branchesC (list.take (dec (list.size exhaustive-branchesC))
                                                      exhaustive-branchesC)]
              (_.test "Will reject non-exhaustive pattern-matching."
                      (|> (analyse-pm non-exhaustive-branchesC)
                          _structure.check-fails)))
            (do @
              [redundant-patterns (exhaustive-branches false variantTC inputC)
               redundancy-idx (|> r.nat (:: @ map (n/% (list.size redundant-patterns))))
               #let [redundant-branchesC (<| (list@map (branch outputC))
                                             list.concat
                                             (list (list.take redundancy-idx redundant-patterns)
                                                   (list (maybe.assume (list.nth redundancy-idx redundant-patterns)))
                                                   (list.drop redundancy-idx redundant-patterns)))]]
              (_.test "Will reject redundant pattern-matching."
                      (|> (analyse-pm redundant-branchesC)
                          _structure.check-fails)))
            (do @
              [[heterogeneousT heterogeneousC] (r.filter (|>> product.left (check.checks? outputT) not)
                                                         _primitive.primitive)
               heterogeneous-idx (|> r.nat (:: @ map (n/% (list.size exhaustive-patterns))))
               #let [heterogeneous-branchesC (list.concat (list (list.take heterogeneous-idx exhaustive-branchesC)
                                                                (list (let [[_pattern _body] (maybe.assume (list.nth heterogeneous-idx exhaustive-branchesC))]
                                                                        [_pattern heterogeneousC]))
                                                                (list.drop (inc heterogeneous-idx) exhaustive-branchesC)))]]
              (_.test "Will reject pattern-matching if the bodies of the branches do not all have the same type."
                      (|> (analyse-pm heterogeneous-branchesC)
                          _structure.check-fails)))
            ))))