module univTypes.Decidability.SN.Inductive where open import Data.Nat using (suc) open import Data.Fin using (Fin) open import Data.Product using (∃-syntax; _×_) renaming (_,_ to ⟨_,_⟩) open import univTypes.Specification -- Strong normalisation, inductively (cf. van Raamsdonk–Severi; Abel et al., -- POPLMark Reloaded): neutral terms with SN arguments, introduction forms -- with SN components, and closure under weak head expansion (with SN side -- conditions for the parts a step discards). -- -- A derivation yields a normal form by plain reduction (nf). The relation -- only uses reduction, not level equality: accessibility modulo ~ would be -- false for levels, since with a ↪ a' -- latom a' ⊔ latom a ~ latom a' ⊔ (latom a ⊔ latom a) -- ↪ latom a' ⊔ (latom a' ⊔ latom a) ~ latom a' ⊔ latom a -- is a cycle, although every atom is SN. infix 4 _⇒_ data _⇒_ : ∀ {s n} → Tm s n → Tm s n → Set data SNe : ∀ {n} → Term n → Set data SN : ∀ {s n} → Tm s n → Set -- Weak head reduction. data _⇒_ where ⇒β : ∀ {n} {A : Term n} {b : Term (suc n)} {a : Term n} → SN A → SN a → (`λ[x⦂ A ] b) · a ⇒ b [ a ] ⇒· : ∀ {n} {e₁ e₁' e₂ : Term n} → e₁ ⇒ e₁' → e₁ · e₂ ⇒ e₁' · e₂ ⇒π₁ : ∀ {n} {a b : Term n} → SN b → `proj₁ (a `, b) ⇒ a ⇒π₂ : ∀ {n} {a b : Term n} → SN a → `proj₂ (a `, b) ⇒ b ⇒proj₁ : ∀ {n} {e e' : Term n} → e ⇒ e' → `proj₁ e ⇒ `proj₁ e' ⇒proj₂ : ∀ {n} {e e' : Term n} → e ⇒ e' → `proj₂ e ⇒ `proj₂ e' ⇒subst-refl : ∀ {n} {t : Term (suc n)} {e : Term n} → SN t → `subst t `refl e ⇒ e ⇒subst : ∀ {n} {t : Term (suc n)} {e₁ e₁' e₂ : Term n} → e₁ ⇒ e₁' → `subst t e₁ e₂ ⇒ `subst t e₁' e₂ ⇒latom : ∀ {n} {l : Lvl n} → `latom (`lvl l) ⇒ l ⇒latom-cong : ∀ {n} {e e' : Term n} → e ⇒ e' → `latom e ⇒ `latom e' ⇒natrec-zero : ∀ {n} {P z s : Term n} → SN P → SN s → `natrec P z s `zero ⇒ z ⇒natrec-suc : ∀ {n} {P z s k : Term n} → `natrec P z s (`suc k) ⇒ s · k · `natrec P z s k ⇒natrec : ∀ {n} {P z s t t' : Term n} → t ⇒ t' → `natrec P z s t ⇒ `natrec P z s t' -- Strongly normalising neutral terms. data SNe where ne-` : ∀ {n} {x : Fin n} → SNe (` x) ne-· : ∀ {n} {e a : Term n} → SNe e → SN a → SNe (e · a) ne-proj₁ : ∀ {n} {e : Term n} → SNe e → SNe (`proj₁ e) ne-proj₂ : ∀ {n} {e : Term n} → SNe e → SNe (`proj₂ e) ne-subst : ∀ {n} {t : Term (suc n)} {e₁ e₂ : Term n} → SN t → SNe e₁ → SN e₂ → SNe (`subst t e₁ e₂) ne-natrec : ∀ {n} {P z s t : Term n} → SN P → SN z → SN s → SNe t → SNe (`natrec P z s t) -- Strongly normalising terms and level expressions. data SN where sn-λ : ∀ {n} {A : Term n} {e : Term (suc n)} → SN A → SN e → SN (`λ[x⦂ A ] e) sn-∀ : ∀ {n} {t₁ : Term n} {t₂ : Term (suc n)} → SN t₁ → SN t₂ → SN (∀[x⦂ t₁ ] t₂) sn-∃ : ∀ {n} {t₁ : Term n} {t₂ : Term (suc n)} → SN t₁ → SN t₂ → SN (∃[x⦂ t₁ ] t₂) sn-, : ∀ {n} {e₁ e₂ : Term n} → SN e₁ → SN e₂ → SN (e₁ `, e₂) sn-≡ : ∀ {n} {e₁ e₂ : Term n} → SN e₁ → SN e₂ → SN (e₁ `≡ e₂) sn-refl : ∀ {n} → SN (`refl {n}) sn-tt : ∀ {n} → SN (`tt {n}) sn-Unit : ∀ {n} → SN (`Unit {n}) sn-Level : ∀ {n} → SN (`Level {n}) sn-Setω : ∀ {n m} → SN (`Setω_ {n} m) sn-Setn : ∀ {n} {l : Lvl n} → SN l → SN (`Setn l) sn-lvl : ∀ {n} {l : Lvl n} → SN l → SN (`lvl l) sn-lzero : ∀ {n} → SN (`lzero {n}) sn-lsuc : ∀ {n} {l : Lvl n} → SN l → SN (`lsuc l) sn-⊔ : ∀ {n} {l₁ l₂ : Lvl n} → SN l₁ → SN l₂ → SN (l₁ `⊔ l₂) sn-latom : ∀ {n} {e : Term n} → SNe e → SN (`latom e) sn-ℕ : ∀ {n} → SN (`ℕ {n}) sn-zero : ∀ {n} → SN (`zero {n}) sn-suc : ∀ {n} {k : Term n} → SN k → SN (`suc k) sn-ne : ∀ {n} {e : Term n} → SNe e → SN e sn-⇒ : ∀ {s n} {e e' : Tm s n} → e ⇒ e' → SN e' → SN e ⇒→↪ : ∀ {s n} {e e' : Tm s n} → e ⇒ e' → e ↪ e' ⇒→↪ (⇒β _ _) = β-λ ⇒→↪ (⇒· r) = ξ-·₁ (⇒→↪ r) ⇒→↪ (⇒π₁ _) = β-proj₁ ⇒→↪ (⇒π₂ _) = β-proj₂ ⇒→↪ (⇒proj₁ r) = ξ-proj₁ (⇒→↪ r) ⇒→↪ (⇒proj₂ r) = ξ-proj₂ (⇒→↪ r) ⇒→↪ (⇒subst-refl _) = β-subst ⇒→↪ (⇒subst r) = ξ-subst₂ (⇒→↪ r) ⇒→↪ ⇒latom = β-latom ⇒→↪ (⇒latom-cong r) = ξ-latom (⇒→↪ r) ⇒→↪ (⇒natrec-zero _ _) = β-natrec-zero ⇒→↪ ⇒natrec-suc = β-natrec-suc ⇒→↪ (⇒natrec r) = ξ-natrec₄ (⇒→↪ r) -- Normal forms ------------------------------------------------------------------- ↪*-map : ∀ {s s' m n} (f : Tm s m → Tm s' n) → (∀ {a b} → a ↪ b → f a ↪ f b) → ∀ {a b} → a ↪* b → f a ↪* f b ↪*-map f ξ ↪*-refl = ↪*-refl ↪*-map f ξ (↪*-step r rs) = ↪*-step (ξ r) (↪*-map f ξ rs) private -- Congruence of _↪*_ in two arguments, one after the other. map₂ : ∀ {s₁ s₂ s m₁ m₂ n} (f : Tm s₁ m₁ → Tm s₂ m₂ → Tm s n) → (∀ {a a' b} → a ↪ a' → f a b ↪ f a' b) → (∀ {a b b'} → b ↪ b' → f a b ↪ f a b') → ∀ {a a' b b'} → a ↪* a' → b ↪* b' → f a b ↪* f a' b' map₂ f ξ₁ ξ₂ {a' = a'} {b = b} p q = ↪*-trans (↪*-map (λ x → f x b) ξ₁ p) (↪*-map (f a') ξ₂ q) mutual nf : ∀ {s n} {e : Tm s n} → SN e → ∃[ v ] e ↪* v × Value v nf (sn-λ s₁ s₂) with nf s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ = ⟨ `λ[x⦂ v₁ ] v₂ , ⟨ map₂ `λ[x⦂_]_ ξ-λ₁ ξ-λ rs₁ rs₂ , val-λ V₁ V₂ ⟩ ⟩ nf (sn-∀ s₁ s₂) with nf s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ = ⟨ ∀[x⦂ v₁ ] v₂ , ⟨ map₂ ∀[x⦂_]_ ξ-∀₁ ξ-∀₂ rs₁ rs₂ , val-∀ V₁ V₂ ⟩ ⟩ nf (sn-∃ s₁ s₂) with nf s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ = ⟨ ∃[x⦂ v₁ ] v₂ , ⟨ map₂ ∃[x⦂_]_ ξ-∃₁ ξ-∃₂ rs₁ rs₂ , val-∃ V₁ V₂ ⟩ ⟩ nf (sn-, s₁ s₂) with nf s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ = ⟨ v₁ `, v₂ , ⟨ map₂ _`,_ ξ-,₁ ξ-,₂ rs₁ rs₂ , val-, V₁ V₂ ⟩ ⟩ nf (sn-≡ s₁ s₂) with nf s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ = ⟨ v₁ `≡ v₂ , ⟨ map₂ _`≡_ ξ-≡₁ ξ-≡₂ rs₁ rs₂ , val-≡ V₁ V₂ ⟩ ⟩ nf sn-refl = ⟨ _ , ⟨ ↪*-refl , val-refl ⟩ ⟩ nf sn-tt = ⟨ _ , ⟨ ↪*-refl , val-tt ⟩ ⟩ nf sn-Unit = ⟨ _ , ⟨ ↪*-refl , val-Unit ⟩ ⟩ nf sn-Level = ⟨ _ , ⟨ ↪*-refl , val-Level ⟩ ⟩ nf sn-Setω = ⟨ _ , ⟨ ↪*-refl , val-Setω ⟩ ⟩ nf (sn-Setn s) with nf s ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ `Setn v , ⟨ ↪*-map `Setn_ ξ-Setn rs , val-Setn V ⟩ ⟩ nf (sn-lvl s) with nf s ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ `lvl v , ⟨ ↪*-map `lvl ξ-lvl rs , val-lvl V ⟩ ⟩ nf sn-lzero = ⟨ _ , ⟨ ↪*-refl , val-lzero ⟩ ⟩ nf (sn-lsuc s) with nf s ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ `lsuc v , ⟨ ↪*-map `lsuc ξ-lsuc rs , val-lsuc V ⟩ ⟩ nf (sn-⊔ s₁ s₂) with nf s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ = ⟨ v₁ `⊔ v₂ , ⟨ map₂ _`⊔_ ξ-⊔₁ ξ-⊔₂ rs₁ rs₂ , val-⊔ V₁ V₂ ⟩ ⟩ nf (sn-latom s) with nfₑ s ... | ⟨ v , ⟨ rs , N ⟩ ⟩ = ⟨ `latom v , ⟨ ↪*-map `latom ξ-latom rs , val-latom N ⟩ ⟩ nf (sn-ne s) with nfₑ s ... | ⟨ v , ⟨ rs , N ⟩ ⟩ = ⟨ v , ⟨ rs , val-neu N ⟩ ⟩ nf (sn-⇒ r s) with nf s ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ v , ⟨ ↪*-step (⇒→↪ r) rs , V ⟩ ⟩ nf sn-ℕ = ⟨ _ , ⟨ ↪*-refl , val-ℕ ⟩ ⟩ nf sn-zero = ⟨ _ , ⟨ ↪*-refl , val-zero ⟩ ⟩ nf (sn-suc s) with nf s ... | ⟨ v , ⟨ rs , V ⟩ ⟩ = ⟨ `suc v , ⟨ ↪*-map `suc_ ξ-suc rs , val-suc V ⟩ ⟩ -- Neutral terms have neutral normal forms. nfₑ : ∀ {n} {e : Term n} → SNe e → ∃[ v ] e ↪* v × Neutral v nfₑ ne-` = ⟨ _ , ⟨ ↪*-refl , neu-` ⟩ ⟩ nfₑ (ne-· s₁ s₂) with nfₑ s₁ | nf s₂ ... | ⟨ v₁ , ⟨ rs₁ , N ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V ⟩ ⟩ = ⟨ v₁ · v₂ , ⟨ map₂ _·_ ξ-·₁ ξ-·₂ rs₁ rs₂ , neu-· N V ⟩ ⟩ nfₑ (ne-proj₁ s) with nfₑ s ... | ⟨ v , ⟨ rs , N ⟩ ⟩ = ⟨ `proj₁ v , ⟨ ↪*-map `proj₁ ξ-proj₁ rs , neu-proj₁ N ⟩ ⟩ nfₑ (ne-proj₂ s) with nfₑ s ... | ⟨ v , ⟨ rs , N ⟩ ⟩ = ⟨ `proj₂ v , ⟨ ↪*-map `proj₂ ξ-proj₂ rs , neu-proj₂ N ⟩ ⟩ nfₑ (ne-subst {t = t} {e₂ = e₂} s₁ s₂ s₃) with nf s₁ | nfₑ s₂ | nf s₃ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , N ⟩ ⟩ | ⟨ v₃ , ⟨ rs₃ , V₃ ⟩ ⟩ = ⟨ `subst v₁ v₂ v₃ , ⟨ ↪*-trans (↪*-map (λ x → `subst x _ e₂) ξ-subst₁ rs₁) (map₂ (`subst v₁) ξ-subst₂ ξ-subst₃ rs₂ rs₃) , neu-subst V₁ N V₃ ⟩ ⟩ nfₑ (ne-natrec {z = z} {s = s} {t = t} s₁ s₂ s₃ s₄) with nf s₁ | nf s₂ | nf s₃ | nfₑ s₄ ... | ⟨ v₁ , ⟨ rs₁ , V₁ ⟩ ⟩ | ⟨ v₂ , ⟨ rs₂ , V₂ ⟩ ⟩ | ⟨ v₃ , ⟨ rs₃ , V₃ ⟩ ⟩ | ⟨ v₄ , ⟨ rs₄ , N ⟩ ⟩ = ⟨ `natrec v₁ v₂ v₃ v₄ , ⟨ ↪*-trans (map₂ (λ x y → `natrec x y s t) ξ-natrec₁ ξ-natrec₂ rs₁ rs₂) (map₂ (`natrec v₁ v₂) ξ-natrec₃ ξ-natrec₄ rs₃ rs₄) , neu-natrec V₁ V₂ V₃ N ⟩ ⟩