-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathshen-elisp.html
3467 lines (3322 loc) · 400 KB
/
shen-elisp.html
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Shen Elisp</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Aditya Siram" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2018 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">Shen Elisp</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orga15dbc0">1. Package Details</a></li>
<li><a href="#orgb6cc94a">2. KLambda Primitives</a>
<ul>
<li><a href="#org2647a1e">2.1. License</a></li>
<li><a href="#org9a26a9e">2.2. Implementation Constants</a></li>
<li><a href="#orgecca41a">2.3. Symbols</a>
<ul>
<li><a href="#org9923cd7">2.3.1. Prefixing Utilities</a></li>
</ul>
</li>
<li><a href="#orgaf76460">2.4. Assignments</a></li>
<li><a href="#org24082a3">2.5. KLambda Constants</a></li>
<li><a href="#org12efe15">2.6. Boolean Operations</a></li>
<li><a href="#org5b1f385">2.7. Lambdas</a></li>
<li><a href="#org04d77d6">2.8. Lets</a></li>
<li><a href="#orgcf0ebef">2.9. Defuns</a></li>
<li><a href="#orgaef7e80">2.10. Equality</a></li>
<li><a href="#org401216d">2.11. Other Generic Functions</a></li>
<li><a href="#orgaf0d9b3">2.12. Lists</a></li>
<li><a href="#orgb0fc293">2.13. Strings</a></li>
<li><a href="#orge86d3de">2.14. Error Handling</a></li>
<li><a href="#org4b2085f">2.15. Vectors</a></li>
<li><a href="#org1c0998c">2.16. Arithmetic Operations</a></li>
<li><a href="#orgbbf6edc">2.17. Time</a></li>
<li><a href="#org7b70b68">2.18. Streams and I/O</a></li>
</ul>
</li>
<li><a href="#org5a0d209">3. Utilities</a>
<ul>
<li><a href="#org2f7f2d7">3.1. Lookup</a></li>
<li><a href="#org6b297cf">3.2. AST Utilities</a>
<ul>
<li><a href="#org647918f">3.2.1. Paths</a></li>
<li><a href="#orge295df1">3.2.2. AST Getter/Setter</a></li>
<li><a href="#org255696d">3.2.3. AST Search</a></li>
<li><a href="#org950b938">3.2.4. Path Utilities</a></li>
<li><a href="#org5338cf2">3.2.5. AST Modification</a></li>
</ul>
</li>
<li><a href="#org7ec21ec">3.3. List</a>
<ul>
<li><a href="#org6697563">3.3.1. Detect Dotted Pair</a></li>
<li><a href="#orge8c40dd">3.3.2. List Filtering</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orged64af4">4. Rewriting The AST</a>
<ul>
<li><a href="#org5d44a1d">4.1. Walking The AST</a></li>
<li><a href="#org0910bd6">4.2. Function Application</a></li>
<li><a href="#orgb724e38">4.3. Finding Tail Calls</a></li>
<li><a href="#orgfe13668">4.4. Generating A TCO'ed Function</a></li>
<li><a href="#org5598048">4.5. Modifying The AST</a></li>
<li><a href="#org8cac10a">4.6. (Unused) Isolating and Filling</a></li>
</ul>
</li>
<li><a href="#org565093a">5. Optimizations</a>
<ul>
<li><a href="#orgb8375a5">5.1. Consolidate Call Chains</a></li>
<li><a href="#orgd55431b">5.2. Consolidate Cons</a></li>
<li><a href="#orgf836e0d">5.3. Consolidate @s</a></li>
<li><a href="#org1e2f75f">5.4. Consolidate tl</a></li>
<li><a href="#org7e1f07a">5.5. Add 1+'s</a></li>
<li><a href="#orgb7e6e4c">5.6. Nil Comparisons To Null</a></li>
</ul>
</li>
<li><a href="#org29a73b5">6. Overrides</a>
<ul>
<li><a href="#org92e720b">6.1. Performance</a></li>
<li><a href="#org7316f76">6.2. Namespacing</a></li>
<li><a href="#orge37464f">6.3. Bug Fixes</a></li>
</ul>
</li>
<li><a href="#org7d396b4">7. Evaluate KLambda</a>
<ul>
<li><a href="#orgf8946ec">7.1. Generate From Seed KLambda Files</a>
<ul>
<li><a href="#org1b5a266">7.1.1. Evaluating Bootstrapped KLambda</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org012e78f">8. Providing The Primitives</a></li>
<li><a href="#orgb527822">9. Overlays</a>
<ul>
<li><a href="#orgcdc6713">9.1. License</a></li>
<li><a href="#org7067b3c">9.2. Repl</a>
<ul>
<li><a href="#org40f6c85">9.2.1. Questions</a></li>
<li><a href="#org98016cf">9.2.2. Changing Directories</a></li>
</ul>
</li>
<li><a href="#org1eb22af">9.3. Provide it</a></li>
</ul>
</li>
<li><a href="#orgdff2142">10. Shen REPL</a>
<ul>
<li><a href="#orgdac64f7">10.1. Credits</a></li>
<li><a href="#orgf6c7b17">10.2. Prompt</a></li>
<li><a href="#org0055b46">10.3. Input Events</a></li>
<li><a href="#org0fd6b16">10.4. Sending Input</a></li>
<li><a href="#org472d9db">10.5. Evaluating User Input</a></li>
<li><a href="#org7a17845">10.6. The REPL Mode</a></li>
<li><a href="#orgd505117">10.7. Starting the REPL</a></li>
<li><a href="#org2f8ebe6">10.8. Provide it</a></li>
</ul>
</li>
<li><a href="#orgb9644fb">11. Bootstrap</a>
<ul>
<li><a href="#org6761f05">11.1. Collecting KLambda files</a></li>
<li><a href="#org65e7f26">11.2. Modifying The Elisp Reader For KLambda</a></li>
<li><a href="#org1b5e46f">11.3. Iterating over KLambda Files</a></li>
</ul>
</li>
<li><a href="#orgcabdcf3">12. The Runner</a></li>
</ul>
</div>
</div>
<div id="outline-container-orga15dbc0" class="outline-2">
<h2 id="orga15dbc0"><span class="section-number-2">1</span> Package Details</h2>
<div class="outline-text-2" id="text-1">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span>define-package
<span style="color: #008000;">"shen-elisp"</span>
<span style="color: #008000;">"19.2-0.0.1"</span>
<span style="color: #008000;">"Shen implementation in Elisp"</span>
'<span style="color: #7388D6;">(</span><span style="color: #909183;">(</span>emacs <span style="color: #008000;">"24.4"</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span>
<span style="color: #006FE0;">:url</span> <span style="color: #008000;">"http://github.com/deech/shen-elisp"</span>
<span style="color: #006FE0;">:keywords</span> '<span style="color: #7388D6;">(</span><span style="color: #008000;">"shen"</span> <span style="color: #008000;">"elisp"</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgb6cc94a" class="outline-2">
<h2 id="orgb6cc94a"><span class="section-number-2">2</span> KLambda Primitives</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-org2647a1e" class="outline-3">
<h3 id="org2647a1e"><span class="section-number-3">2.1</span> License</h3>
<div class="outline-text-3" id="text-2-1">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">-*- lexical-binding: t -*-</span>
<span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">Copyright (c) 2015-2018 Aditya Siram. All Rights Reserved.</span>
<span style="color: #8D8D84;">;; </span><span style="color: #8D8D84; font-style: italic;">BSD 3-Clause License: http://opensource.org/licenses/BSD-3-Clause</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org9a26a9e" class="outline-3">
<h3 id="org9a26a9e"><span class="section-number-3">2.2</span> Implementation Constants</h3>
<div class="outline-text-3" id="text-2-2">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defconst</span> <span style="color: #BA36A5;">shen/prefix</span> <span style="color: #008000;">"shen/"</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgecca41a" class="outline-3">
<h3 id="orgecca41a"><span class="section-number-3">2.3</span> Symbols</h3>
<div class="outline-text-3" id="text-2-3">
<p>
Symbols in KLambda (and Shen) are very much like symbols in Elisp :
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/symbol-p</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>not <span style="color: #909183;">(</span><span style="color: #0000FF;">or</span> <span style="color: #709870;">(</span>consp X<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>bufferp X<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>vectorp X<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>numberp X<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>stringp X<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
except for a few important differences:
</p>
<ol class="org-ol">
<li><p>
In KLambda (and Shen) symbols don't need to be quoted:
</p>
<pre class="example">
> some-symbol
some-symbol
</pre>
<p>
whereras in Elisp they do:
</p>
<pre class="example">
> some-symbol
*** Eval error *** Symbol's value as variable is void: some-symbol
> 'some-symbol
some-symbol
</pre></li>
<li><p>
When calling a symbol function in Shen you can
simply apply the arguments:
</p>
<pre class="example">
(defun call-f (f a b c) (f a b c))
</pre>
<p>
But in Elisp we need to use <code>funcall</code>:
</p>
<pre class="example">
(defun call-f (f a b c) (funcall f a b c))
</pre></li>
</ol>
<p>
To deal with these differences a <a href="#org5d44a1d">pass</a> is taken over the KLambda ast (before
macroexpansion) that identifies symbols that requires quoting and function calls
that need to be applied correctly.
</p>
<p>
Interning a Shen symbol delegates to Elisp's <code>intern</code>:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/intern</span> <span style="color: #7388D6;">(</span>String<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>intern String<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
as does <code>shen/symbol->string</code>:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/symbol->string</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>symbol-name X<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
<div id="outline-container-org9923cd7" class="outline-4">
<h4 id="org9923cd7"><span class="section-number-4">2.3.1</span> Prefixing Utilities</h4>
<div class="outline-text-4" id="text-2-3-1">
<p>
Elisp does not have namespaces so to insulate the rest of the user's Emacs image
the primitive functions, user-defined functions and variables are prefixed with <code>shen/</code>.
Raw symbols are not touched since they can't harm existing Elisp code.
</p>
<p>
When decompiling Klambda to Elisp we need functions that prefix the appropriate
symbol with <code>shen/</code>:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/internal/prefix-symbol</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">if</span> <span style="color: #909183;">(</span>shen/symbol-p X<span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>intern <span style="color: #709870;">(</span>concat shen/prefix <span style="color: #907373;">(</span>symbol-name X<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
X<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
, detect if it has already has a prefixed:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/internal/symbol-prefixed-p</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">and</span> <span style="color: #909183;">(</span>shen/symbol-p X<span style="color: #909183;">)</span> <span style="color: #909183;">(</span>string-prefix-p shen/prefix <span style="color: #709870;">(</span>symbol-name X<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
, and removes the prefix it for consumption by Shen:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/internal/unprefix-symbol</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">if</span> <span style="color: #909183;">(</span>shen/internal/symbol-prefixed-p X<span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>intern <span style="color: #709870;">(</span>substring <span style="color: #907373;">(</span>symbol-name X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>length shen/prefix<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
X<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgaf76460" class="outline-3">
<h3 id="orgaf76460"><span class="section-number-3">2.4</span> Assignments</h3>
<div class="outline-text-3" id="text-2-4">
<p>
When setting and getting a variable, the name is prefixed to prevent the user from stomping over one that
already exists in the Emacs image. See <a href="#org9923cd7">2.3.1</a> for more on how this works.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/set</span> <span style="color: #7388D6;">(</span>X Y<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>set <span style="color: #909183;">(</span>intern <span style="color: #709870;">(</span>concat shen/prefix <span style="color: #907373;">(</span>symbol-name X<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #0000FF;">let</span> <span style="color: #709870;">(</span><span style="color: #907373;">(</span>prefixed<span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span><span style="color: #0000FF;">or</span> <span style="color: #907373;">(</span><span style="color: #0000FF;">and</span> <span style="color: #6276BA;">(</span>symbolp Y<span style="color: #6276BA;">)</span>
<span style="color: #6276BA;">(</span>not <span style="color: #858580;">(</span>shen/internal/symbol-prefixed-p Y<span style="color: #858580;">)</span><span style="color: #6276BA;">)</span>
<span style="color: #6276BA;">(</span><span style="color: #0000FF;">progn</span>
<span style="color: #858580;">(</span><span style="color: #0000FF;">setq</span> prefixed <span style="color: #80A880;">(</span>shen/internal/prefix-symbol Y<span style="color: #80A880;">)</span><span style="color: #858580;">)</span>
<span style="color: #858580;">(</span><span style="color: #0000FF;">or</span> <span style="color: #80A880;">(</span>boundp prefixed<span style="color: #80A880;">)</span>
<span style="color: #80A880;">(</span>fboundp prefixed<span style="color: #80A880;">)</span><span style="color: #858580;">)</span><span style="color: #6276BA;">)</span>
prefixed<span style="color: #907373;">)</span>
Y<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/value</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">condition-case</span> ex
<span style="color: #909183;">(</span>symbol-value <span style="color: #709870;">(</span>intern <span style="color: #907373;">(</span>concat shen/prefix <span style="color: #6276BA;">(</span>symbol-name X<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>'error <span style="color: #709870;">(</span><span style="color: #ff0000; font-weight: bold;">error</span> <span style="color: #907373;">(</span>format <span style="color: #008000;">"%s has not been assigned"</span> X<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org24082a3" class="outline-3">
<h3 id="org24082a3"><span class="section-number-3">2.5</span> KLambda Constants</h3>
<div class="outline-text-3" id="text-2-5">
<p>
Once <a href="#orgaf76460">the assigning mechanism</a> is in place the spec requires some global variables:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span>shen/set '*home-directory* <span style="color: #008000;">""</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*stoutput* standard-output<span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*stinput* <span style="color: #7388D6;">[</span><span style="color: #909183;">()</span><span style="color: #7388D6;">]</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*language* <span style="color: #008000;">"Elisp"</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*implementation* system-configuration<span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*porters* <span style="color: #008000;">"Aditya Siram"</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*release* emacs-version<span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*port* <span style="color: #D0372D;">1.7</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>shen/set '*os* <span style="color: #7388D6;">(</span>symbol-name system-type<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org12efe15" class="outline-3">
<h3 id="org12efe15"><span class="section-number-3">2.6</span> Boolean Operations</h3>
<div class="outline-text-3" id="text-2-6">
<p>
Unlike Elisp in KLambda booleans are <code>false</code> and <code>true</code>, distinct symbols which
are not synonymous with <code>nil</code> and <code>(not nil)</code> so wrappers are required:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/internal/shen->predicate</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>eq X 'true<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/internal/predicate->shen</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">if</span> X <span style="color: #909183;">(</span><span style="color: #0000FF;">quote</span> true<span style="color: #909183;">)</span> <span style="color: #909183;">(</span><span style="color: #0000FF;">quote</span> false<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Since <code>if</code> , <code>and</code> and <code>or</code> are special forms they are defined as macros to
preserve evaluation order.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/if</span> <span style="color: #7388D6;">(</span>X Y Z<span style="color: #7388D6;">)</span>
`<span style="color: #7388D6;">(</span><span style="color: #0000FF;">if</span> <span style="color: #909183;">(</span>eq ,X 'true<span style="color: #909183;">)</span> ,Y ,Z<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/and</span> <span style="color: #7388D6;">(</span>X Y<span style="color: #7388D6;">)</span> `<span style="color: #7388D6;">(</span>shen/internal/predicate->shen <span style="color: #909183;">(</span><span style="color: #0000FF;">and</span> <span style="color: #709870;">(</span>eq ,X 'true<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>eq ,Y 'true<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/or</span> <span style="color: #7388D6;">(</span>X Y<span style="color: #7388D6;">)</span> `<span style="color: #7388D6;">(</span>shen/internal/predicate->shen <span style="color: #909183;">(</span><span style="color: #0000FF;">or</span> <span style="color: #709870;">(</span>eq ,X 'true<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>eq ,Y 'true<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
KLambda's <code>cond</code>, unlike Elisp's forbids a fallthrough case - one of the
predicates must be true.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/cond</span> <span style="color: #7388D6;">(</span><span style="color: #6434A3;">&rest</span> CASES<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">let*</span> <span style="color: #909183;">(</span><span style="color: #709870;">(</span>predicates-quoted-cases
<span style="color: #907373;">(</span>mapcar <span style="color: #6276BA;">(</span><span style="color: #0000FF;">lambda</span> <span style="color: #858580;">(</span>predicate-result-pair<span style="color: #858580;">)</span>
<span style="color: #858580;">(</span>list <span style="color: #80A880;">(</span><span style="color: #0000FF;">if</span> <span style="color: #887070;">(</span>shen/symbol-p <span style="color: #707183;">(</span>nth <span style="color: #D0372D;">0</span> predicate-result-pair<span style="color: #707183;">)</span><span style="color: #887070;">)</span>
<span style="color: #887070;">(</span>list 'quote <span style="color: #707183;">(</span>nth <span style="color: #D0372D;">0</span> predicate-result-pair<span style="color: #707183;">)</span><span style="color: #887070;">)</span>
<span style="color: #887070;">(</span>list 'shen/internal/shen->predicate <span style="color: #707183;">(</span>nth <span style="color: #D0372D;">0</span> predicate-result-pair<span style="color: #707183;">)</span><span style="color: #887070;">)</span><span style="color: #80A880;">)</span>
<span style="color: #80A880;">(</span>nth <span style="color: #D0372D;">1</span> predicate-result-pair<span style="color: #80A880;">)</span><span style="color: #858580;">)</span><span style="color: #6276BA;">)</span>
CASES<span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span>fallthrough-added <span style="color: #907373;">(</span>append predicates-quoted-cases <span style="color: #6276BA;">(</span>list '<span style="color: #858580;">(</span>t <span style="color: #80A880;">(</span><span style="color: #ff0000; font-weight: bold;">error</span> <span style="color: #008000;">"One of the cond predicates must be true."</span><span style="color: #80A880;">)</span><span style="color: #858580;">)</span><span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
`<span style="color: #909183;">(</span><span style="color: #0000FF;">cond</span> ,@fallthrough-added<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5b1f385" class="outline-3">
<h3 id="org5b1f385"><span class="section-number-3">2.7</span> Lambdas</h3>
<div class="outline-text-3" id="text-2-7">
<p>
Since Shen supports currying by default KLambda's <code>lambda</code> form is stripped down to only accept one
argument.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">lambda</span> X <span style="color: #7388D6;">(</span>...<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Multiple arguments are supported via nesting:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">lambda</span> X <span style="color: #7388D6;">(</span><span style="color: #0000FF;">lambda</span> Y <span style="color: #909183;">(</span>...<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
The implementation delegates to Elisp's <code>lambda</code> form:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/lambda</span> <span style="color: #7388D6;">(</span>X Y<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">if</span> <span style="color: #909183;">(</span>eq X nil<span style="color: #909183;">)</span>
`<span style="color: #909183;">(</span><span style="color: #0000FF;">lambda</span> <span style="color: #709870;">()</span> ,Y<span style="color: #909183;">)</span>
`<span style="color: #909183;">(</span><span style="color: #0000FF;">lambda</span> <span style="color: #709870;">(</span>,X<span style="color: #709870;">)</span> ,Y<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org04d77d6" class="outline-3">
<h3 id="org04d77d6"><span class="section-number-3">2.8</span> Lets</h3>
<div class="outline-text-3" id="text-2-8">
<p>
KLambda's <code>let</code> in a similar way only takes one assignment:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">let</span> X Y ...<span style="color: #707183;">)</span>
</pre>
</div>
<p>
Similarly multiple assignments are supported via nested <code>lets</code>:
</p>
<pre class="example">
(let W X (let Y Z ...))
</pre>
<p>
The implementation delegates to Elisp's <code>let</code>:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/let</span> <span style="color: #7388D6;">(</span>X Y Z<span style="color: #7388D6;">)</span>
`<span style="color: #7388D6;">(</span><span style="color: #0000FF;">let</span> <span style="color: #909183;">(</span><span style="color: #709870;">(</span>,X ,Y<span style="color: #709870;">)</span><span style="color: #909183;">)</span> ,Z<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgcf0ebef" class="outline-3">
<h3 id="orgcf0ebef"><span class="section-number-3">2.9</span> Defuns</h3>
<div class="outline-text-3" id="text-2-9">
<p>
The structure of <code>defun</code> in KLambda is identical to Elisp's so a straight
translation works.
</p>
<p>
Functions are required to be tail-call optimized but that is done below
before the body is spliced in.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/defun</span> <span style="color: #7388D6;">(</span>F Args Body<span style="color: #7388D6;">)</span> `<span style="color: #7388D6;">(</span><span style="color: #0000FF;">defun</span> ,F ,Args ,Body<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgaef7e80" class="outline-3">
<h3 id="orgaef7e80"><span class="section-number-3">2.10</span> Equality</h3>
<div class="outline-text-3" id="text-2-10">
<p>
Using <a href="#org4b2085f">hash-tables for tuples and vectors</a> complicates equality. It also slows
down equality tests across the board but using hash-tables still results in
a substantial net-gain.
</p>
<p>
In the case of <a href="#coderef-strings-and-numbers" class="coderef" onmouseover="CodeHighlightOn(this, 'coderef-strings-and-numbers');" onmouseout="CodeHighlightOff(this, 'coderef-strings-and-numbers');">strings and numbers</a> using built-in functions suffices but for
compound structures which might contain hash-tables simply using <code>(equal ...)</code>
will not work because two hash-tables containing the same keys and values are not
<code>equal</code>.
</p>
<p>
While <code>(equal ...)</code> isn't sufficient, it does have the nice property of being
right when it returns true. Two structures which are <code>(equal ...)</code> are equal but
<code>(equal ...)</code> returning nil does not mean they are unequal. So we first try the
<a href="#coderef-obvious-equality-test" class="coderef" onmouseover="CodeHighlightOn(this, 'coderef-obvious-equality-test');" onmouseout="CodeHighlightOff(this, 'coderef-obvious-equality-test');">obvious equality test</a> and only perform the expensive tests when that fails.
</p>
<p>
If both arguments are cons lists and the equality check has failed it might be
that they contain hash tables so we need to check for that. First ensure they
are the same length. Note that we use <code>safe-length</code> and not <code>length</code>. Only the
former is robust to lists that end in dotted pairs, eg. <code>'(a b c . d)</code> which are
sometimes generated from KLambda code.
</p>
<p>
Then we loop through the elements of the list ensuring that the <a href="#coderef-elements are of the same type" class="coderef" onmouseover="CodeHighlightOn(this, 'coderef-elements are of the same type');" onmouseout="CodeHighlightOff(this, 'coderef-elements are of the same type');">elements are of the same type</a> ,
or <a href="#coderef-store the inner list" class="coderef" onmouseover="CodeHighlightOn(this, 'coderef-store the inner list');" onmouseout="CodeHighlightOff(this, 'coderef-store the inner list');">storing inner lists</a> as we encounter then or recursing to
<a href="#coderef-compare the elements" class="coderef" onmouseover="CodeHighlightOn(this, 'coderef-compare the elements');" onmouseout="CodeHighlightOff(this, 'coderef-compare the elements');">compare the elements</a>. Note that even though we recur there is no risk of
blowing stack because only list comparison recurses and by this point we have
ensured that the elements are not lists.
</p>
<p>
One might wonder why we're using <code>(string= ...)</code> to <a href="#coderef-compare hash tables" class="coderef" onmouseover="CodeHighlightOn(this, 'coderef-compare hash tables');" onmouseout="CodeHighlightOff(this, 'coderef-compare hash tables');">compare hash tables</a>. The
unsatisfying answer is that it was about 30% faster than whatever I could cook
up in Elisp.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/internal/=</span> <span style="color: #7388D6;">(</span>X Y<span style="color: #7388D6;">)</span>
<span id="coderef-strings-and-numbers" class="coderef-off"> <span style="color: #7388D6;">(</span><span style="color: #0000FF;">cond</span> <span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>stringp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>stringp Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>string-equal X Y<span style="color: #709870;">)</span><span style="color: #909183;">)</span> <span style="color: #8D8D84;">;;;</span> (strings-and-numbers)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>numberp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>numberp Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>= X Y<span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>symbolp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>symbolp Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>eq X Y<span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>t
<span id="coderef-obvious-equality-test" class="coderef-off"> <span style="color: #709870;">(</span><span style="color: #0000FF;">or</span> <span style="color: #907373;">(</span>equal X Y<span style="color: #907373;">)</span> <span style="color: #8D8D84;">;;;</span> (obvious-equality-test)</span>
<span style="color: #907373;">(</span><span style="color: #0000FF;">cond</span>
<span style="color: #6276BA;">(</span><span style="color: #858580;">(</span><span style="color: #0000FF;">and</span> <span style="color: #80A880;">(</span>consp X<span style="color: #80A880;">)</span> <span style="color: #80A880;">(</span>consp Y<span style="color: #80A880;">)</span><span style="color: #858580;">)</span>
<span style="color: #858580;">(</span><span style="color: #0000FF;">let</span> <span style="color: #80A880;">(</span><span style="color: #887070;">(</span>LengthX <span style="color: #707183;">(</span>safe-length X<span style="color: #707183;">)</span><span style="color: #887070;">)</span>
<span style="color: #887070;">(</span>LengthY <span style="color: #707183;">(</span>safe-length Y<span style="color: #707183;">)</span><span style="color: #887070;">)</span><span style="color: #80A880;">)</span>
<span style="color: #80A880;">(</span><span style="color: #0000FF;">and</span>
<span style="color: #887070;">(</span>= LengthX LengthY<span style="color: #887070;">)</span>
<span style="color: #887070;">(</span><span style="color: #0000FF;">let</span> <span style="color: #707183;">(</span><span style="color: #7388D6;">(</span>SoFar 't<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>InnerListsX <span style="color: #909183;">(</span>list X<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>InnerListsY <span style="color: #909183;">(</span>list Y<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>FirstTime 't<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>CurrentIndex <span style="color: #D0372D;">0</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">while</span> <span style="color: #7388D6;">(</span><span style="color: #0000FF;">and</span> SoFar InnerListsX InnerListsY<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">let*</span> <span style="color: #909183;">(</span><span style="color: #709870;">(</span>CurrentListX <span style="color: #907373;">(</span><span style="color: #0000FF;">pop</span> InnerListsX<span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span>CurrentListY <span style="color: #907373;">(</span><span style="color: #0000FF;">pop</span> InnerListsY<span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span>Iterate
<span style="color: #907373;">(</span><span style="color: #0000FF;">lambda</span> <span style="color: #6276BA;">()</span>
<span style="color: #6276BA;">(</span><span style="color: #0000FF;">let</span> <span style="color: #858580;">(</span><span style="color: #80A880;">(</span>I <span style="color: #D0372D;">0</span><span style="color: #80A880;">)</span><span style="color: #858580;">)</span>
<span style="color: #858580;">(</span><span style="color: #0000FF;">while</span> <span style="color: #80A880;">(</span><span style="color: #0000FF;">and</span> SoFar <span style="color: #887070;">(</span>< I LengthX<span style="color: #887070;">)</span><span style="color: #80A880;">)</span>
<span style="color: #80A880;">(</span><span style="color: #0000FF;">let*</span> <span style="color: #887070;">(</span><span style="color: #707183;">(</span>CurrentX <span style="color: #7388D6;">(</span>nth I CurrentListX<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span>CurrentY <span style="color: #7388D6;">(</span>nth I CurrentListY<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span><span style="color: #887070;">)</span>
<span style="color: #887070;">(</span><span style="color: #0000FF;">cond</span>
<span id="coderef-elements are of the same type" class="coderef-off"> <span style="color: #707183;">(</span><span style="color: #7388D6;">(</span>not <span style="color: #909183;">(</span>equal <span style="color: #709870;">(</span>type-of CurrentX<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>type-of CurrentY<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span> <span style="color: #8D8D84;">;;;</span> (elements are of the same type)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">setq</span> SoFar nil<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span id="coderef-store the inner list" class="coderef-off"> <span style="color: #707183;">(</span><span style="color: #7388D6;">(</span><span style="color: #0000FF;">and</span> <span style="color: #909183;">(</span>consp CurrentX<span style="color: #909183;">)</span> <span style="color: #909183;">(</span>consp CurrentY<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span> <span style="color: #8D8D84;">;;;</span> (store the inner list)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">progn</span>
<span style="color: #909183;">(</span><span style="color: #0000FF;">push</span> CurrentX InnerListsX<span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #0000FF;">push</span> CurrentY InnerListsY<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span id="coderef-compare the elements" class="coderef-off"> <span style="color: #707183;">(</span>t <span style="color: #7388D6;">(</span><span style="color: #0000FF;">setq</span> SoFar <span style="color: #909183;">(</span>shen/internal/= CurrentX CurrentY<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span><span style="color: #887070;">)</span> <span style="color: #8D8D84;">;;;</span> (compare the elements)</span>
<span style="color: #887070;">(</span><span style="color: #0000FF;">setq</span> I <span style="color: #707183;">(</span>1+ I<span style="color: #707183;">)</span><span style="color: #887070;">)</span><span style="color: #80A880;">)</span><span style="color: #858580;">)</span><span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #0000FF;">if</span> <span style="color: #709870;">(</span>not FirstTime<span style="color: #709870;">)</span>
<span style="color: #709870;">(</span><span style="color: #0000FF;">progn</span>
<span style="color: #907373;">(</span><span style="color: #0000FF;">setq</span> FirstTime nil<span style="color: #907373;">)</span>
<span style="color: #907373;">(</span><span style="color: #0000FF;">setq</span> LengthX <span style="color: #6276BA;">(</span>safe-length CurrentListX<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span>
<span style="color: #907373;">(</span><span style="color: #0000FF;">setq</span> LengthY <span style="color: #6276BA;">(</span>safe-length CurrentListY<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span>
<span style="color: #907373;">(</span><span style="color: #0000FF;">setq</span> SoFar <span style="color: #6276BA;">(</span>= LengthX LengthY<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span>
<span style="color: #907373;">(</span>funcall Iterate<span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span>funcall Iterate<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
SoFar<span style="color: #887070;">)</span><span style="color: #80A880;">)</span><span style="color: #858580;">)</span><span style="color: #6276BA;">)</span>
<span id="coderef-compare hash tables" class="coderef-off"> <span style="color: #6276BA;">(</span><span style="color: #858580;">(</span><span style="color: #0000FF;">and</span> <span style="color: #80A880;">(</span>hash-table-p X<span style="color: #80A880;">)</span> <span style="color: #80A880;">(</span>hash-table-p Y<span style="color: #80A880;">)</span><span style="color: #858580;">)</span> <span style="color: #8D8D84;">;;;</span> (compare hash tables)</span>
<span style="color: #858580;">(</span><span style="color: #0000FF;">and</span> <span style="color: #80A880;">(</span>= <span style="color: #887070;">(</span>hash-table-count X<span style="color: #887070;">)</span> <span style="color: #887070;">(</span>hash-table-count Y<span style="color: #887070;">)</span><span style="color: #80A880;">)</span>
<span id="coderef-hash table comparison" class="coderef-off"> <span style="color: #80A880;">(</span>string= <span style="color: #8D8D84;">;;;</span> (hash table comparison)</span>
<span style="color: #887070;">(</span>prin1-to-string X<span style="color: #887070;">)</span>
<span style="color: #887070;">(</span>prin1-to-string Y<span style="color: #887070;">)</span><span style="color: #80A880;">)</span><span style="color: #858580;">)</span><span style="color: #6276BA;">)</span>
<span style="color: #6276BA;">(</span>t nil<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/=</span> <span style="color: #7388D6;">(</span>X Y<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>shen/internal/predicate->shen <span style="color: #909183;">(</span>shen/internal/= X Y<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org401216d" class="outline-3">
<h3 id="org401216d"><span class="section-number-3">2.11</span> Other Generic Functions</h3>
<div class="outline-text-3" id="text-2-11">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/freeze</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
`<span style="color: #7388D6;">(</span><span style="color: #0000FF;">function</span> <span style="color: #909183;">(</span><span style="color: #0000FF;">lambda</span> nil ,X<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/type</span> <span style="color: #7388D6;">(</span>X MyType<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span><span style="color: #0000FF;">declare</span> <span style="color: #909183;">(</span>ignore MyType<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span> X<span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgaf0d9b3" class="outline-3">
<h3 id="orgaf0d9b3"><span class="section-number-3">2.12</span> Lists</h3>
<div class="outline-text-3" id="text-2-12">
<p>
List construction in KLambda is done with <code>cons</code> exclusively. The KLambda list
<code>[ a b c ]</code> for example is constructed:
</p>
<pre class="example">
(cons a (cons b (cons c ())))
</pre>
<p>
Elisp also provides a <code>cons</code> so a straightforward translation is possible <b>but</b>
it blows the recursion stack after a certain number of elements. They are <a href="#orgd55431b">rewritten</a> to
<code>list</code> calls below but a <code>cons</code> is provided to adhere to the standard:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/cons</span> <span style="color: #7388D6;">(</span>A Rest<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span>cons A Rest<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
The rest of the list operations function as expected:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/hd</span> <span style="color: #7388D6;">(</span>List<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>car List<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/tl</span> <span style="color: #7388D6;">(</span>List<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>cdr List<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/cons?</span> <span style="color: #7388D6;">(</span>List<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>shen/internal/predicate->shen <span style="color: #909183;">(</span>consp List<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgb0fc293" class="outline-3">
<h3 id="orgb0fc293"><span class="section-number-3">2.13</span> Strings</h3>
<div class="outline-text-3" id="text-2-13">
<ul class="org-ul">
<li><p>
Printing KLambda datatypes.
</p>
<p>
The only weirdness here is why we print the <code>buffer-name</code>
of a stream. That is explained in the <a href="#org7b70b68">2.18</a>.:
</p></li>
</ul>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/str</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">cond</span> <span style="color: #909183;">(</span><span style="color: #709870;">(</span>null X<span style="color: #709870;">)</span> <span style="color: #709870;">(</span><span style="color: #ff0000; font-weight: bold;">error</span> <span style="color: #008000;">"null is not an atom in Shen; str cannot convert it to a string.~%"</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">or</span> <span style="color: #907373;">(</span>symbolp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>functionp X<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>symbol-name X<span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span>numberp X<span style="color: #709870;">)</span> <span style="color: #709870;">(</span>number-to-string X<span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span>stringp X<span style="color: #709870;">)</span> X<span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>bufferp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>buffer-file-name X<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>buffer-name X<span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span>eq X standard-input<span style="color: #709870;">)</span> <span style="color: #008000;">"standard-input"</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span>eq X standard-output<span style="color: #709870;">)</span> <span style="color: #008000;">"standard-output"</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>t
<span style="color: #709870;">(</span><span style="color: #ff0000; font-weight: bold;">error</span> <span style="color: #907373;">(</span>format <span style="color: #008000;">"%s is not an atom, stream or closure; str cannot convert it to a string."</span> X<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<ul class="org-ul">
<li>Given string <code>S</code> get the character at index <code>N</code>:</li>
</ul>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/pos</span> <span style="color: #7388D6;">(</span>S N<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>string <span style="color: #909183;">(</span>aref S N<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<ul class="org-ul">
<li>Get the rest of a non-empty string:</li>
</ul>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/tlstr</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>substring X <span style="color: #D0372D;">1</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<ul class="org-ul">
<li>Test for a string, join them and convert between characters and strings:</li>
</ul>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/string?</span> <span style="color: #7388D6;">(</span>S<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>shen/internal/predicate->shen <span style="color: #909183;">(</span>stringp S<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/cn</span> <span style="color: #7388D6;">(</span>Str1 Str2<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>concat Str1 Str2<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/n->string</span> <span style="color: #7388D6;">(</span>N<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>string N<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/string->n</span> <span style="color: #7388D6;">(</span>S<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>string-to-char S<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
NOTE: If a non-empty string is converted to a character only the first character
of the string is considered.
</p>
</div>
</div>
<div id="outline-container-orge86d3de" class="outline-3">
<h3 id="orge86d3de"><span class="section-number-3">2.14</span> Error Handling</h3>
<div class="outline-text-3" id="text-2-14">
<p>
Elisp's <code>error</code> and <code>condition-case</code> covers the primitive error handling
required by the spec:
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span>define-error 'shen/error <span style="color: #008000;">"Shen error"</span> 'error<span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/simple-error</span> <span style="color: #7388D6;">(</span>E<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #ff0000; font-weight: bold;">signal</span> 'shen/error
<span style="color: #909183;">(</span><span style="color: #0000FF;">if</span> <span style="color: #709870;">(</span>stringp E<span style="color: #709870;">)</span>
<span style="color: #709870;">(</span>list E<span style="color: #709870;">)</span>
E<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defmacro</span> <span style="color: #006699;">shen/trap-error</span> <span style="color: #7388D6;">(</span>X F<span style="color: #7388D6;">)</span>
`<span style="color: #7388D6;">(</span><span style="color: #0000FF;">condition-case</span> ex ,X <span style="color: #909183;">(</span>'error <span style="color: #709870;">(</span>funcall ,F ex<span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/error-to-string</span> <span style="color: #7388D6;">(</span>E<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>format <span style="color: #008000;">"%s"</span> E<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org4b2085f" class="outline-3">
<h3 id="org4b2085f"><span class="section-number-3">2.15</span> Vectors</h3>
<div class="outline-text-3" id="text-2-15">
<p>
Hash tables are used to represent Klambda vectors. This is counter-intuitive
since Elisp does have native vectors but unfortunately they are not resizable.
Since KLambda code tends to allocate huge vectors and resize often, switching
from vectors to hash-tables where resizing is natively supported led to 4-5x
speed-ups across the board.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/absvector</span> <span style="color: #7388D6;">(</span>N<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>make-hash-table <span style="color: #006FE0;">:size</span> N <span style="color: #006FE0;">:rehash-size</span> <span style="color: #D0372D;">3.0</span> <span style="color: #006FE0;">:test</span> 'shen/internal/hash-table-test<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/address-></span> <span style="color: #7388D6;">(</span>Vector N Value<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span><span style="color: #0000FF;">progn</span> <span style="color: #909183;">(</span>puthash N Value Vector<span style="color: #909183;">)</span> Vector<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/<-address</span> <span style="color: #7388D6;">(</span>Vector N<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>gethash N Vector<span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defsubst</span> <span style="color: #006699;">shen/absvector?</span> <span style="color: #7388D6;">(</span>X<span style="color: #7388D6;">)</span> <span style="color: #7388D6;">(</span>shen/internal/predicate->shen <span style="color: #909183;">(</span>hash-table-p X<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
While using hash-tables for Shen vectors, tuples and property lists do offer a
significant speed-ups there is an ugly downside: if any of those structures are
used as keys, lookups will fail because in Elisp two hash-tables do not hash to
the same number and so using <code>(make-hash-table ... :test 'equal)</code> does not work.
So we roll our own test which delegates to <code>shen/internal/=</code> for testing equality
and only generates a hash for these structures using only their keys and values.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span>define-hash-table-test
'shen/internal/hash-table-test
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">lambda</span> <span style="color: #909183;">(</span>X Y<span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>shen/internal/= X Y<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">lambda</span> <span style="color: #909183;">(</span>X<span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #0000FF;">cond</span>
<span style="color: #709870;">(</span><span style="color: #907373;">(</span>numberp X<span style="color: #907373;">)</span> X<span style="color: #709870;">)</span>
<span style="color: #709870;">(</span><span style="color: #907373;">(</span>consp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>sxhash <span style="color: #6276BA;">(</span>prin1-to-string X<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span><span style="color: #907373;">(</span>hash-table-p X<span style="color: #907373;">)</span>
<span style="color: #907373;">(</span>sxhash <span style="color: #6276BA;">(</span>prin1-to-string X<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span>t <span style="color: #907373;">(</span>sxhash X<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org1c0998c" class="outline-3">
<h3 id="org1c0998c"><span class="section-number-3">2.16</span> Arithmetic Operations</h3>
<div class="outline-text-3" id="text-2-16">
<p>
In KLambda there is only <code>number</code> so we have to take care to coerce to between <code>float</code> and
<code>integer</code> as necessary.
</p>
<p>
Most of this code is heavily borrowed from <a href="https://github.com/larsbrinkhoff/emacs-cl/blob/master/src/cl-numbers.el">emacs-cl</a> but simplified since Shen does not have
the zoo of numeric types supported by CL.
</p>
<p>
First a couple of limits to detect when a multiplication or addition might exceed the bounds
of an <code>integer</code>,
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defconst</span> <span style="color: #BA36A5;">shen/multiplication-limit</span> <span style="color: #7388D6;">(</span>floor <span style="color: #909183;">(</span>sqrt most-positive-fixnum<span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
<span style="color: #707183;">(</span><span style="color: #0000FF;">defconst</span> <span style="color: #BA36A5;">shen/addition-limit</span> <span style="color: #7388D6;">(</span>floor <span style="color: #909183;">(</span>/ most-positive-fixnum <span style="color: #D0372D;">2</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
a generic function to coerce to a float if necessary,
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #707183;">(</span><span style="color: #0000FF;">defun</span> <span style="color: #006699;">shen/number-op</span> <span style="color: #7388D6;">(</span>X Y max op<span style="color: #7388D6;">)</span>
<span style="color: #7388D6;">(</span><span style="color: #0000FF;">cond</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>integerp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>integerp Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span>
<span style="color: #709870;">(</span><span style="color: #0000FF;">if</span> <span style="color: #907373;">(</span><span style="color: #0000FF;">and</span> <span style="color: #6276BA;">(</span>< X max<span style="color: #6276BA;">)</span>
<span style="color: #6276BA;">(</span>> X <span style="color: #858580;">(</span>- max<span style="color: #858580;">)</span><span style="color: #6276BA;">)</span>
<span style="color: #6276BA;">(</span>< Y max<span style="color: #6276BA;">)</span>
<span style="color: #6276BA;">(</span>> Y <span style="color: #858580;">(</span>- max<span style="color: #858580;">)</span><span style="color: #6276BA;">)</span><span style="color: #907373;">)</span>
<span style="color: #907373;">(</span>apply op <span style="color: #6276BA;">(</span>list X Y<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span>
<span style="color: #907373;">(</span>apply op <span style="color: #6276BA;">(</span>list <span style="color: #858580;">(</span>float X<span style="color: #858580;">)</span> <span style="color: #858580;">(</span>float Y<span style="color: #858580;">)</span><span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>floatp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>numberp Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>apply op <span style="color: #907373;">(</span>list X <span style="color: #6276BA;">(</span>float Y<span style="color: #6276BA;">)</span><span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span><span style="color: #709870;">(</span><span style="color: #0000FF;">and</span> <span style="color: #907373;">(</span>numberp X<span style="color: #907373;">)</span> <span style="color: #907373;">(</span>floatp Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span> <span style="color: #709870;">(</span>apply op <span style="color: #907373;">(</span>list <span style="color: #6276BA;">(</span>float X<span style="color: #6276BA;">)</span> Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span>
<span style="color: #909183;">(</span>t <span style="color: #709870;">(</span><span style="color: #ff0000; font-weight: bold;">error</span> <span style="color: #907373;">(</span>format <span style="color: #008000;">"Trying to %s. Both %s and %s must be numbers"</span> op X Y<span style="color: #907373;">)</span><span style="color: #709870;">)</span><span style="color: #909183;">)</span><span style="color: #7388D6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
and the standard arithmetic functions.
</p>
<div class="org-src-container">