e948f0b48123694b7b1c1f328ff91520a374f4bd
[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         ; version 3 lists
26
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)
32                                 ; where shift is
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
38         
39
40         ; version 1 lists
41
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
48         
49
50
51         ; Church numerals: easy operations
52
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
63         ; b succ : adds b
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
68
69
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)
73                 ; where shift is
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
77                                         ; else
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
80                                                 (succ x))
81                                           ; base term
82                                           (K (K zero))
83                                         )  in
84         ; from Bunder/Urbanek
85         let pred = \n s z. n (\u v. v (u s)) (K z) I  in ; or
86
87                                         
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
92
93
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)
96                         ; where base is
97                         (make_pair true junk)
98                         ; and build is
99                         (\p. make_pair false p)
100                         ; and consume is
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
107                         ; base is
108                         (make_pair true (K (make_pair false I)))
109                         ; and build is
110                         (\p. make_pair false (K p))
111                         ; and consume is
112                         (\p. p get_2nd p)  in
113         
114
115         ; -n is a fixedpoint of \x. add (add n x) x
116         ; but unfortunately Y that_function doesn't normalize
117         ; instead:
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))
121                                 ; where base is
122                                 (make_triple n false zero)
123                                 ; and build is
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
127                                 ))  in
128         ; or
129         let sub = (\base build consume. \m n. n consume (m build base) get_1st)
130                         ; where base is
131                         (make_pair zero I) ; see second defn of eq for explanation of 2nd element
132                         ; and build is
133                         (\p. p (\x y. make_pair (succ x) (K p)))
134                         ; and consume is
135                         (\p. p get_2nd p)  in
136
137
138         let min = \m n. sub m (sub m n) in
139         let max = \m n. add n (sub m n) in
140
141
142         ; (m/n) is a fixedpoint of \x. add (sub (mul n x) m) x
143         ; but unfortunately Y that_function doesn't normalize
144         ; instead:
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))
147                                 ; where base is
148                                 (make_triple m true zero)
149                                 ; and build is
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
153                                 ))  in
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))
156                 ; where base is
157                 (make_pair m true)
158                 ; and build is
159                 (\p. p (\cur go. and go (leq n cur)
160                         (make_pair (sub cur n) true) ; continue
161                         (make_pair cur false) ; enough
162                 ))  in
163
164         ; or
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) )
169                                 ; where base is
170                                 (make_triple succ (K 0) I) ; see second defn of eq for explanation of 3rd element
171                                 ; and build is
172                         (\t. make_triple I succ (K t))
173                                 ; and mtail is
174                                 (\dhead d. d (\dz mz df mf drest sel. drest dhead (sel (df dz) (mf mz))))
175         in
176         let div = \n d. divmod n d get_1st  in
177         let mod = \n d. divmod n d get_2nd  in
178
179
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
182
183
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
186         ; instead:
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))
189                 ; where base is
190                 (make_triple b true 0)
191                 ; and build is
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
195                 ))  in
196
197
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
202
203
204         ; length for version 1 lists
205         let length = Y (\self lst. isempty lst 0 (succ (self (tail lst))))  in
206
207
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
212
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
215
216
217
218         fact Z 3  ; returns 6
219
220
221 <!--
222         ; my original efficient comparisons
223         let leq = (\base build consume. \m n. n consume (m build base) get_1st (\x. false) true)
224                         ; where base is
225                         (make_pair zero I) ; supplying this pair as an arg to its 2nd term returns the pair
226                         ; and build is
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)
228                         ; and consume is
229                         (\p. p get_2nd p)  in
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)
232                         ; where base is
233                         (make_pair zero (K (make_pair one I)))
234                         ; and build is
235                         (\p. p (\x y. make_pair (succ x) (K p)))
236                         ; and consume is
237                         (\p. p get_2nd p)  in ; or
238 -->