Repository navigation
Expand file tree
/
Copy pathlambda.py
More file actions
296 lines (237 loc) · 8.22 KB
/
Copy pathlambda.py
File metadata and controls
296 lines (237 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Python universe with only single argumented functions - lambda
#
# It's illegal to have numbers, strings, operators, control flow etc.
# PyCon 2019 Tutorial https://www.youtube.com/watch?v=5C6sv7-eTKg
# But, what can you build with it?
# Switch
def LEFT(a):
def f(b):
return a # return first argument
return f
def RIGHT(a):
def f(b):
return b # return second argument
return f
# Passing a 'second' argument is called Currying in lambda, although the function
# only accepts one argument
# https://en.wikipedia.org/wiki/Currying
assert LEFT('v5')('gnd') == 'v5'
assert RIGHT('v5')('gnd') == 'gnd'
# Function definitions
# def AND(x):
# def f(y):
# return x(y)(x)
# return f
#
# shorter with lambda
# def AND(x):
# return lambda y: x(y)(x)
#
# Even shorter
# AND = lambda x: lambda y: x(y)(x) # or
# AND = lambda xy: x(y)(x) # although illegal, two argumented function
# AND = lambda: xy:xyx
# AND = lambda: xy.xyx # :'D
# lambda = function with parameter(s) e.g. lambda x: x -> creates a function with x as param and returns x
#incr = lambda x: x + 1
#assert incr(41) == 42
# The truth
def TRUE(x):
return lambda y: x # return the first argument
def FALSE(x):
return lambda y: y # return the second argument
assert TRUE('v5')('gnd') == 'v5'
assert FALSE('v5')('gnd') == 'gnd'
assert TRUE(TRUE)(FALSE) is TRUE # return function TRUE
assert TRUE(FALSE)(TRUE) is FALSE
assert FALSE(FALSE)(TRUE) is TRUE
# The bools
def NOT(x):
return x(FALSE)(TRUE) # if not false
# NOT(TRUE) is FALSE because TRUE always returns the first argument
# it gets executed as -> TRUE(FALSE)(TRUE)
assert NOT(TRUE) is FALSE
# NOT(FALSE) is the opposite and gets executed as FALSE(FALSE)(TRUE) which is true,
# because FALSE returns the second argument
assert NOT(FALSE) is TRUE
def AND(x):
return lambda y: x(y)(x)
assert AND(TRUE)(TRUE) is TRUE
assert AND(FALSE)(TRUE) is FALSE
assert AND(FALSE)(FALSE) is FALSE
assert AND(TRUE)(FALSE) is FALSE
def OR(x):
return lambda y: x(x)(y)
assert OR(TRUE)(FALSE) is TRUE
assert OR(FALSE)(FALSE) is FALSE
assert OR(FALSE)(TRUE) is TRUE
# Numbers
# They are not numbers, but behaviours
# https://en.wikipedia.org/wiki/Church_encoding
# API for a number f(x)
ZERO = lambda f: lambda x: x # no use of the 'f' function, same impl as false. Do nothing
ONE = lambda f: lambda x: f(x) # do one function call on x
TWO = lambda f: lambda x: f(f(x)) # Two functions call on x
THREE = lambda f: lambda x: f(f(f(x)))
FOUR = lambda f: lambda x: f(f(f(f(x))))
# Debug, so that you can 'see' what the behaviour is of the functions
def incr(x):
return x + 1 # Illegal in rules
assert incr(0) == 1
assert incr(incr(0)) == 2
assert THREE(incr)(0) == 3 # do three times incr with starting point 0
# doing FOUR(THREE) which will be 81... crazy! :0
assert FOUR(THREE)(incr)(0) == 81 # 3^4 -> 3 * 3 * 3 * 3
assert ZERO(incr)(0) == 0
# Math
# Successor, which will get to the next number
# SUCC(TWO) --> THREE
# The 'API' of a number is f(x), in order to implement a successor
# you must add an other function too it and then call the API of number.
SUCC = lambda n: (lambda f: lambda x: f(n(f)(x)))
assert SUCC(FOUR)(incr)(0) == 5 # successor of 4 -> 5
assert SUCC(SUCC(FOUR))(incr)(0) == 6
# Arithmetic
# Add
ADD = lambda x: lambda y: y(SUCC)(x) # Take SUCC and apply it times the other
assert ADD(FOUR)(THREE)(incr)(0) == 7
# Multi
MUL = lambda x: lambda y: lambda f: y(x(f)) # Execute y times the function with x
assert MUL(FOUR)(THREE)(incr)(0) == 12
# Data (structures)
# define functions for representing a simple data structure (pair)
# In LISP
#
# cons is a pair (tuple) datastructure
# car will select the first
# cdr will select the second
# (cons 2 3) -> (2, 3)
# (car p) -> 2
# (cdr p) -> 3
# car and cdr come from the IMB 704
# Illegal in the single arg. universe
def cons(a, b):
def select(m):
if m == 0:
return a
elif m == 1:
return b
return select
p = cons(2, 3)
p
p(0)
p(1)
def car(p):
return p(0)
def cdr(p):
return p(1)
# These are not illegal!
CONS = lambda a: lambda b: lambda s: s(a)(b) # s -> select left or right
CAR = lambda p: p(TRUE) # TRUE selects the first one
CDR = lambda p: p(FALSE) # FALSE selects the second one
assert CONS(2)(3)(TRUE) == 2
assert CAR(CONS(2)(3)) == 2
assert CDR(CONS(2)(3)) == 3
# Linked list
CONS(2)(CONS(3)(4))
# Can you subtract? the reverse
# THREE, how to get TWO?
# tuple with + 1
# (0, 0)
# (1, 0)
# (2, 1)
# (3, 2)
# (n + 1, n)
def t(p):
return (p[0]+1, p[0]) # Is illegal, because of the + 1
THREE(t)((0,0))
# Lambda version of t
# p[0] = CAR... the first / left one
# T = you have a pair, make a CONS and apply SUCC (is next value) on CAR (left value) of p
# which results in CAR + 1 and apply CAR again for the other value of the pair
# input (0, 0) -> output (1, 0)
T = lambda p: CONS(SUCC(CAR(p)))(CAR(p))
assert CAR(FOUR(T)(CONS(ZERO)(ZERO)))(incr)(0) == 4
a = FOUR(T)(CONS(ZERO)(ZERO))
assert CAR(a)(incr)(0) == 4
assert CDR(a)(incr)(0) == 3 # reverse
# Predecessor
# count from 0 -> to x and then take the predecessor from pairs
PRED = lambda n: CDR(n(T)(CONS(ZERO)(ZERO)))
a = FOUR(THREE) # = 81 -> 3^4 -> 3 * 3 * 3 * 3
b = PRED(a) # (0, 0) (1, 0) (2, 1) ... etc (81, 80)
assert b(incr)(0) == 80
# Substract
SUB = lambda x: lambda y: y(PRED)(x)
assert SUB(FOUR)(TWO)(incr)(0) == 2
# Test a number for zero
# ZERO doesn't execute a function, therefor TRUE will be returned
# for all other numbers fALSE will be executed
ISZERO = lambda n: n(lambda f: FALSE)(TRUE)
ISZERO(ZERO) # returns TRUE function
ISZERO(ONE) # returns FALSE
# The assembly
# Recursion
# Factorial
def fact(n):
if n == 0:
return 1
else:
return n*fact(n-1)
assert fact(4) == 24 # Illegal
# Need a 'lazy' evaluation because both branches of ISZERO are
# getting evaluated which gives a max depth recursion error.
#FACT = lambda n: ISZERO(n)\
# (ONE)\
# (MUL(n)(FACT(PRED(n))))
LAZY_TRUE = lambda x: lambda y: x()
LAZY_FALSE = lambda x: lambda y: y()
ISZERO = lambda n: n(lambda f: LAZY_FALSE)(LAZY_TRUE)
FACT = lambda n: ISZERO(n)\
(lambda: ONE)\
(lambda: MUL(n)(FACT(PRED(n))))
# ^^^^ Illegal
assert FACT(THREE)(incr)(0) == 6
# The anonymous
# How does recurion work when no variables are allowed?
# In lambda calculus this not allowed. No globals, no assignment...
fact = lambda n: 1 if n == 0 else n*fact(n-1) # Illegal
# ^^^^ NO!
# How to rewrite
fact = (lambda f: lambda n: 1 if n == 0 else n *f(f)(n-1))\
(lambda f: lambda n: 1 if n == 0 else n *f(f)(n-1)) # DRY :'D, paste yourself in there
# two arg. function + number f^ n^^
print(
(lambda f: lambda n: 1 if n == 0 else n *f(f)(n-1))\
(lambda f: lambda n: 1 if n == 0 else n *f(f)(n-1))(5))
# Take out the middle
R = (lambda f: lambda n: 1 if n == 0 else n*f(n-1))
# fact must be a fixed point of R.
# fixed point = you get back what you put in
# e.g. 1 = sqrt(1)
# factorial = R(factorial)
# print(fact(5))
# Suppose that there's a function Y that computes
# the fixed point of R.
#
# Y(R) -> Fixed point of R (whatever it is)
#
# Y(R) = R(Y(R))
#
# Recursion trick:
#
# Y(R) = (lambda x: R(x))(Y(R)) # can't be done, because Y(R) -> Nothing
# Y(R) = (lambda x: R(x))(lambda x: R(x)) # repeat yourself trick, but does not work. you need the function trick
# Y(R) = (lambda x: R(x(x))(lambda x: R(x(x)))
# Y(R) = (lambda f: (lambda x: f(x(x)))(lambda x: f(x(x))))(R) # Formula, but R on both sides
Y = lambda f: (lambda x: f(x(x)))(lambda x: f(x(x))) # drop the R on both sides
# The Y combinator -> Haskell B Curry
R = (lambda f: lambda n: 1 if n == 0 else n*f(n-1)) # the middle part
#Y = lambda f: (lambda x: f(x(x)))(lambda x: f(x(x))) # abstraction of recursion with functions, but gives a max recursion depth error, because of python eager arg evaluation
Y = lambda f: (lambda x: f(lambda z: x(x)(z)))(lambda x: f(lambda z: x(x)(z))) # solution is to provide a 'extra' argument z and pass it through like a decorator (wrap the function)
fact = Y(R)
print(fact(6))
# Now we've a abstraction of recursion Y, lets calculate fib
R1 = lambda f: lambda n: 1 if n <= 2 else f(n-1) + f(n-2)
fib = Y(R1)
print(fib(10))