Brendan Hart 2019-2020 \begin{code} {-# OPTIONS --safe --without-K #-} open import MLTT.Spartan open import UF.FunExt open import UF.PropTrunc open import UF.Subsingletons module PCF.Lambda.Substitution (pt : propositional-truncations-exist) (fe : โ {๐ค ๐ฅ} โ funext ๐ค ๐ฅ) (pe : propext ๐คโ) where open PropositionalTruncation pt open import PCF.Lambda.AbstractSyntax pt open import UF.Base ids : {n : โ} {ฮ : Context n} {A : type} โ ฮ โ A โ PCF ฮ A ids x = v x exts-cong : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} โ {f g : โ {A} โ ฮ โ A โ PCF ฮ A} โ (โ {A} โ f {A = A} โผ g) โ (โ {A} โ (exts f {B}) {A = A} โผ (exts g)) exts-cong eq Z = refl exts-cong eq (S x) = ap (rename S) (eq x) subst-cong : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} โ (M M' : PCF ฮ B) โ (f g : โ {A} โ ฮ โ A โ PCF ฮ A) โ M ๏ผ M' โ (โ {A} โ f {A = A} โผ g) โ subst f M ๏ผ subst g M' subst-cong Zero .Zero f g refl eq = refl subst-cong (Succ M) .(Succ M) f g refl eq = ap Succ (subst-cong M M f g refl eq) subst-cong (Pred M) .(Pred M) f g refl eq = ap Pred (subst-cong M M f g refl eq) subst-cong (IfZero M Mโ Mโ) .(IfZero M Mโ Mโ) f g refl eq = apโ IfZero (subst-cong M M f g refl eq) (subst-cong Mโ Mโ f g refl eq) (subst-cong Mโ Mโ f g refl eq) subst-cong (ฦ M) .(ฦ M) f g refl eq = ap ฦ (subst-cong M M (exts f) (exts g) refl (exts-cong eq)) subst-cong (M ยท Mโ) .(M ยท Mโ) f g refl eq = apโ _ยท_ (subst-cong M M f g refl eq) (subst-cong Mโ Mโ f g refl eq) subst-cong (v x) .(v x) f g refl eq = eq x subst-cong (Fix M) .(Fix M) f g refl eq = ap Fix (subst-cong M M f g refl eq) ext-cong : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} โ {ฯ ฯ' : โ {A} โ ฮ โ A โ ฮ โ A} โ (โ {A} โ ฯ {A = A} โผ ฯ') โ (โ {A} โ (ext ฯ {B}) {A = A} โผ ext ฯ') ext-cong eq Z = refl ext-cong eq (S x) = ap S (eq x) compose-ext : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} {B : type} (ฯ : โ {A} โ ฮ โ A โ ฮ โ A) (ฯ' : โ {A} โ ฮ โ A โ ฮฃ โ A) {A : type} โ (x : (ฮ โ B) โ A) โ (ext ฯ' โ ext ฯ) x ๏ผ ext (ฯ' โ ฯ) x compose-ext ฯ ฯ' Z = refl compose-ext ฯ ฯ' (S x) = refl rename-cong : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} (M M' : PCF ฮ B) (ฯ ฯ' : โ {A} โ ฮ โ A โ ฮ โ A) โ M ๏ผ M' โ (โ {A} โ ฯ {A = A} โผ ฯ') โ rename ฯ M ๏ผ rename ฯ' M' rename-cong Zero .Zero ฯ ฯ' refl eq = refl rename-cong (Succ M) .(Succ M) ฯ ฯ' refl eq = ap Succ (rename-cong M M ฯ ฯ' refl eq) rename-cong (Pred M) .(Pred M) ฯ ฯ' refl eq = ap Pred (rename-cong M M ฯ ฯ' refl eq) rename-cong (IfZero M Mโ Mโ) .(IfZero M Mโ Mโ) ฯ ฯ' refl eq = apโ IfZero (rename-cong M M ฯ ฯ' refl eq) (rename-cong Mโ Mโ ฯ ฯ' refl eq) (rename-cong Mโ Mโ ฯ ฯ' refl eq) rename-cong (ฦ M) .(ฦ M) ฯ ฯ' refl eq = ap ฦ (rename-cong M M (ext ฯ) (ext ฯ') refl (ext-cong eq)) rename-cong (M ยท Mโ) .(M ยท Mโ) ฯ ฯ' refl eq = apโ _ยท_ (rename-cong M M ฯ ฯ' refl eq) (rename-cong Mโ Mโ ฯ ฯ' refl eq) rename-cong (v x) .(v x) ฯ ฯ' refl eq = ap v (eq x) rename-cong (Fix M) .(Fix M) ฯ ฯ' refl eq = ap Fix (rename-cong M M ฯ ฯ' refl eq) compose-rename : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} {B : type} (M : PCF ฮ B) (ฯ : โ {A} โ ฮ โ A โ ฮ โ A) (ฯ' : โ {A} โ ฮ โ A โ ฮฃ โ A) โ rename ฯ' (rename ฯ M) ๏ผ rename (ฯ' โ ฯ) M compose-rename Zero ฯ ฯ' = refl compose-rename (Succ M) ฯ ฯ' = ap Succ (compose-rename M ฯ ฯ') compose-rename (Pred M) ฯ ฯ' = ap Pred (compose-rename M ฯ ฯ') compose-rename (IfZero M Mโ Mโ) ฯ ฯ' = apโ IfZero (compose-rename M ฯ ฯ') (compose-rename Mโ ฯ ฯ') (compose-rename Mโ ฯ ฯ') compose-rename (ฦ M) ฯ ฯ' = ap ฦ ฮณ where ฮณ : rename (ext ฯ') (rename (ext ฯ) M) ๏ผ rename (ext (ฯ' โ ฯ)) M ฮณ = rename (ext ฯ') (rename (ext ฯ) M) ๏ผโจ i โฉ rename ((ext ฯ') โ (ext ฯ)) M ๏ผโจ ii โฉ rename (ext (ฮป x โ ฯ' (ฯ x))) M โ where i = compose-rename M (ext ฯ) (ext ฯ') ii = rename-cong M M (ฮป {A} x โ ext ฯ' (ext ฯ x)) (ext (ฮป {A} x โ ฯ' (ฯ x))) refl (compose-ext ฯ ฯ') compose-rename (M ยท Mโ) ฯ ฯ' = apโ _ยท_ (compose-rename M ฯ ฯ') (compose-rename Mโ ฯ ฯ') compose-rename (v x) ฯ ฯ' = refl compose-rename (Fix M) ฯ ฯ' = ap Fix (compose-rename M ฯ ฯ') exts-ids : {n : โ} {ฮ : Context n} {B A : type} (x : (ฮ โ B) โ A) โ exts ids x ๏ผ ids x exts-ids Z = refl exts-ids (S x) = refl sub-id : {n : โ} {ฮ : Context n} {B : type} (M : PCF ฮ B) โ subst ids M ๏ผ M sub-id Zero = refl sub-id (Succ M) = ap Succ (sub-id M) sub-id (Pred M) = ap Pred (sub-id M) sub-id (IfZero M Mโ Mโ) = apโ IfZero (sub-id M) (sub-id Mโ) (sub-id Mโ) sub-id (ฦ M) = ap ฦ ฮณ where ฮณ : subst (exts ids) M ๏ผ M ฮณ = subst (exts ids) M ๏ผโจ subst-cong M M (exts ids) ids refl exts-ids โฉ subst ids M ๏ผโจ sub-id M โฉ M โ sub-id (M ยท Mโ) = apโ _ยท_ (sub-id M) (sub-id Mโ) sub-id (v x) = refl sub-id (Fix M) = ap Fix (sub-id M) exts-ext-ids : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} (ฯ : โ {A} โ ฮ โ A โ ฮ โ A) {A : type} (x : (ฮ โ B) โ A) โ (ids โ ext ฯ) x ๏ผ exts (ids โ ฯ) x exts-ext-ids ฯ Z = refl exts-ext-ids ฯ (S x) = refl rename-is-a-substitution : {n m : โ} {ฮ : Context n} {ฮ : Context m} {A : type} (ฯ : โ {A} โ ฮ โ A โ ฮ โ A) (M : PCF ฮ A) โ rename ฯ M ๏ผ subst (ids โ ฯ) M rename-is-a-substitution ฯ Zero = refl rename-is-a-substitution ฯ (Succ M) = ap Succ (rename-is-a-substitution ฯ M) rename-is-a-substitution ฯ (Pred M) = ap Pred (rename-is-a-substitution ฯ M) rename-is-a-substitution ฯ (IfZero M Mโ Mโ) = apโ IfZero (rename-is-a-substitution ฯ M) (rename-is-a-substitution ฯ Mโ) (rename-is-a-substitution ฯ Mโ) rename-is-a-substitution ฯ (ฦ M) = ap ฦ ฮณ where ฮณ : rename (ext ฯ) M ๏ผ subst (exts (ids โ ฯ)) M ฮณ = rename (ext ฯ) M ๏ผโจ i โฉ subst (ids โ ext ฯ) M ๏ผโจ ii โฉ subst (exts (ids โ ฯ)) M โ where i = rename-is-a-substitution (ext ฯ) M ii = subst-cong M M (ids โ ext ฯ) (exts (ids โ ฯ)) refl (exts-ext-ids ฯ) rename-is-a-substitution ฯ (M ยท Mโ) = apโ _ยท_ (rename-is-a-substitution ฯ M) (rename-is-a-substitution ฯ Mโ) rename-is-a-substitution ฯ (v x) = refl rename-is-a-substitution ฯ (Fix M) = ap Fix (rename-is-a-substitution ฯ M) _๏ผ_ : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} โ (โ {A} โ ฮ โ A โ PCF ฮ A) โ (โ {A} โ ฮ โ A โ PCF ฮฃ A) โ (โ {A} โ ฮ โ A โ PCF ฮฃ A) _๏ผ_ f g x = subst g (f x) sub-ids-right : {n m : โ} {ฮ : Context n} {ฮ : Context m} (f : โ {A} โ ฮ โ A โ PCF ฮ A) {A : type} (x : ฮ โ A) โ (f ๏ผ ids) x ๏ผ f x sub-ids-right f x = sub-id (f x) ext-comp : {a b c : โ} {ฮ : Context a} {ฮ : Context b} {ฮจ : Context c} (ฯโ : โ {A} โ ฮ โ A โ ฮ โ A) (ฯโ : โ {A} โ ฮ โ A โ ฮจ โ A) {B A : type} (x : (ฮ โ B) โ A) โ (ext ฯโ โ ext ฯโ) x ๏ผ ext (ฯโ โ ฯโ) x ext-comp ฯโ ฯโ Z = refl ext-comp ฯโ ฯโ (S x) = refl exts-ext : {a b c : โ} {ฮ : Context a} {ฮ : Context b} {ฮจ : Context c} (B : type) (ฯ : โ {A} โ ฮ โ A โ ฮ โ A) (ฯ : โ {A} โ ฮ โ A โ PCF ฮจ A) {A : type} (x : (ฮ โ B) โ A) โ (exts ฯ โ ext ฯ) x ๏ผ exts (ฯ โ ฯ) x exts-ext B ฯ ฯ Z = refl exts-ext B ฯ ฯ (S x) = refl rename-comp : {a b c : โ} {ฮ : Context a} {ฮ : Context b} {ฮจ : Context c} {B : type} (ฯโ : โ {A} โ ฮ โ A โ ฮ โ A) (ฯโ : โ {A} โ ฮ โ A โ ฮจ โ A) (t : PCF ฮ B) โ rename ฯโ (rename ฯโ t) ๏ผ rename (ฯโ โ ฯโ) t rename-comp ฯโ ฯโ Zero = refl rename-comp ฯโ ฯโ (Succ t) = ap Succ (rename-comp ฯโ ฯโ t) rename-comp ฯโ ฯโ (Pred t) = ap Pred (rename-comp ฯโ ฯโ t) rename-comp ฯโ ฯโ (IfZero t tโ tโ) = apโ IfZero (rename-comp ฯโ ฯโ t) (rename-comp ฯโ ฯโ tโ) (rename-comp ฯโ ฯโ tโ) rename-comp ฯโ ฯโ (ฦ {_} {_} {ฯ} t) = ap ฦ ฮณ where IH : rename (ext ฯโ) (rename (ext ฯโ) t) ๏ผ rename (ext ฯโ โ ext ฯโ) t IH = rename-comp (ext ฯโ) (ext ฯโ) t i : rename (ext ฯโ โ ext ฯโ) t ๏ผ rename (ฮป {A} โ ext (ฮป {A = Aโ} x โ ฯโ (ฯโ x))) t i = rename-cong t t (ext ฯโ โ ext ฯโ) (ext (ฯโ โ ฯโ)) refl (ext-comp ฯโ ฯโ) ฮณ : rename (ext ฯโ) (rename (ext ฯโ) t) ๏ผ rename (ext (ฯโ โ ฯโ)) t ฮณ = IH โ i rename-comp ฯโ ฯโ (t ยท tโ) = apโ _ยท_ (rename-comp ฯโ ฯโ t) (rename-comp ฯโ ฯโ tโ) rename-comp ฯโ ฯโ (v x) = refl rename-comp ฯโ ฯโ (Fix t) = ap Fix (rename-comp ฯโ ฯโ t) subst-rename-comp : {a b c : โ} {ฮ : Context a} {ฮ : Context b} {ฮจ : Context c} {A : type} (ฯ : โ {A} โ ฮ โ A โ ฮ โ A) (ฯ : โ {A} โ ฮ โ A โ PCF ฮจ A) (M : PCF ฮ A) โ subst ฯ (rename ฯ M) ๏ผ subst (ฯ โ ฯ) M subst-rename-comp ฯ ฯ Zero = refl subst-rename-comp ฯ ฯ (Succ M) = ap Succ (subst-rename-comp ฯ ฯ M) subst-rename-comp ฯ ฯ (Pred M) = ap Pred (subst-rename-comp ฯ ฯ M) subst-rename-comp ฯ ฯ (IfZero M Mโ Mโ) = apโ IfZero (subst-rename-comp ฯ ฯ M) (subst-rename-comp ฯ ฯ Mโ) (subst-rename-comp ฯ ฯ Mโ) subst-rename-comp ฯ ฯ (ฦ {_} {_} {A} M) = ap ฦ ฮณ where IH : subst (exts ฯ) (rename (ext ฯ) M) ๏ผ subst (exts ฯ โ ext ฯ) M IH = subst-rename-comp (ext ฯ) (exts ฯ) M ฮด : subst (exts ฯ โ ext ฯ) M ๏ผ subst (exts (ฯ โ ฯ)) M ฮด = subst-cong M M (ฮป {A} โ exts ฯ โ ext ฯ) (exts (ฮป {A} โ ฯ โ ฯ)) refl (exts-ext A ฯ ฯ) ฮณ : subst (exts ฯ) (rename (ext ฯ) M) ๏ผ subst (exts (ฯ โ ฯ)) M ฮณ = IH โ ฮด subst-rename-comp ฯ ฯ (M ยท Mโ) = apโ _ยท_ (subst-rename-comp ฯ ฯ M) (subst-rename-comp ฯ ฯ Mโ) subst-rename-comp ฯ ฯ (v x) = refl subst-rename-comp ฯ ฯ (Fix M) = ap Fix (subst-rename-comp ฯ ฯ M) rename-exts : {a b c : โ} {ฮ : Context a} {ฮ : Context b} {ฮจ : Context c} {B : type} (ฯ : โ {A} โ ฮ โ A โ PCF ฮ A) (ฯ : โ {A} โ ฮ โ A โ ฮจ โ A) {A : type} (x : (ฮ โ B) โ A) โ rename (ext ฯ) (exts ฯ x) ๏ผ exts (rename ฯ โ ฯ) x rename-exts ฯ ฯ Z = refl rename-exts ฯ ฯ (S x) = ฮณ where ฮณ : rename (ext ฯ) (exts ฯ (S x)) ๏ผ rename S (rename ฯ (ฯ x)) ฮณ = rename (ext ฯ) (exts ฯ (S x)) ๏ผโจ i โฉ rename (S โ ฯ) (ฯ x) ๏ผโจ ii โฉ rename S (rename ฯ (ฯ x)) โ where i = rename-comp S (ext ฯ) (ฯ x) ii = rename-comp ฯ S (ฯ x) โปยน subst-rename-commute : {a b c : โ} {ฮ : Context a} {ฮ : Context b} {ฮจ : Context c} {A : type} (ฯ : โ {A} โ ฮ โ A โ PCF ฮ A) (ฯ : โ {A} โ ฮ โ A โ ฮจ โ A) (M : PCF ฮ A) โ rename ฯ (subst ฯ M) ๏ผ subst (rename ฯ โ ฯ) M subst-rename-commute ฯ ฯ Zero = refl subst-rename-commute ฯ ฯ (Succ M) = ap Succ (subst-rename-commute ฯ ฯ M) subst-rename-commute ฯ ฯ (Pred M) = ap Pred (subst-rename-commute ฯ ฯ M) subst-rename-commute ฯ ฯ (IfZero M Mโ Mโ) = apโ IfZero (subst-rename-commute ฯ ฯ M) (subst-rename-commute ฯ ฯ Mโ) (subst-rename-commute ฯ ฯ Mโ) subst-rename-commute ฯ ฯ (ฦ M) = ap ฦ ฮณ where IH : rename (ext ฯ) (subst (exts ฯ) M) ๏ผ subst (rename (ext ฯ) โ exts ฯ) M IH = subst-rename-commute (exts ฯ) (ext ฯ) M ฮด : subst (rename (ext ฯ) โ exts ฯ) M ๏ผ subst (exts (rename ฯ โ ฯ)) M ฮด = subst-cong M M (rename (ext ฯ) โ exts ฯ) (exts (rename ฯ โ ฯ)) refl (rename-exts ฯ ฯ) ฮณ : rename (ext ฯ) (subst (exts ฯ) M) ๏ผ subst (exts (rename ฯ โ ฯ)) M ฮณ = IH โ ฮด subst-rename-commute ฯ ฯ (M ยท Mโ) = apโ _ยท_ (subst-rename-commute ฯ ฯ M) (subst-rename-commute ฯ ฯ Mโ) subst-rename-commute ฯ ฯ (v x) = refl subst-rename-commute ฯ ฯ (Fix M) = ap Fix (subst-rename-commute ฯ ฯ M) exts-seq : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} {B : type} (f : โ {A} โ ฮ โ A โ PCF ฮ A) (g : โ {A} โ ฮ โ A โ PCF ฮฃ A) {A : type} (x : (ฮ โ B) โ A) โ (exts f ๏ผ exts g) x ๏ผ exts (f ๏ผ g) x exts-seq f g Z = refl exts-seq f g (S x) = subst (exts g) (rename S (f x)) ๏ผโจ i โฉ subst (exts g โ S) (f x) ๏ผโจ refl โฉ subst (rename S โ g) (f x) ๏ผโจ ii โฉ rename S (subst g (f x)) โ where i = subst-rename-comp S (exts g) (f x) ii = subst-rename-commute g S (f x) โปยน sub-sub-lem : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} {B : type} (M : PCF ฮ B) (f : โ {A} โ ฮ โ A โ PCF ฮ A) (g : โ {A} โ ฮ โ A โ PCF ฮฃ A) โ subst g (subst f M) ๏ผ subst (f ๏ผ g) M sub-sub-lem Zero f g = refl sub-sub-lem (Succ M) f g = ap Succ (sub-sub-lem M f g) sub-sub-lem (Pred M) f g = ap Pred (sub-sub-lem M f g) sub-sub-lem (IfZero M Mโ Mโ) f g = apโ IfZero (sub-sub-lem M f g) (sub-sub-lem Mโ f g) (sub-sub-lem Mโ f g) sub-sub-lem (ฦ M) f g = ap ฦ ฮณ where IH : subst (exts g) (subst (exts f) M) ๏ผ subst (exts f ๏ผ exts g) M IH = sub-sub-lem M (exts f) (exts g) ฮด : subst (exts f ๏ผ exts g) M ๏ผ subst (exts (f ๏ผ g)) M ฮด = subst-cong M M (exts f ๏ผ exts g) (exts (f ๏ผ g)) refl (exts-seq f g) ฮณ : subst (exts g) (subst (exts f) M) ๏ผ subst (exts (f ๏ผ g)) M ฮณ = IH โ ฮด sub-sub-lem (M ยท Mโ) f g = apโ _ยท_ (sub-sub-lem M f g) (sub-sub-lem Mโ f g) sub-sub-lem (v x) f g = refl sub-sub-lem (Fix M) f g = ap Fix (sub-sub-lem M f g) cons-cong : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} {M M' : PCF ฮ B} {f g : โ {A} โ ฮ โ A โ PCF ฮ A} โ M ๏ผ M' โ (โ {A} โ f {A = A} โผ g ) โ (โ {A} โ (x : (ฮ โ B) โ A) โ extend-with M f x ๏ผ extend-with M' g x) cons-cong refl eq Z = refl cons-cong refl eq (S x) = eq x exts-extend-S : {n m : โ} {ฮ : Context n} {ฮ : Context m} {B : type} (f : โ {A} โ (x : ฮ โ A) โ PCF ฮ A) {A : type} (x : (ฮ โ B) โ A) โ exts f x ๏ผ extend-with (v Z) (f ๏ผ (ids โ S)) x exts-extend-S f Z = refl exts-extend-S f (S x) = rename-is-a-substitution S (f x) ๏ผ-cong : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} (f g : โ {A} โ ฮ โ A โ PCF ฮ A) (h j : โ {A} โ ฮ โ A โ PCF ฮฃ A) โ (โ {A} โ f {A = A} โผ g) โ (โ {A} โ h {A = A} โผ j) โ {A : type} (x : ฮ โ A) โ (f ๏ผ h) x ๏ผ (g ๏ผ j) x ๏ผ-cong f g h j x xโ xโ = subst-cong (f xโ) (g xโ) h j (x xโ) xโ ๏ผ-assoc : {n m k l : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} {ฯ : Context l} (f : โ {A} โ ฮ โ A โ PCF ฮ A) (g : โ {A} โ ฮ โ A โ PCF ฮฃ A) (h : โ {A} โ ฮฃ โ A โ PCF ฯ A) {A : type} (x : ฮ โ A) โ ((f ๏ผ g) ๏ผ h) x ๏ผ (f ๏ผ (g ๏ผ h)) x ๏ผ-assoc f g h x = sub-sub-lem (f x) g h subst-Z-cons-ids : {n : โ} {ฮ : Context n} {B : type} (Mโ : PCF ฮ B) {A : type} (x : (ฮ โ B) โ A) โ replace-first B ฮ Mโ x ๏ผ extend-with Mโ ids x subst-Z-cons-ids Mโ Z = refl subst-Z-cons-ids Mโ (S x) = refl sub-dist : {n m k : โ} {ฮ : Context n} {ฮ : Context m} {ฮฃ : Context k} {B : type} (M : PCF ฮ B) (f : โ {A} โ ฮ โ A โ PCF ฮ A) (g : โ {A} โ ฮ โ A โ PCF ฮฃ A) {A : type} (x : (ฮ โ B) โ A) โ (extend-with M f ๏ผ g) x ๏ผ (extend-with (subst g M) (f ๏ผ g)) x sub-dist M f g Z = refl sub-dist M f g (S x) = refl extend-replace-first : {C : type} {n m : โ} {ฮ : Context n} {ฮ : Context m} (Mโ : PCF ฮ C) (f : โ {A} โ ฮ โ A โ PCF ฮ A) {B : type} (x : (ฮ โ C) โ B) โ extend-with Mโ f x ๏ผ (exts f ๏ผ replace-first C ฮ Mโ) x extend-replace-first {ฯ} {n} {m} {ฮ} {ฮ} Mโ f x = ฮณ โปยน where ฮณ : (exts f ๏ผ replace-first ฯ ฮ Mโ) x ๏ผ extend-with Mโ f x ฮณ = (exts f ๏ผ replace-first ฯ ฮ Mโ) x ๏ผโจ i โฉ ((extend-with (v Z) (f ๏ผ (ids โ S))) ๏ผ extend-with Mโ ids) x ๏ผโจ ii โฉ extend-with (subst (extend-with Mโ ids) (v Z)) ((f ๏ผ (ids โ S)) ๏ผ extend-with Mโ ids) x ๏ผโจ iii โฉ extend-with Mโ (((f ๏ผ (ids โ S)) ๏ผ extend-with Mโ ids)) x ๏ผโจ iv โฉ extend-with Mโ (f ๏ผ ((ids โ S) ๏ผ extend-with Mโ ids)) x ๏ผโจ iiiii โฉ extend-with Mโ (f ๏ผ ids) x ๏ผโจ vi โฉ extend-with Mโ f x โ where i = ๏ผ-cong (exts f) (extend-with (v Z) (f ๏ผ (ids โ S))) (replace-first ฯ ฮ Mโ) (extend-with Mโ ids) (exts-extend-S f) (subst-Z-cons-ids Mโ) x ii = sub-dist (v Z) (f ๏ผ (ids โ S)) (extend-with Mโ ids) x iii = cons-cong refl (ฮป xโ โ refl) x iv = cons-cong refl (๏ผ-assoc f (ids โ S) (extend-with Mโ v)) x iiiii = cons-cong refl (ฮป xโ โ refl) x vi = cons-cong refl (sub-ids-right f) x subst-ext : {A B : type} {n m : โ} {ฮ : Context n} {ฮ : Context m} (M : PCF (ฮ โ A) B) (Mโ : PCF ฮ A) (f : โ {A} โ ฮ โ A โ PCF ฮ A) โ subst (extend-with Mโ f) M ๏ผ subst (replace-first A ฮ Mโ) (subst (exts f) M) subst-ext {ฯ} {ฯ} {n} {m} {ฮ} {ฮ} M Mโ f = ii โ i โปยน where i : subst (replace-first ฯ ฮ Mโ) (subst (exts f) M) ๏ผ subst (exts f ๏ผ (replace-first ฯ ฮ Mโ)) M i = sub-sub-lem M (exts f) (replace-first ฯ ฮ Mโ) ii : subst (extend-with Mโ f) M ๏ผ subst (exts f ๏ผ replace-first ฯ ฮ Mโ) M ii = subst-cong M M (extend-with Mโ f) (exts f ๏ผ replace-first ฯ ฮ Mโ) refl (extend-replace-first Mโ f) \end{code}