XGitUrl: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=lists_and_numbers.mdwn;h=571ed7b8e6295c2f3d7d4f71bd84b3c93c0e1ace;hp=bde7c2f3c0e01294cffee3689c7ab455437d3218;hb=HEAD;hpb=b9ea2a12c36370a96a3d2565fbb88c800921118c;ds=inline
diff git a/lists_and_numbers.mdwn b/lists_and_numbers.mdwn
deleted file mode 100644
index bde7c2f3..00000000
 a/lists_and_numbers.mdwn
+++ /dev/null
@@ 1,628 +0,0 @@
Building Lists
==============

To build a datastructure, you begin by deciding what the datastructure needs to do. When we built booleans, what they needed to do was select between two choices. When we built ordered pairs, what we needed was a way to wrap two elements into the pair, and ways to operate on the wrapped elements, especially a way to extract a specified one of them.

Now we're going to try to build lists. First, let's explain what is the difference bwteen a list and a pair.

A list can two elements, but it can also have more elements, or fewer. A list can even have zero elements: this is called the empty list. Sometimes this is written `nil`. In Scheme it's also written `'()` and `(list)`, and in OCaml it's written `[]`. Those languages are nice and have list structures prebuilt into them. But we're going to build lists ourselves, from scratch.

OK, so a list doesn't have to have two elements, but still, what's the difference between a twoelement list and a pair? And the difference between a threeelement list and a triple?

The difference has to do with types. In the untyped lambda calculus we don't explicitly refer to or manipulate types, however even here we'll need to pay attention to which types of arguments we're giving to which functions. For instance, if you wrote:

 and getthirdoftriple makepair

This would evaluate to something, but it's not immediately obvious what it would be, and it's not likely to be especially useful. Even in the untyped lambda calculus, if we want computations that are easy to work with and reason about, we're going to want to pay some attention to the types we're treating formulas to have. For example, we're only going to want to pass formulas that represent boolean values as arguments to `and`. The computation may well terminate even when we don't. But the result won't be one we're in a position to make any good use of.

Moreover, lists and pairs (and triples, and so on), are data structures we'll find in many typed languages as well. Thinking about types is the way to understand what makes these data structures different.

The differences are:

* A list is typehomegeneous, that is, all its elements must be (or be treatable as being) of the same type.

* Not so a pair: its elements need not be of the same type.

We regard two pairs as being of the same type when their corresponding members are of the same type.

Another difference between lists and pairs:

* The length of a list is not essential to its type. A twoelement list can be of the same type as a threeelement list (whose members are of the right type).

* Not so a pair: no pair is of the same type as any triple, no matter what the types of their elements.

Q: Sometimes mathematicians identify the triple (1,2,3) with the pair (1,(2,3)), whose first member is 1 and whose second member is the pair (2,3). Wouldn't then a triple have the same type as pair, namely the pair it's identical to?

A: This is not an identity but an implementation. The claim that the triple (1,2,3) is not the same type as any pair is akin to the claim that the integer 1 is not the same type as any set. It allows you to go on and build settheoretic constructions whose structure matches the desired behavior of the integers, and so is a reasonable implementation of them. This is exactly what we did when building higherorder functions to implement pairs, in the first place.

It is true, there are interesting and difficult questions here in the philosophical foundations of mathematics. But I hope we can proceed nonetheless.


Version 1


OK, then, what sort of behavior do we want out of lists?

Well we need something to serve as the empty list. And we need a way to take an arbitrary object `hd`, and a list (possibly the empty list) `tl`, and return the list whose **head** is `hd` and whose **tail** is `tl`. And we need a way to tell, of an arbitrary list, whether it's the empty list. And we need a way to extract, from a nonempty list, that list's head and its tail. That's it. Given these basic operations, we should be able to do whatever else we want to do with lists: count their length, reverse them, or whatever. We'll explore how to do those more complex operations in the assignment.

It's natural to help ourselves to pairs as bricks to use in constructing lists. The way we described building a new list out of a head `hd` and a tail `tl`, for instance, sounds a lot like building a new pair `(hd, tl)`. But we also have to think about what the empty list will beand more importantly, how to tell the empty list apart from other lists.

We'll work through a couple of different ways to do this. They'll get more principled as we proceed.

First, one way to handle the issue of "Is this the empty list or not?" is to have every list contain a boolean flag set to `true` if it's the empty list, and `false` if it isn't. Perhaps the lists could be, not pairs, but triples. The conventional implementation of this idea makes them instead a pair whose first member is the boolean flag, and whose second member, when the list is nonempty, is the list's head and tail. So a (nonempty) list whose head is `hd` and whose tail is `tl` would be represented by:

 (false, (hd, tl))

What about the empty list? Well we know it will be:

 (true, N)

for some `N`. What should stand in for `N`?

No particular choice seems forced here. One strategy would be to go ahead and build your family of list operations, and see whether any particular choice for `N` made some of the other operations easier to define, or more elegant. Heres's an example. We shouldn't expect the result of extracting the head of the empty list to be meaningful. But what about the result of extracting its tail? You could argue that this operation should also be meaningless. Or you could argue that the empty list should be its own tail. If we went the latter way, it would be nice to let `N` in our construction of the empty list be some value, such that, when we tried to extract the empty list's tail in the same way we try to extract other lists' tails, we got back the empty list itself. And in fact it's possible to do this. (However, it requires a fixedpoint combinator, which we won't discuss until next week.)



For the time being, though, let's not worry about what stands in for `N` in our construction of the empty list. What should our other primitive list operations look like?

Well, building a list from a new element `hd` and an existing list `tl` isn't hard: we just build a pair whose first value is false and whose second value is a pair of `hd` and `tl`:

 makepair false (makepair hd tl)

Determining whether a list is the empty list is just a matter of extracting the first element of the (outer) pair:

 somelist getfirst

Given a nonempty list, extracting its head is just a matter of extracting the pair that is its second element, and extracting the first element of that pair:

 somenonemptylist getsecond getfirst

and so on.


Version 2


OK, version 1 works. But it might look ad hoc. Plus there's that matter of the `N` in the construction of the empty list that we don't know what should be.

If we do things just a bit differently, it will be easier to see some systematic rationale for them.

We've already seen some **enumerations**, These are datastructures that consist of several discrete values: such as true and false, or black and white, or red and green and blue. (Sometimes these groups are understood to have an order, but that's not important for our purposes.)

We've already seen how to build up data structures like this. For instance, red could be:

 \rd gn bl. rd

that is a function that waits to be supplied with three choices: the "ifyouarered" choice, the "ifyouareblue" choice, and the "ifyouaregreen" choice, and then yields the "ifyouarered" choice.

Now what if we wanted one of the enumerated possibilities to be associated with some further parameter. For instance, we wanted a structure that represented things as being (just) blue, or (just) green, or as *having some specific degree* of redness. How could we do that?

We might do it by adopting the convention that the "ifyouarered" selection be not just an arbitrary value, but specifically a function that expected to be supplied with the degree of redness we're dealing with.

In other words, given an member `colored` of this new data structure, we'd use it like this:

 colored (\deg. coloredisredtodegreedeg) coloredisinsteadgreen coloredisinsteadblue

and then if `colored` had the blue value from our data structure, this would evaluate to `coloredisinsteadblue`. If `colored` had a redtodegreedeg value, it would evaluate to the result of supplying the relevant degree `deg` to the function `(\deg. coloredisredtodegreedeg)`.

To build a value of redtodegreedeg, we'd replace our original:

 \rd gn bl. rd

with:

 \rd gn bl. rd deg

Understand?

If so, then you should be able to understand the underlying rationale of a list. We've just considered a datastructure that models an exclusive choice from among beingredtoagivendegree, just being green, or just being blue. Eliminate the blue choice. And let's associate a second parameter with being red, so that we have beingredtoagivendegreeandilluminatedtoadifferentdegree. Our red value would then look like this:

 \rd gn. rd deg illum

And our green value would look like this:

 \rd gn. gn

Why are we talking about this? Can you anticipate?

Answer: For "green", substitute "empty list". For "red", substitute "nonempty list". For "degreeofredness", substitute "head of the nonempty list." for "degreeofillumination," substitute "tail of the noneempty list." And voilà!

Spelling it out explicitly, we say:

> **nil** is defined to be `\ifnonempty ifempty. ifempty`

> **makelist** is defined to be `\hd tl. \ifnonempty ifempty. ifnonempty hd tl`

Defining **isnil** and the head and tail extractors takes some more thought. When operating with any list implemented as we're proposing, we have to pass the list an "ifyou'renonempty" handler and a "ifyou'reempty handler." If the list is nonempty, this will evaluate to the result of supplying the list's head and tail as arguments to the handler. If the list is empty, it will instead give us back the emptyhandler, unprocessed.

So to check whether the list is empty, we could pass it an emptyhandler of `true`, and a nonempty handler which accepts a head and tail argument, and then just returns the constant false:

 somelist (\hd tl. false) true

What about extracting the head of a list? It only makes sense to do this when the list is known to be nonempty. In that case, the emptyhandler can be anything since we know it's going to be discarded. We'll designate this dummy handler `H`. On the other hand, it's easy to see what our nonempty handler should be:

 somelist (\hd tl. hd) H

Similarly for extracting the tail of a list.

When we get to discussing types, you'll see that the strategy deployed here has great generality. (Moreover, you can see the version 1 strategy as an approximate implementation of it.)


There are other reasonable choices you could make for how to implement lists. We'll come back later and discuss a third. If you're creative, you'll be able to design more yourself. The hard part is making the design principled and minimizing extraneous cruft, like the `N` and the `H` in our above discussion.


Building Numbers
================

Now how might we go about building numbers? We'll just try to build the natural numbers: 0, 1, 2, ...

If you think about lists and numbers, you should start to see some interesting similarities between them. In each case there's a base value (the empty list, 0). And then further values are always the result of some operation (appending a new head to, taking the successor of) on an existing value.

Because of this underlying similarity, we could in fact use either of the strategies described above to implement numbers.

Following the version 1 strategy for lists, we could let 0 be:

 (true, Z)

for some usefulor, if need be, arbitraryvalue `Z`. And given a number `n`, we could let the successor of `n` be:

 (false, n)

Given this implementation of the numbers, it would be an easy matter to determine whether a given number was zero. (How would you do it?) And it would also be an easy matter to determine the predecessor of any number that wasn't zero. (How would you do it?) Other arithmetic operations, however, would be more complicated. We haven't yet learned the tools that would be needed to determine whether two numbers were equal, or to add two numbers.

Following the version 2 strategy for lists, we could adopt the convention that we'd operate on numbers by passing them an "ifyou'renonzero" handler and a "ifyou'rezero" handler. If the number is nonzero, it will evaluate to the result of supplying the number's predecessor as an argument to the handler. If the number is zero, it will instead give us back the zero handler, unprocessed.

With that convention, we could let 0 be:

 \ifnonzero ifzero. ifzero

and we could let the successor function be:

 \n. \ifnonzero ifzero. ifnonzero n

This is a more principled implementation, and would again make some arithmetic operations easy to implement. But as before, others would be more difficult.


Composition
===========

We're going now to describe a third strategy, which goes in a different direction.

The **composition** of two functions is the operation that first applies one of them, and then applies the second. For instance, the arithmetic operation that maps a real number *r* to *r^{2}+1* is the composition of the squaring function and the successor function. This complex function is standardly written:

successor ∘ square

and in general:

(s ∘ f) z

should be understood as:

 s (f z)

Now consider the following series:

 z
 s z
 s (s z)
 s (s (s z))
 ...

Remembering that I is the identity combinator, this could also be written:

(I) z
(s) z
(s ∘ s) z
(s ∘ s ∘ s) z
...

And we might adopt the following natural shorthand for this:

s^{0} z
s^{1} z
s^{2} z
s^{3} z
...

We haven't introduced any new constants 0, 1, 2 into the object language, nor any new form of syntactic combination. This is all just a metalanguage abbreviation for:

 z
 s z
 s (s z)
 s (s (s z))
 ...

Church had the idea to implement the number *n* by an operation that accepted an arbitrary function `s` and base value `z` as arguments, and returned s^{n} z
as a result. In other words:

zero ≡ \s z. s^{0} z ≡ \s z. z
one ≡ \s z. s^{1} z ≡ \s z. s z
two ≡ \s z. s^{2} z ≡ \s z. s (s z)
three ≡ \s z. s^{3} z ≡ \s z. s (s (s z))
...

This is a very elegant idea. Implementing numbers this way, we'd let the successor function be:

succ ≡ \n. \s z. s (n s z)

So, for example:

 succ two
 ≡ (\n. \s z. s (n s z)) (\s z. s (s z))
~~> \s z. s ((\s z, s (s z)) s z)
~~> \s z. s (s (s z))

Adding *m* to *n* is a matter of applying the successor function to *n* *m* times. And we know how to apply an arbitrary function s to *n* *m* times: we just give that function s, and the basevalue *n*, to *m* as arguments. Because that's what the function we're using to implement *m* *does*. Hence **add** can be defined to be, simply:

 \m \n. m succ n

Isn't that nice?

How would we tell whether a number was 0? Well, look again at the implementations of the first few numbers:

zero ≡ \s z. s^{0} z ≡ \s z. z
one ≡ \s z. s^{1} z ≡ \s z. s z
two ≡ \s z. s^{2} z ≡ \s z. s (s z)
three ≡ \s z. s^{3} z ≡ \s z. s (s (s z))
...

We can see that with the nonzero numbers, the function s is always applied to an argument at least once. With zero, on the other hand, we just get back the basevalue. Hence we can determine whether a number is zero as follows:

 somenumber (\x. false) true

If somenumber is zero, this will evaluate to the base value true. If somenumber is nonzero, then it will evaluate to the result of applying (\x. false) to the result of applying ... to the result of applying (\x. false) to the base value true. But the result of applying (\x. false) to any argument is always false. So when somenumber is nonzero, this expressions evaluates to false.

Perhaps not as elegant as addition, but still decently principled.

Multiplication is even more elegant. Consider that applying an arbitrary function s to a base value z *m × n* times is a matter of applying s to z *n* times, and then doing that again, and again, and so on...for *m* repetitions. In other words, it's a matter of applying the function (\z. n s z) to z *m* times. In other words, *m × n* can be represented as:

 \s z. m (\z. n s z) z
 ~~> \s z. m n s z

which etareduces to:

 m n

Isn't that nice?

However, at this point the elegance gives out. The predecessor function is substantially more difficult to construct on this implementation. As with all of these operations, there are several ways to do it, but they all take at least a bit of ingenuity. If you're only first learning programming right now, it would be unreasonable to expect you to be able to figure out how to do it.

However, if on the other hand you do have some experience programming, consider how you might construct a predecessor function for numbers implemented in this way. Using only the resources we've so far discussed. (So you have no general facility for performing recursion, for instance.)






(list?)
nil
cons
nil?, (pair?)
head
tail

Chris's lists:
 nil = (t,N) = \f. f true N
 [a] = (f,(a,nil))
 [b,a] = (f,(b,[a]))

isnil = getfirst
head = L getsecond getfirst
tail = L getsecond getsecond



Lists 2:
 nil = false
 [a] = (a,nil)

L (\h\t.K deal_with_h_and_t) ifnil

We've already seen enumerations: true  false, red  green  blue
What if you want one or more of the elements to have associated data? e.g. red  green  blue

could handle like this:
 thevalue ifred ifgreen (\n. handlerifbluetodegreen)

 then red = \r \g \bhandler. r
 green = \r \g \bhandler. g
 makeblue = \degree. \r \g \bhandler. bhandler degree

A list is basically: empty  nonempty

 empty = \nonemptyhandler \ifempty. ifempty = false
 cons = \h \t. \nonemptyhandler \ifempty. nonemptyhandler h t

 so [a] = cons a empty = \nonemptyhandler \_. nonemptyhandler a empty



Lists 3:
[a; tl] isnil == (\f. f a tl) (\h \t.false) a b ~~> false a b

nil isnil == (\f. M) (\h \t. false) a b ~~> M[f:=isnil] a b == a

 so M could be \a \b. a, i.e. true
 so nil = \f. true == K true == K K = \_ K

 nil = K true
 [a] = (a,nil)
 [b,a] = (b,[a])

isnil = (\x\y.false)

nil tail = K true tail = true = \x\y.x = \f.f? such that f? = Kx. there is no such.
nil head = K true head = true. could mislead.



Church figured out how to encode integers and arithmetic operations
using lambda terms. Here are the basics:

0 = \f\x.fx
1 = \f\x.f(fx)
2 = \f\x.f(f(fx))
3 = \f\x.f(f(f(fx)))
...

Adding two integers involves applying a special function + such that
(+ 1) 2 = 3. Here is a term that works for +:

+ = \m\n\f\x.m(f((n f) x))

So (+ 0) 0 =
(((\m\n\f\x.m(f((n f) x))) ;+
 \f\x.fx) ;0
 \f\x.fx) ;0

~~>_beta targeting m for beta conversion

((\n\f\x.[\f\x.fx](f((n f) x)))
 \f\x.fx)

\f\x.[\f\x.fx](f(([\f\x.fx] f) x))

\f\x.[\f\x.fx](f(fx))

\f\x.\x.[f(fx)]x

\f\x.f(fx)









let t = < y>>
let f = < n>>
let b = < f (g x)>>
let k = << $t$ >>
let get1 = << $t$ >>
let get2 = << $f$ >>
let id = < x>>
let w = < f f>>
let w' = < f f n>>
let pair = < theta x y>>

let zero = < z>>
let succ = < s (n s z)>>
let one = << $succ$ $zero$ >>
let two = << $succ$ $one$ >>
let three = << $succ$ $two$ >>
let four = << $succ$ $three$ >>
let five = << $succ$ $four$ >>
let six = << $succ$ $five$ >>
let seven = << $succ$ $six$ >>
let eight = << $succ$ $seven$ >>
let nine = << $succ$ $eight$ >>

(*
let pred = < n (fun u v > v (u $succ$)) ($k$ $zero$) $id$ >>
*)
let pred = < n (fun u v > v (u s)) ($k$ z) $id$ >>
(* ifzero n withp whenz *)
let ifzero = < n (fun u v > v (u $succ$)) ($k$ $zero$) (fun n' withp
whenz > withp n') >>

let pred' =
 let iszero = << fun n > n (fun _ > $f$) $t$ >> in
 << fun n > n ( fun f z' > $iszero$ (f $one$) z' ($succ$ (f z')) ) ($k$ $zero$) $zero$ >>

(*
 so n = zero ==> (k zero) zero
 n = one ==> f=(k zero) z'=zero ==> z' i.e. zero
 n = two ==> g(g (k zero)) zero
 f = g(k zero) z'=zero
 f = fun z'>z' z'=zero ==> succ (f z') = succ(zero)
 n = three ==> g(g(g (k zero))) zero
 f = g(g(k zero)) z'=zero
 f = fun z' > succ(i z') z'=zero
 ==> succ (f z') ==> succ(succ(z'))
*)

let pred'' =
 let shift = (* > *)
 < d (fun d1 _ > $pair$ ($succ$ d1) d1) >> in
 < n $shift$ ($pair$ $zero$ $zero$) $get2$ >>



let add = < n $succ$ m>>
(* let add = < fun s z > m s (n s z) >> *)
let mul = << fun m n > n (fun z' > $add$ m z') $zero$ >>


(* we create a pairslist of the numbers up to m, and take the
 * head of the nth tail. the tails are in the form (k tail), with
 * the tail of mzero being instead (id). We unwrap the content by:
 * (k tail) tail_of_mzero
 * or
 * (id) tail_of_mzero
 * we let tail_of_mzero be the mzero itself, so the nth predecessor of
 * zero will still be zero.
 *)

let sub =
 let mzero = << $pair$ $zero$ $id$ >> in
 let msucc = << fun d > d (fun d1 _ > $pair$ ($succ$ d1) ($k$ d)) >> in
 let mtail = << fun d > d $get2$ d >> in (* or could use d $get2$ $mzero$ *)
 < n $mtail$ (m $msucc$ $mzero$) $get1$ >>

let min' = < $sub$ m ($sub$ m n) >>
let max' = < $add$ n ($sub$ m n) >>

let lt' =
 let mzero = << $pair$ $zero$ $id$ >> in
 let msucc = << fun d > d (fun d1 _ > $pair$ ($succ$ d1) ($k$ d)) >> in
 let mtail = << fun d > d $get2$ d >> in (* or could use d $get2$ $mzero$ *)
 < n $mtail$ (m $msucc$ $mzero$) $get1$ (fun _ > $t$) $f$ >>

let leq' =
 let mzero = << $pair$ $zero$ $id$ >> in
 let msucc = << fun d > d (fun d1 _ > $pair$ ($succ$ d1) ($k$ d)) >> in
 let mtail = << fun d > d $get2$ d >> in (* or could use d $get2$ $mzero$ *)
 < n $mtail$ (m $msucc$ $mzero$) $get1$ (fun _ > $f$) $t$ >>

let eq' =
 (* like leq, but now we make mzero have a selfreferential tail *)
 let mzero = << $pair$ $zero$ ($k$ ($pair$ $one$ $id$)) >> in
 let msucc = << fun d > d (fun d1 _ > $pair$ ($succ$ d1) ($k$ d)) >> in
 let mtail = << fun d > d $get2$ d >> in (* or could use d $get2$ $mzero$ *)
 < n $mtail$ (m $msucc$ $mzero$) $get1$ (fun _ > $f$) $t$ >>

(*
let divmod' = << fun n d > n
 (fun f' > f' (fun d' m' >
 $lt'$ ($succ$ m') d ($pair$ d' ($succ$ m')) ($pair$ ($succ$ d') $zero$)
 ))
 ($pair$ $zero$ $zero$) >>
let div' = < $divmod'$ n d $get1$ >>
let mod' = < $divmod'$ n d $get2$ >>
*)

let divmod' =
 let triple = << fun d1 d2 d3 > fun sel > sel d1 d2 d3 >> in
 let mzero = << $triple$ $succ$ ($k$ $zero$) $id$ >> in
 let msucc = << fun d > $triple$ $id$ $succ$ ($k$ d) >> in
 let mtail = (* open in dhead *)
 << fun d > d (fun dz mz df mf drest >
 fun sel > (drest dhead) (sel (df dz) (mf mz))) >> in
 << fun n divisor >
 ( fun dhead > n $mtail$ (fun sel > dhead (sel $zero$ $zero$)) )
 (divisor $msucc$ $mzero$ (fun _ _ d3 > d3 _))
 (fun dz mz _ _ _ > $pair$ dz mz) >>

let div' = < $divmod'$ n d $get1$ >>
let mod' = < $divmod'$ n d $get2$ >>

(*
 ISZERO = lambda n. n (lambda x. false) true,

 LE = lambda x. lambda y. ISZERO (MONUS x y),
 { ? x <= y ? }

 MONUS = lambda a. lambda b. b PRED a,
 {NB. assumes a >= b >= 0}

 DIVMOD = lambda x. lambda y.
 let rec dm = lambda q. lambda x.
 if LE y x then {y <= x}
 dm (SUCC q) (MONUS x y)
 else pair q x
 in dm ZERO x,
*)

(* f n =def. phi n_prev f_prev *)
let bernays = < n (fun d > $pair$ (d (fun n_prev f_prev > $succ$ n_prev)) (d phi)) ($pair$ $zero$ z) (fun n f > f)>>


(*
let pred_b = << $bernays$ $k$ $zero$ >>
let fact_b = << $bernays$ (fun x p > $mul$ ($succ$ x) p) $one$ >>

(* if m is zero, returns z; else returns withp (pred m) *)
let ifzero = < $bernays$ (fun x p > withp x) z m>>
let ifzero = < m ($k$ (withp ($pred$ m))) z>>
*)

let y = < (fun u > f (u u)) (fun u > f (u u))>>
(* strict ycombinator from The Little Schemer, Crockford's http://www.crockford.com/javascript/little.html *)
let y' = < (fun u > f (fun n > u u n)) (fun u > f (fun n > u u n))>>
(*
let y'' = < (fun u n > f (u u n)) (fun u n > f (u u n))>>
*)

let turing = <<(fun u f > f (u u f)) (fun u f > f (u u f))>>
let turing' = <<(fun u f > f (fun n > u u f n)) (fun u f > f (fun n > u u f n))>>


let fact_w = << $w$ (fun f n > $ifzero$ n (fun p > $mul$ n (f f
p)) $one$)>>
let fact_w' = <<(fun f n > f f n) (fun f n > $ifzero$ n (fun p >
 $mul$ n (f f p)) $one$)>>
let fact_w'' = let u = <<(fun f n > $ifzero$ n (fun p > $mul$ n (f f
p)) $one$)>> in < $u$ $u$ n>>

let fact_y = << $y$ (fun f n > $ifzero$ n (fun p > $mul$ n (f p)) $one$)>>
let fact_y' = << $y'$ (fun f n > $ifzero$ n (fun p > $mul$ n (f p)) $one$)>>

let fact_turing = << $turing$ (fun f n > $ifzero$ n (fun p > $mul$ n (f p)) $one$)>>
let fact_turing' = << $turing'$ (fun f n > $ifzero$ n (fun p > $mul$ n (f p)) $one$)>>



let zero_ = < z>>;;
let succ_ = < s m (m s z)>>;;
let one_ = << $succ_$ $zero_$ >>;;
let two_ = << $succ_$ $one_$ >>;;
let three_ = << $succ_$ $two_$ >>;;
let four_ = << $succ_$ $three_$ >>;;
let five_ = << $succ_$ $four_$ >>;;
let six_ = << $succ_$ $five_$ >>;;
let seven_ = << $succ_$ $six_$ >>;;
let eight_ = << $succ_$ $seven_$ >>;;
let nine_ = << $succ_$ $eight_$ >>;;

let pred_ = < n (fun n' _ > n') $zero_$ >>
let add_ = < n (fun _ f' > $succ_$ f') m>>
let mul_ = < n (fun _ f' > $add_$ m f') $zero_$ >>
(* let pow_ = *)

let sub_ = < n (fun _ f' > $pred_$ f') m>>
let min_ = < $sub_$ m ($sub_$ m n)>> (* mmax(mn,0) = m+min(nm,0) = min(n,m) *)
let max_ = < $add_$ n ($sub_$ m n)>> (* n+max(mn,0) = max(m,n) *)

let eq_ = < m (fun _ fm' n > n (fun n' _ > fm' n') $f$) (fun n > n (fun _ _ > $f$) $t$)>>
let lt_ = < ($sub_$ n m) (fun _ _ > $t$) $f$ >>
let leq_ = << fun m n > ($sub_$ m n) (fun _ _ > $f$) $t$ >>

let divmod_ = << fun n d > n
 (fun _ f' > f' (fun d' m' >
 $lt_$ ($succ_$ m') d ($pair$ d' ($succ_$ m')) ($pair$ ($succ_$ d') $zero_$)
 ))
 ($pair$ $zero_$ $zero_$) >>
let div_ = < $divmod_$ n d $get1$ >>
let mod_ = < $divmod_$ n d $get2$ >>
