Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Jan 4, 2024
1 parent bf51778 commit faa05f2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
8 changes: 8 additions & 0 deletions base/src/main/java/org/aya/base/core/AppTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ public record AppTerm(@NotNull Term fun, @NotNull Term arg) implements Term {
if (newFun == fun && newArg == arg) return this;
return new AppTerm(newFun, newArg);
}

@Override public @NotNull Term replace(int index, @NotNull Term incoming) {
var newArg = arg.replace(index, incoming);
var newFun = fun.replace(index, incoming);
if (newFun instanceof LamTerm(var body)) return body.instantiate(incoming);
if (newFun == fun && newArg == arg) return this;
return new AppTerm(newFun, newArg);
}
}
4 changes: 4 additions & 0 deletions base/src/main/java/org/aya/base/core/FreeTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public record FreeTerm(@NotNull LocalVar name) implements Term {
if (name == var) return new LocalTerm(depth);
return this;
}

@Override public @NotNull Term replace(int index, @NotNull Term arg) {
return this;
}
}
7 changes: 7 additions & 0 deletions base/src/main/java/org/aya/base/core/LamTerm.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.aya.base.core;

import kala.collection.mutable.MutableList;
import org.aya.base.generic.LocalVar;
import org.jetbrains.annotations.NotNull;

Expand All @@ -9,4 +10,10 @@ public record LamTerm(Term body) implements Term {
if (newBody == body) return this;
return new LamTerm(newBody);
}

@Override public @NotNull Term replace(int index, @NotNull Term arg) {
var newBody = body.replace(index + 1, arg);
if (newBody == body) return this;
return new LamTerm(newBody);
}
}
5 changes: 5 additions & 0 deletions base/src/main/java/org/aya/base/core/LocalTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public record LocalTerm(int index) implements Term {
@Override public @NotNull Term bindAt(@NotNull LocalVar var, int depth) {
return this;
}

@Override public @NotNull Term replace(int incoming, @NotNull Term arg) {
if (index == incoming) return arg;
return this;
}
}
23 changes: 22 additions & 1 deletion base/src/main/java/org/aya/base/core/Term.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@
import java.io.Serializable;

public sealed interface Term extends Serializable
permits AppTerm, FreeTerm, LamTerm, LocalTerm {
permits AppTerm, FreeTerm, LamTerm, LocalTerm {
@ApiStatus.Internal
@NotNull Term bindAt(@NotNull LocalVar var, int depth);

/**
* Corresponds to <emph>abstract</emph> operator in [MM 2004].
* However, <code>abstract</code> is a keyword in Java, so we can't
* use it as a method name.
* <pre>
* abstract :: Name → Expr → Scope
* </pre>
*
* @see #instantiate
*/
default @NotNull Term bind(@NotNull LocalVar var) {
return bindAt(var, 0);
}

@ApiStatus.Internal
@NotNull Term replace(int index, @NotNull Term arg);

/**
* Corresponds to <emph>instantiate</emph> operator in [MM 2004].
* Could be called <code>apply</code>
*/
default @NotNull Term instantiate(Term arg) {
return replace(0, arg);
}
}

0 comments on commit faa05f2

Please sign in to comment.