Beispiel #1
0
  public SubstMap unify(final Loc loc, final Type other, final TypeEnv env) {
    if (env.checkVisited(this, other)) return SubstMap.EMPTY;

    if (other instanceof TypeVar) return SubstMap.bindVar(loc, (TypeVar) other, this);

    // if our application expression can be evaluated, unify against that.
    if (isAbsApply()) return eval().unify(loc, other, env);

    final Type otherEval = other.deref().eval();

    if (otherEval instanceof TypeApp) {
      final TypeApp otherApp = (TypeApp) otherEval;
      final SubstMap baseSubst = base.unify(loc, otherApp.base, env);

      if (baseSubst != null) {
        final SubstMap argSubst =
            arg.subst(baseSubst).unify(loc, otherApp.arg.subst(baseSubst), env);

        if (argSubst != null) return baseSubst.compose(loc, argSubst);
      }
    } else if (kind.equals(otherEval.getKind())) {
      // other is not an application expression, but kinds match.
      // other type classes do opportunistic matching of type apps
      // implementations of unify. This approach is really ad-hoc,
      // needs to be rationalized.

      return otherEval.unify(loc, this, env);
    }

    return null;
  }