Skip to content

Commit

Permalink
term: use bindAllFrom primitively
Browse files Browse the repository at this point in the history
  • Loading branch information
HoshinoTented committed Jan 9, 2025
1 parent 4d0f91c commit c1c3847
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
16 changes: 2 additions & 14 deletions base/src/main/java/org/aya/tyck/tycker/TeleTycker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 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.tyck.tycker;

Expand Down Expand Up @@ -77,19 +77,7 @@ public sealed interface TeleTycker extends Contextful {
*/
@Contract(mutates = "param2")
static void bindTele(ImmutableSeq<LocalVar> binds, MutableSeq<Param> tele) {
final var lastIndex = tele.size() - 1;
// fix some param, say [p]
// we skip the last parameter, which need no binds
for (int i = lastIndex - 1; i >= 0; i--) {
var p = binds.get(i);
// for any other param that is able to refer to [p]
for (int j = i + 1; j < tele.size(); j++) {
var og = tele.get(j);
// j - i is the human distance between [p] and [og]. However, we count from 0
int ii = i, jj = j;
tele.set(j, og.descent(x -> x.bindAt(p, jj - ii - 1)));
}
}
tele.replaceAllIndexed((i, p) -> p.descent(t -> t.bindTele(binds.sliceView(0, i))));
}

@Contract(mutates = "param3")
Expand Down
2 changes: 1 addition & 1 deletion syntax/src/main/java/org/aya/syntax/compile/JitCon.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ protected JitCon(
this.selfTele = selfTele.toImmutableSeq();
}

return Param.substTele(selfTele.view(), ownerArgs.view()).toImmutableSeq();
return Param.instTele(selfTele.view(), ownerArgs.view()).toImmutableSeq();
}
}
4 changes: 2 additions & 2 deletions syntax/src/main/java/org/aya/syntax/core/def/ConDef.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 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.syntax.core.def;

Expand Down Expand Up @@ -55,7 +55,7 @@ public static final class Delegate extends TyckAnyDef<ConDef> implements ConDefL
@Override public int selfTeleSize() { return core().selfTele.size(); }
@Override public int ownerTeleSize() { return core().ownerTele.size(); }
@Override public @NotNull ImmutableSeq<Param> selfTele(@NotNull ImmutableSeq<Term> ownerArgs) {
return Param.substTele(core().selfTele.view(), ownerArgs.view()).toImmutableSeq();
return Param.instTele(core().selfTele.view(), ownerArgs.view()).toImmutableSeq();
}

@Override public @NotNull DataDefLike dataRef() { return core().dataRef; }
Expand Down
18 changes: 14 additions & 4 deletions syntax/src/main/java/org/aya/syntax/core/term/FreeTerm.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 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.syntax.core.term;

import kala.collection.immutable.ImmutableSeq;
import kala.function.IndexedFunction;
import org.aya.syntax.core.term.marker.TyckInternal;
import org.aya.syntax.ref.LocalVar;
Expand All @@ -10,8 +11,17 @@
public record FreeTerm(@NotNull LocalVar name) implements TyckInternal {
public FreeTerm(@NotNull String name) { this(LocalVar.generate(name)); }
@Override public @NotNull Term descent(@NotNull IndexedFunction<Term, Term> f) { return this; }
@Override public @NotNull Term bindAt(@NotNull LocalVar var, int depth) {
if (name == var) return new LocalTerm(depth);
return this;
// @Override public @NotNull Term bindAt(@NotNull LocalVar var, int depth) {
// if (name == var) return new LocalTerm(depth);
// return this;
// }

@Override
public @NotNull Term bindAllFrom(@NotNull ImmutableSeq<LocalVar> vars, int fromDepth) {
var idx = vars.indexOf(this.name);
if (idx == -1) return this;

var realDepth = fromDepth + idx;
return new LocalTerm(realDepth);
}
}
9 changes: 7 additions & 2 deletions syntax/src/main/java/org/aya/syntax/core/term/Param.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 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.syntax.core.term;

import kala.collection.SeqView;
import kala.collection.immutable.ImmutableSeq;
import org.aya.generic.AyaDocile;
import org.aya.pretty.doc.Doc;
import org.aya.syntax.core.RichParam;
Expand All @@ -17,10 +18,14 @@
import java.util.function.UnaryOperator;

public record Param(@NotNull String name, @NotNull Term type, boolean explicit) implements AyaDocile {
public static @NotNull SeqView<Param> substTele(SeqView<Param> tele, SeqView<Term> subst) {
public static @NotNull SeqView<Param> instTele(SeqView<Param> tele, SeqView<Term> subst) {
return tele.mapIndexed((idx, p) -> p.descent(ty -> ty.instTeleFrom(idx, subst)));
}

public static @NotNull SeqView<Param> bindTele(@NotNull SeqView<Param> tele, @NotNull SeqView<LocalVar> vars) {
return tele.mapIndexed((idx, p) -> p.descent(t -> t.bindTele(idx, vars)));

Check warning on line 26 in syntax/src/main/java/org/aya/syntax/core/term/Param.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/term/Param.java#L26

Added line #L26 was not covered by tests
}

public boolean nameEq(@Nullable String otherName) { return name.equals(otherName); }
// public @NotNull Arg<Term> toArg() { return new Arg<>(type, explicit); }
public @NotNull Pat toFreshPat() { return new Pat.Bind(LocalVar.generate(name), type); }
Expand Down
23 changes: 16 additions & 7 deletions syntax/src/main/java/org/aya/syntax/core/term/Term.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 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.syntax.core.term;

Expand Down Expand Up @@ -34,7 +34,16 @@ public sealed interface Term extends Serializable, AyaDocile
}

default @NotNull Term bindAt(@NotNull LocalVar var, int depth) {
return descent((i, t) -> t.bindAt(var, depth + i));
return bindAllFrom(ImmutableSeq.of(var), depth);
}

/// Bind all [LocalVar] since `fromDepth`,
/// the i-th [LocalVar] in `vars` will be replaced by a [LocalTerm] with index `fromDepth + i`.
///
/// @see #replaceAllFrom
default @NotNull Term bindAllFrom(@NotNull ImmutableSeq<LocalVar> vars, int fromDepth) {
if (vars.isEmpty()) return this;
return descent((i, t) -> t.bindAllFrom(vars, fromDepth + i));
}

/**
Expand All @@ -60,17 +69,17 @@ public sealed interface Term extends Serializable, AyaDocile
*/
default @NotNull Term bindTele(int depth, @NotNull SeqView<LocalVar> teleVars) {
if (teleVars.isEmpty()) return this;
return teleVars.reversed().foldLeftIndexed(this,
(idx, acc, var) -> acc.bindAt(var, depth + idx));
return bindAllFrom(teleVars.reversed().toImmutableSeq(), depth);
}

default @NotNull Term bindTele(@NotNull SeqView<LocalVar> teleVars) {
return bindTele(0, teleVars);
}

/**
* Replacing indexes from {@code from} to {@code from + list.size()} (exclusive) with {@code list}
*/
/// Replacing indexes from `from` to `from + list.size()` (exclusive) with `list`,
/// a [LocalTerm] with index `from + i` will be replaced by `list[i]` if possible.
///
/// @see #bindAllFrom
@ApiStatus.Internal
default @NotNull Term replaceAllFrom(int from, @NotNull ImmutableSeq<Term> list) {
if (list.isEmpty()) return this;
Expand Down

0 comments on commit c1c3847

Please sign in to comment.