tweaked arithmetic
[lambda.git] / arithmetic.mdwn
1 Here are a bunch of pre-tested operations for the untyped lambda calculus. In some cases multiple versions are offered.
2
3         ; booleans
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
15
16         ; pairs
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
20
21         ; triples
22         let make_triple = \x y z f. f x y z  in
23
24
25         ; Church numerals
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
36         ; b succ : adds b
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
41
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)
45                 ; where shift is
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
49                                         ; else
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
52                                                 (succ x))
53                                           ; base term
54                                           (K (K zero))
55                                         )  in
56         ; from Bunder/Urbanek
57         let pred = \n s z. n (\u v. v (u s)) (K z) I  in ; or
58
59                                         
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
64
65
66         ; more efficient comparisons
67         let leq = (\base build consume. \m n. n consume (m build base) get_1st (\x. false) true)
68                         ; where base is
69                         (make_pair zero I) ; supplying this pair as an arg to its 2nd term returns the pair
70                         ; and build is
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)
72                         ; and consume is
73                         (\p. p get_2nd p)  in
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)
76                         ; where base is
77                         (make_pair zero (K (make_pair one I)))
78                         ; and build is
79                         (\p. p (\x y. make_pair (succ x) (K p)))
80                         ; and consume is
81                         (\p. p get_2nd p)  in ; or
82
83
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)
86                         ; where base is
87                         (make_pair true junk)
88                         ; and build is
89                         (\p. make_pair false p)
90                         ; and consume is
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
97                         ; base is
98                         (make_pair true (K (make_pair false I)))
99                         ; and build is
100                         (\p. make_pair false (K p))
101                         ; and consume is
102                         (\p. p get_2nd p)  in
103         
104         ; -n is a fixedpoint of \x. add (add n x) x
105         ; but unfortunately Y that_function doesn't normalize
106         ; instead:
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))
110                                 ; where base is
111                                 (make_triple n false zero)
112                                 ; and build is
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
116                                 ))  in
117         ; or
118         let sub = (\base build consume. \m n. n consume (m build base) get_1st)
119                         ; where base is
120                         (make_pair zero I) ; see second defn of eq for explanation of 2nd element
121                         ; and build is
122                         (\p. p (\x y. make_pair (succ x) (K p)))
123                         ; and consume is
124                         (\p. p get_2nd p)  in
125
126         let min = \m n. sub m (sub m n) in
127         let max = \m n. add n (sub m n) in
128
129         ; (m/n) is a fixedpoint of \x. add (sub (mul n x) m) x
130         ; but unfortunately Y that_function doesn't normalize
131         ; instead:
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))
134                                 ; where base is
135                                 (make_triple m true zero)
136                                 ; and build is
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
140                                 ))  in
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))
143                 ; where base is
144                 (make_pair m true)
145                 ; and build is
146                 (\p. p (\cur go. and go (leq n cur)
147                         (make_pair (sub cur n) true) ; continue
148                         (make_pair cur false) ; enough
149                 ))  in
150
151         ; or
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) )
156                                 ; where base is
157                                 (make_triple succ (K 0) I) ; see second defn of eq for explanation of 3rd element
158                                 ; and build is
159                         (\t. make_triple I succ (K t))
160                                 ; and mtail is
161                                 (\dhead d. d (\dz mz df mf drest sel. drest dhead (sel (df dz) (mf mz))))
162         in
163         let div = \n d. divmod n d get_1st  in
164         let mod = \n d. divmod n d get_2nd  in
165
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
168
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
171         ; instead:
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))
174                 ; where base is
175                 (make_triple b true 0)
176                 ; and build is
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
180                 ))  in
181
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
186
187
188
189
190         ; version 3 lists
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)
196                                 ; where shift is
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
202         
203
204         ; version 1 lists
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
211         
212         let length = Y (\self lst. isempty lst 0 (succ (self (tail lst))))  in
213
214
215
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
220
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
223
224
225         fact Z 3  ; returns 6
226
227