aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/compiler/synthesis/case.lux
blob: 88bfd36e9d757b3ae1f9a2c3fde8c9807de89788 (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
(.module:
  lux
  (lux (control [equality #+ Eq]
                pipe
                [monad #+ do])
       (data [product]
             [bool "bool/" Eq<Bool>]
             [text "text/" Eq<Text>]
             text/format
             [number "frac/" Eq<Frac>]
             (coll [list "list/" Fold<List> Monoid<List>])))
  [///reference]
  [///compiler #+ Operation "operation/" Monad<Operation>]
  [///analysis #+ Pattern Match Analysis]
  [// #+ Path Synthesis]
  [//function])

(def: (path' pattern bodyC)
  (-> Pattern (Operation //.State Path) (Operation //.State Path))
  (case pattern
    (#///analysis.Simple simple)
    (case simple
      #///analysis.Unit
      bodyC
      
      (^template [<from> <to>]
        (<from> value)
        (operation/map (|>> (#//.Seq (#//.Test (|> value <to>))))
                       bodyC))
      ([#///analysis.Bool #//.Bool]
       [#///analysis.Nat  (<| #//.I64 .i64)]
       [#///analysis.Int  (<| #//.I64 .i64)]
       [#///analysis.Rev  (<| #//.I64 .i64)]
       [#///analysis.Frac #//.F64]
       [#///analysis.Text #//.Text]))
    
    (#///analysis.Bind register)
    (<| (do ///compiler.Monad<Operation>
          [arity //.scope-arity])
        (:: @ map (|>> (#//.Seq (#//.Bind (if (//function.nested? arity)
                                            (n/+ (dec arity) register)
                                            register)))))
        //.with-new-local
        bodyC)

    (#///analysis.Complex _)
    (case (///analysis.variant-pattern pattern)
      (#.Some [lefts right? value-pattern])
      (operation/map (|>> (#//.Seq (#//.Access (#//.Side (if right?
                                                           (#.Right lefts)
                                                           (#.Left lefts))))))
                     (path' value-pattern bodyC))
      
      #.None
      (let [tuple (///analysis.tuple-pattern pattern)
            tuple/last (dec (list.size tuple))]
        (list/fold (function (_ [tuple/idx tuple/member] thenC)
                     (case tuple/member
                       (#///analysis.Simple #///analysis.Unit)
                       thenC

                       _
                       (let [last? (n/= tuple/last tuple/idx)]
                         (|> (if (or last?
                                     (is? bodyC thenC))
                               thenC
                               (operation/map (|>> (#//.Seq #//.Pop)) thenC))
                             (path' tuple/member)
                             (operation/map (|>> (#//.Seq (#//.Access (#//.Member (if last?
                                                                                    (#.Right (dec tuple/idx))
                                                                                    (#.Left tuple/idx)))))))))))
                   bodyC
                   (list.reverse (list.enumerate tuple)))))))

(def: #export (path synthesize pattern bodyA)
  (-> //.Synthesizer Pattern Analysis (Operation //.State Path))
  (path' pattern (operation/map (|>> #//.Then) (synthesize bodyA))))

(def: #export (weave leftP rightP)
  (-> Path Path Path)
  (with-expansions [<default> (as-is (#//.Alt leftP rightP))]
    (case [leftP rightP]
      [(#//.Seq preL postL)
       (#//.Seq preR postR)]
      (case (weave preL preR)
        (#//.Alt _)
        <default>

        weavedP
        (#//.Seq weavedP (weave postL postR)))

      [#//.Pop #//.Pop]
      rightP

      (^template [<tag> <eq>]
        [(#//.Test (<tag> leftV))
         (#//.Test (<tag> rightV))]
        (if (<eq> leftV rightV)
          rightP
          <default>))
      ([#//.Bool bool/=]
       [#//.I64 (:coerce (Eq I64) i/=)]
       [#//.F64 frac/=]
       [#//.Text text/=])

      (^template [<access> <side>]
        [(#//.Access (<access> (<side> leftL)))
         (#//.Access (<access> (<side> rightL)))]
        (if (n/= leftL rightL)
          rightP
          <default>))
      ([#//.Side #.Left]
       [#//.Side #.Right]
       [#//.Member #.Left]
       [#//.Member #.Right])

      [(#//.Bind leftR) (#//.Bind rightR)]
      (if (n/= leftR rightR)
        rightP
        <default>)

      _
      <default>)))

(def: #export (synthesize synthesize^ inputA [headB tailB+])
  (-> //.Synthesizer Analysis Match (Operation //.State Synthesis))
  (do ///compiler.Monad<Operation>
    [inputS (synthesize^ inputA)]
    (with-expansions [<unnecesary-let>
                      (as-is (^multi (^ (#///analysis.Reference (///reference.local outputR)))
                                     (n/= inputR outputR))
                             (wrap inputS))

                      <let>
                      (as-is [[(#///analysis.Bind inputR) headB/bodyA]
                              #.Nil]
                             (case headB/bodyA
                               <unnecesary-let>

                               _
                               (do @
                                 [arity //.scope-arity
                                  headB/bodyS (//.with-new-local
                                                (synthesize^ headB/bodyA))]
                                 (wrap (//.branch/let [inputS
                                                       (if (//function.nested? arity)
                                                         (n/+ (dec arity) inputR)
                                                         inputR)
                                                       headB/bodyS])))))

                      <if>
                      (as-is (^or (^ [[(///analysis.pattern/bool true) thenA]
                                      (list [(///analysis.pattern/bool false) elseA])])
                                  (^ [[(///analysis.pattern/bool false) elseA]
                                      (list [(///analysis.pattern/bool true) thenA])]))
                             (do @
                               [thenS (synthesize^ thenA)
                                elseS (synthesize^ elseA)]
                               (wrap (//.branch/if [inputS thenS elseS]))))

                      <case>
                      (as-is _
                             (let [[[lastP lastA] prevsPA] (|> (#.Cons headB tailB+)
                                                               list.reverse
                                                               (case> (#.Cons [lastP lastA] prevsPA)
                                                                      [[lastP lastA] prevsPA]

                                                                      _
                                                                      (undefined)))]
                               (do @
                                 [lastSP (path synthesize^ lastP lastA)
                                  prevsSP+ (monad.map @ (product.uncurry (path synthesize^)) prevsPA)]
                                 (wrap (//.branch/case [inputS (list/fold weave lastSP prevsSP+)])))))]
      (case [headB tailB+]
        <let>
        <if>
        <case>))))