aboutsummaryrefslogtreecommitdiff
path: root/lib/lambda.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lambda.py')
-rw-r--r--lib/lambda.py88
1 files changed, 44 insertions, 44 deletions
diff --git a/lib/lambda.py b/lib/lambda.py
index 7d62b83..10fe8e0 100644
--- a/lib/lambda.py
+++ b/lib/lambda.py
@@ -13,16 +13,16 @@
# f and x are stored as data attributes of the member.
class _CurryClass():
- def new(self, (f, x)):
- self.f = f
- self.x = x
- return self
- def fx(self, y):
- return self.f(self.x, y)
+ def new(self, (f, x)):
+ self.f = f
+ self.x = x
+ return self
+ def fx(self, y):
+ return self.f(self.x, y)
def CURRY(f, x):
- # NB: f is not "simple": it has 2 arguments
- return _CurryClass().new(f, x).fx
+ # NB: f is not "simple": it has 2 arguments
+ return _CurryClass().new(f, x).fx
# Numbers in Lambda Calculus
@@ -32,9 +32,9 @@ def CURRY(f, x):
# times to x, e.g., Twice(f, x) = f(f(x)).
# As far as I understand, there is no difference in real lambda
# calculus between
-# lambda f x : f(f(x))
+# lambda f x : f(f(x))
# and
-# lambda f : f o f # 'o' means function composition
+# lambda f : f o f # 'o' means function composition
# but in Python we have to write the first as Twice(f, x) and
# the second as twice(f).
@@ -55,9 +55,9 @@ def fourfold(f): return CURRY(Fourfold, f)
# NB: actually 'ntimes' is less concrete than 'Ntimes', as
# 'ntimes' returns a function, while 'Ntimes' returns a value
-# similar to what f(x) returns. 'Ntimes' can be derived
+# similar to what f(x) returns. 'Ntimes' can be derived
# from 'ntimes', as follows:
-# def Ntimes(f, x): return ntimes(f)(x)
+# def Ntimes(f, x): return ntimes(f)(x)
# but this doesn't help us since 'ntimes' can only be defined
# using Ntimes (or a trick like the one used by CURRY).
@@ -65,21 +65,21 @@ def fourfold(f): return CURRY(Fourfold, f)
# Arithmetic in Lambda Calculus
#
# We can perform simple arithmetic on the un-curried versions, e.g.,
-# Successor(Twice) = Thrice (2+1)
-# Sum(Thrice, Twice) = Fivefold (3+2)
-# Product(Thrice, Twice) = Sixfold (3*2)
-# Power(Thrice, Twice) = Ninefold (3**2)
+# Successor(Twice) = Thrice (2+1)
+# Sum(Thrice, Twice) = Fivefold (3+2)
+# Product(Thrice, Twice) = Sixfold (3*2)
+# Power(Thrice, Twice) = Ninefold (3**2)
#
# First we define versions that need f and x arguments.
# They have funny argument forms so the final functions can
# use CURRY, which only works on functions of exactly 2 arguments.
def SUCCESSOR(Ntimes, (f, x)): return f(Ntimes(f, x))
-def SUCCESSOR(Ntimes, (f, x)): return Ntimes(f, f(x)) # Same effect
+def SUCCESSOR(Ntimes, (f, x)): return Ntimes(f, f(x)) # Same effect
def SUM(Ntimes, (Mtimes, (f, x))): return Ntimes(f, Mtimes(f, x))
def PRODUCT(Ntimes, (Mtimes, (f, x))): return Ntimes(CURRY(Mtimes, f), x)
def POWER(Ntimes, (Mtimes, (f, x))):
- return Mtimes(CURRY(CURRY, Ntimes), f)(x)
+ return Mtimes(CURRY(CURRY, Ntimes), f)(x)
def Successor(Ntimes): return CURRY(SUCCESSOR, Ntimes)
def Sum(Ntimes, Mtimes): return CURRY(CURRY(SUM, Ntimes), Mtimes)
@@ -98,54 +98,54 @@ def Power(Ntimes, Mtimes): return CURRY(CURRY(POWER, Ntimes), Mtimes)
# P.S.: Here is a Lambda function in Python.
# It uses 'exec' and expects two strings to describe the arguments
-# and the function expression. Example:
-# lambda('x', 'x+1')
+# and the function expression. Example:
+# lambda('x', 'x+1')
# defines the successor function.
def lambda(args, expr):
- if '\n' in args or '\n' in expr:
- raise RuntimeError, 'lambda: no cheating!'
- stmt = 'def func(' + args + '): return ' + expr + '\n'
- print 'lambda:', stmt,
- exec(stmt)
- return func
+ if '\n' in args or '\n' in expr:
+ raise RuntimeError, 'lambda: no cheating!'
+ stmt = 'def func(' + args + '): return ' + expr + '\n'
+ print 'lambda:', stmt,
+ exec(stmt)
+ return func
# P.P.S.S.: Here is a way to construct Ntimes and ntimes directly.
# Example:
-# GenericNtimes(4)
+# GenericNtimes(4)
# is equivalent to Fourfold.
class _GenericNtimesClass():
- def new(self, n):
- self.n = n
- return self
- def Ntimes(self, (f, x)):
- n = self.n
- while n > 0: x, n = f(x), n-1
- return x
+ def new(self, n):
+ self.n = n
+ return self
+ def Ntimes(self, (f, x)):
+ n = self.n
+ while n > 0: x, n = f(x), n-1
+ return x
def GenericNtimes(n):
- return _GenericNtimesClass().new(n).Ntimes
+ return _GenericNtimesClass().new(n).Ntimes
# To construct any 'ntimes' function from the corresponding 'Ntimes',
-# we use a trick as used by CURRY. For example,
-# Ntimes2ntimes(Fourfold)
+# we use a trick as used by CURRY. For example,
+# Ntimes2ntimes(Fourfold)
# yields a function equivalent to fourfold.
class _Ntimes2ntimesClass():
- def new(self, Ntimes):
- self.Ntimes = Ntimes
- return self
- def ntimes(self, f):
- return CURRY(self.Ntimes, f)
+ def new(self, Ntimes):
+ self.Ntimes = Ntimes
+ return self
+ def ntimes(self, f):
+ return CURRY(self.Ntimes, f)
def Ntimes2ntimes(Ntimes): return _Ntimes2ntimesClass().new(Ntimes).ntimes
-# This allows us to construct generic 'ntimes' functions. Example:
-# generic_ntimes(3)
+# This allows us to construct generic 'ntimes' functions. Example:
+# generic_ntimes(3)
# is the same as thrice.
def generic_ntimes(n): return Ntimes2ntimes(GenericNtimes(n))