-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
144 lines (117 loc) · 3.93 KB
/
test.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
import pprint
from inner_type import *
from inference import *
type1 = Arrow(
Product(
Arrow(Slot("a1"), Slot("a2")),
Composite(Primitive("list"), Slot("a3"))),
Composite(Primitive("list"), Slot("a2")))
type2 = Arrow(
Product(
Arrow(Slot("a3"), Slot("a4")),
Composite(Primitive("list"), Slot("a3"))),
Slot("a5"))
print "Unify Type 1 and Type 2:\n"
print "Type 1 ->", type1
print "Type 2 ->", type2, '\n'
map = {}
unify(map, type1, type2)
print "Slot map:", map, '\n'
print "Unifed result: ", "apply 1 ->", type1.apply(map)
print "Unifed result: ", "apply 2 ->", type2.apply(map), '\n'
env = Environment()
env['call'] = Polymorphic(
set([Slot('a'), Slot('b')]),
Arrow(Arrow(Slot('a'), Slot('b')), Arrow(Slot('a'), Slot('b'))))
env['seq'] = Polymorphic(
set([Slot('a'), Slot('b')]),
Arrow(Slot('a'), Arrow(Slot('b'), Slot('b'))))
env['car'] = Polymorphic(
set([Slot('a')]),
Arrow(
Composite(Primitive('list'), Slot('a')),
Slot('a')))
env['cdr'] = Polymorphic(
set([Slot('a')]),
Arrow(
Composite(Primitive('list'), Slot('a')),
Composite(Primitive('list'), Slot('a'))))
env['cons'] = Polymorphic(
set([Slot('a')]),
Arrow(
Slot('a'),
Arrow(
Composite(Primitive('list'), Slot('a')),
Composite(Primitive('list'), Slot('a')))))
env['newlist'] = Polymorphic(
set([Slot('a')]),
Arrow(
Primitive('unit'),
Composite(Primitive('list'), Slot('a'))))
env['empty?'] = Polymorphic(
set([Slot('a')]),
Arrow(Composite(Primitive('list'), Slot('a')), Primitive('bool')))
env['0'], env['1'] = Primitive('int'), Primitive('int')
env['if'] = Polymorphic(
set([Slot('a')]),
Arrow(Primitive('bool'),
Arrow(Composite(Primitive('thunk'), Slot('a')),
Arrow(Composite(Primitive('thunk'), Slot('a')), Slot('a')))))
env['nothing'] = Primitive('unit')
env['+'] = Arrow(
Primitive('int'),
Arrow(Primitive('int'), Primitive('int')))
env['hold'] = Polymorphic(
set([Slot('a')]),
Arrow(Slot('a'),
Composite(Primitive('thunk'), Slot('a'))))
def translate(a):
if isinstance(a, list):
if a[0] == 'function':
return FunctionDefine(a[1], translate(a[2]), translate(a[3]))
elif a[0] == 'let' and len(a) == 3:
return Assign(a[1], translate(a[2]))
elif a[0] == 'letf' and len(a) == 4:
return FunctionDefine(a[1], translate(a[2]), translate(a[3]), True)
elif a[0] == 'lambda':
t = Form.new_var()
return translate(['seq', ['letf', t, a[1], a[2]], t])
elif a[0] == 'begin':
return translate(reduce(lambda x, y: ['seq', x, y], a[1:]))
elif len(a) == 2:
return Apply(translate(a[0]), translate(a[1]))
else:
return Apply(translate(a[:-1]), translate(a[-1]))
else:
return Id(a)
f_id = translate(
["function", "crz", "x",
["seq", ["letf", "crz1", "y",
["seq", ["letf", "crz2", "z",
["seq", "x",
["seq", "y", "z"]]],
"crz2"]],
"crz1"]])
f_length = translate(
["function", "length", "a",
["if", ["empty?", "a"],
["hold", "0"],
["hold", ["+", "1", ["length", ["cdr", "a"]]]]]])
f_sum = translate(
["function", "sum", "a",
["if", ["empty?", "a"],
["hold", "0"],
["hold", ["+", ["car", "a"], ["sum", ["cdr", "a"]]]]]])
f_map = translate(
["function", "map", "f",
["begin",
["lambda", "a",
["if", ["empty?", "a"],
["hold", ["newlist", "nothing"]],
["hold", ["cons", ["f", ["car", "a"]], ["map", "f", ["cdr", "a"]]]]]]]])
f_id.infer(env)
f_length.infer(env)
f_sum.infer(env)
f_map.infer(env)
print "Some inference examples are defined in test.py, there is inference result:\n"
pprint.pprint(env)