Documentation
¶
Overview ¶
Package check implements bidirectional type checking for the HoTT kernel.
Bidirectional type checking uses two modes:
- Synthesis (Synth): infer the type of a term
- Checking (Check): verify a term has an expected type
This approach provides better error messages by tracking source positions and reduces the annotation burden by propagating type information.
References:
- Dunfield, J. and Krishnaswami, N. "Bidirectional Typing"
- Löh, A. et al. "A Tutorial Implementation of a Dependently Typed Lambda Calculus"
Index ¶
- func CheckMutualPositivity(indNames []string, constructors map[string][]Constructor) error
- func CheckPositivity(indName string, constructors []Constructor) error
- func GenerateRecursorType(ind *Inductive) ast.Term
- func GenerateRecursorTypeSimple(ind *Inductive) ast.Term
- func IsRecursiveArg(indName string, argType ast.Term) bool
- func OccursIn(name string, ty ast.Term) bool
- type Axiom
- type CannotInferDetails
- type Checker
- func (c *Checker) Check(ctx *tyctx.Ctx, span Span, term ast.Term, expected ast.Term) *TypeError
- func (c *Checker) CheckIVar(ix int) bool
- func (c *Checker) CheckIsType(ctx *tyctx.Ctx, span Span, term ast.Term) (ast.Level, *TypeError)
- func (c *Checker) Globals() *GlobalEnv
- func (c *Checker) ICtxDepth() int
- func (c *Checker) InferAndCheck(ctx *tyctx.Ctx, span Span, term ast.Term, expected ast.Term) *TypeError
- func (c *Checker) PushIVar() func()
- func (c *Checker) Synth(ctx *tyctx.Ctx, span Span, term ast.Term) (ast.Term, *TypeError)
- type Constructor
- type ConstructorError
- type Definition
- type ErrorDetails
- type ErrorKind
- type GlobalEnv
- func (g *GlobalEnv) AddAxiom(name string, ty ast.Term)
- func (g *GlobalEnv) AddDefinition(name string, ty, body ast.Term, trans Transparency)
- func (g *GlobalEnv) AddInductive(name string, ty ast.Term, numParams int, paramTypes []ast.Term, numIndices int, ...)
- func (g *GlobalEnv) DeclareInductive(name string, ty ast.Term, constrs []Constructor, elim string) error
- func (g *GlobalEnv) DeclareMutual(specs []MutualInductiveSpec) error
- func (g *GlobalEnv) Has(name string) bool
- func (g *GlobalEnv) LookupDefinitionBody(name string) (ast.Term, bool)
- func (g *GlobalEnv) LookupType(name string) ast.Term
- func (g *GlobalEnv) Order() []string
- type ICtx
- type Inductive
- type InductiveError
- type MutualInductiveSpec
- type NotAFunctionDetails
- type NotAPairDetails
- type NotATypeDetails
- type PiArg
- type Polarity
- type Pos
- type PositivityError
- type Primitive
- type Span
- type Transparency
- type TypeError
- type TypeMismatchDetails
- type UnboundVariableDetails
- type UnknownGlobalDetails
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckMutualPositivity ¶ added in v1.6.0
func CheckMutualPositivity(indNames []string, constructors map[string][]Constructor) error
CheckMutualPositivity verifies that mutually recursive inductive types satisfy the strict positivity condition across all types in the mutual block.
For mutual inductives, each type must occur strictly positively in the constructors of ALL types in the mutual block. That is: - For each type T in the mutual block - For each constructor C of any type in the block - T must occur only in strictly positive positions in C
func CheckPositivity ¶
func CheckPositivity(indName string, constructors []Constructor) error
CheckPositivity verifies that a single inductive type definition satisfies the strict positivity condition. This ensures the inductive is well-founded and prevents logical inconsistencies.
A type T occurs strictly positively in X if: - X does not mention T, OR - X = T (the type itself), OR - X = (a : A) -> B where T does NOT occur in A and occurs strictly positively in B
func GenerateRecursorType ¶
GenerateRecursorType generates the type of the eliminator for an inductive type.
For an inductive T with constructors c1, ..., cn, the recursor has type:
T-elim : (P : T -> Type_j) -> case(c1) -> ... -> case(cn) -> (t : T) -> P t
For parameterized inductives like List : Type -> Type:
listElim : (A : Type) -> (P : List A -> Type) -> case_nil -> case_cons -> (xs : List A) -> P xs
For indexed inductives like Vec : Type -> Nat -> Type:
vecElim : (A : Type) -> (P : (n : Nat) -> Vec A n -> Type) ->
case_vnil -> case_vcons -> (n : Nat) -> (xs : Vec A n) -> P n xs
where case(ci) provides the elimination principle for constructor ci:
- Non-recursive args pass through as-is
- Recursive args (of type T) get an induction hypothesis (ih : P indices arg)
The universe j for the motive is derived from the inductive's type.
func GenerateRecursorTypeSimple ¶
GenerateRecursorTypeSimple generates a simpler recursor type without complex index manipulation. This is an alternative implementation that's easier to understand but may have subtle bugs.
func IsRecursiveArg ¶
IsRecursiveArg checks if a constructor argument type contains a reference to the inductive type being defined.
Types ¶
type CannotInferDetails ¶
CannotInferDetails provides information about terms whose type cannot be inferred.
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker performs bidirectional type checking.
func NewChecker ¶
NewChecker creates a new type checker with the given global environment.
func NewCheckerWithEta ¶
NewCheckerWithEta creates a checker with eta-equality enabled.
func NewCheckerWithPrimitives ¶
func NewCheckerWithPrimitives() *Checker
NewCheckerWithPrimitives creates a checker with built-in Nat and Bool types.
func (*Checker) Check ¶
Check verifies that a term has the expected type. Returns nil on success. If ctx is nil, an empty context is used.
func (*Checker) CheckIVar ¶
CheckIVar validates an interval variable index against the current context. Returns true if valid, false if invalid or outside a path context.
func (*Checker) CheckIsType ¶
CheckIsType verifies that a term is a well-formed type. Returns the universe level and nil error on success. If ctx is nil, an empty context is used.
func (*Checker) ICtxDepth ¶
ICtxDepth returns the current interval context depth (0 if not tracking).
func (*Checker) InferAndCheck ¶
func (c *Checker) InferAndCheck(ctx *tyctx.Ctx, span Span, term ast.Term, expected ast.Term) *TypeError
InferAndCheck is a convenience that synthesizes a type and checks it against expected. If ctx is nil, an empty context is used.
type Constructor ¶
Constructor represents an inductive type constructor.
type ConstructorError ¶
ConstructorError represents an error in constructor validation.
func (*ConstructorError) Error ¶
func (e *ConstructorError) Error() string
type Definition ¶
Definition represents a defined constant with type and body.
type ErrorDetails ¶
type ErrorDetails interface {
// contains filtered or unexported methods
}
ErrorDetails provides additional context for specific error kinds.
type GlobalEnv ¶
type GlobalEnv struct {
// contains filtered or unexported fields
}
GlobalEnv holds all global declarations in dependency order.
func NewGlobalEnv ¶
func NewGlobalEnv() *GlobalEnv
NewGlobalEnv creates an empty global environment.
func NewGlobalEnvWithPrimitives ¶
func NewGlobalEnvWithPrimitives() *GlobalEnv
NewGlobalEnvWithPrimitives creates a global environment with Nat and Bool.
func (*GlobalEnv) AddDefinition ¶
func (g *GlobalEnv) AddDefinition(name string, ty, body ast.Term, trans Transparency)
AddDefinition adds a definition to the environment.
func (*GlobalEnv) AddInductive ¶
func (g *GlobalEnv) AddInductive(name string, ty ast.Term, numParams int, paramTypes []ast.Term, numIndices int, indexTypes []ast.Term, constrs []Constructor, elim string, mutualGroup []string)
AddInductive adds an inductive type to the environment without validation. For validated addition, use DeclareInductive or DeclareMutual.
func (*GlobalEnv) DeclareInductive ¶
func (g *GlobalEnv) DeclareInductive(name string, ty ast.Term, constrs []Constructor, elim string) error
DeclareInductive validates and adds a single inductive type to the environment. This is a convenience wrapper around DeclareMutual for non-mutual inductives.
func (*GlobalEnv) DeclareMutual ¶ added in v1.6.0
func (g *GlobalEnv) DeclareMutual(specs []MutualInductiveSpec) error
DeclareMutual validates and adds mutually recursive inductive types. For a single inductive, use DeclareInductive for convenience.
It checks: - Each inductive type is well-formed (Sort or Pi chain ending in Sort) - Each constructor type is well-formed (can reference any type in the mutual block) - Each constructor returns its inductive type applied to params/indices - The definition satisfies strict positivity across all mutual types It also generates and registers eliminators for each type.
func (*GlobalEnv) LookupDefinitionBody ¶
LookupDefinitionBody returns the body of a definition if transparent.
func (*GlobalEnv) LookupType ¶
LookupType returns the type of a global name, or nil if not found.
type ICtx ¶ added in v1.6.0
type ICtx struct {
// contains filtered or unexported fields
}
ICtx tracks interval variable bindings for cubical type checking. Interval variables are tracked separately from term variables.
type Inductive ¶
type Inductive struct {
Name string
Type ast.Term
NumParams int // Number of parameters (uniform across constructors)
ParamTypes []ast.Term // Types of each parameter
NumIndices int // Number of indices (vary per constructor)
IndexTypes []ast.Term // Types of each index (under param binders)
Constructors []Constructor
Eliminator string // Name of the elimination principle
MutualGroup []string // Names of other types in mutual block (nil for single inductives)
}
Inductive represents an inductive type definition.
type InductiveError ¶
InductiveError represents an error in inductive type validation.
func (*InductiveError) Error ¶
func (e *InductiveError) Error() string
type MutualInductiveSpec ¶ added in v1.6.0
type MutualInductiveSpec struct {
Name string
Type ast.Term
Constructors []Constructor
Eliminator string
}
MutualInductiveSpec specifies one inductive type in a mutual block.
type NotAFunctionDetails ¶
NotAFunctionDetails provides information when a non-function is applied.
type NotAPairDetails ¶
NotAPairDetails provides information when projecting from a non-pair.
type NotATypeDetails ¶
NotATypeDetails provides information when a type was expected.
type Pos ¶
type Pos struct {
Line int // 1-indexed line number
Column int // 1-indexed column number
Offset int // 0-indexed byte offset
}
Pos represents a position in source code.
type PositivityError ¶
PositivityError represents a strict positivity violation.
func (*PositivityError) Error ¶
func (e *PositivityError) Error() string
type Span ¶
Span represents a range in source code.
type Transparency ¶
type Transparency int
Transparency controls whether a definition can be unfolded during conversion.
const ( Opaque Transparency = iota // Never unfold Transparent // Always unfold during conversion )
type TypeError ¶
type TypeError struct {
Span Span
Kind ErrorKind
Message string
Details ErrorDetails
}
TypeError represents a type checking error with location and details.
type TypeMismatchDetails ¶
TypeMismatchDetails provides information about type mismatches.
type UnboundVariableDetails ¶
type UnboundVariableDetails struct {
Index int
}
UnboundVariableDetails provides information about unbound variables.
type UnknownGlobalDetails ¶
type UnknownGlobalDetails struct {
Name string
}
UnknownGlobalDetails provides information about unknown global names.
type ValidationError ¶
type ValidationError struct {
Msg string
}
ValidationError represents a validation error during inductive declaration.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string