-
Notifications
You must be signed in to change notification settings - Fork 0
/
λ.py
153 lines (131 loc) · 5.41 KB
/
λ.py
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
from dataclasses import dataclass
@dataclass
class Var:
ix: int
def __repr__(self): return f'${self.ix}'
def __hash__(self): return int(self.ix)
@dataclass
class Ref:
ix: int
def __repr__(self): return f'#{self.ix}'
def __hash__(self): return -int(self.ix)-1
@dataclass
class Lam:
body: any
def __repr__(self): return f'(λ {self.body})'
def __hash__(self): return hash(self.body)
@dataclass
class App:
__match_args__ = ('fn', 'xs')
def __init__(self, fn, *xs):
self.fn = fn
self.xs = xs
def __repr__(self): return f'({repr(self.fn)} {" ".join(repr(x) for x in self.xs)})'
def __hash__(self): return hash((self.fn, self.xs))
def __eq__(self, λ): return isinstance(λ, App) and self.fn == λ.fn and self.xs == λ.xs
def shift(λ, s: int, l: int):
match λ:
case Lam(body): return Lam(shift(body, s, l+1))
case App(f, (x,)): return App(shift(f, s, l), shift(x, s, l))
case Var(ix): return λ if ix < l else Var(ix + s)
case _: return λ
def subst(λ, n: int, e):
match λ:
case Lam(body): return Lam(subst(body, n+1, shift(e, 1, 0)))
case App(f, xs): return App(subst(f, n, e), *[subst(x, n, e) for x in xs])
case Var(ix): return e if n == ix else λ
case _: return λ
def redux(λ, L=[]):
match λ:
case App(Lam(f), (x,)): return shift(subst(f, 0, shift(x, 1, 0)), -1, 0)
case Lam(body): return Lam(redux(body, L))
case App(f, xs) if callable(f): return f(*[reduce(x, L) for x in xs])
case App(f, xs): return App(redux(f, L), *[redux(x, L) for x in xs])
case Ref(ix): return L[ix]
case _: return λ
def reduce(λ, L=[], limit=100):
while (reduced := redux(λ, L)) != λ and limit:
λ = reduced
limit -= 1
return λ
def parse(s: str):
"Parse string into ast"
ast = []
ix = 0
while ix < len(s):
if s[ix] == '(':
nopen = 1
six = ix
while nopen != 0:
ix += 1
if s[ix] == ')':
nopen -= 1
elif s[ix] == '(':
nopen += 1
ast.append(parse(s[six+1:ix]))
else:
chars = []
while ix < len(s) and not s[ix].isspace():
chars.append(s[ix])
ix += 1
if chars:
ast.append(''.join(chars))
ix += 1
return ast
def encode(ast, L=[]):
"Convert the raw ast by replacing names with their indices from L"
match ast:
case [ast]: return encode(ast, L)
case ['λ', body]: return Lam(encode(body, L))
case ['@', f, *xs]: return App(encode(f, L), *[encode(x, L) for x in xs]) # for conversion
case [*xs]: return App(*[encode(x, L) for x in ast])
case hole if hole[0] == '?': return hole
case debruijn if debruijn[0] == '$': return Var(int(debruijn[1:]))
case reference if reference[0] == '#': return Ref(int(reference[1:]))
case name if (ix := L.index(name)) is not None: return Ref(ix)
case _: raise ValueError(f"{ast}, it's all greek to me")
def decode(λ, L=[]):
match λ:
case App(f, xs): return f'({decode(f, L)} {" ".join([decode(x, L) for x in xs])})'
case Ref(ix): return L.terms[ix].repr
case Lam(body): return f'(λ {decode(body, L)})'
case _: return repr(λ)
def length(λ) -> int:
match λ:
case App(f, xs): return length(f) + sum(length(x) for x in xs)
case Lam(body): return 1 + length(body)
case _: return 1
if __name__ == '__main__':
assert parse("abc (ijk (xyz))") == ['abc', ['ijk', ['xyz']]]
assert parse("(-111 000 111)") == [['-111', '000', '111']]
assert parse("(λ $0) (#1 (λ $0))") == [['λ', '$0'], ['#1', ['λ', '$0']]]
T = lambda s: encode(parse(s))
assert redux(T("(λ (λ $0))")) == T("(λ (λ $0))")
assert redux(T("(λ (λ $2))")) == T("(λ (λ $2))")
assert redux(T("((λ $0) $1)")) == T("$1")
assert redux(T("((λ $0) (λ $2))")) == T("(λ $2)")
assert redux(T("((λ $2) (λ $0))")) == T("$1")
assert redux(T("((λ $0) (λ $1))")) == T("(λ $1)")
assert redux(T("((λ ($0 $0)) $1)")) == T("($1 $1)")
assert redux(redux(T("((λ ($0 $0)) (λ $0))"))) == T("(λ $0)")
assert redux(redux(T("((λ (λ (λ ($2 $1)))) (λ $0))"))) == T("(λ (λ $1))")
assert redux(redux(redux(T("(((λ (λ (λ ($2 $1)))) (λ $0)) (λ $1))")))) == T("(λ (λ $2))")
assert redux(T("((λ (λ $1)) (λ $0))")) == T("(λ (λ $0))")
assert redux(T("((λ (λ $0)) $1)")) == T("(λ $0)")
assert redux(T("((λ (λ $1)) (λ $2))")) == T("(λ (λ $3))")
assert redux(T("((λ (λ $1)) (λ $3))")) == T("(λ (λ $4))")
succ = T("(λ (λ (λ ($1 (($2 $1) $0)))))")
zero = T("(λ (λ $0))")
four = T("(λ (λ ($1 ($1 ($1 ($1 $0))))))")
assert reduce(App(succ, App(succ, App(succ, App(succ, zero))))) == four
Y = T("(λ ((λ ($1 ($0 $0))) (λ ($1 ($0 $0)))))")
assert redux(redux(App(Y, Y))) == App(Y, redux(App(Y, Y)))
cons = T("(λ (λ (λ (($0 $2) $1))))")
car = T("(λ ($0 (λ (λ $1))))")
cdr = T("(λ ($0 (λ (λ $0))))")
λ = App(App(cons, four), App(App(cons, four), four))
assert reduce(App(car, λ)) == reduce(App(car, App(cdr, λ))) == reduce(App(cdr, App(cdr, λ)))
L = [T("(λ $0)"), lambda x: x**2, 10]
λ = T("(#1 (#1 (#0 #2)))")
assert reduce(λ, L) == 10000
assert length(λ) == 4