lambda_mountain 1.12.9

Lambda Mountain
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689

fail := λmsg . (print-s msg) (exit 1);

label-case := λk . (match k (
   ()
   (() ())
   ((l r) ('_ (label-case l) (label-case r) '_))
   (a (label-case-atom a))
));

debug-memory-usage-cons := ();
debug-memory-usage-atom := ();
debug-memory-usage := λ at . (if config-perf (tail(
   (if debug-memory-usage-cons () (
      (set debug-memory-usage-cons (i2s(show-cons-tail())))
   ))
   (if debug-memory-usage-atom () (
      (set debug-memory-usage-atom (i2s(show-atom-tail())))
   ))
   (print-s 'CountMemory:)
   (print-s at)
   (print-s '\n)
   (print-s 'ConsCount:)
   (print-s (dump-i( (show-cons-tail()) )))
   (print-s '\n)
   (print-s 'AtomCount:)
   (print-s (dump-i( (show-atom-tail()) )))
   (print-s '\n)
   ()
)) ());

deep-eq := λl r . match (l r) (
   ()
   ( (() ()) True )
   ( ((ll lr) (rl rr)) (
      if (deep-eq( ll rl )) (
         (deep-eq( lr rr ))
      ) ()
   ))
   ( (la ra) (eq( la ra )) )
);

is-builtin := λf . (match f (
   ()
   ('show-cons-tail True)
   ('show-atom-tail True)
   ('is True)
   ('is-atom True)
   ('is-cons True)
   ('eq True)
   ('not True)
   ('head True)
   ('head-string True)
   ('tail-string True)
   ('tail True)
   ('inc True)
   ('dec True)
   ('add True)
   ('mul True)
   ('div True)
   ('mod True)
   ('inv True)
   ('is-neg True)
   ('digit True)
   ('dump-i True)
   ('print-s True)
   ('print-i True)
   ('print-p True)
   ('print-p True)
   ('print-d True)
   ('clone-rope True)
   ('write-file True)
   ('load-file True)
   (_ ())
));

uuid-counter := ();
uuid := λ . (tail(
   (set uuid-counter (inc uuid-counter))
   (clone-rope ('uuid_ (dump-i uuid-counter)))
));

assert-not-typeof := λ loc term tt . (
   (if config-debug (tail(
      (set tt (parse-type tt))
      (if (is-typeof( term tt )) (tail(
         (print-s FailedAssertNotTypeof)(print-s \s)(print-s 'in)(print-s \s)(print-s loc)(print-s \n)
         (print-s \t)(print-s tt)(print-s \n)
         (print-s \t)(print-s term)(print-s \n)
         (exit 1)
      )) ())
   )) ())
);

s2i := λs . (tail(
   (local i)
   (local is_neg)
   (local one)
   (local two)
   (local three)
   (local four)
   (local five)
   (local six)
   (local seven)
   (local eight)
   (local nine)
   (local ten)
   (set one( inc() ))
   (set two( inc(inc( )) ))
   (set three( inc(inc(inc( ))) ))
   (set four( inc(inc(inc(inc( )))) ))
   (set five( inc(inc(inc(inc(inc( ))))) ))
   (set six( inc( inc(inc(inc(inc(inc( ))))) ) ))
   (set seven( inc(inc( inc(inc(inc(inc(inc( ))))) )) ))
   (set eight( inc(inc(inc( inc(inc(inc(inc(inc( ))))) ))) ))
   (set nine( inc(inc(inc(inc( inc(inc(inc(inc(inc( ))))) )))) ))
   (set ten( inc(inc(inc(inc(inc( inc(inc(inc(inc(inc( ))))) ))))) ))
   (if (eq( (head-string s) '- )) (tail(
      (set is_neg True)
      (set s (tail-string s))
   )) ())
   (while s (tail(
      (set i (mul( i ten )))
      (match (head-string s) (
         ()
         ('0 ())
         ('1 (set i (add( i one ))))
         ('2 (set i (add( i two ))))
         ('3 (set i (add( i three ))))
         ('4 (set i (add( i four ))))
         ('5 (set i (add( i five ))))
         ('6 (set i (add( i six ))))
         ('7 (set i (add( i seven ))))
         ('8 (set i (add( i eight ))))
         ('9 (set i (add( i nine ))))
         (u (fail (InvalidDigit u)))
      ))
      (set s (tail-string s))
   )))
   (if is_neg (
      (mul( (dec()) i ))
   ) i )
));

max := λ l r . (tail(
   (local li)
   (set li (s2i l))
   (local ri)
   (set ri (s2i r))
   (local return)
   (while (not return) (tail(
      (if (not li) (
        (set return r)
      ) (
         (if (not ri) (
            (set return l)
         ) ())
      ))
      (set li (dec li))
      (set ri (dec ri))
   )))
   return
));

assert-eq := λ loc l r . (
   (if config-debug (
      (if (deep-eq( l r )) () (tail(
         (print-s FailedAssertEqual)(print-s \s)(print-s 'in)(print-s \s)(print-s loc)(print-s \n)
         (print-s \t)(print-s l)(print-s \n)
         (print-s \t)(print-s r)(print-s \n)
         (exit 1)
      )))
   ) ())
);


is-variable := λv . (match (head-string v) (
   ()
   ('. ( if (eq( v '. )) () True ))
   ('@ True )
   ('[ True )
   ('? True )
   ('! True )
   ('= True )
   ('> True )
   ('< True )
   ('/ True )
   ('% True )
   ('* True )
   ('+ True )
   ('- True )
   ('$ True )
   ('& True )
   ('| True )
   ('_ True )
   ('a True )
   ('b True )
   ('c True )
   ('d True )
   ('e True )
   ('f True )
   ('g True )
   ('h True )
   ('i True )
   ('j True )
   ('k True )
   ('l True )
   ('m True )
   ('n True )
   ('o True )
   ('p True )
   ('q True )
   ('r True )
   ('s True )
   ('t True )
   ('u True )
   ('v True )
   ('w True )
   ('x True )
   ('y True )
   ('z True )
));

merge-list := λl r . (tail(if l (
   (while r (tail(
      (set l (l (tail r)))
      (set r (head r))
   )))
   l
) r ));

merge-safe-list := λl r . (if l (
   (while r (tail(
      (set l (l (tail r)))
      (set r (head r))
   )))
   l
) r );

is-suffix := λs suff . (tail(
   (local match)
   (while s (tail(
      (if (eq( s suff )) (set match True) ())
      (set s (tail-string s))
   )))
   match
));

remove-suffix := λs suff . (tail(
   (local prefix)
   (while s (tail(
      (if (eq( s suff )) (
         (set s ())
      ) (
         (set prefix (prefix (clone-rope (head-string s))))
         (set s (tail-string s))
      ))
   )))
   (clone-rope prefix)
));

reverse-list := λl . (tail(
   (local r)
   (while l (tail(
      (set r ( (tail l) r ))
      (set l (head l))
   )))
   r
));

is-typeof := λ term tt . (match tt (
   ()
   ( Nil (if (eq( term Nil )) True (not term)) )
   ( Atom (is-atom term) )
   ( Cons (is-cons term) )
   ( (List te) (
      (if (is-cons term) (
         (if (is-typeof( (tail term) te )) (
            (is-typeof( (head term) tt ))
         ) ())
      ) (
         (not term)
      ))
   ))
   ( (Or( ('[]( lt rt )) )) (
      (if (is-typeof( term lt )) True (
          (is-typeof( term rt ))
      ))
   ))
   ( ('[] ( lt rt )) (
     (if (is-cons term) (
        (if (is-typeof( (head term) lt )) (
           (is-typeof( (tail term) rt ))
        ) ())
     ) ())
   ))
   ( (l r) (fail (UnknownTypeofType tt)))
   ( () (fail (UnknownTypeofType tt)))
   ( label (eq( (head term) label )) )
));

assert-typeof := λ loc term tt . (
   (if config-debug (tail(
      (set tt (parse-type tt))
      (if (is-typeof( term tt )) () (tail(
         (print-s FailedAssertTypeof)(print-s \s)(print-s 'in)(print-s \s)(print-s loc)(print-s \n)
         (print-s \t)(print-s tt)(print-s \n)
         (print-s \t)(print-s term)(print-s \n)
         (exit 1)
      )))
   )) ())
);

i2s := λi . (tail(
   (local ten)
   (set ten( inc(inc(inc(inc(inc( inc(inc(inc(inc(inc( ))))) ))))) ))
   (local sign)
   (local s)
   (local r)
   (if (is-neg i) (tail(
      (set sign '-)
      (set i (inv i))
   )) ())
   (while i (tail(
      (set r (mod(i ten)))
      (set s ((clone-rope(digit r) s)))
      (set i (div(i ten)))
   )))
   (if s () (set s 0))
   (clone-rope (sign s))
));

label-case-atom := λk . (tail(
   (local nk)
   (while k (match (head-string k) (
      ()
      (() ())
      ('- (
         (set nk (nk '_))
         (set k (tail-string k))
      ))
      ('+ (
         (set nk (nk '_pl_))
         (set k (tail-string k))
      ))
      ('= (
         (set nk (nk '_eq_))
         (set k (tail-string k))
      ))
      ('? (
         (set nk (nk '_Q_))
         (set k (tail-string k))
      ))
      (': (
         (set nk (nk '_C_))
         (set k (tail-string k))
      ))
      ('< (
         (set nk (nk '_LB_))
         (set k (tail-string k))
      ))
      ('> (
         (set nk (nk '_RB_))
         (set k (tail-string k))
      ))
      (\[ (
         (set nk (nk '_LP_))
         (set k (tail-string k))
      ))
      (\] (
         (set nk (nk '_RP_))
         (set k (tail-string k))
      ))
      ('% (
         (set nk (nk '_MD_))
         (set k (tail-string k))
      ))
      ('/ (
         (set nk (nk '_DV_))
         (set k (tail-string k))
      ))
      ('! (
         (set nk (nk '_EX_))
         (set k (tail-string k))
      ))
      ('$ (
         (set nk (nk '_DS_))
         (set k (tail-string k))
      ))
      (c (
         (set nk (nk (clone-rope c)))
         (set k (tail-string k))
      ))
   )))
   (clone-rope nk)
));

mangle-identifier := λname tt . (
   (clone-rope (label-case ( name : tt )))
);

parse-type-cache := ();
parse-type := λ tt . (tail(
   (local original-type)
   (set original-type tt)
   (local cache)
   (set cache parse-type-cache)
   (local found)
   (while cache (tail(
      (if (eq( (head(tail( cache ))) tt )) (
         (set found (tail(tail cache)))
      ) ())
      (set cache (head cache))
   )))
   (if found found (tail(
      (local buff)
      (local base-type)
      (local depth)
      (while tt (tail(
         (match (head-string tt) (
            ()
            ('< (set depth (inc depth)))
            ('[ (set depth (inc depth)))
            ('> (set depth (dec depth)))
            ('] (set depth (dec depth)))
         ))
         (if depth (
            (set buff (buff (clone-rope (head-string tt))))
         ) (
            (if (eq( (head-string tt) '+ )) (tail(
               (if base-type (
                  (set base-type (And( base-type (parse-type-single (clone-rope buff)) )))
               ) (
                  (set base-type (parse-type-single (clone-rope buff)))
               ))
               (set buff ())
            )) (
               (set buff (buff (clone-rope (head-string tt))))
            ))
         ))
         (set tt (tail-string tt))
      )))
      (set base-type (if base-type (
         (And( base-type (parse-type-single (clone-rope buff)) ))
      ) (
         (parse-type-single (clone-rope buff))
      )))
      (set parse-type-cache (parse-type-cache (original-type base-type)))
      base-type
   )))
));

parse-type-suffix := λ base suffix . (tail(
   (assert-typeof( 'parse-array-type::suffix suffix Atom ))
   (local depth)
   (local buff)
   (local mode)
   (while suffix (match (head-string suffix) (
      ()
      ( '[ (tail(
         (set suffix (tail-string suffix))
         (if depth (
            (set buff (buff '[ ))
         ) (
            (set mode '[)
         ))
         (set depth (inc depth))
      )))
      ( '< (tail(
         (set suffix (tail-string suffix))
         (if depth (
            (set buff (buff '< ))
         ) (
            (set mode '<)
         ))
         (set depth (inc depth))
      )))
      ( '] (tail(
         (set depth (dec depth))
         (set suffix (tail-string suffix))
         (if depth (
            (set buff (buff '] ))
         ) (
            (assert-eq( 'parse-suffix::mode '[ mode ))
            (if buff (
               (set base (Array( base
                  (parse-type( clone-rope buff ))
               )))
            ) (
               (set base (Array( base '? )))
            ))
            (set buff ())
            (set mode ())
         ))
      )))
      ( '> (tail(
         (set depth (dec depth))
         (set suffix (tail-string suffix))
         (if depth (
            (set buff (buff '> ))
         ) (
            (assert-eq( 'parse-suffix::mode '< mode ))
            (if buff (
               (set base (
                  base (parse-type-comma-sep( clone-rope buff ))
               ))
            ) (
               (set base (base '? ))
            ))
            (set buff ())
            (set mode ())
         ))
      )))
      ( c (tail(
         (set buff (buff (clone-rope c)))
         (set suffix (tail-string suffix))
      )))
   )))
   (assert-eq( 'parse-type-suffix::buff buff () ))
   base
));

parse-type-comma-sep := λ tt . (tail(
   (local buff)
   (local base)
   (local depth)
   (while tt (tail(
      (match (head-string tt) (
         ()
         ('< (set depth (inc depth)))
         ('[ (set depth (inc depth)))
         ('> (set depth (dec depth)))
         ('] (set depth (dec depth)))
      ))
      (match (head-string tt) (
         ()
         (', (tail(
            (if depth (
               (set buff (buff ',))
            ) (
               (if base (
                  (set base (base (parse-type (clone-rope buff))))
               ) (
                  (set base (parse-type (clone-rope buff)))
               ))
               (set buff ())
            ))
            (set tt (tail-string tt))
         )))
         (c (tail(
            (set buff (buff (clone-rope c)))
            (set tt (tail-string tt))
         )))
      ))
   )))
   (if base (
      (set base (base (parse-type (clone-rope buff))))
   ) (
      (set base (parse-type (clone-rope buff)))
   ))
   base
));

parse-type-tuple := λ tt . (tail(
   (assert-eq( 'parse-type-tuple::tt::head '[ (head-string tt) ))
   (local depth)
   (local base)
   (local buff)
   (while tt (match (head-string tt) (
      ()
      ( '[ (tail(
         (if depth (
            (set buff (buff '[))
         ) ())
         (set depth (inc depth))
         (set tt (tail-string tt))
      )))
      ( '] (tail(
         (set depth (dec depth))
         (if depth (
            (set buff (buff ']))
         ) (
            (if base (
               (set base ('[]( base (parse-type (clone-rope buff)) )))
            ) (
               (set base (parse-type (clone-rope buff)))
            ))            
         ))
         (set tt (tail-string tt))
      )))
      ( ', (tail(
         (if (dec depth) (
            (set buff (buff ',))
         ) (
            (if base (
               (set base ('[]( base (parse-type (clone-rope buff)) )))
            ) (
               (set base (parse-type (clone-rope buff)))
            ))
            (set buff ())
         ))
         (set tt (tail-string tt))
      )))
      ( c (tail(
         (set buff (buff (clone-rope c)))
         (set tt (tail-string tt))
      )))
   )))
   base
));

parse-type-single := λ tt . (tail(
   (local buff)
   (local base)
   (while tt (tail(
      (match (head-string tt) (
         ()
         ('[ (tail(
            (if buff (
               (set base (parse-type-suffix( (clone-rope buff) tt )))
            ) (
               (set base (parse-type-tuple tt))
            ))
            (set tt ())
         )))
         ('< (tail(
            (set base (parse-type-suffix( (clone-rope buff) tt )))
            (set tt ())
         )))
         (c (set buff (buff (clone-rope c)) ))
      ))
      (set tt (tail-string tt))
   )))
   (if base base (clone-rope buff))
));

escape-literal-char := λc . match c (
   ()
   ( '\\ '\\\\ )
   ( '\\: '\: )
   ( '\\o '\o )
   ( '\\n (\\ 'n))
   ( '\\t '\t )
   ( '\\s '\s )
   ( '\\l '\l )
   ( '\\[ '\[ )
   ( '\\] '\] )
   ( c (clone-rope c) )
);
escape-escape-char := λc . match c (
   ()
   ( '[ '\[ )
   ( '] '\] )
   ( '` '' )
   ( '\\ '\\\\ )
   ( ': '\: )
   ( 'o '\o )
   ( 'n (\\ 'n))
   ( 't '\t )
   ( 's '\s )
   ( 'l '\l )
   ( _ () )
);
escape-literal := λs . (tail(
   (local buff)
   (while s (tail(
      (if (eq( (head-string s) '\ )) (tail(
         (local e)
         (set e (escape-escape-char( (head-string(tail-string( s ))) )))
         (if e (
            (set buff (buff e))
            (set s (tail-string(tail-string s)))
         ) (
            (set buff (buff (escape-literal-char (head-string s))))
            (set s (tail-string s))
         ))
      )) (tail(
         (set buff (buff (escape-literal-char (head-string s))))
         (set s (tail-string s))
      )))
   )))
   (clone-rope buff)
));

quote-s := λt . (match t (
   ()
   ( (l r) ('\[ (quote-s l) '\s (quote-s r) '\]) )
   ( x x )
));