module univTypes.Decidability.Decision where open import Data.Product using (∃-syntax; _×_) renaming (_,_ to ⟨_,_⟩) open import Data.Sum using (inj₁; inj₂) open import Relation.Nullary using (Dec; yes; no) open import univTypes.Specification open import univTypes.Metatheory.Confluence.Conversion using (≈-sym; ≈-trans) open import univTypes.Metatheory.Confluence.Modulo using (↪*→↪~*; ~→↪~*) open import univTypes.Metatheory.Typing.Progress using (progress; progressₗ) open import univTypes.Metatheory.Typing.SubjectReduction using (subject-reduction; subject-reductionₗ) open import univTypes.Metatheory.Levels.Decision using (_~?_) open import univTypes.Decidability.Statement open import univTypes.Decidability.NormalForms using (≈-Value) import univTypes.Decidability.SN.Inductive as I -- The reductions of the proof, independent of how normalisation is proven: -- strong normalisation → normalisation → normalisation up to ~ -- → decidability of conversion. -- A normaliser from strong normalisation ------------------------------------------ -- Take the step that progress finds, keep the typing by subject reduction, -- and recurse on the SN proof. normalise : ∀ {n} {Γ : Context n} {e t : Term n} → Γ ⊢ e ⦂ t → SN e → ∃[ v ] e ↪* v × Value v normalise ⊢e (sn h) with progress ⊢e ... | inj₁ V = ⟨ _ , ⟨ ↪*-refl , V ⟩ ⟩ ... | inj₂ ⟨ _ , r ⟩ with normalise (subject-reduction ⊢e r) (h r) ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ v , ⟨ ↪*-step r rs , V ⟩ ⟩ normaliseₗ : ∀ {n} {Γ : Context n} {l : Lvl n} → Γ ⊢ₗ l → SN l → ∃[ v ] l ↪* v × Value v normaliseₗ ⊢l (sn h) with progressₗ ⊢l ... | inj₁ V = ⟨ _ , ⟨ ↪*-refl , V ⟩ ⟩ ... | inj₂ ⟨ _ , r ⟩ with normaliseₗ (subject-reductionₗ ⊢l r) (h r) ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ v , ⟨ ↪*-step r rs , V ⟩ ⟩ SN→Normalisation : StrongNormalisation → Normalisation SN→Normalisation S ⊢Γ ⊢e = normalise ⊢e (S ⊢Γ ⊢e) SN→Normalisationₗ : StrongNormalisationₗ → Normalisationₗ SN→Normalisationₗ S ⊢Γ ⊢l = normaliseₗ ⊢l (S ⊢Γ ⊢l) -- Decidability from normalisation ------------------------------------------------- Normalisation→Normalisation~ : Normalisation → Normalisation~ Normalisation→Normalisation~ N ⊢Γ ⊢e with N ⊢Γ ⊢e ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ v , ⟨ ↪*→↪~* rs , V ⟩ ⟩ Normalisationₗ→Normalisation~ₗ : Normalisationₗ → Normalisation~ₗ Normalisationₗ→Normalisation~ₗ N ⊢Γ ⊢l with N ⊢Γ ⊢l ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ v , ⟨ ↪*→↪~* rs , V ⟩ ⟩ -- e₁ ≈ e₂ iff their normal forms are convertible iff they are ~-equal -- (≈-Value), which the level normaliser decides. private ↪~*→≈ : ∀ {s n} {e v : Tm s n} → e ↪~* v → e ≈ v ↪~*→≈ rs = mk-≈ _ rs ↪~*-refl decide : ∀ {s n} {e₁ e₂ v₁ v₂ : Tm s n} → e₁ ↪~* v₁ → Value v₁ → e₂ ↪~* v₂ → Value v₂ → Dec (e₁ ≈ e₂) decide {v₁ = v₁} {v₂} r₁ V₁ r₂ V₂ with v₁ ~? v₂ ... | yes p = yes (mk-≈ v₂ (↪~*-trans r₁ (~→↪~* p)) r₂) ... | no ¬p = no λ q → ¬p (≈-Value V₁ V₂ (≈-trans (≈-sym (↪~*→≈ r₁)) (≈-trans q (↪~*→≈ r₂)))) Normalisation~→Decidable-≈ : Normalisation~ → Decidable-≈ Normalisation~→Decidable-≈ N ⊢Γ ⊢e₁ ⊢e₂ with N ⊢Γ ⊢e₁ | N ⊢Γ ⊢e₂ ... | ⟨ _ , ⟨ r₁ , V₁ ⟩ ⟩ | ⟨ _ , ⟨ r₂ , V₂ ⟩ ⟩ = decide r₁ V₁ r₂ V₂ Normalisation~ₗ→Decidable-≈ₗ : Normalisation~ₗ → Decidable-≈ₗ Normalisation~ₗ→Decidable-≈ₗ N ⊢Γ ⊢l₁ ⊢l₂ with N ⊢Γ ⊢l₁ | N ⊢Γ ⊢l₂ ... | ⟨ _ , ⟨ r₁ , V₁ ⟩ ⟩ | ⟨ _ , ⟨ r₂ , V₂ ⟩ ⟩ = decide r₁ V₁ r₂ V₂ Normalisation→Decidable-≈ : Normalisation → Decidable-≈ Normalisation→Decidable-≈ N = Normalisation~→Decidable-≈ (Normalisation→Normalisation~ N) Normalisationₗ→Decidable-≈ₗ : Normalisationₗ → Decidable-≈ₗ Normalisationₗ→Decidable-≈ₗ N = Normalisation~ₗ→Decidable-≈ₗ (Normalisationₗ→Normalisation~ₗ N) -- From the inductive characterisation of SN ----------------------------------------- -- What the fundamental theorem provides: every well-typed term (level) has -- an inductive SN derivation, which yields its normal form. SNᵢ : Set SNᵢ = ∀ {n} {Γ : Context n} {e t : Term n} → ⊢ Γ → Γ ⊢ e ⦂ t → I.SN e SNᵢₗ : Set SNᵢₗ = ∀ {n} {Γ : Context n} {l : Lvl n} → ⊢ Γ → Γ ⊢ₗ l → I.SN l SNᵢ→Normalisation : SNᵢ → Normalisation SNᵢ→Normalisation S ⊢Γ ⊢e = I.nf (S ⊢Γ ⊢e) SNᵢₗ→Normalisationₗ : SNᵢₗ → Normalisationₗ SNᵢₗ→Normalisationₗ S ⊢Γ ⊢l = I.nf (S ⊢Γ ⊢l) SNᵢ→Decidable-≈ : SNᵢ → Decidable-≈ SNᵢ→Decidable-≈ S = Normalisation→Decidable-≈ (SNᵢ→Normalisation S) SNᵢₗ→Decidable-≈ₗ : SNᵢₗ → Decidable-≈ₗ SNᵢₗ→Decidable-≈ₗ S = Normalisationₗ→Decidable-≈ₗ (SNᵢₗ→Normalisationₗ S)