blob: ee4a78e1391665c7bb8d88d302b0a3dc11853732 (
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
(.module:
[lux (#- if loop when
n/+ n/- n/* n// n/% n/= n/< n/<= n/> n/>=
i/+ i/- i/* i// i/% i/= i/< i/<= i/> i/>=
r/+ r/- r/* r// r/% r/= r/< r/<= r/> r/>=
f/+ f/- f/* f// f/% f/= f/< f/<= f/> f/>=)
[control
["p" parser ("parser/." Monad<Parser>)]
["." monad]]
[data
["." text
format]
["." maybe ("maybe/." Monad<Maybe>)]
[collection
["." list ("list/." Fold<List> Functor<List>)]]]
["." macro (#+ with-gensyms Monad<Meta>)
["." code]
["s" syntax (#+ syntax:)]
[syntax
["cs" common]
[common
["csr" reader]
["csw" writer]]]]])
## [Syntax]
(type: Alias [Text Code])
(type: Stack
{#bottom (Maybe Nat)
#top (List Code)})
(def: aliases^
(s.Syntax (List Alias))
(|> (p.and s.local-identifier s.any)
p.some
s.record
(p.default (list))))
(def: bottom^
(s.Syntax Nat)
(s.form (p.after (s.this (` #.Parameter)) s.nat)))
(def: stack^
(s.Syntax Stack)
(p.either (p.and (p.maybe bottom^)
(s.tuple (p.some s.any)))
(p.and (|> bottom^ (parser/map (|>> #.Some)))
(parser/wrap (list)))))
(def: (stack-fold tops bottom)
(-> (List Code) Code Code)
(list/fold (function (_ top bottom)
(` [(~ bottom) (~ top)]))
bottom
tops))
(def: (singleton expander)
(-> (Meta (List Code)) (Meta Code))
(monad.do Monad<Meta>
[expansion expander]
(case expansion
(#.Cons singleton #.Nil)
(wrap singleton)
_
(macro.fail (format "Cannot expand to more than a single AST/Code node:\n"
(|> expansion (list/map %code) (text.join-with " ")))))))
(syntax: #export (=> {aliases aliases^}
{inputs stack^}
{outputs stack^})
(let [de-alias (function (_ aliased)
(list/fold (function (_ [from to] pre)
(code.replace (code.local-identifier from) to pre))
aliased
aliases))]
(case [(|> inputs (get@ #bottom) (maybe/map (|>> code.nat (~) #.Parameter (`))))
(|> outputs (get@ #bottom) (maybe/map (|>> code.nat (~) #.Parameter (`))))]
[(#.Some bottomI) (#.Some bottomO)]
(monad.do @
[inputC (singleton (macro.expand-all (stack-fold (get@ #top inputs) bottomI)))
outputC (singleton (macro.expand-all (stack-fold (get@ #top outputs) bottomO)))]
(wrap (list (` (-> (~ (de-alias inputC))
(~ (de-alias outputC)))))))
[?bottomI ?bottomO]
(with-gensyms [g!stack]
(monad.do @
[inputC (singleton (macro.expand-all (stack-fold (get@ #top inputs) (maybe.default g!stack ?bottomI))))
outputC (singleton (macro.expand-all (stack-fold (get@ #top outputs) (maybe.default g!stack ?bottomO))))]
(wrap (list (` (All [(~ g!stack)]
(-> (~ (de-alias inputC))
(~ (de-alias outputC))))))))))))
(def: begin! Any [])
(def: end!
(All [a] (-> [Any a] a))
(function (_ [_ top])
top))
(def: (prepare command)
(-> Code Code)
(case command
(^or [_ (#.Bit _)]
[_ (#.Nat _)] [_ (#.Int _)]
[_ (#.Rev _)] [_ (#.Frac _)]
[_ (#.Text _)]
[_ (#.Tag _)] (^ [_ (#.Form (list [_ (#.Tag _)]))]))
(` (..push (~ command)))
[_ (#.Tuple block)]
(` (..push (|>> (~+ (list/map prepare block)))))
_
command))
(syntax: #export (||> {commands (p.some s.any)})
(wrap (list (` (|> (~! ..begin!) (~+ (list/map prepare commands)) (~! ..end!))))))
(syntax: #export (word:
{export csr.export}
{name s.local-identifier}
{annotations (p.default cs.empty-annotations csr.annotations)}
type
{commands (p.some s.any)})
(wrap (list (` (def: (~+ (csw.export export)) (~ (code.local-identifier name))
(~ (csw.annotations annotations))
(~ type)
(|>> (~+ (list/map prepare commands))))))))
(syntax: #export (apply {arity (|> s.nat (p.filter (.n/> +0)))})
(with-gensyms [g! g!func g!stack g!output]
(monad.do @
[g!inputs (|> (macro.gensym "input") (list.repeat arity) (monad.seq @))]
(wrap (list (` (: (All [(~+ g!inputs) (~ g!output)]
(-> (-> (~+ g!inputs) (~ g!output))
(=> [(~+ g!inputs)] [(~ g!output)])))
(function ((~ g!) (~ g!func))
(function ((~ g!) (~ (stack-fold g!inputs g!stack)))
[(~ g!stack) ((~ g!func) (~+ g!inputs))])))))))))
## [Primitives]
(def: #export apply1 (apply +1))
(def: #export apply2 (apply +2))
(def: #export apply3 (apply +3))
(def: #export apply4 (apply +4))
(def: #export apply5 (apply +5))
(def: #export apply6 (apply +6))
(def: #export apply7 (apply +7))
(def: #export apply8 (apply +8))
(def: #export (push x)
(All [a] (-> a (=> [] [a])))
(function (_ stack)
[stack x]))
(def: #export drop
(All [t] (=> [t] []))
(function (_ [stack top])
stack))
(def: #export nip
(All [_ a] (=> [_ a] [a]))
(function (_ [[stack _] top])
[stack top]))
(def: #export dup
(All [a] (=> [a] [a a]))
(function (_ [stack top])
[[stack top] top]))
(def: #export swap
(All [a b] (=> [a b] [b a]))
(function (_ [[stack l] r])
[[stack r] l]))
(def: #export rotL
(All [a b c] (=> [a b c] [b c a]))
(function (_ [[[stack a] b] c])
[[[stack b] c] a]))
(def: #export rotR
(All [a b c] (=> [a b c] [c a b]))
(function (_ [[[stack a] b] c])
[[[stack c] a] b]))
(def: #export &&
(All [a b] (=> [a b] [(& a b)]))
(function (_ [[stack l] r])
[stack [l r]]))
(def: #export ||L
(All [a b] (=> [a] [(| a b)]))
(function (_ [stack l])
[stack (+0 l)]))
(def: #export ||R
(All [a b] (=> [b] [(| a b)]))
(function (_ [stack r])
[stack (+1 r)]))
(do-template [<input> <output> <word> <func>]
[(def: #export <word>
(=> [<input> <input>] [<output>])
(function (_ [[stack subject] param])
[stack (<func> param subject)]))]
[Nat Nat n/+ .n/+]
[Nat Nat n/- .n/-]
[Nat Nat n/* .n/*]
[Nat Nat n// .n//]
[Nat Nat n/% .n/%]
[Nat Bit n/= .n/=]
[Nat Bit n/< .n/<]
[Nat Bit n/<= .n/<=]
[Nat Bit n/> .n/>]
[Nat Bit n/>= .n/>=]
[Int Int i/+ .i/+]
[Int Int i/- .i/-]
[Int Int i/* .i/*]
[Int Int i// .i//]
[Int Int i/% .i/%]
[Int Bit i/= .i/=]
[Int Bit i/< .i/<]
[Int Bit i/<= .i/<=]
[Int Bit i/> .i/>]
[Int Bit i/>= .i/>=]
[Rev Rev r/+ .r/+]
[Rev Rev r/- .r/-]
[Rev Rev r/* .r/*]
[Rev Rev r// .r//]
[Rev Rev r/% .r/%]
[Rev Bit r/= .r/=]
[Rev Bit r/< .r/<]
[Rev Bit r/<= .r/<=]
[Rev Bit r/> .r/>]
[Rev Bit r/>= .r/>=]
[Frac Frac f/+ .f/+]
[Frac Frac f/- .f/-]
[Frac Frac f/* .f/*]
[Frac Frac f// .f//]
[Frac Frac f/% .f/%]
[Frac Bit f/= .f/=]
[Frac Bit f/< .f/<]
[Frac Bit f/<= .f/<=]
[Frac Bit f/> .f/>]
[Frac Bit f/>= .f/>=]
)
(def: #export if
(All [__a __b]
(=> {then (=> __a __b)
else (=> __a __b)}
__a [Bit then else] __b))
(function (_ [[[stack test] then] else])
(.if test
(then stack)
(else stack))))
(def: #export call
(All [__a __b]
(=> {quote (=> __a __b)}
__a [quote] __b))
(function (_ [stack block])
(block stack)))
(def: #export loop
(All [___]
(=> {test (=> ___ ___ [Bit])}
___ [test] ___))
(function (loop [stack pred])
(let [[stack' verdict] (pred stack)]
(.if verdict
(loop [stack' pred])
stack'))))
(def: #export dip
(All [___ a]
(=> ___ [a (=> ___ ___)]
___ [a]))
(function (_ [[stack a] quote])
[(quote stack) a]))
(def: #export dip2
(All [___ a b]
(=> ___ [a b (=> ___ ___)]
___ [a b]))
(function (_ [[[stack a] b] quote])
[[(quote stack) a] b]))
(def: #export do
(All [__a __b]
(=> {pred (=> __a __b [Bit])
body (=> __b __a)}
__b [pred body]
__a [pred body]))
(function (_ [[stack pred] body])
[[(body stack) pred] body]))
(def: #export while
(All [__a __b]
(=> {pred (=> __a __b [Bit])
body (=> __b __a)}
__a [pred body]
__b))
(function (while [[stack pred] body])
(let [[stack' verdict] (pred stack)]
(.if verdict
(while [[(body stack') pred] body])
stack'))))
(def: #export compose
(All [__a __b __c]
(=> [(=> __a __b) (=> __b __c)]
[(=> __a __c)]))
(function (_ [[stack f] g])
[stack (|>> f g)]))
(def: #export curry
(All [__a __b a]
(=> __a [a (=> __a [a] __b)]
__a [(=> __a __b)]))
(function (_ [[stack arg] quote])
[stack (|>> (push arg) quote)]))
## [Words]
(word: #export when
(All [___]
(=> {body (=> ___ ___)}
___ [Bit body]
___))
swap [call] [drop] if)
(word: #export ?
(All [a]
(=> [Bit a a] [a]))
rotL [drop] [nip] if)
|