-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinner_type.py
221 lines (169 loc) · 6.1 KB
/
inner_type.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
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
class Monomorphic(object):
"""
Base class of monomorphic type.
"""
def __init__(self, name):
self.name = name
def __repr__(self):
return super(Monomorphic, self).__repr__()
def apply(self, symbol_map):
pass
def get_free_slots(self, m, a):
pass
def free_from(self, symbol):
pass
def __eq__(self, another):
return False
class Slot(Monomorphic):
"""
Slot for free variables
"""
type_map = {}
def __new__(cls, name):
if name in cls.type_map:
return cls.type_map[name]
type = super(Slot, cls).__new__(cls, name)
cls.type_map[name] = type
return type
def __init__(self, name):
super(Slot, self).__init__(name)
def __repr__(self):
return "'{}".format(self.name)
def apply(self, symbol_map):
symbol = symbol_map.get(self)
if not symbol or symbol == self:
return self
return symbol.apply(symbol_map)
def __eq__(self, another):
return another and isinstance(another, Slot) and self.name == another.name
def get_free_slots(self, symbol_map, free_slots=set()):
if self not in symbol_map and self not in free_slots:
free_slots.add(self)
def free_from(self, symbol):
return self != symbol
class Primitive(Monomorphic):
"""
Primitive type
"""
instance_map = {}
def __new__(cls, name, argument=None):
if name in cls.instance_map:
return cls.instance_map[name]
instance = super(Primitive, cls).__new__(cls, name, argument)
cls.instance_map[name] = instance
return instance
def __init__(self, name, argument=None):
super(Primitive, self).__init__(name)
self.argument = argument
def __repr__(self):
return self.name
def apply(self, symbol_map):
return self
def __eq__(self, another):
return another and isinstance(another, Primitive) and self.name == another.name
def free_from(self, symbol):
return True
class Composite(Monomorphic):
"""
Composite type
"""
def __init__(self, constructor, argument):
self.constructor = constructor
self.argument = argument
def __repr__(self):
if isinstance(self.constructor, Composite) \
and isinstance(self.constructor.constructor, Primitive) \
and self.constructor.constructor.name == '->':
left = self.constructor.argument
right = self.argument
if isinstance(left, Composite):
return '({}) -> {}'.format(left, right)
else:
return '{} -> {}'.format(left, right)
else:
if isinstance(self.argument, Composite):
return "{} ({})".format(self.constructor, self.argument)
else:
return "{} {}".format(self.constructor, self.argument)
def apply(self, symbol_map):
return Composite(self.constructor.apply(symbol_map), self.argument.apply(symbol_map))
def __eq__(self, another):
return another \
and isinstance(another, Composite) \
and self.constructor == another.constructor \
and self.argument == another.argument
def get_free_slots(self, symbol_map, free_slots=set()):
self.constructor.get_free_slots(symbol_map, free_slots)
self.argument.get_free_slots(symbol_map, free_slots)
return free_slots
def free_from(self, symbol):
return self.constructor.free_from(symbol) and self.argument.free_from(symbol)
class Polymorphic(object):
def __init__(self, quantifier, base):
self.n, self.quantifier, symbol_map = 1, set(), {}
for key in sorted(quantifier, lambda x, y: cmp(x.name, y.name)):
renamed_slot = Slot(self.rename(self.n))
if key != renamed_slot:
symbol_map[key] = renamed_slot
self.quantifier.add(renamed_slot)
self.n += 1
self.base = base.apply(symbol_map)
def instantiate(self, generator):
symbol_map = {}
for key in self.quantifier:
symbol_map[key] = generator()
return self.base.apply(symbol_map)
@staticmethod
def rename(number):
base_char = ord('a')
letters = []
while number > 0:
number -= 1
letters.append(chr(base_char + number % 26))
number = number / 26
letters.reverse()
return ''.join(letters)
def __repr__(self):
buf = 'forall'
for item in sorted(self.quantifier, lambda x, y: cmp(x.name, y.name)):
buf += ' ' + repr(item)
return '{}. {}'.format(buf, repr(self.base))
class Arrow(Composite):
def __init__(self, argument, result):
super(Arrow, self).__init__(
Composite(Primitive("->"), argument),
result
)
class Product(Composite):
def __init__(self, argument, result):
super(Product, self).__init__(
Composite(Primitive("*"), argument),
result
)
def unify(symbol_map, one, another):
if isinstance(one, Slot) \
and isinstance(another, Slot) \
and one.apply(symbol_map) == another.apply(symbol_map):
return True
elif isinstance(one, Primitive) \
and isinstance(another, Primitive) \
and one.name == another.name \
and one.argument == another.argument:
return True
elif isinstance(one, Composite) and isinstance(another, Composite):
return unify(symbol_map, one.constructor, another.constructor) \
and unify(symbol_map, one.argument, another.argument)
elif isinstance(one, Slot):
another_prime = another.apply(symbol_map)
if another_prime.free_from(one):
symbol_map[one] = another_prime
return True
return False
elif isinstance(another, Slot):
one_prime = one.apply(symbol_map)
if one_prime.free_from(another):
symbol_map[another] = one_prime
return True
return False
else:
return False