owl-basic 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- owl_basic/__init__.py +3 -0
- owl_basic/algorithms.py +29 -0
- owl_basic/ast_utils.py +204 -0
- owl_basic/basic_visitor.py +55 -0
- owl_basic/cfg_vertex.py +65 -0
- owl_basic/codegen/__init__.py +0 -0
- owl_basic/codegen/clr/__init__.py +0 -0
- owl_basic/codegen/clr/cil_visitor.py +1296 -0
- owl_basic/codegen/clr/cts.py +56 -0
- owl_basic/codegen/clr/emitters.py +94 -0
- owl_basic/codegen/clr/generate.py +539 -0
- owl_basic/correlation_visitor.py +119 -0
- owl_basic/data_visitor.py +62 -0
- owl_basic/decoder.py +339 -0
- owl_basic/errors.py +22 -0
- owl_basic/flow/__init__.py +17 -0
- owl_basic/flow/basic_block.py +34 -0
- owl_basic/flow/basic_block_identifier.py +66 -0
- owl_basic/flow/basic_block_orderer.py +29 -0
- owl_basic/flow/connectors.py +19 -0
- owl_basic/flow/convert_sub_visitor.py +28 -0
- owl_basic/flow/entry_point_locator.py +55 -0
- owl_basic/flow/entry_point_visitor.py +48 -0
- owl_basic/flow/flow_analysis.py +56 -0
- owl_basic/flow/flow_graph_creator.py +14 -0
- owl_basic/flow/flowgraph_visitor.py +178 -0
- owl_basic/flow/longjump_converter.py +20 -0
- owl_basic/flow/longjump_visitor.py +53 -0
- owl_basic/flow/subroutine_converter.py +38 -0
- owl_basic/flow/traversal.py +110 -0
- owl_basic/gml_visitor.py +151 -0
- owl_basic/line_mapper.py +43 -0
- owl_basic/line_number_visitor.py +65 -0
- owl_basic/main.py +381 -0
- owl_basic/node.py +21 -0
- owl_basic/options.py +22 -0
- owl_basic/owltyping/__init__.py +1 -0
- owl_basic/owltyping/function_type_inferer.py +50 -0
- owl_basic/owltyping/hindley_milner.py +524 -0
- owl_basic/owltyping/set_function_type_visitor.py +25 -0
- owl_basic/owltyping/type_system.py +220 -0
- owl_basic/owltyping/typecheck.py +60 -0
- owl_basic/owltyping/typecheck_visitor.py +471 -0
- owl_basic/parent_visitor.py +37 -0
- owl_basic/process.py +36 -0
- owl_basic/separation_visitor.py +98 -0
- owl_basic/sigil.py +30 -0
- owl_basic/simplify_visitor.py +204 -0
- owl_basic/singleton.py +127 -0
- owl_basic/source_debugging.py +124 -0
- owl_basic/symbol_table_visitor.py +220 -0
- owl_basic/symbol_tables.py +195 -0
- owl_basic/syntax/__init__.py +0 -0
- owl_basic/syntax/ast.py +1081 -0
- owl_basic/syntax/ast_meta.py +228 -0
- owl_basic/syntax/grammar.py +1972 -0
- owl_basic/syntax/lexer.py +943 -0
- owl_basic/syntax/parser.py +77 -0
- owl_basic/utility.py +26 -0
- owl_basic/visitor.py +43 -0
- owl_basic/xml_blocks.py +137 -0
- owl_basic/xml_visitor.py +101 -0
- owl_basic-0.6.0.dist-info/METADATA +37 -0
- owl_basic-0.6.0.dist-info/RECORD +69 -0
- owl_basic-0.6.0.dist-info/WHEEL +5 -0
- owl_basic-0.6.0.dist-info/entry_points.txt +2 -0
- owl_basic-0.6.0.dist-info/licenses/LICENSE +21 -0
- owl_basic-0.6.0.dist-info/licenses/THIRD-PARTY-NOTICES.md +57 -0
- owl_basic-0.6.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import operator
|
|
2
|
+
|
|
3
|
+
from owl_basic.algorithms import all_equal
|
|
4
|
+
from owl_basic.syntax.ast import ReturnFromFunction
|
|
5
|
+
from owl_basic import errors
|
|
6
|
+
from owl_basic.algorithms import representative
|
|
7
|
+
from owl_basic.flow.traversal import depthFirstSearch
|
|
8
|
+
from owl_basic.owltyping.type_system import PendingOwlType, ObjectOwlType, FloatOwlType
|
|
9
|
+
|
|
10
|
+
def inferTypeOfFunction(entry_point):
|
|
11
|
+
'''
|
|
12
|
+
Infer the type of the function defined at entry_point by discovering the
|
|
13
|
+
type of all of the return points from the function. If the types are
|
|
14
|
+
different numeric types, promote IntegerTypes to FloatTypes. If the types
|
|
15
|
+
are a mixture of StringTypes and NumericTypes box the return value in an
|
|
16
|
+
object type.
|
|
17
|
+
|
|
18
|
+
:param entry_point: A DefineFunction AstNode at the start of a user
|
|
19
|
+
defined function.
|
|
20
|
+
:returns: The infered type of the function. One of IntegerType, FloatType,
|
|
21
|
+
StringType or ObjectType. If the type of the function could not
|
|
22
|
+
be inferred (possibly because other function types need inferring too)
|
|
23
|
+
return PendingType.
|
|
24
|
+
'''
|
|
25
|
+
print("DEF ", entry_point.name)
|
|
26
|
+
return_types = set()
|
|
27
|
+
for vertex in depthFirstSearch(entry_point):
|
|
28
|
+
if isinstance(vertex, ReturnFromFunction):
|
|
29
|
+
return_types.add(vertex.returnValue.actualType)
|
|
30
|
+
|
|
31
|
+
for type in return_types:
|
|
32
|
+
print(" =", type)
|
|
33
|
+
|
|
34
|
+
# If there is only one return type, set the type of the function, and exit
|
|
35
|
+
if len(return_types) == 0:
|
|
36
|
+
errors.warning("%s never returns at line %s" % (entry_point.name, entry_point.lineNum))
|
|
37
|
+
elif PendingOwlType() in return_types:
|
|
38
|
+
return_type = PendingOwlType()
|
|
39
|
+
elif len(return_types) == 1:
|
|
40
|
+
return_type = representative(return_types)
|
|
41
|
+
elif reduce(operator.and_, [type.isA(NumericOwlType()) for type in return_types]):
|
|
42
|
+
# TODO: Modify all function returns to cast to FloatOwlType, if necessary
|
|
43
|
+
return_type = FloatOwlType()
|
|
44
|
+
else:
|
|
45
|
+
# TODO: Modify all function returns to box to ObjectOwlType, if necessary
|
|
46
|
+
# TODO: Modify all function calls to unbox from ObjectOwlType, to what?
|
|
47
|
+
return_type = ObjectOwlType()
|
|
48
|
+
entry_point.returnType = return_type
|
|
49
|
+
return return_type
|
|
50
|
+
|
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
'''
|
|
3
|
+
.. module:: hindley_milner
|
|
4
|
+
:synopsis: An implementation of the Hindley Milner type checking algorithm
|
|
5
|
+
based on the Scala code by Andrew Forrest, the Perl code by
|
|
6
|
+
Nikita Borisov and the paper "Basic Polymorphic Typechecking"
|
|
7
|
+
by Cardelli.
|
|
8
|
+
.. moduleauthor:: Robert Smallshire
|
|
9
|
+
'''
|
|
10
|
+
|
|
11
|
+
from __future__ import print_function
|
|
12
|
+
|
|
13
|
+
#=======================================================#
|
|
14
|
+
# Class definitions for the abstract syntax tree nodes
|
|
15
|
+
# which comprise the little language for which types
|
|
16
|
+
# will be inferred
|
|
17
|
+
|
|
18
|
+
class Lambda(object):
|
|
19
|
+
"""Lambda abstraction"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, v, body):
|
|
22
|
+
self.v = v
|
|
23
|
+
self.body = body
|
|
24
|
+
|
|
25
|
+
def __str__(self):
|
|
26
|
+
return "(fn {v} => {body})".format(v=self.v, body=self.body)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Ident(object):
|
|
30
|
+
"""Identfier"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, name):
|
|
33
|
+
self.name = name
|
|
34
|
+
|
|
35
|
+
def __str__(self):
|
|
36
|
+
return self.name
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Apply(object):
|
|
40
|
+
"""Function application"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, fn, arg):
|
|
43
|
+
self.fn = fn
|
|
44
|
+
self.arg = arg
|
|
45
|
+
|
|
46
|
+
def __str__(self):
|
|
47
|
+
return "({fn} {arg})".format(fn=self.fn, arg=self.arg)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Let(object):
|
|
51
|
+
"""Let binding"""
|
|
52
|
+
|
|
53
|
+
def __init__(self, v, defn, body):
|
|
54
|
+
self.v = v
|
|
55
|
+
self.defn = defn
|
|
56
|
+
self.body = body
|
|
57
|
+
|
|
58
|
+
def __str__(self):
|
|
59
|
+
return "(let {v} = {defn} in {body})".format(v=self.v, defn=self.defn, body=self.body)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Letrec(object):
|
|
63
|
+
"""Letrec binding"""
|
|
64
|
+
|
|
65
|
+
def __init__(self, v, defn, body):
|
|
66
|
+
self.v = v
|
|
67
|
+
self.defn = defn
|
|
68
|
+
self.body = body
|
|
69
|
+
|
|
70
|
+
def __str__(self):
|
|
71
|
+
return "(letrec {v} = {defn} in {body})".format(v=self.v, defn=self.defn, body=self.body)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
#=======================================================#
|
|
76
|
+
# Exception types
|
|
77
|
+
|
|
78
|
+
class TypeError(Exception):
|
|
79
|
+
"""Raised if the type inference algorithm cannot infer types successfully"""
|
|
80
|
+
|
|
81
|
+
def __init__(self, message):
|
|
82
|
+
self.__message = message
|
|
83
|
+
|
|
84
|
+
message = property(lambda self: self.__message)
|
|
85
|
+
|
|
86
|
+
def __str__(self):
|
|
87
|
+
return str(self.message)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class ParseError(Exception):
|
|
91
|
+
"""Raised if the type environment supplied for is incomplete"""
|
|
92
|
+
def __init__(self, message):
|
|
93
|
+
self.__message = message
|
|
94
|
+
|
|
95
|
+
message = property(lambda self: self.__message)
|
|
96
|
+
|
|
97
|
+
def __str__(self):
|
|
98
|
+
return str(self.message)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
#=======================================================#
|
|
103
|
+
# Types and type constructors
|
|
104
|
+
|
|
105
|
+
class TypeVariable(object):
|
|
106
|
+
"""A type variable standing for an arbitrary type.
|
|
107
|
+
|
|
108
|
+
All type variables have a unique id, but names are only assigned lazily,
|
|
109
|
+
when required.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
next_variable_id = 0
|
|
113
|
+
|
|
114
|
+
def __init__(self):
|
|
115
|
+
self.id = TypeVariable.next_variable_id
|
|
116
|
+
TypeVariable.next_variable_id += 1
|
|
117
|
+
self.instance = None
|
|
118
|
+
self.__name = None
|
|
119
|
+
|
|
120
|
+
next_variable_name = 'a'
|
|
121
|
+
|
|
122
|
+
def _getName(self):
|
|
123
|
+
"""Names are allocated to TypeVariables lazily, so that only TypeVariables
|
|
124
|
+
present
|
|
125
|
+
"""
|
|
126
|
+
if self.__name is None:
|
|
127
|
+
self.__name = TypeVariable.next_variable_name
|
|
128
|
+
TypeVariable.next_variable_name = chr(ord(TypeVariable.next_variable_name) + 1)
|
|
129
|
+
return self.__name
|
|
130
|
+
|
|
131
|
+
name = property(_getName)
|
|
132
|
+
|
|
133
|
+
def __str__(self):
|
|
134
|
+
if self.instance is not None:
|
|
135
|
+
return str(self.instance)
|
|
136
|
+
else:
|
|
137
|
+
return self.name
|
|
138
|
+
|
|
139
|
+
def __repr__(self):
|
|
140
|
+
return "TypeVariable(id = {0})".format(self.id)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class TypeOperator(object):
|
|
144
|
+
"""An n-ary type constructor which builds a new type from old"""
|
|
145
|
+
|
|
146
|
+
def __init__(self, name, types):
|
|
147
|
+
self.name = name
|
|
148
|
+
self.types = types
|
|
149
|
+
|
|
150
|
+
def __str__(self):
|
|
151
|
+
num_types = len(self.types)
|
|
152
|
+
if num_types == 0:
|
|
153
|
+
return self.name
|
|
154
|
+
elif num_types == 2:
|
|
155
|
+
return "({0} {1} {2})".format(str(self.types[0]), self.name, str(self.types[1]))
|
|
156
|
+
else:
|
|
157
|
+
return "{0} {1}" % (self.name, ' '.join(self.types))
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class Function(TypeOperator):
|
|
161
|
+
"""A binary type constructor which builds function types"""
|
|
162
|
+
|
|
163
|
+
def __init__(self, from_type, to_type):
|
|
164
|
+
super(Function, self).__init__("->", [from_type, to_type])
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
# Basic types are constructed with a nullary type constructor
|
|
168
|
+
Integer = TypeOperator("int", []) # Basic integer
|
|
169
|
+
Bool = TypeOperator("bool", []) # Basic bool
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
#=======================================================#
|
|
174
|
+
# Type inference machinery
|
|
175
|
+
|
|
176
|
+
def analyse(node, env, non_generic=None):
|
|
177
|
+
"""Computes the type of the expression given by node.
|
|
178
|
+
|
|
179
|
+
The type of the node is computed in the context of the context of the
|
|
180
|
+
supplied type environment env. Data types can be introduced into the
|
|
181
|
+
language simply by having a predefined set of identifiers in the initial
|
|
182
|
+
environment. environment; this way there is no need to change the syntax or, more
|
|
183
|
+
importantly, the type-checking program when extending the language.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
node: The root of the abstract syntax tree.
|
|
187
|
+
env: The type environment is a mapping of expression identifier names
|
|
188
|
+
to type assignments.
|
|
189
|
+
to type assignments.
|
|
190
|
+
non_generic: A set of non-generic variables, or None
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
The computed type of the expression.
|
|
194
|
+
|
|
195
|
+
Raises:
|
|
196
|
+
TypeError: The type of the expression could not be inferred, for example
|
|
197
|
+
if it is not possible to unify two types such as Integer and Bool
|
|
198
|
+
ParseError: The abstract syntax tree rooted at node could not be parsed
|
|
199
|
+
"""
|
|
200
|
+
|
|
201
|
+
if non_generic is None:
|
|
202
|
+
non_generic = set()
|
|
203
|
+
|
|
204
|
+
if isinstance(node, Ident):
|
|
205
|
+
return getType(node.name, env, non_generic)
|
|
206
|
+
elif isinstance(node, Apply):
|
|
207
|
+
fun_type = analyse(node.fn, env, non_generic)
|
|
208
|
+
arg_type = analyse(node.arg, env, non_generic)
|
|
209
|
+
result_type = TypeVariable()
|
|
210
|
+
unify(Function(arg_type, result_type), fun_type)
|
|
211
|
+
return result_type
|
|
212
|
+
elif isinstance(node, Lambda):
|
|
213
|
+
arg_type = TypeVariable()
|
|
214
|
+
new_env = env.copy()
|
|
215
|
+
new_env[node.v] = arg_type
|
|
216
|
+
new_non_generic = non_generic.copy()
|
|
217
|
+
new_non_generic.add(arg_type)
|
|
218
|
+
result_type = analyse(node.body, new_env, new_non_generic)
|
|
219
|
+
return Function(arg_type, result_type)
|
|
220
|
+
elif isinstance(node, Let):
|
|
221
|
+
defn_type = analyse(node.defn, env, non_generic)
|
|
222
|
+
new_env = env.copy()
|
|
223
|
+
new_env[node.v] = defn_type
|
|
224
|
+
return analyse(node.body, new_env, non_generic)
|
|
225
|
+
elif isinstance(node, Letrec):
|
|
226
|
+
new_type = TypeVariable()
|
|
227
|
+
new_env = env.copy()
|
|
228
|
+
new_env[node.v] = new_type
|
|
229
|
+
new_non_generic = non_generic.copy()
|
|
230
|
+
new_non_generic.add(new_type)
|
|
231
|
+
defn_type = analyse(node.defn, new_env, new_non_generic)
|
|
232
|
+
unify(new_type, defn_type)
|
|
233
|
+
return analyse(node.body, new_env, non_generic)
|
|
234
|
+
assert 0, "Unhandled syntax node {0}".format(type(t))
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def getType(name, env, non_generic):
|
|
238
|
+
"""Get the type of identifier name from the type environment env.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
name: The identifier name
|
|
242
|
+
env: The type environment mapping from identifier names to types
|
|
243
|
+
non_generic: A set of non-generic TypeVariables
|
|
244
|
+
|
|
245
|
+
Raises:
|
|
246
|
+
ParseError: Raised if name is an undefined symbol in the type
|
|
247
|
+
environment.
|
|
248
|
+
"""
|
|
249
|
+
if name in env:
|
|
250
|
+
return fresh(env[name], non_generic)
|
|
251
|
+
elif isIntegerLiteral(name):
|
|
252
|
+
return Integer
|
|
253
|
+
else:
|
|
254
|
+
raise ParseError("Undefined symbol {0}".format(name))
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def fresh(t, non_generic):
|
|
258
|
+
"""Makes a copy of a type expression.
|
|
259
|
+
|
|
260
|
+
The type t is copied. The the generic variables are duplicated and the
|
|
261
|
+
non_generic variables are shared.
|
|
262
|
+
|
|
263
|
+
Args:
|
|
264
|
+
t: A type to be copied.
|
|
265
|
+
non_generic: A set of non-generic TypeVariables
|
|
266
|
+
"""
|
|
267
|
+
mappings = {} # A mapping of TypeVariables to TypeVariables
|
|
268
|
+
|
|
269
|
+
def freshrec(tp):
|
|
270
|
+
p = prune(tp)
|
|
271
|
+
if isinstance(p, TypeVariable):
|
|
272
|
+
if isGeneric(p, non_generic):
|
|
273
|
+
if p not in mappings:
|
|
274
|
+
mappings[p] = TypeVariable()
|
|
275
|
+
return mappings[p]
|
|
276
|
+
else:
|
|
277
|
+
return p
|
|
278
|
+
elif isinstance(p, TypeOperator):
|
|
279
|
+
return TypeOperator(p.name, [freshrec(x) for x in p.types])
|
|
280
|
+
|
|
281
|
+
return freshrec(t)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def unify(t1, t2):
|
|
285
|
+
"""Unify the two types t1 and t2.
|
|
286
|
+
|
|
287
|
+
Makes the types t1 and t2 the same.
|
|
288
|
+
|
|
289
|
+
Args:
|
|
290
|
+
t1: The first type to be made equivalent
|
|
291
|
+
t2: The second type to be be equivalent
|
|
292
|
+
|
|
293
|
+
Returns:
|
|
294
|
+
None
|
|
295
|
+
|
|
296
|
+
Raises:
|
|
297
|
+
TypeError: Raised if the types cannot be unified.
|
|
298
|
+
"""
|
|
299
|
+
|
|
300
|
+
a = prune(t1)
|
|
301
|
+
b = prune(t2)
|
|
302
|
+
if isinstance(a, TypeVariable):
|
|
303
|
+
if a != b:
|
|
304
|
+
if occursInType(a, b):
|
|
305
|
+
raise TypeError("recursive unification")
|
|
306
|
+
a.instance = b
|
|
307
|
+
elif isinstance(a, TypeOperator) and isinstance(b, TypeVariable):
|
|
308
|
+
unify(b, a)
|
|
309
|
+
elif isinstance(a, TypeOperator) and isinstance(b, TypeOperator):
|
|
310
|
+
if (a.name != b.name or len(a.types) != len(b.types)):
|
|
311
|
+
raise TypeError("Type mismatch: {0} != {1}".format(str(a), str(b)))
|
|
312
|
+
for p, q in zip(a.types, b.types):
|
|
313
|
+
unify(p, q)
|
|
314
|
+
else:
|
|
315
|
+
assert 0, "Not unified"
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def prune(t):
|
|
319
|
+
"""Returns the currently defining instance of t.
|
|
320
|
+
|
|
321
|
+
As a side effect, collapses the list of type instances. The function Prune
|
|
322
|
+
is used whenever a type expression has to be inspected: it will always
|
|
323
|
+
return a type expression which is either an uninstantiated type variable or
|
|
324
|
+
a type operator; i.e. it will skip instantiated variables, and will
|
|
325
|
+
actually prune them from expressions to remove long chains of instantiated
|
|
326
|
+
variables.
|
|
327
|
+
|
|
328
|
+
Args:
|
|
329
|
+
t: The type to be pruned
|
|
330
|
+
|
|
331
|
+
Returns:
|
|
332
|
+
An uninstantiated TypeVariable or a TypeOperator
|
|
333
|
+
"""
|
|
334
|
+
if isinstance(t, TypeVariable):
|
|
335
|
+
if t.instance is not None:
|
|
336
|
+
t.instance = prune(t.instance)
|
|
337
|
+
return t.instance
|
|
338
|
+
return t
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def isGeneric(v, non_generic):
|
|
342
|
+
"""Checks whether a given variable occurs in a list of non-generic variables
|
|
343
|
+
|
|
344
|
+
Note that a variables in such a list may be instantiated to a type term,
|
|
345
|
+
in which case the variables contained in the type term are considered
|
|
346
|
+
non-generic.
|
|
347
|
+
|
|
348
|
+
Note: Must be called with v pre-pruned
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
v: The TypeVariable to be tested for genericity
|
|
352
|
+
non_generic: A set of non-generic TypeVariables
|
|
353
|
+
|
|
354
|
+
Returns:
|
|
355
|
+
True if v is a generic variable, otherwise False
|
|
356
|
+
"""
|
|
357
|
+
return not occursIn(v, non_generic)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def occursInType(v, type2):
|
|
361
|
+
"""Checks whether a type variable occurs in a type expression.
|
|
362
|
+
|
|
363
|
+
Note: Must be called with v pre-pruned
|
|
364
|
+
|
|
365
|
+
Args:
|
|
366
|
+
v: The TypeVariable to be tested for
|
|
367
|
+
type2: The type in which to search
|
|
368
|
+
|
|
369
|
+
Returns:
|
|
370
|
+
True if v occurs in type2, otherwise False
|
|
371
|
+
"""
|
|
372
|
+
pruned_type2 = prune(type2)
|
|
373
|
+
if pruned_type2 == v:
|
|
374
|
+
return True
|
|
375
|
+
elif isinstance(pruned_type2, TypeOperator):
|
|
376
|
+
return occursIn(v, pruned_type2.types)
|
|
377
|
+
return False
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def occursIn(t, types):
|
|
381
|
+
"""Checks whether a types variable occurs in any other types.
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
v: The TypeVariable to be tested for
|
|
385
|
+
types: The sequence of types in which to search
|
|
386
|
+
|
|
387
|
+
Returns:
|
|
388
|
+
True if t occurs in any of types, otherwise False
|
|
389
|
+
"""
|
|
390
|
+
return any(occursInType(t, t2) for t2 in types)
|
|
391
|
+
|
|
392
|
+
def isIntegerLiteral(name):
|
|
393
|
+
"""Checks whether name is an integer literal string.
|
|
394
|
+
|
|
395
|
+
Args:
|
|
396
|
+
name: The identifier to check
|
|
397
|
+
|
|
398
|
+
Returns:
|
|
399
|
+
True if name is an integer literal, otherwise False
|
|
400
|
+
"""
|
|
401
|
+
result = True
|
|
402
|
+
try:
|
|
403
|
+
int(name)
|
|
404
|
+
except ValueError:
|
|
405
|
+
result = False
|
|
406
|
+
return result
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
#==================================================================#
|
|
411
|
+
# Example code to exercise the above
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def tryExp(env, node):
|
|
415
|
+
"""Try to evaluate a type printing the result or reporting errors.
|
|
416
|
+
|
|
417
|
+
Args:
|
|
418
|
+
env: The type environment in which to evaluate the expression.
|
|
419
|
+
node: The root node of the abstract syntax tree of the expression.
|
|
420
|
+
|
|
421
|
+
Returns:
|
|
422
|
+
None
|
|
423
|
+
"""
|
|
424
|
+
print(str(node) + " : ", end=' ')
|
|
425
|
+
try:
|
|
426
|
+
t = analyse(node, env)
|
|
427
|
+
print(str(t))
|
|
428
|
+
except (ParseError, TypeError) as e:
|
|
429
|
+
print(e)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def main():
|
|
433
|
+
"""The main example program.
|
|
434
|
+
|
|
435
|
+
Sets up some predefined types using the type constructors TypeVariable,
|
|
436
|
+
TypeOperator and Function. Creates a list of example expressions to be
|
|
437
|
+
evaluated. Evaluates the expressions, printing the type or errors arising
|
|
438
|
+
from each.
|
|
439
|
+
|
|
440
|
+
Returns:
|
|
441
|
+
None
|
|
442
|
+
"""
|
|
443
|
+
|
|
444
|
+
var1 = TypeVariable()
|
|
445
|
+
var2 = TypeVariable()
|
|
446
|
+
pair_type = TypeOperator("*", (var1, var2))
|
|
447
|
+
|
|
448
|
+
var3 = TypeVariable()
|
|
449
|
+
|
|
450
|
+
my_env = { "pair" : Function(var1, Function(var2, pair_type)),
|
|
451
|
+
"true" : Bool,
|
|
452
|
+
"cond" : Function(Bool, Function(var3, Function(var3, var3))),
|
|
453
|
+
"zero" : Function(Integer, Bool),
|
|
454
|
+
"pred" : Function(Integer, Integer),
|
|
455
|
+
"times": Function(Integer, Function(Integer, Integer)) }
|
|
456
|
+
|
|
457
|
+
pair = Apply(Apply(Ident("pair"), Apply(Ident("f"), Ident("4"))), Apply(Ident("f"), Ident("true")))
|
|
458
|
+
|
|
459
|
+
examples = [
|
|
460
|
+
# factorial
|
|
461
|
+
Letrec("factorial", # letrec factorial =
|
|
462
|
+
Lambda("n", # fn n =>
|
|
463
|
+
Apply(
|
|
464
|
+
Apply( # cond (zero n) 1
|
|
465
|
+
Apply(Ident("cond"), # cond (zero n)
|
|
466
|
+
Apply(Ident("zero"), Ident("n"))),
|
|
467
|
+
Ident("1")),
|
|
468
|
+
Apply( # times n
|
|
469
|
+
Apply(Ident("times"), Ident("n")),
|
|
470
|
+
Apply(Ident("factorial"),
|
|
471
|
+
Apply(Ident("pred"), Ident("n")))
|
|
472
|
+
)
|
|
473
|
+
)
|
|
474
|
+
), # in
|
|
475
|
+
Apply(Ident("factorial"), Ident("5"))
|
|
476
|
+
),
|
|
477
|
+
|
|
478
|
+
# Should fail:
|
|
479
|
+
# fn x => (pair(x(3) (x(true)))
|
|
480
|
+
Lambda("x",
|
|
481
|
+
Apply(
|
|
482
|
+
Apply(Ident("pair"),
|
|
483
|
+
Apply(Ident("x"), Ident("3"))),
|
|
484
|
+
Apply(Ident("x"), Ident("true")))),
|
|
485
|
+
|
|
486
|
+
# pair(f(3), f(true))
|
|
487
|
+
Apply(
|
|
488
|
+
Apply(Ident("pair"), Apply(Ident("f"), Ident("4"))),
|
|
489
|
+
Apply(Ident("f"), Ident("true"))),
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
# let f = (fn x => x) in ((pair (f 4)) (f true))
|
|
493
|
+
Let("f", Lambda("x", Ident("x")), pair),
|
|
494
|
+
|
|
495
|
+
# fn f => f f (fail)
|
|
496
|
+
Lambda("f", Apply(Ident("f"), Ident("f"))),
|
|
497
|
+
|
|
498
|
+
# let g = fn f => 5 in g g
|
|
499
|
+
Let("g",
|
|
500
|
+
Lambda("f", Ident("5")),
|
|
501
|
+
Apply(Ident("g"), Ident("g"))),
|
|
502
|
+
|
|
503
|
+
# example that demonstrates generic and non-generic variables:
|
|
504
|
+
# fn g => let f = fn x => g in pair (f 3, f true)
|
|
505
|
+
Lambda("g",
|
|
506
|
+
Let("f",
|
|
507
|
+
Lambda("x", Ident("g")),
|
|
508
|
+
Apply(
|
|
509
|
+
Apply(Ident("pair"),
|
|
510
|
+
Apply(Ident("f"), Ident("3"))
|
|
511
|
+
),
|
|
512
|
+
Apply(Ident("f"), Ident("true"))))),
|
|
513
|
+
|
|
514
|
+
# Function composition
|
|
515
|
+
# fn f (fn g (fn arg (f g arg)))
|
|
516
|
+
Lambda("f", Lambda("g", Lambda("arg", Apply(Ident("g"), Apply(Ident("f"), Ident("arg"))))))
|
|
517
|
+
]
|
|
518
|
+
|
|
519
|
+
for example in examples:
|
|
520
|
+
tryExp(my_env, example)
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
if __name__ == '__main__':
|
|
524
|
+
main()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from owl_basic.visitor import Visitor
|
|
2
|
+
|
|
3
|
+
class SetFunctionTypeVisitor(Visitor):
|
|
4
|
+
'''
|
|
5
|
+
Visitor for assigning the type of a named function at every call
|
|
6
|
+
site of that function.
|
|
7
|
+
'''
|
|
8
|
+
|
|
9
|
+
def __init__(self, function_name, function_type):
|
|
10
|
+
'''
|
|
11
|
+
:param function_name: The name of the function including the FN prefix.
|
|
12
|
+
:param type: The actual type to be assigned to the function.
|
|
13
|
+
'''
|
|
14
|
+
self.function_name = function_name
|
|
15
|
+
self.function_type = function_type
|
|
16
|
+
|
|
17
|
+
def visitAstNode(self, node):
|
|
18
|
+
node.forEachChild(self.visit)
|
|
19
|
+
|
|
20
|
+
def visitUserFunc(self, user_func):
|
|
21
|
+
if user_func.name == self.function_name:
|
|
22
|
+
print("Setting type of call to %s to %s" % (self.function_name, self.function_type))
|
|
23
|
+
user_func.actualType = self.function_type
|
|
24
|
+
user_func.forEachChild(self.visit)
|
|
25
|
+
|