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

Commit

Permalink
term: def (unable to pass the compilation though)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoshinoTented committed Jan 13, 2024
1 parent b4889a6 commit 4622634
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
23 changes: 22 additions & 1 deletion base/src/main/java/org/aya/syntax/core/def/Def.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// 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.syntax.core.def;

public interface Def {
import org.aya.generic.AyaDocile;
import org.aya.pretty.doc.Doc;
import org.aya.syntax.core.term.Term;
import org.aya.syntax.ref.DefVar;
import org.aya.util.prettier.PrettierOptions;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

/**
* @author zaoqi
*/
public sealed interface Def extends AyaDocile permits ClassDef, TeleDef {
@NotNull DefVar<?, ?> ref();

void descentConsume(@NotNull Consumer<Term> f, @NotNull Consumer<Pat> g);

@Override default @NotNull Doc toDoc(@NotNull PrettierOptions options) {
throw new UnsupportedOperationException("TODO");
}
}
50 changes: 50 additions & 0 deletions base/src/main/java/org/aya/syntax/core/def/TeleDef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.syntax.core.def;

import kala.collection.Seq;
import kala.collection.immutable.ImmutableSeq;
import org.aya.syntax.concrete.stmt.decl.Decl;
import org.aya.syntax.concrete.stmt.decl.TeleDecl;
import org.aya.syntax.core.term.PiTerm;
import org.aya.syntax.core.term.Term;
import org.aya.syntax.ref.DefVar;
import org.aya.util.Arg;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* @author ice1000
*/
public sealed interface TeleDef extends Def permits SubLevelDef, TopLevelDef {
static @NotNull Term defType(@NotNull DefVar<? extends TeleDef, ? extends TeleDecl<?>> defVar) {
return PiTerm.make(defTele(defVar), defResult(defVar));
}

static @NotNull ImmutableSeq<Arg<Term>> defTele(@NotNull DefVar<? extends TeleDef, ? extends TeleDecl<?>> defVar) {
if (defVar.core != null) return defVar.core.telescope();
// guaranteed as this is already a core term
var signature = defVar.concrete.signature;
assert signature != null : defVar.name();
return signature.param();
}
static @NotNull Seq<CtorDef> dataBody(@NotNull DefVar<? extends DataDef, ? extends TeleDecl.DataDecl> defVar) {
if (defVar.core != null) return defVar.core.body;
// guaranteed as this is already a core term
else return defVar.concrete.checkedBody;
}

@SuppressWarnings("unchecked") @Contract(pure = true)
static <T extends Term> @NotNull T
defResult(@NotNull DefVar<? extends TeleDef, ? extends TeleDecl<? extends T>> defVar) {
if (defVar.core != null) return (T) defVar.core.result();
// guaranteed as this is already a core term
else return Objects.requireNonNull(defVar.concrete.signature).result();
}

@Override @NotNull DefVar<? extends TeleDef, ? extends Decl> ref();
@NotNull Term result();
@NotNull ImmutableSeq<Arg<Term>> telescope();
}

0 comments on commit 4622634

Please sign in to comment.