-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_test.go
352 lines (286 loc) · 8.46 KB
/
merge_test.go
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"testing"
"Paratype/context"
"Paratype"
)
var funcCounter int = 0
var n int = 4
func TestFlow1(t *testing.T) {
f,g,h,q := FlowExample(0)
main.RunParatype(n, "out", true, f, q, g, h)
}
func TestFlow2(t *testing.T) {
f,g,h,q := FlowExample(0)
m,p,o := TwoExample(0)
main.RunParatype(n, "out", true, f, q, g, h, m, p, o)
}
func TestFlow3(t *testing.T) {
f,g,h,q := FlowExample(0)
m,p,o := TwoExample(0)
w,z := DownExample(0)
main.RunParatype(n, "out", true, f, q, g, h, m, p, o, w, z)
}
func TestFlow4(t *testing.T) {
f,g,h,q := FlowExample(0)
m,p,o := TwoExample(0)
w,z := DownExample(1)
v,l := DownExample(3)
u,a := DownExample(2)
main.RunParatype(n, "out", true, f, q, g, h, m, p, o, w, z, v, l, u, a)
}
func BenchmarkFlow5(b *testing.B) {
for i := 0; i < b.N; i++ {
f,g,h,q := FlowExample(0)
m,p,o := TwoExample(0)
v,l := DownExample(3)
main.RunParatype(n, "out", true, f, q, g, h, m, p, o, v, l)
}
}
func TestFlow5(t *testing.T) {
f,g,h,q := FlowExample(0)
m,p,o := TwoExample(0)
v,l := DownExample(3)
main.RunParatype(n, "out", true, f, q, g, h, m, p, o, v, l)
}
// g and h call f, mixed explicit types
func TestTwo(t *testing.T) {
f,g,h := TwoExample(0)
main.RunParatype(n, "out", true, f,g,h)
}
// f calls g, g has explicit types
func TestUp0(t *testing.T) {
// func f constraint T<Num> (T R) S
// = g(T R)
// func g(int float) int
// = int
// f : F_0 F_1 F_2
// f \circ g : F_0 F_1 F_2
// g : G_0 G_1 G_2
f, g := DownExample(0) // explicit type conflict (F_0 fl, G_0 in)
main.RunParatype(n, "out", true, f, g)
}
func TestUp1(t *testing.T) {
f, g := DownExample(1) // typeclass conflict
main.RunParatype(n, "out", true, f, g)
}
func TestUp2(t *testing.T) {
f, g := DownExample(2) // explicit type not in merged typeclass (in not mat)
main.RunParatype(n, "out", true, f, g)
}
func TestUp3(t *testing.T) {
f, g := DownExample(3) // no error
main.RunParatype(n, "out", true, f, g)
}
func MakeTestTypes() (num *context.TypeClass, mat *context.TypeClass,
in *context.Type, fl *context.Type, err *context.Type) {
s := make(map[*context.TypeClass]bool)
s[nil] = true
num = new(context.TypeClass)
num.Name = "Num"
num.Inherits = s
p := make(map[*context.TypeClass]bool)
p[nil] = true
mat = new(context.TypeClass)
mat.Name = "Matrix"
mat.Inherits = p;
ma := make(map[*context.TypeClass]bool)
ma[num] = true
ma[nil] = true
in = new(context.Type)
in.Name = "int"
in.Implements = ma
err = new(context.Type)
err.Name = "errorType"
tcmap := make(map[*context.TypeClass]bool)
tcmap[nil] = true
err.Implements = tcmap
fl = new(context.Type)
fl.Name = "float"
fl.Implements = ma
return
}
func MakeFunction(name string, numArgs int) *context.Function {
g := new(context.Function)
g.Name = name
g.Id = funcCounter
g.Atlas = make(map[string]map[int]*context.TypeVariable)
g.TypeMap = make(map[*context.TypeVariable]*context.Type)
g.TypeVarMap = make(map[*context.TypeVariable]*context.TypeVariable)
g.Errors = make(map[*context.Type]bool)
g.Parents = make(map[*context.Function]bool)
g.Children = make(map[int]map[*context.Function]bool)
funcCounter++
return g
}
func MakeTypeVar(name string, res bool) *context.TypeVariable {
s := new(context.TypeVariable)
s.Constraints = make(map[*context.TypeClass]bool)
s.Constraints[nil] = true
s.Resolved = res
s.Name = name
return s
}
// Test of: f calls g, f has explicit types
func TestDown(t *testing.T) {
// func f() Int
// = g(Int Float)
// func g(T R) S throws errorType
// = S
// f : F_0
// f \circ g : F_0 F_1 F_2
// g : G_0 G_1 G_2
_, _, in, fl, err := MakeTestTypes()
F0 := MakeTypeVar("F_0", true)
F1 := MakeTypeVar("F_1", true)
F2 := MakeTypeVar("F_2", true)
G0 := MakeTypeVar("G_0", false)
G1 := MakeTypeVar("G_1", false)
G2 := MakeTypeVar("G_2", false)
g := MakeFunction("g", 3)
g.TypeMap[G0] = nil
g.TypeMap[G1] = nil
g.TypeMap[G2] = nil
f := MakeFunction("f", 1)
f.TypeMap[F0] = in
f.TypeMap[F1] = fl
f.TypeMap[F2] = in
pf := context.FunctionsToPath(f)
pfg := context.FunctionsToPath(f, g)
pg := context.FunctionsToPath(g)
f.Atlas[pf] = map[int]*context.TypeVariable{0 : F0}
f.Atlas[pfg] = map[int]*context.TypeVariable{0 : F0, 1 : F1, 2 : F2}
g.Atlas[pg] = map[int]*context.TypeVariable{0 : G0, 1 : G1, 2 : G2}
f.Children[0] = make(map[*context.Function]bool)
f.Children[0][g] = true
g.Parents[f] = true
g.Errors[err] = true
main.RunParatype(n, "out", true, f, g)
}
func DownExample(errcode int) (f *context.Function, g *context.Function) {
num, mat, in, fl, _ := MakeTestTypes()
F0 := MakeTypeVar("F_0", false)
G0 := MakeTypeVar("G_0", true)
delete(F0.Constraints, nil)
if errcode == 2 || errcode == 1 {
F0.Constraints[mat] = true
} else {
F0.Constraints[num] = true
}
F1 := MakeTypeVar("F_1", false)
F2 := MakeTypeVar("F_2", false)
if errcode == 1 {
delete(G0.Constraints, nil)
G0.Constraints[num] = true
}
G1 := MakeTypeVar("G_1", true)
G2 := MakeTypeVar("G_2", true)
g = MakeFunction("w", 3)
g.TypeMap[G0] = in
g.TypeMap[G1] = fl
g.TypeMap[G2] = in
f = MakeFunction("z", 3)
if errcode == 0 {
f.TypeMap[F0] = fl
} else {
f.TypeMap[F0] = nil
}
f.TypeMap[F1] = nil
f.TypeMap[F2] = nil
pf := context.FunctionsToPath(f)
pfg := context.FunctionsToPath(f, g)
pg := context.FunctionsToPath(g)
f.Atlas[pf] = map[int]*context.TypeVariable{0 : F0, 1 : F1, 2 : F2}
f.Atlas[pfg] = map[int]*context.TypeVariable{0 : F0, 1 : F1, 2 : F2}
g.Atlas[pg] = map[int]*context.TypeVariable{0 : G0, 1 : G1, 2 : G2}
f.Children[0] = make(map[*context.Function]bool)
f.Children[0][g] = true
g.Parents[f] = true
return
}
func TwoExample(errcode int) (f *context.Function, g *context.Function, h *context.Function){
_, _, in, fl, _ := MakeTestTypes()
F0 := MakeTypeVar("F_0", true)
F1 := MakeTypeVar("F_1", false)
G0 := MakeTypeVar("G_0", false)
G1 := MakeTypeVar("G_1", true)
G2 := MakeTypeVar("G_2", true)
H0 := MakeTypeVar("H_0", false)
H1 := MakeTypeVar("H_1", true)
H2 := MakeTypeVar("H_2", true)
g = MakeFunction("m", 2)
g.TypeMap[G0] = nil
g.TypeMap[G1] = in
g.TypeMap[G2] = in
h = MakeFunction("n", 2)
h.TypeMap[H0] = nil
h.TypeMap[H1] = fl
h.TypeMap[H2] = fl
f = MakeFunction("o", 2)
f.TypeMap[F0] = fl
f.TypeMap[F1] = nil
pf := context.FunctionsToPath(f)
pgf := context.FunctionsToPath(g, f)
pg := context.FunctionsToPath(g)
phf := context.FunctionsToPath(h, f)
ph := context.FunctionsToPath(h)
f.Atlas[pf] = map[int]*context.TypeVariable{0 : F0, 1 : F1}
g.Atlas[pgf] = map[int]*context.TypeVariable{0 : G0, 1 : G2}
g.Atlas[pg] = map[int]*context.TypeVariable{0 : G0, 1 : G1}
h.Atlas[phf] = map[int]*context.TypeVariable{0 : H0, 1 : H2}
h.Atlas[ph] = map[int]*context.TypeVariable{0 : H0, 1 : H1}
h.Children[0] = make(map[*context.Function]bool)
g.Children[0] = make(map[*context.Function]bool)
g.Children[0][f] = true
h.Children[0][f] = true
f.Parents[g] = true
f.Parents[h] = true
return
}
func FlowExample(errcode int) (f *context.Function, g *context.Function, h *context.Function, q *context.Function) {
_, _, in, fl, _ := MakeTestTypes()
F0 := MakeTypeVar("F_0", true)
F1 := MakeTypeVar("F_1", true)
Q0 := MakeTypeVar("Q_0", true)
Q1 := MakeTypeVar("Q_1", true)
G0 := MakeTypeVar("G_0", false)
G1 := MakeTypeVar("G_1", false)
H0 := MakeTypeVar("H_0", false)
H1 := MakeTypeVar("H_1", false)
g = MakeFunction("g", 1)
g.TypeMap[G0] = nil
g.TypeMap[G1] = nil
h = MakeFunction("h", 1)
h.TypeMap[H0] = nil
h.TypeMap[H1] = nil
f = MakeFunction("f", 1)
f.TypeMap[F0] = fl
f.TypeMap[F1] = in
q = MakeFunction("q", 1)
q.TypeMap[Q0] = in
q.TypeMap[Q1] = fl
pf := context.FunctionsToPath(f)
pfg := context.FunctionsToPath(f, g)
pq := context.FunctionsToPath(q)
pqg := context.FunctionsToPath(q, g)
pg := context.FunctionsToPath(g)
pgh := context.FunctionsToPath(g, h)
ph := context.FunctionsToPath(h)
f.Atlas[pf] = map[int]*context.TypeVariable{0 : F0}
f.Atlas[pfg] = map[int]*context.TypeVariable{0 : F0, 1 : F1}
q.Atlas[pq] = map[int]*context.TypeVariable{0 : Q0}
q.Atlas[pqg] = map[int]*context.TypeVariable{0 : Q0, 1 : Q1}
g.Atlas[pg] = map[int]*context.TypeVariable{0 : G0, 1 : G1}
g.Atlas[pgh] = map[int]*context.TypeVariable{0 : G0, 1 : G1}
h.Atlas[ph] = map[int]*context.TypeVariable{0 : H0, 1 : H1}
f.Children[0] = make(map[*context.Function]bool)
q.Children[0] = make(map[*context.Function]bool)
g.Children[0] = make(map[*context.Function]bool)
f.Children[0][g] = true
q.Children[0][g] = true
g.Children[0][h] = true
h.Parents[g] = true
g.Parents[f] = true
g.Parents[q] = true
return
}