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 closed-¬Neutral (neu-absurd _ 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 ⟩