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
|
/**
* Meta
*/
print = console.log
y = f => x => f(y(f))(x)
k = t => f => t
ki = t => f => f
/**
* Logic
*/
// true/false Church booleans
tru = t => f => t
fls = t => f => f
// evaluate
evalBool = bool => bool("true")("false")
print(evalBool(tru)) // "true"
print(evalBool(fls)) // "false"
// negate (c combinator)
negate = bool => t => f => bool(f)(t)
print(evalBool(negate(tru))) // "false"
print(evalBool(negate(fls))) // "true"
// logical and
and = a => b => b(a)(b)
// how?
// - if b is true, then b is re-bound to a (first arg, see lines 1,3)
// - if b is false, then b is re-bound to b (second arg, always false, see lines 2,4)
print(evalBool(and(tru)(tru))) // "true"
print(evalBool(and(tru)(fls))) // "false"
print(evalBool(and(fls)(tru))) // "false"
print(evalBool(and(fls)(fls))) // "false"
/**
* Church Pairs
*/
// construction
cons = a => b => s => s(a)(b)
// example
examplePair = cons("a")("b")
// selectors
car = pair => pair(a => b => a)
cdr = pair => pair(a => b => b)
print(car(examplePair)) // "a"
print(cdr(examplePair)) // "b"
/**
* Church Lists
*/
// ["a", "b", "c"] => pair "a" (pair "b" (pair "c" nil))
// => s1 => (s1 "a" (s2 => (s2 "b" (s3 => (s3 "c" nil)))))
// end of list
nil = head => tail => tail
exampleList = cons("a")(cons("b")(cons("c")(nil)))
// true if list is empty
isNil = list => list(head => tail => rest => fls)(tru)
// first argument gets substituted into first selector (s1)
// just ignore all arguments and return false
// "rest" is the right argument "tru"
// nil ignores first argument, second one is just returned as is ("true")
print(evalBool(isNil(nil))) // "true"
print(evalBool(isNil(exampleList))) // "false"
// length = y(rec => n => list => isNil(list)(n)(rec(n + 1)(cdr(list))))(0)
// thunked due to JS' strictness..
length = y(rec => n => list => isNil(list)(() => n)(() => rec(n + 1)(cdr(list))()))(0)
print(length(exampleList)())
/**
* Scott Lists
*/
exampleScottList = end => s1 => s2("a")(s2 => s2("b")(end))
/**
* Parigot Lists
*/
exampleParigotList = s1 => end1 => s1("a")(s2 => end2 => s2("b")(s3 => end3 => end3))
/**
* Product types
*/
// data Friends = Friends { best :: String, friendly :: String, weird :: String }
Friends = best => friendly => weird => s => s(best)(friendly)(weird)
best = friends => friends(best => _ => _ => best)
friendly = friends => friends(_ => friendly => _ => friendly)
weird = friends => friends(_ => _ => weird => weird)
friends = Friends("Alice")("Bob")("Carol")
print(best(friends))
/**
* Sum types, tagged unions
*/
// data Tree = Leaf Int | Node Tree Tree
Leaf = n => leaf => node => leaf(n)
Node = a => b => leaf => node => node(a)(b)
nodeLeft = node => node(_ => _)(a => b => a)
nodeRight = node => node(_ => _)(a => b => b)
leafValue = leaf => leaf(n => n)(_ => _)
isLeaf = tree => tree(n => tru)(a => b => fls)
isNode = tree => tree(n => fls)(a => b => tru)
exampleTree = Node(Leaf(1))(Node(Leaf(2))(Leaf(3)))
print(evalBool(isNode(exampleTree)))
print(evalBool(isLeaf(nodeLeft(exampleTree))))
print(leafValue(nodeLeft(exampleTree)))
print(leafValue(nodeRight(nodeRight(exampleTree))))
/**
* Rose, Balanced, binary, finger, etc. trees
*/
// skipped, see bruijn std
// for example: AVL - can even be used for sets and hashmaps!
/**
* Strings, chars
*/
// just a list of numbers/bits/etc.
/**
* Church numerals
*/
churchZero = s => z => z
churchSucc = n => s => z => s(n(s)(z))
churchPred = n => f => x => n(g => h => h(g(f)))(u => x)(u => u)
churchIsZero = n => n(z => fls)(tru)
print(evalBool(churchIsZero(churchZero))) // true
print(evalBool(churchIsZero(churchSucc(churchZero)))) // false
print(evalBool(churchIsZero(churchPred(churchSucc(churchZero))))) // true
/**
* Scott numerals
*/
scottZero = z => s => z
scottSucc = n => z => s => s(n)
scottPred = n => n(scottZero)(x => x)
scottIsZero = n => n(tru)(x => fls)
print(evalBool(scottIsZero(scottZero))) // true
print(evalBool(scottIsZero(scottSucc(scottZero)))) // false
print(evalBool(scottIsZero(scottPred(scottSucc(scottZero))))) // true
/**
* Parigot numerals
*/
parigotZero = n => n
parigotSucc = n => a => b => b(n(a))
parigotPred = n => a => n(a(x => x))
/**
* Wadsworth numerals
*/
wadsworthZero = n => n(u => k)
wadsworthSucc = n => a => b => n(c => a(b(c))(b))
wadsworthPred = n => a => n(b => k(a(b)))(x => x)
wadsworthIsZero = n => n(x => x)(k(k(ki)))
print(evalBool(wadsworthIsZero(wadsworthZero))) // true
print(evalBool(wadsworthIsZero(wadsworthSucc(wadsworthZero)))) // false
print(evalBool(wadsworthIsZero(wadsworthPred(wadsworthSucc(wadsworthZero))))) // true
/**
* de Bruijn numerals
*/
bruijnZero = n => n
bruijnSucc = n => a => b => n(a)
bruijnPred = n => a => n(a)(a)
bruijnMult = a => b => a(b)
/**
* n-ary numerals
*/
exampleMogensenBinary = end => b1 => b0 => b1(b1(end))
exampleMogensenTernary = end => tn => tp => t0 => t0(tp(end))
/**
* rational, real, complex
*/
// skipped, see bruijn std
/**
* Maybe monad
*/
Nothing = empty => full => empty
Just = v => empty => full => full(v)
isNothing = m => m(tru)(u => fls)
isJust = m => m(fls)(u => tru)
map => f => m => m(nothing)(v => just(f(v)))
pure = Just
bind = mx => f => mx(mx)(f)
// ------------^^ ^-----------------
// case Nothing case Just (apply)
evalMaybe = maybe => maybe("Nothing")(v => "Just " + v)
print(evalMaybe(bind(Nothing)(n => pure(n + 1))))
print(evalMaybe(bind(Just(42))(n => pure(n + 1))))
/**
* Either monad
*/
// data Either a b = Left a | Right b
Left = a => left => right => left(a)
Right = b => left => right => right(b)
// instance Monad
pure = Right
bind = mx => f => mx(Left)(f)
// ---------^^^^ ^------------------
// case Left case Right (apply)
evalEither = either => either(a => "Left " + a)(b => "Right " + b)
print(evalEither(bind(Left(42))(n => pure(n + 1))))
print(evalEither(bind(Right(42))(n => pure(n + 1))))
/**
* Mogensen-Scott meta
*/
// enc[x] = sym => app => lam => sym(x)
// enc[f(x)] = sym => app => lam => app(enc[f])(enc[x])
// enc[x => m] = sym => app => lam => lam(x => enc[m])
evalMeta = term => term
(x => x)
(f => x => eval(f)(eval(x)))
(m => x => eval(m(x)))
/**
* Bruijn-Church meta
*/
// enc[i] = idx => app => lam => church[idx]
// enc[f(x)] = idx => app => lam => app(enc[f])(enc[x])
// enc[b] = idx => app => lam => lam(enc[b])
/**
* Lambda Screen
*/
// a screen is (s => s(tl)(tr)(bl)(br)), where tl,tr,bl,br in [screen, pixel]
// pixel is (w => b => w) || (w => b => b)
|