aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/concatenative.lux
blob: 84383bba437120c6f535067a62329435351341d9 (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
342
343
344
(.using
 [library
  [lux {"-" Alias if loop}
   ["[0]" meta]
   [abstract
    ["[0]" monad]]
   [control
    ["[0]" maybe ("[1]#[0]" monad)]]
   [data
    ["[0]" text
     ["%" format {"+" format}]]
    [collection
     ["[0]" list ("[1]#[0]" mix functor)]]]
   ["[0]" macro {"+" with_symbols}
    ["[0]" code]
    ["[0]" template]
    [syntax {"+" syntax:}
     ["|[0]|" export]]]
   [math
    [number
     ["n" nat]
     ["i" int]
     ["r" rev]
     ["f" frac]]]]]
 [//
  ["<>" parser ("[1]#[0]" monad)
   ["<[0]>" code {"+" Parser}]]])

(type: Alias
  [Text Code])

(type: Stack
  (Record
   [#bottom (Maybe Code)
    #top (List Code)]))

(def: aliases^
  (Parser (List Alias))
  (|> (<>.and <code>.local_symbol <code>.any)
      <>.some
      <code>.tuple))

(def: top^
  (Parser (List Code))
  (<code>.tuple (<>.some <code>.any)))

(def: bottom^
  (Parser Code)
  (<code>.not ..top^))

(def: stack^
  (Parser Stack)
  (<>.either (<>.and (<>.maybe bottom^)
                     ..top^)
             (<>.and (<>#each (|>> {.#Some}) bottom^)
                     (<>#in (list)))))

(def: (stack_mix tops bottom)
  (-> (List Code) Code Code)
  (list#mix (function (_ top bottom)
              (` [(~ bottom) (~ top)]))
            bottom
            tops))

(def: (singleton expander)
  (-> (Meta (List Code)) (Meta Code))
  (monad.do meta.monad
    [expansion expander]
    (case expansion
      {.#Item singleton {.#End}}
      (in singleton)

      _
      (meta.failure (format "Cannot expand to more than a single AST/Code node:" text.new_line
                            (|> expansion (list#each %.code) (text.interposed " ")))))))

(def: signature^
  (Parser [(List Alias) Stack Stack])
  (<>.either ($_ <>.and aliases^ stack^ stack^)
             ($_ <>.and (<>#in (list)) stack^ stack^)))

(syntax: .public (=> [[aliases inputs outputs] signature^])
  (let [de_alias (function (_ aliased)
                   (list#mix (function (_ [from to] pre)
                               (code.replaced (code.local_symbol from) to pre))
                             aliased
                             aliases))]
    (case [(the #bottom inputs)
           (the #bottom outputs)]
      [{.#Some bottomI} {.#Some bottomO}]
      (monad.do meta.monad
        [inputC (singleton (macro.full_expansion (stack_mix (the #top inputs) bottomI)))
         outputC (singleton (macro.full_expansion (stack_mix (the #top outputs) bottomO)))]
        (in (list (` (-> (~ (de_alias inputC))
                         (~ (de_alias outputC)))))))

      [?bottomI ?bottomO]
      (with_symbols [g!stack]
        (monad.do meta.monad
          [inputC (singleton (macro.full_expansion (stack_mix (the #top inputs) (maybe.else g!stack ?bottomI))))
           outputC (singleton (macro.full_expansion (stack_mix (the #top outputs) (maybe.else g!stack ?bottomO))))]
          (with_symbols [g!_]
            (in (list (` (All ((~ g!_) (~ g!stack))
                           (-> (~ (de_alias inputC))
                               (~ (de_alias outputC)))))))))))))

(def: begin!
  Any
  [])

(def: end!
  (All (_ a) (-> [Any a] a))
  (function (_ [_ top])
    top))

(syntax: .public (||> [commands (<>.some <code>.any)])
  (in (list (` (|> (~! ..begin!) (~+ commands) ((~! ..end!)))))))

(def: word
  (Parser [Code Text Code (List Code)])
  (|export|.parser
   ($_ <>.and
       <code>.local_symbol
       <code>.any
       (<>.many <code>.any))))

(syntax: .public (word: [[export_policy name type commands] ..word])
  (in (list (` (def: (~ export_policy) (~ (code.local_symbol name))
                 (~ type)
                 (|>> (~+ commands)))))))

(syntax: .public (apply [arity (<>.only (n.> 0) <code>.nat)])
  (with_symbols [g!_ g!func g!stack g!output]
    (monad.do [! meta.monad]
      [g!inputs (|> (macro.symbol "input") (list.repeated arity) (monad.all !))]
      (in (list (` (: (All ((~ g!_) (~+ g!inputs) (~ g!output))
                        (-> (-> (~+ g!inputs) (~ g!output))
                            (=> [(~+ g!inputs)] [(~ g!output)])))
                      (function ((~ g!_) (~ g!func))
                        (function ((~ g!_) (~ (stack_mix g!inputs g!stack)))
                          [(~ g!stack) ((~ g!func) (~+ g!inputs))])))))))))

(template [<arity>]
  [(`` (def: .public (~~ (template.symbol ["apply/" <arity>]))
         (..apply <arity>)))]

  [1] [2] [3] [4]
  [5] [6] [7] [8]
  )

(def: .public (push x)
  (All (_ a) (-> a (=> [] [a])))
  (function (_ stack)
    [stack x]))

(def: .public drop
  (All (_ t) (=> [t] []))
  (function (_ [stack top])
    stack))

(def: .public nip
  (All (_ _ a) (=> [_ a] [a]))
  (function (_ [[stack _] top])
    [stack top]))

(def: .public dup
  (All (_ a) (=> [a] [a a]))
  (function (_ [stack top])
    [[stack top] top]))

(def: .public swap
  (All (_ a b) (=> [a b] [b a]))
  (function (_ [[stack l] r])
    [[stack r] l]))

(def: .public rotL
  (All (_ a b c) (=> [a b c] [b c a]))
  (function (_ [[[stack a] b] c])
    [[[stack b] c] a]))

(def: .public rotR
  (All (_ a b c) (=> [a b c] [c a b]))
  (function (_ [[[stack a] b] c])
    [[[stack c] a] b]))

(def: .public &&
  (All (_ a b) (=> [a b] [(Tuple a b)]))
  (function (_ [[stack l] r])
    [stack [l r]]))

(def: .public ||L
  (All (_ a b) (=> [a] [(Or a b)]))
  (function (_ [stack l])
    [stack {0 #0 l}]))

(def: .public ||R
  (All (_ a b) (=> [b] [(Or a b)]))
  (function (_ [stack r])
    [stack {0 #1 r}]))

(template [<input> <output> <word> <func>]
  [(`` (def: .public <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: .public if
  (All (_ ,,,0 ,,,1)
    (=> [then (=> ,,,0 ,,,1)
         else (=> ,,,0 ,,,1)]
        ,,,0 [Bit then else] ,,,1))
  (function (_ [[[stack test] then] else])
    (.if test
      (then stack)
      (else stack))))

(def: .public call
  (All (_ ,,,0 ,,,1)
    (=> [quote (=> ,,,0 ,,,1)]
        ,,,0 [quote] ,,,1))
  (function (_ [stack quote])
    (quote stack)))

(def: .public loop
  (All (_ ,,,)
    (=> [test (=> ,,, ,,, [Bit])]
        ,,, [test] ,,,))
  (function (loop [stack pred])
    (let [[stack' verdict] (pred stack)]
      (.if verdict
        (loop [stack' pred])
        stack'))))

(def: .public dip
  (All (_ ,,, a)
    (=> ,,, [a (=> ,,, ,,,)]
        ,,, [a]))
  (function (_ [[stack a] quote])
    [(quote stack) a]))

(def: .public dip/2
  (All (_ ,,, a b)
    (=> ,,, [a b (=> ,,, ,,,)]
        ,,, [a b]))
  (function (_ [[[stack a] b] quote])
    [[(quote stack) a] b]))

(def: .public do
  (All (_ ,,,0 ,,,1)
    (=> [body (=> ,,,0 ,,,1)
         pred (=> ,,,1 ,,,0 [Bit])]
        ,,,0 [pred body]
        ,,,1 [pred body]))
  (function (_ [[stack pred] body])
    [[(body stack) pred] body]))

(def: .public while
  (All (_ ,,,0 ,,,1)
    (=> [body (=> ,,,1 ,,,0)
         pred (=> ,,,0 ,,,1 [Bit])]
        ,,,0 [pred body]
        ,,,1))
  (function (while [[stack pred] body])
    (let [[stack' verdict] (pred stack)]
      (.if verdict
        (while [[(body stack') pred] body])
        stack'))))

(def: .public compose
  (All (_ ,,,0 ,,, ,,,1)
    (=> [(=> ,,,0 ,,,) (=> ,,, ,,,1)]
        [(=> ,,,0 ,,,1)]))
  (function (_ [[stack f] g])
    [stack (|>> f g)]))

(def: .public partial
  (All (_ ,,,0 ,,,1 a)
    (=> ,,,0 [a (=> ,,,0 [a] ,,,1)]
        ,,,0 [(=> ,,,0 ,,,1)]))
  (function (_ [[stack arg] quote])
    [stack (|>> (push arg) quote)]))

(word: .public when
  (All (_ ,,,)
    (=> [body (=> ,,, ,,,)]
        ,,, [Bit body]
        ,,,))
  swap
  (push ..call)
  (push ..drop)
  if)

(word: .public ?
  (All (_ a)
    (=> [Bit a a] [a]))
  rotL
  (push ..drop)
  (push ..nip)
  if)