Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
(define (<ξ>::defun name args &body body)
  `(define (,name ,@args) ,@body))
(eval-when :compile
  (sys::set-macro 'defun '<ξ>::defun))

(defun <ξ>::defmacro (name args &body body)
  (let ((mac-fn-name (sys::concat-symbols '<ξ>:: name)))
    `(progn
       (define (,mac-fn-name ,@args)
         ,@body)
       (eval-when :compile
         (sys::set-macro ',name ',mac-fn-name)))))
(eval-when :compile
  (sys::set-macro 'defmacro '<ξ>::defmacro))

(defun head (x)
  (car x))
(defun tail (x)
  (cdr x))
(defun caar (x)
  (car (car x)))
(defun cadr (x)
  (car (cdr x)))
(defun cdar (x)
  (cdr (car x)))
(defun cddr (x)
  (cdr (cdr x)))
(defun caaar (x)
  (car (car (car x))))
(defun caadr (x)
  (car (car (cdr x))))
(defun cadar (x)
  (car (cdr (car x))))
(defun caddr (x)
  (car (cdr (cdr x))))
(defun cdaar (x)
  (cdr (car (car x))))
(defun cdadr (x)
  (cdr (car (cdr x))))
(defun cddar (x)
  (cdr (cdr (car x))))
(defun cdddr (x)
  (cdr (cdr (cdr x))))
(defun caaaar (x)
  (car (car (car (car x)))))
(defun caaadr (x)
  (car (car (car (cdr x)))))
(defun caadar (x)
  (car (car (cdr (car x)))))
(defun caaddr (x)
  (car (car (cdr (cdr x)))))
(defun cadaar (x)
  (car (cdr (car (car x)))))
(defun cadadr (x)
  (car (cdr (car (cdr x)))))
(defun caddar (x)
  (car (cdr (cdr (car x)))))
(defun cadddr (x)
  (car (cdr (cdr (cdr x)))))
(defun cdaaar (x)
  (cdr (car (car (car x)))))
(defun cdaadr (x)
  (cdr (car (car (cdr x)))))
(defun cdadar (x)
  (cdr (car (cdr (car x)))))
(defun cdaddr (x)
  (cdr (car (cdr (cdr x)))))
(defun cddaar (x)
  (cdr (cdr (car (car x)))))
(defun cddadr (x)
  (cdr (cdr (car (cdr x)))))
(defun cdddar (x)
  (cdr (cdr (cdr (car x)))))
(defun cddddr (x)
  (cdr (cdr (cdr (cdr x)))))

(defmacro while (cnd &body body)
  `(loop
    (if (not ,cnd)
        (break))
    ,@body))

(defmacro until (cnd &body body)
  `(loop
    (if ,cnd (break))
    ,@body))

(defmacro inc! (var &? n)
  (let ((num (or n 1)))
    `(set ,var (+ ,var ,num))))

(defmacro dec! (var &? n)
  (let ((num (or n 1)))
    `(set ,var (- ,var ,num))))

(defmacro defvar (name value)
  `(intr::define-static ,name ,value))

(defun gensym ()
  (defvar num-syms 0)
  (let ((sym (sys::make-symbol (sys::concat "<β>::#" num-syms))))
    (inc! num-syms)
    sym))

(defmacro when (cnd &body if-true)
  `(if ,cnd
       (progn
         ,@if-true)))

(defmacro unless (cnd &body if-false)
  `(if (not ,cnd)
       (progn
         ,@if-false)))

(defmacro set* (to from)
  (let ((let-set nil)
        (set-set nil))
    (while to
      (let ((tmp-sym (gensym)))
        (set let-set (cons `(,tmp-sym ,(car from)) let-set))
        (set set-set (cons `(set ,(car to) ,tmp-sym) set-set)))
      (set to (cdr to))
      (set from (cdr from)))
    `(let ,let-set
       ,@set-set)))

(defun let*/helper (pairs body)
  (if pairs
      `(let (,(car pairs))
         ,(let*/helper (cdr pairs) body))
    `(progn ,@body)))

(defmacro let* (pairs &body body)
  (let*/helper pairs body))

(defun math/fibonacci (n)
  (let ((a 0) (b 1))
    (while (> n 0)
      (set* (a b) (b (+ a b)))
      (dec! n))
    a))

(defun math/factorial (n)
  (let ((x 1))
    (while (> n 0)
      (set* (x n) ((* x n) (- n 1))))
    x))

(defmacro ++ (var)
  (let ((val (gensym)))
    `(let ((,val ,var))
       (inc! ,var)
       ,val)))

(defmacro -- (var)
  (let ((val (gensym)))
    `(let ((,val ,var))
       (dec! ,var)
       ,val)))

(defun count! ()
  (defvar cnt 0)
  (++ cnt))

(defmacro alias (alias-name name)
  `(defmacro ,alias-name (&rest r)
     (cons ',name r)))

(defmacro fn-alias (alias-name args name)
  `(defun ,alias-name ,args
     (,name ,@args)))

;; Turn (f a b c d) into (f a (f b (f c d)))
(defun <ξ>::2-ary-to-n-ary/helper (fn args)
  (if (and args (cdr args))
      `(,fn ,(car args)
            ,(<ξ>::2-ary-to-n-ary/helper
              fn
              (cdr args)))
      (car args)))

(defmacro 2-ary-to-n-ary (fn alias)
  `(defmacro ,alias (&rest args)
     (<ξ>::2-ary-to-n-ary/helper ',fn
                                 args)))

;; Aliases
(alias % sys::modulo)
(alias ^ sys::pow)
(alias disall sys::disassemble-all)
(alias to-symbol sys::make-symbol)
(alias read sys::read)
(alias string sys::string)
(alias make-symbol sys::make-symbol)
(fn-alias macroexpand (x) sys::macroexpand)
(fn-alias type-of (x) sys::type-of)
(fn-alias disassemble (x) sys::disassemble)
(2-ary-to-n-ary sys::concat-symbols concat-symbols)
(2-ary-to-n-ary sys::concat concat)


(defun eval (expr)
  (sys::eval `(,expr)))

(defmacro load (lib)
  `(eval-when :compile
     (sys::load ,lib)))

;;; FIXME: (next)/(break) don't work inside dolist/range,
;;;        a compiler built-in is needed to fix it.

(defmacro dolist (cnd &body body)
  (let ((name (car cnd))
        (init (car (cdr cnd)))
        (xs (gensym)))
    `(let ((,name nil)
           (,xs ,init))
       (while ,xs
         (set ,name (car ,xs))
         ,@body
         (set ,xs (cdr ,xs))))))

(defmacro range (cnd &body body)
  (let ((loop-var (car cnd))
        (min (caadr cnd))
        (max (cadadr cnd)))
    `(let ((,loop-var ,min))
       (while (< ,loop-var ,max)
         ,@body
         (inc! ,loop-var)))))

(defun range-list (a b)
  (let ((xs nil))
    (range (x (a b))
      (set xs (cons x xs)))
    (reverse xs)))

(defmacro ignore (&body code)
  `(progn ,@code nil))

(defmacro println (&rest args)
  `(ignore (sys::println (concat ,@args))))

(defmacro take! (var)
  (let ((head (gensym)))
    `(let ((,head (car ,var)))
       (set ,var (cdr ,var))
       ,head)))

(defun map (f xs)
  (when xs
    (cons (f (car xs))
          (map f (cdr xs)))))

(defun filter (f xs)
  (when xs
    (let ((x (car xs)))
      (if (f x)
          (cons x (filter f (cdr xs)))
        (filter f (cdr xs))))))

(defun zip (xs ys)
  (when (and xs ys)
    (cons (cons (car xs)
                (car ys))
          (zip (cdr xs)
               (cdr ys)))))

(defun reverse (xs)
  (let ((ys nil))
    (dolist (x xs)
      (set ys (cons x ys)))
    ys))

(defun all? (f xs)
  (not
   (dolist (x xs)
     (unless (f x)
       (break (and))))))

(defun any? (f xs)
  (dolist (x xs)
    (when (f x)
      (break (and)))))

(defun elem? (x ys)
  (dolist (y ys)
    (when (= x y)
      (break (and)))))

(defmacro extreme-value (ord src)
  (let ((m (gensym)) (xs (gensym)) (x (gensym)))
    `(let ((,m (car ,src))
           (,xs (cdr ,src)))
       (dolist (,x ,xs)
         (when (,ord ,x ,m)
           (set ,m ,x)))
       ,m)))

(defun min (xs)
  (extreme-value < xs))

(defun max (xs)
  (extreme-value > xs))

(defun sum (xs)
  (let ((s 0))
    (dolist (x xs)
      (inc! s x))
    s))

(define (pow a b)
  (let ((s 1))
    (range (_ (0 b))
      (set s (* s a)))
    s))

(defun abs (x)
  (if (< x 0)
      (- x)
    x))

(defun mean (xs)
  (/ (sum xs) (len xs)))

(defun id (x)
  x)

(defmacro m-map (m xs)
  (let ((p '()))
    (dolist (x xs)
      (set p (cons `(,m ,x) p)))
    `(progn ,@(reverse p))))

(defmacro make-tcheck (type)
  (let ((name (sys::concat-symbols type '?)))
    `(defun ,name (x)
       (= (type-of x) ,type))))

(m-map make-tcheck (integer symbol
                    unsigned-integer
                    float bool
                    string cons))

(defun number? (x)
  (or (integer? x)
      (float? x)))

(defun nil? (x)
  (= x nil))


(defun equal-cons? (as bs)
  (loop (when (or (= as nil)
                  (= bs nil))
          (break (= as bs)))
   (unless (equal? (car as) (car bs))
     (break false))
   (set as (cdr as))
   (set bs (cdr bs))))

(defun equal? (a b)
  (if (and (cons? a) (cons? b))
      (equal-cons? a b)
    (= a b)))

(defun exit (&? status)
  (sys::exit (or status 'ok)))

(defun dot-node-label (node)
  (make-symbol (or (and (string? node)
                        (concat "'" node "'"))
                   (string
                    (if (cons? node)
                        (car node)
                      node)))))

(defun dot-node-name (idx)
  (concat-symbols 'N_ (make-symbol idx)))

(defun print-ast-dot-rec (tree idx)
  (let ((root-idx (dot-node-name idx))
        (root-label (dot-node-label tree)))
    (println "    " root-idx " [label=\"" root-label "\"]")
    (when (cons? tree)
      (dolist (node (cdr tree))
        (println "    " root-idx " -> " (dot-node-name (inc! idx)))
        (set idx (print-ast-dot-rec node idx)))))
  idx)

(defun print-ast-dot (ast)
  (println "digraph {")
  (print-ast-dot-rec ast 0)
  (println "}"))

(defmacro show-ast (&body body)
  ;; (print-ast-dot `(progn ,@(macroexpand body)))
  (print-ast-dot (macroexpand `(progn ,@body)))
  `(progn ,@body))

(defmacro cond (&rest cnds)
  `(loop
    ,@(map (lambda (cnd)
             `(if ,(car cnd)
                  (break ,@(cdr cnd))))
           cnds)
    (break nil)))

(defmacro case (this &rest is)
  `(loop
    ,@(map (lambda (x)
             (if (= (car x) '_)
                 `(break ,@(cdr x))
                 `(if (= ,this ,(car x))
                      (break ,@(cdr x)))))
           is)
    (break nil)))