Substitution: Substitution in the untyped lambda calculus
module plfa.part2.Substitution where
Introduction
The primary purpose of this chapter is to prove that substitution commutes with itself. Barendregt (1984) refers to this as the substitution lemma:
M [x:=N] [y:=L] = M [y:=L] [x:= N[y:=L] ]In our setting, with de Bruijn indices for variables, the statement of the lemma becomes:
M [ N ] [ L ] β‘ Mγ L γ[ N [ L ] ] (substitution)where the notation M γ L γ is for substituting L for index 1 inside M. In addition, because we define substitution in terms of parallel substitution, we have the following generalization, replacing the substitution of L with an arbitrary parallel substitution Ο.
subst Ο (M [ N ]) β‘ (subst (exts Ο) M) [ subst Ο N ] (subst-commute)The special case for renamings is also useful.
rename Ο (M [ N ]) β‘ (rename (ext Ο) M) [ rename Ο N ]
(rename-subst-commute)The secondary purpose of this chapter is to define the Ο algebra of parallel substitution due to Abadi, Cardelli, Curien, and Levy (1991). The equations of this algebra not only help us prove the substitution lemma, but they are generally useful. Furthermore, when the equations are applied from left to right, they form a rewrite system that decides whether any two substitutions are equal.
Imports
import Relation.Binary.PropositionalEquality as Eq open Eq using (_β‘_; refl; sym; cong; congβ; cong-app) open Eq.β‘-Reasoning using (begin_; step-β‘-β£; step-β‘-β©; _β) open import Function.Base using (_β_) open import plfa.part2.Untyped using (Type; Context; _β’_; β ; _β_; β ; _,_; Z; S_; `_; Ζ_; _Β·_; rename; subst; ext; exts; _[_]; subst-zero)
postulate extensionality : β {A B : Set} {f g : A β B} β (β (x : A) β f x β‘ g x) ----------------------- β f β‘ g
Notation
We introduce the following shorthand for the type of a renaming from variables in context Ξ to variables in context Ξ.
Rename : Context β Context β Set Rename Ξ Ξ = β{A} β Ξ β A β Ξ β A
Similarly, we introduce the following shorthand for the type of a substitution from variables in context Ξ to terms in context Ξ.
Subst : Context β Context β Set Subst Ξ Ξ = β{A} β Ξ β A β Ξ β’ A
We use the following more succinct notation for the subst function.
βͺ_β« : β{Ξ Ξ A} β Subst Ξ Ξ β Ξ β’ A β Ξ β’ A βͺ Ο β« = Ξ» M β subst Ο M
The Ο algebra of substitution
A substitution maps de Bruijn indices (natural numbers) to terms, so we can view a substitution simply as a sequence of terms, or more precisely, as an infinite sequence of terms. The Ο algebra consists of four operations for building such sequences: identity ids, shift β, cons M β’ Ο, and sequencing Ο β¨ Ο. The sequence 0, 1, 2, ... is constructed by the identity substitution.
ids : β{Ξ} β Subst Ξ Ξ ids x = ` x
The shift operation β constructs the sequence
1, 2, 3, ...and is defined as follows.
β : β{Ξ A} β Subst Ξ (Ξ , A) β x = ` (S x)
Given a term M and substitution Ο, the operation M β’ Ο constructs the sequence
M , Ο 0, Ο 1, Ο 2, ...This operation is analogous to the cons operation of Lisp.
infixr 6 _β’_ _β’_ : β{Ξ Ξ A} β (Ξ β’ A) β Subst Ξ Ξ β Subst (Ξ , A) Ξ (M β’ Ο) Z = M (M β’ Ο) (S x) = Ο x
Given two substitutions Ο and Ο, the sequencing operation Ο β¨ Ο composes the two substitutions by first applying Ο and then applying Ο. So it produces the sequence
βͺΟβ«(Ο 0), βͺΟβ«(Ο 1), βͺΟβ«(Ο 2), ...Here is the definition.
infixr 5 _β¨_ _β¨_ : β{Ξ Ξ Ξ£} β Subst Ξ Ξ β Subst Ξ Ξ£ β Subst Ξ Ξ£ Ο β¨ Ο = βͺ Ο β« β Ο
For the sequencing operation, Abadi et al. use the notation of function composition, writing Ο β Ο, but still with Ο applied before Ο, which is the opposite of standard mathematical practice. We instead write Ο β¨ Ο, because semicolon is the standard notation for forward function composition.
The Ο algebra equations
The Ο algebra includes the following equations.
(sub-head) βͺ M β’ Ο β« (` Z) β‘ M
(sub-tail) β β¨ (M β’ Ο) β‘ Ο
(sub-Ξ·) (βͺ Ο β« (` Z)) β’ (β β¨ Ο) β‘ Ο
(Z-shift) (` Z) β’ β β‘ ids
(sub-id) βͺ ids β« M β‘ M
(sub-app) βͺ Ο β« (L Β· M) β‘ (βͺ Ο β« L) Β· (βͺ Ο β« M)
(sub-abs) βͺ Ο β« (Ζ N) β‘ Ζ βͺ (` Z) β’ (Ο β¨ β) β« N
(sub-sub) βͺ Ο β« βͺ Ο β« M β‘ βͺ Ο β¨ Ο β« M
(sub-idL) ids β¨ Ο β‘ Ο
(sub-idR) Ο β¨ ids β‘ Ο
(sub-assoc) (Ο β¨ Ο) β¨ ΞΈ β‘ Ο β¨ (Ο β¨ ΞΈ)
(sub-dist) (M β’ Ο) β¨ Ο β‘ (βͺ Ο β« M) β’ (Ο β¨ Ο)The first group of equations describe how the β’ operator acts like cons. The equation sub-head says that the variable zero Z returns the head of the sequence (it acts like the car of Lisp). Similarly, sub-tail says that sequencing with shift β returns the tail of the sequence (it acts like cdr of Lisp). The sub-Ξ· equation is the Ξ·-expansion rule for sequences, saying that taking the head and tail of a sequence, and then consβing them together yields the original sequence. The Z-shift equation says that consβing zero onto the shifted sequence produces the identity sequence.
The next four equations involve applying substitutions to terms. The equation sub-id says that the identity substitution returns the term unchanged. The equations sub-app and sub-abs says that substitution is a congruence for the lambda calculus. The sub-sub equation says that the sequence operator β¨ behaves as intended.
The last four equations concern the sequencing of substitutions. The first two equations, sub-idL and sub-idR, say that ids is the left and right unit of the sequencing operator. The sub-assoc equation says that sequencing is associative. Finally, sub-dist says that post-sequencing distributes through cons.
Relating the Ο algebra and substitution functions
The definitions of substitution N [ M ] and parallel substitution subst Ο N depend on several auxiliary functions: rename, exts, ext, and subst-zero. We shall relate those functions to terms in the Ο algebra.
To begin with, renaming can be expressed in terms of substitution. We have
rename Ο M β‘ βͺ ren Ο β« M (rename-subst-ren)where ren turns a renaming Ο into a substitution by post-composing Ο with the identity substitution.
ren : β{Ξ Ξ} β Rename Ξ Ξ β Subst Ξ Ξ ren Ο = ids β Ο
When the renaming is the increment function, then it is equivalent to shift.
ren S_ β‘ β (ren-shift)
rename S_ M β‘ βͺ β β« M (rename-shift)Renaming with the identity renaming leaves the term unchanged.
rename (Ξ» {A} x β x) M β‘ M (rename-id)Next we relate the exts function to the Ο algebra. Recall that the exts function extends a substitution as follows:
exts Ο = ` Z, rename S_ (Ο 0), rename S_ (Ο 1), rename S_ (Ο 2), ...So exts is equivalent to consβing Z onto the sequence formed by applying Ο and then shifting.
exts Ο β‘ ` Z β’ (Ο β¨ β) (exts-cons-shift)The ext function does the same job as exts but for renamings instead of substitutions. So composing ext with ren is the same as composing ren with exts.
ren (ext Ο) β‘ exts (ren Ο) (ren-ext)Thus, we can recast the exts-cons-shift equation in terms of renamings.
ren (ext Ο) β‘ ` Z β’ (ren Ο β¨ β) (ext-cons-Z-shift)It is also useful to specialize the sub-sub equation of the Ο algebra to the situation where the first substitution is a renaming.
βͺ Ο β« (rename Ο M) β‘ βͺ Ο β Ο β« M (rename-subst)The subst-zero M substitution is equivalent to consβing M onto the identity substitution.
subst-zero M β‘ M β’ ids (subst-Z-cons-ids)Finally, sequencing exts Ο with subst-zero M is equivalent to consβing M onto Ο.
exts Ο β¨ subst-zero M β‘ (M β’ Ο) (subst-zero-exts-cons)Proofs of sub-head, sub-tail, sub-Ξ·, Z-shift, sub-idL, sub-dist, and sub-app
We start with the proofs that are immediate from the definitions of the operators.
sub-head : β {Ξ Ξ} {A} {M : Ξ β’ A}{Ο : Subst Ξ Ξ} β βͺ M β’ Ο β« (` Z) β‘ M sub-head = refl
sub-tail : β{Ξ Ξ} {A B} {M : Ξ β’ A} {Ο : Subst Ξ Ξ} β (β β¨ M β’ Ο) {A = B} β‘ Ο sub-tail = extensionality Ξ» x β refl
sub-Ξ· : β{Ξ Ξ} {A B} {Ο : Subst (Ξ , A) Ξ} β (βͺ Ο β« (` Z) β’ (β β¨ Ο)) {A = B} β‘ Ο sub-Ξ· {Ξ}{Ξ}{A}{B}{Ο} = extensionality Ξ» x β lemma where lemma : β {x} β ((βͺ Ο β« (` Z)) β’ (β β¨ Ο)) x β‘ Ο x lemma {x = Z} = refl lemma {x = S x} = refl
Z-shift : β{Ξ}{A B} β ((` Z) β’ β) β‘ ids {Ξ , A} {B} Z-shift {Ξ}{A}{B} = extensionality lemma where lemma : (x : Ξ , A β B) β ((` Z) β’ β) x β‘ ids x lemma Z = refl lemma (S y) = refl
sub-idL : β{Ξ Ξ} {Ο : Subst Ξ Ξ} {A} β ids β¨ Ο β‘ Ο {A} sub-idL = extensionality Ξ» x β refl
sub-dist : β{Ξ Ξ Ξ£ : Context} {A B} {Ο : Subst Ξ Ξ} {Ο : Subst Ξ Ξ£} {M : Ξ β’ A} β ((M β’ Ο) β¨ Ο) β‘ ((subst Ο M) β’ (Ο β¨ Ο)) {B} sub-dist {Ξ}{Ξ}{Ξ£}{A}{B}{Ο}{Ο}{M} = extensionality Ξ» x β lemma {x = x} where lemma : β {x : Ξ , A β B} β ((M β’ Ο) β¨ Ο) x β‘ ((subst Ο M) β’ (Ο β¨ Ο)) x lemma {x = Z} = refl lemma {x = S x} = refl
sub-app : β{Ξ Ξ} {Ο : Subst Ξ Ξ} {L : Ξ β’ β }{M : Ξ β’ β } β βͺ Ο β« (L Β· M) β‘ (βͺ Ο β« L) Β· (βͺ Ο β« M) sub-app = refl
Interlude: congruences
In this section we establish congruence rules for the Ο algebra operators β’ and β¨ and for subst and its helper functions ext, rename, exts, and subst-zero. These congruence rules help with the equational reasoning in the later sections of this chapter.
cong-ext : β{Ξ Ξ}{Ο Οβ² : Rename Ξ Ξ}{B} β (β{A} β Ο β‘ Οβ² {A}) --------------------------------- β β{A} β ext Ο {B = B} β‘ ext Οβ² {A} cong-ext{Ξ}{Ξ}{Ο}{Οβ²}{B} rr {A} = extensionality Ξ» x β lemma {x} where lemma : β{x : Ξ , B β A} β ext Ο x β‘ ext Οβ² x lemma {Z} = refl lemma {S y} = cong S_ (cong-app rr y)
cong-rename : β{Ξ Ξ}{Ο Οβ² : Rename Ξ Ξ}{B}{M : Ξ β’ B} β (β{A} β Ο β‘ Οβ² {A}) ------------------------ β rename Ο M β‘ rename Οβ² M cong-rename {M = ` x} rr = cong `_ (cong-app rr x) cong-rename {Ο = Ο} {Οβ² = Οβ²} {M = Ζ N} rr = cong Ζ_ (cong-rename {Ο = ext Ο}{Οβ² = ext Οβ²}{M = N} (cong-ext rr)) cong-rename {M = L Β· M} rr = congβ _Β·_ (cong-rename rr) (cong-rename rr)
cong-exts : β{Ξ Ξ}{Ο Οβ² : Subst Ξ Ξ}{B} β (β{A} β Ο β‘ Οβ² {A}) ----------------------------------- β β{A} β exts Ο {B = B} β‘ exts Οβ² {A} cong-exts{Ξ}{Ξ}{Ο}{Οβ²}{B} ss {A} = extensionality Ξ» x β lemma {x} where lemma : β{x} β exts Ο x β‘ exts Οβ² x lemma {Z} = refl lemma {S x} = cong (rename S_) (cong-app (ss {A}) x)
cong-sub : β{Ξ Ξ}{Ο Οβ² : Subst Ξ Ξ}{A}{M Mβ² : Ξ β’ A} β (β{A} β Ο β‘ Οβ² {A}) β M β‘ Mβ² ------------------------------ β subst Ο M β‘ subst Οβ² Mβ² cong-sub {Ξ} {Ξ} {Ο} {Οβ²} {A} {` x} ss refl = cong-app ss x cong-sub {Ξ} {Ξ} {Ο} {Οβ²} {A} {Ζ M} ss refl = cong Ζ_ (cong-sub {Ο = exts Ο}{Οβ² = exts Οβ²} {M = M} (cong-exts ss) refl) cong-sub {Ξ} {Ξ} {Ο} {Οβ²} {A} {L Β· M} ss refl = congβ _Β·_ (cong-sub {M = L} ss refl) (cong-sub {M = M} ss refl)
cong-sub-zero : β{Ξ}{B : Type}{M Mβ² : Ξ β’ B} β M β‘ Mβ² ----------------------------------------- β β{A} β subst-zero M β‘ (subst-zero Mβ²) {A} cong-sub-zero {Ξ}{B}{M}{Mβ²} mm' {A} = extensionality Ξ» x β cong (Ξ» z β subst-zero z x) mm'
cong-cons : β{Ξ Ξ}{A}{M N : Ξ β’ A}{Ο Ο : Subst Ξ Ξ} β M β‘ N β (β{A} β Ο {A} β‘ Ο {A}) -------------------------------- β β{A} β (M β’ Ο) {A} β‘ (N β’ Ο) {A} cong-cons{Ξ}{Ξ}{A}{M}{N}{Ο}{Ο} refl st {Aβ²} = extensionality lemma where lemma : (x : Ξ , A β Aβ²) β (M β’ Ο) x β‘ (M β’ Ο) x lemma Z = refl lemma (S x) = cong-app st x
cong-seq : β{Ξ Ξ Ξ£}{Ο Οβ² : Subst Ξ Ξ}{Ο Οβ² : Subst Ξ Ξ£} β (β{A} β Ο {A} β‘ Οβ² {A}) β (β{A} β Ο {A} β‘ Οβ² {A}) β β{A} β (Ο β¨ Ο) {A} β‘ (Οβ² β¨ Οβ²) {A} cong-seq {Ξ}{Ξ}{Ξ£}{Ο}{Οβ²}{Ο}{Οβ²} ss' tt' {A} = extensionality lemma where lemma : (x : Ξ β A) β (Ο β¨ Ο) x β‘ (Οβ² β¨ Οβ²) x lemma x = begin (Ο β¨ Ο) x β‘β¨β© subst Ο (Ο x) β‘β¨ cong (subst Ο) (cong-app ss' x) β© subst Ο (Οβ² x) β‘β¨ cong-sub{M = Οβ² x} tt' refl β© subst Οβ² (Οβ² x) β‘β¨β© (Οβ² β¨ Οβ²) x β
Relating rename, exts, ext, and subst-zero to the Ο algebra
In this section we establish equations that relate subst and its helper functions (rename, exts, ext, and subst-zero) to terms in the Ο algebra.
The first equation we prove is
rename Ο M β‘ βͺ ren Ο β« M (rename-subst-ren)Because subst uses the exts function, we need the following lemma which says that exts and ext do the same thing except that ext works on renamings and exts works on substitutions.
ren-ext : β {Ξ Ξ}{B C : Type} {Ο : Rename Ξ Ξ} β ren (ext Ο {B = B}) β‘ exts (ren Ο) {C} ren-ext {Ξ}{Ξ}{B}{C}{Ο} = extensionality Ξ» x β lemma {x = x} where lemma : β {x : Ξ , B β C} β (ren (ext Ο)) x β‘ exts (ren Ο) x lemma {x = Z} = refl lemma {x = S x} = refl
With this lemma in hand, the proof is a straightforward induction on the term M.
rename-subst-ren : β {Ξ Ξ}{A} {Ο : Rename Ξ Ξ}{M : Ξ β’ A} β rename Ο M β‘ βͺ ren Ο β« M rename-subst-ren {M = ` x} = refl rename-subst-ren {Ο = Ο}{M = Ζ N} = begin rename Ο (Ζ N) β‘β¨β© Ζ rename (ext Ο) N β‘β¨ cong Ζ_ (rename-subst-ren {Ο = ext Ο}{M = N}) β© Ζ βͺ ren (ext Ο) β« N β‘β¨ cong Ζ_ (cong-sub {M = N} ren-ext refl) β© Ζ βͺ exts (ren Ο) β« N β‘β¨β© βͺ ren Ο β« (Ζ N) β rename-subst-ren {M = L Β· M} = congβ _Β·_ rename-subst-ren rename-subst-ren
The substitution ren S_ is equivalent to β.
ren-shift : β{Ξ}{A}{B} β ren S_ β‘ β {A = B} {A} ren-shift {Ξ}{A}{B} = extensionality Ξ» x β lemma {x = x} where lemma : β {x : Ξ β A} β ren (S_{B = B}) x β‘ β {A = B} x lemma {x = Z} = refl lemma {x = S x} = refl
The substitution rename S_ M is equivalent to shifting: βͺ β β« M.
rename-shift : β{Ξ} {A} {B} {M : Ξ β’ A} β rename (S_{B = B}) M β‘ βͺ β β« M rename-shift{Ξ}{A}{B}{M} = begin rename S_ M β‘β¨ rename-subst-ren β© βͺ ren S_ β« M β‘β¨ cong-sub{M = M} ren-shift refl β© βͺ β β« M β
Next we prove the equation exts-cons-shift, which states that exts is equivalent to consβing Z onto the sequence formed by applying Ο and then shifting. The proof is by case analysis on the variable x, using rename-subst-ren for when x = S y.
exts-cons-shift : β{Ξ Ξ} {A B} {Ο : Subst Ξ Ξ} β exts Ο {A} {B} β‘ (` Z β’ (Ο β¨ β)) exts-cons-shift = extensionality Ξ» x β lemma{x = x} where lemma : β{Ξ Ξ} {A B} {Ο : Subst Ξ Ξ} {x : Ξ , B β A} β exts Ο x β‘ (` Z β’ (Ο β¨ β)) x lemma {x = Z} = refl lemma {x = S y} = rename-subst-ren
As a corollary, we have a similar correspondence for ren (ext Ο).
ext-cons-Z-shift : β{Ξ Ξ} {Ο : Rename Ξ Ξ}{A}{B} β ren (ext Ο {B = B}) β‘ (` Z β’ (ren Ο β¨ β)) {A} ext-cons-Z-shift {Ξ}{Ξ}{Ο}{A}{B} = begin ren (ext Ο) β‘β¨ ren-ext β© exts (ren Ο) β‘β¨ exts-cons-shift{Ο = ren Ο} β© ((` Z) β’ (ren Ο β¨ β)) β
Finally, the subst-zero M substitution is equivalent to consβing M onto the identity substitution.
subst-Z-cons-ids : β{Ξ}{A B : Type}{M : Ξ β’ B} β subst-zero M β‘ (M β’ ids) {A} subst-Z-cons-ids = extensionality Ξ» x β lemma {x = x} where lemma : β{Ξ}{A B : Type}{M : Ξ β’ B}{x : Ξ , B β A} β subst-zero M x β‘ (M β’ ids) x lemma {x = Z} = refl lemma {x = S x} = refl
Proofs of sub-abs, sub-id, and rename-id
The equation sub-abs follows immediately from the equation exts-cons-shift.
sub-abs : β{Ξ Ξ} {Ο : Subst Ξ Ξ} {N : Ξ , β β’ β } β βͺ Ο β« (Ζ N) β‘ Ζ βͺ (` Z) β’ (Ο β¨ β) β« N sub-abs {Ο = Ο}{N = N} = begin βͺ Ο β« (Ζ N) β‘β¨β© Ζ βͺ exts Ο β« N β‘β¨ cong Ζ_ (cong-sub{M = N} exts-cons-shift refl) β© Ζ βͺ (` Z) β’ (Ο β¨ β) β« N β
The proof of sub-id requires the following lemma which says that extending the identity substitution produces the identity substitution.
exts-ids : β{Ξ}{A B} β exts ids β‘ ids {Ξ , B} {A} exts-ids {Ξ}{A}{B} = extensionality lemma where lemma : (x : Ξ , B β A) β exts ids x β‘ ids x lemma Z = refl lemma (S x) = refl
The proof of βͺ ids β« M β‘ M now follows easily by induction on M, using exts-ids in the case for M β‘ Ζ N.
sub-id : β{Ξ} {A} {M : Ξ β’ A} β βͺ ids β« M β‘ M sub-id {M = ` x} = refl sub-id {M = Ζ N} = begin βͺ ids β« (Ζ N) β‘β¨β© Ζ βͺ exts ids β« N β‘β¨ cong Ζ_ (cong-sub{M = N} exts-ids refl) β© Ζ βͺ ids β« N β‘β¨ cong Ζ_ sub-id β© Ζ N β sub-id {M = L Β· M} = congβ _Β·_ sub-id sub-id
The rename-id equation is a corollary is sub-id.
rename-id : β {Ξ}{A} {M : Ξ β’ A} β rename (Ξ» {A} x β x) M β‘ M rename-id {M = M} = begin rename (Ξ» {A} x β x) M β‘β¨ rename-subst-ren β© βͺ ren (Ξ» {A} x β x) β« M β‘β¨β© βͺ ids β« M β‘β¨ sub-id β© M β
Proof of sub-idR
The proof of sub-idR follows directly from sub-id.
sub-idR : β{Ξ Ξ} {Ο : Subst Ξ Ξ} {A} β (Ο β¨ ids) β‘ Ο {A} sub-idR {Ξ}{Ο = Ο}{A} = begin Ο β¨ ids β‘β¨β© βͺ ids β« β Ο β‘β¨ extensionality (Ξ» x β sub-id) β© Ο β
Proof of sub-sub
The sub-sub equation states that sequenced substitutions Ο β¨ Ο are equivalent to first applying Ο then applying Ο.
βͺ Ο β« βͺ Ο β« M β‘ βͺ Ο β¨ Ο β« MThe proof requires several lemmas. First, we need to prove the specialization for renaming.
rename Ο (rename Οβ² M) β‘ rename (Ο β Οβ²) MThis in turn requires the following lemma about ext.
compose-ext : β{Ξ Ξ Ξ£}{Ο : Rename Ξ Ξ£} {Οβ² : Rename Ξ Ξ} {A B} β ((ext Ο) β (ext Οβ²)) β‘ ext (Ο β Οβ²) {B} {A} compose-ext = extensionality Ξ» x β lemma {x = x} where lemma : β{Ξ Ξ Ξ£}{Ο : Rename Ξ Ξ£} {Οβ² : Rename Ξ Ξ} {A B} {x : Ξ , B β A} β ((ext Ο) β (ext Οβ²)) x β‘ ext (Ο β Οβ²) x lemma {x = Z} = refl lemma {x = S x} = refl
To prove that composing renamings is equivalent to applying one after the other using rename, we proceed by induction on the term M, using the compose-ext lemma in the case for M β‘ Ζ N.
compose-rename : β{Ξ Ξ Ξ£}{A}{M : Ξ β’ A}{Ο : Rename Ξ Ξ£}{Οβ² : Rename Ξ Ξ} β rename Ο (rename Οβ² M) β‘ rename (Ο β Οβ²) M compose-rename {M = ` x} = refl compose-rename {Ξ}{Ξ}{Ξ£}{A}{Ζ N}{Ο}{Οβ²} = cong Ζ_ G where G : rename (ext Ο) (rename (ext Οβ²) N) β‘ rename (ext (Ο β Οβ²)) N G = begin rename (ext Ο) (rename (ext Οβ²) N) β‘β¨ compose-rename{Ο = ext Ο}{Οβ² = ext Οβ²} β© rename ((ext Ο) β (ext Οβ²)) N β‘β¨ cong-rename compose-ext β© rename (ext (Ο β Οβ²)) N β compose-rename {M = L Β· M} = congβ _Β·_ compose-rename compose-rename
The next lemma states that if a renaming and substitution commute on variables, then they also commute on terms. We explain the proof in detail below.
commute-subst-rename : β{Ξ Ξ}{M : Ξ β’ β }{Ο : Subst Ξ Ξ} {Οβ : Rename Ξ (Ξ , β )}{Οβ : Rename Ξ (Ξ , β )} β (β{x : Ξ β β } β exts Ο (Οβ x) β‘ rename Οβ (Ο x)) β subst (exts Ο) (rename Οβ M) β‘ rename Οβ (subst Ο M) commute-subst-rename {M = ` x} H = H commute-subst-rename {Ξ}{Ξ}{Ζ N}{Ο}{Οβ}{Οβ} H = cong Ζ_ (commute-subst-rename{Ξ , β }{Ξ , β }{N} {exts Ο}{ext Οβ}{ext Οβ} (Ξ» {x} β Hβ² {x})) where Hβ² : {x : Ξ , β β β } β exts (exts Ο) (ext Οβ x) β‘ rename (ext Οβ) (exts Ο x) Hβ² {Z} = refl Hβ² {S y} = begin exts (exts Ο) (ext Οβ (S y)) β‘β¨β© rename S_ (exts Ο (Οβ y)) β‘β¨ cong (rename S_) H β© rename S_ (rename Οβ (Ο y)) β‘β¨ compose-rename β© rename (S_ β Οβ) (Ο y) β‘β¨β© rename ((ext Οβ) β S_) (Ο y) β‘β¨ sym compose-rename β© rename (ext Οβ) (rename S_ (Ο y)) β‘β¨β© rename (ext Οβ) (exts Ο (S y)) β commute-subst-rename {M = L Β· N} H = congβ _Β·_ (commute-subst-rename{M = L} H) (commute-subst-rename{M = N} H)
The proof is by induction on the term M.
If
Mis a variable, then we use the premise to conclude.If
M β‘ Ζ N, we conclude using the induction hypothesis forN. However, to use the induction hypothesis, we must show thatexts (exts Ο) (ext Οβ x) β‘ rename (ext Οβ) (exts Ο x)We prove this equation by cases on x.
If
x = Z, the two sides are equal by definition.If
x = S y, we obtain the goal by the following equational reasoning.exts (exts Ο) (ext Οβ (S y)) β‘ rename S_ (exts Ο (Οβ y)) β‘ rename S_ (rename Οβ (Ο y)) (by the premise) β‘ rename (S_ β Οβ) (Ο y) (by compose-rename) β‘ rename ((ext Οβ) β S_) (Ο y) β‘ rename (ext Οβ) (rename S_ (Ο y)) (by compose-rename) β‘ rename (ext Οβ) (exts Ο (S y))
If
Mis an application, we obtain the goal using the induction hypothesis for each subterm.
The last lemma needed to prove sub-sub states that the exts function distributes with sequencing. It is a corollary of commute-subst-rename as described below. (It would have been nicer to prove this directly by equational reasoning in the Ο algebra, but that would require the sub-assoc equation, whose proof depends on sub-sub, which in turn depends on this lemma.)
exts-seq : β{Ξ Ξ Ξβ²} {Οβ : Subst Ξ Ξ} {Οβ : Subst Ξ Ξβ²} β β {A} β (exts Οβ β¨ exts Οβ) {A} β‘ exts (Οβ β¨ Οβ) exts-seq = extensionality Ξ» x β lemma {x = x} where lemma : β{Ξ Ξ Ξβ²}{A}{x : Ξ , β β A} {Οβ : Subst Ξ Ξ}{Οβ : Subst Ξ Ξβ²} β (exts Οβ β¨ exts Οβ) x β‘ exts (Οβ β¨ Οβ) x lemma {x = Z} = refl lemma {A = β }{x = S x}{Οβ}{Οβ} = begin (exts Οβ β¨ exts Οβ) (S x) β‘β¨β© βͺ exts Οβ β« (rename S_ (Οβ x)) β‘β¨ commute-subst-rename{M = Οβ x}{Ο = Οβ}{Οβ = S_}{Οβ = S_} refl β© rename S_ (βͺ Οβ β« (Οβ x)) β‘β¨β© rename S_ ((Οβ β¨ Οβ) x) β
The proof proceed by cases on x.
If
x = Z, the two sides are equal by the definition ofextsand sequencing.If
x = S x, we unfold the first use ofextsand sequencing, then apply the lemmacommute-subst-rename. We conclude by the definition of sequencing.
Now we come to the proof of sub-sub, which we explain below.
sub-sub : β{Ξ Ξ Ξ£}{A}{M : Ξ β’ A} {Οβ : Subst Ξ Ξ}{Οβ : Subst Ξ Ξ£} β βͺ Οβ β« (βͺ Οβ β« M) β‘ βͺ Οβ β¨ Οβ β« M sub-sub {M = ` x} = refl sub-sub {Ξ}{Ξ}{Ξ£}{A}{Ζ N}{Οβ}{Οβ} = begin βͺ Οβ β« (βͺ Οβ β« (Ζ N)) β‘β¨β© Ζ βͺ exts Οβ β« (βͺ exts Οβ β« N) β‘β¨ cong Ζ_ (sub-sub{M = N}{Οβ = exts Οβ}{Οβ = exts Οβ}) β© Ζ βͺ exts Οβ β¨ exts Οβ β« N β‘β¨ cong Ζ_ (cong-sub{M = N} (Ξ» {A} β exts-seq) refl) β© Ζ βͺ exts ( Οβ β¨ Οβ) β« N β sub-sub {M = L Β· M} = congβ _Β·_ (sub-sub{M = L}) (sub-sub{M = M})
We proceed by induction on the term M.
If
M = x, then both sides are equal toΟβ (Οβ x).If
M = Ζ N, we first use the induction hypothesis to show thatΖ βͺ exts Οβ β« (βͺ exts Οβ β« N) β‘ Ζ βͺ exts Οβ β¨ exts Οβ β« Nand then use the lemma
exts-seqto showΖ βͺ exts Οβ β¨ exts Οβ β« N β‘ Ζ βͺ exts ( Οβ β¨ Οβ) β« NIf
Mis an application, we use the induction hypothesis for both subterms.
The following corollary of sub-sub specializes the first substitution to a renaming.
rename-subst : β{Ξ Ξ Ξβ²}{M : Ξ β’ β }{Ο : Rename Ξ Ξ}{Ο : Subst Ξ Ξβ²} β βͺ Ο β« (rename Ο M) β‘ βͺ Ο β Ο β« M rename-subst {Ξ}{Ξ}{Ξβ²}{M}{Ο}{Ο} = begin βͺ Ο β« (rename Ο M) β‘β¨ cong βͺ Ο β« (rename-subst-ren{M = M}) β© βͺ Ο β« (βͺ ren Ο β« M) β‘β¨ sub-sub{M = M} β© βͺ ren Ο β¨ Ο β« M β‘β¨β© βͺ Ο β Ο β« M β
Proof of sub-assoc
The proof of sub-assoc follows directly from sub-sub and the definition of sequencing.
sub-assoc : β{Ξ Ξ Ξ£ Ξ¨ : Context} {Ο : Subst Ξ Ξ} {Ο : Subst Ξ Ξ£} {ΞΈ : Subst Ξ£ Ξ¨} β β{A} β (Ο β¨ Ο) β¨ ΞΈ β‘ (Ο β¨ Ο β¨ ΞΈ) {A} sub-assoc {Ξ}{Ξ}{Ξ£}{Ξ¨}{Ο}{Ο}{ΞΈ}{A} = extensionality Ξ» x β lemma{x = x} where lemma : β {x : Ξ β A} β ((Ο β¨ Ο) β¨ ΞΈ) x β‘ (Ο β¨ Ο β¨ ΞΈ) x lemma {x} = begin ((Ο β¨ Ο) β¨ ΞΈ) x β‘β¨β© βͺ ΞΈ β« (βͺ Ο β« (Ο x)) β‘β¨ sub-sub{M = Ο x} β© βͺ Ο β¨ ΞΈ β« (Ο x) β‘β¨β© (Ο β¨ Ο β¨ ΞΈ) x β
Proof of subst-zero-exts-cons
The last equation we needed to prove subst-zero-exts-cons was sub-assoc, so we can now go ahead with its proof. We simply apply the equations for exts and subst-zero and then apply the Ο algebra equation to arrive at the normal form M β’ Ο.
subst-zero-exts-cons : β{Ξ Ξ}{Ο : Subst Ξ Ξ}{B}{M : Ξ β’ B}{A} β exts Ο β¨ subst-zero M β‘ (M β’ Ο) {A} subst-zero-exts-cons {Ξ}{Ξ}{Ο}{B}{M}{A} = begin exts Ο β¨ subst-zero M β‘β¨ cong-seq exts-cons-shift subst-Z-cons-ids β© (` Z β’ (Ο β¨ β)) β¨ (M β’ ids) β‘β¨ sub-dist β© (βͺ M β’ ids β« (` Z)) β’ ((Ο β¨ β) β¨ (M β’ ids)) β‘β¨ cong-cons (sub-head{Ο = ids}) refl β© M β’ ((Ο β¨ β) β¨ (M β’ ids)) β‘β¨ cong-cons refl (sub-assoc{Ο = Ο}) β© M β’ (Ο β¨ (β β¨ (M β’ ids))) β‘β¨ cong-cons refl (cong-seq{Ο = Ο} refl (sub-tail{M = M}{Ο = ids})) β© M β’ (Ο β¨ ids) β‘β¨ cong-cons refl (sub-idR{Ο = Ο}) β© M β’ Ο β
Proof of the substitution lemma
We first prove the generalized form of the substitution lemma, showing that a substitution Ο commutes with the substitution of M into N.
βͺ exts Ο β« N [ βͺ Ο β« M ] β‘ βͺ Ο β« (N [ M ])This proof is where the Ο algebra pays off. The proof is by direct equational reasoning. Starting with the left-hand side, we apply Ο algebra equations, oriented left-to-right, until we arrive at the normal form
βͺ βͺ Ο β« M β’ Ο β« NWe then do the same with the right-hand side, arriving at the same normal form.
subst-commute : β{Ξ Ξ}{N : Ξ , β β’ β }{M : Ξ β’ β }{Ο : Subst Ξ Ξ } β βͺ exts Ο β« N [ βͺ Ο β« M ] β‘ βͺ Ο β« (N [ M ]) subst-commute {Ξ}{Ξ}{N}{M}{Ο} = begin βͺ exts Ο β« N [ βͺ Ο β« M ] β‘β¨β© βͺ subst-zero (βͺ Ο β« M) β« (βͺ exts Ο β« N) β‘β¨ cong-sub {M = βͺ exts Ο β« N} subst-Z-cons-ids refl β© βͺ βͺ Ο β« M β’ ids β« (βͺ exts Ο β« N) β‘β¨ sub-sub {M = N} β© βͺ (exts Ο) β¨ ((βͺ Ο β« M) β’ ids) β« N β‘β¨ cong-sub {M = N} (cong-seq exts-cons-shift refl) refl β© βͺ (` Z β’ (Ο β¨ β)) β¨ (βͺ Ο β« M β’ ids) β« N β‘β¨ cong-sub {M = N} (sub-dist {M = ` Z}) refl β© βͺ βͺ βͺ Ο β« M β’ ids β« (` Z) β’ ((Ο β¨ β) β¨ (βͺ Ο β« M β’ ids)) β« N β‘β¨β© βͺ βͺ Ο β« M β’ ((Ο β¨ β) β¨ (βͺ Ο β« M β’ ids)) β« N β‘β¨ cong-sub{M = N} (cong-cons refl (sub-assoc{Ο = Ο})) refl β© βͺ βͺ Ο β« M β’ (Ο β¨ β β¨ βͺ Ο β« M β’ ids) β« N β‘β¨ cong-sub{M = N} refl refl β© βͺ βͺ Ο β« M β’ (Ο β¨ ids) β« N β‘β¨ cong-sub{M = N} (cong-cons refl (sub-idR{Ο = Ο})) refl β© βͺ βͺ Ο β« M β’ Ο β« N β‘β¨ cong-sub{M = N} (cong-cons refl (sub-idL{Ο = Ο})) refl β© βͺ βͺ Ο β« M β’ (ids β¨ Ο) β« N β‘β¨ cong-sub{M = N} (sym sub-dist) refl β© βͺ M β’ ids β¨ Ο β« N β‘β¨ sym (sub-sub{M = N}) β© βͺ Ο β« (βͺ M β’ ids β« N) β‘β¨ cong βͺ Ο β« (sym (cong-sub{M = N} subst-Z-cons-ids refl)) β© βͺ Ο β« (N [ M ]) β
A corollary of subst-commute is that rename also commutes with substitution. In the proof below, we first exchange rename Ο for the substitution βͺ ren Ο β«, and apply subst-commute, and then convert back to rename Ο.
rename-subst-commute : β{Ξ Ξ}{N : Ξ , β β’ β }{M : Ξ β’ β }{Ο : Rename Ξ Ξ } β (rename (ext Ο) N) [ rename Ο M ] β‘ rename Ο (N [ M ]) rename-subst-commute {Ξ}{Ξ}{N}{M}{Ο} = begin (rename (ext Ο) N) [ rename Ο M ] β‘β¨ cong-sub (cong-sub-zero (rename-subst-ren{M = M})) (rename-subst-ren{M = N}) β© (βͺ ren (ext Ο) β« N) [ βͺ ren Ο β« M ] β‘β¨ cong-sub refl (cong-sub{M = N} ren-ext refl) β© (βͺ exts (ren Ο) β« N) [ βͺ ren Ο β« M ] β‘β¨ subst-commute{N = N} β© subst (ren Ο) (N [ M ]) β‘β¨ sym (rename-subst-ren) β© rename Ο (N [ M ]) β
To present the substitution lemma, we introduce the following notation for substituting a term M for index 1 within term N.
_γ_γ : β {Ξ A B C} β Ξ , B , C β’ A β Ξ β’ B --------- β Ξ , C β’ A _γ_γ {Ξ} {A} {B} {C} N M = subst {Ξ , B , C} {Ξ , C} (exts (subst-zero M)) {A} N
The substitution lemma is stated as follows and proved as a corollary of the subst-commute lemma.
substitution : β{Ξ}{M : Ξ , β , β β’ β }{N : Ξ , β β’ β }{L : Ξ β’ β } β (M [ N ]) [ L ] β‘ (M γ L γ) [ (N [ L ]) ] substitution{M = M}{N = N}{L = L} = sym (subst-commute{N = M}{M = N}{Ο = subst-zero L})
Notes
Most of the properties and proofs in this file are based on the paper Autosubst: Reasoning with de Bruijn Terms and Parallel Substitution by Schafer, Tebbi, and Smolka (ITP 2015). That paper, in turn, is based on the paper of Abadi, Cardelli, Curien, and Levy (1991) that defines the Ο algebra.
Unicode
This chapter uses the following unicode:
βͺ U+27EA MATHEMATICAL LEFT DOUBLE ANGLE BRACKET (\<<)
β« U+27EA MATHEMATICAL RIGHT DOUBLE ANGLE BRACKET (\>>)
β U+2191 UPWARDS ARROW (\u)
β’ U+2022 BULLET (\bub)
β¨ U+2A1F Z NOTATION SCHEMA COMPOSITION (C-x 8 RET Z NOTATION SCHEMA COMPOSITION)
γ U+3014 LEFT TORTOISE SHELL BRACKET (\( option 9 on page 2)
γ U+3015 RIGHT TORTOISE SHELL BRACKET (\) option 9 on page 2)