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