λ calculus (Lambda Calculus) II: Curry and encoding

1. Currying (Currying)

Currying a function is to be converted to a single multi-parameter argument function method. Similar to the composite function.

Now we have two input values ​​xy corresponds to a function f (x, y), how to make a function F begun to accept only a value of it?

We can define a F1 = λx.F2, then make F2 = λy.f (x, y) to complete currying.

 

2. The encoding (Encoding)

With so many algorithms specific to what use it? Then we can use some programming language code to indicate the basic data and statements, to achieve functional programming.

 

TRUE := λxy.x

FALSE := λxy.y

NOT := λfxy.fyx

OR := λxy.xxy

AND := λxy.xyx

IFELSE := λfxy.fxy

 

We all know that in the computer world binary 0 is NOT, 1 is TRUE. The same is true in the λ calculus:

ZERO := λxy.y

 

And we have an addend, based on each zero plus one can get all the natural numbers, we have opposite subtrahend each time minus one.

SUCCESSOR := λxyz.y(xyz)

PRED := λnfx.n(λgh.h(gf)) (λy.x) (λu.u)

 

We can get through this formula for a natural number of items:

n = λfx. f^n (x)

 

We have some other algorithms:

ADD := λxypq.xp(ypq)

MULT := λxyz.x(yz)

EXP := λxy.yx

 

Let's try!

 NOT TRUE

= (λfxy.fyx) (λxy.x) 
= (λfxy.fyx) (λab.a)
= λxy.(λab.a) yx
= λxy.(λb.y) x
= λxy.y
= FALSE

 

   ONE
= SUCCESSOR ZERO
= (λxyz.y(xyz)) (λxy.y)
= (λxyz.y(xyz)) (λab.b)
= λyz.y((λab.b) yz) 
= λyz.y((λb.b) z) 
= λyz.yz
= ONE

 

    ADD ZERO ONE
= (λxypq.xp(ypq)) (λxy.y) (λxy.xy)
= (λxypq.xp(ypq)) (λab.b) (λcd.cd))
= (λypq. (λab.b) p(ypq)) (λcd.cd))
= (λypq. (λb.b) (ypq)) (λcd.cd))
= (λypq. (ypq)) (λcd.cd))
= (λpq. ((λcd.cd)pq)) 
= (λpq. ((λd.pd)q)) 
= (λpq.pq)) 
= (λxy.xy)
= ONE

 

So that we can perform basic arithmetic, we know that modern procedural programming language, there are two basic structures, namely, to determine statements and loops, we have given coding IFELSE implementation of judgment statement. Then loop it? Unfortunately, in functional programming in general there is no loop, because there is only a function of functional programming, we want to achieve recycling can only use a recursive (Recursion) - are also seeking methods we often see that the Fibonacci number Fibonacci sequence. The next chapter describes the coding λ calculus list and realize functional programming recursive, recursive and configured to traverse the list.

Guess you like

Origin www.cnblogs.com/qianyexi/p/12159374.html