lang::typecheck::type_kinds

lang/src/typecheck/type_kinds.mo

Type-level multiplicity kinds: #[linear] and #[proof] inductives.

Part of the view-types milestone (plans/type-system/view-types.md) and the QTT #[linear] item (quantitative-types.md Phase 7). Both are ordinary attributes already carried on Inductive.attrs, so this module is a classifier, not a parser change:

  • #[linear] — every binder whose type is this inductive is used exactly once. ?x may relax it to affine at a binding site; %x is an error. A Copy/Clone instance for it is rejected.
  • #[proof] — stronger: every binder is linear, never Copy, and the type has no run-time representation (in v1 it is also not constructible, so no value exists).

Recognition is by ATTRIBUTE, not by name — the #[shared] precedent (usage.mo's shared_inductive). The sets are built once per module from the scope's inductives, keyed by both the rendered full name and the bare last segment, because an elaborated type head is usually the bare identifier (Cell, not init::view::Cell).

structTypeKinds#
pub struct TypeKinds {
    proof : HashMap String Bool,
    linear : HashMap String Bool,
}

The two name sets, built once per module.

deftype_kinds_empty#
pub def type_kinds_empty : TypeKinds

No #[linear]/#[proof] type in scope. The affine rule's historical entry points use this so their behaviour is unchanged when no kinds are involved (e.g. the report's unit tests).

deftype_kinds_proof#
pub def type_kinds_proof (k : TypeKinds) : HashMap String Bool
deftype_kinds_linear#
pub def type_kinds_linear (k : TypeKinds) : HashMap String Bool
deftype_kinds#
pub def type_kinds (s : Scope) : TypeKinds

Scan the scope's inductives once. A struct's synthetic inductive is included (its attrs are carried by build_scope_struct; see that function's own comment).

Both spellings an elaborated type head can arrive as are registered: the full rendered name and the bare last segment. The bare key is what makes classification work at all (an elaborated head is usually Cell, not init::view::Cell).

Deliberately over-approximating. If a user declares an unrelated type also named Cell (or At), a bare reference is treated as the kinded one too. That is a loud false positive (the affine/view rules may reject valid code) rather than a silent fail-open: dropping ambiguous bare names would disable #[linear]/#[proof] enforcement for the real type in any module that happens to import a collision. Exact resolution through scope_resolve_name is the durable fix.

defkind_head_name#
pub def kind_head_name (t : Term) : Option String

The head name of a type term, peeling ctx and the application spine. A bare or qualified Term.var is the only shape that names a type.

defis_proof_type#
pub def is_proof_type (k : TypeKinds) (t : Term) : Bool
defis_linear_type#
pub def is_linear_type (k : TypeKinds) (t : Term) : Bool
defis_linear_or_proof#
pub def is_linear_or_proof (k : TypeKinds) (t : Term) : Bool
defmentions_linear_or_proof#
pub def mentions_linear_or_proof (k : TypeKinds) (t : Term) : Bool

Does any type head ANYWHERE in t name a linear or proof kind? Used by the container-shape rule: a Shared/Ref/Array wrapping one is unrepresentable because those are re-readable (and Shared is Copy), so they could hand out two handles to one allocation.

defis_container_head#
pub def is_container_head (t : Term) : Bool

Is this type term a Shared/Ref/Array application? Only the spine head matters; the argument is checked by the caller.

defcontainer_wraps_linear#
pub def container_wraps_linear (k : TypeKinds) (t : Term) : Bool

A container (Shared/Ref/Array) applied to a type that mentions a linear or proof kind, anywhere in the type expression.