示例#1
0
  @Override
  public TypeResult typecheck(TypeEnv E) throws TypeError {
    Logger.i("----------type check in App");
    TypeResult tr1 = l.typecheck(E);
    Logger.i("e1:" + tr1.t);
    TypeResult tr2 = r.typecheck(E);
    Logger.i("e2:" + tr2.t);

    Type t1 = tr1.t;
    Type t2 = tr2.t;

    Substitution substitution = tr2.s.compose(tr1.s);

    t1 = substitution.apply(t1);
    t2 = substitution.apply(t2);

    Type resultType;

    if (t1 instanceof ArrowType) {
      substitution = ((ArrowType) t1).t1.unify(t2).compose(substitution); // t1=t2->a
      t1 = substitution.apply(t1);
      resultType = ((ArrowType) t1).t2;
    } else if (t1 instanceof TypeVar) {
      TypeVar tv = new TypeVar(false); // new type a
      substitution = t1.unify(new ArrowType(t2, tv)).compose(substitution);
      resultType = substitution.apply(tv);
    } else {
      throw new TypeError("no function found");
    }

    Logger.i("----------end check in App");
    return TypeResult.of(substitution, resultType); // return a
  }
示例#2
0
  @Override
  public TypeResult typecheck(TypeEnv E) throws TypeError {
    TypeResult l_type = l.typecheck(E);
    Substitution sub = l_type.s;
    sub = sub.apply(l_type.t).unify(Type.BOOL).compose(sub);

    TypeResult r_type = r.typecheck(sub.compose(E));
    sub = r_type.s.compose(sub);
    sub = sub.apply(r_type.t).unify(Type.BOOL).compose(sub);
    return TypeResult.of(sub, Type.BOOL);
  }