-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfoundation.py
More file actions
189 lines (140 loc) · 5.28 KB
/
foundation.py
File metadata and controls
189 lines (140 loc) · 5.28 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
#!/usr/bin/env python3
from dataclasses import dataclass
import collections
class Term:
pass
@dataclass(frozen=True)
class Reference:
dtype: Term
value: Term
class Context:
def __init__(self):
self.declarations = collections.defaultdict(list)
def lookup(self, name: str) -> Reference:
d = self.declarations[name]
return None if not d else d[-1]
def declare(self, name: str, type: Term, value: Term = None):
self.declarations[name].append(Reference(type, value))
def destroy(self, name):
self.declarations[name].pop()
@dataclass(frozen=True)
class Declaration(Term):
name: str
dtype: Term
value: Term
def __str__(self):
return (self.name +
' : ' +
str(self.dtype) +
(f' = {str(self.value)}' if self.value else ''))
def type(self, ctx: Context):
return self.dtype.eval(ctx)
def eval(self, ctx: Context):
return Declaration(self.name,
self.dtype.eval(ctx),
self.value and self.value.eval(ctx))
def replace(self, name, value):
if name == self.name:
return self
return Declaration(self.name,
self.dtype.replace(name, value),
self.value and self.value.replace(name, value))
@dataclass(frozen=True)
class Atom(Term):
identifier: str
def eval(self, ctx: Context) -> Term:
ref = ctx.lookup(self.identifier)
if not ref:
return self
return ref.value or self
def type(self, ctx: Context) -> Term:
ref = ctx.lookup(self.identifier)
if not ref:
return self
return ref.dtype.eval(ctx) or self
def replace(self, name: str, value: Term) -> Term:
if self.identifier == name:
return value
return self
def __str__(self):
return self.identifier
@dataclass(frozen=True)
class Arrow(Term):
input_types: tuple[Term]
output_type: Term
def eval(self, ctx: Context) -> Term:
return self
def type(self, ctx: Context) -> Term:
return Atom('type')
def replace(self, name: str, value: Term) -> Term:
return Arrow(tuple(it.replace(name, value) for it in self.input_types),
self.output_type.replace(name, value))
def __str__(self):
return ('[' +
' -> '.join(map(str, self.input_types + (self.output_type,)))
+ ']')
@dataclass(frozen=True)
class Lambda(Term):
parameters: tuple[Declaration]
body: Term
def eval(self, ctx: Context) -> Term:
return self
def type(self, ctx: Context) -> Term:
return Arrow(tuple(p.dtype for p in self.parameters), self.body.type(ctx))
def replace(self, name: str, value: Term) -> Term:
if any(name == p.name for p in self.parameters):
return self
return Lambda(tuple(Declaration(d.name,
d.dtype.replace(name, value))
for d in self.parameters),
self.body.replace(name, value))
def __str__(self):
return ('lambda (' +
', '.join(map(str, self.parameters)) +
') ' +
str(self.body))
@dataclass(frozen=True)
class Application(Term):
function: Term
arguments: tuple[Term]
def eval(self, ctx: Context) -> Term:
# Check if function is a Lambda or None
f = self.function.eval(ctx)
if isinstance(f, Lambda):
remaining, b = self._apply_lambda(f, ctx)
if remaining:
return Lambda(remaining, b)
return b
elif isinstance(f, Application):
return Application(f.function, f.arguments + self.arguments)
return Application(f, tuple(a.eval(ctx) for a in self.arguments))
def type(self, ctx: Context) -> Term:
ft = self.function.eval(ctx).type(ctx)
input_types = list(ft.input_types)
output_type = ft.output_type
for i, a in enumerate(self.arguments):
if isinstance(input_types[i], Declaration):
v = a.eval(ctx)
name = input_types[i].name
for j in range(i+1, len(input_types)):
input_types[j] = input_types[j].replace(name, v).eval(ctx)
output_type = output_type.replace(name, v).eval(ctx)
if len(self.arguments) == len(ft.input_types):
return output_type
return Arrow(tuple(input_types[len(self.arguments):]), output_type)
def _apply_lambda(self, f: Lambda, ctx: Context) -> tuple:
p, b = list(f.parameters), f.body
for i, a in enumerate(self.arguments):
a = a.eval(ctx)
b = b.replace(p[i].name, a)
for j in range(i+1, len(p)):
p[j] = p[j].replace(p[i].name, a)
remaining = tuple(p[len(self.arguments):])
return remaining, b
def replace(self, name: str, value: Term) -> Term:
return Application(self.function.replace(name, value),
tuple(a.replace(name, value) for a in self.arguments))
def __str__(self):
return ('(' +
' '.join(map(str, (self.function,) + self.arguments)) +
')')