lundi 1 janvier 2018

Lambda Calculus function to return TRUE/FALSE without "if"

I'm a new comer to Lambda Calculus & trying to understand its workings via Python code.


In Lambda calculus, TRUE, FALSE & IFELSE conditional can be represented as:

(I'm using L to stand for Lambda symbol)

TRUE = Lx.Ly.x
FALSE = Lx.Ly.y
IFELSE = Lc.Lx.Ly.((c x) y)

Using the above definitions, the following hold true:

(I'm writing the function applications in Python style below)

TRUE(e1)(e2) = e1
FALSE(e1)(e2) = e2
IFELSE(TRUE)(e1)(e2) = e1
IFELSE(FALSE)(e1)(e2) = e2

My attempt at creating an Lambda Calculus-style ABS function is coded below in Python. ABS applies the return value from GTZ (Greater-than-zero) repeatedly to compute abs(x).

The GTZ function uses an if statement. And since Lambda calculus doesn't have an if statement (AFAIK), how can this be implemented in Lambda calculus?

The online lambda calculus materials I reviewed all mention that IFELSE can be coded as function application; but none of them mention how the conditional input to IFELSE itself can be generated without an "if" statement or equivalent.

Please help me understand how this can be written in Lambda calculus.

Thanks!

def TRUE(x):
    def f(y):
        return x   
    return f

def FALSE(x):
    def s(y):
        return y    
    return s

def GTZ(x):
    return TRUE if x > 0 else FALSE

def ABS(x):
    return GTZ(x)(x)(-x)

Aucun commentaire:

Enregistrer un commentaire