Skip to content

Commit

Permalink
tyck: class signature
Browse files Browse the repository at this point in the history
Co-authored-by: HoshinoTented <[email protected]>
  • Loading branch information
ice1000 and HoshinoTented committed Jun 9, 2024
1 parent 4662538 commit 96790c3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
44 changes: 43 additions & 1 deletion syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@
// 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;

import kala.collection.Seq;
import kala.collection.SeqView;
import kala.collection.immutable.ImmutableSeq;
import kala.value.LazyValue;
import org.aya.syntax.concrete.stmt.decl.ClassDecl;
import org.aya.syntax.core.term.PiTerm;
import org.aya.syntax.core.term.Term;
import org.aya.syntax.ref.DefVar;
import org.aya.syntax.telescope.AbstractTele;
import org.jetbrains.annotations.NotNull;

/**
* <pre>
* class Cat
* | A : Type
* | Hom (a b : A) : Type
* | id (a : A) : Hom a a
* </pre>
*
* <ul>
* <li>{@code Cat : Type1}, the type of all categories</li>
* <li>{@code Cat A : Type1}, the type of all categories where the object type is A</li>
* <li>{@code Cat A (->) : Type0}, essentially type of {@code A -> A}</li>
* </ul>
*/
public record ClassDef(
@Override @NotNull DefVar<ClassDef, ClassDecl> ref,
@NotNull ImmutableSeq<MemberDef> members
Expand All @@ -21,7 +39,31 @@ public static final class Delegate extends TyckAnyDef<ClassDef> implements Class
public Delegate(@NotNull DefVar<ClassDef, ?> ref) { super(ref); }
@Override public @NotNull ImmutableSeq<MemberDefLike> members() { return members.get(); }

Check warning on line 40 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L40

Added line #L40 was not covered by tests
@Override public @NotNull AbstractTele takeMembers(int size) {
return null;
return new TakeMembers(core(), size);

Check warning on line 42 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L42

Added line #L42 was not covered by tests
}
}

record TakeMembers(@NotNull ClassDef clazz, @Override int telescopeSize) implements AbstractTele {
@Override public boolean telescopeLicit(int i) { return true; }

Check warning on line 47 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L46-L47

Added lines #L46 - L47 were not covered by tests
@Override public @NotNull String telescopeName(int i) {
assert i < telescopeSize;
return clazz.members.get(i).ref().name();

Check warning on line 50 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L50

Added line #L50 was not covered by tests
}
@Override public @NotNull Term telescope(int i, Seq<Term> teleArgs) {
assert i < telescopeSize;
var member = clazz.members.get(i);

Check warning on line 54 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L54

Added line #L54 was not covered by tests
// FIXME: duplicated with somewhere, abstract!
var instedParam = member.telescope().mapIndexed((idx, param) ->
param.type().replaceTeleFrom(idx, teleArgs.view()));
var instedResult = member.result().replaceTeleFrom(instedParam.size(), teleArgs.view());
return PiTerm.make(instedParam.view(), instedResult);

Check warning on line 59 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L56-L59

Added lines #L56 - L59 were not covered by tests
}
@Override public @NotNull Term result(Seq<Term> teleArgs) {
// Use SigmaTerm::lub
throw new UnsupportedOperationException("TODO");

Check warning on line 63 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L63

Added line #L63 was not covered by tests
}
@Override public @NotNull SeqView<String> namesView() {
return clazz.members.sliceView(0, telescopeSize).map(i -> i.ref().name());

Check warning on line 66 in syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java

View check run for this annotation

Codecov / codecov/patch

syntax/src/main/java/org/aya/syntax/core/def/ClassDef.java#L66

Added line #L66 was not covered by tests
}
}
}
3 changes: 3 additions & 0 deletions syntax/src/main/java/org/aya/syntax/core/def/MemberDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.aya.syntax.ref.DefVar;
import org.jetbrains.annotations.NotNull;

/**
* @apiNote the telescope/result of a member probably need to be instantiated, they may refer to the former members.
*/
public record MemberDef(
@NotNull DefVar<ClassDef, ClassDecl> classRef,
@Override @NotNull DefVar<MemberDef, ClassMember> ref,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ record Lift(
@Override public @NotNull AbstractTele lift(int i) { return new Lift(signature, lift + i); }
@Override public @NotNull SeqView<String> namesView() { return signature.namesView(); }
}
record Locns(
@NotNull ImmutableSeq<Param> telescope,
@NotNull Term result
) implements AbstractTele {
record Locns(@NotNull ImmutableSeq<Param> telescope, @NotNull Term result) implements AbstractTele {
@Override public int telescopeSize() { return telescope.size(); }
@Override public boolean telescopeLicit(int i) { return telescope.get(i).explicit(); }
@Override public @NotNull String telescopeName(int i) { return telescope.get(i).name(); }
Expand Down

0 comments on commit 96790c3

Please sign in to comment.