Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do ReplaceWith #1017

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/src/main/java/org/aya/concrete/desugar/Desugarer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static class DesugarInterruption extends Exception {}
yield pre(new BinExprParser(info, seq.view()).build(pos));
}
case Expr.Do(var sourcePos, var monadBind, var binds) -> {
var last = binds.last();
var last = binds.getLast();
if (last.var() != LocalVar.IGNORED) {
info.opSet().reporter.report(new DoNotationError(last.sourcePos(), expr));
yield new Expr.Error(sourcePos, expr);
Expand Down
4 changes: 2 additions & 2 deletions base/src/main/java/org/aya/core/ops/Eta.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2023 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.core.ops;

Expand Down Expand Up @@ -29,7 +29,7 @@ case TupTerm(var items) -> {
var etaItems = items.map(x -> x.descent(this::uneta));
var defaultRes = new TupTerm(etaItems);
// Get first item's Proj.of Term to compare with other items'
var firstItem = etaItems.first().term();
var firstItem = etaItems.getFirst().term();
if (!(firstItem instanceof ProjTerm proj
&& proj.of() instanceof RefTerm ref
&& ctx.get(ref.var()) instanceof SigmaTerm sigmaTerm)) yield defaultRes;
Expand Down
8 changes: 4 additions & 4 deletions base/src/main/java/org/aya/prettier/BasePrettier.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected BasePrettier(@NotNull PrettierOptions options) {
var visibleArgs = (showImplicits ? args : args.filter(BinOpParser.Elem::explicit)).toImmutableSeq();
if (visibleArgs.isEmpty()) return assoc != null ? Doc.parened(fn) : fn;
if (assoc != null) {
var firstArg = visibleArgs.first();
var firstArg = visibleArgs.getFirst();
if (!firstArg.explicit()) return prefix(Doc.parened(fn), fmt, outer, visibleArgs.view());
var first = fmt.apply(Outer.BinOp, firstArg.term());
if (assoc.isBinary()) {
Expand Down Expand Up @@ -196,14 +196,14 @@ public static <T extends AyaDocile> Doc arg(@NotNull Fmt<T> fmt, @NotNull BinOpP
@Nullable Term body, @NotNull ToIntBiFunction<Term, AnyVar> altF7
) {
if (telescope.isEmpty()) return Doc.empty();
var last = telescope.first();
var last = telescope.getFirst();
var buf = MutableList.<Doc>create();
var names = MutableList.of(last.ref());
for (int i = 1; i < telescope.size(); i++) {
var param = telescope.get(i);
if (!Objects.equals(param.type(), last.type())) {
if (body != null && names.sizeEquals(1)) {
var ref = names.first();
var ref = names.getFirst();
var used = telescope.sliceView(i, telescope.size())
.map(ParamLike::type).appended(body)
.anyMatch(p -> altF7.applyAsInt(p, ref) > 0);
Expand All @@ -216,7 +216,7 @@ public static <T extends AyaDocile> Doc arg(@NotNull Fmt<T> fmt, @NotNull BinOpP
names.append(param.ref());
}
if (body != null && names.sizeEquals(1)
&& altF7.applyAsInt(body, names.first()) == 0) {
&& altF7.applyAsInt(body, names.getFirst()) == 0) {
buf.append(justType(last, Outer.ProjHead));
} else buf.append(mutableListNames(names, last));
return Doc.sep(buf);
Expand Down
8 changes: 4 additions & 4 deletions base/src/main/java/org/aya/tyck/ExprTycker.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public final class ExprTycker extends UnifiedTycker {
fTy = whnf(fTy);
} else if (fTy instanceof SortTerm) {
if (whnf(app) instanceof ClassCall classCall) {
var member = classCall.missingMembers().firstOrNull();
var member = classCall.missingMembers().getFirstOrNull();
if (member == null) {
throw new InternalException("TODO: too many fields");
}
Expand Down Expand Up @@ -226,7 +226,7 @@ public final class ExprTycker extends UnifiedTycker {
var type = ctx.freshTyHole("_ty" + lit.integer() + "'", lit.sourcePos());
yield new Result.Default(new MetaLitTerm(lit.sourcePos(), lit.integer(), defs, type.component1()), type.component1());
}
var match = defs.first();
var match = defs.getFirst();
var type = new DataCall(((DataDef) match.component1()).ref, 0, ImmutableSeq.empty());
yield new Result.Default(new IntegerTerm(integer, match.component2(), type), type);
}
Expand All @@ -253,11 +253,11 @@ public final class ExprTycker extends UnifiedTycker {
if (defs.sizeGreaterThan(1)) yield fail(expr, new Zonker.UnsolvedLit(new MetaLitTerm(
arr.sourcePos(), arr, defs, ErrorTerm.typeOf(arr))));

var match = defs.first();
var match = defs.getFirst();
var def = (DataDef) match.component1();

// preparing
var dataParam = Def.defTele(def.ref).first();
var dataParam = Def.defTele(def.ref).getFirst();
var sort = dataParam.type(); // the sort of type below.
var hole = ctx.freshHole(sort, arr.sourcePos());
var type = new DataCall(def.ref(), 0, ImmutableSeq.of(
Expand Down
17 changes: 17 additions & 0 deletions base/src/main/java/org/aya/tyck/env/LocalCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ default void forward(@NotNull LocalCtx dest, @NotNull Term term, @NotNull TyckSt
}
}.accept(term);
}

/**
* Perform {@param action} under this context + ({@param var} : {@param type})
*/
default <T> T with(@NotNull LocalVar var, @NotNull Term type, @NotNull Supplier<T> action) {
put(var, type);
try {
Expand All @@ -97,9 +101,17 @@ default <T> T with(@NotNull LocalVar var, @NotNull Term type, @NotNull Supplier<
remove(SeqView.of(var));
}
}

/**
* Perform {@param action} under this context + {@param param}
*/
default <T> T with(@NotNull Supplier<T> action, @NotNull Term.Param... param) {
return with(action, Seq.of(param).view());
}

/**
* Perform {@param action} under this context + {@param param}
*/
default <T> T with(@NotNull Supplier<T> action, @NotNull SeqView<Term.Param> param) {
for (var p : param) put(p);
try {
Expand All @@ -108,6 +120,10 @@ default <T> T with(@NotNull Supplier<T> action, @NotNull SeqView<Term.Param> par
remove(param.map(Term.Param::ref));
}
}

/**
* @return this context in telescope style
*/
default @NotNull ImmutableSeq<Term.Param> extract() {
var ctx = MutableList.<Term.Param>create();
var map = this;
Expand All @@ -117,6 +133,7 @@ default <T> T with(@NotNull Supplier<T> action, @NotNull SeqView<Term.Param> par
}
return ctx.toImmutableSeq();
}

@Contract(mutates = "param1") void extractToLocal(@NotNull MutableList<Term.Param> dest);
@Contract(pure = true) default @NotNull Term get(@NotNull LocalVar var) {
var res = getUnchecked(var);
Expand Down
2 changes: 1 addition & 1 deletion base/src/main/java/org/aya/tyck/env/SeqLocalCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record P(@NotNull LocalVar var, @NotNull Term type) {
}

@Override public @Nullable Term getLocal(@NotNull LocalVar var) {
return localSeq.firstOption(p -> p.var.equals(var)).map(p -> p.type).getOrNull();
return localSeq.findFirst(p -> p.var.equals(var)).map(p -> p.type).getOrNull();
}

@Override public void putUnchecked(@NotNull LocalVar var, @NotNull Term term) {
Expand Down
2 changes: 1 addition & 1 deletion base/src/main/java/org/aya/tyck/unify/Synthesizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ case InTerm(var phi, var u) -> {
}
case OutTerm outS -> {
var ty = tryPress(outS.of());
if (ty instanceof PrimCall sub) yield sub.args().first().term();
if (ty instanceof PrimCall sub) yield sub.args().getFirst().term();
yield null;
}
case PAppTerm app -> {
Expand Down
12 changes: 6 additions & 6 deletions base/src/main/java/org/aya/tyck/unify/TermComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
) {
if (!l.sizeEquals(r)) return fail.get();
if (l.isEmpty()) return success.apply(lsub, rsub);
return checkParam(l.first(), r.first(), lsub, rsub, lr, rl, fail, (ls, rs) ->
return checkParam(l.getFirst(), r.getFirst(), lsub, rsub, lr, rl, fail, (ls, rs) ->
checkParams(l.drop(1), r.drop(1), ls, rs, lr, rl, fail, success));
}

Expand All @@ -227,7 +227,7 @@
var ru = r.toImmutableSeq();
for (int i = 0; lu.sizeGreaterThan(i); i++) {
var li = lu.get(i);
var head = typesSubst.first();
var head = typesSubst.getFirst();
if (!compare(li, ru.get(i), lr, rl, head.type())) return false;
typesSubst = typesSubst.drop(1).map(type -> type.subst(head.ref(), li));
}
Expand Down Expand Up @@ -277,7 +277,7 @@
var params = paramsSeq.view();
for (int i = 1, size = paramsSeq.size(); i <= size; i++) {
var l = ProjTerm.proj(lhs, i);
var currentParam = params.first();
var currentParam = params.getFirst();
ctx.put(currentParam);
if (!compare(l, ProjTerm.proj(rhs, i), lr, rl, currentParam.type())) yield false;
params = params.drop(1).map(x -> x.subst(currentParam.ref(), l));
Expand Down Expand Up @@ -450,12 +450,12 @@
var subst = new Subst(MutableMap.create());
for (int i = 1; i < lhs.ix(); i++) {
var l = ProjTerm.proj(lhs, i);
var currentParam = params.first();
var currentParam = params.getFirst();
subst.add(currentParam.ref(), l);
params = params.drop(1);
}
if (params.isNotEmpty()) yield params.first().subst(subst).type();
yield params.last().subst(subst).type();
if (params.isNotEmpty()) yield params.getFirst().subst(subst).type();
yield params.getLast().subst(subst).type();

Check warning on line 458 in base/src/main/java/org/aya/tyck/unify/TermComparator.java

View check run for this annotation

Codecov / codecov/patch

base/src/main/java/org/aya/tyck/unify/TermComparator.java#L458

Added line #L458 was not covered by tests
}
case FormulaTerm lhs -> {
if (!(preRhs instanceof FormulaTerm rhs)) yield null;
Expand Down
6 changes: 3 additions & 3 deletions base/src/test/java/org/aya/concrete/ParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,19 @@ def uncurry (A : Type) (B : Type) (C : Type)
}

private void parseImport(@Language("Aya") String code) {
assertTrue(parseStmt(code).first() instanceof Command.Import s && !s.toDoc(AyaPrettierOptions.debug()).debugRender().isEmpty());
assertTrue(parseStmt(code).getFirst() instanceof Command.Import s && !s.toDoc(AyaPrettierOptions.debug()).debugRender().isEmpty());
}

private void parseOpen(@Language("Aya") String code) {
assertTrue(parseStmt(code).last() instanceof Command.Open s && !s.toDoc(AyaPrettierOptions.debug()).debugRender().isEmpty());
}

private void parseFn(@Language("Aya") String code) {
assertTrue(parseDecl(code).first() instanceof TeleDecl.FnDecl s && !s.toDoc(AyaPrettierOptions.debug()).debugRender().isEmpty());
assertTrue(parseDecl(code).getFirst() instanceof TeleDecl.FnDecl s && !s.toDoc(AyaPrettierOptions.debug()).debugRender().isEmpty());
}

private void parseData(@Language("Aya") String code) {
assertTrue(parseDecl(code).first() instanceof TeleDecl.DataDecl);
assertTrue(parseDecl(code).getFirst() instanceof TeleDecl.DataDecl);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion cli-impl/src/test/java/org/aya/cli/ModifierParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ModifierParserTest {
assertEquals(1, reporter.problems().size());
assertEquals(1, reporter.problemSize(Problem.Severity.WARN));

var warn = reporter.problems().first();
var warn = reporter.problems().getFirst();
assertInstanceOf(ModifierProblem.class, warn);
assertEquals(Private, ((ModifierProblem) warn).modifier());

Expand Down
2 changes: 1 addition & 1 deletion ide-lsp/src/test/java/org/aya/lsp/LspTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class LspTest {
.filter(x -> x.ref.name().equals("test"))
.firstOption();
assertFalse(testOpt.isEmpty(), "Do not delete the function called test in Vec");
var testClause = ((TeleDecl.BlockBody) testOpt.get().body).clauses().first();
var testClause = ((TeleDecl.BlockBody) testOpt.get().body).clauses().getFirst();
// vnil, ys => 0
var testPat = (Pattern.Bind) testClause.patterns.last().term();
var testTy = assertInstanceOf(DataCall.class, testPat.type().get());
Expand Down