Section 2: Affine Non-Size-Increasing Types, Terms, and Head-Controlled Reduction
DefinitionAehligSchwichtenberg2002_SyntaxFormalizes Section 2 of Aehlig and Schwichtenberg (2002): types ρ, σ ::= B | ⋄ | ρ ⊸ σ | !ρ → σ (with B = L(⋄)), constructors c_i, destructors d_i, higher-type iteration it_ρ (for !-free ρ), explicit affine typing Δ; Γ ⊢ r : ρ, and the head-controlled one-step reduction relation ↦ (Definition 2.4) that prevents duplication of unreduced !-arguments during recursion.
import Std
/-!
# Syntax, Typing, and Reductions for Aehlig–Schwichtenberg (2002, Sections 2–3)
Source: Klaus Aehlig and Helmut Schwichtenberg,
"A syntactical analysis of non-size-increasing polynomial time computation",
*Proceedings of the 15th IEEE Symposium on Logic in Computer Science (LICS 2000)*, pp. 84–91;
expanded version in *ACM Transactions on Computational Logic* 3(3), 2002, pp. 383–401
(arXiv:cs/0011037v2), Sections 2–3 (pp. 3–9).
This module formalizes:
- Definition 2.1: Finite linear types (`Ty`).
- Definition 2.2 & 2.3: Raw terms (`Term`) and constructor symbols (`Constructor`).
- Definition 2.4: Context-indexed affine linear typing `HasType Γ t τ` (`Γ ⊢ t^τ`), where
eliminations for `×`, `B`, `⊗`, and `L(τ)` are written in application notation.
- Definition 3.1, 3.2, 3.3: Lists (`listEntryCount`, `IsListTerm`), conversions (`ConvStep`),
one-step and multi-step reduction (`RedStep`, `RedStar`, `RedSteps`), normal forms (`IsNormal`),
and almost closed terms (`IsAlmostClosed`).
-/
namespace AehligSchwichtenberg2002
/-- Definition 2.1 (Finite types, p. 3):
`ρ, τ ::= ⋄ | B | τ ⊸ ρ | τ ⊗ ρ | τ × ρ | L(τ)`. -/
inductive Ty where
| diamond : Ty
| bool : Ty
| lolli : Ty → Ty → Ty
| tensor : Ty → Ty → Ty
| prod : Ty → Ty → Ty
| list : Ty → Ty
deriving DecidableEq, Repr
namespace Ty
/-- Binary numerals `L(B)` (Section 2, p. 3 & Corollary 4.9, p. 14). -/
def bin : Ty := .list .bool
/-- Iterated linear function type `τ₁ ⊸ ⋯ ⊸ τₙ ⊸ ρ`. -/
def lolliMany : List Ty → Ty → Ty
| [], ρ => ρ
| τ :: τs, ρ => .lolli τ (lolliMany τs ρ)
end Ty
/-- Typed variable `x^τ` (Section 2, p. 4). -/
structure Var where
id : Nat
ty : Ty
deriving DecidableEq, Repr
/-- Definition 2.3 (Constructor symbols, p. 4):
`tt : B`, `ff : B`, `nil_τ : L(τ)`, `cons_τ : ⋄ ⊸ τ ⊸ L(τ) ⊸ L(τ)`, `⊗_{τ,ρ} : τ ⊸ ρ ⊸ τ ⊗ ρ`. -/
inductive Constructor where
| tt : Constructor
| ff : Constructor
| nil : Ty → Constructor
| cons : Ty → Constructor
| tensorPair : Ty → Ty → Constructor
deriving DecidableEq, Repr
/-- Type assignment for constructor symbols (Definition 2.3, p. 4). -/
def Constructor.ty : Constructor → Ty
| .tt => .bool
| .ff => .bool
| .nil τ => .list τ
| .cons τ => .lolli .diamond (.lolli τ (.lolli (.list τ) (.list τ)))
| .tensorPair τ ρ => .lolli τ (.lolli ρ (.tensor τ ρ))
/-- Definition 2.2 (Terms, p. 4):
`r, s, t ::= x^τ | c | λx^τ. t | ⟨t, s⟩ | t s | {t}`. -/
inductive Term where
| var : Var → Term
| const : Constructor → Term
| lam : Var → Term → Term
| pair : Term → Term → Term
| app : Term → Term → Term
| brace : Term → Term
deriving DecidableEq, Repr
namespace Term
/-- Multi-argument application `t s₁ ... sₙ`. -/
def appMany : Term → List Term → Term
| t, [] => t
| t, s :: ss => appMany (.app t s) ss
end Term
/-- A context `Γ` is a finite set of variables (Section 2, p. 4), represented as a list. -/
abbrev Context := List Var
/-- Disjointness of two contexts `Γ₁ ∩ Γ₂ = ∅`. -/
def DisjointCtx (Γ₁ Γ₂ : Context) : Prop :=
∀ v, v ∈ Γ₁ → v ∉ Γ₂
/-- Set-extensional union `Γ = Γ₁ ∪ Γ₂` with no disjointness requirement. -/
def IsCtxUnion (Γ₁ Γ₂ Γ : Context) : Prop :=
∀ v, v ∈ Γ ↔ (v ∈ Γ₁ ∨ v ∈ Γ₂)
/-- Set-extensional disjoint union `Γ = Γ₁, Γ₂` (Section 2, p. 4:
"For two contexts `Γ₁` and `Γ₂` the notation `Γ₁, Γ₂` stands for the union, expressing that
`Γ₁` and `Γ₂` are disjoint."). -/
def IsDisjointCtxUnion (Γ₁ Γ₂ Γ : Context) : Prop :=
DisjointCtx Γ₁ Γ₂ ∧ IsCtxUnion Γ₁ Γ₂ Γ
/-- Free variable occurrences of a term `t`. -/
def freeVarOccurrences : Term → List Var
| .var x => [x]
| .const _ => []
| .lam x t => (freeVarOccurrences t).filter (· != x)
| .pair t s => freeVarOccurrences t ++ freeVarOccurrences s
| .app t s => freeVarOccurrences t ++ freeVarOccurrences s
| .brace t => freeVarOccurrences t
/-- Underlying set of free variables `FV(t)` (duplicate-free list). -/
def freeVars (t : Term) : List Var :=
(freeVarOccurrences t).eraseDups
/-- Cardinality `|FV(t)|` of the set of free variables of `t`. -/
def freeVarCard (t : Term) : Nat :=
(freeVars t).length
/-- Almost closed terms (Section 3, p. 8): terms with free variables of type `⋄` only. -/
def IsAlmostClosed (t : Term) : Prop :=
∀ v ∈ freeVars t, v.ty = .diamond
/-- Closed terms: terms with no free variables. -/
def IsClosed (t : Term) : Prop :=
freeVars t = []
/-- Maximum variable id occurring anywhere in a term. -/
def maxVarId : Term → Nat
| .var x => x.id
| .const _ => 0
| .lam x t => Nat.max x.id (maxVarId t)
| .pair t s => Nat.max (maxVarId t) (maxVarId s)
| .app t s => Nat.max (maxVarId t) (maxVarId s)
| .brace t => maxVarId t
/-- Rename free occurrences of `x` to `y` in `t`. -/
def renameFreeVar (t : Term) (x y : Var) : Term :=
match t with
| .var v => if v == x then .var y else .var v
| .const c => .const c
| .lam v body => if v == x then .lam v body else .lam v (renameFreeVar body x y)
| .pair r s => .pair (renameFreeVar r x y) (renameFreeVar s x y)
| .app r s => .app (renameFreeVar r x y) (renameFreeVar s x y)
| .brace r => .brace (renameFreeVar r x y)
/-- Structural size of a raw term used for termination of capture-avoiding substitution. -/
def rawSize : Term → Nat
| .var _ => 1
| .const _ => 1
| .lam _ t => rawSize t + 1
| .pair t s => rawSize t + rawSize s + 1
| .app t s => rawSize t + rawSize s + 1
| .brace t => rawSize t + 1
theorem renameFreeVar_rawSize (t : Term) (x y : Var) :
rawSize (renameFreeVar t x y) = rawSize t := by
induction t with
| var v => simp only [renameFreeVar]; split <;> rfl
| const c => rfl
| lam v body ih =>
simp only [renameFreeVar]
split
· rfl
· simp [rawSize, ih]
| pair r s ihr ihs => simp [renameFreeVar, rawSize, ihr, ihs]
| app r s ihr ihs => simp [renameFreeVar, rawSize, ihr, ihs]
| brace r ihr => simp [renameFreeVar, rawSize, ihr]
/-- Capture-avoiding substitution `t[s/x]` (Section 2, p. 4 & Definition 3.2, p. 7). -/
def substTerm (t : Term) (x : Var) (s : Term) : Term :=
match t with
| .var v => if v == x then s else .var v
| .const c => .const c
| .lam v body =>
if v == x then
.lam v body
else if v ∈ freeVarOccurrences s then
let freshId := Nat.max (maxVarId body) (Nat.max (maxVarId s) x.id) + 1
let v' : Var := ⟨freshId, v.ty⟩
.lam v' (substTerm (renameFreeVar body v v') x s)
else
.lam v (substTerm body x s)
| .pair r u => .pair (substTerm r x s) (substTerm u x s)
| .app r u => .app (substTerm r x s) (substTerm u x s)
| .brace r => .brace (substTerm r x s)
termination_by rawSize t
decreasing_by
all_goals
simp_wf
simp [rawSize, renameFreeVar_rawSize]
try omega
/-- Simultaneous substitution `r[t, s / x, y]` for two distinct variables `x, y`
(used in `⊗_{τ,ρ} t s (λx^τ, y^ρ. r) ↦ r[t, s / x, y]`, Definition 3.2, p. 7). -/
def substTwo (r : Term) (x y : Var) (t s : Term) : Term :=
if y ∈ freeVarOccurrences t then
let freshId := Nat.max (maxVarId r) (Nat.max (maxVarId t) (Nat.max (maxVarId s) (Nat.max x.id y.id))) + 1
let y' : Var := ⟨freshId, y.ty⟩
substTerm (substTerm (renameFreeVar r y y') x t) y' s
else
substTerm (substTerm r x t) y s
/-- Definition 2.4 (Typing `Γ ⊢ t^τ`, pp. 4–5). -/
inductive HasType : Context → Term → Ty → Prop where
/-- `(Var)`: `Γ, x^τ ⊢ x^τ` -/
| var {Γ Γ' : Context} (x : Var) :
IsDisjointCtxUnion Γ [x] Γ' →
HasType Γ' (.var x) x.ty
/-- `(Const)`: `Γ ⊢ c^τ` -/
| const (Γ : Context) (c : Constructor) :
HasType Γ (.const c) c.ty
/-- `(⊸⁺)`: from `Γ ∪ {x^τ} ⊢ t^ρ` infer `Γ ⊢ (λx^τ. t)^{τ⊸ρ}` -/
| lam {Γ Γ' : Context} (x : Var) {t : Term} {ρ : Ty} :
IsCtxUnion Γ [x] Γ' →
HasType Γ' t ρ →
HasType Γ (.lam x t) (.lolli x.ty ρ)
/-- `(⊸⁻)`: from `Γ₁ ⊢ t^{τ⊸ρ}` and `Γ₂ ⊢ s^τ` infer `Γ₁, Γ₂ ⊢ (t s)^ρ` -/
| app {Γ₁ Γ₂ Γ : Context} {t s : Term} {τ ρ : Ty} :
IsDisjointCtxUnion Γ₁ Γ₂ Γ →
HasType Γ₁ t (.lolli τ ρ) →
HasType Γ₂ s τ →
HasType Γ (.app t s) ρ
/-- `(×⁺)`: from `Γ ⊢ t^τ` and `Γ ⊢ s^ρ` infer `Γ ⊢ ⟨t, s⟩^{τ×ρ}` -/
| pair {Γ : Context} {t s : Term} {τ ρ : Ty} :
HasType Γ t τ →
HasType Γ s ρ →
HasType Γ (.pair t s) (.prod τ ρ)
/-- `(×₁⁻)`: from `Γ ⊢ t^{τ×ρ}` infer `Γ ⊢ (t tt)^τ` -/
| projLeft {Γ : Context} {t : Term} {τ ρ : Ty} :
HasType Γ t (.prod τ ρ) →
HasType Γ (.app t (.const .tt)) τ
/-- `(×₀⁻)`: from `Γ ⊢ t^{τ×ρ}` infer `Γ ⊢ (t ff)^ρ` -/
| projRight {Γ : Context} {t : Term} {τ ρ : Ty} :
HasType Γ t (.prod τ ρ) →
HasType Γ (.app t (.const .ff)) ρ
/-- `(B⁻)`: from `Γ₁ ⊢ t^B`, `Γ₂ ⊢ s^τ`, `Γ₂ ⊢ r^τ` infer `Γ₁, Γ₂ ⊢ (t ⟨s, r⟩)^τ` -/
| boolElim {Γ₁ Γ₂ Γ : Context} {t s r : Term} {τ : Ty} :
IsDisjointCtxUnion Γ₁ Γ₂ Γ →
HasType Γ₁ t .bool →
HasType Γ₂ s τ →
HasType Γ₂ r τ →
HasType Γ (.app t (.pair s r)) τ
/-- `(⊗⁻)`: from `Γ₁ ⊢ t^{τ⊗ρ}` and `Γ₂, x^τ, y^ρ ⊢ s^σ` infer `Γ₁, Γ₂ ⊢ (t (λx^τ, y^ρ. s))^σ` -/
| tensorElim {Γ₁ Γ₂ Γ₂' Γ : Context} {t s : Term} (x y : Var) {σ : Ty} :
x ≠ y →
IsDisjointCtxUnion Γ₂ [x, y] Γ₂' →
IsDisjointCtxUnion Γ₁ Γ₂ Γ →
HasType Γ₁ t (.tensor x.ty y.ty) →
HasType Γ₂' s σ →
HasType Γ (.app t (.lam x (.lam y s))) σ
/-- `(L(τ)⁻)`: from `Γ ⊢ t^{L(τ)}` and `∅ ⊢ s^{⋄⊸τ⊸ρ⊸ρ}` infer `Γ ⊢ (t {s})^{ρ⊸ρ}` -/
| listElim {Γ : Context} {t s : Term} {τ ρ : Ty} :
HasType Γ t (.list τ) →
HasType [] s (.lolli .diamond (.lolli τ (.lolli ρ ρ))) →
HasType Γ (.app t (.brace s)) (.lolli ρ ρ)
/-- Definition 3.1 (Lists, p. 7):
Lists `ℓ` (with `n` entries) are terms of the form `cons_τ d₁ a₁ (... (cons_τ dₙ aₙ nil_τ))`. -/
def listEntryCount : Term → Option Nat
| .const (.nil _) => some 0
| .app (.app (.app (.const (.cons _)) _) _) l =>
match listEntryCount l with
| some k => some (k + 1)
| none => none
| _ => none
/-- Predicate asserting that `l` is a list in the sense of Definition 3.1 (p. 7). -/
def IsListTerm (l : Term) : Prop :=
∃ n, listEntryCount l = some n
/-- Definition 3.2 (Conversions, p. 7): `t ↦ t'`. -/
inductive ConvStep : Term → Term → Prop where
| beta (x : Var) (t s : Term) :
ConvStep (.app (.lam x t) s) (substTerm t x s)
| projTt (t s : Term) :
ConvStep (.app (.pair t s) (.const .tt)) t
| projFf (t s : Term) :
ConvStep (.app (.pair t s) (.const .ff)) s
| ifTt (t s : Term) :
ConvStep (.app (.const .tt) (.pair t s)) t
| ifFf (t s : Term) :
ConvStep (.app (.const .ff) (.pair t s)) s
| tensorBeta (τ ρ : Ty) (t s : Term) (x y : Var) (r : Term) :
x.ty = τ → y.ty = ρ → x ≠ y →
ConvStep
(.app (.app (.app (.const (.tensorPair τ ρ)) t) s) (.lam x (.lam y r)))
(substTwo r x y t s)
| iterNil (τ : Ty) (t s : Term) :
ConvStep (.app (.app (.const (.nil τ)) (.brace t)) s) s
| iterCons (τ : Ty) (d a l t s : Term) :
IsListTerm l →
ConvStep
(.app (.app (.app (.app (.app (.const (.cons τ)) d) a) l) (.brace t)) s)
(.app (.app (.app t d) a) (.app (.app l (.brace t)) s))
/-- Definition 3.3 (Reduction `t → t'`, p. 8). -/
inductive RedStep : Term → Term → Prop where
| conv {t t' : Term} :
ConvStep t t' → RedStep t t'
| appLeft {t t' : Term} (s : Term) :
RedStep t t' → RedStep (.app t s) (.app t' s)
| appRight (t : Term) {s s' : Term} :
RedStep s s' → RedStep (.app t s) (.app t s')
/-- Reflexive-transitive closure of reduction `t →* t'`. -/
inductive RedStar : Term → Term → Prop where
| refl (t : Term) : RedStar t t
| step {t s u : Term} : RedStep t s → RedStar s u → RedStar t u
/-- Exact `k`-step reduction chain `t →^k t'`. -/
inductive RedSteps : Nat → Term → Term → Prop where
| zero (t : Term) : RedSteps 0 t t
| succ {k : Nat} {t s u : Term} :
RedStep t s → RedSteps k s u → RedSteps (k + 1) t u
/-- A term `t` is normal if there is no `t'` such that `t → t'` (Section 3, p. 8). -/
def IsNormal (t : Term) : Prop :=
¬ ∃ t', RedStep t t'
end AehligSchwichtenberg2002
Read-back
What the Lean code literally says, in plain math · Gemini 3.1 Pro (Independent Blind Auditor)
Read-back: Def_AehligSchwichtenberg2002_Syntax.lean
Types and Constructors (Ty, Ty.bin, Ty.lolliMany, Var, Constructor, Constructor.ty)
Finite types (Ty). The inductive type of types is generated freely by six constructors:
The abbreviation (Ty.bin) denotes the list-of-booleans type . For a finite list of argument types and a target type , the iterated linear function type (Ty.lolliMany) is defined recursively by right-associative folding:
Typed variables (Var). A variable is a pair consisting of a natural number identifier and a type . Two variables are equal if and only if both their natural number identifiers and their types are equal; in particular, two variables with the same identifier but distinct types are treated as distinct variables throughout the development.
Constructors (Constructor, Constructor.ty). The inductive type of constructor symbols consists of five forms, each assigned a fixed type as follows:
Terms, Contexts, Free Variables, and Substitution (Term, Term.appMany, Context, DisjointCtx, IsCtxUnion, IsDisjointCtxUnion, freeVarOccurrences, freeVars, freeVarCard, IsAlmostClosed, IsClosed, maxVarId, renameFreeVar, rawSize, renameFreeVar_rawSize, substTerm, substTwo)
Raw terms (Term, Term.appMany). The inductive type of raw (untyped) terms is generated by six constructors:
where ranges over typed variables (Var) and ranges over constructor symbols (Constructor). For a head term and a finite list of argument terms , multi-argument application (Term.appMany) is defined by left-associative folding:
Contexts and context relations (Context, DisjointCtx, IsCtxUnion, IsDisjointCtxUnion). A context is a finite list of typed variables . Three set-extensional predicates are defined on contexts :
- Disjointness (
DisjointCtx): and are disjoint if for every variable , . (Multiplicity within or within is not restricted, and variables with the same numerical identifier but distinct types are considered distinct.) - Set-extensional union (
IsCtxUnion): is a union of and if for every variable , . - Disjoint union (
IsDisjointCtxUnion): is a disjoint union of and (written ) if both and hold.
Free variables and closure predicates (freeVarOccurrences, freeVars, freeVarCard, IsAlmostClosed, IsClosed). The list of free variable occurrences is defined structurally by:
The set of free variables (freeVars) is obtained by removing duplicate entries from , and its cardinality (freeVarCard) is the length of . A term is almost closed (IsAlmostClosed) if every satisfies , and closed (IsClosed) if .
Variable renaming and capture-avoiding substitution (maxVarId, renameFreeVar, rawSize, renameFreeVar_rawSize, substTerm, substTwo).
- computes the maximum numerical identifier among all variables occurring free or bound in (returning on constructor constants).
- replaces every free occurrence of variable in by variable , halting under if (without checking whether is captured by when ).
- assigns size to variables and constants, to and , and to and . The lemma
renameFreeVar_rawSizestates that for all terms and variables , . - Single capture-avoiding substitution (
substTerm t x s) is defined by recursion on :- if , and otherwise;
- ;
- , , and ;
- equals if ; if and , it chooses the fresh variable and returns ; otherwise it returns .
- Two-variable substitution (
substTwo r x y t s) is defined by: if , let and return ; otherwise return . (If , the second substitution targets or after has already been replaced by .)
Typing Judgment (HasType)
The inductive relation (HasType Γ t τ) relates a context , a term , and a type via ten inference rules:
- Variable (
var): If (IsDisjointCtxUnion Γ [x] Γ'), then . - Constructor (
const): For any context and constructor , . - Linear abstraction (
lam): If is a set-extensional union of and (IsCtxUnion Γ [x] Γ', which does not require ) and , then . - Linear application (
app): If , , and , then . - Additive pair (
pair): If and , then . - Left projection (
projLeft): If , then . - Right projection (
projRight): If , then . - Boolean elimination (
boolElim): If , , , and , then . - Tensor elimination (
tensorElim): If , , , , and , then . (Note that requires and , while is enforced by the separate hypothesis.) - List elimination / iteration (
listElim): If and (in the empty list context ), then . Note that a braced term never has a standalone type underHasType; it can only appear as the argument of a list elimination .
Lists, Conversions, and Reductions (listEntryCount, IsListTerm, ConvStep, RedStep, RedStar, RedSteps, IsNormal)
Syntactic lists (listEntryCount, IsListTerm). The partial function is defined syntactically on raw terms by:
A term satisfies if and only if there exists such that . Note that and do not check that the type subscripts along the spine agree, nor that the arguments and are well-typed or normal.
Conversions (ConvStep). One-step top-level conversion (ConvStep t t') is the inductive relation generated by eight rules:
Reduction and normal forms (RedStep, RedStar, RedSteps, IsNormal).
- One-step reduction (
RedStep): holds if and only if either (ConvStep), or and with (appLeft), or and with (appRight). Because congruence rules exist only for application, reduction is weak: no reduction step can ever occur inside a -abstraction , inside an additive pair , or inside braces . - Multi-step reduction (
RedStar,RedSteps): (RedStar t u) is the reflexive-transitive closure of , and (RedSteps k t u) holds for when there is a chain of exactly one-step reductions. - Normal form (
IsNormal): A term is normal if there does not exist any term such that . Because does not reduce under , , or , any term whose outermost constructor is , , , a variable, or a constant is vacuously normal regardless of whether its proper subterms contain redexes.