aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/synthesizer.lux
blob: e1eb67bd70f58202d2ec7ce15cf34a729914347b (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
(;module:
  lux
  (lux (control ["p" parser])
       (data [maybe]
             ["e" error]
             [number]
             [product]
             text/format
             (coll [list "list/" Functor<List> Fold<List> Monoid<List>]
                   [dict #+ Dict]))
       (meta [code]
             ["s" syntax]))
  (luxc ["&" base]
        (lang ["la" analysis]
              ["ls" synthesis])
        (synthesizer ["&&;" structure]
                     ["&&;" case]
                     ["&&;" function]
                     ["&&;" loop])
        ))

(def: init-env (List ls;Variable) (list))
(def: init-resolver (Dict Int Int) (dict;new number;Hash<Int>))

(def: (prepare-body inner-arity arity body)
  (-> Nat Nat ls;Synthesis ls;Synthesis)
  (if (&&function;nested? inner-arity)
    body
    (&&loop;reify-recursion arity body)))

(def: (parse-environment env)
  (-> (List Code) (e;Error (List ls;Variable)))
  (s;run env (p;some s;int)))

(def: (let$ register inputS bodyS)
  (-> Nat ls;Synthesis ls;Synthesis ls;Synthesis)
  (` ("lux let" (~ (code;nat register)) (~ inputS) (~ bodyS))))

(def: (if$ testS thenS elseS)
  (-> ls;Synthesis ls;Synthesis ls;Synthesis ls;Synthesis)
  (` ("lux if" (~ testS)
      (~ thenS)
      (~ elseS))))

(def: (function$ arity environment body)
  (-> ls;Arity (List ls;Variable) ls;Synthesis ls;Synthesis)
  (` ("lux function" (~ (code;nat arity))
      [(~@ (list/map code;int environment))]
      (~ body))))

(def: (variant$ tag last? valueS)
  (-> Nat Bool ls;Synthesis ls;Synthesis)
  (` ((~ (code;nat tag)) (~ (code;bool last?)) (~ valueS))))

(def: (var$ var)
  (-> ls;Variable ls;Synthesis)
  (` ((~ (code;int var)))))

(def: (procedure$ name argsS)
  (-> Text (List ls;Synthesis) ls;Synthesis)
  (` ((~ (code;text name)) (~@ argsS))))

(def: (call$ funcS argsS)
  (-> ls;Synthesis (List ls;Synthesis) ls;Synthesis)
  (` ("lux call" (~ funcS) (~@ argsS))))

(def: (synthesize-case synthesize inputA branchesA)
  (-> (-> la;Analysis ls;Synthesis)
      la;Analysis (List [la;Pattern la;Analysis])
      ls;Synthesis)
  (let [inputS (synthesize inputA)]
    (case (list;reverse branchesA)
      (^multi (^ (list [(#la;BindP input-register)
                        (#la;Variable (#;Local output-register))]))
              (n.= input-register output-register))
      inputS

      (^ (list [(#la;BindP register) bodyA]))
      (let$ register inputS (synthesize bodyA))
      
      (^or (^ (list [(#la;BoolP true) thenA] [(#la;BoolP false) elseA]))
           (^ (list [(#la;BoolP false) elseA] [(#la;BoolP true) thenA])))
      (if$ inputS (synthesize thenA) (synthesize elseA))

      (#;Cons [lastP lastA] prevsPA)
      (let [transform-branch (: (-> la;Pattern la;Analysis ls;Path)
                                (function [pattern expr]
                                  (|> (synthesize expr)
                                      (~) ("lux case exec")
                                      ("lux case seq" (~ (&&case;path pattern)))
                                      (`))))]
        (` ("lux case" (~ inputS)
            (~ (list/fold &&case;weave
                          (transform-branch lastP lastA)
                          (list/map (product;uncurry transform-branch) prevsPA))))))

      _
      (undefined)
      )))

(def: #export (synthesize analysis)
  (-> la;Analysis ls;Synthesis)
  (loop [outer-arity +0
         resolver init-resolver
         num-locals +0
         exprA analysis]
    (case exprA
      #la;Unit
      (' [])

      (^template [<from> <to>]
        (<from> value)
        (<to> value))
      ([#la;Bool       code;bool]
       [#la;Nat        code;nat]
       [#la;Int        code;int]
       [#la;Deg        code;deg]
       [#la;Frac       code;frac]
       [#la;Text       code;text]
       [#la;Definition code;symbol])

      (#la;Product _)
      (` [(~@ (list/map (recur +0 resolver num-locals) (&&structure;unfold-tuple exprA)))])

      (#la;Sum choice)
      (let [[tag last? value] (&&structure;unfold-variant choice)]
        (variant$ tag last? (recur +0 resolver num-locals value)))

      (#la;Variable ref)
      (case ref
        (#;Local register)
        (if (&&function;nested? outer-arity)
          (if (n.= +0 register)
            (call$ (var$ 0) (|> (list;n.range +1 (n.dec outer-arity))
                                (list/map (|>. &&function;to-local code;int (~) () (`)))))
            (var$ (&&function;adjust-var outer-arity (&&function;to-local register))))
          (var$ (&&function;to-local register)))
        
        (#;Captured register)
        (var$ (let [var (&&function;to-captured register)]
                (maybe;default var (dict;get var resolver)))))

      (#la;Case inputA branchesA)
      (synthesize-case (recur +0 resolver num-locals) inputA branchesA)
      
      (#la;Function scope bodyA)
      (let [inner-arity (n.inc outer-arity)
            raw-env (&&function;environment scope)
            env (list/map (function [var] (maybe;default var (dict;get var resolver))) raw-env)
            env-vars (let [env-size (list;size raw-env)]
                       (: (List ls;Variable)
                          (case env-size
                            +0 (list)
                            _ (list/map &&function;to-captured (list;n.range +0 (n.dec env-size))))))
            resolver' (if (&&function;nested? inner-arity)
                        (list/fold (function [[from to] resolver']
                                     (dict;put from to resolver'))
                                   init-resolver
                                   (list;zip2 env-vars env))
                        (list/fold (function [var resolver']
                                     (dict;put var var resolver'))
                                   init-resolver
                                   env-vars))]
        (case (recur inner-arity resolver' +0 bodyA)
          (^ [_ (#;Form (list [_ (#;Text "lux function")] [_ (#;Nat arity')] env' bodyS'))])
          (let [arity (n.inc arity')]
            (function$ arity env (prepare-body inner-arity arity bodyS')))

          bodyS
          (function$ +1 env (prepare-body inner-arity +1 bodyS))))

      (#la;Apply _)
      (let [[funcA argsA] (&&function;unfold-apply exprA)
            funcS (recur +0 resolver num-locals funcA)
            argsS (list/map (recur +0 resolver num-locals) argsA)]
        (case funcS
          (^multi (^ [_ (#;Form (list [_ (#;Text "lux function")] [_ (#;Nat _arity)] [_ (#;Tuple _env)] _bodyS))])
                  (and (n.= _arity (list;size argsS))
                       (not (&&loop;contains-self-reference? _bodyS)))
                  [(parse-environment _env) (#e;Success _env)])
          (let [register-offset (if (&&function;top? outer-arity)
                                  num-locals
                                  (|> outer-arity n.inc (n.+ num-locals)))]
            (` ("lux loop" (~ (code;nat register-offset)) [(~@ argsS)]
                (~ (&&loop;adjust _env register-offset _bodyS)))))

          (^ [_ (#;Form (list& [_ (#;Text "lux call")] funcS' argsS'))])
          (call$ funcS' (list/compose argsS' argsS))

          _
          (call$ funcS argsS)))

      (#la;Procedure name args)
      (procedure$ name (list/map (recur +0 resolver num-locals) args))
      )))