Files
univTypes/SubjectReduction/Inversion.agda
2026-10-30 16:49:02 +01:00

250 lines
14 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.SubjectReduction.Inversion where
open import Data.Product renaming (_,_ to ⟨_,_⟩)
open import Data.Nat using (ℕ; zero; suc)
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
open import Relation.Nullary using (¬_)
open import Data.Empty using (⊥-elim)
open import univTypes.SubjectReduction.Specification
open import univTypes.SubjectReduction.ConversionProperties using (≈-refl; ≈-trans; ≈-sym; ≈-sub)
open import univTypes.SubjectReduction.ConfluenceModulo using (~→↪~*)
open import univTypes.SubjectReduction.LevelEqualityProperties using (~→≋; _≋_)
open _≋_
--------------------------------------------------------------------------------
-- Shapes. Neither reduction nor level equality changes the outermost
-- constructor of ∀, ∃, ≡ and universes, so convertible types with such a
-- constructor have convertible components, and different such constructors
-- are never convertible.
↪~-∀-shape : ∀ {n} {t : Term n} {t₁ : Term n} {t₂ : Term (suc n)} →
(∀[x⦂ t₁ ] t₂) ↪~ t →
∃[ t₁' ] ∃[ t₂' ] t ≡ (∀[x⦂ t₁' ] t₂') × (t₁ ↪~* t₁') × (t₂ ↪~* t₂')
↪~-∀-shape (↪~-↪ (ξ-∀₁ r)) = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-step (↪~-↪ r) ↪~*-refl , ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~-∀-shape (↪~-↪ (ξ-∀₂ r)) = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-refl , ↪~*-step (↪~-↪ r) ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~-∀-shape (↪~-~ p) with ~→≋ p
... | ≋-∀ q r = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ~→↪~* q , ~→↪~* r ⟩ ⟩ ⟩ ⟩
↪~*-∀-shape : ∀ {n} {t : Term n} {t₁ : Term n} {t₂ : Term (suc n)} →
(∀[x⦂ t₁ ] t₂) ↪~* t →
∃[ t₁' ] ∃[ t₂' ] t ≡ (∀[x⦂ t₁' ] t₂') × (t₁ ↪~* t₁') × (t₂ ↪~* t₂')
↪~*-∀-shape ↪~*-refl = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-refl , ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~*-∀-shape (↪~*-step r rs) with ↪~-∀-shape r
... | ⟨ x₁ , ⟨ x₂ , ⟨ refl , ⟨ t₁↪~*x₁ , t₂↪~*x₂ ⟩ ⟩ ⟩ ⟩ with ↪~*-∀-shape rs
... | ⟨ y₁ , ⟨ y₂ , ⟨ refl , ⟨ x₁↪~*y₁ , x₂↪~*y₂ ⟩ ⟩ ⟩ ⟩
= ⟨ y₁ , ⟨ y₂ , ⟨ refl , ⟨ ↪~*-trans t₁↪~*x₁ x₁↪~*y₁ , ↪~*-trans t₂↪~*x₂ x₂↪~*y₂ ⟩ ⟩ ⟩ ⟩
invert-≈-∀ : ∀ {n} {t₁ t₁' : Term n} {t₂ t₂' : Term (suc n)} →
(∀[x⦂ t₁ ] t₂) ≈ (∀[x⦂ t₁' ] t₂') →
t₁ ≈ t₁' × t₂ ≈ t₂'
invert-≈-∀ (mk-≈ x p q) with ↪~*-∀-shape p | ↪~*-∀-shape q
... | ⟨ a₁ , ⟨ a₂ , ⟨ refl , ⟨ t₁↪~*a₁ , t₂↪~*a₂ ⟩ ⟩ ⟩ ⟩
| ⟨ _ , ⟨ _ , ⟨ refl , ⟨ t₁'↪~*a₁ , t₂'↪~*a₂ ⟩ ⟩ ⟩ ⟩
= ⟨ mk-≈ a₁ t₁↪~*a₁ t₁'↪~*a₁ , mk-≈ a₂ t₂↪~*a₂ t₂'↪~*a₂ ⟩
↪~-∃-shape : ∀ {n} {t : Term n} {t₁ : Term n} {t₂ : Term (suc n)} →
(∃[x⦂ t₁ ] t₂) ↪~ t →
∃[ t₁' ] ∃[ t₂' ] t ≡ (∃[x⦂ t₁' ] t₂') × (t₁ ↪~* t₁') × (t₂ ↪~* t₂')
↪~-∃-shape (↪~-↪ (ξ-∃₁ r)) = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-step (↪~-↪ r) ↪~*-refl , ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~-∃-shape (↪~-↪ (ξ-∃₂ r)) = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-refl , ↪~*-step (↪~-↪ r) ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~-∃-shape (↪~-~ p) with ~→≋ p
... | ≋-∃ q r = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ~→↪~* q , ~→↪~* r ⟩ ⟩ ⟩ ⟩
↪~*-∃-shape : ∀ {n} {t : Term n} {t₁ : Term n} {t₂ : Term (suc n)} →
(∃[x⦂ t₁ ] t₂) ↪~* t →
∃[ t₁' ] ∃[ t₂' ] t ≡ (∃[x⦂ t₁' ] t₂') × (t₁ ↪~* t₁') × (t₂ ↪~* t₂')
↪~*-∃-shape ↪~*-refl = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-refl , ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~*-∃-shape (↪~*-step r rs) with ↪~-∃-shape r
... | ⟨ x₁ , ⟨ x₂ , ⟨ refl , ⟨ t₁↪~*x₁ , t₂↪~*x₂ ⟩ ⟩ ⟩ ⟩ with ↪~*-∃-shape rs
... | ⟨ y₁ , ⟨ y₂ , ⟨ refl , ⟨ x₁↪~*y₁ , x₂↪~*y₂ ⟩ ⟩ ⟩ ⟩
= ⟨ y₁ , ⟨ y₂ , ⟨ refl , ⟨ ↪~*-trans t₁↪~*x₁ x₁↪~*y₁ , ↪~*-trans t₂↪~*x₂ x₂↪~*y₂ ⟩ ⟩ ⟩ ⟩
invert-≈-∃ : ∀ {n} {t₁ t₁' : Term n} {t₂ t₂' : Term (suc n)} →
(∃[x⦂ t₁ ] t₂) ≈ (∃[x⦂ t₁' ] t₂') →
t₁ ≈ t₁' × t₂ ≈ t₂'
invert-≈-∃ (mk-≈ x p q) with ↪~*-∃-shape p | ↪~*-∃-shape q
... | ⟨ a₁ , ⟨ a₂ , ⟨ refl , ⟨ t₁↪~*a₁ , t₂↪~*a₂ ⟩ ⟩ ⟩ ⟩
| ⟨ _ , ⟨ _ , ⟨ refl , ⟨ t₁'↪~*a₁ , t₂'↪~*a₂ ⟩ ⟩ ⟩ ⟩
= ⟨ mk-≈ a₁ t₁↪~*a₁ t₁'↪~*a₁ , mk-≈ a₂ t₂↪~*a₂ t₂'↪~*a₂ ⟩
↪~-≡-shape : ∀ {n} {t : Term n} {t₁ : Term n} {t₂ : Term n} →
(t₁ `≡ t₂) ↪~ t →
∃[ t₁' ] ∃[ t₂' ] t ≡ (t₁' `≡ t₂') × (t₁ ↪~* t₁') × (t₂ ↪~* t₂')
↪~-≡-shape (↪~-↪ (ξ-≡₁ r)) = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-step (↪~-↪ r) ↪~*-refl , ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~-≡-shape (↪~-↪ (ξ-≡₂ r)) = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-refl , ↪~*-step (↪~-↪ r) ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~-≡-shape (↪~-~ p) with ~→≋ p
... | ≋-≡ q r = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ~→↪~* q , ~→↪~* r ⟩ ⟩ ⟩ ⟩
↪~*-≡-shape : ∀ {n} {t : Term n} {t₁ : Term n} {t₂ : Term n} →
(t₁ `≡ t₂) ↪~* t →
∃[ t₁' ] ∃[ t₂' ] t ≡ (t₁' `≡ t₂') × (t₁ ↪~* t₁') × (t₂ ↪~* t₂')
↪~*-≡-shape ↪~*-refl = ⟨ _ , ⟨ _ , ⟨ refl , ⟨ ↪~*-refl , ↪~*-refl ⟩ ⟩ ⟩ ⟩
↪~*-≡-shape (↪~*-step r rs) with ↪~-≡-shape r
... | ⟨ x₁ , ⟨ x₂ , ⟨ refl , ⟨ t₁↪~*x₁ , t₂↪~*x₂ ⟩ ⟩ ⟩ ⟩ with ↪~*-≡-shape rs
... | ⟨ y₁ , ⟨ y₂ , ⟨ refl , ⟨ x₁↪~*y₁ , x₂↪~*y₂ ⟩ ⟩ ⟩ ⟩
= ⟨ y₁ , ⟨ y₂ , ⟨ refl , ⟨ ↪~*-trans t₁↪~*x₁ x₁↪~*y₁ , ↪~*-trans t₂↪~*x₂ x₂↪~*y₂ ⟩ ⟩ ⟩ ⟩
invert-≈-≡ : ∀ {n} {t₁ t₁' : Term n} {t₂ t₂' : Term n} →
(t₁ `≡ t₂) ≈ (t₁' `≡ t₂') →
t₁ ≈ t₁' × t₂ ≈ t₂'
invert-≈-≡ (mk-≈ x p q) with ↪~*-≡-shape p | ↪~*-≡-shape q
... | ⟨ a₁ , ⟨ a₂ , ⟨ refl , ⟨ t₁↪~*a₁ , t₂↪~*a₂ ⟩ ⟩ ⟩ ⟩
| ⟨ _ , ⟨ _ , ⟨ refl , ⟨ t₁'↪~*a₁ , t₂'↪~*a₂ ⟩ ⟩ ⟩ ⟩
= ⟨ mk-≈ a₁ t₁↪~*a₁ t₁'↪~*a₁ , mk-≈ a₂ t₂↪~*a₂ t₂'↪~*a₂ ⟩
↪~-Setn-shape : ∀ {n} {t : Term n} {l : Lvl n} →
`Setn l ↪~ t →
∃[ l' ] t ≡ `Setn l' × (l ↪~* l')
↪~-Setn-shape (↪~-↪ (ξ-Setn r)) = ⟨ _ , ⟨ refl , ↪~*-step (↪~-↪ r) ↪~*-refl ⟩ ⟩
↪~-Setn-shape (↪~-~ p) with ~→≋ p
... | ≋-Setn q = ⟨ _ , ⟨ refl , ~→↪~* q ⟩ ⟩
↪~*-Setn-shape : ∀ {n} {t : Term n} {l : Lvl n} →
`Setn l ↪~* t →
∃[ l' ] t ≡ `Setn l' × (l ↪~* l')
↪~*-Setn-shape ↪~*-refl = ⟨ _ , ⟨ refl , ↪~*-refl ⟩ ⟩
↪~*-Setn-shape (↪~*-step r rs) with ↪~-Setn-shape r
... | ⟨ x , ⟨ refl , l↪~*x ⟩ ⟩ with ↪~*-Setn-shape rs
... | ⟨ y , ⟨ refl , x↪~*y ⟩ ⟩ = ⟨ y , ⟨ refl , ↪~*-trans l↪~*x x↪~*y ⟩ ⟩
invert-≈-Setn : ∀ {n} {l l' : Lvl n} →
`Setn l ≈ `Setn l' →
l ≈ l'
invert-≈-Setn (mk-≈ x p q) with ↪~*-Setn-shape p | ↪~*-Setn-shape q
... | ⟨ a , ⟨ refl , l↪~*a ⟩ ⟩ | ⟨ _ , ⟨ refl , l'↪~*a ⟩ ⟩ = mk-≈ a l↪~*a l'↪~*a
↪~-Setω-shape : ∀ {n} {t : Term n} {l : ℕ} →
`Setω l ↪~ t →
t ≡ `Setω l
↪~-Setω-shape (↪~-↪ ())
↪~-Setω-shape (↪~-~ p) with ~→≋ p
... | ≋-Setω = refl
↪~*-Setω-shape : ∀ {n} {t : Term n} {l : ℕ} →
`Setω l ↪~* t →
t ≡ `Setω l
↪~*-Setω-shape ↪~*-refl = refl
↪~*-Setω-shape (↪~*-step r rs) with ↪~-Setω-shape r
... | refl = ↪~*-Setω-shape rs
invert-≈-Setω : ∀ {n} {l l' : ℕ} →
`Setω_ {n} l ≈ `Setω l' →
l ≡ l'
invert-≈-Setω (mk-≈ x p q) with ↪~*-Setω-shape p | ↪~*-Setω-shape q
... | refl | refl = refl
Setn≉∀ : ∀ {n} {l : Lvl n} {A : Term n} {B : Term (suc n)} → ¬ (`Setn l ≈ ∀[x⦂ A ] B)
Setn≉∀ (mk-≈ _ p q) with ↪~*-Setn-shape p | ↪~*-∀-shape q
... | ⟨ _ , ⟨ refl , _ ⟩ ⟩ | ⟨ _ , ⟨ _ , ⟨ () , _ ⟩ ⟩ ⟩
Setn≉∃ : ∀ {n} {l : Lvl n} {A : Term n} {B : Term (suc n)} → ¬ (`Setn l ≈ ∃[x⦂ A ] B)
Setn≉∃ (mk-≈ _ p q) with ↪~*-Setn-shape p | ↪~*-∃-shape q
... | ⟨ _ , ⟨ refl , _ ⟩ ⟩ | ⟨ _ , ⟨ _ , ⟨ () , _ ⟩ ⟩ ⟩
Setn≉≡ : ∀ {n} {l : Lvl n} {A : Term n} {B : Term n} → ¬ (`Setn l ≈ A `≡ B)
Setn≉≡ (mk-≈ _ p q) with ↪~*-Setn-shape p | ↪~*-≡-shape q
... | ⟨ _ , ⟨ refl , _ ⟩ ⟩ | ⟨ _ , ⟨ _ , ⟨ () , _ ⟩ ⟩ ⟩
Setω≉∀ : ∀ {n} {l : ℕ} {A : Term n} {B : Term (suc n)} → ¬ (`Setω l ≈ ∀[x⦂ A ] B)
Setω≉∀ (mk-≈ _ p q) with ↪~*-Setω-shape p | ↪~*-∀-shape q
... | refl | ⟨ _ , ⟨ _ , ⟨ () , _ ⟩ ⟩ ⟩
Setω≉∃ : ∀ {n} {l : ℕ} {A : Term n} {B : Term (suc n)} → ¬ (`Setω l ≈ ∃[x⦂ A ] B)
Setω≉∃ (mk-≈ _ p q) with ↪~*-Setω-shape p | ↪~*-∃-shape q
... | refl | ⟨ _ , ⟨ _ , ⟨ () , _ ⟩ ⟩ ⟩
Setω≉≡ : ∀ {n} {l : ℕ} {A : Term n} {B : Term n} → ¬ (`Setω l ≈ A `≡ B)
Setω≉≡ (mk-≈ _ p q) with ↪~*-Setω-shape p | ↪~*-≡-shape q
... | refl | ⟨ _ , ⟨ _ , ⟨ () , _ ⟩ ⟩ ⟩
--------------------------------------------------------------------------------
-- Inversion for lambda terms. The cumulativity rules only give universe
-- types, which are never convertible to a ∀.
invert-⊢λ' : ∀ {n} {Γ : Context n} {e : Term (suc n)} {t : Term n} →
Γ ⊢ `λ e ⦂ t →
∃[ t₁ ] ∃[ t₂ ] ∃[ T₂ ]
t ≈ (∀[x⦂ t₁ ] t₂) ×
Γ ⊢ t₁ ⦂ T₂ ×
Γ , t₁ ⊢ e ⦂ t₂
invert-⊢λ' (⊢-λ ⊢λ ⊢e) = ⟨ _ , ⟨ _ , ⟨ _ , ⟨ ≈-refl , ⟨ ⊢λ , ⊢e ⟩ ⟩ ⟩ ⟩ ⟩
invert-⊢λ' (⊢-≈ t₁≈t ⊢λ⦂t₁) with invert-⊢λ' ⊢λ⦂t₁
... | ⟨ X , ⟨ E , ⟨ L , ⟨ t₁≈∀[X]E , ⟨ X⦂Set , Γ,X⊢e⦂E ⟩ ⟩ ⟩ ⟩ ⟩
= ⟨ X , ⟨ E , ⟨ _ , ⟨ ≈-trans (≈-sym t₁≈t) t₁≈∀[X]E , ⟨ X⦂Set , Γ,X⊢e⦂E ⟩ ⟩ ⟩ ⟩ ⟩
invert-⊢λ' (⊢-Set<Setω ⊢λ) with invert-⊢λ' ⊢λ
... | ⟨ _ , ⟨ _ , ⟨ _ , ⟨ Set≈∀ , _ ⟩ ⟩ ⟩ ⟩ = ⊥-elim (Setn≉∀ Set≈∀)
invert-⊢λ : ∀ {n} {Γ : Context n} {e : Term (suc n)} {t₁ : Term n} {t₂ : Term (suc n)}
→ Γ ⊢ `λ e ⦂ ∀[x⦂ t₁ ] t₂
→ ∃[ t₁' ] ∃[ t₂' ] ∃[ T₁ ]
t₁ ≈ t₁' ×
t₂ ≈ t₂' ×
Γ ⊢ t₁' ⦂ T₁ ×
Γ , t₁' ⊢ e ⦂ t₂'
invert-⊢λ ⊢λ with invert-⊢λ' ⊢λ
... | ⟨ X , ⟨ E , ⟨ L , ⟨ ∀≈∀' , ⟨ X⦂Set , Γ,X⊢e⦂E ⟩ ⟩ ⟩ ⟩ ⟩ with invert-≈-∀ ∀≈∀'
... | ⟨ ≈₁ , ≈₂ ⟩
= ⟨ X , ⟨ E , ⟨ L , ⟨ ≈₁ , ⟨ ≈₂ , ⟨ X⦂Set , Γ,X⊢e⦂E ⟩ ⟩ ⟩ ⟩ ⟩ ⟩
-- Inversion for pairs
invert-⊢,' : ∀ {n} {Γ : Context n} {l r t : Term n} →
Γ ⊢ l `, r ⦂ t →
∃[ t₁ ] ∃[ t₂ ]
t ≈ (∃[x⦂ t₁ ] t₂) ×
Γ ⊢ l ⦂ t₁ ×
Γ ⊢ r ⦂ t₂ [ l ]
invert-⊢,' (⊢-≈ t₁≈t ⊢lr) with invert-⊢,' ⊢lr
... | ⟨ x₁ , ⟨ x₂ , ⟨ t≈∃ , ⟨ ⊢l , ⊢r ⟩ ⟩ ⟩ ⟩
= ⟨ x₁ , ⟨ x₂ , ⟨ ≈-sym (≈-trans (≈-sym t≈∃) t₁≈t) , ⟨ ⊢l , ⊢r ⟩ ⟩ ⟩ ⟩
invert-⊢,' (⊢-, {t₁ = t₁} {t₂} ⊢l ⊢r) = ⟨ t₁ , ⟨ t₂ , ⟨ ≈-refl , ⟨ ⊢l , ⊢r ⟩ ⟩ ⟩ ⟩
invert-⊢,' (⊢-Set<Setω ⊢lr) with invert-⊢,' ⊢lr
... | ⟨ _ , ⟨ _ , ⟨ Set≈∃ , _ ⟩ ⟩ ⟩ = ⊥-elim (Setn≉∃ Set≈∃)
invert-⊢, : ∀ {n} {Γ : Context n} {l r : Term n} {t₁ : Term n} {t₂ : Term (suc n)}
→ Γ ⊢ l `, r ⦂ ∃[x⦂ t₁ ] t₂
→ ∃[ t₁' ] ∃[ t₂' ]
t₁ ≈ t₁' ×
t₂ ≈ t₂' ×
Γ ⊢ l ⦂ t₁ ×
Γ ⊢ r ⦂ t₂ [ l ]
invert-⊢, ⊢lr with invert-⊢,' ⊢lr
... | ⟨ x₁ , ⟨ x₂ , ⟨ t≈∃' , ⟨ ⊢l , ⊢r ⟩ ⟩ ⟩ ⟩ with invert-≈-∃ t≈∃'
... | ⟨ ≈₁ , ≈₂ ⟩
= ⟨ x₁ , ⟨ x₂ , ⟨ ≈₁ , ⟨ ≈₂ , ⟨ ⊢-≈ (≈-sym ≈₁) ⊢l , ⊢-≈ (≈-sub _ (≈-sym ≈₂)) ⊢r ⟩ ⟩ ⟩ ⟩ ⟩
-- Inversion for refl
invert-⊢refl' : ∀ {n} {Γ : Context n} {t : Term n}
→ Γ ⊢ `refl ⦂ t
→ ∃[ u₁ ] ∃[ u₂ ]
t ≈ u₁ `≡ u₂ ×
u₁ ≈ u₂
invert-⊢refl' (⊢-≈ t₁≈t ⊢refl) with invert-⊢refl' ⊢refl
... | ⟨ u₁ , ⟨ u₂ , ⟨ t₁≈≡ , u₁≈u₂ ⟩ ⟩ ⟩
= ⟨ _ , ⟨ _ , ⟨ ≈-trans (≈-sym t₁≈t) t₁≈≡ , u₁≈u₂ ⟩ ⟩ ⟩
invert-⊢refl' (⊢-refl ⊢refl) = ⟨ _ , ⟨ _ , ⟨ ≈-refl , ≈-refl ⟩ ⟩ ⟩
invert-⊢refl' (⊢-Set<Setω ⊢refl) with invert-⊢refl' ⊢refl
... | ⟨ _ , ⟨ _ , ⟨ Set≈≡ , _ ⟩ ⟩ ⟩ = ⊥-elim (Setn≉≡ Set≈≡)
invert-⊢refl : ∀ {n} {Γ : Context n} {u₁ u₂ : Term n}
→ Γ ⊢ `refl ⦂ u₁ `≡ u₂
→ u₁ ≈ u₂
invert-⊢refl ⊢refl with invert-⊢refl' ⊢refl
... | ⟨ u₁' , ⟨ u₂' , ⟨ t≈≡' , ≈' ⟩ ⟩ ⟩ with invert-≈-≡ t≈≡'
... | ⟨ ≈₁ , ≈₂ ⟩
= ≈-trans (≈-trans ≈₁ ≈') (≈-sym ≈₂)
-- Inversion for levels used as terms: whatever the type, the level is
-- well-formed.
invert-⊢lvl : ∀ {n} {Γ : Context n} {l : Lvl n} {t : Term n}
→ Γ ⊢ `lvl l ⦂ t
→ Γ ⊢ₗ l
invert-⊢lvl (⊢-lvl ⊢l) = ⊢l
invert-⊢lvl (⊢-≈ _ ⊢l) = invert-⊢lvl ⊢l
invert-⊢lvl (⊢-Set<Setω ⊢l) = invert-⊢lvl ⊢l