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

Use bindAllFrom primitively #1282

Merged
merged 3 commits into from
Jan 9, 2025
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
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
17 changes: 13 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,16 @@
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);
}
}
12 changes: 3 additions & 9 deletions syntax/src/main/java/org/aya/syntax/core/term/LocalTerm.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 All @@ -12,14 +12,8 @@ public record LocalTerm(int index) implements Term {
assert index >= 0 : "Sanity check";
}

@Override
public @NotNull Term descent(@NotNull IndexedFunction<Term, Term> f) {
return this;
}

@Override public @NotNull Term bindAt(@NotNull LocalVar var, int depth) {
return this;
}
@Override public @NotNull Term descent(@NotNull IndexedFunction<Term, Term> f) { return this; }
@Override public @NotNull Term bindAt(@NotNull LocalVar var, int depth) { return this; }

@Override public @NotNull Term replaceAllFrom(int from, @NotNull ImmutableSeq<Term> list) {
var i = index - from;
Expand Down
4 changes: 2 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,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 All @@ -17,7 +17,7 @@
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)));
}

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
Loading