-
Notifications
You must be signed in to change notification settings - Fork 12
/
AbstractDataType.java
433 lines (349 loc) · 13.1 KB
/
AbstractDataType.java
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* jurgen@vinju.org
*******************************************************************************/
package io.usethesource.vallang.type;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import org.checkerframework.checker.nullness.qual.Nullable;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IListWriter;
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.exceptions.UndeclaredAbstractDataTypeException;
import io.usethesource.vallang.random.util.RandomUtil;
import io.usethesource.vallang.type.TypeFactory.RandomTypesConfig;
import io.usethesource.vallang.type.TypeFactory.TypeReifier;
/**
* A AbstractDataType is an algebraic sort. A sort is produced by
* constructors, @see NodeType. There can be many constructors for a single
* sort.
*
* @see ConstructorType
*/
/* package */ class AbstractDataType extends NodeType {
private final String fName;
private final Type fParameters;
protected AbstractDataType(String name, Type parameters) {
fName = name;
fParameters = parameters;
}
public static class Info implements TypeReifier {
@Override
public Type getSymbolConstructorType() {
return symbols().typeSymbolConstructor("adt", TF.stringType(), "name", TF.listType(symbols().symbolADT()), "parameters");
}
@Override
public IConstructor toSymbol(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar, Set<IConstructor> done) {
IConstructor res = simpleToSymbol(type, vf, store, grammar, done);
if (!done.contains(res)) {
done.add(res);
if (type.getTypeParameters().getArity() != 0) {
// we have to look up the original definition to find the original constructors
Type adt = store.lookupAbstractDataType(type.getName());
if (adt != null) {
type = adt; // then collect the grammar for the uninstantiated adt.
}
}
asProductions(type, vf, store, grammar, done);
}
return res;
}
private IConstructor simpleToSymbol(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar, Set<IConstructor> done) {
IListWriter w = vf.listWriter();
Type params = type.getTypeParameters();
if (params.getArity() > 0) {
for (Type param : params) {
w.append(param.asSymbol(vf, store, grammar, done));
}
}
return vf.constructor(getSymbolConstructorType(), vf.string(type.getName()), w.done());
}
@Override
public void asProductions(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar, Set<IConstructor> done) {
store.lookupAlternatives(type).stream().forEach(x -> x.asProductions(vf, store, grammar, done));
}
@Override
public Type fromSymbol(IConstructor symbol, TypeStore store, Function<IConstructor,Set<IConstructor>> grammar) {
String name = ((IString) symbol.get("name")).getValue();
Type adt = store.lookupAbstractDataType(name);
if (adt != null) {
// this stops infinite recursions while exploring the type store.
return adt;
}
Type params = symbols().fromSymbols((IList) symbol.get("parameters"), store, grammar);
if (params.isBottom() || params.getArity() == 0) {
adt = TF.abstractDataType(store, name);
}
else {
adt = TF.abstractDataTypeFromTuple(store, name, params);
}
// explore the rest of the definition and add it to the store
for (IConstructor t : grammar.apply(symbol)) {
((ConstructorType.Info) t.getConstructorType().getTypeReifier()).fromProduction(t, store, grammar);
}
return adt;
}
@Override
public boolean isRecursive() {
return true;
}
@Override
public Type randomInstance(Supplier<Type> next, TypeStore store, RandomTypesConfig rnd) {
if (!rnd.isWithRandomAbstractDatatypes()) {
return next.get();
}
if (rnd.nextBoolean()) {
Type[] adts = store.getAbstractDataTypes().toArray(new Type[0]);
if (adts.length > 0) { // otherwise we will generate a new instance down below
return adts[rnd.nextInt(adts.length)];
}
}
Type adt;
String adtName;
do {
// do not accidentally generate an ADT which already exists.
adtName = randomLabel(rnd);
} while (store.lookupAbstractDataType(adtName) != null);
if (rnd.nextBoolean()) {
Type param1 = new ParameterType.Info().randomInstance(next, store, rnd.withTypeParameters());
if (rnd.nextBoolean()) {
// first declare the open type:
adt = tf().abstractDataTypeFromTuple(store, adtName, tf().tupleType(param1));
}
else {
Type param2 = new ParameterType.Info().randomInstance(next, store, rnd.withTypeParameters());
// first declare the open type
adt = tf().abstractDataTypeFromTuple(store, adtName, tf().tupleType(param1, param2));
}
}
else {
adt = tf().abstractDataType(store, adtName);
}
// should be defined by at least one nullary constructor
tf().constructor(store, adt, randomLabel(rnd));
if (rnd.nextBoolean()) {
// and perhaps we generate another one with it:
tf().constructorFromTuple(store, randomInstance(next, store, rnd), randomLabel(rnd), randomTuple(next, store, rnd));
}
return adt;
}
}
@Override
public TypeReifier getTypeReifier() {
return new Info();
}
@Override
protected boolean isSupertypeOf(Type type) {
return type.isSubtypeOfAbstractData(this);
}
@Override
public boolean isOpen() {
return getTypeParameters().isOpen();
}
@Override
public Type lub(Type other) {
return other.lubWithAbstractData(this);
}
@Override
public Type glb(Type type) {
return type.glbWithAbstractData(this);
}
@Override
public boolean intersects(Type other) {
return other.intersectsWithAbstractData(this);
}
@Override
protected boolean intersectsWithAbstractData(Type type) {
if (!isParameterized()) {
return type == this;
}
if (type.getName().equals(this.getName())) {
return getTypeParameters().intersects(type.getTypeParameters());
}
return false;
}
@Override
protected boolean isSubtypeOfNode(Type type) {
return true;
}
@Override
protected boolean isSubtypeOfAbstractData(Type type) {
if (this == type) {
return true;
}
if (getName().equals(type.getName())) {
return getTypeParameters().isSubtypeOf(type.getTypeParameters());
}
return false;
}
@Override
protected Type lubWithAbstractData(Type type) {
if (this == type) {
return this;
}
if (fName.equals(type.getName())) {
return TF.abstractDataTypeFromTuple(new TypeStore(), fName,
getTypeParameters().lub(type.getTypeParameters()));
}
return TF.nodeType();
}
@Override
protected Type lubWithConstructor(Type type) {
return lubWithAbstractData(type.getAbstractDataType());
}
@Override
protected Type glbWithNode(Type type) {
return this;
}
@Override
protected Type glbWithAbstractData(Type type) {
if (this == type) {
return this;
}
if (fName.equals(type.getName())) {
return TF.abstractDataTypeFromTuple(new TypeStore(), fName,
getTypeParameters().glb(type.getTypeParameters()));
}
return TF.voidType();
}
@Override
protected Type glbWithConstructor(Type type) {
if (type.isSubtypeOf(this)) {
return type;
}
return TF.voidType();
}
@Override
public boolean isParameterized() {
return !fParameters.isBottom();
}
@Override
public boolean hasField(String fieldName, TypeStore store) {
// we look up by name because this might be an instantiated
// parameterized data-type
// which will not be present in the store.
Type parameterizedADT = store.lookupAbstractDataType(getName());
if (parameterizedADT == null) {
throw new UndeclaredAbstractDataTypeException(this);
}
for (Type alt : store.lookupAlternatives(parameterizedADT)) {
if (alt.hasField(fieldName, store)) {
return true;
}
}
return false;
}
@Override
public boolean hasKeywordField(String fieldName, TypeStore store) {
// we look up by name because this might be an instantiated
// parameterized data-type
// which will not be present in the store.
Type parameterizedADT = store.lookupAbstractDataType(getName());
if (parameterizedADT == null) {
throw new UndeclaredAbstractDataTypeException(this);
}
if (store.getKeywordParameterType(this, fieldName) != null) {
return true;
}
for (Type alt : store.lookupAlternatives(parameterizedADT)) {
if (alt.hasKeywordField(fieldName, store)) {
return true;
}
}
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(fName);
if (isParameterized()) {
sb.append("[");
int idx = 0;
for (Type elemType : fParameters) {
if (idx++ > 0) {
sb.append(",");
}
sb.append(elemType.toString());
}
sb.append("]");
}
return sb.toString();
}
@Override
public int hashCode() {
return 49991 + 49831 * fName.hashCode() + 49991 + fParameters.hashCode();
}
@Override
public boolean equals(@Nullable Object o) {
if (o == null) {
return false;
}
if (o.getClass().equals(getClass())) {
AbstractDataType other = (AbstractDataType) o;
return fName.equals(other.fName) && fParameters == other.fParameters;
}
return false;
}
@Override
public Type instantiate(Map<Type, Type> bindings) {
if (bindings.isEmpty()) {
return this;
}
Type[] params = new Type[0];
if (isParameterized()) {
params = new Type[fParameters.getArity()];
int i = 0;
for (Type p : fParameters) {
params[i++] = p.instantiate(bindings);
}
}
TypeStore store = new TypeStore();
store.declareAbstractDataType(this);
return TypeFactory.getInstance().abstractDataType(store, fName, params);
}
@Override
public String getName() {
return fName;
}
@Override
public Type getTypeParameters() {
return fParameters;
}
@Override
public Type getAbstractDataType() {
return this;
}
@Override
public <T, E extends Throwable> T accept(ITypeVisitor<T, E> visitor) throws E {
return visitor.visitAbstractData(this);
}
@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
return super.match(matched, bindings) && fParameters.match(matched.getTypeParameters(), bindings);
}
@Override
public IValue randomValue(Random random, IValueFactory vf, TypeStore store, Map<Type, Type> typeParameters,
int maxDepth, int maxWidth) {
IValue done = RandomUtil.randomADT(this, random, vf, store, typeParameters, maxDepth, maxWidth);
match(done.getType(), typeParameters);
return done;
}
@Override
public boolean isAbstractData() {
return true;
}
}