1 package org.simantics.scl.compiler.types;
3 import java.io.StringReader;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
9 import org.simantics.scl.compiler.errors.Locations;
10 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
11 import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
12 import org.simantics.scl.compiler.internal.types.HashConsing;
13 import org.simantics.scl.compiler.internal.types.TypeElaborationContext;
14 import org.simantics.scl.compiler.internal.types.effects.EffectIdMap;
15 import org.simantics.scl.compiler.types.exceptions.KindUnificationException;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
17 import org.simantics.scl.compiler.types.exceptions.Problem;
18 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
19 import org.simantics.scl.compiler.types.exceptions.UnificationException;
20 import org.simantics.scl.compiler.types.kinds.Kind;
21 import org.simantics.scl.compiler.types.kinds.Kinds;
22 import org.simantics.scl.compiler.types.util.ITypeEnvironment;
23 import org.simantics.scl.compiler.types.util.MultiApply;
24 import org.simantics.scl.compiler.types.util.MultiFunction;
25 import org.simantics.scl.compiler.types.util.TMultiApply;
26 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
27 import org.simantics.scl.compiler.types.util.Typed;
29 import gnu.trove.map.hash.THashMap;
30 import gnu.trove.set.hash.THashSet;
33 * An utility class for creating and manipulating types.
35 * @author Hannu Niemistö
39 private static final HashConsing<TCon> conCache =
40 new HashConsing<TCon>() {
41 protected boolean equals(TCon a, TCon b) {
42 return a.name.equals(b.name) && a.module.equals(b.module);
45 protected int hashCode(TCon obj) {
46 return obj.module.hashCode()*31 + obj.name.hashCode();
50 public static final String BUILTIN = "Builtin";
52 public static final TCon BOOLEAN = con(BUILTIN, "Boolean");
53 public static final TCon BYTE = con(BUILTIN, "Byte");
54 public static final TCon CHARACTER = con(BUILTIN, "Character");
55 public static final TCon SHORT = con(BUILTIN, "Short");
56 public static final TCon INTEGER = con(BUILTIN, "Integer");
57 public static final TCon LONG = con(BUILTIN, "Long");
58 public static final TCon FLOAT = con(BUILTIN, "Float");
59 public static final TCon DOUBLE = con(BUILTIN, "Double");
61 public static final TCon BOOLEAN_ARRAY = con(BUILTIN, "BooleanArray");
62 public static final TCon BYTE_ARRAY = con(BUILTIN, "ByteArray");
63 public static final TCon CHARACTER_ARRAY = con(BUILTIN, "CharacterArray");
64 public static final TCon SHORT_ARRAY = con(BUILTIN, "ShortArray");
65 public static final TCon INTEGER_ARRAY = con(BUILTIN, "IntegerArray");
66 public static final TCon LONG_ARRAY = con(BUILTIN, "LongArray");
67 public static final TCon FLOAT_ARRAY = con(BUILTIN, "FloatArray");
68 public static final TCon DOUBLE_ARRAY = con(BUILTIN, "DoubleArray");
70 public static final TCon STRING = con(BUILTIN, "String");
71 public static final TCon ARROW = con(BUILTIN, "->");
73 public static final TCon LIST = con(BUILTIN, "[]");
74 public static final TCon VECTOR = con(BUILTIN, "Vector");
75 public static final TCon MVECTOR = con(BUILTIN, "MVector");
76 public static final TCon MAYBE = con(BUILTIN, "Maybe");
77 public static final TCon ARRAY = con(BUILTIN, "Array");
78 public static final TCon UNIT = con(BUILTIN, "()");
80 public static final TCon PUNIT = con(BUILTIN, "@");
82 public static final TCon TYPE_PROXY = con(BUILTIN, "TypeProxy");
84 public static final TCon TYPEABLE = con(BUILTIN, "Typeable");
85 public static final TCon SERIALIZABLE = con(BUILTIN, "Serializable");
86 public static final TCon VEC_COMP = con(BUILTIN, "VecComp");
87 public static final TCon BINDING = con(BUILTIN, "Binding");
89 public static final TCon TYPE = con(BUILTIN, "Type");
91 public static final TCon DYNAMIC = con("Prelude", "Dynamic");
92 public static final TCon VARIANT = con(BUILTIN, "Variant");
94 public static final TCon ADDITIVE = con("Prelude", "Additive");
95 public static final TCon MONAD = con("Prelude", "Monad");
96 public static final TCon INTEGRAL = con("Prelude", "Integral");
97 public static final TCon RING = con("Prelude", "Ring");
98 public static final TCon ORDERED_RING = con("Prelude", "OrderedRing");
99 public static final TCon REAL = con("Prelude", "Real");
100 public static final TCon SHOW = con("Prelude", "Show");
101 public static final TCon EQ = con("Prelude", "Eq");
102 public static final TCon ORD = con("Prelude", "Ord");
103 public static final TCon HASHABLE = con("Prelude", "Hashable");
104 public static final TCon IO = con("Serialization", "IO");
106 public static final Type REF = con("Prelude", "Ref");
108 public static final TCon RANDOM = Types.con("Random", "Random");
109 public static final TCon READ_GRAPH = Types.con("Simantics/DB", "ReadGraph");
110 public static final TCon WRITE_GRAPH = Types.con("Simantics/DB", "WriteGraph");
111 public static final Type RESOURCE = Types.con("Simantics/DB", "Resource");
113 public static final TUnion NO_EFFECTS = new TUnion();
114 public static final TCon PROC = con(BUILTIN, "Proc");
116 public static final TCon BRANCH_POINT = con(BUILTIN, "BranchPoint");
118 private volatile static TCon[] tupleCache = new TCon[] {
122 private static final ITypeEnvironment DUMMY_TYPE_ENVIRONMENT = new ITypeEnvironment() {
125 public TCon resolve(String namespace, String name) {
126 if(namespace == null)
127 return con(BUILTIN, name);
129 return con(namespace, name);
134 public static boolean isPrimitive(Type type) {
135 return type == BOOLEAN || type == BYTE || type == CHARACTER || type == SHORT ||
136 type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE || type == STRING;
139 public static boolean isNumeric(Type type) {
140 return type == BYTE || type == SHORT || type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE;
143 public static TApply apply(Type function, Type parameter) {
144 return new TApply(function, parameter);
147 public static Type apply(Type function, Type ... parameters) {
148 for(Type parameter : parameters)
149 function = apply(function, parameter);
154 * Get the concrete type pointed to by a chain of type meta-variables.
156 public static Type canonical(Type type) {
157 while(type instanceof TMetaVar) {
158 TMetaVar metaVar = (TMetaVar)type;
166 public static Type closure(Type type, ArrayList<TVar> vars) {
167 for(int i=vars.size()-1;i>=0;--i)
168 type = forAll(vars.get(i), type);
172 public static Type closure(Type type, TVar[] vars) {
173 for(int i=vars.length-1;i>=0;--i)
174 type = forAll(vars[i], type);
178 public static Type closure(Type type) {
179 return closure(type, freeVars(type));
182 public static TCon con(String module, String name) {
183 return conCache.canonical(new TCon(module, name));
186 public static Type[] concat(Type[] a, Type[] b) {
191 Type[] result = new Type[a.length + b.length];
192 for(int i=0;i<a.length;++i)
194 for(int i=0;i<b.length;++i)
195 result[i+a.length] = b[i];
199 public static TVar[] concat(TVar[] a, TVar[] b) {
204 TVar[] result = new TVar[a.length + b.length];
205 for(int i=0;i<a.length;++i)
207 for(int i=0;i<b.length;++i)
208 result[i+a.length] = b[i];
212 public static boolean equals(TApply a, TApply b) {
213 return equals(a.parameter, b.parameter)
214 && equals(a.function , b.function );
217 public static boolean equals(TFun a, TFun b) {
218 return equals(a.domain, b.domain)
219 && equals(a.effect, b.effect)
220 && equals(a.range, b.range);
223 public static boolean subsumes(TFun a, TFun b) {
224 return subsumes(b.domain, a.domain)
225 && subsumesEffect(a.effect, b.effect)
226 && subsumes(a.range, b.range);
229 public static boolean subsumesEffect(Type a, Type b) {
230 EffectIdMap idMap = new EffectIdMap();
231 ArrayList<TMetaVar> mVars = new ArrayList<TMetaVar>(0);
232 int idA = idMap.toId(a, mVars);
233 int idB = idMap.toId(b, mVars);
234 return (idA&idB) == idA;
237 public static boolean equalsEffect(Type a, Type b) {
238 EffectIdMap idMap = new EffectIdMap();
239 ArrayList<TMetaVar> mVars = new ArrayList<TMetaVar>(0);
240 int idA = idMap.toId(a, mVars);
241 int idB = idMap.toId(b, mVars);
245 public static boolean equals(TForAll a, TForAll b) {
246 Kind aKind = a.var.getKind();
247 if(!Kinds.equalsCanonical(aKind, b.var.getKind()))
249 TVar newVar = var(aKind);
250 return equals(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
253 public static boolean equals(TPred a, TPred b) {
254 if(a.typeClass != b.typeClass
255 || a.parameters.length != b.parameters.length)
257 Type[] aParameters = a.parameters;
258 Type[] bParameters = b.parameters;
259 for(int i=0;i<aParameters.length;++i)
260 if(!equals(aParameters[i], bParameters[i]))
265 public static boolean equals(TUnion a, TUnion b) {
266 if(a.effects.length != b.effects.length)
268 for(int i=0;i<a.effects.length;++i)
269 if(!equals(a.effects[i], b.effects[i]))
275 * Tests equality of two types. Unbound TVars
276 * are equal only if they are the same variable.
277 * Bound TMetaVar is equal to the type it is bound to.
278 * Unbound TMetaVars are equal only if they are the same metavariable.
279 * Order of predicates and forall quantifiers matters.
281 public static boolean equals(Type a, Type b) {
286 Class<?> ca = a.getClass();
287 Class<?> cb = b.getClass();
290 if(ca == TApply.class)
291 return equals((TApply)a, (TApply)b);
292 else if(ca == TFun.class)
293 return equals((TFun)a, (TFun)b);
294 else if(ca == TForAll.class)
295 return equals((TForAll)a, (TForAll)b);
296 else if(ca == TPred.class)
297 return equals((TPred)a, (TPred)b);
298 else if(ca == TUnion.class)
299 return equals((TUnion)a, (TUnion)b);
300 else // ca == TCon.class
301 // || (ca == TMetaVar.class && a.ref == null && b.ref == null)
302 // || ca = TVar.class
303 return false; // Equals only if a == b, that was already tested
306 public static boolean subsumes(Type a, Type b) {
311 Class<?> ca = a.getClass();
312 Class<?> cb = b.getClass();
315 if(ca == TApply.class)
316 return equals((TApply)a, (TApply)b);
317 else if(ca == TFun.class)
318 return subsumes((TFun)a, (TFun)b);
319 else if(ca == TForAll.class) {
320 TForAll aForAll = (TForAll)a;
321 TForAll bForAll = (TForAll)b;
322 TVar newVar = var(aForAll.var.getKind());
323 return subsumes(aForAll.type.replace(aForAll.var, newVar),
324 bForAll.type.replace(bForAll.var, newVar));
326 else if(ca == TPred.class)
327 return equals((TPred)a, (TPred)b);
328 else if(ca == TUnion.class)
329 return equals((TUnion)a, (TUnion)b);
330 else // ca == TCon.class
331 // || (ca == TMetaVar.class && a.ref == null && b.ref == null)
332 // || ca = TVar.class
333 return false; // Equals only if a == b, that was already tested
336 public static TForAll forAll(TVar parameter, Type type) {
337 return new TForAll(parameter, type);
340 public static Type forAll(TVar[] parameters, Type type) {
341 for(int i=parameters.length-1;i>=0;--i)
342 type = forAll(parameters[i], type);
346 public static ArrayList<TVar> freeVars(Type type) {
347 ArrayList<TVar> vars = new ArrayList<TVar>(2);
348 type.collectFreeVars(vars);
352 public static ArrayList<TVar> freeVars(Type[] types) {
353 ArrayList<TVar> vars = new ArrayList<TVar>(2);
354 for(Type type : types)
355 type.collectFreeVars(vars);
359 public static TVar[] freeVarsArray(Type type) {
360 ArrayList<TVar> vars = freeVars(type);
361 return vars.toArray(new TVar[vars.size()]);
364 public static TVar[] freeVarsArray(Type[] types) {
365 ArrayList<TVar> vars = freeVars(types);
366 return vars.toArray(new TVar[vars.size()]);
369 public static TPred pred(TCon typeClass, Type ... parameters) {
370 return new TPred(typeClass, parameters);
373 public static Type function(Type ... types) {
374 Type result = types[types.length-1];
375 for(int i=types.length-2;i>=0;--i)
376 result = function(types[i], result);
380 public static Type function(Type from, Type to) {
381 return new TFun(from, Types.NO_EFFECTS, to);
384 public static Type function(Type[] from, Type to) {
385 for(int i=from.length-1;i>=0;--i)
386 to = function(from[i], to);
390 public static TFun functionE(Type from, Type effect, Type to) {
391 return new TFun(from, effect, to);
394 public static Type functionE(Type[] from, Type effect, Type to) {
395 for(int i=from.length-1;i>=0;--i) {
396 to = functionE(from[i], effect, to);
397 effect = Types.NO_EFFECTS;
402 public static Type removeForAll(Type type, ArrayList<TVar> vars) {
404 if(type instanceof TForAll) {
405 TForAll forAll = (TForAll)type;
407 vars.add(forAll.var);
409 else if(type instanceof TMetaVar) {
410 TMetaVar var = (TMetaVar)type;
421 public static Type removeForAll(Type type) {
423 if(type instanceof TForAll) {
424 TForAll forAll = (TForAll)type;
427 else if(type instanceof TMetaVar) {
428 TMetaVar var = (TMetaVar)type;
439 public static Type instantiate(TForAll forAll, ArrayList<TMetaVar> vars) {
440 TMetaVar metaVar = metaVar(forAll.var.getKind());
442 return instantiate(forAll.type.replace(forAll.var, metaVar), vars);
445 public static Type instantiate(Type type, ArrayList<TMetaVar> vars) {
447 throw new NullPointerException();
448 type = canonical(type);
449 if(type instanceof TForAll)
450 return instantiate((TForAll)type, vars);
455 public static Type list(Type parameter) {
456 return apply(LIST, parameter);
459 public static Type vector(Type parameter) {
460 return apply(VECTOR, parameter);
463 public static Type mvector(Type parameter) {
464 return apply(MVECTOR, parameter);
467 public static MultiFunction matchFunction(Type type, int arity) throws MatchException {
468 if (type instanceof TForAll)
469 return matchFunction(((TForAll)type).type, arity);
471 type = canonical(type);
472 /*while(type instanceof TForAll)
473 type = canonical(((TForAll)type).type);*/
474 Type[] parameterTypes = new Type[arity];
475 Type effect = Types.NO_EFFECTS;
476 for(int i=0;i<arity;++i) {
477 if(type instanceof TFun) {
478 TFun fun = (TFun)type;
479 parameterTypes[i] = fun.domain;
480 type = canonical(fun.range);
483 else if(Types.canonical(fun.effect) != Types.NO_EFFECTS)
484 throw new MatchException();
486 /*else if(type instanceof TMetaVar) {
487 TMetaVar metaVar = (TMetaVar)type;
488 type = Types.metaVar(Kinds.STAR);
489 Type template = type;
490 effect = Types.metaVar(Kinds.EFFECT);
491 for(int j=arity-1;j>=i;--j) {
492 Type pType = Types.metaVar(Kinds.STAR);
493 parameterTypes[j] = pType;
494 template = Types.functionE(pType,
495 j==arity-1 ? effect : Types.NO_EFFECTS,
499 metaVar.setRef(template);
500 } catch (UnificationException e) {
501 // Should never happen
502 throw new MatchException();
506 /*else if(type instanceof TApply) {
507 TApply apply1 = (TApply)type;
508 Type function1 = canonical(apply1.function);
509 if(function1 instanceof TApply) {
510 TApply apply2 = (TApply)function1;
511 Type function2 = canonical(apply2.function);
512 if(function2 == ARROW) {
513 result[i] = apply2.parameter;
514 type = canonical(apply1.parameter);
517 throw new MatchException();
520 throw new MatchException();
523 throw new MatchException();
525 return new MultiFunction(parameterTypes, effect, type);
528 public static boolean isApply(Type func, int arity, Type type) {
530 type = canonical(type);
531 if(!(type instanceof TApply))
533 type = ((TApply)type).function;
535 return equals(func, type);
538 public static Type matchApply(TCon func, Type type) throws MatchException {
539 type = canonical(type);
540 if(type instanceof TApply) {
541 TApply apply = (TApply)type;
542 Type f = canonical(apply.function);
544 return canonical(apply.parameter);
546 throw new MatchException();
549 public static MultiApply matchApply(Type type) {
550 ArrayList<Type> parameters = new ArrayList<Type>();
551 type = canonical(type);
552 while(type instanceof TApply) {
553 TApply apply = (TApply)type;
554 parameters.add(Types.canonical(apply.parameter));
555 type = canonical(apply.function);
557 return new MultiApply(type, parameters.toArray(new Type[parameters.size()]));
560 public static Type unifyApply(TCon func, Type type) throws MatchException {
561 type = canonical(type);
562 if(type instanceof TApply) {
563 TApply apply = (TApply)type;
564 Type f = canonical(apply.function);
566 return canonical(apply.parameter);
567 else if(f instanceof TMetaVar)
569 ((TMetaVar)f).setRef(func);
570 return canonical(apply.parameter);
571 } catch (UnificationException e) {
572 throw new MatchException();
575 else if(type instanceof TMetaVar) {
576 TMetaVar parameter = Types.metaVar(Kinds.metaVar());
578 ((TMetaVar) type).setRef(apply(func, parameter));
579 } catch (UnificationException e) {
580 throw new MatchException();
584 throw new MatchException();
587 public static MultiFunction matchFunction(Type type) {
588 type = canonical(type);
589 while(type instanceof TForAll)
590 type = canonical(((TForAll)type).type);
591 ArrayList<Type> parameterTypes = new ArrayList<Type>();
592 Type effect = Types.NO_EFFECTS;
594 if(type instanceof TFun) {
595 TFun fun = (TFun)type;
596 parameterTypes.add(fun.domain);
597 type = canonical(fun.range);
598 if(canonical(fun.effect) != Types.NO_EFFECTS) {
603 /*else if(type instanceof TApply) {
604 TApply apply1 = (TApply)type;
605 Type function1 = canonical(apply1.function);
606 if(function1 instanceof TApply) {
607 TApply apply2 = (TApply)function1;
608 Type function2 = canonical(apply2.function);
609 if(function2 == ARROW) {
610 types.add(apply2.parameter);
611 type = apply1.parameter;
627 return new MultiFunction(
628 parameterTypes.toArray(new Type[parameterTypes.size()]),
633 public static MultiFunction unifyFunction(Type type, int arity) throws UnificationException {
634 Type[] parameterTypes = new Type[arity];
635 for(int i=0;i<arity;++i)
636 parameterTypes[i] = metaVar(Kinds.STAR);
637 Type effect = metaVar(Kinds.EFFECT);
638 Type requiredType = metaVar(Kinds.STAR);
639 MultiFunction result = new MultiFunction(parameterTypes, effect, requiredType);
641 for(int i=arity-1;i>=0;--i) {
642 requiredType = functionE(parameterTypes[i], effect, requiredType);
643 effect = Types.NO_EFFECTS;
645 unify(type, requiredType);
649 private static Type getRangeIfFunction(Type type) {
650 type = canonical(type);
652 if(type instanceof TFun) {
653 return ((TFun)type).range;
655 /*else if(type instanceof TApply) {
656 TApply apply1 = (TApply)type;
657 Type f = canonical(apply1.function);
658 if(f instanceof TApply) {
659 if( canonical(((TApply)f).function) == Types.ARROW ) {
660 return apply1.parameter;
672 public static int getArity(Type type) {
675 type = getRangeIfFunction(type);
683 public static TMetaVar metaVar(Kind kind) {
684 return new TMetaVar(kind);
687 public static Type constrained(TPred constraint, Type type) {
688 return new TFun(constraint, Types.NO_EFFECTS, type);
691 public static Type constrained(TPred[] constraints, Type type) {
692 for(int i=constraints.length-1;i>=0;--i)
693 type = constrained(constraints[i], type);
697 public static TMultiApply toMultiApply(Type type) {
698 ArrayList<Type> parameters = new ArrayList<Type>();
699 type = canonical(type);
700 while(type instanceof TApply) {
701 TApply apply = (TApply)type;
702 parameters.add(apply.parameter);
703 type = canonical(apply.function);
705 Collections.reverse(parameters);
706 return new TMultiApply(type, parameters);
709 public static Type tuple(Type ... parameters) {
710 if(parameters.length == 1)
711 return parameters[0];
713 return apply(tupleConstructor(parameters.length), parameters);
716 public static TCon tupleConstructor(int arity) {
717 if(arity < 0 || arity == 1)
718 throw new IllegalArgumentException("The arity of a tuple cannot be " + arity + ".");
720 TCon[] oldTupleCache = tupleCache;
721 if(oldTupleCache.length <= arity) {
722 int oldLength = oldTupleCache.length;
723 int newLength = oldLength*2;
724 while(newLength <= arity)
726 TCon[] newTupleCache = Arrays.copyOf(oldTupleCache, newLength);
727 for(int i=oldLength;i<newLength;++i) {
728 StringBuilder b = new StringBuilder();
733 newTupleCache[i] = con(BUILTIN, b.toString());
735 TCon result = newTupleCache[arity];
736 tupleCache = newTupleCache;
740 return oldTupleCache[arity];
743 public static void unify(TFun a, TFun b) throws UnificationException {
744 unify(a.domain, b.domain);
745 unify(a.effect, b.effect);
746 unify(a.range, b.range);
749 public static void unify(TApply a, TApply b) throws UnificationException {
750 unify(a.function, b.function);
751 unify(a.parameter, b.parameter);
754 public static void unify(TForAll a, TForAll b) throws UnificationException {
756 Kinds.unify(a.var.getKind(), b.var.getKind());
757 } catch (KindUnificationException e) {
758 throw new UnificationException(a, b);
760 TVar newVar = var(a.var.getKind());
761 unify(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
764 public static void unify(TPred a, TPred b) throws UnificationException {
765 if(a.typeClass != b.typeClass
766 || a.parameters.length != b.parameters.length)
767 throw new UnificationException(a, b);
768 for(int i=0;i<a.parameters.length;++i)
769 unify(a.parameters[i], b.parameters[i]);
772 public static void unify(TUnion a, TUnion b) throws UnificationException {
773 if(a.effects.length != b.effects.length)
774 throw new UnificationException(a, b);
775 for(int i=0;i<a.effects.length;++i)
776 unify(a.effects[i], b.effects[i]);
779 public static void unify(Type a, Type b) throws UnificationException {
784 if(a instanceof TMetaVar) {
785 ((TMetaVar)a).setRef(b);
788 if(b instanceof TMetaVar) {
789 ((TMetaVar)b).setRef(a);
794 Class<?> ca = a.getClass();
795 Class<?> cb = b.getClass();
797 throw new UnificationException(a, b);
799 if(ca == TApply.class)
800 unify((TApply)a, (TApply)b);
801 else if(ca == TFun.class)
802 unify((TFun)a, (TFun)b);
803 else if(ca == TForAll.class)
804 unify((TForAll)a, (TForAll)b);
805 else if(ca == TPred.class)
806 unify((TPred)a, (TPred)b);
807 else if(ca == TUnion.class)
808 unify((TUnion)a, (TUnion)b);
809 else // ca == TCon.class || ca = TVar.class
810 throw new UnificationException(a, b);
813 public static TVar var(Kind kind) {
814 return new TVar(kind);
817 public static TVar[] vars(TVar[] otherVars) {
818 TVar[] vars = new TVar[otherVars.length];
819 for(int i=0;i<otherVars.length;++i)
820 vars[i] = var(otherVars[i].getKind());
824 public static Type instantiate(Type type, Type ... parameters) {
825 for(int i=0;i<parameters.length;++i) {
826 type = canonical(type);
827 if(!(type instanceof TForAll))
828 throw new IllegalArgumentException();
829 TForAll forAll = (TForAll)type;
830 type = forAll.type.replace(forAll.var, parameters[i]);
835 public static Type[] getTypes(Typed[] values) {
836 Type[] types = new Type[values.length];
837 for(int i=0;i<values.length;++i)
838 types[i] = values[i].getType();
843 * Matches b to a, i.e. finds a substitution such that a[substitution] = b.
844 * Unbound metavariables in b are consired as normal variables. It is assumed
845 * that a does not contain metavariables and b does not contain any type variables
846 * in a (no occurs checks needed).
849 * @param substitution
852 public static boolean match(Type a, Type b, THashMap<TVar, Type> substitution) {
855 Class<?> ca = a.getClass();
856 if(ca == TVar.class) {
858 Type t = substitution.get(ta);
860 substitution.put(ta, b); // no occurs check needed
864 return match(t, b, substitution);
868 Class<?> cb = b.getClass();
869 if(ca != cb || ca == TCon.class)
871 if(ca == TApply.class)
872 return match((TApply)a, (TApply)b, substitution);
873 else if(ca == TFun.class)
874 return match((TFun)a, (TFun)b, substitution);
875 else if(ca == TPred.class)
876 return match((TPred)a, (TPred)b, substitution);
878 throw new UnsupportedOperationException("match(" + a + ", " + b +") not supported"); // TForAll not supported
882 public static boolean match(TApply a, TApply b, THashMap<TVar, Type> substitution) {
883 return match(a.function, b.function, substitution) && match(a.parameter, b.parameter, substitution);
886 public static boolean match(TPred a, TPred b, THashMap<TVar, Type> substitution) {
887 if(a.typeClass != b.typeClass || a.parameters.length != b.parameters.length)
889 for(int i=0;i<a.parameters.length;++i)
890 if(!match(a.parameters[i], b.parameters[i], substitution))
895 public static boolean match(TFun a, TFun b, THashMap<TVar, Type> substitution) {
896 return match(a.domain, b.domain, substitution)
897 && match(a.effect, b.effect, substitution)
898 && match(a.range, b.range, substitution);
901 public static Type removePred(Type type,
902 ArrayList<TPred> preds) {
903 while(type instanceof TFun) {
904 TFun pred = (TFun)type;
905 if(!(pred.domain instanceof TPred))
907 preds.add((TPred)pred.domain);
908 type = canonical(pred.range);
913 public static <T extends Typed> Type[] getTypes(List<T> vars) {
914 Type[] result = new Type[vars.size()];
915 for(int i=0;i<result.length;++i)
916 result[i] = vars.get(i).getType();
920 public static boolean isBoxed(Type type) {
922 if(type instanceof TVar)
924 else if(type instanceof TApply) {
925 TApply apply = (TApply)type;
926 Type function = Types.canonical(apply.function);
927 if(function == Types.MAYBE || function == Types.MVECTOR || function == Types.VECTOR)
928 // FIXME Special case handled now here.
929 // The same problem is possibly with other types also!!!
930 type = apply.parameter;
934 else if(type instanceof TMetaVar) {
935 type = ((TMetaVar)type).ref;
939 else if(type instanceof TForAll) {
940 type = ((TForAll)type).type;
947 public static boolean isFunction(Type type) {
948 type = canonical(type);
949 return type instanceof TFun;
950 /*if(!(type instanceof TApply))
952 type = canonical(((TApply)type).function);
953 if(!(type instanceof TApply))
955 type = canonical(((TApply)type).function);
956 return type == ARROW;*/
959 public static boolean equals(Type[] as, Type[] bs) {
960 if(as.length != bs.length)
962 for(int i=0;i<as.length;++i)
963 if(!equals(as[i], bs[i]))
968 public static String toString(Type[] types) {
969 StringBuilder b = new StringBuilder();
970 TypeUnparsingContext tuc = new TypeUnparsingContext();
972 boolean first = true;
973 for(Type type : types) {
978 b.append(type.toString(tuc));
984 public static TCon getConstructor(Type type) throws MatchException {
986 if(type instanceof TCon)
988 else if(type instanceof TApply)
989 type = ((TApply)type).function;
990 else if(type instanceof TMetaVar) {
991 Type ref = ((TMetaVar)type).ref;
993 throw new MatchException();
997 throw new MatchException();
1001 public static Type[] replace(Type[] types, TVar[] from, Type[] to) {
1002 if(types.length == 0)
1003 return Type.EMPTY_ARRAY;
1004 Type[] result = new Type[types.length];
1005 for(int i=0;i<types.length;++i)
1006 result[i] = types[i].replace(from, to);
1010 public static <T extends Type> Type[] replace(Type[] types, THashMap<TVar, T> map) {
1011 if(types.length == 0)
1012 return Type.EMPTY_ARRAY;
1013 Type[] result = new Type[types.length];
1014 for(int i=0;i<types.length;++i)
1015 result[i] = types[i].replace(map);
1019 public static Type union(Type ... effects) {
1020 if(effects.length == 0)
1022 else if(effects.length == 1)
1025 return new TUnion(effects);
1028 public static Type union(List<Type> effects) {
1029 if(effects.size() == 0)
1031 else if(effects.size() == 1)
1032 return effects.get(0);
1034 return new TUnion(effects.toArray(new Type[effects.size()]));
1037 public static void canonize(Type[] types) {
1038 for(int i=0;i<types.length;++i)
1039 types[i] = canonical(types[i]);
1042 public static Type simplifyFinalEffect(Type effect) {
1043 effect = canonical(effect);
1044 if(effect instanceof TMetaVar) {
1046 //((TMetaVar) effect).setRef(Types.NO_EFFECTS);
1047 Type t = Types.var(Kinds.EFFECT);
1048 ((TMetaVar) effect).setRef(t);
1050 } catch (UnificationException e) {
1051 // Should not happen.
1052 throw new RuntimeException(e);
1055 if(effect instanceof TUnion) {
1056 TUnion union = (TUnion)effect;
1057 if(union.effects.length == 0)
1058 return Types.NO_EFFECTS;
1059 ArrayList<Type> effects = new ArrayList<Type>(union.effects.length);
1060 for(Type c : union.effects) {
1061 c = simplifyFinalEffect(c);
1062 if(c instanceof TUnion)
1063 for(Type c2 : ((TUnion)c).effects)
1068 return union(effects);
1073 public static Type simplifyType(Type effect) {
1074 effect = canonical(effect);
1075 if(effect instanceof TUnion) {
1076 TUnion union = (TUnion)effect;
1077 if(union.effects.length == 0)
1078 return Types.NO_EFFECTS;
1079 THashSet<Type> effects = new THashSet<Type>(union.effects.length);
1080 for(Type c : union.effects) {
1081 c = simplifyFinalEffect(c);
1082 if(c instanceof TUnion)
1083 for(Type c2 : ((TUnion)c).effects)
1088 return union(effects.toArray(new Type[effects.size()]));
1093 public static Type parseType(ITypeEnvironment environment, String text) throws SCLTypeParseException {
1094 return parseType(new TypeElaborationContext(environment), text);
1097 public static Type parseType(ITypeEnvironment environment, THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1098 return parseType(new TypeElaborationContext(localTypeVars, environment), text);
1101 public static Type parseType(String text) throws SCLTypeParseException {
1102 return parseType(new TypeElaborationContext(DUMMY_TYPE_ENVIRONMENT), text);
1105 public static Type parseType(THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1106 return parseType(new TypeElaborationContext(localTypeVars, DUMMY_TYPE_ENVIRONMENT), text);
1109 private static Type parseType(TypeElaborationContext context, String text) throws SCLTypeParseException {
1110 SCLParserImpl parser = new SCLParserImpl(new StringReader(text));
1112 org.simantics.scl.compiler.internal.parsing.types.TypeAst ast =
1113 (org.simantics.scl.compiler.internal.parsing.types.TypeAst)parser.parseType();
1114 return ast.toType(context);
1115 } catch (SCLSyntaxErrorException e) {
1116 throw new SCLTypeParseException(new Problem(
1117 Locations.beginOf(e.location),
1118 Locations.endOf(e.location),
1123 public static Type instantiateAndStrip(Type type) {
1125 if(type instanceof TForAll) {
1126 TForAll forAll = (TForAll)type;
1127 type = forAll.type.replace(forAll.var, metaVar(forAll.var.getKind()));
1129 else if(type instanceof TFun) {
1130 TFun fun = (TFun)type;
1131 if(fun.domain instanceof TPred || fun.domain == Types.PUNIT)
1136 else if(type instanceof TMetaVar) {
1137 TMetaVar metaVar = (TMetaVar)type;
1138 if(metaVar.ref == null)