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 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, ...)
- func (g *GlobalEnv) DeclareInductive(name string, ty ast.Term, constrs []Constructor, elim string) 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 Inductive
- type InductiveError
- 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 CheckPositivity ¶
func CheckPositivity(indName string, constructors []Constructor) error
CheckPositivity verifies that an 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
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 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, constrs []Constructor, elim string)
AddInductive adds an inductive type to the environment without validation. For validated addition, use DeclareInductive.
func (*GlobalEnv) DeclareInductive ¶
func (g *GlobalEnv) DeclareInductive(name string, ty ast.Term, constrs []Constructor, elim string) error
DeclareInductive validates and adds an inductive type to the environment. It checks: - The inductive type is well-formed (Sort or Pi chain ending in Sort) - Each constructor type is well-formed (uses Checker API) - Each constructor returns the inductive type applied to parameters - The definition satisfies strict positivity It also generates and registers the eliminator.
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 Inductive ¶
type Inductive struct {
Name string
Type ast.Term
NumParams int // Number of parameters extracted from Type
ParamTypes []ast.Term // Types of each parameter
Constructors []Constructor
Eliminator string // Name of the elimination principle
}
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 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