module univTypes.SubjectReduction.SubstitutionPreservesTyping where open import Data.Product renaming (_,_ to ⟨_,_⟩) open import Data.Sum open import Data.Nat using (ℕ; zero; suc; _+_) open import Function using (_∘_) open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym; subst; cong; cong₂; module ≡-Reasoning) open import Relation.Nullary using (¬_) open import Data.Empty using (⊥; ⊥-elim) open import univTypes.SubjectReduction.Specification open import univTypes.SubjectReduction.ConversionProperties using (≈-ren; ≈-sub; ≡→≈) open import univTypes.SubjectReduction.Composition using (wk-extᵣ-fusion; swap-0↦-extᵣ-fusion; wk-extₛ-fusion; swap-0↦-extₛ-fusion; wk-0↦-fusion) open import Data.Fin using (Fin; zero; suc) -- Renaming Typings infix 4 _⦂_⇒ᵣ_ _⦂_⇒ᵣ_ : ∀ {m n} → Ren m n → Context m → Context n → Set ρ ⦂ Γ₁ ⇒ᵣ Γ₂ = ∀ x → lookup Γ₂ (ρ x) ≡ ren ρ (lookup Γ₁ x) ⊢extᵣ : ∀ {m n t ρ} {Γ₁ : Context m} {Γ₂ : Context n} → ρ ⦂ Γ₁ ⇒ᵣ Γ₂ → extᵣ ρ ⦂ Γ₁ , t ⇒ᵣ Γ₂ , (ren ρ t) ⊢extᵣ {ρ = ρ} ⊢ρ zero = wk-extᵣ-fusion _ _ ⊢extᵣ ⊢ρ (suc x) rewrite ⊢ρ x = wk-extᵣ-fusion _ _ type : ∀ {n} {Γ : Context n} {e t : Term n} → Γ ⊢ e ⦂ t → Term n type {t = t} _ = t term : ∀ {n} {Γ : Context n} {e t : Term n} → Γ ⊢ e ⦂ t → Term n term {e = e} _ = e ∀-type : ∀ {n} {Γ : Context n} {e : Term n} {x t} → Γ ⊢ e ⦂ ∀[x⦂ x ] t → Term _ ∀-type {t = t} _ = t ∃-type : ∀ {n} {Γ : Context n} {e : Term n} {x t} → Γ ⊢ e ⦂ ∃[x⦂ x ] t → Term _ ∃-type {t = t} _ = t sub-type : ∀ {n} {Γ : Context n} {e : Term n} {a b} → Γ ⊢ e ⦂ a [ b ] → Term _ sub-type {a = a} _ = a ⊢ren : ∀ {m n} {Γ₁ : Context m} {Γ₂ : Context n} {e t ρ} → ρ ⦂ Γ₁ ⇒ᵣ Γ₂ → Γ₁ ⊢ e ⦂ t ------------------------ → Γ₂ ⊢ ren ρ e ⦂ ren ρ t ⊢renₗ : ∀ {m n} {Γ₁ : Context m} {Γ₂ : Context n} {l ρ} → ρ ⦂ Γ₁ ⇒ᵣ Γ₂ → Γ₁ ⊢ₗ l → Γ₂ ⊢ₗ ren ρ l -- A codomain level weakened under a binder, renamed under the binder. wk-Setn-ren : ∀ {m n} (ρ : Ren m n) (l : Lvl m) → `Setn ren (extᵣ ρ) (ren wk l) ≈ `Setn ren wk (ren ρ l) wk-Setn-ren ρ l = ≡→≈ (cong `Setn_ (sym (wk-extᵣ-fusion ρ l))) ⊢ren ⊢ρ (⊢-refl ⊢-e) = ⊢-refl (⊢ren ⊢ρ ⊢-e) ⊢ren ⊢ρ (⊢-` x refl) = ⊢-` _ (⊢ρ x) ⊢ren ⊢ρ (⊢-≡ ⊢t ⊢l ⊢r) = ⊢-≡ (⊢ren ⊢ρ ⊢t) (⊢ren ⊢ρ ⊢l) (⊢ren ⊢ρ ⊢r) ⊢ren {m} {n} {Γ₁} {Γ₂} {e} { ∃[x⦂ _ ] a } {ρ} ⊢ρ (⊢-, ⊢l ⊢r) = ⊢-, (⊢ren ⊢ρ ⊢l) (⊢-≈ (≡→≈ (sym (swap-0↦-extᵣ-fusion ρ (term ⊢l) a) )) (⊢ren {Γ₂ = Γ₂} ⊢ρ ⊢r)) ⊢ren ⊢ρ (⊢-≈ t₁≈t ⊢e) = ⊢-≈ (≈-ren _ t₁≈t) (⊢ren ⊢ρ ⊢e) ⊢ren {ρ = ρ} ⊢ρ (⊢-∀ {l₂ = l₂} ⊢e₁ ⊢e₂) = ⊢-∀ (⊢ren ⊢ρ ⊢e₁) (⊢-≈ (wk-Setn-ren ρ l₂) (⊢ren (⊢extᵣ ⊢ρ) ⊢e₂)) ⊢ren ⊢ρ (⊢-∀ω ⊢e₁ ⊢e₂) = ⊢-∀ω (⊢ren ⊢ρ ⊢e₁) (⊢ren (⊢extᵣ ⊢ρ) ⊢e₂) ⊢ren ⊢ρ (⊢-∀-dep ⊢e₁ ⊢e₂) = ⊢-∀-dep (⊢ren ⊢ρ ⊢e₁) (⊢ren (⊢extᵣ ⊢ρ) ⊢e₂) ⊢ren {ρ = ρ} ⊢ρ (⊢-∃ {l₂ = l₂} ⊢e₁ ⊢e₂) = ⊢-∃ (⊢ren ⊢ρ ⊢e₁) (⊢-≈ (wk-Setn-ren ρ l₂) (⊢ren (⊢extᵣ ⊢ρ) ⊢e₂)) ⊢ren ⊢ρ (⊢-∃ω ⊢e₁ ⊢e₂) = ⊢-∃ω (⊢ren ⊢ρ ⊢e₁) (⊢ren (⊢extᵣ ⊢ρ) ⊢e₂) ⊢ren ⊢ρ (⊢-∃-dep ⊢e₁ ⊢e₂) = ⊢-∃-dep (⊢ren ⊢ρ ⊢e₁) (⊢ren (⊢extᵣ ⊢ρ) ⊢e₂) ⊢ren ⊢ρ (⊢-λ ⊢t t⊢e) = ⊢-λ (⊢ren ⊢ρ ⊢t) (⊢ren (⊢extᵣ ⊢ρ) t⊢e) ⊢ren {ρ = ρ} ⊢ρ (⊢-· ⊢l ⊢r) = ⊢-≈ (≡→≈ ((swap-0↦-extᵣ-fusion ρ (term ⊢r) (∀-type ⊢l)))) (⊢-· (⊢ren ⊢ρ ⊢l) (⊢ren ⊢ρ ⊢r)) ⊢ren ⊢ρ (⊢-proj₁ ⊢e) = ⊢-proj₁ (⊢ren ⊢ρ ⊢e) ⊢ren {m} {n} {Γ₁} {Γ₂} {e} {t} {ρ} ⊢ρ (⊢-proj₂ ⊢e) = ⊢-≈ (≡→≈ (swap-0↦-extᵣ-fusion ρ (`proj₁ (term ⊢e)) (∃-type ⊢e))) (⊢-proj₂ (⊢ren ⊢ρ ⊢e)) ⊢ren {m} {n} {Γ₁} {Γ₂} {e} {t} {ρ} ⊢ρ x@(⊢-subst {l = l} {t = t*} t'⊢t ⊢u₁ ⊢u₂ ⊢≡ ⊢t[u₁]) = ⊢-≈ (≡→≈ (swap-0↦-extᵣ-fusion ρ (term ⊢u₂) t* )) (⊢-subst (⊢-≈ (wk-Setn-ren ρ l) (⊢ren (⊢extᵣ ⊢ρ) t'⊢t)) (⊢ren ⊢ρ ⊢u₁) (⊢ren ⊢ρ ⊢u₂) (⊢ren ⊢ρ ⊢≡) (⊢-≈ (≡→≈ (sym (swap-0↦-extᵣ-fusion ρ (term ⊢u₁) (t*)))) (⊢ren ⊢ρ ⊢t[u₁]))) -- New ⊢ren ⊢ρ (⊢-Setn ⊢l) = ⊢-Setn (⊢renₗ ⊢ρ ⊢l) ⊢ren ⊢ρ ⊢-Setω = ⊢-Setω ⊢ren ⊢ρ (⊢-lvl ⊢l) = ⊢-lvl (⊢renₗ ⊢ρ ⊢l) ⊢ren ⊢ρ ⊢-Level = ⊢-Level ⊢ren ⊢ρ (⊢-Set