1 package org.simantics.scl.compiler.types.kinds;
3 import org.simantics.scl.compiler.types.exceptions.KindUnificationException;
6 public static final KCon STAR = new KCon("*");
7 public static final KCon EFFECT = new KCon("E");
8 public static final Kind STAR_TO_STAR = new KArrow(STAR, STAR);
9 public static final Kind STAR_TO_STAR_TO_STAR = new KArrow(STAR, STAR_TO_STAR);
11 public static KArrow arrow(Kind domain, Kind range) {
12 return new KArrow(domain, range);
15 public static KMetaVar metaVar() {
16 return new KMetaVar();
19 public static Kind canonical(Kind a) {
20 while(a instanceof KMetaVar) {
21 KMetaVar mv = (KMetaVar)a;
29 public static void unifyWithStar(Kind a) throws KindUnificationException {
33 if(a instanceof KMetaVar)
34 ((KMetaVar)a).ref = STAR;
35 throw new KindUnificationException();
39 * Tries to unify two kinds by linking matching meta-variables.
40 * @throws KindUnificationException if unification fails
42 public static void unify(Kind a, Kind b) throws KindUnificationException {
47 if(a instanceof KMetaVar) {
48 ((KMetaVar)a).setRef(b);
51 if(b instanceof KMetaVar) {
52 ((KMetaVar)b).setRef(a);
55 if(a instanceof KArrow && b instanceof KArrow) {
56 KArrow arrowA = (KArrow)a;
57 KArrow arrowB = (KArrow)b;
58 unify(arrowA.domain, arrowB.domain);
59 unify(arrowA.range, arrowB.range);
62 throw new KindUnificationException();
65 public static boolean equalsCanonical(Kind a, Kind b) {
68 if(!(a instanceof KArrow))
70 if(!(b instanceof KArrow))
72 KArrow arrowA = (KArrow)a;
73 KArrow arrowB = (KArrow)b;
74 return equals(arrowA.domain, arrowB.domain) &&
75 equals(arrowA.range, arrowB.range);
78 public static boolean equals(Kind a, Kind b) {
79 return equalsCanonical(canonical(a), canonical(b));