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 public static final TCon CHRContext = con(BUILTIN, "CHRContext");
119 private volatile static TCon[] tupleCache = new TCon[] {
123 private static final ITypeEnvironment DUMMY_TYPE_ENVIRONMENT = new ITypeEnvironment() {
126 public TCon resolve(String namespace, String name) {
127 if(namespace == null)
128 return con(BUILTIN, name);
130 return con(namespace, name);
135 public static boolean isPrimitive(Type type) {
136 return type == BOOLEAN || type == BYTE || type == CHARACTER || type == SHORT ||
137 type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE || type == STRING;
140 public static boolean isNumeric(Type type) {
141 return type == BYTE || type == SHORT || type == INTEGER || type == LONG || type == FLOAT || type == DOUBLE;
144 public static TApply apply(Type function, Type parameter) {
145 return new TApply(function, parameter);
148 public static Type apply(Type function, Type ... parameters) {
149 for(Type parameter : parameters)
150 function = apply(function, parameter);
155 * Get the concrete type pointed to by a chain of type meta-variables.
157 public static Type canonical(Type type) {
158 if(type instanceof TMetaVar) {
159 TMetaVar metaVar = (TMetaVar)type;
164 return metaVar.ref = canonical(type);
169 public static Type closure(Type type, ArrayList<TVar> vars) {
170 for(int i=vars.size()-1;i>=0;--i)
171 type = forAll(vars.get(i), type);
175 public static Type closure(Type type, TVar[] vars) {
176 for(int i=vars.length-1;i>=0;--i)
177 type = forAll(vars[i], type);
181 public static Type closure(Type type) {
182 return closure(type, freeVars(type));
185 public static TCon con(String module, String name) {
186 return conCache.canonical(new TCon(module, name));
189 public static Type[] concat(Type[] a, Type[] b) {
194 Type[] result = new Type[a.length + b.length];
195 for(int i=0;i<a.length;++i)
197 for(int i=0;i<b.length;++i)
198 result[i+a.length] = b[i];
202 public static TVar[] concat(TVar[] a, TVar[] b) {
207 TVar[] result = new TVar[a.length + b.length];
208 for(int i=0;i<a.length;++i)
210 for(int i=0;i<b.length;++i)
211 result[i+a.length] = b[i];
215 public static boolean equals(TApply a, TApply b) {
216 return equals(a.parameter, b.parameter)
217 && equals(a.function , b.function );
220 public static boolean equals(TFun a, TFun b) {
221 return equals(a.domain, b.domain)
222 && equals(a.effect, b.effect)
223 && equals(a.range, b.range);
226 public static boolean subsumes(TFun a, TFun b) {
227 return subsumes(b.domain, a.domain)
228 && subsumesEffect(a.effect, b.effect)
229 && subsumes(a.range, b.range);
232 public static boolean subsumesEffect(Type a, Type b) {
233 EffectIdMap idMap = new EffectIdMap();
234 ArrayList<TMetaVar> mVars = new ArrayList<TMetaVar>(0);
235 int idA = idMap.toId(a, mVars);
236 int idB = idMap.toId(b, mVars);
237 return (idA&idB) == idA;
240 public static boolean equalsEffect(Type a, Type b) {
241 EffectIdMap idMap = new EffectIdMap();
242 ArrayList<TMetaVar> mVars = new ArrayList<TMetaVar>(0);
243 int idA = idMap.toId(a, mVars);
244 int idB = idMap.toId(b, mVars);
248 public static boolean equals(TForAll a, TForAll b) {
249 Kind aKind = a.var.getKind();
250 if(!Kinds.equalsCanonical(aKind, b.var.getKind()))
252 TVar newVar = var(aKind);
253 return equals(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
256 public static boolean equals(TPred a, TPred b) {
257 if(a.typeClass != b.typeClass
258 || a.parameters.length != b.parameters.length)
260 Type[] aParameters = a.parameters;
261 Type[] bParameters = b.parameters;
262 for(int i=0;i<aParameters.length;++i)
263 if(!equals(aParameters[i], bParameters[i]))
268 public static boolean equals(TUnion a, TUnion b) {
269 if(a.effects.length != b.effects.length)
271 for(int i=0;i<a.effects.length;++i)
272 if(!equals(a.effects[i], b.effects[i]))
278 * Tests equality of two types. Unbound TVars
279 * are equal only if they are the same variable.
280 * Bound TMetaVar is equal to the type it is bound to.
281 * Unbound TMetaVars are equal only if they are the same metavariable.
282 * Order of predicates and forall quantifiers matters.
284 public static boolean equals(Type a, Type b) {
289 Class<?> ca = a.getClass();
290 Class<?> cb = b.getClass();
293 if(ca == TApply.class)
294 return equals((TApply)a, (TApply)b);
295 else if(ca == TFun.class)
296 return equals((TFun)a, (TFun)b);
297 else if(ca == TForAll.class)
298 return equals((TForAll)a, (TForAll)b);
299 else if(ca == TPred.class)
300 return equals((TPred)a, (TPred)b);
301 else if(ca == TUnion.class)
302 return equals((TUnion)a, (TUnion)b);
303 else // ca == TCon.class
304 // || (ca == TMetaVar.class && a.ref == null && b.ref == null)
305 // || ca = TVar.class
306 return false; // Equals only if a == b, that was already tested
309 public static boolean subsumes(Type a, Type b) {
314 Class<?> ca = a.getClass();
315 Class<?> cb = b.getClass();
318 if(ca == TApply.class)
319 return equals((TApply)a, (TApply)b);
320 else if(ca == TFun.class)
321 return subsumes((TFun)a, (TFun)b);
322 else if(ca == TForAll.class) {
323 TForAll aForAll = (TForAll)a;
324 TForAll bForAll = (TForAll)b;
325 TVar newVar = var(aForAll.var.getKind());
326 return subsumes(aForAll.type.replace(aForAll.var, newVar),
327 bForAll.type.replace(bForAll.var, newVar));
329 else if(ca == TPred.class)
330 return equals((TPred)a, (TPred)b);
331 else if(ca == TUnion.class)
332 return equals((TUnion)a, (TUnion)b);
333 else // ca == TCon.class
334 // || (ca == TMetaVar.class && a.ref == null && b.ref == null)
335 // || ca = TVar.class
336 return false; // Equals only if a == b, that was already tested
339 public static TForAll forAll(TVar parameter, Type type) {
340 return new TForAll(parameter, type);
343 public static Type forAll(TVar[] parameters, Type type) {
344 for(int i=parameters.length-1;i>=0;--i)
345 type = forAll(parameters[i], type);
349 public static ArrayList<TVar> freeVars(Type type) {
350 ArrayList<TVar> vars = new ArrayList<TVar>(2);
351 type.collectFreeVars(vars);
355 public static ArrayList<TVar> freeVars(Type[] types) {
356 ArrayList<TVar> vars = new ArrayList<TVar>(2);
357 for(Type type : types)
358 type.collectFreeVars(vars);
362 public static TVar[] freeVarsArray(Type type) {
363 ArrayList<TVar> vars = freeVars(type);
364 return vars.toArray(new TVar[vars.size()]);
367 public static TVar[] freeVarsArray(Type[] types) {
368 ArrayList<TVar> vars = freeVars(types);
369 return vars.toArray(new TVar[vars.size()]);
372 public static TPred pred(TCon typeClass, Type ... parameters) {
373 return new TPred(typeClass, parameters);
376 public static Type function(Type ... types) {
377 Type result = types[types.length-1];
378 for(int i=types.length-2;i>=0;--i)
379 result = function(types[i], result);
383 public static Type function(Type from, Type to) {
384 return new TFun(from, Types.NO_EFFECTS, to);
387 public static Type function(Type[] from, Type to) {
388 for(int i=from.length-1;i>=0;--i)
389 to = function(from[i], to);
393 public static TFun functionE(Type from, Type effect, Type to) {
394 return new TFun(from, effect, to);
397 public static Type functionE(Type[] from, Type effect, Type to) {
398 for(int i=from.length-1;i>=0;--i) {
399 to = functionE(from[i], effect, to);
400 effect = Types.NO_EFFECTS;
405 public static Type removeForAll(Type type, ArrayList<TVar> vars) {
407 if(type instanceof TForAll) {
408 TForAll forAll = (TForAll)type;
410 vars.add(forAll.var);
412 else if(type instanceof TMetaVar) {
413 TMetaVar var = (TMetaVar)type;
424 public static Type removeForAll(Type type) {
426 if(type instanceof TForAll) {
427 TForAll forAll = (TForAll)type;
430 else if(type instanceof TMetaVar) {
431 TMetaVar var = (TMetaVar)type;
442 public static Type instantiate(TForAll forAll, ArrayList<TMetaVar> vars) {
443 TMetaVar metaVar = metaVar(forAll.var.getKind());
445 return instantiate(forAll.type.replace(forAll.var, metaVar), vars);
448 public static Type instantiate(Type type, ArrayList<TMetaVar> vars) {
450 throw new NullPointerException();
451 type = canonical(type);
452 if(type instanceof TForAll)
453 return instantiate((TForAll)type, vars);
458 public static Type list(Type parameter) {
459 return apply(LIST, parameter);
462 public static Type vector(Type parameter) {
463 return apply(VECTOR, parameter);
466 public static Type mvector(Type parameter) {
467 return apply(MVECTOR, parameter);
470 public static MultiFunction matchFunction(Type type, int arity) throws MatchException {
471 if (type instanceof TForAll)
472 return matchFunction(((TForAll)type).type, arity);
474 type = canonical(type);
475 /*while(type instanceof TForAll)
476 type = canonical(((TForAll)type).type);*/
477 Type[] parameterTypes = new Type[arity];
478 Type effect = Types.NO_EFFECTS;
479 for(int i=0;i<arity;++i) {
480 if(type instanceof TFun) {
481 TFun fun = (TFun)type;
482 parameterTypes[i] = fun.domain;
483 type = canonical(fun.range);
486 else if(Types.canonical(fun.effect) != Types.NO_EFFECTS)
487 throw new MatchException();
489 /*else if(type instanceof TMetaVar) {
490 TMetaVar metaVar = (TMetaVar)type;
491 type = Types.metaVar(Kinds.STAR);
492 Type template = type;
493 effect = Types.metaVar(Kinds.EFFECT);
494 for(int j=arity-1;j>=i;--j) {
495 Type pType = Types.metaVar(Kinds.STAR);
496 parameterTypes[j] = pType;
497 template = Types.functionE(pType,
498 j==arity-1 ? effect : Types.NO_EFFECTS,
502 metaVar.setRef(template);
503 } catch (UnificationException e) {
504 // Should never happen
505 throw new MatchException();
509 /*else if(type instanceof TApply) {
510 TApply apply1 = (TApply)type;
511 Type function1 = canonical(apply1.function);
512 if(function1 instanceof TApply) {
513 TApply apply2 = (TApply)function1;
514 Type function2 = canonical(apply2.function);
515 if(function2 == ARROW) {
516 result[i] = apply2.parameter;
517 type = canonical(apply1.parameter);
520 throw new MatchException();
523 throw new MatchException();
526 throw new MatchException();
528 return new MultiFunction(parameterTypes, effect, type);
531 public static boolean isApply(Type func, int arity, Type type) {
533 type = canonical(type);
534 if(!(type instanceof TApply))
536 type = ((TApply)type).function;
538 return equals(func, type);
541 public static Type matchApply(TCon func, Type type) throws MatchException {
542 type = canonical(type);
543 if(type instanceof TApply) {
544 TApply apply = (TApply)type;
545 Type f = canonical(apply.function);
547 return canonical(apply.parameter);
549 throw new MatchException();
552 public static MultiApply matchApply(Type type) {
553 ArrayList<Type> parameters = new ArrayList<Type>();
554 type = canonical(type);
555 while(type instanceof TApply) {
556 TApply apply = (TApply)type;
557 parameters.add(Types.canonical(apply.parameter));
558 type = canonical(apply.function);
560 Type[] parametersArray;
561 if(parameters.isEmpty())
562 parametersArray = Type.EMPTY_ARRAY;
564 parametersArray = new Type[parameters.size()];
565 for(int i=0,j=parametersArray.length-1;i<parametersArray.length;++i,--j)
566 parametersArray[i] = parameters.get(j);
568 return new MultiApply(type, parametersArray);
571 public static Type unifyApply(TCon func, Type type) throws MatchException {
572 type = canonical(type);
573 if(type instanceof TApply) {
574 TApply apply = (TApply)type;
575 Type f = canonical(apply.function);
577 return canonical(apply.parameter);
578 else if(f instanceof TMetaVar)
580 ((TMetaVar)f).setRef(func);
581 return canonical(apply.parameter);
582 } catch (UnificationException e) {
583 throw new MatchException();
586 else if(type instanceof TMetaVar) {
587 TMetaVar parameter = Types.metaVar(Kinds.metaVar());
589 ((TMetaVar) type).setRef(apply(func, parameter));
590 } catch (UnificationException e) {
591 throw new MatchException();
595 throw new MatchException();
598 public static MultiFunction matchFunction(Type type) {
599 type = canonical(type);
600 while(type instanceof TForAll)
601 type = canonical(((TForAll)type).type);
602 ArrayList<Type> parameterTypes = new ArrayList<Type>();
603 Type effect = Types.NO_EFFECTS;
605 if(type instanceof TFun) {
606 TFun fun = (TFun)type;
607 parameterTypes.add(fun.domain);
608 type = canonical(fun.range);
609 if(canonical(fun.effect) != Types.NO_EFFECTS) {
614 /*else if(type instanceof TApply) {
615 TApply apply1 = (TApply)type;
616 Type function1 = canonical(apply1.function);
617 if(function1 instanceof TApply) {
618 TApply apply2 = (TApply)function1;
619 Type function2 = canonical(apply2.function);
620 if(function2 == ARROW) {
621 types.add(apply2.parameter);
622 type = apply1.parameter;
638 return new MultiFunction(
639 parameterTypes.toArray(new Type[parameterTypes.size()]),
644 public static MultiFunction unifyFunction(Type type, int arity) throws UnificationException {
645 Type[] parameterTypes = new Type[arity];
646 for(int i=0;i<arity;++i)
647 parameterTypes[i] = metaVar(Kinds.STAR);
648 Type effect = metaVar(Kinds.EFFECT);
649 Type requiredType = metaVar(Kinds.STAR);
650 MultiFunction result = new MultiFunction(parameterTypes, effect, requiredType);
652 for(int i=arity-1;i>=0;--i) {
653 requiredType = functionE(parameterTypes[i], effect, requiredType);
654 effect = Types.NO_EFFECTS;
656 unify(type, requiredType);
660 private static Type getRangeIfFunction(Type type) {
661 type = canonical(type);
663 if(type instanceof TFun) {
664 return ((TFun)type).range;
666 /*else if(type instanceof TApply) {
667 TApply apply1 = (TApply)type;
668 Type f = canonical(apply1.function);
669 if(f instanceof TApply) {
670 if( canonical(((TApply)f).function) == Types.ARROW ) {
671 return apply1.parameter;
683 public static int getArity(Type type) {
686 type = getRangeIfFunction(type);
694 public static TMetaVar metaVar(Kind kind) {
695 return new TMetaVar(kind);
698 public static Type constrained(TPred constraint, Type type) {
699 return new TFun(constraint, Types.NO_EFFECTS, type);
702 public static Type constrained(TPred[] constraints, Type type) {
703 for(int i=constraints.length-1;i>=0;--i)
704 type = constrained(constraints[i], type);
708 public static TMultiApply toMultiApply(Type type) {
709 ArrayList<Type> parameters = new ArrayList<Type>();
710 type = canonical(type);
711 while(type instanceof TApply) {
712 TApply apply = (TApply)type;
713 parameters.add(apply.parameter);
714 type = canonical(apply.function);
716 Collections.reverse(parameters);
717 return new TMultiApply(type, parameters);
720 public static Type tuple(Type ... parameters) {
721 if(parameters.length == 1)
722 return parameters[0];
724 return apply(tupleConstructor(parameters.length), parameters);
727 public static TCon tupleConstructor(int arity) {
728 if(arity < 0 || arity == 1)
729 throw new IllegalArgumentException("The arity of a tuple cannot be " + arity + ".");
731 TCon[] oldTupleCache = tupleCache;
732 if(oldTupleCache.length <= arity) {
733 int oldLength = oldTupleCache.length;
734 int newLength = oldLength*2;
735 while(newLength <= arity)
737 TCon[] newTupleCache = Arrays.copyOf(oldTupleCache, newLength);
738 for(int i=oldLength;i<newLength;++i) {
739 StringBuilder b = new StringBuilder();
744 newTupleCache[i] = con(BUILTIN, b.toString());
746 TCon result = newTupleCache[arity];
747 tupleCache = newTupleCache;
751 return oldTupleCache[arity];
754 public static void unify(TFun a, TFun b) throws UnificationException {
755 unify(a.domain, b.domain);
756 unify(a.effect, b.effect);
757 unify(a.range, b.range);
760 public static void unify(TApply a, TApply b) throws UnificationException {
761 unify(a.function, b.function);
762 unify(a.parameter, b.parameter);
765 public static void unify(TForAll a, TForAll b) throws UnificationException {
767 Kinds.unify(a.var.getKind(), b.var.getKind());
768 } catch (KindUnificationException e) {
769 throw new UnificationException(a, b);
771 TVar newVar = var(a.var.getKind());
772 unify(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
775 public static void unify(TPred a, TPred b) throws UnificationException {
776 if(a.typeClass != b.typeClass
777 || a.parameters.length != b.parameters.length)
778 throw new UnificationException(a, b);
779 for(int i=0;i<a.parameters.length;++i)
780 unify(a.parameters[i], b.parameters[i]);
783 public static void unify(TUnion a, TUnion b) throws UnificationException {
784 if(a.effects.length != b.effects.length)
785 throw new UnificationException(a, b);
786 for(int i=0;i<a.effects.length;++i)
787 unify(a.effects[i], b.effects[i]);
790 public static void unify(Type a, Type b) throws UnificationException {
795 if(a instanceof TMetaVar) {
796 ((TMetaVar)a).setRef(b);
799 if(b instanceof TMetaVar) {
800 ((TMetaVar)b).setRef(a);
805 Class<?> ca = a.getClass();
806 Class<?> cb = b.getClass();
808 throw new UnificationException(a, b);
810 if(ca == TApply.class)
811 unify((TApply)a, (TApply)b);
812 else if(ca == TFun.class)
813 unify((TFun)a, (TFun)b);
814 else if(ca == TForAll.class)
815 unify((TForAll)a, (TForAll)b);
816 else if(ca == TPred.class)
817 unify((TPred)a, (TPred)b);
818 else if(ca == TUnion.class)
819 unify((TUnion)a, (TUnion)b);
820 else // ca == TCon.class || ca = TVar.class
821 throw new UnificationException(a, b);
824 public static TVar var(Kind kind) {
825 return new TVar(kind);
828 public static TVar[] vars(TVar[] otherVars) {
829 TVar[] vars = new TVar[otherVars.length];
830 for(int i=0;i<otherVars.length;++i)
831 vars[i] = var(otherVars[i].getKind());
835 public static Type instantiate(Type type, Type ... parameters) {
836 for(int i=0;i<parameters.length;++i) {
837 type = canonical(type);
838 if(!(type instanceof TForAll))
839 throw new IllegalArgumentException();
840 TForAll forAll = (TForAll)type;
841 type = forAll.type.replace(forAll.var, parameters[i]);
846 public static Type[] getTypes(Typed[] values) {
847 Type[] types = new Type[values.length];
848 for(int i=0;i<values.length;++i)
849 types[i] = values[i].getType();
854 * Matches b to a, i.e. finds a substitution such that a[substitution] = b.
855 * Unbound metavariables in b are consired as normal variables. It is assumed
856 * that a does not contain metavariables and b does not contain any type variables
857 * in a (no occurs checks needed).
860 * @param substitution
863 public static boolean match(Type a, Type b, THashMap<TVar, Type> substitution) {
866 Class<?> ca = a.getClass();
867 if(ca == TVar.class) {
869 Type t = substitution.get(ta);
871 substitution.put(ta, b); // no occurs check needed
875 return match(t, b, substitution);
879 Class<?> cb = b.getClass();
880 if(ca != cb || ca == TCon.class)
882 if(ca == TApply.class)
883 return match((TApply)a, (TApply)b, substitution);
884 else if(ca == TFun.class)
885 return match((TFun)a, (TFun)b, substitution);
886 else if(ca == TPred.class)
887 return match((TPred)a, (TPred)b, substitution);
889 throw new UnsupportedOperationException("match(" + a + ", " + b +") not supported"); // TForAll not supported
893 public static boolean match(TApply a, TApply b, THashMap<TVar, Type> substitution) {
894 return match(a.function, b.function, substitution) && match(a.parameter, b.parameter, substitution);
897 public static boolean match(TPred a, TPred b, THashMap<TVar, Type> substitution) {
898 if(a.typeClass != b.typeClass || a.parameters.length != b.parameters.length)
900 for(int i=0;i<a.parameters.length;++i)
901 if(!match(a.parameters[i], b.parameters[i], substitution))
906 public static boolean match(TFun a, TFun b, THashMap<TVar, Type> substitution) {
907 return match(a.domain, b.domain, substitution)
908 && match(a.effect, b.effect, substitution)
909 && match(a.range, b.range, substitution);
912 public static Type removePred(Type type,
913 ArrayList<TPred> preds) {
914 while(type instanceof TFun) {
915 TFun pred = (TFun)type;
916 if(!(pred.domain instanceof TPred))
918 preds.add((TPred)pred.domain);
919 type = canonical(pred.range);
924 public static <T extends Typed> Type[] getTypes(List<T> vars) {
925 Type[] result = new Type[vars.size()];
926 for(int i=0;i<result.length;++i)
927 result[i] = vars.get(i).getType();
931 public static boolean isBoxed(Type type) {
933 if(type instanceof TVar)
935 else if(type instanceof TApply) {
936 TApply apply = (TApply)type;
937 Type function = Types.canonical(apply.function);
938 if(function == Types.MAYBE || function == Types.MVECTOR || function == Types.VECTOR)
939 // FIXME Special case handled now here.
940 // The same problem is possibly with other types also!!!
941 type = apply.parameter;
945 else if(type instanceof TMetaVar) {
946 type = ((TMetaVar)type).ref;
950 else if(type instanceof TForAll) {
951 type = ((TForAll)type).type;
958 public static boolean isFunction(Type type) {
959 type = canonical(type);
960 return type instanceof TFun;
961 /*if(!(type instanceof TApply))
963 type = canonical(((TApply)type).function);
964 if(!(type instanceof TApply))
966 type = canonical(((TApply)type).function);
967 return type == ARROW;*/
970 public static boolean equals(Type[] as, Type[] bs) {
971 if(as.length != bs.length)
973 for(int i=0;i<as.length;++i)
974 if(!equals(as[i], bs[i]))
979 public static String toString(Type[] types) {
980 StringBuilder b = new StringBuilder();
981 TypeUnparsingContext tuc = new TypeUnparsingContext();
983 boolean first = true;
984 for(Type type : types) {
989 b.append(type.toString(tuc));
995 public static TCon getConstructor(Type type) throws MatchException {
997 if(type instanceof TCon)
999 else if(type instanceof TApply)
1000 type = ((TApply)type).function;
1001 else if(type instanceof TMetaVar) {
1002 Type ref = ((TMetaVar)type).ref;
1004 throw new MatchException();
1008 throw new MatchException();
1012 public static Type[] replace(Type[] types, TVar[] from, Type[] to) {
1013 if(types.length == 0)
1014 return Type.EMPTY_ARRAY;
1015 Type[] result = new Type[types.length];
1016 for(int i=0;i<types.length;++i)
1017 result[i] = types[i].replace(from, to);
1021 public static TPred[] replace(TPred[] types, TVar[] from, Type[] to) {
1022 if(types.length == 0)
1023 return TPred.EMPTY_ARRAY;
1024 TPred[] result = new TPred[types.length];
1025 for(int i=0;i<types.length;++i)
1026 result[i] = (TPred)types[i].replace(from, to);
1030 public static <T extends Type> Type[] replace(Type[] types, THashMap<TVar, T> map) {
1031 if(types.length == 0)
1032 return Type.EMPTY_ARRAY;
1033 Type[] result = new Type[types.length];
1034 for(int i=0;i<types.length;++i)
1035 result[i] = types[i].replace(map);
1039 public static Type union(Type ... effects) {
1040 if(effects.length == 0)
1042 else if(effects.length == 1)
1045 return new TUnion(effects);
1048 public static Type union(Type effect1, Type effect2) {
1049 return new TUnion(effect1, effect2);
1052 public static Type union(List<Type> effects) {
1053 if(effects.size() == 0)
1055 else if(effects.size() == 1)
1056 return effects.get(0);
1058 return new TUnion(effects.toArray(new Type[effects.size()]));
1061 public static void canonize(Type[] types) {
1062 for(int i=0;i<types.length;++i)
1063 types[i] = canonical(types[i]);
1066 public static Type simplifyFinalEffect(Type effect) {
1067 effect = canonical(effect);
1068 if(effect instanceof TMetaVar) {
1070 //((TMetaVar) effect).setRef(Types.NO_EFFECTS);
1071 Type t = Types.var(Kinds.EFFECT);
1072 ((TMetaVar) effect).setRef(t);
1074 } catch (UnificationException e) {
1075 // Should not happen.
1076 throw new RuntimeException(e);
1079 if(effect instanceof TUnion) {
1080 TUnion union = (TUnion)effect;
1081 if(union.effects.length == 0)
1082 return Types.NO_EFFECTS;
1083 ArrayList<Type> effects = new ArrayList<Type>(union.effects.length);
1084 for(Type c : union.effects) {
1085 c = simplifyFinalEffect(c);
1086 if(c instanceof TUnion)
1087 for(Type c2 : ((TUnion)c).effects)
1092 return union(effects);
1097 public static Type simplifyType(Type effect) {
1098 effect = canonical(effect);
1099 if(effect instanceof TUnion) {
1100 TUnion union = (TUnion)effect;
1101 if(union.effects.length == 0)
1102 return Types.NO_EFFECTS;
1103 THashSet<Type> effects = new THashSet<Type>(union.effects.length);
1104 for(Type c : union.effects) {
1105 c = simplifyFinalEffect(c);
1106 if(c instanceof TUnion)
1107 for(Type c2 : ((TUnion)c).effects)
1112 return union(effects.toArray(new Type[effects.size()]));
1117 public static Type parseType(ITypeEnvironment environment, String text) throws SCLTypeParseException {
1118 return parseType(new TypeElaborationContext(environment), text);
1121 public static Type parseType(ITypeEnvironment environment, THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1122 return parseType(new TypeElaborationContext(localTypeVars, environment), text);
1125 public static Type parseType(String text) throws SCLTypeParseException {
1126 return parseType(new TypeElaborationContext(DUMMY_TYPE_ENVIRONMENT), text);
1129 public static Type parseType(THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1130 return parseType(new TypeElaborationContext(localTypeVars, DUMMY_TYPE_ENVIRONMENT), text);
1133 private static Type parseType(TypeElaborationContext context, String text) throws SCLTypeParseException {
1134 SCLParserImpl parser = new SCLParserImpl(new StringReader(text));
1136 org.simantics.scl.compiler.internal.parsing.types.TypeAst ast =
1137 (org.simantics.scl.compiler.internal.parsing.types.TypeAst)parser.parseType();
1138 return ast.toType(context);
1139 } catch (SCLSyntaxErrorException e) {
1140 throw new SCLTypeParseException(new Problem(
1141 Locations.beginOf(e.location),
1142 Locations.endOf(e.location),
1147 public static Type instantiateAndStrip(Type type) {
1149 if(type instanceof TForAll) {
1150 TForAll forAll = (TForAll)type;
1151 type = forAll.type.replace(forAll.var, metaVar(forAll.var.getKind()));
1153 else if(type instanceof TFun) {
1154 TFun fun = (TFun)type;
1155 if(fun.domain instanceof TPred || fun.domain == Types.PUNIT)
1160 else if(type instanceof TMetaVar) {
1161 TMetaVar metaVar = (TMetaVar)type;
1162 if(metaVar.ref == null)