OSDN Git Service

Working on the Sphinx docs.
[joypy/Thun.git] / docs / sphinx_docs / lib.rst
1
2 Examples (and some documentation) for the Words in the Library
3 ==============================================================
4
5 .. code:: ipython2
6
7     from notebook_preamble import J, V
8
9 Stack Chatter
10 =============
11
12 This is what I like to call the functions that just rearrange things on
13 the stack. (One thing I want to mention is that during a hypothetical
14 compilation phase these "stack chatter" words effectively disappear,
15 because we can map the logical stack locations to registers that remain
16 static for the duration of the computation. This remains to be done but
17 it's "off the shelf" technology.)
18
19 ``clear``
20 ~~~~~~~~~
21
22 .. code:: ipython2
23
24     J('1 2 3 clear')
25
26
27 .. parsed-literal::
28
29     
30
31
32 ``dup`` ``dupd``
33 ~~~~~~~~~~~~~~~~
34
35 .. code:: ipython2
36
37     J('1 2 3 dup')
38
39
40 .. parsed-literal::
41
42     1 2 3 3
43
44
45 .. code:: ipython2
46
47     J('1 2 3 dupd')
48
49
50 .. parsed-literal::
51
52     1 2 2 3
53
54
55 ``enstacken`` ``disenstacken`` ``stack`` ``unstack``
56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57
58 (I may have these paired up wrong. I.e. ``disenstacken`` should be
59 ``unstack`` and vice versa.)
60
61 .. code:: ipython2
62
63     J('1 2 3 enstacken') # Replace the stack with a quote of itself.
64
65
66 .. parsed-literal::
67
68     [3 2 1]
69
70
71 .. code:: ipython2
72
73     J('4 5 6 [3 2 1] disenstacken')  # Unpack a list onto the stack.
74
75
76 .. parsed-literal::
77
78     4 5 6 3 2 1
79
80
81 .. code:: ipython2
82
83     J('1 2 3 stack')  # Get the stack on the stack.
84
85
86 .. parsed-literal::
87
88     1 2 3 [3 2 1]
89
90
91 .. code:: ipython2
92
93     J('1 2 3 [4 5 6] unstack')  # Replace the stack with the list on top.
94                                 # The items appear reversed but they are not,
95                                 # 4 is on the top of both the list and the stack.
96
97
98 .. parsed-literal::
99
100     6 5 4
101
102
103 ``pop`` ``popd`` ``popop``
104 ~~~~~~~~~~~~~~~~~~~~~~~~~~
105
106 .. code:: ipython2
107
108     J('1 2 3 pop')
109
110
111 .. parsed-literal::
112
113     1 2
114
115
116 .. code:: ipython2
117
118     J('1 2 3 popd')
119
120
121 .. parsed-literal::
122
123     1 3
124
125
126 .. code:: ipython2
127
128     J('1 2 3 popop')
129
130
131 .. parsed-literal::
132
133     1
134
135
136 ``roll<`` ``rolldown`` ``roll>`` ``rollup``
137 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138
139 The "down" and "up" refer to the movement of two of the top three items
140 (displacing the third.)
141
142 .. code:: ipython2
143
144     J('1 2 3 roll<')
145
146
147 .. parsed-literal::
148
149     2 3 1
150
151
152 .. code:: ipython2
153
154     J('1 2 3 roll>')
155
156
157 .. parsed-literal::
158
159     3 1 2
160
161
162 ``swap``
163 ~~~~~~~~
164
165 .. code:: ipython2
166
167     J('1 2 3 swap')
168
169
170 .. parsed-literal::
171
172     1 3 2
173
174
175 ``tuck`` ``over``
176 ~~~~~~~~~~~~~~~~~
177
178 .. code:: ipython2
179
180     J('1 2 3 tuck')
181
182
183 .. parsed-literal::
184
185     1 3 2 3
186
187
188 .. code:: ipython2
189
190     J('1 2 3 over')
191
192
193 .. parsed-literal::
194
195     1 2 3 2
196
197
198 ``unit`` ``quoted`` ``unquoted``
199 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
201 .. code:: ipython2
202
203     J('1 2 3 unit')
204
205
206 .. parsed-literal::
207
208     1 2 [3]
209
210
211 .. code:: ipython2
212
213     J('1 2 3 quoted')
214
215
216 .. parsed-literal::
217
218     1 [2] 3
219
220
221 .. code:: ipython2
222
223     J('1 [2] 3 unquoted')
224
225
226 .. parsed-literal::
227
228     1 2 3
229
230
231 .. code:: ipython2
232
233     V('1 [dup] 3 unquoted')  # Unquoting evaluates.  Be aware.
234
235
236 .. parsed-literal::
237
238                   . 1 [dup] 3 unquoted
239                 1 . [dup] 3 unquoted
240           1 [dup] . 3 unquoted
241         1 [dup] 3 . unquoted
242         1 [dup] 3 . [i] dip
243     1 [dup] 3 [i] . dip
244           1 [dup] . i 3
245                 1 . dup 3
246               1 1 . 3
247             1 1 3 . 
248
249
250 List words
251 ==========
252
253 ``concat`` ``swoncat`` ``shunt``
254 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
255
256 .. code:: ipython2
257
258     J('[1 2 3] [4 5 6] concat')
259
260
261 .. parsed-literal::
262
263     [1 2 3 4 5 6]
264
265
266 .. code:: ipython2
267
268     J('[1 2 3] [4 5 6] swoncat')
269
270
271 .. parsed-literal::
272
273     [4 5 6 1 2 3]
274
275
276 .. code:: ipython2
277
278     J('[1 2 3] [4 5 6] shunt')
279
280
281 .. parsed-literal::
282
283     [6 5 4 1 2 3]
284
285
286 ``cons`` ``swons`` ``uncons``
287 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288
289 .. code:: ipython2
290
291     J('1 [2 3] cons')
292
293
294 .. parsed-literal::
295
296     [1 2 3]
297
298
299 .. code:: ipython2
300
301     J('[2 3] 1 swons')
302
303
304 .. parsed-literal::
305
306     [1 2 3]
307
308
309 .. code:: ipython2
310
311     J('[1 2 3] uncons')
312
313
314 .. parsed-literal::
315
316     1 [2 3]
317
318
319 ``first`` ``second`` ``third`` ``rest``
320 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
321
322 .. code:: ipython2
323
324     J('[1 2 3 4] first')
325
326
327 .. parsed-literal::
328
329     1
330
331
332 .. code:: ipython2
333
334     J('[1 2 3 4] second')
335
336
337 .. parsed-literal::
338
339     2
340
341
342 .. code:: ipython2
343
344     J('[1 2 3 4] third')
345
346
347 .. parsed-literal::
348
349     3
350
351
352 .. code:: ipython2
353
354     J('[1 2 3 4] rest')
355
356
357 .. parsed-literal::
358
359     [2 3 4]
360
361
362 ``flatten``
363 ~~~~~~~~~~~
364
365 .. code:: ipython2
366
367     J('[[1] [2 [3] 4] [5 6]] flatten')
368
369
370 .. parsed-literal::
371
372     [1 2 [3] 4 5 6]
373
374
375 ``getitem`` ``at`` ``of`` ``drop`` ``take``
376 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
377
378 ``at`` and ``getitem`` are the same function. ``of == swap at``
379
380 .. code:: ipython2
381
382     J('[10 11 12 13 14] 2 getitem')
383
384
385 .. parsed-literal::
386
387     12
388
389
390 .. code:: ipython2
391
392     J('[1 2 3 4] 0 at')
393
394
395 .. parsed-literal::
396
397     1
398
399
400 .. code:: ipython2
401
402     J('2 [1 2 3 4] of')
403
404
405 .. parsed-literal::
406
407     3
408
409
410 .. code:: ipython2
411
412     J('[1 2 3 4] 2 drop')
413
414
415 .. parsed-literal::
416
417     [3 4]
418
419
420 .. code:: ipython2
421
422     J('[1 2 3 4] 2 take')  # reverses the order
423
424
425 .. parsed-literal::
426
427     [2 1]
428
429
430 ``reverse`` could be defines as ``reverse == dup size take``
431
432 ``remove``
433 ~~~~~~~~~~
434
435 .. code:: ipython2
436
437     J('[1 2 3 1 4] 1 remove')
438
439
440 .. parsed-literal::
441
442     [2 3 1 4]
443
444
445 ``reverse``
446 ~~~~~~~~~~~
447
448 .. code:: ipython2
449
450     J('[1 2 3 4] reverse')
451
452
453 .. parsed-literal::
454
455     [4 3 2 1]
456
457
458 ``size``
459 ~~~~~~~~
460
461 .. code:: ipython2
462
463     J('[1 1 1 1] size')
464
465
466 .. parsed-literal::
467
468     4
469
470
471 ``swaack``
472 ~~~~~~~~~~
473
474 "Swap stack" swap the list on the top of the stack for the stack, and
475 put the old stack on top of the new one. Think of it as a context
476 switch. Niether of the lists/stacks change their order.
477
478 .. code:: ipython2
479
480     J('1 2 3 [4 5 6] swaack')
481
482
483 .. parsed-literal::
484
485     6 5 4 [3 2 1]
486
487
488 ``choice`` ``select``
489 ~~~~~~~~~~~~~~~~~~~~~
490
491 .. code:: ipython2
492
493     J('23 9 1 choice')
494
495
496 .. parsed-literal::
497
498     9
499
500
501 .. code:: ipython2
502
503     J('23 9 0 choice')
504
505
506 .. parsed-literal::
507
508     23
509
510
511 .. code:: ipython2
512
513     J('[23 9 7] 1 select')  # select is basically getitem, should retire it?
514
515
516 .. parsed-literal::
517
518     9
519
520
521 .. code:: ipython2
522
523     J('[23 9 7] 0 select')
524
525
526 .. parsed-literal::
527
528     23
529
530
531 ``zip``
532 ~~~~~~~
533
534 .. code:: ipython2
535
536     J('[1 2 3] [6 5 4] zip')
537
538
539 .. parsed-literal::
540
541     [[6 1] [5 2] [4 3]]
542
543
544 .. code:: ipython2
545
546     J('[1 2 3] [6 5 4] zip [sum] map')
547
548
549 .. parsed-literal::
550
551     [7 7 7]
552
553
554 Math words
555 ==========
556
557 ``+`` ``add``
558 ~~~~~~~~~~~~~
559
560 .. code:: ipython2
561
562     J('23 9 +')
563
564
565 .. parsed-literal::
566
567     32
568
569
570 ``-`` ``sub``
571 ~~~~~~~~~~~~~
572
573 .. code:: ipython2
574
575     J('23 9 -')
576
577
578 .. parsed-literal::
579
580     14
581
582
583 ``*`` ``mul``
584 ~~~~~~~~~~~~~
585
586 .. code:: ipython2
587
588     J('23 9 *')
589
590
591 .. parsed-literal::
592
593     207
594
595
596 ``/`` ``div`` ``floordiv`` ``truediv``
597 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
598
599 .. code:: ipython2
600
601     J('23 9 /')
602
603
604 .. parsed-literal::
605
606     2.5555555555555554
607
608
609 .. code:: ipython2
610
611     J('23 -9 truediv')
612
613
614 .. parsed-literal::
615
616     -2.5555555555555554
617
618
619 .. code:: ipython2
620
621     J('23 9 div')
622
623
624 .. parsed-literal::
625
626     2
627
628
629 .. code:: ipython2
630
631     J('23 9 floordiv')
632
633
634 .. parsed-literal::
635
636     2
637
638
639 .. code:: ipython2
640
641     J('23 -9 div')
642
643
644 .. parsed-literal::
645
646     -3
647
648
649 .. code:: ipython2
650
651     J('23 -9 floordiv')
652
653
654 .. parsed-literal::
655
656     -3
657
658
659 ``%`` ``mod`` ``modulus`` ``rem`` ``remainder``
660 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
661
662 .. code:: ipython2
663
664     J('23 9 %')
665
666
667 .. parsed-literal::
668
669     5
670
671
672 ``neg``
673 ~~~~~~~
674
675 .. code:: ipython2
676
677     J('23 neg -5 neg')
678
679
680 .. parsed-literal::
681
682     -23 5
683
684
685 pow
686 ~~~
687
688 .. code:: ipython2
689
690     J('2 10 pow')
691
692
693 .. parsed-literal::
694
695     1024
696
697
698 ``sqr`` ``sqrt``
699 ~~~~~~~~~~~~~~~~
700
701 .. code:: ipython2
702
703     J('23 sqr')
704
705
706 .. parsed-literal::
707
708     529
709
710
711 .. code:: ipython2
712
713     J('23 sqrt')
714
715
716 .. parsed-literal::
717
718     4.795831523312719
719
720
721 ``++`` ``succ`` ``--`` ``pred``
722 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
723
724 .. code:: ipython2
725
726     J('1 ++')
727
728
729 .. parsed-literal::
730
731     2
732
733
734 .. code:: ipython2
735
736     J('1 --')
737
738
739 .. parsed-literal::
740
741     0
742
743
744 ``<<`` ``lshift`` ``>>`` ``rshift``
745 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
746
747 .. code:: ipython2
748
749     J('8 1 <<')
750
751
752 .. parsed-literal::
753
754     16
755
756
757 .. code:: ipython2
758
759     J('8 1 >>')
760
761
762 .. parsed-literal::
763
764     4
765
766
767 ``average``
768 ~~~~~~~~~~~
769
770 .. code:: ipython2
771
772     J('[1 2 3 5] average')
773
774
775 .. parsed-literal::
776
777     2.75
778
779
780 ``range`` ``range_to_zero`` ``down_to_zero``
781 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
782
783 .. code:: ipython2
784
785     J('5 range')
786
787
788 .. parsed-literal::
789
790     [4 3 2 1 0]
791
792
793 .. code:: ipython2
794
795     J('5 range_to_zero')
796
797
798 .. parsed-literal::
799
800     [0 1 2 3 4 5]
801
802
803 .. code:: ipython2
804
805     J('5 down_to_zero')
806
807
808 .. parsed-literal::
809
810     5 4 3 2 1 0
811
812
813 ``product``
814 ~~~~~~~~~~~
815
816 .. code:: ipython2
817
818     J('[1 2 3 5] product')
819
820
821 .. parsed-literal::
822
823     30
824
825
826 ``sum``
827 ~~~~~~~
828
829 .. code:: ipython2
830
831     J('[1 2 3 5] sum')
832
833
834 .. parsed-literal::
835
836     11
837
838
839 ``min``
840 ~~~~~~~
841
842 .. code:: ipython2
843
844     J('[1 2 3 5] min')
845
846
847 .. parsed-literal::
848
849     1
850
851
852 ``gcd``
853 ~~~~~~~
854
855 .. code:: ipython2
856
857     J('45 30 gcd')
858
859
860 .. parsed-literal::
861
862     15
863
864
865 ``least_fraction``
866 ~~~~~~~~~~~~~~~~~~
867
868 If we represent fractions as a quoted pair of integers [q d] this word
869 reduces them to their ... least common factors or whatever.
870
871 .. code:: ipython2
872
873     J('[45 30] least_fraction')
874
875
876 .. parsed-literal::
877
878     [3 2]
879
880
881 .. code:: ipython2
882
883     J('[23 12] least_fraction')
884
885
886 .. parsed-literal::
887
888     [23 12]
889
890
891 Logic and Comparison
892 ====================
893
894 ``?`` ``truthy``
895 ~~~~~~~~~~~~~~~~
896
897 Get the Boolean value of the item on the top of the stack.
898
899 .. code:: ipython2
900
901     J('23 truthy')
902
903
904 .. parsed-literal::
905
906     True
907
908
909 .. code:: ipython2
910
911     J('[] truthy')  # Python semantics.
912
913
914 .. parsed-literal::
915
916     False
917
918
919 .. code:: ipython2
920
921     J('0 truthy')
922
923
924 .. parsed-literal::
925
926     False
927
928
929 ::
930
931     ? == dup truthy
932
933 .. code:: ipython2
934
935     V('23 ?')
936
937
938 .. parsed-literal::
939
940             . 23 ?
941          23 . ?
942          23 . dup truthy
943       23 23 . truthy
944     23 True . 
945
946
947 .. code:: ipython2
948
949     J('[] ?')
950
951
952 .. parsed-literal::
953
954     [] False
955
956
957 .. code:: ipython2
958
959     J('0 ?')
960
961
962 .. parsed-literal::
963
964     0 False
965
966
967 ``&`` ``and``
968 ~~~~~~~~~~~~~
969
970 .. code:: ipython2
971
972     J('23 9 &')
973
974
975 .. parsed-literal::
976
977     1
978
979
980 ``!=`` ``<>`` ``ne``
981 ~~~~~~~~~~~~~~~~~~~~
982
983 .. code:: ipython2
984
985     J('23 9 !=')
986
987
988 .. parsed-literal::
989
990     True
991
992
993 | The usual suspects: - ``<`` ``lt`` - ``<=`` ``le``
994 | - ``=`` ``eq`` - ``>`` ``gt`` - ``>=`` ``ge`` - ``not`` - ``or``
995
996 ``^`` ``xor``
997 ~~~~~~~~~~~~~
998
999 .. code:: ipython2
1000
1001     J('1 1 ^')
1002
1003
1004 .. parsed-literal::
1005
1006     0
1007
1008
1009 .. code:: ipython2
1010
1011     J('1 0 ^')
1012
1013
1014 .. parsed-literal::
1015
1016     1
1017
1018
1019 Miscellaneous
1020 =============
1021
1022 ``help``
1023 ~~~~~~~~
1024
1025 .. code:: ipython2
1026
1027     J('[help] help')
1028
1029
1030 .. parsed-literal::
1031
1032     Accepts a quoted symbol on the top of the stack and prints its docs.
1033     
1034
1035
1036 ``parse``
1037 ~~~~~~~~~
1038
1039 .. code:: ipython2
1040
1041     J('[parse] help')
1042
1043
1044 .. parsed-literal::
1045
1046     Parse the string on the stack to a Joy expression.
1047     
1048
1049
1050 .. code:: ipython2
1051
1052     J('1 "2 [3] dup" parse')
1053
1054
1055 .. parsed-literal::
1056
1057     1 [2 [3] dup]
1058
1059
1060 ``run``
1061 ~~~~~~~
1062
1063 Evaluate a quoted Joy sequence.
1064
1065 .. code:: ipython2
1066
1067     J('[1 2 dup + +] run')
1068
1069
1070 .. parsed-literal::
1071
1072     [5]
1073
1074
1075 Combinators
1076 ===========
1077
1078 ``app1`` ``app2`` ``app3``
1079 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1080
1081 .. code:: ipython2
1082
1083     J('[app1] help')
1084
1085
1086 .. parsed-literal::
1087
1088     Given a quoted program on TOS and anything as the second stack item run
1089     the program and replace the two args with the first result of the
1090     program.
1091     
1092                 ... x [Q] . app1
1093        -----------------------------------
1094           ... [x ...] [Q] . infra first
1095     
1096
1097
1098 .. code:: ipython2
1099
1100     J('10 4 [sqr *] app1')
1101
1102
1103 .. parsed-literal::
1104
1105     10 160
1106
1107
1108 .. code:: ipython2
1109
1110     J('10 3 4 [sqr *] app2')
1111
1112
1113 .. parsed-literal::
1114
1115     10 90 160
1116
1117
1118 .. code:: ipython2
1119
1120     J('[app2] help')
1121
1122
1123 .. parsed-literal::
1124
1125     Like app1 with two items.
1126     
1127            ... y x [Q] . app2
1128     -----------------------------------
1129        ... [y ...] [Q] . infra first
1130            [x ...] [Q]   infra first
1131     
1132
1133
1134 .. code:: ipython2
1135
1136     J('10 2 3 4 [sqr *] app3')
1137
1138
1139 .. parsed-literal::
1140
1141     10 40 90 160
1142
1143
1144 ``anamorphism``
1145 ~~~~~~~~~~~~~~~
1146
1147 Given an initial value, a predicate function ``[P]``, and a generator
1148 function ``[G]``, the ``anamorphism`` combinator creates a sequence.
1149
1150 ::
1151
1152        n [P] [G] anamorphism
1153     ---------------------------
1154               [...]
1155
1156 Example, ``range``:
1157
1158 ::
1159
1160     range == [0 <=] [1 - dup] anamorphism
1161
1162 .. code:: ipython2
1163
1164     J('3 [0 <=] [1 - dup] anamorphism')
1165
1166
1167 .. parsed-literal::
1168
1169     [2 1 0]
1170
1171
1172 ``branch``
1173 ~~~~~~~~~~
1174
1175 .. code:: ipython2
1176
1177     J('3 4 1 [+] [*] branch')
1178
1179
1180 .. parsed-literal::
1181
1182     12
1183
1184
1185 .. code:: ipython2
1186
1187     J('3 4 0 [+] [*] branch')
1188
1189
1190 .. parsed-literal::
1191
1192     7
1193
1194
1195 ``cleave``
1196 ~~~~~~~~~~
1197
1198 ::
1199
1200     ... x [P] [Q] cleave
1201
1202 From the original Joy docs: "The cleave combinator expects two
1203 quotations, and below that an item ``x`` It first executes ``[P]``, with
1204 ``x`` on top, and saves the top result element. Then it executes
1205 ``[Q]``, again with ``x``, and saves the top result. Finally it restores
1206 the stack to what it was below ``x`` and pushes the two results P(X) and
1207 Q(X)."
1208
1209 Note that ``P`` and ``Q`` can use items from the stack freely, since the
1210 stack (below ``x``) is restored. ``cleave`` is a kind of *parallel*
1211 primitive, and it would make sense to create a version that uses, e.g.
1212 Python threads or something, to actually run ``P`` and ``Q``
1213 concurrently. The current implementation of ``cleave`` is a definition
1214 in terms of ``app2``:
1215
1216 ::
1217
1218     cleave == [i] app2 [popd] dip
1219
1220 .. code:: ipython2
1221
1222     J('10 2 [+] [-] cleave')
1223
1224
1225 .. parsed-literal::
1226
1227     10 12 8
1228
1229
1230 ``dip`` ``dipd`` ``dipdd``
1231 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1232
1233 .. code:: ipython2
1234
1235     J('1 2 3 4 5 [+] dip')
1236
1237
1238 .. parsed-literal::
1239
1240     1 2 7 5
1241
1242
1243 .. code:: ipython2
1244
1245     J('1 2 3 4 5 [+] dipd')
1246
1247
1248 .. parsed-literal::
1249
1250     1 5 4 5
1251
1252
1253 .. code:: ipython2
1254
1255     J('1 2 3 4 5 [+] dipdd')
1256
1257
1258 .. parsed-literal::
1259
1260     3 3 4 5
1261
1262
1263 ``dupdip``
1264 ~~~~~~~~~~
1265
1266 Expects a quoted program ``[Q]`` on the stack and some item under it,
1267 ``dup`` the item and ``dip`` the quoted program under it.
1268
1269 ::
1270
1271     n [Q] dupdip == n Q n
1272
1273 .. code:: ipython2
1274
1275     V('23 [++] dupdip *')  # N(N + 1)
1276
1277
1278 .. parsed-literal::
1279
1280             . 23 [++] dupdip *
1281          23 . [++] dupdip *
1282     23 [++] . dupdip *
1283          23 . ++ 23 *
1284          24 . 23 *
1285       24 23 . *
1286         552 . 
1287
1288
1289 ``genrec`` ``primrec``
1290 ~~~~~~~~~~~~~~~~~~~~~~
1291
1292 .. code:: ipython2
1293
1294     J('[genrec] help')
1295
1296
1297 .. parsed-literal::
1298
1299     General Recursion Combinator.
1300     
1301                             [if] [then] [rec1] [rec2] genrec
1302       ---------------------------------------------------------------------
1303          [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
1304     
1305     From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun:
1306     "The genrec combinator takes four program parameters in addition to
1307     whatever data parameters it needs. Fourth from the top is an if-part,
1308     followed by a then-part. If the if-part yields true, then the then-part
1309     is executed and the combinator terminates. The other two parameters are
1310     the rec1-part and the rec2-part. If the if-part yields false, the
1311     rec1-part is executed. Following that the four program parameters and
1312     the combinator are again pushed onto the stack bundled up in a quoted
1313     form. Then the rec2-part is executed, where it will find the bundled
1314     form. Typically it will then execute the bundled form, either with i or
1315     with app2, or some other combinator."
1316     
1317     The way to design one of these is to fix your base case [then] and the
1318     test [if], and then treat rec1 and rec2 as an else-part "sandwiching"
1319     a quotation of the whole function.
1320     
1321     For example, given a (general recursive) function 'F':
1322     
1323         F == [I] [T] [R1] [R2] genrec
1324     
1325     If the [I] if-part fails you must derive R1 and R2 from:
1326     
1327         ... R1 [F] R2
1328     
1329     Just set the stack arguments in front, and figure out what R1 and R2
1330     have to do to apply the quoted [F] in the proper way.  In effect, the
1331     genrec combinator turns into an ifte combinator with a quoted copy of
1332     the original definition in the else-part:
1333     
1334         F == [I] [T] [R1]   [R2] genrec
1335           == [I] [T] [R1 [F] R2] ifte
1336     
1337     (Primitive recursive functions are those where R2 == i.
1338     
1339         P == [I] [T] [R] primrec
1340           == [I] [T] [R [P] i] ifte
1341           == [I] [T] [R P] ifte
1342     )
1343     
1344
1345
1346 .. code:: ipython2
1347
1348     J('3 [1 <=] [] [dup --] [i *] genrec')
1349
1350
1351 .. parsed-literal::
1352
1353     6
1354
1355
1356 ``i``
1357 ~~~~~
1358
1359 .. code:: ipython2
1360
1361     V('1 2 3 [+ +] i')
1362
1363
1364 .. parsed-literal::
1365
1366                 . 1 2 3 [+ +] i
1367               1 . 2 3 [+ +] i
1368             1 2 . 3 [+ +] i
1369           1 2 3 . [+ +] i
1370     1 2 3 [+ +] . i
1371           1 2 3 . + +
1372             1 5 . +
1373               6 . 
1374
1375
1376 ``ifte``
1377 ~~~~~~~~
1378
1379 ::
1380
1381     [predicate] [then] [else] ifte
1382
1383 .. code:: ipython2
1384
1385     J('1 2 [1] [+] [*] ifte')
1386
1387
1388 .. parsed-literal::
1389
1390     3
1391
1392
1393 .. code:: ipython2
1394
1395     J('1 2 [0] [+] [*] ifte')
1396
1397
1398 .. parsed-literal::
1399
1400     2
1401
1402
1403 ``infra``
1404 ~~~~~~~~~
1405
1406 .. code:: ipython2
1407
1408     V('1 2 3 [4 5 6] [* +] infra')
1409
1410
1411 .. parsed-literal::
1412
1413                         . 1 2 3 [4 5 6] [* +] infra
1414                       1 . 2 3 [4 5 6] [* +] infra
1415                     1 2 . 3 [4 5 6] [* +] infra
1416                   1 2 3 . [4 5 6] [* +] infra
1417           1 2 3 [4 5 6] . [* +] infra
1418     1 2 3 [4 5 6] [* +] . infra
1419                   6 5 4 . * + [3 2 1] swaack
1420                    6 20 . + [3 2 1] swaack
1421                      26 . [3 2 1] swaack
1422              26 [3 2 1] . swaack
1423              1 2 3 [26] . 
1424
1425
1426 ``loop``
1427 ~~~~~~~~
1428
1429 .. code:: ipython2
1430
1431     J('[loop] help')
1432
1433
1434 .. parsed-literal::
1435
1436     Basic loop combinator.
1437     
1438        ... True [Q] loop
1439     -----------------------
1440          ... Q [Q] loop
1441     
1442        ... False [Q] loop
1443     ------------------------
1444               ...
1445     
1446
1447
1448 .. code:: ipython2
1449
1450     V('3 dup [1 - dup] loop')
1451
1452
1453 .. parsed-literal::
1454
1455                   . 3 dup [1 - dup] loop
1456                 3 . dup [1 - dup] loop
1457               3 3 . [1 - dup] loop
1458     3 3 [1 - dup] . loop
1459                 3 . 1 - dup [1 - dup] loop
1460               3 1 . - dup [1 - dup] loop
1461                 2 . dup [1 - dup] loop
1462               2 2 . [1 - dup] loop
1463     2 2 [1 - dup] . loop
1464                 2 . 1 - dup [1 - dup] loop
1465               2 1 . - dup [1 - dup] loop
1466                 1 . dup [1 - dup] loop
1467               1 1 . [1 - dup] loop
1468     1 1 [1 - dup] . loop
1469                 1 . 1 - dup [1 - dup] loop
1470               1 1 . - dup [1 - dup] loop
1471                 0 . dup [1 - dup] loop
1472               0 0 . [1 - dup] loop
1473     0 0 [1 - dup] . loop
1474                 0 . 
1475
1476
1477 ``map`` ``pam``
1478 ~~~~~~~~~~~~~~~
1479
1480 .. code:: ipython2
1481
1482     J('10 [1 2 3] [*] map')
1483
1484
1485 .. parsed-literal::
1486
1487     10 [10 20 30]
1488
1489
1490 .. code:: ipython2
1491
1492     J('10 5 [[*][/][+][-]] pam')
1493
1494
1495 .. parsed-literal::
1496
1497     10 5 [50 2.0 15 5]
1498
1499
1500 ``nullary`` ``unary`` ``binary`` ``ternary``
1501 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1502
1503 Run a quoted program enforcing
1504 `arity <https://en.wikipedia.org/wiki/Arity>`__.
1505
1506 .. code:: ipython2
1507
1508     J('1 2 3 4 5 [+] nullary')
1509
1510
1511 .. parsed-literal::
1512
1513     1 2 3 4 5 9
1514
1515
1516 .. code:: ipython2
1517
1518     J('1 2 3 4 5 [+] unary')
1519
1520
1521 .. parsed-literal::
1522
1523     1 2 3 4 9
1524
1525
1526 .. code:: ipython2
1527
1528     J('1 2 3 4 5 [+] binary')  # + has arity 2 so this is technically pointless...
1529
1530
1531 .. parsed-literal::
1532
1533     1 2 3 9
1534
1535
1536 .. code:: ipython2
1537
1538     J('1 2 3 4 5 [+] ternary')
1539
1540
1541 .. parsed-literal::
1542
1543     1 2 9
1544
1545
1546 ``step``
1547 ~~~~~~~~
1548
1549 .. code:: ipython2
1550
1551     J('[step] help')
1552
1553
1554 .. parsed-literal::
1555
1556     Run a quoted program on each item in a sequence.
1557     
1558             ... [] [Q] . step
1559          -----------------------
1560                    ... .
1561     
1562     
1563            ... [a] [Q] . step
1564         ------------------------
1565                  ... a . Q
1566     
1567     
1568        ... [a b c] [Q] . step
1569     ----------------------------------------
1570                  ... a . Q [b c] [Q] step
1571     
1572     The step combinator executes the quotation on each member of the list
1573     on top of the stack.
1574     
1575
1576
1577 .. code:: ipython2
1578
1579     V('0 [1 2 3] [+] step')
1580
1581
1582 .. parsed-literal::
1583
1584                   . 0 [1 2 3] [+] step
1585                 0 . [1 2 3] [+] step
1586         0 [1 2 3] . [+] step
1587     0 [1 2 3] [+] . step
1588           0 1 [+] . i [2 3] [+] step
1589               0 1 . + [2 3] [+] step
1590                 1 . [2 3] [+] step
1591           1 [2 3] . [+] step
1592       1 [2 3] [+] . step
1593           1 2 [+] . i [3] [+] step
1594               1 2 . + [3] [+] step
1595                 3 . [3] [+] step
1596             3 [3] . [+] step
1597         3 [3] [+] . step
1598           3 3 [+] . i
1599               3 3 . +
1600                 6 . 
1601
1602
1603 ``times``
1604 ~~~~~~~~~
1605
1606 .. code:: ipython2
1607
1608     V('3 2 1 2 [+] times')
1609
1610
1611 .. parsed-literal::
1612
1613                 . 3 2 1 2 [+] times
1614               3 . 2 1 2 [+] times
1615             3 2 . 1 2 [+] times
1616           3 2 1 . 2 [+] times
1617         3 2 1 2 . [+] times
1618     3 2 1 2 [+] . times
1619           3 2 1 . + 1 [+] times
1620             3 3 . 1 [+] times
1621           3 3 1 . [+] times
1622       3 3 1 [+] . times
1623             3 3 . +
1624               6 . 
1625
1626
1627 ``b``
1628 ~~~~~
1629
1630 .. code:: ipython2
1631
1632     J('[b] help')
1633
1634
1635 .. parsed-literal::
1636
1637     b == [i] dip i
1638     
1639     ... [P] [Q] b == ... [P] i [Q] i
1640     ... [P] [Q] b == ... P Q
1641     
1642
1643
1644 .. code:: ipython2
1645
1646     V('1 2 [3] [4] b')
1647
1648
1649 .. parsed-literal::
1650
1651                 . 1 2 [3] [4] b
1652               1 . 2 [3] [4] b
1653             1 2 . [3] [4] b
1654         1 2 [3] . [4] b
1655     1 2 [3] [4] . b
1656             1 2 . 3 4
1657           1 2 3 . 4
1658         1 2 3 4 . 
1659
1660
1661 ``while``
1662 ~~~~~~~~~
1663
1664 ::
1665
1666     [predicate] [body] while
1667
1668 .. code:: ipython2
1669
1670     J('3 [0 >] [dup --] while')
1671
1672
1673 .. parsed-literal::
1674
1675     3 2 1 0
1676
1677
1678 ``x``
1679 ~~~~~
1680
1681 .. code:: ipython2
1682
1683     J('[x] help')
1684
1685
1686 .. parsed-literal::
1687
1688     x == dup i
1689     
1690     ... [Q] x = ... [Q] dup i
1691     ... [Q] x = ... [Q] [Q] i
1692     ... [Q] x = ... [Q]  Q
1693     
1694
1695
1696 .. code:: ipython2
1697
1698     V('1 [2] [i 3] x')  # Kind of a pointless example.
1699
1700
1701 .. parsed-literal::
1702
1703                 . 1 [2] [i 3] x
1704               1 . [2] [i 3] x
1705           1 [2] . [i 3] x
1706     1 [2] [i 3] . x
1707     1 [2] [i 3] . i 3
1708           1 [2] . i 3 3
1709               1 . 2 3 3
1710             1 2 . 3 3
1711           1 2 3 . 3
1712         1 2 3 3 . 
1713
1714
1715 ``void``
1716 ========
1717
1718 Implements `**Laws of Form**
1719 *arithmetic* <https://en.wikipedia.org/wiki/Laws_of_Form#The_primary_arithmetic_.28Chapter_4.29>`__
1720 over quote-only datastructures (that is, datastructures that consist
1721 soley of containers, without strings or numbers or anything else.)
1722
1723 .. code:: ipython2
1724
1725     J('[] void')
1726
1727
1728 .. parsed-literal::
1729
1730     False
1731
1732
1733 .. code:: ipython2
1734
1735     J('[[]] void')
1736
1737
1738 .. parsed-literal::
1739
1740     True
1741
1742
1743 .. code:: ipython2
1744
1745     J('[[][[]]] void')
1746
1747
1748 .. parsed-literal::
1749
1750     True
1751
1752
1753 .. code:: ipython2
1754
1755     J('[[[]][[][]]] void')
1756
1757
1758 .. parsed-literal::
1759
1760     False
1761