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 ORD = con("Prelude", "Ord");
102 public static final TCon IO = con("Serialization", "IO");
104 public static final Type REF = con("Prelude", "Ref");
106 public static final TCon RANDOM = Types.con("Random", "Random");
107 public static final TCon READ_GRAPH = Types.con("Simantics/DB", "ReadGraph");
108 public static final TCon WRITE_GRAPH = Types.con("Simantics/DB", "WriteGraph");
109 public static final Type RESOURCE = Types.con("Simantics/DB", "Resource");
111 public static final TUnion NO_EFFECTS = new TUnion();
112 public static final TCon PROC = con(BUILTIN, "Proc");
114 public static final TCon BRANCH_POINT = con(BUILTIN, "BranchPoint");
116 private volatile static TCon[] tupleCache = new TCon[] {
120 private static final ITypeEnvironment DUMMY_TYPE_ENVIRONMENT = new ITypeEnvironment() {
123 public TCon resolve(String namespace, String name) {
124 if(namespace == null)
125 return con(BUILTIN, name);
127 return con(namespace, name);
132 public static boolean isPrimitive(Type type) {
133 return type == BOOLEAN || type == BYTE || type == CHARACTER || type == SHORT ||
134 type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE || type == STRING;
137 public static boolean isNumeric(Type type) {
138 return type == BYTE || type == SHORT || type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE;
141 public static TApply apply(Type function, Type parameter) {
142 return new TApply(function, parameter);
145 public static Type apply(Type function, Type ... parameters) {
146 for(Type parameter : parameters)
147 function = apply(function, parameter);
152 * Get the concrete type pointed to by a chain of type meta-variables.
154 public static Type canonical(Type type) {
155 if(type instanceof TMetaVar) {
156 TMetaVar metaVar = (TMetaVar)type;
161 return metaVar.ref = canonical(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 Type[] parametersArray;
558 if(parameters.isEmpty())
559 parametersArray = Type.EMPTY_ARRAY;
561 parametersArray = new Type[parameters.size()];
562 for(int i=0,j=parametersArray.length-1;i<parametersArray.length;++i,--j)
563 parametersArray[i] = parameters.get(j);
565 return new MultiApply(type, parametersArray);
568 public static Type unifyApply(TCon func, Type type) throws MatchException {
569 type = canonical(type);
570 if(type instanceof TApply) {
571 TApply apply = (TApply)type;
572 Type f = canonical(apply.function);
574 return canonical(apply.parameter);
575 else if(f instanceof TMetaVar)
577 ((TMetaVar)f).setRef(func);
578 return canonical(apply.parameter);
579 } catch (UnificationException e) {
580 throw new MatchException();
583 else if(type instanceof TMetaVar) {
584 TMetaVar parameter = Types.metaVar(Kinds.metaVar());
586 ((TMetaVar) type).setRef(apply(func, parameter));
587 } catch (UnificationException e) {
588 throw new MatchException();
592 throw new MatchException();
595 public static MultiFunction matchFunction(Type type) {
596 type = canonical(type);
597 while(type instanceof TForAll)
598 type = canonical(((TForAll)type).type);
599 ArrayList<Type> parameterTypes = new ArrayList<Type>();
600 Type effect = Types.NO_EFFECTS;
602 if(type instanceof TFun) {
603 TFun fun = (TFun)type;
604 parameterTypes.add(fun.domain);
605 type = canonical(fun.range);
606 if(canonical(fun.effect) != Types.NO_EFFECTS) {
611 /*else if(type instanceof TApply) {
612 TApply apply1 = (TApply)type;
613 Type function1 = canonical(apply1.function);
614 if(function1 instanceof TApply) {
615 TApply apply2 = (TApply)function1;
616 Type function2 = canonical(apply2.function);
617 if(function2 == ARROW) {
618 types.add(apply2.parameter);
619 type = apply1.parameter;
635 return new MultiFunction(
636 parameterTypes.toArray(new Type[parameterTypes.size()]),
641 public static MultiFunction unifyFunction(Type type, int arity) throws UnificationException {
642 Type[] parameterTypes = new Type[arity];
643 for(int i=0;i<arity;++i)
644 parameterTypes[i] = metaVar(Kinds.STAR);
645 Type effect = metaVar(Kinds.EFFECT);
646 Type requiredType = metaVar(Kinds.STAR);
647 MultiFunction result = new MultiFunction(parameterTypes, effect, requiredType);
649 for(int i=arity-1;i>=0;--i) {
650 requiredType = functionE(parameterTypes[i], effect, requiredType);
651 effect = Types.NO_EFFECTS;
653 unify(type, requiredType);
657 private static Type getRangeIfFunction(Type type) {
658 type = canonical(type);
660 if(type instanceof TFun) {
661 return ((TFun)type).range;
663 /*else if(type instanceof TApply) {
664 TApply apply1 = (TApply)type;
665 Type f = canonical(apply1.function);
666 if(f instanceof TApply) {
667 if( canonical(((TApply)f).function) == Types.ARROW ) {
668 return apply1.parameter;
680 public static int getArity(Type type) {
683 type = getRangeIfFunction(type);
691 public static TMetaVar metaVar(Kind kind) {
692 return new TMetaVar(kind);
695 public static Type constrained(TPred constraint, Type type) {
696 return new TFun(constraint, Types.NO_EFFECTS, type);
699 public static Type constrained(TPred[] constraints, Type type) {
700 for(int i=constraints.length-1;i>=0;--i)
701 type = constrained(constraints[i], type);
705 public static TMultiApply toMultiApply(Type type) {
706 ArrayList<Type> parameters = new ArrayList<Type>();
707 type = canonical(type);
708 while(type instanceof TApply) {
709 TApply apply = (TApply)type;
710 parameters.add(apply.parameter);
711 type = canonical(apply.function);
713 Collections.reverse(parameters);
714 return new TMultiApply(type, parameters);
717 public static Type tuple(Type ... parameters) {
718 if(parameters.length == 1)
719 return parameters[0];
721 return apply(tupleConstructor(parameters.length), parameters);
724 public static TCon tupleConstructor(int arity) {
725 if(arity < 0 || arity == 1)
726 throw new IllegalArgumentException("The arity of a tuple cannot be " + arity + ".");
728 TCon[] oldTupleCache = tupleCache;
729 if(oldTupleCache.length <= arity) {
730 int oldLength = oldTupleCache.length;
731 int newLength = oldLength*2;
732 while(newLength <= arity)
734 TCon[] newTupleCache = Arrays.copyOf(oldTupleCache, newLength);
735 for(int i=oldLength;i<newLength;++i) {
736 StringBuilder b = new StringBuilder();
741 newTupleCache[i] = con(BUILTIN, b.toString());
743 TCon result = newTupleCache[arity];
744 tupleCache = newTupleCache;
748 return oldTupleCache[arity];
751 public static void unify(TFun a, TFun b) throws UnificationException {
752 unify(a.domain, b.domain);
753 unify(a.effect, b.effect);
754 unify(a.range, b.range);
757 public static void unify(TApply a, TApply b) throws UnificationException {
758 unify(a.function, b.function);
759 unify(a.parameter, b.parameter);
762 public static void unify(TForAll a, TForAll b) throws UnificationException {
764 Kinds.unify(a.var.getKind(), b.var.getKind());
765 } catch (KindUnificationException e) {
766 throw new UnificationException(a, b);
768 TVar newVar = var(a.var.getKind());
769 unify(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
772 public static void unify(TPred a, TPred b) throws UnificationException {
773 if(a.typeClass != b.typeClass
774 || a.parameters.length != b.parameters.length)
775 throw new UnificationException(a, b);
776 for(int i=0;i<a.parameters.length;++i)
777 unify(a.parameters[i], b.parameters[i]);
780 public static void unify(TUnion a, TUnion b) throws UnificationException {
781 if(a.effects.length != b.effects.length)
782 throw new UnificationException(a, b);
783 for(int i=0;i<a.effects.length;++i)
784 unify(a.effects[i], b.effects[i]);
787 public static void unify(Type a, Type b) throws UnificationException {
792 if(a instanceof TMetaVar) {
793 ((TMetaVar)a).setRef(b);
796 if(b instanceof TMetaVar) {
797 ((TMetaVar)b).setRef(a);
802 Class<?> ca = a.getClass();
803 Class<?> cb = b.getClass();
805 throw new UnificationException(a, b);
807 if(ca == TApply.class)
808 unify((TApply)a, (TApply)b);
809 else if(ca == TFun.class)
810 unify((TFun)a, (TFun)b);
811 else if(ca == TForAll.class)
812 unify((TForAll)a, (TForAll)b);
813 else if(ca == TPred.class)
814 unify((TPred)a, (TPred)b);
815 else if(ca == TUnion.class)
816 unify((TUnion)a, (TUnion)b);
817 else // ca == TCon.class || ca = TVar.class
818 throw new UnificationException(a, b);
821 public static TVar var(Kind kind) {
822 return new TVar(kind);
825 public static TVar[] vars(TVar[] otherVars) {
826 TVar[] vars = new TVar[otherVars.length];
827 for(int i=0;i<otherVars.length;++i)
828 vars[i] = var(otherVars[i].getKind());
832 public static Type instantiate(Type type, Type ... parameters) {
833 for(int i=0;i<parameters.length;++i) {
834 type = canonical(type);
835 if(!(type instanceof TForAll))
836 throw new IllegalArgumentException();
837 TForAll forAll = (TForAll)type;
838 type = forAll.type.replace(forAll.var, parameters[i]);
843 public static Type[] getTypes(Typed[] values) {
844 Type[] types = new Type[values.length];
845 for(int i=0;i<values.length;++i)
846 types[i] = values[i].getType();
851 * Matches b to a, i.e. finds a substitution such that a[substitution] = b.
852 * Unbound metavariables in b are consired as normal variables. It is assumed
853 * that a does not contain metavariables and b does not contain any type variables
854 * in a (no occurs checks needed).
857 * @param substitution
860 public static boolean match(Type a, Type b, THashMap<TVar, Type> substitution) {
863 Class<?> ca = a.getClass();
864 if(ca == TVar.class) {
866 Type t = substitution.get(ta);
868 substitution.put(ta, b); // no occurs check needed
872 return match(t, b, substitution);
876 Class<?> cb = b.getClass();
877 if(ca != cb || ca == TCon.class)
879 if(ca == TApply.class)
880 return match((TApply)a, (TApply)b, substitution);
881 else if(ca == TFun.class)
882 return match((TFun)a, (TFun)b, substitution);
883 else if(ca == TPred.class)
884 return match((TPred)a, (TPred)b, substitution);
886 throw new UnsupportedOperationException("match(" + a + ", " + b +") not supported"); // TForAll not supported
890 public static boolean match(TApply a, TApply b, THashMap<TVar, Type> substitution) {
891 return match(a.function, b.function, substitution) && match(a.parameter, b.parameter, substitution);
894 public static boolean match(TPred a, TPred b, THashMap<TVar, Type> substitution) {
895 if(a.typeClass != b.typeClass || a.parameters.length != b.parameters.length)
897 for(int i=0;i<a.parameters.length;++i)
898 if(!match(a.parameters[i], b.parameters[i], substitution))
903 public static boolean match(TFun a, TFun b, THashMap<TVar, Type> substitution) {
904 return match(a.domain, b.domain, substitution)
905 && match(a.effect, b.effect, substitution)
906 && match(a.range, b.range, substitution);
909 public static Type removePred(Type type,
910 ArrayList<TPred> preds) {
911 while(type instanceof TFun) {
912 TFun pred = (TFun)type;
913 if(!(pred.domain instanceof TPred))
915 preds.add((TPred)pred.domain);
916 type = canonical(pred.range);
921 public static <T extends Typed> Type[] getTypes(List<T> vars) {
922 Type[] result = new Type[vars.size()];
923 for(int i=0;i<result.length;++i)
924 result[i] = vars.get(i).getType();
928 public static boolean isBoxed(Type type) {
930 if(type instanceof TVar)
932 else if(type instanceof TApply) {
933 TApply apply = (TApply)type;
934 Type function = Types.canonical(apply.function);
935 if(function == Types.MAYBE || function == Types.MVECTOR || function == Types.VECTOR)
936 // FIXME Special case handled now here.
937 // The same problem is possibly with other types also!!!
938 type = apply.parameter;
942 else if(type instanceof TMetaVar) {
943 type = ((TMetaVar)type).ref;
947 else if(type instanceof TForAll) {
948 type = ((TForAll)type).type;
955 public static boolean isFunction(Type type) {
956 type = canonical(type);
957 return type instanceof TFun;
958 /*if(!(type instanceof TApply))
960 type = canonical(((TApply)type).function);
961 if(!(type instanceof TApply))
963 type = canonical(((TApply)type).function);
964 return type == ARROW;*/
967 public static boolean equals(Type[] as, Type[] bs) {
968 if(as.length != bs.length)
970 for(int i=0;i<as.length;++i)
971 if(!equals(as[i], bs[i]))
976 public static String toString(Type[] types) {
977 StringBuilder b = new StringBuilder();
978 TypeUnparsingContext tuc = new TypeUnparsingContext();
980 boolean first = true;
981 for(Type type : types) {
986 b.append(type.toString(tuc));
992 public static TCon getConstructor(Type type) throws MatchException {
994 if(type instanceof TCon)
996 else if(type instanceof TApply)
997 type = ((TApply)type).function;
998 else if(type instanceof TMetaVar) {
999 Type ref = ((TMetaVar)type).ref;
1001 throw new MatchException();
1005 throw new MatchException();
1009 public static Type[] replace(Type[] types, TVar[] from, Type[] to) {
1010 if(types.length == 0)
1011 return Type.EMPTY_ARRAY;
1012 Type[] result = new Type[types.length];
1013 for(int i=0;i<types.length;++i)
1014 result[i] = types[i].replace(from, to);
1018 public static TPred[] replace(TPred[] types, TVar[] from, Type[] to) {
1019 if(types.length == 0)
1020 return TPred.EMPTY_ARRAY;
1021 TPred[] result = new TPred[types.length];
1022 for(int i=0;i<types.length;++i)
1023 result[i] = (TPred)types[i].replace(from, to);
1027 public static <T extends Type> Type[] replace(Type[] types, THashMap<TVar, T> map) {
1028 if(types.length == 0)
1029 return Type.EMPTY_ARRAY;
1030 Type[] result = new Type[types.length];
1031 for(int i=0;i<types.length;++i)
1032 result[i] = types[i].replace(map);
1036 public static Type union(Type ... effects) {
1037 if(effects.length == 0)
1039 else if(effects.length == 1)
1042 return new TUnion(effects);
1045 public static Type union(Type effect1, Type effect2) {
1046 return new TUnion(effect1, effect2);
1049 public static Type union(List<Type> effects) {
1050 if(effects.size() == 0)
1052 else if(effects.size() == 1)
1053 return effects.get(0);
1055 return new TUnion(effects.toArray(new Type[effects.size()]));
1058 public static void canonize(Type[] types) {
1059 for(int i=0;i<types.length;++i)
1060 types[i] = canonical(types[i]);
1063 public static Type simplifyFinalEffect(Type effect) {
1064 effect = canonical(effect);
1065 if(effect instanceof TMetaVar) {
1067 //((TMetaVar) effect).setRef(Types.NO_EFFECTS);
1068 Type t = Types.var(Kinds.EFFECT);
1069 ((TMetaVar) effect).setRef(t);
1071 } catch (UnificationException e) {
1072 // Should not happen.
1073 throw new RuntimeException(e);
1076 if(effect instanceof TUnion) {
1077 TUnion union = (TUnion)effect;
1078 if(union.effects.length == 0)
1079 return Types.NO_EFFECTS;
1080 ArrayList<Type> effects = new ArrayList<Type>(union.effects.length);
1081 for(Type c : union.effects) {
1082 c = simplifyFinalEffect(c);
1083 if(c instanceof TUnion)
1084 for(Type c2 : ((TUnion)c).effects)
1089 return union(effects);
1094 public static Type simplifyType(Type effect) {
1095 effect = canonical(effect);
1096 if(effect instanceof TUnion) {
1097 TUnion union = (TUnion)effect;
1098 if(union.effects.length == 0)
1099 return Types.NO_EFFECTS;
1100 THashSet<Type> effects = new THashSet<Type>(union.effects.length);
1101 for(Type c : union.effects) {
1102 c = simplifyFinalEffect(c);
1103 if(c instanceof TUnion)
1104 for(Type c2 : ((TUnion)c).effects)
1109 return union(effects.toArray(new Type[effects.size()]));
1114 public static Type parseType(ITypeEnvironment environment, String text) throws SCLTypeParseException {
1115 return parseType(new TypeElaborationContext(environment), text);
1118 public static Type parseType(ITypeEnvironment environment, THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1119 return parseType(new TypeElaborationContext(localTypeVars, environment), text);
1122 public static Type parseType(String text) throws SCLTypeParseException {
1123 return parseType(new TypeElaborationContext(DUMMY_TYPE_ENVIRONMENT), text);
1126 public static Type parseType(THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1127 return parseType(new TypeElaborationContext(localTypeVars, DUMMY_TYPE_ENVIRONMENT), text);
1130 private static Type parseType(TypeElaborationContext context, String text) throws SCLTypeParseException {
1131 SCLParserImpl parser = new SCLParserImpl(new StringReader(text));
1133 org.simantics.scl.compiler.internal.parsing.types.TypeAst ast =
1134 (org.simantics.scl.compiler.internal.parsing.types.TypeAst)parser.parseType();
1135 return ast.toType(context);
1136 } catch (SCLSyntaxErrorException e) {
1137 throw new SCLTypeParseException(new Problem(
1138 Locations.beginOf(e.location),
1139 Locations.endOf(e.location),
1144 public static Type instantiateAndStrip(Type type) {
1146 if(type instanceof TForAll) {
1147 TForAll forAll = (TForAll)type;
1148 type = forAll.type.replace(forAll.var, metaVar(forAll.var.getKind()));
1150 else if(type instanceof TFun) {
1151 TFun fun = (TFun)type;
1152 if(fun.domain instanceof TPred || fun.domain == Types.PUNIT)
1157 else if(type instanceof TMetaVar) {
1158 TMetaVar metaVar = (TMetaVar)type;
1159 if(metaVar.ref == null)