Skip to content

Commit c13272c

Browse files
committed
add hetero subst laws
1 parent 74a3238 commit c13272c

3 files changed

Lines changed: 130 additions & 15 deletions

File tree

Examples/LambdaCalc.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ namespace LeanSubst.Examples.LambdaCalc
5252

5353
@[simp]
5454
theorem subst_var {x} {σ : Subst Term} : (Term.var x)[σ] = σ x := by
55-
unfold Subst.apply; simp [SubstMap.smap]
55+
simp [Subst.apply, SubstMap.smap]
5656

5757
@[simp]
5858
theorem subst_app {t1 t2} {σ : Subst Term} : (t1 :@ t2)[σ] = t1[σ] :@ t2[σ] := by
59-
unfold Subst.apply; simp [SubstMap.smap]
59+
simp [Subst.apply, SubstMap.smap]
6060

6161
@[simp]
6262
theorem subst_lam {t} {σ : Subst Term} : (:λ t)[σ] = :λ t[σ.lift] := by
63-
unfold Subst.apply; simp [SubstMap.smap]
63+
simp [Subst.apply, SubstMap.smap]
6464

6565
@[simp]
6666
theorem Term.from_action_compose {x} {σ τ : Subst Term}
@@ -81,7 +81,7 @@ namespace LeanSubst.Examples.LambdaCalc
8181
: r = σ -> Ren.apply (T := Term) r = Subst.apply σ
8282
:= by solve_stable Term, r, σ
8383

84-
instance : SubstMapStable Term Term where
84+
instance : SubstMapStable Term where
8585
apply_stable := apply_stable
8686

8787
theorem apply_compose {s : Term} {σ τ : Subst Term} : s[σ][τ] = s[σ ∘ τ] := by

LeanSubst/Basic.lean

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ namespace LeanSubst
6060

6161
def Subst.apply [RenMap T] [SubstMap S T] (σ : Subst T) : S -> S := SubstMap.smap Subst.lift σ
6262

63+
abbrev Subst.apply1 [RenMap S] [SubstMap S S] := Subst.apply (S := S) (T := S)
64+
6365
def Subst.compose [RenMap T] [SubstMap T T] : Subst T -> Subst T -> Subst T
6466
| σ, τ, n =>
6567
match σ n with
@@ -116,18 +118,20 @@ namespace LeanSubst
116118
funext; case _ x =>
117119
cases x <;> simp [Ren.to, Subst.compose]
118120

119-
abbrev the {T : Type} (_ : T) := T
120-
121-
macro:max t:term noWs "[" σ:term "]" : term => `(Subst.apply (S := the $t) (T := the $t) $σ $t)
121+
macro:max t:term noWs "[" σ:term "]" : term => `(Subst.apply1 $σ $t)
122122
macro:max t:term noWs "[" σ:term ":" T:term "]" : term => `(Subst.apply (T := $T) $σ $t)
123123
infixr:85 " ∘ " => Subst.compose
124-
infixr:85 " • " => Subst.hcompose
124+
infixr:85 " ◾ " => Subst.hcompose
125+
126+
@[app_unexpander Subst.apply1]
127+
def unexpandSubstApply1 : Lean.PrettyPrinter.Unexpander
128+
| `($_ $σ $t) => `($t[$σ])
129+
| _ => throw ()
125130

126131
@[app_unexpander Subst.apply]
127132
def unexpandSubstApply : Lean.PrettyPrinter.Unexpander
128-
| `($_ $σ $t) => `($t[$σ])
133+
| `($_ $σ $t) => `($t[$σ : _])
129134
| `($_ (T := $T) $σ $t) => `($t[$σ : $T])
130-
| `($_ (S := the $_) (T := the $_) $σ $t) => `($t[$σ])
131135
| _ => throw ()
132136

133137
end LeanSubst

LeanSubst/Laws.lean

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,115 @@ namespace LeanSubst
128128
simp [Subst.compose]
129129
cases σ x <;> simp
130130

131+
@[simp]
132+
theorem hrewrite1
133+
[RenMap T] [SubstMap S T] [SubstMapId S T]
134+
{σ : Subst S}
135+
: σ ◾ (+0@T) = σ
136+
:= by
137+
funext; case _ x =>
138+
simp [Subst.hcompose]
139+
generalize zdef : σ x = z
140+
cases z <;> simp
141+
142+
theorem hcomp_ren_left
143+
[RenMap S] [RenMap T] [SubstMap S T]
144+
(r : Ren) (σ : Subst T)
145+
: (@Ren.to S r) ◾ σ = r.to
146+
:= by
147+
funext; case _ x =>
148+
induction x <;> simp [Subst.hcompose, Ren.to]
149+
150+
@[simp]
151+
theorem hrewrite2
152+
[RenMap S] [RenMap T] [SubstMap S T]
153+
{σ : Subst T}
154+
: (+0@S) ◾ σ = +0
155+
:= by
156+
have lem := hcomp_ren_left (S := S) (T := T) (λ x => x) σ
157+
simp at lem; exact lem
158+
159+
@[simp]
160+
theorem hrewrite3
161+
[RenMap S] [RenMap T] [SubstMap S T]
162+
{σ : Subst T}
163+
: (+1@S) ◾ σ = +1
164+
:= by
165+
have lem := hcomp_ren_left (S := S) (T := T) (· + 1) σ
166+
simp at lem; exact lem
167+
168+
@[simp]
169+
theorem hrewrite4
170+
[RenMap T] [SubstMap S T]
171+
{x} {σ : Subst S} {τ : Subst T}
172+
: (re x :: σ) ◾ τ = re x :: (σ ◾ τ)
173+
:= by
174+
funext; case _ i =>
175+
cases i <;> simp [Subst.hcompose]
176+
177+
theorem hcomp_dist_ren_left
178+
[RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T]
179+
(r : Ren) {σ : Subst S} {τ : Subst T}
180+
: (r.to ∘ σ) ◾ τ = r.to ∘ σ ◾ τ
181+
:= by
182+
funext; case _ x =>
183+
simp [Subst.hcompose, Subst.compose, Ren.to]
184+
185+
class SubstMapRenCommute (S T : Type) [RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T] where
186+
apply_ren_commute {s : S} (r : Ren) (τ : Subst T) : s[r.to][τ:T] = s[τ:T][r.to]
187+
188+
@[simp]
189+
theorem hrewrite5
190+
[RenMap S] [RenMap T] [SubstMap T T] [SubstMap S T] [SubstMapCompose S T]
191+
{σ : Subst S} {τ μ : Subst T}
192+
: (σ ◾ τ) ◾ μ = σ ◾ (τ ∘ μ)
193+
:= by
194+
funext; case _ x =>
195+
simp [Subst.hcompose]
196+
generalize zdef : σ x = z
197+
cases z <;> simp
198+
199+
theorem hcomp_distr_ren_right
200+
[RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T] [SubstMapRenCommute S T]
201+
(r : Ren) (σ : Subst S) (μ : Subst T)
202+
: (σ ∘ r.to) ◾ μ = (σ ◾ μ) ∘ r.to
203+
:= by
204+
funext; case _ x =>
205+
simp [Subst.hcompose, Subst.compose, Ren.to]
206+
generalize zdef : σ x = z
207+
cases z <;> simp
208+
rw [SubstMapRenCommute.apply_ren_commute r μ]
209+
210+
@[simp]
211+
theorem hrewrite6
212+
[RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T] [SubstMapRenCommute S T]
213+
(r : Ren) (σ : Subst S)
214+
: (σ ∘ r.to) ◾ (+1@T) = (σ ◾ +1@T) ∘ r.to
215+
:= by
216+
have lem := hcomp_distr_ren_right (T := T) r σ +1
217+
simp at lem; exact lem
218+
219+
class SubstMapHetCompose (S T : Type) [RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T] where
220+
apply_hcompose {s : S} {σ : Subst S} {τ : Subst T} : s[σ][τ:T] = s[τ:T][σ ◾ τ]
221+
222+
@[simp]
223+
theorem apply_hcompose
224+
[RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T] [SubstMapHetCompose S T]
225+
{s : S} {σ : Subst S} {τ : Subst T}
226+
: s[σ][τ:T] = s[τ:T][σ ◾ τ]
227+
:= by exact SubstMapHetCompose.apply_hcompose
228+
229+
@[simp]
230+
theorem hrewrite7
231+
[RenMap S] [RenMap T] [SubstMap S S] [SubstMap S T] [SubstMapHetCompose S T]
232+
{σ τ : Subst S} (μ : Subst T)
233+
: (σ ∘ τ) ◾ μ = (σ ◾ μ) ∘ τ ◾ μ
234+
:= by
235+
funext; case _ x =>
236+
simp [Subst.hcompose, Subst.compose]
237+
generalize zdef : σ x = z
238+
cases z <;> simp
239+
131240
end Subst
132241

133242
macro "solve_stable" S:term "," r:term "," σ:term : tactic => `(tactic| {
@@ -136,7 +245,7 @@ namespace LeanSubst
136245
induction t generalizing $r $σ
137246
all_goals simp [RenMap.rmap, SubstMap.smap, *] at *; try simp [*]
138247
any_goals solve | (
139-
rw [<-Ren.lift_eq_from_eq (S := $S) h])
248+
rw [<-Ren.lift_eq_from_eq (S := $S) (r := $r) h])
140249
any_goals solve | (rw [<-h]; simp [Ren.to])
141250
})
142251

@@ -152,13 +261,14 @@ namespace LeanSubst
152261
cases x <;> simp
153262
case _ x => simp [Ren.to, Subst.succ, Subst.compose]
154263
have lem2s {σ : Subst $Ty} : (+1 ∘ σ).lift = +1.lift ∘ σ.lift := by rw [<-Ren.to_succ, lem2]
155-
have lem3 {σ : Subst $Ty} {r : Ren} {t} : t[r][σ] = t[r ∘ σ] := by
264+
have lem3 {σ : Subst $Ty} {r : Ren} {t : $Ty} : t[r:$Ty][σ:_] = t[(r ∘ σ):_] := by
156265
induction t generalizing σ r
157266
any_goals solve | simp [*]
158267
any_goals solve | (
159268
simp [-Subst.rewrite_lift, *]
160269
rw [<-Ren.to_lift (S := $Ty)]; simp [*])
161-
have lem3s {σ : Subst $Ty} {t} : t[+1][σ] = t[+1 ∘ σ] := by rw [<-Ren.to_succ, lem3]
270+
have lem3s {σ : Subst $Ty} {t : $Ty} : t[+1:$Ty][σ:_] = t[+1 ∘ σ:_] := by
271+
rw [<-Ren.to_succ, lem3]
162272
have lem4 {σ τ : Subst $Ty} : +1 ∘ τ ∘ σ = (+1 ∘ τ) ∘ σ := by
163273
funext; case _ x =>
164274
cases x <;> simp [Subst.compose, Subst.succ]
@@ -174,13 +284,14 @@ namespace LeanSubst
174284
simp [Subst.compose]
175285
cases τ x <;> simp [*]
176286
have lem6s {τ : Subst $Ty} : (τ ∘ +1).lift = τ.lift ∘ +1.lift := by rw [<-Ren.to_succ, lem6]
177-
have lem7 {τ : Subst $Ty} {t} {r : Ren} : t[τ][r] = t[τ ∘ r.to] := by
287+
have lem7 {τ : Subst $Ty} {t:$Ty} {r : Ren} : t[τ:_][r:$Ty] = t[τ ∘ r.to:_] := by
178288
induction t generalizing τ r
179289
any_goals solve | simp [*]
180290
any_goals solve | (
181291
simp [-Subst.rewrite_lift, *]
182292
rw [<-Ren.to_lift (S := $Ty)]; simp [*])
183-
have lem7s {τ : Subst $Ty} {t} : t[τ][+1] = t[τ ∘ +1] := by rw [<-Ren.to_succ, lem7]
293+
have lem7s {τ : Subst $Ty} {t : $Ty} : t[τ:_][+1:$Ty] = t[τ ∘ +1:_] := by
294+
rw [<-Ren.to_succ, lem7]
184295
have lem8 {σ τ : Subst $Ty} : (σ ∘ +1) ∘ τ = σ ∘ +1 ∘ τ := by
185296
funext; case _ x =>
186297
unfold Subst.compose; simp

0 commit comments

Comments
 (0)