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
26 let zero = \s z. z in ; aka false
27 let one = \s z. s z in ; aka I
28 let succ = \n s z. s (n s z) in
29 ; for any Church numeral n > zero : n (K y) z ~~> y
30 let iszero = \n. n (\x. false) true in
31 let add = \m n. m succ n in ; or
32 let add = \m n s z. m s (n s z) in
33 let mul = \m n. m (\z. add n z) zero in ; or
34 let mul = \m n s. m (n s) in
35 let pow = \b exp. exp (mul b) one in ; or
37 ; b (b succ) ; adds b b times, ie adds b^2
38 ; b (b (b succ)) ; adds b^2 b times, ie adds b^3
39 ; exp b succ ; adds b^exp
40 let pow = \b exp s z. exp b s z in
42 ; three strategies for predecessor
43 let pred_zero = zero in
44 let pred = (\shift n. n shift (make_pair zero pred_zero) get_2nd)
46 (\p. p (\x y. make_pair (succ x) x)) in ; or
47 ; from Oleg; observe that for any Church numeral n: n I ~~> I
48 let pred = \n. iszero n zero
50 (n (\x. x I ; when x is the base term, this will be K zero
51 ; when x is a Church numeral, it will be I
57 let pred = \n s z. n (\u v. v (u s)) (K z) I in ; or
60 ; inefficient but simple comparisons
61 let leq = \m n. iszero (n pred m) in
62 let lt = \m n. not (leq n m) in
63 let eq = \m n. and (leq m n) (leq n m) in ; or
66 ; more efficient comparisons
67 let leq = (\base build consume. \m n. n consume (m build base) get_1st (\x. false) true)
69 (make_pair zero I) ; supplying this pair as an arg to its 2nd term returns the pair
71 (\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)
74 let lt = \m n. not (leq n m) in
75 let eq = (\base build consume. \m n. n consume (m build base) true (\x. false) true)
77 (make_pair zero (K (make_pair one I)))
79 (\p. p (\x y. make_pair (succ x) (K p)))
81 (\p. p get_2nd p) in ; or
84 ; more efficient comparisons, Oleg's gt provided some simplification
85 let leq = (\base build consume. \m n. n consume (m build base) get_1st)
89 (\p. make_pair false p)
91 (\p. p get_1st p (p get_2nd)) in
92 let lt = \m n. not (leq n m) in
93 let eq = (\base build consume. \m n. n consume (m build base) get_1st)
94 ; 2nd element of a pair will now be of the form (K sthg) or I
95 ; we supply the pair being consumed itself as an argument
96 ; getting back either sthg or the pair we just consumed
98 (make_pair true (K (make_pair false I)))
100 (\p. make_pair false (K p))
104 ; -n is a fixedpoint of \x. add (add n x) x
105 ; but unfortunately Y that_function doesn't normalize
107 let sub = \m n. n pred m in ; or
108 ; how many times we can succ n until m <= result
109 let sub = \m n. (\base build. m build base (\cur fin sofar. sofar))
111 (make_triple n false zero)
113 (\t. t (\cur fin sofar. or fin (leq m cur)
114 (make_triple cur true sofar) ; enough
115 (make_triple (succ cur) false (succ sofar)) ; continue
118 let sub = (\base build consume. \m n. n consume (m build base) get_1st)
120 (make_pair zero I) ; see second defn of eq for explanation of 2nd element
122 (\p. p (\x y. make_pair (succ x) (K p)))
126 let min = \m n. sub m (sub m n) in
127 let max = \m n. add n (sub m n) in
129 ; (m/n) is a fixedpoint of \x. add (sub (mul n x) m) x
130 ; but unfortunately Y that_function doesn't normalize
132 ; how many times we can sub n from m while n <= result
133 let div = \m n. (\base build. m build base (\cur go sofar. sofar))
135 (make_triple m true zero)
137 (\t. t (\cur go sofar. and go (leq n cur)
138 (make_triple (sub cur n) true (succ sofar)) ; continue
139 (make_triple cur false sofar) ; enough
141 ; what's left after sub n from m while n <= result
142 let mod = \m n. (\base build. m build base (\cur go. cur))
146 (\p. p (\cur go. and go (leq n cur)
147 (make_pair (sub cur n) true) ; continue
148 (make_pair cur false) ; enough
152 let divmod = (\base build mtail. \m n.
153 (\dhead. m (mtail dhead) (\sel. dhead (sel 0 0)))
154 (n build base (\x y z. z junk))
155 (\t u x y z. make_pair t u) )
157 (make_triple succ (K 0) I) ; see second defn of eq for explanation of 3rd element
159 (\t. make_triple I succ (K t))
161 (\dhead d. d (\dz mz df mf drest sel. drest dhead (sel (df dz) (mf mz))))
163 let div = \n d. divmod n d get_1st in
164 let mod = \n d. divmod n d get_2nd in
166 ; sqrt n is a fixedpoint of \x. div (div (add n (mul x x)) 2) x
167 ; but unfortunately Y that_function doesn't normalize
169 ; (log base b of m) is a fixedpoint of \x. add (sub (pow b x) m) x
170 ; but unfortunately Y that_function doesn't normalize
172 ; how many times we can mul b by b while result <= m
173 let log = \m b. (\base build. m build base (\cur go sofar. sofar))
175 (make_triple b true 0)
177 (\t. t (\cur go sofar. and go (leq cur m)
178 (make_triple (mul cur b) true (succ sofar)) ; continue
179 (make_triple cur false sofar) ; enough
182 ; Curry's fixed point combinator
183 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
184 ; Turing's fixed point combinator
185 let Z = (\u f. f (u u f)) (\u f. f (u u f)) in
191 let empty = \f z. z in
192 let make_list = \h t f z. f h (t f z) in
193 let isempty = \lst. lst (\h sofar. false) true in
194 let head = \lst. lst (\h sofar. h) junk in
195 let tail = \lst. (\shift lst. lst shift (make_pair empty junk) get_2nd)
197 (\h p. p (\t y. make_pair (make-list h t) t)) in
198 let length = \lst. lst (\h sofar. succ sofar) zero in
199 let map = \f lst. lst (\h sofar. make_list (f h) sofar) empty in
200 let filter = \f lst. lst (\h sofar. f h (make_list h sofar) sofar) empty in ; or
201 let filter = \f lst. lst (\h. f h (make_list h) I) empty in
205 let empty = make_pair true junk in
206 let make_list = \h t. make_pair false (make_pair h t) in
207 let isempty = \lst. lst get_1st in
208 let head = \lst. isempty lst error (lst get_2nd get_1st) in
209 let tail_empty = empty in
210 let tail = \lst. isempty lst tail_empty (lst get_2nd get_2nd) in
212 let length = Y (\self lst. isempty lst 0 (succ (self (tail lst)))) in
216 ; numhelper 0 f z ~~> z
217 ; when n > 0: numhelper n f z ~~> f (pred n)
218 ; compare Bunder/Urbanek pred
219 let numhelper = \n. n (\u v. v (u succ)) (K 0) (\p f z. f p) in
221 ; accepts fixed point combinator as a parameter, so you can use different ones
222 let fact = \y. y (\self n. numhelper n (\p. mul n (self p)) 1) in