1 Here are a bunch of pre-tested operations for the untyped lambda calculus. In some cases multiple versions are offered.
4 let true = \y n. y in ; aka K
5 let false = \y n. n in ; aka K I
6 let and = \p q. p q false in ; or
7 let and = \p q. p q p in ; aka S C I
8 let or = \p q. p true q in ; or
9 let or = \p q. p p q in ; aka M
10 let not = \p. p false true in ; or
11 let not = \p y n. p n y in ; aka C
12 let xor = \p q. p (not q) q in
13 let iff = \p q. not (xor p q) in ; or
14 let iff = \p q. p q (not q) in
17 let make_pair = \x y f. f x y in
18 let get_1st = \x y. x in ; aka true
19 let get_2nd = \x y. y in ; aka false
22 let make_triple = \x y z f. f x y z in
27 let empty = \f z. z in
28 let make_list = \h t f z. f h (t f z) in
29 let isempty = \lst. lst (\h sofar. false) true in
30 let head = \lst. lst (\h sofar. h) junk in
31 let tail = \lst. (\shift lst. lst shift (make_pair empty junk) get_2nd)
33 (\h p. p (\t y. make_pair (make-list h t) t)) in
34 let length = \lst. lst (\h sofar. succ sofar) zero in
35 let map = \f lst. lst (\h sofar. make_list (f h) sofar) empty in
36 let filter = \f lst. lst (\h sofar. f h (make_list h sofar) sofar) empty in ; or
37 let filter = \f lst. lst (\h. f h (make_list h) I) empty in
42 let empty = make_pair true junk in
43 let make_list = \h t. make_pair false (make_pair h t) in
44 let isempty = \lst. lst get_1st in
45 let head = \lst. isempty lst error (lst get_2nd get_1st) in
46 let tail_empty = empty in
47 let tail = \lst. isempty lst tail_empty (lst get_2nd get_2nd) in
51 ; Church numerals: easy operations
53 let zero = \s z. z in ; aka false
54 let one = \s z. s z in ; aka I
55 let succ = \n s z. s (n s z) in
56 ; for any Church numeral n > zero : n (K y) z ~~> y
57 let iszero = \n. n (\x. false) true in
58 let add = \m n. m succ n in ; or
59 let add = \m n s z. m s (n s z) in
60 let mul = \m n. m (\z. add n z) zero in ; or
61 let mul = \m n s. m (n s) in
62 let pow = \b exp. exp (mul b) one in ; or
64 ; b (b succ) ; adds b b times, ie adds b^2
65 ; b (b (b succ)) ; adds b^2 b times, ie adds b^3
66 ; exp b succ ; adds b^exp
67 let pow = \b exp s z. exp b s z in
70 ; three strategies for predecessor
71 let pred_zero = zero in
72 let pred = (\shift n. n shift (make_pair zero pred_zero) get_2nd)
74 (\p. p (\x y. make_pair (succ x) x)) in ; or
75 ; from Oleg; observe that for any Church numeral n: n I ~~> I
76 let pred = \n. iszero n zero
78 (n (\x. x I ; when x is the base term, this will be K zero
79 ; when x is a Church numeral, it will be I
85 let pred = \n s z. n (\u v. v (u s)) (K z) I in ; or
88 ; inefficient but simple comparisons
89 let leq = \m n. iszero (n pred m) in
90 let lt = \m n. not (leq n m) in
91 let eq = \m n. and (leq m n) (leq n m) in ; or
94 ; more efficient comparisons, Oleg's gt provided some simplifications
95 let leq = (\base build consume. \m n. n consume (m build base) get_1st)
99 (\p. make_pair false p)
101 (\p. p get_1st p (p get_2nd)) in
102 let lt = \m n. not (leq n m) in
103 let eq = (\base build consume. \m n. n consume (m build base) get_1st)
104 ; 2nd element of a pair will now be of the form (K sthg) or I
105 ; we supply the pair being consumed itself as an argument
106 ; getting back either sthg or the pair we just consumed
108 (make_pair true (K (make_pair false I)))
110 (\p. make_pair false (K p))
115 ; -n is a fixedpoint of \x. add (add n x) x
116 ; but unfortunately Y that_function doesn't normalize
118 let sub = \m n. n pred m in ; or
119 ; how many times we can succ n until m <= result
120 let sub = \m n. (\base build. m build base (\cur fin sofar. sofar))
122 (make_triple n false zero)
124 (\t. t (\cur fin sofar. or fin (leq m cur)
125 (make_triple cur true sofar) ; enough
126 (make_triple (succ cur) false (succ sofar)) ; continue
129 let sub = (\base build consume. \m n. n consume (m build base) get_1st)
131 (make_pair zero I) ; see second defn of eq for explanation of 2nd element
133 (\p. p (\x y. make_pair (succ x) (K p)))
138 let min = \m n. sub m (sub m n) in
139 let max = \m n. add n (sub m n) in
142 ; (m/n) is a fixedpoint of \x. add (sub (mul n x) m) x
143 ; but unfortunately Y that_function doesn't normalize
145 ; how many times we can sub n from m while n <= result
146 let div = \m n. (\base build. m build base (\cur go sofar. sofar))
148 (make_triple m true zero)
150 (\t. t (\cur go sofar. and go (leq n cur)
151 (make_triple (sub cur n) true (succ sofar)) ; continue
152 (make_triple cur false sofar) ; enough
154 ; what's left after sub n from m while n <= result
155 let mod = \m n. (\base build. m build base (\cur go. cur))
159 (\p. p (\cur go. and go (leq n cur)
160 (make_pair (sub cur n) true) ; continue
161 (make_pair cur false) ; enough
165 let divmod = (\base build mtail. \m n.
166 (\dhead. m (mtail dhead) (\sel. dhead (sel 0 0)))
167 (n build base (\x y z. z junk))
168 (\t u x y z. make_pair t u) )
170 (make_triple succ (K 0) I) ; see second defn of eq for explanation of 3rd element
172 (\t. make_triple I succ (K t))
174 (\dhead d. d (\dz mz df mf drest sel. drest dhead (sel (df dz) (mf mz))))
176 let div = \n d. divmod n d get_1st in
177 let mod = \n d. divmod n d get_2nd in
180 ; sqrt n is a fixedpoint of \x. div (div (add n (mul x x)) 2) x
181 ; but unfortunately Y that_function doesn't normalize
184 ; (log base b of m) is a fixedpoint of \x. add (sub (pow b x) m) x
185 ; but unfortunately Y that_function doesn't normalize
187 ; how many times we can mul b by b while result <= m
188 let log = \m b. (\base build. m build base (\cur go sofar. sofar))
190 (make_triple b true 0)
192 (\t. t (\cur go sofar. and go (leq cur m)
193 (make_triple (mul cur b) true (succ sofar)) ; continue
194 (make_triple cur false sofar) ; enough
198 ; Curry's fixed point combinator
199 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
200 ; Turing's fixed point combinator
201 let Z = (\u f. f (u u f)) (\u f. f (u u f)) in
204 ; length for version 1 lists
205 let length = Y (\self lst. isempty lst 0 (succ (self (tail lst)))) in
208 ; numhelper 0 f z ~~> z
209 ; when n > 0: numhelper n f z ~~> f (pred n)
210 ; compare Bunder/Urbanek pred
211 let numhelper = \n. n (\u v. v (u succ)) (K 0) (\p f z. f p) in
213 ; accepts fixed point combinator as a parameter, so you can use different ones
214 let fact = \y. y (\self n. numhelper n (\p. mul n (self p)) 1) in
222 ; my original efficient comparisons
223 let leq = (\base build consume. \m n. n consume (m build base) get_1st (\x. false) true)
225 (make_pair zero I) ; supplying this pair as an arg to its 2nd term returns the pair
227 (\p. p (\x y. make_pair (succ x) (K p))) ; supplying the made pair as an arg to its 2nd term returns p (the previous pair)
230 let lt = \m n. not (leq n m) in
231 let eq = (\base build consume. \m n. n consume (m build base) true (\x. false) true)
233 (make_pair zero (K (make_pair one I)))
235 (\p. p (\x y. make_pair (succ x) (K p)))
237 (\p. p get_2nd p) in ; or