aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/synthesis/loop.lux
blob: c00d5626be6f858197bb1fa02a91ec55f2e5257f (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["p" parser])
       (data [maybe]
             (coll [list "list/" Functor<List>]))
       (macro [code]
              [syntax]))
  (luxc (lang ["ls" synthesis]
              [".L" variable #+ Variable Register])))

(def: #export (contains-self-reference? exprS)
  (-> ls.Synthesis Bool)
  (case exprS
    (^ [_ (#.Form (list [_ (#.Nat tag)] [_ (#.Bool last?)] memberS))])
    (contains-self-reference? memberS)

    [_ (#.Tuple membersS)]
    (list.any? contains-self-reference? membersS)

    (^ [_ (#.Form (list [_ (#.Int var)]))])
    (variableL.self? var)

    (^ [_ (#.Form (list [_ (#.Text "lux case")] inputS pathS))])
    (or (contains-self-reference? inputS)
        (loop [pathS pathS]
          (case pathS
            (^or (^ [_ (#.Form (list [_ (#.Text "lux case alt")] leftS rightS))])
                 (^ [_ (#.Form (list [_ (#.Text "lux case seq")] leftS rightS))]))
            (or (recur leftS)
                (recur rightS))

            (^ [_ (#.Form (list [_ (#.Text "lux case exec")] bodyS))])
            (contains-self-reference? bodyS)
            
            _
            false)))

    (^ [_ (#.Form (list [_ (#.Text "lux function")] arity [_ (#.Tuple environment)] bodyS))])
    (list.any? (function (_ captured)
                 (case captured
                   (^ [_ (#.Form (list [_ (#.Int var)]))])
                   (variableL.self? var)

                   _
                   false))
               environment)

    (^ [_ (#.Form (list& [_ (#.Text "lux call")] funcS argsS))])
    (or (contains-self-reference? funcS)
        (list.any? contains-self-reference? argsS))

    (^ [_ (#.Form (list [_ (#.Text "lux let")] register inputS bodyS))])
    (or (contains-self-reference? inputS)
        (contains-self-reference? bodyS))

    (^ [_ (#.Form (list [_ (#.Text "lux if")] inputS thenS elseS))])
    (or (contains-self-reference? inputS)
        (contains-self-reference? thenS)
        (contains-self-reference? elseS))

    (^ [_ (#.Form (list [_ (#.Text "lux loop")] offset [_ (#.Tuple initsS)] bodyS))])
    (or (list.any? contains-self-reference? initsS)
        (contains-self-reference? bodyS))
    
    (^or (^ [_ (#.Form (list& [_ (#.Text "lux recur")] argsS))])
         (^ [_ (#.Form (list& [_ (#.Text procedure)] argsS))]))
    (list.any? contains-self-reference? argsS)

    _
    false
    ))

(def: #export (reify-recursion arity exprS)
  (-> Nat ls.Synthesis ls.Synthesis)
  (loop [exprS exprS]
    (case exprS
      (^ [_ (#.Form (list [_ (#.Text "lux case")] inputS pathS))])
      (` ("lux case" (~ inputS)
          (~ (let [reify-recursion' recur]
               (loop [pathS pathS]
                 (case pathS
                   (^ [_ (#.Form (list [_ (#.Text "lux case alt")] leftS rightS))])
                   (` ("lux case alt" (~ (recur leftS)) (~ (recur rightS))))

                   (^ [_ (#.Form (list [_ (#.Text "lux case seq")] leftS rightS))])
                   (` ("lux case seq" (~ leftS) (~ (recur rightS))))
                   
                   (^ [_ (#.Form (list [_ (#.Text "lux case exec")] bodyS))])
                   (` ("lux case exec" (~ (reify-recursion' bodyS))))
                   
                   _
                   pathS))))))

      (^multi (^ [_ (#.Form (list& [_ (#.Text "lux call")]
                                   [_ (#.Form (list [_ (#.Int 0)]))]
                                   argsS))])
              (n/= arity (list.size argsS)))
      (` ("lux recur" (~+ argsS)))

      (^ [_ (#.Form (list [_ (#.Text "lux let")] register inputS bodyS))])
      (` ("lux let" (~ register) (~ inputS) (~ (recur bodyS))))

      (^ [_ (#.Form (list [_ (#.Text "lux if")] inputS thenS elseS))])
      (` ("lux if" (~ inputS) (~ (recur thenS)) (~ (recur elseS))))

      _
      exprS
      )))

(def: #export (adjust env offset exprS)
  (-> (List Variable) Register ls.Synthesis ls.Synthesis)
  (let [resolve-captured (: (-> Variable Variable)
                            (function (_ var)
                              (let [idx (|> var (i/* -1) int-to-nat n/dec)]
                                (|> env (list.nth idx) maybe.assume))))]
    (loop [exprS exprS]
      (case exprS
        (^code ((~ [_ (#.Nat tag)]) (~ last?) (~ valueS)))
        (` ((~ (code.nat tag)) (~ last?) (~ (recur valueS))))
        
        (^code [(~+ members)])
        (` [(~+ (list/map recur members))])

        (^code ("lux case" (~ inputS) (~ pathS)))
        (` ("lux case" (~ (recur inputS))
            (~ (let [adjust' recur]
                 (loop [pathS pathS]
                   (case pathS
                     (^template [<pattern>]
                       (^ [_ (#.Form (list [_ (#.Text <pattern>)] leftS rightS))])
                       (` (<pattern> (~ (recur leftS)) (~ (recur rightS)))))
                     (["lux case alt"]
                      ["lux case seq"])

                     (^code ("lux case bind" (~ [_ (#.Nat register)])))
                     (` ("lux case bind" (~ (code.nat (n/+ offset register)))))

                     (^ [_ (#.Form (list [_ (#.Text "lux case exec")] bodyS))])
                     (` ("lux case exec" (~ (adjust' bodyS))))
                     
                     _
                     pathS))))))

        (^code ("lux function" (~ arity) [(~+ environment)] (~ bodyS)))
        (` ("lux function" (~ arity)
            [(~+ (list/map (function (_ _var)
                             (case _var
                               (^ [_ (#.Form (list [_ (#.Int var)]))])
                               (` ((~ (code.int (resolve-captured var)))))

                               _
                               _var))
                           environment))]
            (~ bodyS)))

        (^ [_ (#.Form (list& [_ (#.Text "lux call")] funcS argsS))])
        (` ("lux call" (~ (recur funcS)) (~+ (list/map recur argsS))))

        (^ [_ (#.Form (list& [_ (#.Text "lux recur")] argsS))])
        (` ("lux recur" (~+ (list/map recur argsS))))
        
        (^code ("lux let" (~ [_ (#.Nat register)]) (~ inputS) (~ bodyS)))
        (` ("lux let" (~ (code.nat (n/+ offset register)))
            (~ (recur inputS))
            (~ (recur bodyS))))

        (^ [_ (#.Form (list [_ (#.Text "lux if")] inputS thenS elseS))])
        (` ("lux if" (~ (recur inputS))
            (~ (recur thenS))
            (~ (recur elseS))))

        (^ [_ (#.Form (list [_ (#.Text "lux loop")] [_ (#.Nat loop-offset)] [_ (#.Tuple initsS)] bodyS))])
        (` ("lux loop" (~ (code.nat (n/+ offset loop-offset)))
            [(~+ (list/map recur initsS))]
            (~ (recur bodyS))))

        (^ [_ (#.Form (list [_ (#.Int var)]))])
        (if (variableL.captured? var)
          (` ((~ (code.int (resolve-captured var)))))
          (` ((~ (code.int (|> offset nat-to-int (i/+ var)))))))

        (^ [_ (#.Form (list& [_ (#.Text procedure)] argsS))])
        (` ((~ (code.text procedure)) (~+ (list/map recur argsS))))

        _
        exprS
        ))))