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 DYNAMIC = con("Prelude", "Dynamic");
90 public static final TCon VARIANT = con(BUILTIN, "Variant");
92 public static final TCon ADDITIVE = con("Prelude", "Additive");
93 public static final TCon MONAD = con("Prelude", "Monad");
94 public static final TCon INTEGRAL = con("Prelude", "Integral");
95 public static final TCon RING = con("Prelude", "Ring");
96 public static final TCon ORDERED_RING = con("Prelude", "OrderedRing");
97 public static final TCon REAL = con("Prelude", "Real");
98 public static final TCon SHOW = con("Prelude", "Show");
99 public static final TCon EQ = con("Prelude", "Eq");
100 public static final TCon ORD = con("Prelude", "Ord");
101 public static final TCon HASHABLE = con("Prelude", "Hashable");
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 or alias type pointed to by a chain of type meta-variables,
153 * or the last metavariable in the link, if it is not linked to an actual type.
154 * Unlike {@link #canonical(Type)}, this method does not resolve type aliases.
156 public static Type weakCanonical(Type type) {
158 if(type instanceof TMetaVar) {
159 TMetaVar metaVar = (TMetaVar)type;
160 if(metaVar.ref == null)
171 * Get the concrete type pointed to by a chain of type meta-variables. Unlike {@link #weakCanonical(Type)}
172 * this method also resolves type aliases.
174 public static Type canonical(Type type) {
175 while(type instanceof TMetaVar) {
176 TMetaVar metaVar = (TMetaVar)type;
184 public static Type closure(Type type, ArrayList<TVar> vars) {
185 for(int i=vars.size()-1;i>=0;--i)
186 type = forAll(vars.get(i), type);
190 public static Type closure(Type type, TVar[] vars) {
191 for(int i=vars.length-1;i>=0;--i)
192 type = forAll(vars[i], type);
196 public static Type closure(Type type) {
197 return closure(type, freeVars(type));
200 public static TCon con(String module, String name) {
201 return conCache.canonical(new TCon(module, name));
204 public static Type[] concat(Type[] a, Type[] b) {
209 Type[] result = new Type[a.length + b.length];
210 for(int i=0;i<a.length;++i)
212 for(int i=0;i<b.length;++i)
213 result[i+a.length] = b[i];
217 public static TVar[] concat(TVar[] a, TVar[] b) {
222 TVar[] result = new TVar[a.length + b.length];
223 for(int i=0;i<a.length;++i)
225 for(int i=0;i<b.length;++i)
226 result[i+a.length] = b[i];
230 public static boolean equals(TApply a, TApply b) {
231 return equals(a.parameter, b.parameter)
232 && equals(a.function , b.function );
235 public static boolean equals(TFun a, TFun b) {
236 return equals(a.domain, b.domain)
237 && equals(a.effect, b.effect)
238 && equals(a.range, b.range);
241 public static boolean subsumes(TFun a, TFun b) {
242 return subsumes(b.domain, a.domain)
243 && subsumesEffect(a.effect, b.effect)
244 && subsumes(a.range, b.range);
247 public static boolean subsumesEffect(Type a, Type b) {
248 EffectIdMap idMap = new EffectIdMap();
249 ArrayList<TMetaVar> mVars = new ArrayList<TMetaVar>(0);
250 int idA = idMap.toId(a, mVars);
251 int idB = idMap.toId(b, mVars);
252 return (idA&idB) == idA;
255 public static boolean equalsEffect(Type a, Type b) {
256 EffectIdMap idMap = new EffectIdMap();
257 ArrayList<TMetaVar> mVars = new ArrayList<TMetaVar>(0);
258 int idA = idMap.toId(a, mVars);
259 int idB = idMap.toId(b, mVars);
263 public static boolean equals(TForAll a, TForAll b) {
264 Kind aKind = a.var.getKind();
265 if(!Kinds.equalsCanonical(aKind, b.var.getKind()))
267 TVar newVar = var(aKind);
268 return equals(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
271 public static boolean equals(TPred a, TPred b) {
272 if(a.typeClass != b.typeClass
273 || a.parameters.length != b.parameters.length)
275 Type[] aParameters = a.parameters;
276 Type[] bParameters = b.parameters;
277 for(int i=0;i<aParameters.length;++i)
278 if(!equals(aParameters[i], bParameters[i]))
283 public static boolean equals(TUnion a, TUnion b) {
284 if(a.effects.length != b.effects.length)
286 for(int i=0;i<a.effects.length;++i)
287 if(!equals(a.effects[i], b.effects[i]))
293 * Tests equality of two types. Unbound TVars
294 * are equal only if they are the same variable.
295 * Bound TMetaVar is equal to the type it is bound to.
296 * Unbound TMetaVars are equal only if they are the same metavariable.
297 * Order of predicates and forall quantifiers matters.
299 public static boolean equals(Type a, Type b) {
304 Class<?> ca = a.getClass();
305 Class<?> cb = b.getClass();
308 if(ca == TApply.class)
309 return equals((TApply)a, (TApply)b);
310 else if(ca == TFun.class)
311 return equals((TFun)a, (TFun)b);
312 else if(ca == TForAll.class)
313 return equals((TForAll)a, (TForAll)b);
314 else if(ca == TPred.class)
315 return equals((TPred)a, (TPred)b);
316 else if(ca == TUnion.class)
317 return equals((TUnion)a, (TUnion)b);
318 else // ca == TCon.class
319 // || (ca == TMetaVar.class && a.ref == null && b.ref == null)
320 // || ca = TVar.class
321 return false; // Equals only if a == b, that was already tested
324 public static boolean subsumes(Type a, Type b) {
329 Class<?> ca = a.getClass();
330 Class<?> cb = b.getClass();
333 if(ca == TApply.class)
334 return equals((TApply)a, (TApply)b);
335 else if(ca == TFun.class)
336 return subsumes((TFun)a, (TFun)b);
337 else if(ca == TForAll.class) {
338 TForAll aForAll = (TForAll)a;
339 TForAll bForAll = (TForAll)b;
340 TVar newVar = var(aForAll.var.getKind());
341 return subsumes(aForAll.type.replace(aForAll.var, newVar),
342 bForAll.type.replace(bForAll.var, newVar));
344 else if(ca == TPred.class)
345 return equals((TPred)a, (TPred)b);
346 else if(ca == TUnion.class)
347 return equals((TUnion)a, (TUnion)b);
348 else // ca == TCon.class
349 // || (ca == TMetaVar.class && a.ref == null && b.ref == null)
350 // || ca = TVar.class
351 return false; // Equals only if a == b, that was already tested
354 public static TForAll forAll(TVar parameter, Type type) {
355 return new TForAll(parameter, type);
358 public static Type forAll(TVar[] parameters, Type type) {
359 for(int i=parameters.length-1;i>=0;--i)
360 type = forAll(parameters[i], type);
364 public static ArrayList<TVar> freeVars(Type type) {
365 ArrayList<TVar> vars = new ArrayList<TVar>(2);
366 type.collectFreeVars(vars);
370 public static ArrayList<TVar> freeVars(Type[] types) {
371 ArrayList<TVar> vars = new ArrayList<TVar>(2);
372 for(Type type : types)
373 type.collectFreeVars(vars);
377 public static TVar[] freeVarsArray(Type type) {
378 ArrayList<TVar> vars = freeVars(type);
379 return vars.toArray(new TVar[vars.size()]);
382 public static TVar[] freeVarsArray(Type[] types) {
383 ArrayList<TVar> vars = freeVars(types);
384 return vars.toArray(new TVar[vars.size()]);
387 public static TPred pred(TCon typeClass, Type ... parameters) {
388 return new TPred(typeClass, parameters);
391 public static Type function(Type ... types) {
392 Type result = types[types.length-1];
393 for(int i=types.length-2;i>=0;--i)
394 result = function(types[i], result);
398 public static Type function(Type from, Type to) {
399 return new TFun(from, Types.NO_EFFECTS, to);
402 public static Type function(Type[] from, Type to) {
403 for(int i=from.length-1;i>=0;--i)
404 to = function(from[i], to);
408 public static TFun functionE(Type from, Type effect, Type to) {
409 return new TFun(from, effect, to);
412 public static Type functionE(Type[] from, Type effect, Type to) {
413 for(int i=from.length-1;i>=0;--i) {
414 to = functionE(from[i], effect, to);
415 effect = Types.NO_EFFECTS;
420 public static Type removeForAll(Type type, ArrayList<TVar> vars) {
422 if(type instanceof TForAll) {
423 TForAll forAll = (TForAll)type;
425 vars.add(forAll.var);
427 else if(type instanceof TMetaVar) {
428 TMetaVar var = (TMetaVar)type;
439 public static Type removeForAll(Type type) {
441 if(type instanceof TForAll) {
442 TForAll forAll = (TForAll)type;
445 else if(type instanceof TMetaVar) {
446 TMetaVar var = (TMetaVar)type;
457 public static Type instantiate(TForAll forAll, ArrayList<TMetaVar> vars) {
458 TMetaVar metaVar = metaVar(forAll.var.getKind());
460 return instantiate(forAll.type.replace(forAll.var, metaVar), vars);
463 public static Type instantiate(Type type, ArrayList<TMetaVar> vars) {
465 throw new NullPointerException();
466 type = canonical(type);
467 if(type instanceof TForAll)
468 return instantiate((TForAll)type, vars);
473 public static Type list(Type parameter) {
474 return apply(LIST, parameter);
477 public static Type vector(Type parameter) {
478 return apply(VECTOR, parameter);
481 public static Type mvector(Type parameter) {
482 return apply(MVECTOR, parameter);
485 public static MultiFunction matchFunction(Type type, int arity) throws MatchException {
486 if (type instanceof TForAll)
487 return matchFunction(((TForAll)type).type, arity);
489 type = canonical(type);
490 /*while(type instanceof TForAll)
491 type = canonical(((TForAll)type).type);*/
492 Type[] parameterTypes = new Type[arity];
493 Type effect = Types.NO_EFFECTS;
494 for(int i=0;i<arity;++i) {
495 if(type instanceof TFun) {
496 TFun fun = (TFun)type;
497 parameterTypes[i] = fun.domain;
498 type = canonical(fun.range);
501 else if(Types.canonical(fun.effect) != Types.NO_EFFECTS)
502 throw new MatchException();
504 /*else if(type instanceof TMetaVar) {
505 TMetaVar metaVar = (TMetaVar)type;
506 type = Types.metaVar(Kinds.STAR);
507 Type template = type;
508 effect = Types.metaVar(Kinds.EFFECT);
509 for(int j=arity-1;j>=i;--j) {
510 Type pType = Types.metaVar(Kinds.STAR);
511 parameterTypes[j] = pType;
512 template = Types.functionE(pType,
513 j==arity-1 ? effect : Types.NO_EFFECTS,
517 metaVar.setRef(template);
518 } catch (UnificationException e) {
519 // Should never happen
520 throw new MatchException();
524 /*else if(type instanceof TApply) {
525 TApply apply1 = (TApply)type;
526 Type function1 = canonical(apply1.function);
527 if(function1 instanceof TApply) {
528 TApply apply2 = (TApply)function1;
529 Type function2 = canonical(apply2.function);
530 if(function2 == ARROW) {
531 result[i] = apply2.parameter;
532 type = canonical(apply1.parameter);
535 throw new MatchException();
538 throw new MatchException();
541 throw new MatchException();
543 return new MultiFunction(parameterTypes, effect, type);
546 public static boolean isApply(Type func, int arity, Type type) {
548 type = canonical(type);
549 if(!(type instanceof TApply))
551 type = ((TApply)type).function;
553 return equals(func, type);
556 public static Type matchApply(TCon func, Type type) throws MatchException {
557 type = canonical(type);
558 if(type instanceof TApply) {
559 TApply apply = (TApply)type;
560 Type f = canonical(apply.function);
562 return canonical(apply.parameter);
564 throw new MatchException();
567 public static MultiApply matchApply(Type type) {
568 ArrayList<Type> parameters = new ArrayList<Type>();
569 type = canonical(type);
570 while(type instanceof TApply) {
571 TApply apply = (TApply)type;
572 parameters.add(Types.canonical(apply.parameter));
573 type = canonical(apply.function);
575 return new MultiApply(type, parameters.toArray(new Type[parameters.size()]));
578 public static Type unifyApply(TCon func, Type type) throws MatchException {
579 type = canonical(type);
580 if(type instanceof TApply) {
581 TApply apply = (TApply)type;
582 Type f = canonical(apply.function);
584 return canonical(apply.parameter);
585 else if(f instanceof TMetaVar)
587 ((TMetaVar)f).setRef(func);
588 return canonical(apply.parameter);
589 } catch (UnificationException e) {
590 throw new MatchException();
593 else if(type instanceof TMetaVar) {
594 TMetaVar parameter = Types.metaVar(Kinds.metaVar());
596 ((TMetaVar) type).setRef(apply(func, parameter));
597 } catch (UnificationException e) {
598 throw new MatchException();
602 throw new MatchException();
605 public static MultiFunction matchFunction(Type type) {
606 type = canonical(type);
607 while(type instanceof TForAll)
608 type = canonical(((TForAll)type).type);
609 ArrayList<Type> parameterTypes = new ArrayList<Type>();
610 Type effect = Types.NO_EFFECTS;
612 if(type instanceof TFun) {
613 TFun fun = (TFun)type;
614 parameterTypes.add(fun.domain);
615 type = canonical(fun.range);
616 if(canonical(fun.effect) != Types.NO_EFFECTS) {
621 /*else if(type instanceof TApply) {
622 TApply apply1 = (TApply)type;
623 Type function1 = canonical(apply1.function);
624 if(function1 instanceof TApply) {
625 TApply apply2 = (TApply)function1;
626 Type function2 = canonical(apply2.function);
627 if(function2 == ARROW) {
628 types.add(apply2.parameter);
629 type = apply1.parameter;
645 return new MultiFunction(
646 parameterTypes.toArray(new Type[parameterTypes.size()]),
651 public static MultiFunction unifyFunction(Type type, int arity) throws UnificationException {
652 Type[] parameterTypes = new Type[arity];
653 for(int i=0;i<arity;++i)
654 parameterTypes[i] = metaVar(Kinds.STAR);
655 Type effect = metaVar(Kinds.EFFECT);
656 Type requiredType = metaVar(Kinds.STAR);
657 MultiFunction result = new MultiFunction(parameterTypes, effect, requiredType);
659 for(int i=arity-1;i>=0;--i) {
660 requiredType = functionE(parameterTypes[i], effect, requiredType);
661 effect = Types.NO_EFFECTS;
663 unify(type, requiredType);
667 private static Type getRangeIfFunction(Type type) {
668 type = canonical(type);
670 if(type instanceof TFun) {
671 return ((TFun)type).range;
673 /*else if(type instanceof TApply) {
674 TApply apply1 = (TApply)type;
675 Type f = canonical(apply1.function);
676 if(f instanceof TApply) {
677 if( canonical(((TApply)f).function) == Types.ARROW ) {
678 return apply1.parameter;
690 public static int getArity(Type type) {
693 type = getRangeIfFunction(type);
701 public static TMetaVar metaVar(Kind kind) {
702 return new TMetaVar(kind);
705 public static Type constrained(TPred constraint, Type type) {
706 return new TFun(constraint, Types.NO_EFFECTS, type);
709 public static Type constrained(TPred[] constraints, Type type) {
710 for(int i=constraints.length-1;i>=0;--i)
711 type = constrained(constraints[i], type);
715 public static TMultiApply toMultiApply(Type type) {
716 ArrayList<Type> parameters = new ArrayList<Type>();
717 type = canonical(type);
718 while(type instanceof TApply) {
719 TApply apply = (TApply)type;
720 parameters.add(apply.parameter);
721 type = canonical(apply.function);
723 Collections.reverse(parameters);
724 return new TMultiApply(type, parameters);
727 public static Type tuple(Type ... parameters) {
728 if(parameters.length == 1)
729 return parameters[0];
731 return apply(tupleConstructor(parameters.length), parameters);
734 public static TCon tupleConstructor(int arity) {
735 if(arity < 0 || arity == 1)
736 throw new IllegalArgumentException("The arity of a tuple cannot be " + arity + ".");
738 TCon[] oldTupleCache = tupleCache;
739 if(oldTupleCache.length <= arity) {
740 int oldLength = oldTupleCache.length;
741 int newLength = oldLength*2;
742 while(newLength <= arity)
744 TCon[] newTupleCache = Arrays.copyOf(oldTupleCache, newLength);
745 for(int i=oldLength;i<newLength;++i) {
746 StringBuilder b = new StringBuilder();
751 newTupleCache[i] = con(BUILTIN, b.toString());
753 TCon result = newTupleCache[arity];
754 tupleCache = newTupleCache;
758 return oldTupleCache[arity];
761 public static void unify(TFun a, TFun b) throws UnificationException {
762 unify(a.domain, b.domain);
763 unify(a.effect, b.effect);
764 unify(a.range, b.range);
767 public static void unify(TApply a, TApply b) throws UnificationException {
768 unify(a.function, b.function);
769 unify(a.parameter, b.parameter);
772 public static void unify(TForAll a, TForAll b) throws UnificationException {
774 Kinds.unify(a.var.getKind(), b.var.getKind());
775 } catch (KindUnificationException e) {
776 throw new UnificationException(a, b);
778 TVar newVar = var(a.var.getKind());
779 unify(a.type.replace(a.var, newVar), b.type.replace(b.var, newVar));
782 public static void unify(TPred a, TPred b) throws UnificationException {
783 if(a.typeClass != b.typeClass
784 || a.parameters.length != b.parameters.length)
785 throw new UnificationException(a, b);
786 for(int i=0;i<a.parameters.length;++i)
787 unify(a.parameters[i], b.parameters[i]);
790 public static void unify(TUnion a, TUnion b) throws UnificationException {
791 if(a.effects.length != b.effects.length)
792 throw new UnificationException(a, b);
793 for(int i=0;i<a.effects.length;++i)
794 unify(a.effects[i], b.effects[i]);
797 public static void unify(Type a, Type b) throws UnificationException {
798 a = weakCanonical(a);
799 b = weakCanonical(b);
802 if(a instanceof TMetaVar) {
803 ((TMetaVar)a).setRef(b);
806 if(b instanceof TMetaVar) {
807 ((TMetaVar)b).setRef(a);
812 Class<?> ca = a.getClass();
813 Class<?> cb = b.getClass();
815 throw new UnificationException(a, b);
817 if(ca == TApply.class)
818 unify((TApply)a, (TApply)b);
819 else if(ca == TFun.class)
820 unify((TFun)a, (TFun)b);
821 else if(ca == TForAll.class)
822 unify((TForAll)a, (TForAll)b);
823 else if(ca == TPred.class)
824 unify((TPred)a, (TPred)b);
825 else if(ca == TUnion.class)
826 unify((TUnion)a, (TUnion)b);
827 else // ca == TCon.class || ca = TVar.class
828 throw new UnificationException(a, b);
831 public static TVar var(Kind kind) {
832 return new TVar(kind);
835 public static TVar[] vars(TVar[] otherVars) {
836 TVar[] vars = new TVar[otherVars.length];
837 for(int i=0;i<otherVars.length;++i)
838 vars[i] = var(otherVars[i].getKind());
842 public static Type instantiate(Type type, Type ... parameters) {
843 for(int i=0;i<parameters.length;++i) {
844 type = canonical(type);
845 if(!(type instanceof TForAll))
846 throw new IllegalArgumentException();
847 TForAll forAll = (TForAll)type;
848 type = forAll.type.replace(forAll.var, parameters[i]);
853 public static Type[] getTypes(Typed[] values) {
854 Type[] types = new Type[values.length];
855 for(int i=0;i<values.length;++i)
856 types[i] = values[i].getType();
861 * Matches b to a, i.e. finds a substitution such that a[substitution] = b.
862 * Unbound metavariables in b are consired as normal variables. It is assumed
863 * that a does not contain metavariables and b does not contain any type variables
864 * in a (no occurs checks needed).
867 * @param substitution
870 public static boolean match(Type a, Type b, THashMap<TVar, Type> substitution) {
873 Class<?> ca = a.getClass();
874 if(ca == TVar.class) {
876 Type t = substitution.get(ta);
878 substitution.put(ta, b); // no occurs check needed
882 return match(t, b, substitution);
886 Class<?> cb = b.getClass();
887 if(ca != cb || ca == TCon.class)
889 if(ca == TApply.class)
890 return match((TApply)a, (TApply)b, substitution);
891 else if(ca == TFun.class)
892 return match((TFun)a, (TFun)b, substitution);
893 else if(ca == TPred.class)
894 return match((TPred)a, (TPred)b, substitution);
896 throw new UnsupportedOperationException("match(" + a + ", " + b +") not supported"); // TForAll not supported
900 public static boolean match(TApply a, TApply b, THashMap<TVar, Type> substitution) {
901 return match(a.function, b.function, substitution) && match(a.parameter, b.parameter, substitution);
904 public static boolean match(TPred a, TPred b, THashMap<TVar, Type> substitution) {
905 if(a.typeClass != b.typeClass || a.parameters.length != b.parameters.length)
907 for(int i=0;i<a.parameters.length;++i)
908 if(!match(a.parameters[i], b.parameters[i], substitution))
913 public static boolean match(TFun a, TFun b, THashMap<TVar, Type> substitution) {
914 return match(a.domain, b.domain, substitution)
915 && match(a.effect, b.effect, substitution)
916 && match(a.range, b.range, substitution);
919 public static Type removePred(Type type,
920 ArrayList<TPred> preds) {
921 while(type instanceof TFun) {
922 TFun pred = (TFun)type;
923 if(!(pred.domain instanceof TPred))
925 preds.add((TPred)pred.domain);
926 type = canonical(pred.range);
931 public static <T extends Typed> Type[] getTypes(List<T> vars) {
932 Type[] result = new Type[vars.size()];
933 for(int i=0;i<result.length;++i)
934 result[i] = vars.get(i).getType();
938 public static boolean isBoxed(Type type) {
940 if(type instanceof TVar)
942 else if(type instanceof TApply) {
943 TApply apply = (TApply)type;
944 Type function = Types.canonical(apply.function);
945 if(function == Types.MAYBE || function == Types.MVECTOR || function == Types.VECTOR)
946 // FIXME Special case handled now here.
947 // The same problem is possibly with other types also!!!
948 type = apply.parameter;
952 else if(type instanceof TMetaVar) {
953 type = ((TMetaVar)type).ref;
957 else if(type instanceof TForAll) {
958 type = ((TForAll)type).type;
965 public static boolean isFunction(Type type) {
966 type = canonical(type);
967 return type instanceof TFun;
968 /*if(!(type instanceof TApply))
970 type = canonical(((TApply)type).function);
971 if(!(type instanceof TApply))
973 type = canonical(((TApply)type).function);
974 return type == ARROW;*/
977 public static boolean equals(Type[] as, Type[] bs) {
978 if(as.length != bs.length)
980 for(int i=0;i<as.length;++i)
981 if(!equals(as[i], bs[i]))
986 public static String toString(Type[] types) {
987 StringBuilder b = new StringBuilder();
988 TypeUnparsingContext tuc = new TypeUnparsingContext();
990 boolean first = true;
991 for(Type type : types) {
996 b.append(type.toString(tuc));
1002 public static TCon getConstructor(Type type) throws MatchException {
1004 if(type instanceof TCon)
1006 else if(type instanceof TApply)
1007 type = ((TApply)type).function;
1008 else if(type instanceof TMetaVar) {
1009 Type ref = ((TMetaVar)type).ref;
1011 throw new MatchException();
1015 throw new MatchException();
1019 public static Type[] replace(Type[] types, TVar[] from, Type[] to) {
1020 if(types.length == 0)
1021 return Type.EMPTY_ARRAY;
1022 Type[] result = new Type[types.length];
1023 for(int i=0;i<types.length;++i)
1024 result[i] = types[i].replace(from, to);
1028 public static <T extends Type> Type[] replace(Type[] types, THashMap<TVar, T> map) {
1029 if(types.length == 0)
1030 return Type.EMPTY_ARRAY;
1031 Type[] result = new Type[types.length];
1032 for(int i=0;i<types.length;++i)
1033 result[i] = types[i].replace(map);
1037 public static Type union(Type ... effects) {
1038 if(effects.length == 0)
1040 else if(effects.length == 1)
1043 return new TUnion(effects);
1046 public static Type union(List<Type> effects) {
1047 if(effects.size() == 0)
1049 else if(effects.size() == 1)
1050 return effects.get(0);
1052 return new TUnion(effects.toArray(new Type[effects.size()]));
1055 public static void canonize(Type[] types) {
1056 for(int i=0;i<types.length;++i)
1057 types[i] = canonical(types[i]);
1060 public static Type simplifyFinalEffect(Type effect) {
1061 effect = canonical(effect);
1062 if(effect instanceof TMetaVar) {
1064 //((TMetaVar) effect).setRef(Types.NO_EFFECTS);
1065 Type t = Types.var(Kinds.EFFECT);
1066 ((TMetaVar) effect).setRef(t);
1068 } catch (UnificationException e) {
1069 // Should not happen.
1070 throw new RuntimeException(e);
1073 if(effect instanceof TUnion) {
1074 TUnion union = (TUnion)effect;
1075 if(union.effects.length == 0)
1076 return Types.NO_EFFECTS;
1077 ArrayList<Type> effects = new ArrayList<Type>(union.effects.length);
1078 for(Type c : union.effects) {
1079 c = simplifyFinalEffect(c);
1080 if(c instanceof TUnion)
1081 for(Type c2 : ((TUnion)c).effects)
1086 return union(effects);
1091 public static Type simplifyType(Type effect) {
1092 effect = canonical(effect);
1093 if(effect instanceof TUnion) {
1094 TUnion union = (TUnion)effect;
1095 if(union.effects.length == 0)
1096 return Types.NO_EFFECTS;
1097 THashSet<Type> effects = new THashSet<Type>(union.effects.length);
1098 for(Type c : union.effects) {
1099 c = simplifyFinalEffect(c);
1100 if(c instanceof TUnion)
1101 for(Type c2 : ((TUnion)c).effects)
1106 return union(effects.toArray(new Type[effects.size()]));
1111 public static Type parseType(ITypeEnvironment environment, String text) throws SCLTypeParseException {
1112 return parseType(new TypeElaborationContext(environment), text);
1115 public static Type parseType(ITypeEnvironment environment, THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1116 return parseType(new TypeElaborationContext(localTypeVars, environment), text);
1119 public static Type parseType(String text) throws SCLTypeParseException {
1120 return parseType(new TypeElaborationContext(DUMMY_TYPE_ENVIRONMENT), text);
1123 public static Type parseType(THashMap<String, TVar> localTypeVars, String text) throws SCLTypeParseException {
1124 return parseType(new TypeElaborationContext(localTypeVars, DUMMY_TYPE_ENVIRONMENT), text);
1127 private static Type parseType(TypeElaborationContext context, String text) throws SCLTypeParseException {
1128 SCLParserImpl parser = new SCLParserImpl(new StringReader(text));
1130 org.simantics.scl.compiler.internal.parsing.types.TypeAst ast =
1131 (org.simantics.scl.compiler.internal.parsing.types.TypeAst)parser.parseType();
1132 return ast.toType(context);
1133 } catch (SCLSyntaxErrorException e) {
1134 throw new SCLTypeParseException(new Problem(
1135 Locations.beginOf(e.location),
1136 Locations.endOf(e.location),