bc531ecddac814bf05bd763e06c24a01b7f04d24
[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: basic operations
26
27         let zero = \s z. z  in ; aka false
28         let one = \s z. s z  in ; aka I
29         let succ = \n s z. s (n s z)  in
30         ; for any Church numeral n > zero : n (K y) z ~~> y
31         let iszero = \n. n (\x. false) true  in
32
33
34         ; version 3 lists
35
36         let empty = \f z. z  in
37         let make_list = \h t f z. f h (t f z)  in
38         let isempty = \lst. lst (\h sofar. false) true  in
39         let head = \lst. lst (\h sofar. h) junk  in
40         let tail = \lst. (\shift lst. lst shift (make_pair empty junk) get_2nd)
41                                 ; where shift is
42                                 (\h p. p (\t y. make_pair (make_list h t) t))  in
43         let length = \lst. lst (\h sofar. succ sofar) 0  in
44         let map = \f lst. lst (\h sofar. make_list (f h) sofar) empty  in
45         let filter = \f lst. lst (\h sofar. f h (make_list h sofar) sofar) empty  in ; or
46         let filter = \f lst. lst (\h. f h (make_list h) I) empty  in
47
48         ; append list2 to list1 with: list1 make_list list2
49         let singleton = \x f z. f x z  in
50         let reverse = \lst. lst (\h sofar. sofar make_list (singleton h)) empty  in
51         let zip = \left right. left (\h sofar. sofar (\x y. isempty y
52                                                                                 sofar
53                                                                                 (make_pair (make_list (\u v. head y (u v) h) x) (tail y))
54                                                                         )
55                                                                         (make_pair empty (map right (\h u v. u v h)))
56                                                                 )
57                                                 (\x y. reverse x)  in
58         let all = \f lst. lst (\h sofar. and sofar (f h)) true  in
59         let any = \f lst. lst (\h sofar. or sofar (f h)) false  in
60
61
62         ; version 1 lists
63
64         let empty = make_pair true junk  in
65         let make_list = \h t. make_pair false (make_pair h t)  in
66         let isempty = \lst. lst get_1st  in
67         let head = \lst. isempty lst err (lst get_2nd get_1st)  in
68         let tail_empty = empty  in
69         let tail = \lst. isempty lst tail_empty (lst get_2nd get_2nd)  in
70         
71
72         ; more math with Church numerals
73
74         let add = \m n. m succ n  in ; or
75         let add = \m n s z. m s (n s z)  in
76         let mul = \m n. m (\z. add n z) zero  in ; or
77         let mul = \m n s. m (n s)  in
78         let pow = \b exp. exp (mul b) one  in ; or
79         ; b succ : adds b
80         ; b (b succ) ; adds b b times, ie adds b^2
81         ; b (b (b succ)) ; adds b^2 b times, ie adds b^3
82         ; exp b succ ; adds b^exp
83         let pow = \b exp s z. exp b s z  in
84
85
86         ; three strategies for predecessor
87         let pred_zero = zero  in
88         let pred = (\shift n. n shift (make_pair zero pred_zero) get_2nd)
89                 ; where shift is
90                 (\p. p (\x y. make_pair (succ x) x))  in ; or
91         ; from Oleg; observe that for any Church numeral n: n I ~~> I
92         let pred = \n. iszero n zero
93                                         ; else
94                                         (n (\x. x I ; when x is the base term, this will be K zero
95                                                           ; when x is a Church numeral, it will be I
96                                                 (succ x))
97                                           ; base term
98                                           (K (K zero))
99                                         )  in
100         ; from Bunder/Urbanek
101         let pred = \n s z. n (\u v. v (u s)) (K z) I  in ; or
102
103                                         
104         ; inefficient but simple comparisons
105         let leq = \m n. iszero (n pred m)  in
106         let lt = \m n. not (leq n m)  in
107         let eq = \m n. and (leq m n) (leq n m)  in ; or
108
109
110         ; more efficient comparisons, Oleg's gt provided some simplifications
111         let leq = (\base build consume. \m n. n consume (m build base) get_1st)
112                         ; where base is
113                         (make_pair true junk)
114                         ; and build is
115                         (\p. make_pair false p)
116                         ; and consume is
117                         (\p. p get_1st p (p get_2nd))  in
118         let lt = \m n. not (leq n m)  in
119         let eq = (\base build consume. \m n. n consume (m build base) get_1st)
120                         ; 2nd element of a pair will now be of the form (K sthg) or I
121                         ; we supply the pair being consumed itself as an argument
122                         ; getting back either sthg or the pair we just consumed
123                         ; base is
124                         (make_pair true (K (make_pair false I)))
125                         ; and build is
126                         (\p. make_pair false (K p))
127                         ; and consume is
128                         (\p. p get_2nd p)  in
129         
130
131         ; -n is a fixedpoint of \x. add (add n x) x
132         ; but unfortunately Y that_function doesn't normalize
133         ; instead:
134         let sub = \m n. n pred m  in ; or
135         ; how many times we can succ n until m <= result
136         let sub = \m n. (\base build. m build base (\cur fin sofar. sofar))
137                                 ; where base is
138                                 (make_triple n false zero)
139                                 ; and build is
140                                 (\t. t (\cur fin sofar. or fin (leq m cur)
141                                                 (make_triple cur true sofar) ; enough
142                                                 (make_triple (succ cur) false (succ sofar)) ; continue
143                                 ))  in
144         ; or
145         let sub = (\base build consume. \m n. n consume (m build base) get_1st)
146                         ; where base is
147                         (make_pair zero I) ; see second defn of eq for explanation of 2nd element
148                         ; and build is
149                         (\p. p (\x y. make_pair (succ x) (K p)))
150                         ; and consume is
151                         (\p. p get_2nd p)  in
152
153
154         let min = \m n. sub m (sub m n) in
155         let max = \m n. add n (sub m n) in
156
157
158         ; (m/n) is a fixedpoint of \x. add (sub (mul n x) m) x
159         ; but unfortunately Y that_function doesn't normalize
160         ; instead:
161         ; how many times we can sub n from m while n <= result
162         let div = \m n. (\base build. m build base (\cur go sofar. sofar))
163                                 ; where base is
164                                 (make_triple m true zero)
165                                 ; and build is
166                                 (\t. t (\cur go sofar. and go (leq n cur)
167                                                 (make_triple (sub cur n) true (succ sofar)) ; continue
168                                                 (make_triple cur false sofar) ; enough
169                                 ))  in
170     ; what's left after sub n from m while n <= result
171     let mod = \m n. (\base build. m build base (\cur go. cur))
172                 ; where base is
173                 (make_pair m true)
174                 ; and build is
175                 (\p. p (\cur go. and go (leq n cur)
176                         (make_pair (sub cur n) true) ; continue
177                         (make_pair cur false) ; enough
178                 ))  in
179
180         ; or
181         let divmod = (\base build mtail. \m n.
182                                         (\dhead. m (mtail dhead) (\sel. dhead (sel 0 0)))
183                                                         (n build base (\x y z. z junk))
184                                                         (\t u x y z. make_pair t u) )
185                                 ; where base is
186                                 (make_triple succ (K 0) I) ; see second defn of eq for explanation of 3rd element
187                                 ; and build is
188                         (\t. make_triple I succ (K t))
189                                 ; and mtail is
190                                 (\dhead d. d (\dz mz df mf drest sel. drest dhead (sel (df dz) (mf mz))))
191         in
192         let div = \n d. divmod n d get_1st  in
193         let mod = \n d. divmod n d get_2nd  in
194
195
196         ; sqrt n is a fixedpoint of \x. div (div (add n (mul x x)) 2) x
197         ; but unfortunately Y that_function doesn't normalize
198
199
200         ; (log base b of m) is a fixedpoint of \x. add (sub (pow b x) m) x
201         ; but unfortunately Y that_function doesn't normalize
202         ; instead:
203         ; how many times we can mul b by b while result <= m
204         let log = \m b. (\base build. m build base (\cur go sofar. sofar))
205                 ; where base is
206                 (make_triple b true 0)
207                 ; and build is
208                 (\t. t (\cur go sofar. and go (leq cur m)
209                            (make_triple (mul cur b) true (succ sofar)) ; continue
210                            (make_triple cur false sofar) ; enough
211                 ))  in
212
213
214         ; Rosenbloom's fixed point combinator
215         let Y = \f. (\h. f (h h)) (\h. f (h h)) in
216         ; Turing's fixed point combinator
217         let Theta = (\u f. f (u u f)) (\u f. f (u u f))  in
218
219
220         ; length for version 1 lists
221         let length = Y (\self lst. isempty lst 0 (succ (self (tail lst))))  in
222
223
224         ; numhelper 0 f z ~~> z
225         ; when n > 0: numhelper n f z ~~> f (pred n)
226         ; compare Bunder/Urbanek pred
227         let numhelper = \n. n (\u v. v (u succ)) (K 0) (\p f z. f p)  in
228
229         ; accepts fixed point combinator as a parameter, so you can use different ones
230         let fact = \y. y (\self n. numhelper n (\p. mul n (self p)) 1)  in
231
232
233
234         fact Theta 3  ; returns 6
235
236
237 <!--
238         ; my original efficient comparisons
239         let leq = (\base build consume. \m n. n consume (m build base) get_1st (\x. false) true)
240                         ; where base is
241                         (make_pair zero I) ; supplying this pair as an arg to its 2nd term returns the pair
242                         ; and build is
243                         (\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)
244                         ; and consume is
245                         (\p. p get_2nd p)  in
246         let lt = \m n. not (leq n m) in
247         let eq = (\base build consume. \m n. n consume (m build base) true (\x. false) true)
248                         ; where base is
249                         (make_pair zero (K (make_pair one I)))
250                         ; and build is
251                         (\p. p (\x y. make_pair (succ x) (K p)))
252                         ; and consume is
253                         (\p. p get_2nd p)  in ; or
254 -->
255
256 <!--
257         gcd
258         pow_mod
259
260
261         show Oleg's definition of integers:
262                 church_to_int = \n sign. n
263                 church_to_negint = \n sign s z. sign (n s z)
264
265                 ; int_to_church
266                 abs = \int. int I
267
268                 sign_case = \int ifpos ifzero ifneg. int (K ifneg) (K ifpos) ifzero
269
270                 negate_int = \int. sign_case int (church_to_negint (abs int)) zero (church_to_int (abs int))
271
272         for more, see http://okmij.org/ftp/Computation/lambda-arithm-neg.scm
273
274 -->