module univTypes.SubjectReduction.Progress where open import Data.Empty using (⊥) open import Data.Maybe using (Maybe; just; nothing) open import Data.Nat using (ℕ; suc) open import Data.Product renaming (_,_ to ⟨_,_⟩) open import Data.Sum using (_⊎_; inj₁; inj₂) open import Relation.Binary.PropositionalEquality using (_≡_; refl; trans; sym; cong) open import univTypes.SubjectReduction.Specification open import univTypes.SubjectReduction.ConversionProperties using (≈-refl; ≈-sym; ≈-trans) open import univTypes.SubjectReduction.LevelEqualityProperties using (~→≋; _≋_) open _≋_ -- Progress for open terms: a well-typed term is in normal form (Value) or -- reduces. Since ↪ is full reduction, Value describes normal forms, and -- Neutral the stuck terms (a variable applied to / projected / substituted -- along normal forms). -------------------------------------------------------------------------------- -- Heads of types. Neither reduction nor level equality changes the outermost -- constructor of a type former, so convertible types with such a -- constructor have the same head. (Setn and Setω share a head: the -- cumulativity rules move between them.) data Head : Set where h∀ h∃ h≡ hSet hLevel hUnit : Head head : ∀ {n} → Term n → Maybe Head head (∀[x⦂ _ ] _) = just h∀ head (∃[x⦂ _ ] _) = just h∃ head (_ `≡ _) = just h≡ head (`Setn _) = just hSet head (`Setω _) = just hSet head `Level = just hLevel head `Unit = just hUnit head _ = nothing ↪-head : ∀ {n} {t t' : Term n} {h} → t ↪ t' → head t ≡ just h → head t' ≡ just h ↪-head (ξ-∀₁ _) p = p ↪-head (ξ-∀₂ _) p = p ↪-head (ξ-∃₁ _) p = p ↪-head (ξ-∃₂ _) p = p ↪-head (ξ-≡₁ _) p = p ↪-head (ξ-≡₂ _) p = p ↪-head (ξ-Setn _) p = p ↪-head β-λ () ↪-head (ξ-λ _) () ↪-head (ξ-·₁ _) () ↪-head (ξ-·₂ _) () ↪-head β-proj₁ () ↪-head β-proj₂ () ↪-head β-subst () ↪-head (ξ-proj₁ _) () ↪-head (ξ-proj₂ _) () ↪-head (ξ-,₁ _) () ↪-head (ξ-,₂ _) () ↪-head (ξ-subst₁ _) () ↪-head (ξ-subst₂ _) () ↪-head (ξ-subst₃ _) () ↪-head (ξ-lvl _) () ~-head : ∀ {n} {t t' : Term n} {h} → t ~ t' → head t ≡ just h → head t' ≡ just h ~-head q p with ~→≋ q ... | ≋-∀ _ _ = p ... | ≋-∃ _ _ = p ... | ≋-≡ _ _ = p ... | ≋-Setn _ = p ... | ≋-Setω = p ... | ≋-Level = p ... | ≋-`Unit = p ~-head _ () | ≋-` ~-head _ () | ≋-λ _ ~-head _ () | ≋-· _ _ ~-head _ () | ≋-lvl _ ~-head _ () | ≋-, _ _ ~-head _ () | ≋-proj₁ _ ~-head _ () | ≋-proj₂ _ ~-head _ () | ≋-`refl ~-head _ () | ≋-subst _ _ _ ~-head _ () | ≋-`tt ↪~*-head : ∀ {n} {t t' : Term n} {h} → t ↪~* t' → head t ≡ just h → head t' ≡ just h ↪~*-head ↪~*-refl p = p ↪~*-head (↪~*-step (↪~-↪ r) rs) p = ↪~*-head rs (↪-head r p) ↪~*-head (↪~*-step (↪~-~ q) rs) p = ↪~*-head rs (~-head q p) ≈-head : ∀ {n} {t t' : Term n} {h h'} → t ≈ t' → head t ≡ just h → head t' ≡ just h' → h ≡ h' ≈-head (mk-≈ _ p q) ht ht' with trans (sym (↪~*-head p ht)) (↪~*-head q ht') ... | refl = refl -------------------------------------------------------------------------------- -- Canonical forms. The normal forms that are not neutral are introduction -- forms; each determines the head of its type. data Canonical {n} : Head → Term n → Set where can-λ : ∀ {e : Term (suc n)} → Canonical h∀ (`λ e) can-, : ∀ {e₁ e₂ : Term n} → Canonical h∃ (e₁ `, e₂) can-refl : Canonical h≡ `refl can-lvl : ∀ {l : Lvl n} → Canonical hLevel (`lvl l) can-tt : Canonical hUnit `tt can-∀ : ∀ {t₁ : Term n} {t₂} → Canonical hSet (∀[x⦂ t₁ ] t₂) can-∃ : ∀ {t₁ : Term n} {t₂} → Canonical hSet (∃[x⦂ t₁ ] t₂) can-≡ : ∀ {e₁ e₂ : Term n} → Canonical hSet (e₁ `≡ e₂) can-Setn : ∀ {l : Lvl n} → Canonical hSet (`Setn l) can-Setω : ∀ {l : ℕ} → Canonical hSet (`Setω l) can-Level : Canonical hSet `Level can-Unit : Canonical hSet `Unit -- The type of an introduction form is convertible to a type with the -- matching head. typed-canonical : ∀ {n} {Γ : Context n} {e T : Term n} {h} → Γ ⊢ e ⦂ T → Canonical h e → ∃[ T' ] T ≈ T' × head T' ≡ just h typed-canonical (⊢-λ _ _) can-λ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-, _ _) can-, = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-refl _) can-refl = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-lvl _) can-lvl = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-∀ _ _) can-∀ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-∀ω _ _) can-∀ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-∀-dep _ _) can-∀ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-∃ _ _) can-∃ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-∃ω _ _) can-∃ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-∃-dep _ _) can-∃ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-≡ _ _ _) can-≡ = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-Setn _) can-Setn = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical ⊢-Setω can-Setω = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical ⊢-Level can-Level = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical ⊢-tt can-tt = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical ⊢-Unit can-Unit = ⟨ _ , ⟨ ≈-refl , refl ⟩ ⟩ typed-canonical (⊢-≈ T≈T₁ ⊢e) c with typed-canonical ⊢e c ... | ⟨ T' , ⟨ T≈T' , hT' ⟩ ⟩ = ⟨ T' , ⟨ ≈-trans (≈-sym T≈T₁) T≈T' , hT' ⟩ ⟩ typed-canonical (⊢-Set