Skip to content

Commit

Permalink
merge: Fix subtyping from path to pi (#1250)
Browse files Browse the repository at this point in the history
Fix #1249
  • Loading branch information
ice1000 authored Dec 22, 2024
2 parents b56a2ba + 42ebdd3 commit d849d4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 11 additions & 9 deletions base/src/main/java/org/aya/tyck/ExprTycker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.aya.syntax.core.term.xtt.DimTerm;
import org.aya.syntax.core.term.xtt.DimTyTerm;
import org.aya.syntax.core.term.xtt.EqTerm;
import org.aya.syntax.core.term.xtt.PAppTerm;
import org.aya.syntax.ref.*;
import org.aya.syntax.telescope.AbstractTele;
import org.aya.syntax.telescope.Signature;
Expand Down Expand Up @@ -222,8 +223,9 @@ && whnf(type) instanceof DataCall dataCall
// Try coercive subtyping for (Path A ...) into (I -> A)
if (type instanceof DepTypeTerm(var kind, var dom, var cod) && kind == DTKind.Pi && dom == DimTyTerm.INSTANCE) {
if (whnf(resultType) instanceof EqTerm eq) {
var closure = makeClosurePiPath(expr, eq, cod, result.wellTyped());
if (closure == null) return makeErrorResult(type, result);
if (!isConvertiblePiPath(expr, eq, cod)) return makeErrorResult(type, result);
var closure = result.wellTyped() instanceof LamTerm(var clos) ? clos
: new Closure.Jit(i -> new PAppTerm(result.wellTyped(), i, eq.a(), eq.b()));
return new Jdg.Default(new LamTerm(closure), eq);
}
}
Expand All @@ -232,8 +234,9 @@ && whnf(type) instanceof DataCall dataCall
if (whnf(resultType) instanceof DepTypeTerm(
var kind, var dom, var cod
) && kind == DTKind.Pi && dom == DimTyTerm.INSTANCE) {
var closure = makeClosurePiPath(expr, eq, cod, result.wellTyped());
if (closure == null) return makeErrorResult(type, result);
if (!isConvertiblePiPath(expr, eq, cod)) return makeErrorResult(type, result);
var closure = result.wellTyped() instanceof LamTerm(var clos) ? clos
: new Closure.Jit(i -> new AppTerm(result.wellTyped(), i));
checkBoundaries(eq, closure, expr.sourcePos(), msg ->
new CubicalError.BoundaryDisagree(expr, msg, new UnifyInfo(state)));
return new Jdg.Default(new LamTerm(closure), eq);
Expand Down Expand Up @@ -263,18 +266,17 @@ && whnf(type) instanceof DataCall dataCall
return new Jdg.Default(new ErrorTerm(result.wellTyped()), type);
}

private @Nullable Closure makeClosurePiPath(@NotNull WithPos<Expr> expr, EqTerm eq, Closure cod, @NotNull Term core) {
/// @return true if the coercion is successful
private boolean isConvertiblePiPath(@NotNull WithPos<Expr> expr, EqTerm eq, Closure cod) {
var ref = new FreeTerm(new LocalVar("i"));
var wellTyped = false;
try (var _ = subscope(ref.name(), DimTyTerm.INSTANCE)) {
wellTyped = unifyTyReported(eq.appA(ref), cod.apply(ref), expr);
}
if (!wellTyped) return null;
if (!wellTyped) return false;
if (expr.data() instanceof Expr.WithTerm with)
addWithTerm(with, expr.sourcePos(), eq);
return core instanceof LamTerm(var clo) ? clo
// This is kinda unsafe but it should be fine
: new Closure.Jit(i -> new AppTerm(core, i));
return true;
}

public @NotNull Term ty(@NotNull WithPos<Expr> expr) {
Expand Down
5 changes: 5 additions & 0 deletions cli-impl/src/test/resources/success/src/Test.aya
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ module Issue1232 {
def - : Nat => 1
def --> : Nat => 1
}

module Issue1249 {
variable A B : Type
def transp (p : A = B) (a : A) : B => coe 0 1 p a
}

0 comments on commit d849d4d

Please sign in to comment.