Files
Julius FischerandClaude Opus 5.5 0c079aadf2
Typecheck / agda (push) Successful in 1m53s
Canonicity of closed terms
canonicity: a closed term whose type has head h reduces to an
introduction form for h; canonicity-ℕ: a closed natural number reduces to
a numeral ⌜ k ⌝. From normalisation, subject reduction and canonical forms,
since closed normal forms are not neutral. Re-exported in Theorems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q7dfaHQ3dYSfqcxR7V5UrJ
2026-11-21 18:36:15 +01:00

74 lines
3.6 KiB
Agda
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module univTypes.Decidability.Canonicity where
open import Data.Empty using (⊥; ⊥-elim)
open import Data.Maybe using (just)
open import Data.Nat using (ℕ; zero; suc)
open import Data.Product using (Σ; ∃-syntax; _×_) renaming (_,_ to ⟨_,_⟩)
open import Data.Sum using (inj₁; inj₂)
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
open import univTypes.Specification
open import univTypes.Metatheory.Typing.Inversion using (invert-⊢suc)
open import univTypes.Metatheory.Typing.SubjectReduction using (subject-reduction)
open import univTypes.Metatheory.Typing.Progress using (Head; head; Canonical; canonical)
open Canonical
open import univTypes.Decidability.Statement using (⊢∅)
open import univTypes.Decidability.Fundamental using (normalisation)
-- Canonicity: a closed well-typed term reduces to an introduction form of
-- its type. It follows from normalisation and subject reduction, since a
-- closed normal form is not neutral (a neutral term has a variable at its
-- head), so canonical forms apply.
-- Closed terms are not neutral.
closed-¬Neutral : ∀ {e : Term 0} → Neutral e → ⊥
closed-¬Neutral (neu-` {x = ()})
closed-¬Neutral (neu-· n _) = closed-¬Neutral n
closed-¬Neutral (neu-proj₁ n) = closed-¬Neutral n
closed-¬Neutral (neu-proj₂ n) = closed-¬Neutral n
closed-¬Neutral (neu-subst _ n _) = closed-¬Neutral n
closed-¬Neutral (neu-natrec _ _ _ n) = closed-¬Neutral n
subject-reduction* : ∀ {n} {Γ : Context n} {e e' t : Term n} → Γ ⊢ e ⦂ t → e ↪* e' → Γ ⊢ e' ⦂ t
subject-reduction* ⊢e ↪*-refl = ⊢e
subject-reduction* ⊢e (↪*-step r rs) = subject-reduction* (subject-reduction ⊢e r) rs
-- The closed normal form of a term, with its typing.
private
closed-nf : ∀ {e T : Term 0} → ∅ ⊢ e ⦂ T → ∃[ v ] e ↪* v × Value v × ∅ ⊢ v ⦂ T
closed-nf ⊢e with normalisation ⊢∅ ⊢e
... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ v , ⟨ rs , ⟨ V , subject-reduction* ⊢e rs ⟩ ⟩ ⟩
-- Every head ---------------------------------------------------------------------------
-- A closed term whose type is a type former with head h reduces to an
-- introduction form for h: λ for ∀, a pair for ∃, refl for ≡, `lvl l for
-- Level, tt for Unit, zero or suc for ℕ, a type for a universe.
canonicity : ∀ {e T : Term 0} {h : Head}
→ ∅ ⊢ e ⦂ T → head T ≡ just h → ∃[ v ] e ↪* v × Canonical h v
canonicity ⊢e hT with closed-nf ⊢e
... | ⟨ v , ⟨ rs , ⟨ V , ⊢v ⟩ ⟩ ⟩ with canonical ⊢v hT V
... | inj₁ n = ⊥-elim (closed-¬Neutral n)
... | inj₂ c = ⟨ v , ⟨ rs , c ⟩ ⟩
-- Natural numbers ----------------------------------------------------------------------
⌜_⌝ : ∀ {n} → ℕ → Term n
⌜ zero ⌝ = `zero
⌜ suc k ⌝ = `suc ⌜ k ⌝
private
numeral : ∀ {v : Term 0} → ∅ ⊢ v ⦂ `ℕ → Value v → Σ ℕ λ k → v ≡ ⌜ k ⌝
numeral ⊢v V with canonical ⊢v refl V
numeral ⊢v V | inj₁ n = ⊥-elim (closed-¬Neutral n)
numeral ⊢v V | inj₂ can-zero = ⟨ zero , refl ⟩
numeral ⊢v (val-suc V) | inj₂ can-suc with numeral (invert-⊢suc ⊢v) V
... | ⟨ k , refl ⟩ = ⟨ suc k , refl ⟩
numeral ⊢v (val-neu ()) | inj₂ can-suc
-- A closed natural number reduces to a numeral.
canonicity-ℕ : ∀ {e : Term 0} → ∅ ⊢ e ⦂ `ℕ → Σ ℕ λ k → e ↪* ⌜ k ⌝
canonicity-ℕ ⊢e with closed-nf ⊢e
... | ⟨ v , ⟨ rs , ⟨ V , ⊢v ⟩ ⟩ ⟩ with numeral ⊢v V
... | ⟨ k , refl ⟩ = ⟨ k , rs ⟩