module univTypes.SubjectReduction.ConfluenceModulo where open import Data.Product using (∃-syntax; _×_; proj₁; proj₂) renaming (_,_ to ⟨_,_⟩) open import univTypes.SubjectReduction.Specification open import univTypes.SubjectReduction.Confluence using (_↪ₚ_; _⁺; par-triangle; ↪ₚ-refl; ↪→↪ₚ; ↪ₚ→↪*) open import univTypes.SubjectReduction.LevelEqualityProperties -- Church–Rosser modulo level equality: reduction modulo level equality -- (_↪~*_) is confluent. The proof extends Takahashi's: complete development -- respects level equality (~-⁺), so _~_ ; _↪ₚ_ ; _~_ has the triangle -- property with respect to _⁺. -------------------------------------------------------------------------------- -- Complete development respects level equality -- In a redex position, ~→≋ shows that both sides have the same constructor, -- so either both are redexes or neither is. ~-⁺ : ∀ {s n} {e e' : Tm s n} → e ~ e' → e ⁺ ~ e' ⁺ ~-⁺ ~-refl = ~-refl ~-⁺ (~-sym p) = ~-sym (~-⁺ p) ~-⁺ (~-trans p q) = ~-trans (~-⁺ p) (~-⁺ q) ~-⁺ ⊔-assoc = ⊔-assoc ~-⁺ ⊔-comm = ⊔-comm ~-⁺ ⊔-idem = ⊔-idem ~-⁺ ⊔-lzero = ⊔-lzero ~-⁺ lsuc-⊔ = lsuc-⊔ ~-⁺ ⊔-lsuc = ⊔-lsuc ~-⁺ (~-λ p) = ~-λ (~-⁺ p) ~-⁺ (~-∀ p q) = ~-∀ (~-⁺ p) (~-⁺ q) ~-⁺ (~-Setn p) = ~-Setn (~-⁺ p) ~-⁺ (~-lvl p) = ~-lvl (~-⁺ p) ~-⁺ (~-lsuc p) = ~-lsuc (~-⁺ p) ~-⁺ (~-⊔ p q) = ~-⊔ (~-⁺ p) (~-⁺ q) ~-⁺ (~-∃ p q) = ~-∃ (~-⁺ p) (~-⁺ q) ~-⁺ (~-, p q) = ~-, (~-⁺ p) (~-⁺ q) ~-⁺ (~-≡ p q) = ~-≡ (~-⁺ p) (~-⁺ q) ~-⁺ (~-· p q) with ~→≋ p ... | ≋-` = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-λ _) = ~-[] (~-λ-inj (~-⁺ p)) (~-⁺ q) ... | (≋-· _ _) = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-∀ _ _) = ~-· (~-⁺ p) (~-⁺ q) ... | ≋-Setω = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-Setn _) = ~-· (~-⁺ p) (~-⁺ q) ... | ≋-Level = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-lvl _) = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-∃ _ _) = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-, _ _) = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-proj₁ _) = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-proj₂ _) = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-≡ _ _) = ~-· (~-⁺ p) (~-⁺ q) ... | ≋-`refl = ~-· (~-⁺ p) (~-⁺ q) ... | (≋-subst _ _ _) = ~-· (~-⁺ p) (~-⁺ q) ... | ≋-`Unit = ~-· (~-⁺ p) (~-⁺ q) ... | ≋-`tt = ~-· (~-⁺ p) (~-⁺ q) ~-⁺ (~-proj₁ p) with ~→≋ p ... | ≋-` = ~-proj₁ (~-⁺ p) ... | (≋-λ _) = ~-proj₁ (~-⁺ p) ... | (≋-· _ _) = ~-proj₁ (~-⁺ p) ... | (≋-∀ _ _) = ~-proj₁ (~-⁺ p) ... | ≋-Setω = ~-proj₁ (~-⁺ p) ... | (≋-Setn _) = ~-proj₁ (~-⁺ p) ... | ≋-Level = ~-proj₁ (~-⁺ p) ... | (≋-lvl _) = ~-proj₁ (~-⁺ p) ... | (≋-∃ _ _) = ~-proj₁ (~-⁺ p) ... | (≋-, _ _) = proj₁ (~-,-inj (~-⁺ p)) ... | (≋-proj₁ _) = ~-proj₁ (~-⁺ p) ... | (≋-proj₂ _) = ~-proj₁ (~-⁺ p) ... | (≋-≡ _ _) = ~-proj₁ (~-⁺ p) ... | ≋-`refl = ~-proj₁ (~-⁺ p) ... | (≋-subst _ _ _) = ~-proj₁ (~-⁺ p) ... | ≋-`Unit = ~-proj₁ (~-⁺ p) ... | ≋-`tt = ~-proj₁ (~-⁺ p) ~-⁺ (~-proj₂ p) with ~→≋ p ... | ≋-` = ~-proj₂ (~-⁺ p) ... | (≋-λ _) = ~-proj₂ (~-⁺ p) ... | (≋-· _ _) = ~-proj₂ (~-⁺ p) ... | (≋-∀ _ _) = ~-proj₂ (~-⁺ p) ... | ≋-Setω = ~-proj₂ (~-⁺ p) ... | (≋-Setn _) = ~-proj₂ (~-⁺ p) ... | ≋-Level = ~-proj₂ (~-⁺ p) ... | (≋-lvl _) = ~-proj₂ (~-⁺ p) ... | (≋-∃ _ _) = ~-proj₂ (~-⁺ p) ... | (≋-, _ _) = proj₂ (~-,-inj (~-⁺ p)) ... | (≋-proj₁ _) = ~-proj₂ (~-⁺ p) ... | (≋-proj₂ _) = ~-proj₂ (~-⁺ p) ... | (≋-≡ _ _) = ~-proj₂ (~-⁺ p) ... | ≋-`refl = ~-proj₂ (~-⁺ p) ... | (≋-subst _ _ _) = ~-proj₂ (~-⁺ p) ... | ≋-`Unit = ~-proj₂ (~-⁺ p) ... | ≋-`tt = ~-proj₂ (~-⁺ p) ~-⁺ (~-subst t p q) with ~→≋ p ... | ≋-` = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-λ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-· _ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-∀ _ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | ≋-Setω = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-Setn _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | ≋-Level = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-lvl _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-∃ _ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-, _ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-proj₁ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-proj₂ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | (≋-≡ _ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | ≋-`refl = ~-⁺ q ... | (≋-subst _ _ _) = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | ≋-`Unit = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ... | ≋-`tt = ~-subst (~-⁺ t) (~-⁺ p) (~-⁺ q) ~-⁺ (~-latom p) with ~→≋ p ... | ≋-` = ~-latom (~-⁺ p) ... | (≋-λ _) = ~-latom (~-⁺ p) ... | (≋-· _ _) = ~-latom (~-⁺ p) ... | (≋-∀ _ _) = ~-latom (~-⁺ p) ... | ≋-Setω = ~-latom (~-⁺ p) ... | (≋-Setn _) = ~-latom (~-⁺ p) ... | ≋-Level = ~-latom (~-⁺ p) ... | (≋-lvl _) = ~-lvl-inj (~-⁺ p) ... | (≋-∃ _ _) = ~-latom (~-⁺ p) ... | (≋-, _ _) = ~-latom (~-⁺ p) ... | (≋-proj₁ _) = ~-latom (~-⁺ p) ... | (≋-proj₂ _) = ~-latom (~-⁺ p) ... | (≋-≡ _ _) = ~-latom (~-⁺ p) ... | ≋-`refl = ~-latom (~-⁺ p) ... | (≋-subst _ _ _) = ~-latom (~-⁺ p) ... | ≋-`Unit = ~-latom (~-⁺ p) ... | ≋-`tt = ~-latom (~-⁺ p) -------------------------------------------------------------------------------- -- Parallel reduction modulo level equality infix 4 _⇛_ _⇛*_ data _⇛_ {s n} (e e' : Tm s n) : Set where mk-⇛ : ∀ {a b} → e ~ a → a ↪ₚ b → b ~ e' → e ⇛ e' data _⇛*_ : ∀ {s n} → Tm s n → Tm s n → Set where ⇛*-refl : ∀ {s n} {e : Tm s n} → e ⇛* e ⇛*-step : ∀ {s n} {e₁ e₂ e₃ : Tm s n} → e₁ ⇛ e₂ → e₂ ⇛* e₃ → e₁ ⇛* e₃ ⇛-triangle : ∀ {s n} {e e' : Tm s n} → e ⇛ e' → e' ⇛ e ⁺ ⇛-triangle (mk-⇛ e~a a↪ₚb b~e') = mk-⇛ (~-sym b~e') (par-triangle a↪ₚb) (~-⁺ (~-sym e~a)) ⇛-strip : ∀ {s n} {e e₁ e₂ : Tm s n} → e ⇛ e₁ → e ⇛* e₂ → ∃[ e' ] (e₁ ⇛* e') × (e₂ ⇛ e') ⇛-strip e⇛e₁ ⇛*-refl = ⟨ _ , ⟨ ⇛*-refl , e⇛e₁ ⟩ ⟩ ⇛-strip e⇛e₁ (⇛*-step e⇛e₃ e₃⇛*e₂) with ⇛-strip (⇛-triangle e⇛e₃) e₃⇛*e₂ ... | ⟨ e' , ⟨ e⁺⇛*e' , e₂⇛e' ⟩ ⟩ = ⟨ e' , ⟨ ⇛*-step (⇛-triangle e⇛e₁) e⁺⇛*e' , e₂⇛e' ⟩ ⟩ ⇛-confluence : ∀ {s n} {e e₁ e₂ : Tm s n} → e ⇛* e₁ → e ⇛* e₂ → ∃[ e' ] (e₁ ⇛* e') × (e₂ ⇛* e') ⇛-confluence ⇛*-refl e⇛*e₂ = ⟨ _ , ⟨ e⇛*e₂ , ⇛*-refl ⟩ ⟩ ⇛-confluence (⇛*-step e⇛x x⇛*e₁) e⇛*e₂ with ⇛-strip e⇛x e⇛*e₂ ... | ⟨ e* , ⟨ x⇛*e* , e₂⇛e* ⟩ ⟩ with ⇛-confluence x⇛*e₁ x⇛*e* ... | ⟨ e' , ⟨ e₁⇛*e' , e*⇛*e' ⟩ ⟩ = ⟨ e' , ⟨ e₁⇛*e' , ⇛*-step e₂⇛e* e*⇛*e' ⟩ ⟩ -------------------------------------------------------------------------------- -- Back to reduction modulo level equality ↪*→↪~* : ∀ {s n} {e e' : Tm s n} → e ↪* e' → e ↪~* e' ↪*→↪~* ↪*-refl = ↪~*-refl ↪*→↪~* (↪*-step r rs) = ↪~*-step (↪~-↪ r) (↪*→↪~* rs) ~→↪~* : ∀ {s n} {e e' : Tm s n} → e ~ e' → e ↪~* e' ~→↪~* p = ↪~*-step (↪~-~ p) ↪~*-refl ⇛→↪~* : ∀ {s n} {e e' : Tm s n} → e ⇛ e' → e ↪~* e' ⇛→↪~* (mk-⇛ e~a a↪ₚb b~e') = ↪~*-trans (~→↪~* e~a) (↪~*-trans (↪*→↪~* (↪ₚ→↪* a↪ₚb)) (~→↪~* b~e')) ⇛*→↪~* : ∀ {s n} {e e' : Tm s n} → e ⇛* e' → e ↪~* e' ⇛*→↪~* ⇛*-refl = ↪~*-refl ⇛*→↪~* (⇛*-step p ps) = ↪~*-trans (⇛→↪~* p) (⇛*→↪~* ps) ↪~→⇛ : ∀ {s n} {e e' : Tm s n} → e ↪~ e' → e ⇛ e' ↪~→⇛ (↪~-↪ r) = mk-⇛ ~-refl (↪→↪ₚ r) ~-refl ↪~→⇛ (↪~-~ p) = mk-⇛ p ↪ₚ-refl ~-refl ↪~*→⇛* : ∀ {s n} {e e' : Tm s n} → e ↪~* e' → e ⇛* e' ↪~*→⇛* ↪~*-refl = ⇛*-refl ↪~*→⇛* (↪~*-step r rs) = ⇛*-step (↪~→⇛ r) (↪~*→⇛* rs) confluence-modulo : ∀ {s n} {e e₁ e₂ : Tm s n} → e ↪~* e₁ → e ↪~* e₂ → ∃[ e' ] (e₁ ↪~* e') × (e₂ ↪~* e') confluence-modulo e↪~*e₁ e↪~*e₂ with ⇛-confluence (↪~*→⇛* e↪~*e₁) (↪~*→⇛* e↪~*e₂) ... | ⟨ e' , ⟨ e₁⇛*e' , e₂⇛*e' ⟩ ⟩ = ⟨ e' , ⟨ ⇛*→↪~* e₁⇛*e' , ⇛*→↪~* e₂⇛*e' ⟩ ⟩