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
|
/ Combinators /
/ Following Brent Kerby's 'The Theory of Concatenative Combinators' (TCC) /
/ Everything below doubles 3 /
3 ↑ + / Double 3 /
3 ⊂ ↑ + ⊃ ⍎ / Push execution token to stack, execute it /
3 ⊂ ↑ + ⊃ unit ⍎ ⍎ / Push execution token, wrap with unit, unwrap with ⍎, execute with ⍎ /
3 ⊂ ↑ ⊃ ⊂ + ⊃ cat ⍎ / Push separate execution tokens for ↑ and +, combine with cat, execute with ⍎ /
3 ⊂ + ⊃ ⊂ ↑ ⊃ ↕ cat ⍎ / Push executions for + and ↑, swap order with ↕, combine with cat, execute /
/ The examples above illustrate the definitions of the combinators /
/ The combinators also satisfy some interesting identities /
/ For example
unit ≡ ⊂⊃ cons
so this also doubles 3: /
3 ⊂ ↑ + ⊃ ⊂⊃ cons ⍎ ⍎
/ We can define a unit' which does this: /
∇ unit' ⊂⊃ cons ∇
3 ⊂ ↑ + ⊃ unit' ⍎ ⍎
/ The following also doubles three: /
3 ⊂ ↑ ⊃ ⊂ + ⊃ ⊂ ⍎ ⊃ ⊂ dip ⍎ ⊃ cons cons cons ⍎
/ This is because of the identity
cat ≡ ⊂ ⍎ ⊃ ⊂ dip ⍎ ⊃ cons cons cons
We can define a new cat' which does this:
/
⊂ dip ⍎ ⊃ ⊂ ⍎ ⊃ ∇ cat' LITERAL LITERAL cons cons cons ∇
/ The LITERALs just compile the execution tokens in the definition of cat' /
3 ⊂ ↑ ⊃ ⊂ + ⊃ cat' ⍎
/ Also cons can be written in terms of cat,
cons ≡ ⊂ unit ⊃ dip cat
giving /
⊂ unit ⊃ ∇ cons' LITERAL dip cat ∇
3 ⊂ ↑ + ⊃ ⊂⊃ cons' ⍎ ⍎
/ swap ≡ unit dip /
∇ swap' unit dip ∇
3 ⊂ + ⊃ ⊂ ↑ ⊃ swap' cat ⍎
∇ dip' ↕ unit cat ⍎ ∇
3 ⊂ + ⊃ ⊂ ↑ ⊃ unit dip' cat ⍎
/ ⍎ ≡ ↑ dip ↓ / ∇ ⍎' ↑ dip ↓ ∇
/ ⍎ ≡ ⊂⊃ unit dip dip ↓ / ∇ ⍎'' ⊂⊃ unit dip dip ↓ ∇
/ ⍎ ≡ ⊂⊃ unit dip dip dip / ∇ ⍎''' ⊂⊃ unit dip dip dip ∇
3 ⊂ ↑ + ⊃ ⍎'
3 ⊂ ↑ + ⊃ ⍎''
3 ⊂ ↑ + ⊃ ⍎'''
/ Lambdas /
/ λ (written \ in TCC) can be implemented as a Forth defining word /
∇ λ ◁ , ▷ @ ⍎ ∇
/ dip ≡ λ a λ b a ⊂ b ⊃ /
3 ⊂ + ⊃ ⊂ ↑ ⊃ unit λ a λ b a ⊂ b ⊃ cat ⍎
/ End of scope ($ in TCC) can be done with HIDE a /
∇ $ HIDE ∇
/ An Abstraction Algorithm /
/ TCC gives an algorithm to eliminate λ's from combinators. Applying it /
/ gives the equivalence of /
/ λ a b a ⊂ c a ⊃ ≡ ⊂ b ⊃ dip ↑ ⊂ ⍎ ⊃ dip ⊂ c ⊃ ⊂ dip ⍎ ⊃ cons cons /
/ ( lambda ) ( no lambda ) /
/ For example, letting b be ↑ and c be + and trying each of the above /
/ combinators on a stack with 2 ⊂ 1+ ⊃ on ( adding an ⍎ at the end of /
/ both to test the effect of the ⊂ c a ⊃ left on top of the stack) /
⊂ ↑ ⊃ ⊂ + ⊃ λ c λ b
2 ⊂ 1+ ⊃ λ a b a ⊂ c a ⊃ ⍎
2 ⊂ 1+ ⊃ ⊂ b ⊃ dip ↑ ⊂ ⍎ ⊃ dip ⊂ c ⊃ ⊂ dip ⍎ ⊃ cons cons ⍎
/ Both give 6 since in this case both lines are equivalent to 2 ↑ 1+ + 1+ /
/ The sip Combinator /
/ ⊂ b ⊃ ⊂ a ⊃ sip ≡ ⊂ b ⊃ a ⊂ b ⊃ /
/ ↑ ≡ ⊂⊃ sip /
3 ⊂⊃ sip +
/ dip ≡ λ a ⊂ ↓ a ⊃ sip /
3 ⊂ + ⊃ ⊂ ↑ ⊃ unit λ a ⊂ ↓ a ⊃ sip cat ⍎
/ dip ≡ ⊂ ↓ ↓ ⊃ ⊂ sip ⍎ ⊃ cons cons sip /
3 ⊂ + ⊃ ⊂ ↑ ⊃ unit ⊂ ↓ ↓ ⊃ ⊂ sip ⍎ ⊃ cons cons sip cat ⍎
/ Applicative Combinators /
∇ w ['] ↑ dip ⍎ ∇
∇ k ['] ↓ dip ⍎ ∇
∇ b ['] cons dip ⍎ ∇
∇ c ['] ↕ dip ⍎ ∇
∇ s >R ⊤ ↕ cons ↕ R> ⍎ ∇
/ ↑ ≡ ⊂⊃ w /
/ ↓ ≡ ⊂⊃ k /
/ cons ≡ ⊂⊃ b /
/ ↕ ≡ ⊂⊃ c /
3 ⊂⊃ w +
3 ↑ ↑ ⊂⊃ k +
3 ⊂ ↑ + ⊃ ⊂⊃ ⊂⊃ b ⍎ ⍎
3 ⊂ + ⊃ ⊂ ↑ ⊃ ⊂⊃ c cat ⍎
/ b ≡ ⊂ k ⊃ ⊂ s ⊃ ⊂ k ⊃ cons s /
⊂ k ⊃ ⊂ s ⊃ ⊂ k ⊃ ∇ b' LITERAL LITERAL LITERAL cons s ∇
3 ⊂ ↑ + ⊃ ⊂⊃ ⊂⊃ b' ⍎ ⍎
|