Soundness.lean
2044 lines. Every name is a link to its definition, hover shows its type and doc comment. The file on GitHub. A name that is a local variable, or a field of a value whose type is not named, stays plain.
/-
Satex.Soundness: the abstract run of Satex.Abstract over-approximates every
concrete run of Satex.Concrete, and it terminates.
Written with AI assistance from Claude Code (Claude Opus 5.5; build fixes with
Claude Sonnet 5.5), directed and reviewed by the author.
Intuition. Soundness is a simulation: from a covered state, every concrete
step has an abstract run that covers its result. Termination is a budget:
every abstract step charges it, and at zero the run is cut to a state that
covers everything. So precision may be lost but never soundness, and the
run always ends (satex_sound_and_terminating).
Part 1 is domain-independent: orders, the loop with k passes and a summary,
simulation, the budget. Part 2 instantiates it with the domains and
transfers of Satex.Abstract against Satex.Concrete's Step: one lemma
per transfer, then the run theorem by family (section 8). What is still a
hypothesis: semantics/PROOF-STATUS.md. Plain core Lean 4, no mathlib.
-/
import Satex.Core
import Satex.Concrete
import Satex.Abstract
namespace Satex.Soundness
/-! ## Traces -/
/-- Reflexive transitive closure (core Lean has none). -/
inductive Star {Ξ± : Type} (r : Ξ± β Ξ± β Prop) (a : Ξ±) : Ξ± β Prop where
| refl : Star r a a
| tail {b c : Ξ±} : Star r a b β r b c β Star r a c
theorem Star.single {Ξ± : Type} {r : Ξ± β Ξ± β Prop} {a b : Ξ±} (h : r a b) : Star r a b :=
Star.tail Star.refl h
theorem Star.trans {Ξ± : Type} {r : Ξ± β Ξ± β Prop} {a b c : Ξ±}
(hβ : Star r a b) (hβ : Star r b c) : Star r a c := by
induction hβ with
| refl => exact hβ
| tail _ hcd ih => exact Star.tail ih hcd
/-! ## Abstract domains, generically -/
/-- What every abstract domain must provide: a preorder with a join and a
top, and a monotone concretization Ξ³. -/
structure AbsDom (C A : Type) where
le : A β A β Prop
join : A β A β A
top : A
Ξ³ : A β C β Prop
le_refl : β a, le a a
le_trans : β a b c, le a b β le b c β le a c
join_l : β a b, le a (join a b)
join_r : β a b, le b (join a b)
le_top : β a, le a top
Ξ³_mono : β a b, le a b β β c, Ξ³ a c β Ξ³ b c
namespace AbsDom
variable {C A : Type} (D : AbsDom C A)
/-- The join over-approximates the union. -/
theorem Ξ³_join (a b : A) (c : C) (h : D.Ξ³ a c β¨ D.Ξ³ b c) : D.Ξ³ (D.join a b) c := by
cases h with
| inl h => exact D.Ξ³_mono _ _ (D.join_l a b) c h
| inr h => exact D.Ξ³_mono _ _ (D.join_r a b) c h
theorem Ξ³_top (hfull : β a, β c, D.Ξ³ a c) (c : C) : D.Ξ³ D.top c := by
obtain β¨a, haβ© := hfull
exact D.Ξ³_mono _ _ (D.le_top a) c (ha c)
/-- The join is the least upper bound when le says so. -/
theorem join_mono (a a' b b' : A)
(hlub : β x y z, D.le x z β D.le y z β D.le (D.join x y) z)
(ha : D.le a a') (hb : D.le b b') : D.le (D.join a b) (D.join a' b') :=
hlub _ _ _ (D.le_trans _ _ _ ha (D.join_l a' b')) (D.le_trans _ _ _ hb (D.join_r a' b'))
/-! ## Transfer functions -/
/-- Local soundness of an abstract transfer f for a concrete step relation. -/
def Sound (f : A β A) (Step : C β C β Prop) : Prop :=
β a c c', D.Ξ³ a c β Step c c' β D.Ξ³ (f a) c'
/-- An undecided conditional: each arm runs, the results join at \fi. -/
theorem sound_fork (ft ff : A β A) (St Sf : C β C β Prop)
(ht : D.Sound ft St) (hf : D.Sound ff Sf) :
D.Sound (fun a => D.join (ft a) (ff a)) (fun c c' => St c c' β¨ Sf c c') := by
intro a c c' hc hs
cases hs with
| inl h => exact D.Ξ³_join _ _ _ (Or.inl (ht a c c' hc h))
| inr h => exact D.Ξ³_join _ _ _ (Or.inr (hf a c c' hc h))
/-- A decided conditional follows one arm exactly. -/
theorem sound_decided (ft ff : A β A) (St Sf : C β C β Prop) (b : Bool)
(ht : D.Sound ft St) (hf : D.Sound ff Sf) :
D.Sound (if b then ft else ff) (if b then St else Sf) := by
cases b
Β· exact hf
Β· exact ht
/-- Sequencing. -/
theorem sound_comp (f g : A β A) (S T : C β C β Prop)
(hf : D.Sound f S) (hg : D.Sound g T) :
D.Sound (fun a => g (f a)) (fun c c'' => β c', S c c' β§ T c' c'') := by
intro a c c'' hc hs
obtain β¨c', h1, h2β© := hs
exact hg _ _ _ (hf a c c' hc h1) h2
/-- Moving up the lattice keeps a transfer sound (widening, Unknown). -/
theorem sound_weaken (f g : A β A) (S : C β C β Prop)
(hf : D.Sound f S) (hle : β a, D.le (f a) (g a)) : D.Sound g S := by
intro a c c' hc hs
exact D.Ξ³_mono _ _ (hle a) c' (hf a c c' hc hs)
/-- The summary top is sound for any step. -/
theorem sound_top (S : C β C β Prop) (hfull : β a, β c, D.Ξ³ a c) :
D.Sound (fun _ => D.top) S := by
intro _ _ c' _ _
exact D.Ξ³_top hfull c'
/-! ## Loops: k joining passes, then one summary -/
/-- k passes that join each round into the head's state; a round that adds
nothing is the fixpoint. After k growing rounds the summary is taken. -/
def loopAbs (f : A β A) (stable : A β A β Bool) (summary : A β A) : Nat β A β A
| 0, a => summary a
| k + 1, a =>
if stable (D.join a (f a)) a then a
else loopAbs f stable summary k (D.join a (f a))
/-- The loop result covers its entry and is a post-fixpoint of the body. -/
theorem loopAbs_post (f : A β A) (stable : A β A β Bool) (summary : A β A)
(hstable : β x y, stable x y = true β D.le x y)
(hsum_ext : β a, D.le a (summary a))
(hsum_post : β a, D.le (f (summary a)) (summary a)) :
β k a, D.le a (D.loopAbs f stable summary k a) β§
D.le (f (D.loopAbs f stable summary k a)) (D.loopAbs f stable summary k a) := by
intro k
induction k with
| zero => intro a; exact β¨hsum_ext a, hsum_post aβ©
| succ k ih =>
intro a
simp only [loopAbs]
by_cases h : stable (D.join a (f a)) a = true
Β· simp only [h, βreduceIte]
exact β¨D.le_refl a, D.le_trans _ _ _ (D.join_r a (f a)) (hstable _ _ h)β©
Β· simp only [h, Bool.false_eq_true, βreduceIte]
obtain β¨h1, h2β© := ih (D.join a (f a))
exact β¨D.le_trans _ _ _ (D.join_l a (f a)) h1, h2β©
/-- Every number of concrete passes from a state in Ξ³ a ends in the
Ξ³ of the loop's result. -/
theorem loopAbs_sound (f : A β A) (stable : A β A β Bool) (summary : A β A)
(Body : C β C β Prop) (hf : D.Sound f Body)
(hstable : β x y, stable x y = true β D.le x y)
(hsum_ext : β a, D.le a (summary a))
(hsum_post : β a, D.le (f (summary a)) (summary a))
(k : Nat) (a : A) (c c' : C) (hc : D.Ξ³ a c) (hrun : Star Body c c') :
D.Ξ³ (D.loopAbs f stable summary k a) c' := by
have hp := D.loopAbs_post f stable summary hstable hsum_ext hsum_post k a
induction hrun with
| refl => exact D.Ξ³_mono _ _ hp.1 _ hc
| tail _ hbc ih =>
exact D.Ξ³_mono _ _ hp.2 _ (hf _ _ _ ih hbc)
/-- The summary top meets the summary conditions. -/
theorem top_summary (f : A β A) :
(β a : A, D.le a ((fun _ : A => D.top) a)) β§
(β a : A, D.le (f ((fun _ : A => D.top) a)) ((fun _ : A => D.top) a)) :=
β¨fun a => D.le_top a, fun _ => D.le_top (f D.top)β©
/-- The loop runs at most k passes plus one summary. -/
def loopPasses (f : A β A) (stable : A β A β Bool) : Nat β A β Nat
| 0, _ => 1
| k + 1, a =>
if stable (D.join a (f a)) a then 1
else 1 + loopPasses f stable k (D.join a (f a))
theorem loopPasses_le (f : A β A) (stable : A β A β Bool) :
β k a, D.loopPasses f stable k a β€ k + 1 := by
intro k
induction k with
| zero => intro a; exact Nat.le_refl 1
| succ k ih =>
intro a
simp only [loopPasses]
by_cases h : stable (D.join a (f a)) a = true
Β· simp only [h, βreduceIte]; omega
Β· simp only [h, Bool.false_eq_true, βreduceIte]; have := ih (D.join a (f a)); omega
end AbsDom
/-! ## Simulation: the abstract run covers every concrete run -/
section Sim
variable {C A : Type}
/-- If every concrete step from a covered state has a covering abstract step,
every concrete trace has a covering abstract trace. -/
theorem simulation (Step : C β C β Prop) (AStep : A β A β Prop) (Ξ³ : A β C β Prop)
(hloc : β a c c', Ξ³ a c β Step c c' β β a', AStep a a' β§ Ξ³ a' c') :
β cβ c, Star Step cβ c β β aβ, Ξ³ aβ cβ β β a, Star AStep aβ a β§ Ξ³ a c := by
intro cβ c h
induction h with
| refl => intro aβ hβ; exact β¨aβ, Star.refl, hββ©
| tail _ hbc ih =>
intro aβ hβ
obtain β¨a, ha, hΞ³β© := ih aβ hβ
obtain β¨a', h1, h2β© := hloc _ _ _ hΞ³ hbc
exact β¨a', Star.tail ha h1, h2β©
/-- Simulation where one concrete step is covered by several abstract steps
(a primitive that reads a whole argument, a join at \fi). -/
theorem simulation_star (Step : C β C β Prop) (AStep : A β A β Prop) (Ξ³ : A β C β Prop)
(hloc : β a c c', Ξ³ a c β Step c c' β β a', Star AStep a a' β§ Ξ³ a' c') :
β cβ c, Star Step cβ c β β aβ, Ξ³ aβ cβ β β a, Star AStep aβ a β§ Ξ³ a c := by
intro cβ c h
induction h with
| refl => intro aβ hβ; exact β¨aβ, Star.refl, hββ©
| tail _ hbc ih =>
intro aβ hβ
obtain β¨a, ha, hΞ³β© := ih aβ hβ
obtain β¨a', h1, h2β© := hloc _ _ _ hΞ³ hbc
exact β¨a', Star.trans ha h1, h2β©
/-! ## Termination: the never-hang guarantee -/
/-- Every abstract step charges the budget: no infinite abstract run. -/
theorem acc_of_budget (AStep : A β A β Prop) (ΞΌ : A β Nat)
(hdec : β a b, AStep a b β ΞΌ b < ΞΌ a) :
β n a, ΞΌ a < n β Acc (fun b a => AStep a b) a := by
intro n
induction n with
| zero => intro a ha; exact absurd ha (Nat.not_lt_zero _)
| succ n ih =>
intro a ha
constructor
intro b hb
have := hdec a b hb
exact ih b (by omega)
theorem wf_of_budget (AStep : A β A β Prop) (ΞΌ : A β Nat)
(hdec : β a b, AStep a b β ΞΌ b < ΞΌ a) :
WellFounded (fun b a => AStep a b) :=
β¨fun a => acc_of_budget AStep ΞΌ hdec (ΞΌ a + 1) a (Nat.lt_succ_self _)β©
/-- Runs of exactly n steps. -/
inductive StarN {Ξ± : Type} (r : Ξ± β Ξ± β Prop) (a : Ξ±) : Nat β Ξ± β Prop where
| refl : StarN r a 0 a
| tail {n : Nat} {b c : Ξ±} : StarN r a n b β r b c β StarN r a (n + 1) c
/-- An abstract run is never longer than its starting budget. -/
theorem run_length_le_budget (AStep : A β A β Prop) (ΞΌ : A β Nat)
(hdec : β a b, AStep a b β ΞΌ b < ΞΌ a) :
β n a b, StarN AStep a n b β n + ΞΌ b β€ ΞΌ a := by
intro n a b h
induction h with
| refl => omega
| tail _ hbc ih => have := hdec _ _ hbc; omega
end Sim
/-! # The domains and transfers of Satex.Abstract -/
open Satex Satex.Abstract
/-! ## Helpers -/
theorem ite_prop {p : Prop} [Decidable p] {Ξ± : Type} {P : Ξ± β Prop} {x y : Ξ±}
(hx : p β P x) (hy : Β¬p β P y) : P (if p then x else y) := by
by_cases h : p
Β· simp only [h, βreduceIte]; exact hx h
Β· simp only [h, βreduceIte]; exact hy h
theorem upd_same {Ξ± Ξ² : Type} [DecidableEq Ξ±] (f : Ξ± β Ξ²) (a : Ξ±) (b : Ξ²) :
upd f a b a = b := by
simp [upd]
theorem upd_other {Ξ± Ξ² : Type} [DecidableEq Ξ±] {f : Ξ± β Ξ²} {a x : Ξ±} {b : Ξ²}
(h : x β a) : upd f a b x = f x := by
simp [upd, h]
/-- A bounded join folded over a list lies above every member. -/
theorem foldl_cover {Ξ± : Type} (le : Ξ± β Ξ± β Prop) (j : Ξ± β Ξ± β Ξ±)
(hrefl : β x, le x x) (htrans : β x y z, le x y β le y z β le x z)
(hl : β x y, le x (j x y)) (hr : β x y, le y (j x y)) :
β (t : List Ξ±) (acc : Ξ±), le acc (t.foldl j acc) β§ β x, x β t β le x (t.foldl j acc) := by
intro t
induction t with
| nil => intro acc; exact β¨hrefl acc, fun x hx => by simp at hxβ©
| cons y t ih =>
intro acc
obtain β¨h1, h2β© := ih (j acc y)
refine β¨htrans _ _ _ (hl acc y) h1, ?_β©
intro x hx
rcases List.mem_cons.mp hx with e | e
Β· rw [e]; exact htrans _ _ _ (hr acc y) h1
Β· exact h2 x e
/-! ## 1. Intervals -/
theorem itv_le_refl (a : Itv) : Itv.le a a := by
unfold Itv.le; exact β¨Int.le_refl _, Int.le_refl _β©
theorem itv_le_trans (a b c : Itv) (h1 : Itv.le a b) (h2 : Itv.le b c) : Itv.le a c := by
unfold Itv.le at *; constructor <;> omega
/-- The join is the least upper bound. -/
theorem itv_join_lub (a b c : Itv) (ha : Itv.le a c) (hb : Itv.le b c) :
Itv.le (Itv.join a b) c := by
simp only [Itv.le, Itv.join] at *
constructor <;> split <;> omega
/-- The join over-approximates the union. -/
theorem itv_Ξ³_join (a b : Itv) (n : Int) (h : a.Ξ³ n β¨ b.Ξ³ n) : (Itv.join a b).Ξ³ n := by
cases h with
| inl h => exact Itv.Ξ³_mono (Itv.le_join_left a b) h
| inr h => exact Itv.Ξ³_mono (Itv.le_join_right a b) h
/-- Widening is an upper bound of both sides inside the 32-bit word. -/
theorem itv_Ξ³_widen (a b : Itv) (ha : a.inWord) (hb : b.inWord) (n : Int)
(h : a.Ξ³ n β¨ b.Ξ³ n) : (Itv.widen a b).Ξ³ n := by
obtain β¨h1, h2β© := Itv.le_widen a b ha hb
cases h with
| inl h => exact Itv.Ξ³_mono h1 h
| inr h => exact Itv.Ξ³_mono h2 h
/-- How many bounds can still move. -/
def itvRank (i : Itv) : Nat :=
(if i.lo = Itv.wordMin then 0 else 1) + (if i.hi = Itv.wordMax then 0 else 1)
theorem itvRank_le (i : Itv) : itvRank i β€ 2 := by
unfold itvRank; split <;> split <;> omega
/-- One widening step: a change costs a moving bound. -/
theorem widen_rank_step (a b : Itv) :
(if (Itv.widen a b).lo = a.lo β§ (Itv.widen a b).hi = a.hi then 0 else 1) +
itvRank (Itv.widen a b) β€ itvRank a := by
simp only [itvRank, Itv.widen, Itv.wlo, Itv.whi, Itv.wordMin, Itv.wordMax]
by_cases h1 : b.lo < a.lo <;> by_cases h2 : a.hi < b.hi <;>
by_cases h3 : a.lo = -2147483648 <;> by_cases h4 : a.hi = 2147483647 <;>
simp only [h1, h2, h3, h4, βreduceIte, and_true, true_and, and_self, ite_self] <;>
(repeat' split) <;> omega
/-- Steps of a chain that changed its interval. -/
def changes (f : Nat β Itv) : Nat β Nat
| 0 => 0
| n + 1 => changes f n +
(if (f (n + 1)).lo = (f n).lo β§ (f (n + 1)).hi = (f n).hi then 0 else 1)
/-- Termination of widening: along any chain fβββ = widen fβ gβ the
interval changes at most twice, so a loop head that widens is stable
after at most two growing rounds. -/
theorem widen_chain (f g : Nat β Itv) (hf : β n, f (n + 1) = Itv.widen (f n) (g n)) :
β n, changes f n + itvRank (f n) β€ itvRank (f 0) := by
intro n
induction n with
| zero => simp only [changes] <;> omega
| succ n ih =>
have h := widen_rank_step (f n) (g n)
rw [β hf n] at h
simp only [changes] <;> omega
theorem widen_chain_changes_le_two (f g : Nat β Itv)
(hf : β n, f (n + 1) = Itv.widen (f n) (g n)) (n : Nat) : changes f n β€ 2 := by
have h1 := widen_chain f g hf n
have h2 := itvRank_le (f 0)
omega
/-! ## 2. Glue hulls -/
abbrev ghLe (a b : GlueHull) : Prop :=
Itv.le a.width b.width β§ Itv.le a.stretch b.stretch β§ Itv.le a.stretchOrder b.stretchOrder β§
Itv.le a.shrink b.shrink β§ Itv.le a.shrinkOrder b.shrinkOrder
theorem ghLe_refl (a : GlueHull) : ghLe a a :=
β¨itv_le_refl _, itv_le_refl _, itv_le_refl _, itv_le_refl _, itv_le_refl _β©
theorem ghLe_trans (a b c : GlueHull) (h1 : ghLe a b) (h2 : ghLe b c) : ghLe a c :=
β¨itv_le_trans _ _ _ h1.1 h2.1, itv_le_trans _ _ _ h1.2.1 h2.2.1,
itv_le_trans _ _ _ h1.2.2.1 h2.2.2.1, itv_le_trans _ _ _ h1.2.2.2.1 h2.2.2.2.1,
itv_le_trans _ _ _ h1.2.2.2.2 h2.2.2.2.2β©
theorem gh_join_l (a b : GlueHull) : ghLe a (GlueHull.join a b) :=
β¨Itv.le_join_left _ _, Itv.le_join_left _ _, Itv.le_join_left _ _,
Itv.le_join_left _ _, Itv.le_join_left _ _β©
theorem gh_join_r (a b : GlueHull) : ghLe b (GlueHull.join a b) :=
β¨Itv.le_join_right _ _, Itv.le_join_right _ _, Itv.le_join_right _ _,
Itv.le_join_right _ _, Itv.le_join_right _ _β©
theorem gh_Ξ³_mono {a b : GlueHull} (h : ghLe a b) {g : Glue} (hg : a.Ξ³ g) : b.Ξ³ g := by
obtain β¨g1, g2, g3, g4, g5β© := hg
unfold GlueHull.Ξ³
exact β¨Itv.Ξ³_mono h.1 g1, Itv.Ξ³_mono h.2.1 g2, Itv.Ξ³_mono h.2.2.1 g3,
Itv.Ξ³_mono h.2.2.2.1 g4, Itv.Ξ³_mono h.2.2.2.2 g5β©
theorem hullItv_cover (l : List Itv) (i : Itv) (h : i β l) :
β j, hullItv l = some j β§ Itv.le i j := by
cases l with
| nil => simp at h
| cons y t =>
have hc := foldl_cover Itv.le Itv.join itv_le_refl itv_le_trans
Itv.le_join_left Itv.le_join_right t y
refine β¨t.foldl Itv.join y, rfl, ?_β©
rcases List.mem_cons.mp h with e | e
Β· subst e; exact hc.1
Β· exact hc.2 i e
theorem hullGlue_cover (l : List GlueHull) (i : GlueHull) (h : i β l) :
β j, hullGlue l = some j β§ ghLe i j := by
cases l with
| nil => simp at h
| cons y t =>
have hc := foldl_cover ghLe GlueHull.join ghLe_refl ghLe_trans gh_join_l gh_join_r t y
refine β¨t.foldl GlueHull.join y, rfl, ?_β©
rcases List.mem_cons.mp h with e | e
Β· subst e; exact hc.1
Β· exact hc.2 i e
/-! ## 3. Values: AVal is an abstract domain -/
theorem Ξ³_meanings {ms : List AMeaning} {v : Val} :
(AVal.meanings ms).Ξ³ v β β a m, a β ms β§ v = .meaning m β§ a.Ξ³ m := Iff.rfl
theorem Ξ³_ints {ms : List Itv} {v : Val} :
(AVal.ints ms).Ξ³ v β β i n, i β ms β§ v = .int n β§ i.Ξ³ n := Iff.rfl
theorem Ξ³_glues {ms : List GlueHull} {v : Val} :
(AVal.glues ms).Ξ³ v β β h g, h β ms β§ v = .glue g β§ h.Ξ³ g := Iff.rfl
theorem Ξ³_toks {ms : List AText} {v : Val} :
(AVal.toks ms).Ξ³ v β β a p, a β ms β§ v = .toks p β§ AText.Ξ³ a p β§ balanced p = true := Iff.rfl
theorem Ξ³_box {mv : Bool} {wd ht dp : Itv} {v : Val} :
(AVal.box mv wd ht dp).Ξ³ v β (mv = true β§ v = .box .void) β¨
β x y z, (v = .box (.hbox x y z) β¨ v = .box (.vbox x y z)) β§
wd.Ξ³ x β§ ht.Ξ³ y β§ dp.Ξ³ z := Iff.rfl
theorem Ξ³_topv (v : Val) : AVal.top.Ξ³ v := True.intro
/-! ### The text hull
A hull forgets structure and keeps one hole whose class is the union of the
items' classes. It is sound because a class only grows under union, and
every token an item stands for is admitted by the item's class. -/
/-- The class order as meaning: every token a admits, b admits. -/
def ClsLe (a b : TClass) : Prop := β t, a.admits t β b.admits t
/-- A class that admits a letter or other has effHex = hex. -/
theorem admits_eff {a : TClass} {t : Token} (h : a.admits t) :
a.c = true β¨ (a.has (kindOf t) = true β§ (a.effHex = true β hexOk t)) := by
rcases h with h | β¨h1, h2β©
Β· exact Or.inl h
Β· refine Or.inr β¨h1, fun he hk => h2 ?_ hkβ©
unfold TClass.effHex at he
rcases hk with k | k <;> rw [k] at h1 <;> simp only [TClass.has] at h1 <;> simpa [h1] using he
theorem admits_union {a b : TClass} {t : Token} (h : a.c = true β¨
(a.has (kindOf t) = true β§ (a.effHex = true β hexOk t)))
(hab : β k, a.has k = true β (a.union b).has k = true) (hc : a.c = true β (a.union b).c = true)
(he : (a.union b).hex = true β a.effHex = true) : (a.union b).admits t := by
rcases h with h | β¨h1, h2β©
Β· exact Or.inl (hc h)
Β· exact Or.inr β¨hab _ h1, fun hx => h2 (he hx)β©
theorem admits_union_l (a b : TClass) : ClsLe a (a.union b) := fun _ h =>
admits_union (admits_eff h)
(fun k hk => by cases k <;> simp_all [TClass.union, TClass.norm, TClass.has])
(fun hc => by simp [TClass.union, TClass.norm, hc])
(fun hx => by revert hx; simp only [TClass.union, TClass.norm]; cases a.effHex <;> simp)
theorem admits_union_r (a b : TClass) : ClsLe b (a.union b) := fun _ h => by
rcases admits_eff h with hc | β¨h1, h2β©
Β· exact Or.inl (by simp [TClass.union, TClass.norm, hc])
Β· refine Or.inr β¨by cases k : kindOf _ <;> rw [k] at h1 <;> simp_all [TClass.union, TClass.norm, TClass.has],
fun hx => h2 ?_β©
revert hx; simp only [TClass.union, TClass.norm]; cases b.effHex <;> simp
theorem clsLe_trans {a b c : TClass} (h1 : ClsLe a b) (h2 : ClsLe b c) : ClsLe a c :=
fun t h => h2 t (h1 t h)
/-- \number writes digits and a sign only. -/
theorem printInt_number (n : Int) : β t, t β printInt n β TClass.number.admits t := by
intro t ht
have hd : β ch, ch β Nat.toDigits 10 n.natAbs β TClass.number.admits (.char ch.toNat .other) := by
intro ch hch
have h := Nat.isDigit_of_mem_toDigits (by decide) (by decide) hch
have hc : isDigitCode ch.toNat = true := by
simp only [Char.isDigit, Bool.and_eq_true, decide_eq_true_eq] at h
simp only [isDigitCode, Bool.and_eq_true, decide_eq_true_eq]
exact β¨h.1, h.2β©
right
simp [kindOf, hc, TClass.has, TClass.number]
unfold printInt at ht
split at ht
Β· rcases List.mem_cons.mp ht with e | e
Β· subst e; right; simp [kindOf, isDigitCode, TClass.has, TClass.number]
Β· obtain β¨ch, hch, eβ© := List.mem_map.mp e; subst e; exact hd ch hch
Β· obtain β¨ch, hch, eβ© := List.mem_map.mp ht; subst e; exact hd ch hch
/-- Every token an item stands for is admitted by the item's class. -/
theorem item_admits (it : AItem) (p : TokList) (h : it.Ξ³ p) : β t, t β p β it.cls.admits t := by
intro t ht
cases it with
| tok u =>
have e : p = [u] := h
subst e
have e : t = u := List.mem_singleton.mp ht
subst e
unfold AItem.cls TClass.admits
cases k : kindOf t <;> simp [TClass.has, k]
| hole hl => exact h.1 t ht
| digits i => obtain β¨n, _, eβ© := h; subst e; exact printInt_number n t ht
| oneOf _ => left; rfl
theorem text_admits : β (x : AText) (p : TokList), AText.Ξ³ x p β
β t, t β p β β it, it β x β§ it.cls.admits t
| [], p, h, t, ht => by have e : p = [] := h; subst e; cases ht
| it :: rest, p, h, t, ht => by
obtain β¨pβ, pβ, e, h1, h2β© := h
subst e
rcases List.mem_append.mp ht with h' | h'
Β· exact β¨it, List.mem_cons_self .., item_admits it pβ h1 t h'β©
Β· obtain β¨j, hj, haβ© := text_admits rest pβ h2 t h'
exact β¨j, List.mem_cons_of_mem _ hj, haβ©
/-- A fold of union lies above its start and above each folded class. -/
theorem fold_union_cover {Ξ² : Type} (f : Ξ² β TClass) :
β (t : List Ξ²) (k : TClass), ClsLe k (t.foldl (fun k' b => k'.union (f b)) k) β§
β b, b β t β ClsLe (f b) (t.foldl (fun k' b => k'.union (f b)) k)
| [], k => β¨fun _ h => h, fun _ hb => by cases hbβ©
| b :: t, k => by
obtain β¨h1, h2β© := fold_union_cover f t (k.union (f b))
refine β¨clsLe_trans (admits_union_l _ _) h1, fun b' hb' => ?_β©
rcases List.mem_cons.mp hb' with e | e
Β· subst e; exact clsLe_trans (admits_union_r _ _) h1
Β· exact h2 b' e
theorem fold_text_cover :
β (xs : List AText) (k : TClass), ClsLe k (xs.foldl (fun k x =>
x.foldl (fun (k' : TClass) (it : AItem) => k'.union it.cls) k) k) β§
β x, x β xs β β it, it β x β ClsLe it.cls (xs.foldl (fun k x =>
x.foldl (fun (k' : TClass) (it : AItem) => k'.union it.cls) k) k)
| [], k => β¨fun _ h => h, fun _ hx => by cases hxβ©
| x :: xs, k => by
have hx := fold_union_cover AItem.cls x k
obtain β¨h1, h2β© := fold_text_cover xs (x.foldl (fun k' it => k'.union it.cls) k)
refine β¨clsLe_trans hx.1 h1, fun x' hx' it hit => ?_β©
rcases List.mem_cons.mp hx' with e | e
Β· subst e; exact clsLe_trans (hx.2 it hit) h1
Β· exact h2 x' e it hit
/-- The text hull covers every balanced member (PROOF-STATUS P3, P4 fixed:
hex restricts letters and others only, and token registers are balanced). -/
theorem hull_sound (xs : List AText) (x : AText) (p : TokList) (hx : x β xs)
(hp : AText.Ξ³ x p) (hb : balanced p = true) : AText.Ξ³ (AText.hull xs) p := by
refine β¨p, [], (List.append_nil p).symm, β¨fun t ht => ?_, (fun h => by simp at h),
(fun _ h => by simp at h), hbβ©, rflβ©
obtain β¨it, hit, haβ© := text_admits x p hp t ht
exact (fold_text_cover xs {}).2 x hx it hit t ha
theorem join_sound_meanings (L : Limits) (xs ys : List AMeaning) (v : Val)
(h : (AVal.meanings xs).Ξ³ v β¨ (AVal.meanings ys).Ξ³ v) :
(AVal.join L (.meanings xs) (.meanings ys)).Ξ³ v := by
have hm : β a m, a β xs ++ ys β§ v = .meaning m β§ a.Ξ³ m := by
rcases h with h | h
Β· obtain β¨a, m, hi, hv, hnβ© := Ξ³_meanings.mp h
exact β¨a, m, List.mem_append.mpr (Or.inl hi), hv, hnβ©
Β· obtain β¨a, m, hi, hv, hnβ© := Ξ³_meanings.mp h
exact β¨a, m, List.mem_append.mpr (Or.inr hi), hv, hnβ©
simp only [AVal.join]
exact ite_prop (P := fun r => AVal.Ξ³ r v) (fun _ => Ξ³_meanings.mpr hm) (fun _ => Ξ³_topv v)
theorem join_sound_ints (L : Limits) (xs ys : List Itv) (v : Val)
(h : (AVal.ints xs).Ξ³ v β¨ (AVal.ints ys).Ξ³ v) :
(AVal.join L (.ints xs) (.ints ys)).Ξ³ v := by
have hm : β i n, i β xs ++ ys β§ v = .int n β§ i.Ξ³ n := by
rcases h with h | h
Β· obtain β¨i, n, hi, hv, hnβ© := Ξ³_ints.mp h
exact β¨i, n, List.mem_append.mpr (Or.inl hi), hv, hnβ©
Β· obtain β¨i, n, hi, hv, hnβ© := Ξ³_ints.mp h
exact β¨i, n, List.mem_append.mpr (Or.inr hi), hv, hnβ©
simp only [AVal.join]
refine ite_prop (P := fun r => AVal.Ξ³ r v) (fun _ => Ξ³_ints.mpr hm) (fun _ => ?_)
obtain β¨i, n, hi, hv, hnβ© := hm
obtain β¨j, hj, hleβ© := hullItv_cover (xs ++ ys) i hi
rw [hj]
exact Ξ³_ints.mpr β¨j, n, List.mem_singleton.mpr rfl, hv, Itv.Ξ³_mono hle hnβ©
theorem join_sound_glues (L : Limits) (xs ys : List GlueHull) (v : Val)
(h : (AVal.glues xs).Ξ³ v β¨ (AVal.glues ys).Ξ³ v) :
(AVal.join L (.glues xs) (.glues ys)).Ξ³ v := by
have hm : β i g, i β xs ++ ys β§ v = .glue g β§ i.Ξ³ g := by
rcases h with h | h
Β· obtain β¨i, g, hi, hv, hnβ© := Ξ³_glues.mp h
exact β¨i, g, List.mem_append.mpr (Or.inl hi), hv, hnβ©
Β· obtain β¨i, g, hi, hv, hnβ© := Ξ³_glues.mp h
exact β¨i, g, List.mem_append.mpr (Or.inr hi), hv, hnβ©
simp only [AVal.join]
refine ite_prop (P := fun r => AVal.Ξ³ r v) (fun _ => Ξ³_glues.mpr hm) (fun _ => ?_)
obtain β¨i, g, hi, hv, hnβ© := hm
obtain β¨j, hj, hleβ© := hullGlue_cover (xs ++ ys) i hi
rw [hj]
exact Ξ³_glues.mpr β¨j, g, List.mem_singleton.mpr rfl, hv, gh_Ξ³_mono hle hnβ©
theorem join_sound_toks (L : Limits) (xs ys : List AText) (v : Val)
(h : (AVal.toks xs).Ξ³ v β¨ (AVal.toks ys).Ξ³ v) :
(AVal.join L (.toks xs) (.toks ys)).Ξ³ v := by
have hm : β x p, x β xs ++ ys β§ v = .toks p β§ AText.Ξ³ x p β§ balanced p = true := by
rcases h with h | h
Β· obtain β¨x, p, hi, hv, hnβ© := Ξ³_toks.mp h
exact β¨x, p, List.mem_append.mpr (Or.inl hi), hv, hnβ©
Β· obtain β¨x, p, hi, hv, hnβ© := Ξ³_toks.mp h
exact β¨x, p, List.mem_append.mpr (Or.inr hi), hv, hnβ©
simp only [AVal.join]
refine ite_prop (P := fun r => AVal.Ξ³ r v) (fun _ => Ξ³_toks.mpr hm) (fun _ => ?_)
obtain β¨x, p, hx, hv, hp, hbβ© := hm
exact Ξ³_toks.mpr β¨AText.hull (xs ++ ys), p, List.mem_singleton.mpr rfl, hv,
hull_sound _ x p hx hp hb, hbβ©
theorem join_sound_box (L : Limits) (mv : Bool) (wd ht dp : Itv) (mv' : Bool) (wd' ht' dp' : Itv)
(v : Val) (h : (AVal.box mv wd ht dp).Ξ³ v β¨ (AVal.box mv' wd' ht' dp').Ξ³ v) :
(AVal.join L (.box mv wd ht dp) (.box mv' wd' ht' dp')).Ξ³ v := by
simp only [AVal.join]
apply Ξ³_box.mpr
rcases h with h | h
Β· rcases Ξ³_box.mp h with β¨hm, hvβ© | β¨x, y, z, hk, hx, hy, hzβ©
Β· left; subst hm; exact β¨rfl, hvβ©
Β· right
exact β¨x, y, z, hk, Itv.Ξ³_mono (Itv.le_join_left wd wd') hx,
Itv.Ξ³_mono (Itv.le_join_left ht ht') hy, Itv.Ξ³_mono (Itv.le_join_left dp dp') hzβ©
Β· rcases Ξ³_box.mp h with β¨hm, hvβ© | β¨x, y, z, hk, hx, hy, hzβ©
Β· left; subst hm; exact β¨Bool.or_true mv, hvβ©
Β· right
exact β¨x, y, z, hk, Itv.Ξ³_mono (Itv.le_join_right wd wd') hx,
Itv.Ξ³_mono (Itv.le_join_right ht ht') hy, Itv.Ξ³_mono (Itv.le_join_right dp dp') hzβ©
/-- AVal.join_sound (CHANGES.md item 1): the join covers the union,
unconditionally. -/
theorem aval_join_sound (L : Limits) (a b : AVal) (v : Val)
(h : a.Ξ³ v β¨ b.Ξ³ v) : (AVal.join L a b).Ξ³ v := by
cases a <;> cases b
all_goals first
| exact True.intro
| exact join_sound_meanings L _ _ v h
| exact join_sound_ints L _ _ v h
| exact join_sound_glues L _ _ v h
| exact join_sound_toks L _ _ v h
| exact join_sound_box L _ _ _ _ _ _ _ _ v h
/-- AVal with the semantic order is an abstract domain in the sense of AbsDom. -/
def avalDom (L : Limits) : AbsDom Val AVal where
le := AVal.le
join := AVal.join L
top := .top
Ξ³ := AVal.Ξ³
le_refl := by intro a c h; exact h
le_trans := by intro a b d h1 h2 c hc; exact h2 c (h1 c hc)
join_l := by intro a b c hc; exact aval_join_sound L a b c (Or.inl hc)
join_r := by intro a b c hc; exact aval_join_sound L a b c (Or.inr hc)
le_top := by intro a c _; exact True.intro
Ξ³_mono := by intro a b h c hc; exact h c hc
/-! ### Widening
AVal.widen jumps a moving bound to the end of the 32-bit word. That is
sound for every value TeX can hold: counts and dimensions are words. -/
/-- Every member interval lies in the word. -/
def AValInWord : AVal β Prop
| .ints ms => β i, i β ms β i.inWord
| _ => True
theorem inWord_iff (i : Itv) : i.inWord β Itv.le i Itv.word := by
unfold Itv.inWord Itv.le Itv.word; constructor <;> intro h <;> exact β¨h.1, h.2β©
theorem foldl_join_le (c : Itv) : β (t : List Itv) (acc : Itv), Itv.le acc c β
(β i, i β t β Itv.le i c) β Itv.le (t.foldl Itv.join acc) c
| [], _, h, _ => h
| y :: t, _, h, ht => foldl_join_le c t _
(itv_join_lub _ _ _ h (ht y (List.mem_cons_self ..)))
(fun i hi => ht i (List.mem_cons_of_mem _ hi))
theorem hullItv_inWord {l : List Itv} {j : Itv} (h : hullItv l = some j)
(hw : β i, i β l β i.inWord) : j.inWord := by
cases l with
| nil => cases h
| cons y t =>
simp only [hullItv, Option.some.injEq] at h
subst h
exact (inWord_iff _).mpr (foldl_join_le _ t y ((inWord_iff _).mp (hw y (List.mem_cons_self ..)))
(fun i hi => (inWord_iff _).mp (hw i (List.mem_cons_of_mem _ hi))))
theorem widen_other (L : Limits) (a b : AVal) (v : Val) (hjoin : (AVal.join L a b).Ξ³ v) :
(match AVal.join L a b with | .meanings _ => AVal.top | j => j).Ξ³ v := by
revert hjoin; cases AVal.join L a b <;> intro h <;> first | exact True.intro | exact h
/-- AVal.widen_sound (CHANGES item 2): widening covers both sides. -/
theorem aval_widen_sound (L : Limits) (a b : AVal) (ha : AValInWord a) (hb : AValInWord b) (v : Val)
(h : a.Ξ³ v β¨ b.Ξ³ v) : (AVal.widen L a b).Ξ³ v := by
have hjoin := aval_join_sound L a b v h
cases a <;> cases b
case ints.ints xs ys =>
simp only [AVal.widen]
have hw : β i, i β xs ++ ys β i.inWord := fun i hi => by
rcases List.mem_append.mp hi with e | e
Β· exact ha i e
Β· exact hb i e
have hm : β i n, i β xs ++ ys β§ v = .int n β§ i.Ξ³ n := by
rcases h with h | h
Β· obtain β¨i, n, hi, hv, hnβ© := Ξ³_ints.mp h
exact β¨i, n, List.mem_append.mpr (Or.inl hi), hv, hnβ©
Β· obtain β¨i, n, hi, hv, hnβ© := Ξ³_ints.mp h
exact β¨i, n, List.mem_append.mpr (Or.inr hi), hv, hnβ©
split
Β· rename_i p q hp hq
obtain β¨i, n, hi, hv, hnβ© := hm
obtain β¨j, hj, hleβ© := hullItv_cover (xs ++ ys) i hi
rw [hq] at hj; cases hj
have hpw : p.inWord := hullItv_inWord hp ha
have hqw : q.inWord := hullItv_inWord hq hw
exact Ξ³_ints.mpr β¨_, n, List.mem_singleton.mpr rfl, hv,
Itv.Ξ³_mono (Itv.le_widen p q hpw hqw).2 (Itv.Ξ³_mono hle hn)β©
Β· exact Ξ³_ints.mpr hm
case toks.toks xs ys =>
simp only [AVal.widen]
have hm : β x p, x β xs ++ ys β§ v = .toks p β§ AText.Ξ³ x p β§ balanced p = true := by
rcases h with h | h
Β· obtain β¨x, p, hi, hv, hnβ© := Ξ³_toks.mp h
exact β¨x, p, List.mem_append.mpr (Or.inl hi), hv, hnβ©
Β· obtain β¨x, p, hi, hv, hnβ© := Ξ³_toks.mp h
exact β¨x, p, List.mem_append.mpr (Or.inr hi), hv, hnβ©
obtain β¨x, p, hx, hv, hp, hbβ© := hm
exact Ξ³_toks.mpr β¨_, p, List.mem_singleton.mpr rfl, hv, hull_sound _ x p hx hp hb, hbβ©
all_goals first
| exact True.intro
| (simp only [AVal.widen]; exact widen_other L _ _ v hjoin)
/-! ## 4. Assignment and groups (Β§Β§ 268β284)
One lemma per transfer, none with a pin on the group level: under a many
frame or after a summary the transfer leaves touched, and skip absorbs
whatever restore entries TeX made (PROOF-STATUS P1, P5). -/
/-- The semantic order on abstract states. -/
abbrev ALe (x y : AState) : Prop := β s, x.Ξ³ s β y.Ξ³ s
theorem ale_refl (x : AState) : ALe x x := fun _ h => h
theorem ale_trans {x y z : AState} (h1 : ALe x y) (h2 : ALe y z) : ALe x z :=
fun s h => h2 s (h1 s h)
/-- One location of the eqtb is covered: value and level. -/
def CovAt (a : AState) (E : Loc β Entry) (l : Loc) : Prop :=
(a.eqtb l).Ξ³ (E l).val β§ β m, a.level l = some m β (E l).level = m
/-- A write keeps every location covered. -/
theorem covAt_upd {a : AState} {E : Loc β Entry} {l : Loc} {v : AVal} {x : Val}
{lv : Option Nat} {n : Nat} (hv : v.Ξ³ x) (hlv : β m, lv = some m β n = m)
(h : β l', l' β l β CovAt a E l') (l' : Loc) :
CovAt { a with eqtb := upd a.eqtb l v, level := upd a.level l lv }
(upd E l { val := x, level := n }) l' := by
by_cases e : l' = l
Β· subst e; exact β¨by simp only [upd_same]; exact hv, by simp only [upd_same]; exact hlvβ©
Β· obtain β¨h1, h2β© := h l' e
exact β¨by simp only [upd_other e]; exact h1, by simp only [upd_other e]; exact h2β©
/-- The written state: covered eqtb, a related save stack, the rest unchanged. -/
theorem Ξ³_written {a : AState} {s : State} {l : Loc} {v : AVal} {x : Val} {lv : Option Nat}
{n : Nat} {sv : List ASave} {cs : List SaveEntry}
(ha : a.Ξ³ s) (hv : v.Ξ³ x) (hlv : β m, lv = some m β n = m) (hsv : SaveRel sv cs) :
AState.Ξ³ { a with eqtb := upd a.eqtb l v, level := upd a.level l lv, save := sv }
{ s with eqtb := upd s.eqtb l { val := x, level := n }, save := cs } := by
obtain β¨h1, h2, h3, _, h5, h6, h7β© := ha
have hc := covAt_upd (a := a) (E := s.eqtb) (l := l) hv hlv (fun l' _ => β¨h1 l', h2 l'β©)
exact β¨fun l' => (hc l').1, fun l' => (hc l').2, h3, hsv, h5, h6, h7β©
theorem touched_head (l : Loc) (sv : List ASave) : Touched l (.touched l :: sv) := Or.inl rfl
/-- A local write whose save TeX may or may not make: touched covers both. -/
theorem saveRel_touch {sv : List ASave} {cs : List SaveEntry} (l : Loc) (e : Entry) (b : Bool)
(h : SaveRel sv cs) :
SaveRel (.touched l :: sv) (if b then .restore l e :: cs else cs) := by
cases b
Β· exact SaveRel.touched h
Β· exact SaveRel.skip (touched_head l sv) (SaveRel.touched h)
theorem eqDefine_eq (s : State) (l : Loc) (x : Val) :
Concrete.eqDefine s l x =
{ s with eqtb := upd s.eqtb l { val := x, level := s.curLevel },
save := if decide ((s.eqtb l).level β s.curLevel β§ s.curLevel > 1) then
.restore l (s.eqtb l) :: s.save else s.save } := by
simp only [Concrete.eqDefine]
by_cases h : (s.eqtb l).level β s.curLevel β§ s.curLevel > 1 <;> simp [h]
/-- Β§ 279 geq_define. -/
theorem assign_global_sound {a : AState} {s : State} {l : Loc} {v : AVal} {x : Val}
(ha : a.Ξ³ s) (hv : v.Ξ³ x) : (a.assign true l v).Ξ³ (Concrete.geqDefine s l x) :=
Ξ³_written (lv := some 1) (n := 1) (sv := a.save) (cs := s.save) ha hv
(fun _ h => (Option.some.inj h)) ha.2.2.2.1
/-- Β§ 277 eq_define, at any group level and under many frames. -/
theorem assign_local_sound {a : AState} {s : State} {l : Loc} {v : AVal} {x : Val}
(ha : a.Ξ³ s) (hv : v.Ξ³ x) : (a.assign false l v).Ξ³ (Concrete.eqDefine s l x) := by
rw [eqDefine_eq]
have hsr := ha.2.2.2.1
have touchAny : β (b : Bool) (lv : Option Nat), (β m, lv = some m β s.curLevel = m) β
AState.Ξ³ { a with eqtb := upd a.eqtb l v, level := upd a.level l lv,
save := .touched l :: a.save }
{ s with eqtb := upd s.eqtb l { val := x, level := s.curLevel },
save := if b then .restore l (s.eqtb l) :: s.save else s.save } :=
fun b lv hlv => Ξ³_written ha hv hlv (saveRel_touch l _ b hsr)
unfold AState.assign
simp only [Bool.false_eq_true, βreduceIte]
cases hp : a.pinned
Β· simp only [Bool.not_false, βreduceIte]
exact touchAny _ none (fun _ h => by cases h)
Β· have hpin := ha.2.2.1 hp
have hl1 : β m, some a.curLevel = some m β s.curLevel = m :=
fun m h => by rw [β hpin]; exact Option.some.inj h
simp only [Bool.not_true, Bool.false_eq_true, βreduceIte]
cases hl : a.level l with
| none =>
by_cases c : a.curLevel β€ 1
Β· simp only [c, βreduceIte]
have hd : decide ((s.eqtb l).level β s.curLevel β§ s.curLevel > 1) = false := by
simp; omega
rw [hd]
exact Ξ³_written ha hv hl1 hsr
Β· simp only [c, βreduceIte]
exact touchAny _ _ hl1
| some n =>
have hn : (s.eqtb l).level = n := ha.2.1 l n hl
by_cases c : n = a.curLevel β¨ a.curLevel β€ 1
Β· simp only [c, βreduceIte]
have hd : decide ((s.eqtb l).level β s.curLevel β§ s.curLevel > 1) = false := by
simp; omega
rw [hd]
exact Ξ³_written ha hv hl1 hsr
Β· simp only [c, βreduceIte]
have hd : decide ((s.eqtb l).level β s.curLevel β§ s.curLevel > 1) = true := by
simp; omega
rw [hd]
simp only [βreduceIte]
exact Ξ³_written (a := { a with pinned := true }) (l := l)
β¨ha.1, ha.2.1, fun _ => hpin, ha.2.2.2β© hv hl1
(SaveRel.restore (ha.1 l) (fun m h => by rw [hn]; exact Option.some.inj h) hsr)
/-- define with \globaldefs (Β§ 1214). -/
theorem define_sound {a : AState} {s : State} {l : Loc} {v : AVal} {x : Val}
(ha : a.Ξ³ s) (hv : v.Ξ³ x) (glob : Bool) :
(a.assign (Concrete.isGlobal s glob) l v).Ξ³ (Concrete.define s glob l x) := by
unfold Concrete.define
cases Concrete.isGlobal s glob
Β· exact assign_local_sound ha hv
Β· exact assign_global_sound ha hv
theorem lvJoin_some {x y : Option Nat} {m : Nat} (h : AState.lvJoin x y = some m) :
x = some m β§ y = some m := by
unfold AState.lvJoin at h
split at h
Β· rename_i e; subst e; exact β¨h, hβ©
Β· cases h
theorem newLevel_sound {a : AState} {s : State} (ha : a.Ξ³ s) {glob : Bool} {m : Nat}
(h : a.newLevel glob = some m) : (if glob then 1 else s.curLevel) = m := by
unfold AState.newLevel at h
cases glob
Β· cases hp : a.pinned
Β· simp [hp] at h
Β· simp only [hp, Bool.false_eq_true, βreduceIte, Option.some.injEq] at h β’
rw [β h]; exact (ha.2.2.1 hp).symm
Β· simp only [βreduceIte, Option.some.injEq] at h β’; exact h
/-- A weak write at l covers the state where nothing was written. -/
theorem weakAt_keep (L : Limits) {a : AState} {t : State} (glob : Bool) (l : Loc) (v : AVal)
(ha : a.Ξ³ t) : (a.weakAt L glob l v).Ξ³ t := by
obtain β¨h1, h2, h3, h4, h5, h6, h7β© := ha
have hc := covAt_upd (a := a) (E := t.eqtb) (l := l) (x := (t.eqtb l).val)
(v := AVal.join L (a.eqtb l) v) (lv := AState.lvJoin (a.level l) (a.newLevel glob))
(n := (t.eqtb l).level) (aval_join_sound L _ _ _ (Or.inl (h1 l)))
(fun m hm => h2 l m (lvJoin_some hm).1)
(fun l' _ => β¨h1 l', h2 l'β©)
have he : upd t.eqtb l { val := (t.eqtb l).val, level := (t.eqtb l).level } = t.eqtb := by
funext l'; by_cases e : l' = l
Β· subst e; simp [upd_same]
Β· simp [upd_other e]
rw [he] at hc
exact β¨fun l' => (hc l').1, fun l' => (hc l').2, h3, SaveRel.touched h4, h5, h6, h7β©
/-- A weak write at l covers the state where l was written. -/
theorem weakAt_write (L : Limits) {a : AState} {s : State} (glob : Bool) (l : Loc) {v : AVal}
{x : Val} (ha : a.Ξ³ s) (hv : v.Ξ³ x) :
(a.weakAt L glob l v).Ξ³ (if glob then Concrete.geqDefine s l x else Concrete.eqDefine s l x) := by
have hj := aval_join_sound L (a.eqtb l) v x (Or.inr hv)
have hn := fun m (hm : AState.lvJoin (a.level l) (a.newLevel glob) = some m) =>
newLevel_sound ha (lvJoin_some hm).2
cases glob
Β· simp only [Bool.false_eq_true, βreduceIte] at hn β’
rw [eqDefine_eq]
exact Ξ³_written (a := a) (l := l) ha hj hn (saveRel_touch l _ _ ha.2.2.2.1)
Β· simp only [βreduceIte] at hn β’
exact Ξ³_written (a := a) (l := l) (n := 1) ha hj hn (SaveRel.touched ha.2.2.2.1)
/-- weakAssign_sound (CHANGES item 4): a write to one of ls, unknown
which, is covered. -/
theorem weakAssign_sound (L : Limits) (glob : Bool) {v : AVal} {x : Val} {l : Loc} (hv : v.Ξ³ x)
{s : State} :
β (ls : List Loc) (a : AState),
(a.Ξ³ s β l β ls β (a.weakAssign L glob ls v).Ξ³
(if glob then Concrete.geqDefine s l x else Concrete.eqDefine s l x)) β§
β t, a.Ξ³ t β (a.weakAssign L glob ls v).Ξ³ t
| [], a => β¨fun _ h => by simp at h, fun _ h => hβ©
| l' :: ls, a => by
have ih := weakAssign_sound L glob hv (l := l) (s := s) ls
refine β¨fun ha hl => ?_, fun t ht => (ih _).2 t (weakAt_keep L glob l' v ht)β©
show (ls.foldl (fun s l => s.weakAt L glob l v) (a.weakAt L glob l' v)).Ξ³ _
by_cases e : l' = l
Β· subst e; exact (ih _).2 _ (weakAt_write L glob l' ha hv)
Β· rcases List.mem_cons.mp hl with h | h
Β· exact absurd h.symm e
Β· exact (ih _).1 (weakAt_keep L glob l' v ha) h
/-! ### Closing a group
closeFrom_sound walks the related stacks entry by entry. Its invariant: a
location is covered unless a touched entry for it still lies ahead in the
frame, which will make it unknown. -/
/-- Covered except where a touched entry lies ahead in the frame. -/
def CovFrame (a : AState) (E : Loc β Entry) (as : List ASave) : Prop :=
β l, Β¬ Touched l as β CovAt a E l
theorem covAt_top {a : AState} {E : Loc β Entry} {l : Loc} :
CovAt (a.setAt l .top none) E l :=
β¨by simp [AState.setAt, upd_same]; exact True.intro, fun _ h => by simp [AState.setAt, upd_same] at hβ©
theorem covAt_setAt_other {a : AState} {E : Loc β Entry} {l l' : Loc} {v : AVal} {lv : Option Nat}
(e : l' β l) (h : CovAt a E l') : CovAt (a.setAt l v lv) E l' :=
β¨by simp [AState.setAt, upd_other e]; exact h.1, by simp [AState.setAt, upd_other e]; exact h.2β©
theorem covAt_E_other {a : AState} {E : Loc β Entry} {l l' : Loc} {e : Entry}
(ne : l' β l) (h : CovAt a E l') : CovAt a (upd E l e) l' :=
β¨by simp only [upd_other ne]; exact h.1, by simp only [upd_other ne]; exact h.2β©
/-- forget makes its locations unknown and keeps the rest. -/
theorem forget_cov (E : Loc β Entry) : β (ls : List Loc) (a : AState) (l : Loc),
(l β ls β CovAt a E l) β CovAt (a.forget ls) E l
| [], a, l, h => h (fun h => by cases h)
| m :: ls, a, l, h => by
unfold AState.forget
simp only [List.foldl_cons]
refine forget_cov E ls _ l (fun hl => ?_)
by_cases e : l = m
Β· subst e; exact covAt_top
Β· exact covAt_setAt_other e (h (fun hm => by
rcases List.mem_cons.mp hm with h' | h'
Β· exact e h'
Β· exact hl h'))
theorem forget_fields : β (ls : List Loc) (a : AState),
(a.forget ls).save = a.save β§ (a.forget ls).input = a.input β§ (a.forget ls).cond = a.cond β§
(a.forget ls).modes = a.modes
| [], _ => β¨rfl, rfl, rfl, rflβ©
| _ :: ls, a => forget_fields ls (a.setAt _ .top none)
/-- Restore entries of locations ls change no other location. -/
theorem unsaveAux_restores (ls : List Loc) : β (pre : List SaveEntry) (E : Loc β Entry)
(ins : TokList) (rest : List SaveEntry),
(β e, e β pre β β l old, e = .restore l old β§ l β ls) β
β E1, Concrete.unsaveAux E ins (pre ++ rest) = Concrete.unsaveAux E1 ins rest β§
β l, l β ls β E1 l = E l
| [], E, _, _, _ => β¨E, rfl, fun _ _ => rflβ©
| e :: pre, E, ins, rest, h => by
obtain β¨l, old, he, hlβ© := h e (List.mem_cons_self ..)
subst he
obtain β¨E1, e1, h1β© := unsaveAux_restores ls pre
(if (E l).level = 1 then E else upd E l old) ins rest
(fun e' h' => h e' (List.mem_cons_of_mem _ h'))
refine β¨E1, e1, fun l' hl' => ?_β©
rw [h1 l' hl']
have ne : l' β l := fun e => hl' (e βΈ hl)
split
Β· rfl
Β· exact upd_other ne
/-- What closing a frame gives, part by part (the closed group's Ξ³). -/
def Closed (a b : AState) (E' : Loc β Entry) (ins' : TokList) (crest : List SaveEntry) : Prop :=
(β l, CovAt b E' l) β§ SaveRel b.save crest β§ b.cond = a.cond β§ b.modes = a.modes β§
b.input = (if ins' = [] then a.input else .toks .backedUp (AText.ofList ins') :: a.input) β§
(b.pinned = true β a.pinned = true β§ b.curLevel = a.curLevel - 1)
theorem closeFrom_sound (L : Limits) (c : GroupCode) {as : List ASave} {cs : List SaveEntry}
(h : SaveRel as cs) :
β (a : AState) (E : Loc β Entry) (ins : TokList) (E' : Loc β Entry) (ins' : TokList)
(ctx : Option BoxContext) (crest : List SaveEntry),
CovFrame a E as β Concrete.unsaveAux E ins cs = (E', ins', some (c, ctx), crest) β
β b, b β AState.closeFrom L c a ins as β§ Closed a b E' ins' crest := by
induction h with
| nil => intro a E ins E' ins' ctx crest _ hu; simp [Concrete.unsaveAux] at hu
| @restore l old lv e rest crest0 hold hlv _ ih =>
intro a E ins E' ins' ctx crest hcov hu
simp only [Concrete.unsaveAux] at hu
have hf : β b, Closed (a.restoreOne L l old lv) b E' ins' crest β Closed a b E' ins' crest := by
intro b hb; unfold AState.restoreOne at hb; split at hb <;> exact hb
suffices hcf : CovFrame (a.restoreOne L l old lv) (if (E l).level = 1 then E else upd E l e) rest by
obtain β¨b, hb, hcβ© := ih (a.restoreOne L l old lv) _ ins E' ins' ctx crest hcf hu
exact β¨b, hb, hf b hcβ©
intro l' ht
have hc := hcov l' ht
by_cases e' : l' = l
Β· subst e'
obtain β¨hv, hlvlβ© := hc
unfold AState.restoreOne
cases hk : a.level l' with
| none =>
refine β¨?_, fun _ h => by simp [AState.setAt, upd_same] at hβ©
simp only [AState.setAt, upd_same]
split
Β· exact aval_join_sound L _ _ _ (Or.inl hv)
Β· simp only [upd_same]; exact aval_join_sound L _ _ _ (Or.inr hold)
| some k =>
have hE : (E l').level = k := hlvl k hk
by_cases k1 : k = 1
Β· subst k1
simp only [hE, βreduceIte]
exact β¨hv, hlvlβ©
Β· have hne : Β¬ (E l').level = 1 := by omega
simp only [hne, βreduceIte]
have hset : CovAt (a.setAt l' old lv) (upd E l' e) l' :=
β¨by simp [AState.setAt, upd_same]; exact hold,
fun m h => by simp [AState.setAt, upd_same] at h β’; exact hlv m hβ©
cases k with
| zero => exact hset
| succ k' => cases k' with
| zero => exact absurd rfl k1
| succ _ => exact hset
Β· have hr : CovAt (a.restoreOne L l old lv) E l' := by
unfold AState.restoreOne
split
Β· exact hc
Β· exact covAt_setAt_other e' hc
Β· exact covAt_setAt_other e' hc
split
Β· exact hr
Β· exact covAt_E_other e' hr
| @touched l rest crest0 _ ih =>
intro a E ins E' ins' ctx crest hcov hu
suffices hcf : CovFrame (a.setAt l .top none) E rest by
obtain β¨b, hb, hcβ© := ih (a.setAt l .top none) E ins E' ins' ctx crest hcf hu
exact β¨b, hb, hcβ©
intro l' ht
by_cases e' : l' = l
Β· subst e'; exact covAt_top
Β· exact covAt_setAt_other e' (hcov l' (fun h => by
rcases h with h | h
Β· exact e' h.symm
Β· exact ht h))
| @skip l e rest crest0 ht _ ih =>
intro a E ins E' ins' ctx crest hcov hu
simp only [Concrete.unsaveAux] at hu
refine ih a _ ins E' ins' ctx crest (fun l' ht' => ?_) hu
have e' : l' β l := fun h => ht' (h βΈ ht)
split
Β· exact hcov l' ht'
Β· exact covAt_E_other e' (hcov l' ht')
| @retain l rest crest0 _ ih =>
intro a E ins E' ins' ctx crest hcov hu
exact ih a E ins E' ins' ctx crest (fun l' ht => hcov l' ht) hu
| @insertToken t rest crest0 _ ih =>
intro a E ins E' ins' ctx crest hcov hu
exact ih a E (t :: ins) E' ins' ctx crest (fun l' ht => hcov l' ht) hu
| @group c0 ctx0 line rest crest0 hr _ =>
intro a E ins E' ins' ctx crest hcov hu
simp only [Concrete.unsaveAux, Prod.mk.injEq, Option.some.injEq] at hu
obtain β¨e1, e2, β¨e3, -β©, e4β© := hu
subst e1 e2 e3 e4
refine β¨a.closed ins rest, by simp [AState.closeFrom], fun l => hcov l (fun h => h),
hr, rfl, rfl, rfl, fun hp => β¨hp, rflβ©β©
| @manyNone c0 ls rest crest0 _ ih =>
intro a E ins E' ins' ctx crest hcov hu
obtain β¨b, hb, h1, h2, h3, h4, h5, h6β© :=
ih { a with pinned := false } E ins E' ins' ctx crest (fun l _ => hcov l (fun h => h)) hu
refine β¨b, ?_, h1, h2, h3, h4, h5, fun hp => by have h := (h6 hp).1; simp at hβ©
simp only [AState.closeFrom]
exact List.mem_append.mpr (Or.inr hb)
| @manyMore c0 ls ctx0 line rest crest0 pre hr hpre _ =>
intro a E ins E' ins' ctx crest hcov hu
obtain β¨E1, e1, hE1β© := unsaveAux_restores ls pre E ins _ hpre
rw [e1] at hu
simp only [Concrete.unsaveAux, Prod.mk.injEq, Option.some.injEq] at hu
obtain β¨e2, e3, β¨e4, -β©, e5β© := hu
subst e2 e3 e4 e5
obtain β¨f1, f2, f3, f4β© := forget_fields ls (a.closed ins (.group c0 (some ls) :: rest))
refine β¨{ (a.closed ins (.group c0 (some ls) :: rest)).forget ls with
curLevel := a.curLevel, pinned := false }, ?_, ?_, ?_, f3, f4, f2, fun h => by cases hβ©
Β· simp [AState.closeFrom]
Β· intro l
have hc := forget_cov E1 ls (a.closed ins (.group c0 (some ls) :: rest)) l
(fun hl => by
have h := hcov l (fun h => h)
unfold CovAt at h β’; rw [hE1 l hl]; exact h)
exact β¨hc.1, hc.2β©
Β· show SaveRel ((a.closed ins (.group c0 (some ls) :: rest)).forget ls).save _
rw [f1]; exact hr
theorem ofList_Ξ³ : β ts : TokList, AText.Ξ³ (AText.ofList ts) ts
| [] => rfl
| t :: ts => β¨[t], ts, rfl, rfl, ofList_Ξ³ tsβ©
/-- endGroup_sound (CHANGES item 5): the concrete unsave (Β§Β§ 281β283)
of a group of code c is covered by an abstract result, for plain and
many frames, \aftergroup tokens included (PROOF-STATUS P1, P2 fixed). -/
theorem endGroup_sound (L : Limits) {a : AState} {s : State} {c : GroupCode}
{E' : Loc β Entry} {ins : TokList} {ctx : Option BoxContext} {crest : List SaveEntry}
(ha : a.Ξ³ s) (hu : Concrete.unsaveAux s.eqtb [] s.save = (E', ins, some (c, ctx), crest)) :
β b, b β a.endGroup L c β§ b.Ξ³ (Concrete.unsave s).1 := by
obtain β¨h1, h2, h3, h4, h5, h6, h7β© := ha
obtain β¨b, hb, hc1, hc2, hc3, hc4, hc5, hc6β© :=
closeFrom_sound L c h4 a s.eqtb [] E' ins ctx crest (fun l _ => β¨h1 l, h2 lβ©) hu
refine β¨b, hb, ?_β©
unfold Concrete.unsave
rw [hu]
dsimp only
have hp : b.pinned = true β b.curLevel = s.curLevel - 1 := fun hp => by
rw [(hc6 hp).2, h3 (hc6 hp).1]
by_cases e : ins = []
Β· simp only [e, βreduceIte] at hc5 β’
exact β¨fun l => (hc1 l).1, fun l => (hc1 l).2, hp, hc2, hc3 βΈ h5, hc5 βΈ h6, hc4 βΈ h7β©
Β· simp only [e, βreduceIte, Concrete.pushToks] at hc5 β’
exact β¨fun l => (hc1 l).1, fun l => (hc1 l).2, hp, hc2, hc3 βΈ h5,
hc5 βΈ β¨β¨rfl, ofList_Ξ³ insβ©, h6β©, hc4 βΈ h7β©
/-! ### Loop and call summaries (PROOF-STATUS P5)
A summary replaces any number of runs of a body by one transfer. What it
may assume of the body is its footprint: the locations it writes, and that
it leaves the stacks as it found them, apart from restore entries for those
locations. -/
/-- What a body may change: the locations ws, and restore entries for
them on top of the save stack. -/
def Footprint (ws : List Loc) (s s' : State) : Prop :=
(β l, l β ws β s'.eqtb l = s.eqtb l) β§
(β pre, s'.save = pre ++ s.save β§ β e, e β pre β β l old, l β ws β§ e = .restore l old) β§
s'.curLevel = s.curLevel β§ s'.cond = s.cond β§ s'.input = s.input β§ s'.mode = s.mode
theorem footprint_star {ws : List Loc} {s s' : State} (h : Star (Footprint ws) s s') :
Footprint ws s s' := by
induction h with
| refl => exact β¨fun _ _ => rfl, β¨[], rfl, fun _ h => by cases hβ©, rfl, rfl, rfl, rflβ©
| tail _ hbc ih =>
obtain β¨a1, β¨p1, e1, q1β©, a3, a4, a5, a6β© := ih
obtain β¨b1, β¨p2, e2, q2β©, b3, b4, b5, b6β© := hbc
refine β¨fun l hl => (b1 l hl).trans (a1 l hl), β¨p2 ++ p1, ?_, ?_β©,
b3.trans a3, b4.trans a4, b5.trans a5, b6.trans a6β©
Β· rw [e2, e1, List.append_assoc]
Β· intro e he
rcases List.mem_append.mp he with h | h
Β· exact q2 e h
Β· exact q1 e h
theorem touched_map {l : Loc} {ws : List Loc} (h : l β ws) (sv : List ASave) :
Touched l (ws.map ASave.touched ++ sv) := by
induction ws with
| nil => cases h
| cons w ws ih =>
rcases List.mem_cons.mp h with e | e
Β· exact Or.inl e.symm
Β· exact Or.inr (ih e)
theorem saveRel_touchedAll {ws : List Loc} {sv : List ASave} {cs : List SaveEntry}
(h : SaveRel sv cs) : SaveRel (ws.map ASave.touched ++ sv) cs := by
induction ws with
| nil => exact h
| cons w ws ih => exact SaveRel.touched ih
theorem saveRel_summary {ws : List Loc} {sv : List ASave} {cs : List SaveEntry}
(h : SaveRel sv cs) :
β pre : List SaveEntry, (β e, e β pre β β l old, l β ws β§ e = .restore l old) β
SaveRel (ws.map ASave.touched ++ sv) (pre ++ cs)
| [], _ => by simpa using saveRel_touchedAll (ws := ws) h
| e :: pre, hp => by
obtain β¨l, old, hl, heβ© := hp e (List.mem_cons_self ..)
subst he
exact SaveRel.skip (touched_map hl sv)
(saveRel_summary h pre (fun e' h' => hp e' (List.mem_cons_of_mem _ h')))
/-- summaryHavoc_sound (CHANGES item 9, corrected): the summary covers
every number of runs of a body with footprint ws, whatever the levels and
save entries the runs make. -/
theorem summaryHavoc_sound {ws : List Loc} {Body : State β State β Prop}
(hb : β s s', Body s s' β Footprint ws s s') {a : AState} {s s' : State} (ha : a.Ξ³ s)
(hrun : Star Body s s') : (summaryHavoc ws a).Ξ³ s' := by
have hst : Star (Footprint ws) s s' := by
induction hrun with
| refl => exact Star.refl
| tail _ h ih => exact Star.tail ih (hb _ _ h)
obtain β¨f1, β¨pre, e, qβ©, f3, f4, f5, f6β© := footprint_star hst
obtain β¨h1, h2, h3, h4, h5, h6, h7β© := ha
refine β¨fun l => ?_, fun l n hn => ?_, fun hp => ?_, ?_, ?_, ?_, ?_β©
Β· show (if ws.contains l then AVal.top else a.eqtb l).Ξ³ (s'.eqtb l).val
by_cases hl : l β ws
Β· have : ws.contains l = true := by simpa using hl
simp only [this, βreduceIte]; exact True.intro
Β· have : ws.contains l = false := by simpa using hl
simp only [this, Bool.false_eq_true, βreduceIte]; rw [f1 l hl]; exact h1 l
Β· change (if ws.contains l then none else a.level l) = some n at hn
by_cases hl : l β ws
Β· simp at hn; exact absurd hl hn.1
Β· have : ws.contains l = false := by simpa using hl
simp only [this, Bool.false_eq_true, βreduceIte] at hn; rw [f1 l hl]; exact h2 l n hn
Β· rw [f3]; exact h3 hp
Β· show SaveRel (ws.map ASave.touched ++ a.save) s'.save
rw [e]; exact saveRel_summary h4 pre q
Β· show CondRel a.cond s'.cond; rw [f4]; exact h5
Β· show InputRel a.input s'.input; rw [f5]; exact h6
Β· show s'.mode.kind β a.modes; rw [f6]; exact h7
/-! ## 5. Undecided conditionals: fork, narrow, join at \fi -/
/-- branch_sound (CHANGES.md item 6): the concrete answer cb has an arm. -/
theorem branch_sound (test : AState β Option Bool) (narrow : Bool β AState β Option AState)
(a : AState) (s : State) (cb : Bool) (ha : a.Ξ³ s)
(htest : β b, test a = some b β cb = b)
(hnarrow : test a = none β β a', narrow cb a = some a' β§ a'.Ξ³ s) :
β a', (cb, a') β branch test narrow a β§ a'.Ξ³ s := by
unfold branch
cases ht : test a with
| some b =>
have e := htest b ht
subst e
exact β¨a, List.mem_singleton.mpr rfl, haβ©
| none =>
obtain β¨a', hn, hΞ³β© := hnarrow ht
cases cb with
| true =>
rw [hn]
exact β¨a', List.mem_append.mpr (Or.inl (List.mem_singleton.mpr rfl)), hΞ³β©
| false =>
rw [hn]
exact β¨a', List.mem_append.mpr (Or.inr (List.mem_singleton.mpr rfl)), hΞ³β©
/-- \ifnum/\ifdim by interval (Num::compare): a decided answer holds
for every member. This discharges branch_sound's test hypothesis. -/
theorem compare_sound (i : Itv) (r : Itv.Rel) (k : Int) (b : Bool) (n : Int)
(h : i.compare r (Itv.exact k) = some b) (hn : i.Ξ³ n) : r.holds n k = b := by
obtain β¨h1, h2β© := hn
cases r with
| lt =>
simp only [Itv.compare, Itv.exact] at h
by_cases c1 : i.hi < k
Β· simp only [c1, βreduceIte] at h; cases h; simp only [Itv.Rel.holds, decide_eq_true_eq]; omega
Β· simp only [c1, βreduceIte] at h
by_cases c2 : k β€ i.lo
Β· simp only [c2, βreduceIte] at h; cases h; simp only [Itv.Rel.holds, decide_eq_false_iff_not]; omega
Β· simp only [c2, βreduceIte] at h; cases h
| gt =>
simp only [Itv.compare, Itv.exact] at h
by_cases c1 : k < i.lo
Β· simp only [c1, βreduceIte] at h; cases h; simp only [Itv.Rel.holds, decide_eq_true_eq]; omega
Β· simp only [c1, βreduceIte] at h
by_cases c2 : i.hi β€ k
Β· simp only [c2, βreduceIte] at h; cases h; simp only [Itv.Rel.holds, decide_eq_false_iff_not]; omega
Β· simp only [c2, βreduceIte] at h; cases h
| eq =>
simp only [Itv.compare, Itv.exact] at h
split at h
Β· cases h; simp only [Itv.Rel.holds, decide_eq_true_eq]; omega
Β· by_cases c2 : i.hi < k β¨ k < i.lo
Β· simp only [c2, βreduceIte] at h; cases h; simp only [Itv.Rel.holds, decide_eq_false_iff_not]; omega
Β· simp only [c2, βreduceIte] at h; cases h
theorem narrow_some {lo hi n : Int} (h1 : lo β€ n) (h2 : n β€ hi) :
β j, (if lo β€ hi then some (β¨lo, hiβ© : Itv) else none) = some j β§ j.Ξ³ n :=
β¨β¨lo, hiβ©, by simp only [show lo β€ hi by omega, βreduceIte], h1, h2β©
/-- Num::narrow: the member's value survives on the arm its answer takes. -/
theorem narrow_sound (i : Itv) (r : Itv.Rel) (k n : Int) (hn : i.Ξ³ n) :
β j, i.narrow r k (r.holds n k) = some j β§ j.Ξ³ n := by
obtain β¨h1, h2β© := hn
cases r
Β· by_cases c : n < k
Β· rw [show Itv.Rel.holds .lt n k = true by simp [Itv.Rel.holds, c]]
exact narrow_some (lo := i.lo) (hi := min i.hi (k - 1)) (by omega) (by omega)
Β· rw [show Itv.Rel.holds .lt n k = false by simp [Itv.Rel.holds, c]]
exact narrow_some (lo := max i.lo k) (hi := i.hi) (by omega) (by omega)
Β· by_cases c : n = k
Β· rw [show Itv.Rel.holds .eq n k = true by simp [Itv.Rel.holds, c]]
exact narrow_some (lo := max i.lo k) (hi := min i.hi k) (by omega) (by omega)
Β· rw [show Itv.Rel.holds .eq n k = false by simp [Itv.Rel.holds, c]]
exact narrow_some (lo := if i.lo = k then i.lo + 1 else i.lo)
(hi := if i.hi = k then i.hi - 1 else i.hi) (by split <;> omega) (by split <;> omega)
Β· by_cases c : k < n
Β· rw [show Itv.Rel.holds .gt n k = true by simp [Itv.Rel.holds, c]]
exact narrow_some (lo := max i.lo (k + 1)) (hi := i.hi) (by omega) (by omega)
Β· rw [show Itv.Rel.holds .gt n k = false by simp [Itv.Rel.holds, c]]
exact narrow_some (lo := i.lo) (hi := min i.hi k) (by omega) (by omega)
/-- ifNumTest: a decided answer is TeX's (Β§ 503). -/
theorem ifNumTest_sound {a : AState} {s : State} {l : Loc} {r : Itv.Rel} {k n : Int} {b : Bool}
(ha : a.Ξ³ s) (hv : (s.eqtb l).val = .int n) (h : ifNumTest l r k a = some b) :
r.holds n k = b := by
have hl := ha.1 l
rw [hv] at hl
unfold ifNumTest at h
cases e : a.eqtb l with
| ints ms =>
rw [e] at h hl
obtain β¨i, m, hi, hm, hΞ³β© := hl
cases hm
cases ms with
| nil => cases hi
| cons i0 rest =>
simp only [List.map_cons] at h
split at h
Β· rename_i b0 rs heq
simp only [List.cons.injEq] at heq
obtain β¨e1, e2β© := heq
split at h
Β· rename_i hall
cases h
rcases List.mem_cons.mp hi with hi | hi
Β· subst hi; exact compare_sound _ _ _ _ _ e1 hΞ³
Β· subst e2
have := (List.all_eq_true.mp hall) _ (List.mem_map_of_mem hi)
exact compare_sound _ _ _ _ _ (by simpa using this) hΞ³
Β· cases h
Β· cases h
| _ => rw [e] at h; cases h
/-- ifNumNarrow: the state narrowed to TeX's answer still covers s. -/
theorem ifNumNarrow_sound {a : AState} {s : State} {l : Loc} {r : Itv.Rel} {k n : Int}
(ha : a.Ξ³ s) (hv : (s.eqtb l).val = .int n) :
β a', ifNumNarrow l r k (r.holds n k) a = some a' β§ a'.Ξ³ s := by
have hl := ha.1 l
rw [hv] at hl
unfold ifNumNarrow
cases e : a.eqtb l with
| ints ms =>
rw [e] at hl
obtain β¨i, m, hi, hm, hΞ³β© := hl
cases hm
obtain β¨j, hj, hjΞ³β© := narrow_sound i r k n hΞ³
have hmem : j β ms.filterMap (fun i => i.narrow r k (r.holds n k)) :=
List.mem_filterMap.mpr β¨i, hi, hjβ©
have hne : (ms.filterMap (fun i => i.narrow r k (r.holds n k))).isEmpty = false := by
cases hq : ms.filterMap (fun i => i.narrow r k (r.holds n k)) with
| nil => rw [hq] at hmem; cases hmem
| cons _ _ => rfl
simp only [hne, Bool.false_eq_true, βreduceIte]
refine β¨_, rfl, ?_β©
obtain β¨h1, h2, h3, h4, h5, h6, h7β© := ha
refine β¨fun l' => ?_, h2, h3, h4, h5, h6, h7β©
by_cases hl' : l' = l
Β· subst hl'; simp only [upd_same, hv]; exact β¨j, n, hmem, rfl, hjΞ³β©
Β· simp only [upd_other hl']; exact h1 l'
| _ => exact β¨a, rfl, haβ©
/-- \ifnumβ¨locβ©β¨relβ©β¨kβ© by branch: TeX's answer has an arm that covers
the state. branch_sound with both hypotheses discharged. -/
theorem ifNum_branch_sound {a : AState} {s : State} {l : Loc} {r : Itv.Rel} {k n : Int}
(ha : a.Ξ³ s) (hv : (s.eqtb l).val = .int n) :
β a', (r.holds n k, a') β branch (ifNumTest l r k) (ifNumNarrow l r k) a β§ a'.Ξ³ s :=
branch_sound _ _ a s _ ha (fun _ h => ifNumTest_sound ha hv h)
(fun _ => ifNumNarrow_sound ha hv)
/-- AState.join_sound (CHANGES.md item 10): arms meeting at \fi in the
same stacks join soundly. -/
theorem astate_join_sound (L : Limits) (a b : AState) (s : State)
(hj : a.joinable b) (h : a.Ξ³ s β¨ b.Ξ³ s) : (a.join L b).Ξ³ s := by
obtain β¨hc, hs, hco, hi, hlβ© := hj
rcases h with h | h
Β· obtain β¨h1, h2, h3, h4, h5, h6, h7β© := h
unfold AState.Ξ³
refine β¨?_, h2, fun hf => h3 (by simp only [AState.join, Bool.and_eq_true] at hf; exact hf.1),
h4, h5, h6, ?_β©
Β· intro l; exact aval_join_sound L _ _ _ (Or.inl (h1 l))
Β· show s.mode.kind β a.modes ++ b.modes.filter (fun m => !(a.modes.contains m))
exact List.mem_append.mpr (Or.inl h7)
Β· obtain β¨h1, h2, h3, h4, h5, h6, h7β© := h
unfold AState.Ξ³
refine β¨?_, ?_, ?_, ?_, ?_, ?_, ?_β©
Β· intro l; exact aval_join_sound L _ _ _ (Or.inr (h1 l))
Β· intro l n hn; exact h2 l n ((hl l) βΈ hn)
Β· intro hf
show a.curLevel = s.curLevel
rw [hc]; apply h3; simp only [AState.join, Bool.and_eq_true] at hf; exact hf.2
Β· show SaveRel a.save s.save
rw [hs]; exact h4
Β· show CondRel a.cond s.cond
rw [hco]; exact h5
Β· show InputRel a.input s.input
rw [hi]; exact h6
Β· show s.mode.kind β a.modes ++ b.modes.filter (fun m => !(a.modes.contains m))
by_cases hm : s.mode.kind β a.modes
Β· exact List.mem_append.mpr (Or.inl hm)
Β· refine List.mem_append.mpr (Or.inr (List.mem_filter.mpr β¨h7, ?_β©))
first
| simp [hm]
| simp_all [List.elem_iff]
/-! ## 6. Macro expansion: substitution and known delimiters -/
theorem atext_Ξ³_nil {p : TokList} : AText.Ξ³ [] p β p = [] := Iff.rfl
theorem atext_Ξ³_cons {it : AItem} {rest : AText} {p : TokList} :
AText.Ξ³ (it :: rest) p β β pβ pβ, p = pβ ++ pβ β§ it.Ξ³ pβ β§ AText.Ξ³ rest pβ := Iff.rfl
/-- Abstract texts concatenate like their meanings. -/
theorem atext_Ξ³_append : β (x y : AText) (p q : TokList),
AText.Ξ³ x p β AText.Ξ³ y q β AText.Ξ³ (x ++ y) (p ++ q)
| [], y, p, q, hp, hq => by
have e : p = [] := hp
subst e
exact hq
| it :: rest, y, p, q, hp, hq => by
obtain β¨pβ, pβ, e, h1, h2β© := atext_Ξ³_cons.mp hp
subst e
exact atext_Ξ³_cons.mpr β¨pβ, pβ ++ q, List.append_assoc pβ pβ q, h1,
atext_Ξ³_append rest y pβ q h2 hqβ©
/-- Β§ 390 macro_call: substituting covered arguments into a body covers
the concrete substitution. -/
theorem substitute_sound (args : List AText) (cargs : List TokList)
(hargs : β i, AText.Ξ³ (args.getD i []) (cargs.getD i [])) :
β body : TokList, AText.Ξ³ (substitute args body) (Concrete.subst cargs body)
| [] => atext_Ξ³_nil.mpr rfl
| t :: ts => by
have ih := substitute_sound args cargs hargs ts
cases t with
| outParam n =>
show AText.Ξ³ (args.getD (n - 1) [] ++ substitute args ts)
(cargs.getD (n - 1) [] ++ Concrete.subst cargs ts)
exact atext_Ξ³_append _ _ _ _ (hargs (n - 1)) ih
| char c k =>
show AText.Ξ³ (AItem.tok (.char c k) :: substitute args ts)
(Token.char c k :: Concrete.subst cargs ts)
exact atext_Ξ³_cons.mpr β¨[Token.char c k], Concrete.subst cargs ts, rfl, rfl, ihβ©
| cs r =>
show AText.Ξ³ (AItem.tok (.cs r) :: substitute args ts)
(Token.cs r :: Concrete.subst cargs ts)
exact atext_Ξ³_cons.mpr β¨[Token.cs r], Concrete.subst cargs ts, rfl, rfl, ihβ©
| noexp r =>
show AText.Ξ³ (AItem.tok (.noexp r) :: substitute args ts)
(Token.noexp r :: Concrete.subst cargs ts)
exact atext_Ξ³_cons.mpr β¨[Token.noexp r], Concrete.subst cargs ts, rfl, rfl, ihβ©
/-! ### Brace depth -/
/-- Brace depth after a text, none when a } closes below zero. -/
def walk : Nat β TokList β Option Nat
| n, [] => some n
| n, t :: ts =>
if t.isBgroup then walk (n + 1) ts
else if t.isEgroup then (if n = 0 then none else walk (n - 1) ts)
else walk n ts
theorem bal_walk : β (n : Nat) (ts : TokList), balancedFrom n ts = true β walk n ts = some 0
| n, [] => by simp [balancedFrom, walk]
| n, t :: ts => by
have ih1 := bal_walk (n + 1) ts
have ih2 := bal_walk (n - 1) ts
have ih3 := bal_walk n ts
simp only [balancedFrom, walk]
by_cases hb : t.isBgroup = true
Β· simp only [hb, βreduceIte]; exact ih1
Β· by_cases he : t.isEgroup = true
Β· by_cases hn : n = 0
Β· simp [hb, he, hn]
Β· simp [hb, he, hn, ih2]
Β· simp [hb, he, ih3]
theorem concrete_bal : β (n : Nat) (ts : TokList), Concrete.balancedFrom n ts = balancedFrom n ts
| n, [] => rfl
| n, t :: ts => by
simp only [Concrete.balancedFrom, balancedFrom]
split
Β· exact concrete_bal _ ts
Β· split
Β· split <;> simp_all [concrete_bal]
Β· exact concrete_bal _ ts
theorem walk_append : β (n : Nat) (p q : TokList), walk n (p ++ q) = (walk n p).bind (walk Β· q)
| n, [], q => rfl
| n, t :: p, q => by
simp only [List.cons_append, walk]
split
Β· exact walk_append _ p q
Β· split
Β· split
Β· rfl
Β· exact walk_append _ p q
Β· exact walk_append _ p q
theorem walk_shift : β (p : TokList) (n k m : Nat), walk n p = some m β walk (n + k) p = some (m + k)
| [], n, k, m, h => by simp [walk] at h β’; omega
| t :: p, n, k, m, h => by
simp only [walk] at h β’
by_cases hb : t.isBgroup = true
Β· simp only [hb, βreduceIte] at h β’
rw [show n + k + 1 = n + 1 + k by omega]; exact walk_shift p (n + 1) k m h
Β· by_cases he : t.isEgroup = true
Β· simp only [hb, he, Bool.false_eq_true, βreduceIte] at h β’
by_cases hn : n = 0
Β· simp [hn] at h
Β· simp only [hn, βreduceIte] at h
rw [ite_eq_right_iff.mpr (fun h => absurd h (by omega)), show n + k - 1 = n - 1 + k by omega]
exact walk_shift p (n - 1) k m h
Β· simp only [hb, he, Bool.false_eq_true, βreduceIte] at h β’
exact walk_shift p n k m h
/-- Text without braces leaves the depth as it is. -/
theorem walk_plain : β (p : TokList) (n : Nat),
(β t, t β p β t.isBgroup = false β§ t.isEgroup = false) β walk n p = some n
| [], _, _ => rfl
| t :: p, n, h => by
have β¨h1, h2β© := h t (List.mem_cons_self ..)
simp only [walk, h1, h2, Bool.false_eq_true, βreduceIte]
exact walk_plain p n (fun t' ht => h t' (List.mem_cons_of_mem _ ht))
/-- A prefix of a balanced text that ends at depth 0 is balanced, and so is
the rest. This is why a hole cut at a delimiter leaves balanced pieces. -/
theorem walk_cut {p m : TokList} {d : Nat} (hpm : walk 0 (p ++ m) = some 0)
(hp : walk d p = some 0) : walk 0 p = some 0 β§ walk 0 m = some 0 := by
rw [walk_append] at hpm
cases hr : walk 0 p with
| none => rw [hr] at hpm; cases hpm
| some r =>
rw [hr] at hpm
have := walk_shift p 0 d r hr
rw [Nat.zero_add, hp] at this
have hr0 : r = 0 := by simp at this; omega
subst hr0
exact β¨rfl, hpmβ©
/-! ### Cuts and delimiters -/
theorem printInt_plain (n : Int) : β t, t β printInt n β t.isBgroup = false β§ t.isEgroup = false := by
intro t ht
unfold printInt at ht
split at ht
Β· rcases List.mem_cons.mp ht with e | e
Β· subst e; exact β¨rfl, rflβ©
Β· obtain β¨_, _, eβ© := List.mem_map.mp e; subst e; exact β¨rfl, rflβ©
Β· obtain β¨_, _, eβ© := List.mem_map.mp ht; subst e; exact β¨rfl, rflβ©
theorem mem_prefixes (p m : TokList) : p β prefixes (p ++ m) :=
List.mem_map.mpr β¨p.length, List.mem_range.mpr (by simp; omega), by simpβ©
theorem mem_suffixes (p m : TokList) : m β suffixes (p ++ m) :=
List.mem_map.mpr β¨p.length, List.mem_range.mpr (by simp; omega), by simpβ©
/-- A cut inside an item at brace depth 0 has covered pieces. -/
theorem item_cut (it : AItem) (hnt : β t, it β .tok t) {p m : TokList} {d : Nat}
(h : it.Ξ³ (p ++ m)) (hp : walk d p = some 0) :
β c, c β it.cuts β§ c.1.Ξ³ p β§ c.2.Ξ³ m := by
cases it with
| tok t => exact absurd rfl (hnt t)
| hole hl =>
obtain β¨ha, _, hlen, hbβ© := h
have β¨b1, b2β© := walk_cut ((bal_walk 0 _).mp hb) hp
refine β¨_, List.mem_singleton.mpr rfl, β¨fun t ht => ha t (List.mem_append_left _ ht),
fun h => by simp [Hole.part] at h, fun n hn => ?_, (bal_walk 0 _).mpr b1β©,
β¨fun t ht => ha t (List.mem_append_right _ ht), fun h => by simp [Hole.part] at h,
fun n hn => ?_, (bal_walk 0 _).mpr b2β©β©
Β· have := hlen n hn; simp at this; omega
Β· have := hlen n hn; simp at this; omega
| digits i =>
obtain β¨n, _, eβ© := h
have hpl := printInt_plain n
have hnum := printInt_number n
rw [β e] at hpl hnum
refine β¨_, List.mem_singleton.mpr rfl,
β¨fun t ht => hnum t (List.mem_append_left _ ht), fun h => by simp at h, fun _ h => by simp at h,
(bal_walk 0 _).mpr (walk_plain p 0 (fun t ht => hpl t (List.mem_append_left _ ht)))β©,
β¨fun t ht => hnum t (List.mem_append_right _ ht), fun h => by simp at h, fun _ h => by simp at h,
(bal_walk 0 _).mpr (walk_plain m 0 (fun t ht => hpl t (List.mem_append_right _ ht)))β©β©
| oneOf alts =>
exact β¨_, List.mem_singleton.mpr rfl, List.mem_flatMap.mpr β¨_, h, mem_prefixes p mβ©,
List.mem_flatMap.mpr β¨_, h, mem_suffixes p mβ©β©
theorem nil_cut : β xs : AText, ([], xs) β xs.cuts
| [] => List.mem_singleton.mpr rfl
| _ :: _ => List.mem_cons_self ..
/-- Every concrete cut at brace depth 0 has an abstract cut that covers it. -/
theorem cuts_sound : β (x : AText) (d : Nat) (p q : TokList),
AText.Ξ³ x (p ++ q) β walk d p = some 0 β β c, c β x.cuts β§ AText.Ξ³ c.1 p β§ AText.Ξ³ c.2 q
| [], d, p, q, h, _ => by
have e : p ++ q = [] := h
obtain β¨e1, e2β© := List.append_eq_nil_iff.mp e
subst e1 e2
exact β¨([], []), List.mem_singleton.mpr rfl, rfl, rflβ©
| it :: xs, d, p, q, h, hw => by
obtain β¨pβ, pβ, e, h1, h2β© := h
by_cases hp0 : p = []
Β· subst hp0
exact β¨([], it :: xs), List.mem_cons_self .., rfl, β¨pβ, pβ, e, h1, h2β©β©
rcases List.append_eq_append_iff.mp e with β¨m, e1, e2β© | β¨m, e1, e2β©
Β· -- the cut lies inside it: pβ = p ++ m
subst e1 e2
cases it with
| tok t =>
have ht : p ++ m = [t] := h1
have hm : m = [] := by
cases p with
| nil => exact absurd rfl hp0
| cons _ p' =>
simp only [List.cons_append, List.cons.injEq] at ht
exact (List.append_eq_nil_iff.mp ht.2).2
subst hm
rw [List.append_nil] at ht
exact β¨(AItem.tok t :: [], xs), List.mem_cons_of_mem _ (List.mem_append_right _
(List.mem_map.mpr β¨([], xs), nil_cut xs, rflβ©)), β¨p, [], by simp, ht, rflβ©, h2β©
| hole hl => exact inside (.hole hl) (fun _ h => by cases h) h1 hw h2
| digits i => exact inside (.digits i) (fun _ h => by cases h) h1 hw h2
| oneOf alts => exact inside (.oneOf alts) (fun _ h => by cases h) h1 hw h2
Β· -- the cut lies in xs: p = pβ ++ m
subst e1
rw [walk_append] at hw
cases hd : walk d pβ with
| none => rw [hd] at hw; cases hw
| some d' =>
rw [hd] at hw
obtain β¨c, hc, hc1, hc2β© := cuts_sound xs d' m q (e2 βΈ h2) hw
exact β¨(it :: c.1, c.2), List.mem_cons_of_mem _ (List.mem_append_right _
(List.mem_map.mpr β¨c, hc, rflβ©)), β¨pβ, m, rfl, h1, hc1β©, hc2β©
where
inside (it : AItem) (hnt : β t, it β .tok t) {p m pβ : TokList} {xs : AText} {d : Nat}
(h1 : it.Ξ³ (p ++ m)) (hw : walk d p = some 0) (h2 : AText.Ξ³ xs pβ) :
β c, c β AText.cuts (it :: xs) β§ AText.Ξ³ c.1 p β§ AText.Ξ³ c.2 (m ++ pβ) := by
obtain β¨c, hc, hc1, hc2β© := item_cut it hnt h1 hw
exact β¨([c.1], c.2 :: xs), List.mem_cons_of_mem _ (List.mem_append_left _
(List.mem_map.mpr β¨c, hc, rflβ©)), β¨p, [], by simp, hc1, rflβ©, β¨m, pβ, rfl, hc2, h2β©β©
/-- Delimiters and leading tokens hold no braces (Β§ 473: a { ends the
parameter text, a } there is an error). -/
def NoBraces (Ξ΄ : TokList) : Prop := β t, t β Ξ΄ β t.isBgroup = false β§ t.isEgroup = false
/-- The step of dropAbs_sound at an item that is not a known token. -/
theorem dropAbs_item (it : AItem) (hnt : β t, it β .tok t) (x : AText) (d : Token) (ds rest : TokList)
(hΞ΄ : NoBraces (d :: ds)) (h : AText.Ξ³ (it :: x) (d :: ds ++ rest))
(ih : β Ξ΄ rest, NoBraces Ξ΄ β AText.Ξ³ x (Ξ΄ ++ rest) β β r, r β dropAbs Ξ΄ x β§ AText.Ξ³ r rest)
(heq : dropAbs (d :: ds) (it :: x) = it.cuts.map (fun c => c.2 :: x) ++
(List.range (ds.length + 2)).flatMap (fun i => dropAbs ((d :: ds).drop i) x)) :
β r, r β dropAbs (d :: ds) (it :: x) β§ AText.Ξ³ r rest := by
rw [heq]
obtain β¨pβ, pβ, e, h1, h2β© := h
rcases List.append_eq_append_iff.mp e.symm with β¨m, e1, e2β© | β¨m, e1, e2β©
Β· -- it holds a part of Ξ΄ whole: Ξ΄ = pβ ++ m
obtain β¨r, hr, hΞ³β© := ih m rest (fun t ht => hΞ΄ t (e1 βΈ List.mem_append_right _ ht))
(e2 βΈ h2)
refine β¨r, List.mem_append_right _ (List.mem_flatMap.mpr β¨pβ.length, ?_, ?_β©), hΞ³β©
Β· have : (d :: ds).length = pβ.length + m.length := by rw [e1]; simp
exact List.mem_range.mpr (by simp at this; omega)
Β· rw [e1]; simpa using hr
Β· -- Ξ΄ ends inside it: pβ = Ξ΄ ++ m
subst e1
obtain β¨c, hc, _, hc2β© := item_cut it hnt (d := 0) h1 (walk_plain _ 0 hΞ΄)
exact β¨c.2 :: x, List.mem_append_left _ (List.mem_map.mpr β¨c, hc, rflβ©),
β¨m, pβ, e2 βΈ rfl, hc2, h2β©β©
/-- Known tokens at the head of covered text: some rest covers what follows. -/
theorem dropAbs_sound : β (x : AText) (Ξ΄ rest : TokList), NoBraces Ξ΄ β
AText.Ξ³ x (Ξ΄ ++ rest) β β r, r β dropAbs Ξ΄ x β§ AText.Ξ³ r rest
| x, [], rest, _, h => β¨x, by cases x <;> exact List.mem_singleton.mpr rfl, hβ©
| [], d :: ds, rest, _, h => by have : d :: ds ++ rest = [] := h; simp at this
| .tok t :: x, d :: ds, rest, hΞ΄, h => by
obtain β¨pβ, pβ, e, h1, h2β© := h
have e1 : pβ = [t] := h1
subst e1
simp only [List.cons_append, List.cons.injEq] at e
obtain β¨e1, e2β© := e
subst e1
obtain β¨r, hr, hΞ³β© := dropAbs_sound x ds rest (fun t ht => hΞ΄ t (List.mem_cons_of_mem _ ht))
(by simp at e2; exact e2 βΈ h2)
exact β¨r, by simp only [dropAbs, βreduceIte]; exact hr, hΞ³β©
| .hole hl :: x, d :: ds, rest, hΞ΄, h =>
dropAbs_item (.hole hl) (fun _ h => by cases h) x d ds rest hΞ΄ h
(fun Ξ΄ r hΞ΄' h' => dropAbs_sound x Ξ΄ r hΞ΄' h') rfl
| .digits i :: x, d :: ds, rest, hΞ΄, h =>
dropAbs_item (.digits i) (fun _ h => by cases h) x d ds rest hΞ΄ h
(fun Ξ΄ r hΞ΄' h' => dropAbs_sound x Ξ΄ r hΞ΄' h') rfl
| .oneOf alts :: x, d :: ds, rest, hΞ΄, h =>
dropAbs_item (.oneOf alts) (fun _ h => by cases h) x d ds rest hΞ΄ h
(fun Ξ΄ r hΞ΄' h' => dropAbs_sound x Ξ΄ r hΞ΄' h') rfl
/-- scanDelim_sound (CHANGES item 7): TeX's delimited argument (Β§ 392, the
shortest balanced text before the delimiter) and the rest after the
delimiter are covered by a pair the abstract scan lists, also over typed
holes, digits and one-of tokens (REVIEW-2 R2-1, R2-6: the delimiter may
start in one item and end in a later one). -/
theorem scanDelim_sound {x : AText} {arg Ξ΄ rest : TokList} (hΞ΄ : NoBraces Ξ΄)
(h : AText.Ξ³ x (arg ++ Ξ΄ ++ rest)) (harg : Concrete.firstDelim arg Ξ΄) :
β c, c β scanDelim Ξ΄ x β§ AText.Ξ³ c.1 arg β§ AText.Ξ³ c.2 rest := by
have hb : walk 0 arg = some 0 := by
have := harg.1; unfold Concrete.balanced at this; rw [concrete_bal] at this
exact (bal_walk 0 arg).mp this
rw [List.append_assoc] at h
obtain β¨c, hc, hc1, hc2β© := cuts_sound x 0 arg (Ξ΄ ++ rest) h hb
obtain β¨r, hr, hΞ³β© := dropAbs_sound c.2 Ξ΄ rest hΞ΄ hc2
exact β¨(c.1, r), List.mem_flatMap.mpr β¨c, hc, List.mem_map.mpr β¨r, hr, rflβ©β©, hc1, hΞ³β©
/-- Β§ 400: the argument after brace stripping is covered. -/
theorem stripBraces_sound {xa : AText} {ts : TokList} (h : AText.Ξ³ xa ts)
(hb : balanced ts = true) :
β y, y β stripBraces xa β§ AText.Ξ³ y (Concrete.stripBraces ts) := by
unfold Concrete.stripBraces
split
Β· rename_i b rest
split
Β· rename_i e he
split
Β· rename_i hc
simp only [Bool.and_eq_true] at hc
refine β¨AText.hull [xa], List.mem_cons_of_mem _ (List.mem_singleton.mpr rfl), ?_β©
refine β¨rest.dropLast, [], (List.append_nil _).symm, β¨fun t ht => ?_,
(fun h => by simp at h), (fun _ h => by simp at h), ?_β©, rflβ©
Β· have hm : t β b :: rest := List.mem_cons_of_mem _ (List.dropLast_subset _ ht)
obtain β¨it, hit, haβ© := text_admits xa _ h t hm
exact (fold_text_cover [xa] {}).2 xa (List.mem_singleton.mpr rfl) it hit t ha
Β· have := hc.2; unfold Concrete.balanced at this; rw [concrete_bal] at this; exact this
Β· exact β¨xa, List.mem_cons_self .., hβ©
Β· exact β¨xa, List.mem_cons_self .., hβ©
Β· exact β¨xa, List.mem_cons_self .., hβ©
/-- A widened macro (AMeaning.text) has no parameters: its expansion
pushes its body, which Ξ³ covers. -/
theorem text_meaning_body {lg ou pr : Bool} {body : AText} {m : Meaning}
(h : (AMeaning.text lg ou pr body).Ξ³ m) :
β mc : Macro, m = .call mc β§ mc.params = [] β§ AText.Ξ³ body mc.body := by
obtain β¨mc, e, hp, _, _, _, _, hbβ© := h
exact β¨mc, e, hp, hbβ©
/-! ## 7. Loop heads: k passes, widening, the forced summary -/
/-- The state a round of loopHead joins into the head. -/
def headJoin (L : Limits) (join widen : AState β AState β AState) (body : AState β AState)
(passes : Nat) (a : AState) : AState :=
if passes β₯ L.widenAfter then widen a (join a (body a)) else join a (body a)
theorem loopHead_succ (L : Limits) (join widen : AState β AState β AState)
(grows : AState β AState β Bool) (body summary : AState β AState) (n passes : Nat)
(a : AState) :
loopHead L join widen grows body summary (n + 1) passes a =
(if !grows a (headJoin L join widen body passes a) then a
else if passes β₯ 2 * L.widenAfter then summary (headJoin L join widen body passes a)
else loopHead L join widen grows body summary n (passes + 1)
(headJoin L join widen body passes a)) := rfl
/-- The loop head's result covers its entry and is a post-fixpoint of the
body, whatever the fuel: so it covers every number of passes. -/
theorem loopHead_post (L : Limits) (join widen : AState β AState β AState)
(grows : AState β AState β Bool) (body summary : AState β AState)
(hj : β x y, ALe x (join x y) β§ ALe y (join x y))
(hw : β x y, ALe x (widen x y) β§ ALe y (widen x y))
(hg : β x y, grows x y = false β ALe y x)
(hs : β x, ALe x (summary x) β§ ALe (body (summary x)) (summary x)) :
β fuel passes a,
ALe a (loopHead L join widen grows body summary fuel passes a) β§
ALe (body (loopHead L join widen grows body summary fuel passes a))
(loopHead L join widen grows body summary fuel passes a) := by
intro fuel
induction fuel with
| zero => intro passes a; exact hs a
| succ n ih =>
intro passes a
have hJ : ALe a (headJoin L join widen body passes a) β§
ALe (body a) (headJoin L join widen body passes a) := by
unfold headJoin
refine ite_prop (P := fun r => ALe a r β§ ALe (body a) r) (fun _ => ?_) (fun _ => ?_)
Β· exact β¨ale_trans (hj a (body a)).1 (hw a (join a (body a))).2,
ale_trans (hj a (body a)).2 (hw a (join a (body a))).2β©
Β· exact β¨(hj a (body a)).1, (hj a (body a)).2β©
rw [loopHead_succ]
refine ite_prop (P := fun r => ALe a r β§ ALe (body r) r) (fun hgr => ?_) (fun _ => ?_)
Β· have hf : grows a (headJoin L join widen body passes a) = false := by
revert hgr
cases grows a (headJoin L join widen body passes a) <;> decide
exact β¨ale_refl a, ale_trans hJ.2 (hg _ _ hf)β©
Β· refine ite_prop (P := fun r => ALe a r β§ ALe (body r) r) (fun _ => ?_) (fun _ => ?_)
Β· exact β¨ale_trans hJ.1 (hs _).1, (hs _).2β©
Β· obtain β¨h1, h2β© := ih (passes + 1) (headJoin L join widen body passes a)
exact β¨ale_trans hJ.1 h1, h2β©
/-- Loop soundness: every concrete run of the body from a covered state
ends covered by the loop head's result. -/
theorem loopHead_sound (L : Limits) (join widen : AState β AState β AState)
(grows : AState β AState β Bool) (body summary : AState β AState)
(Body : State β State β Prop)
(hbody : β x s s', x.Ξ³ s β Body s s' β (body x).Ξ³ s')
(hj : β x y, ALe x (join x y) β§ ALe y (join x y))
(hw : β x y, ALe x (widen x y) β§ ALe y (widen x y))
(hg : β x y, grows x y = false β ALe y x)
(hs : β x, ALe x (summary x) β§ ALe (body (summary x)) (summary x))
(fuel passes : Nat) (a : AState) (s s' : State) (ha : a.Ξ³ s) (hrun : Star Body s s') :
(loopHead L join widen grows body summary fuel passes a).Ξ³ s' := by
have hp := loopHead_post L join widen grows body summary hj hw hg hs fuel passes a
induction hrun with
| refl => exact hp.1 _ ha
| tail _ hbc ih => exact hp.2 _ (hbody _ _ _ ih hbc)
/-- The never-hang half of a loop: at most fuel + 1 rounds (job 2's
loopRounds_le), whatever the precision. -/
theorem loopHead_rounds (L : Limits) (join widen : AState β AState β AState)
(grows : AState β AState β Bool) (body : AState β AState) (fuel passes : Nat) (a : AState) :
loopRounds L join widen grows body fuel passes a β€ fuel + 1 :=
loopRounds_le L join widen grows body fuel passes a
/-! ## 8. Global theorems
Intuition. Soundness of a whole run is induction on the concrete trace:
each concrete step from a covered state has a covering abstract run
(run_covered). The local obligation is split by family. A step first
reads a command and its operands (the mouth and the scanners), then runs
the command's effect. The effects of assignment and grouping are proven
below (fam_covered); conditionals, loops and delimited arguments have
their lemmas above. What is left is the abstract reading itself and the
families without a transfer (PROOF-STATUS "Open"). -/
/-- T covers every step of R that starts in a covered state. -/
def Covered {A C : Type} (Ξ³ : A β C β Prop) (T : A β A β Prop) (R : C β C β Prop) : Prop :=
β a c c', Ξ³ a c β R c c' β β a', Star T a a' β§ Ξ³ a' c'
/-- Families combine: a step in one of them is covered. -/
theorem covered_or {A C : Type} {Ξ³ : A β C β Prop} {T : A β A β Prop} {R S : C β C β Prop}
(hR : Covered Ξ³ T R) (hS : Covered Ξ³ T S) : Covered Ξ³ T (fun c c' => R c c' β¨ S c c') :=
fun a c c' ha h => h.elim (hR a c c' ha) (hS a c c' ha)
/-- A read, then an effect: covered when both are. -/
theorem covered_comp {A C : Type} {Ξ³ : A β C β Prop} {T : A β A β Prop} {R S : C β C β Prop}
(hR : Covered Ξ³ T R) (hS : Covered Ξ³ T S) :
Covered Ξ³ T (fun c c'' => β c', R c c' β§ S c' c'') := by
intro a c c'' ha β¨c', h1, h2β©
obtain β¨a1, t1, g1β© := hR a c c' ha h1
obtain β¨a2, t2, g2β© := hS a1 c' c'' g1 h2
exact β¨a2, Star.trans t1 t2, g2β©
/-- A command as TeX reads it, with its operand: an assignment (Β§ 1211),
\begingroup/{ (Β§ 1063), a group's end (Β§ 1086), or none: the step's
whole change is in its reading (expansion, \relax, a test). -/
inductive CCmd where
| assign (glob : Bool) (l : Loc) (x : Val)
| begin (c : GroupCode) (ctx : Option BoxContext)
| close (c : GroupCode)
| none
/-- The same command as the abstract run reads it. -/
inductive ACmd where
| assign (glob : Bool) (l : Loc) (v : AVal)
| begin (c : GroupCode)
| close (c : GroupCode)
| none
def ACmd.covers : ACmd β CCmd β Prop
| .assign g l v, .assign g' l' x => g = g' β§ l = l' β§ v.Ξ³ x
| .begin c, .begin c' _ => c = c'
| .close c, .close c' => c = c'
| .none, .none => True
| _, _ => False
/-- The concrete effect of a command. glob is the globality after
\globaldefs (Concrete.isGlobal, Β§ 1214). -/
inductive Effect : State β CCmd β State β Prop
| assign {s : State} {glob : Bool} {l : Loc} {x : Val} :
Effect s (.assign glob l x)
(if glob then Concrete.geqDefine s l x else Concrete.eqDefine s l x)
| begin {s : State} {c : GroupCode} {ctx : Option BoxContext} :
Effect s (.begin c ctx) (Concrete.newSaveLevel c ctx s)
| close {s : State} {c : GroupCode} {E : Loc β Entry} {ins : TokList} {ctx : Option BoxContext}
{crest : List SaveEntry} :
Concrete.unsaveAux s.eqtb [] s.save = (E, ins, some (c, ctx), crest) β
Effect s (.close c) (Concrete.unsave s).1
| none {s : State} : Effect s .none s
/-- The abstract effect: the model's transfers. -/
def aeffect (L : Limits) (a : AState) : ACmd β List AState
| .assign glob l v => [a.assign glob l v]
| .begin c => [a.beginGroup c]
| .close c => a.endGroup L c
| .none => [a]
/-- The abstract step: read a command (read, the abstract mouth and
scanners), then run its effect. -/
def AStep (L : Limits) (read : AState β List (AState Γ ACmd)) (a b : AState) : Prop :=
β a1 k, (a1, k) β read a β§ b β aeffect L a1 k
/-- What the abstract reading must satisfy: it covers TeX's reading, state
and command. -/
def ReadCovers (Read : State β State β CCmd β Prop) (read : AState β List (AState Γ ACmd)) : Prop :=
β a s s1 k, a.Ξ³ s β Read s s1 k β β a1 k', (a1, k') β read a β§ a1.Ξ³ s1 β§ k'.covers k
/-- Β§ 274 new_save_level. -/
theorem beginGroup_sound {a : AState} {s : State} (ha : a.Ξ³ s) (c : GroupCode)
(ctx : Option BoxContext) : (a.beginGroup c).Ξ³ (Concrete.newSaveLevel c ctx s) := by
obtain β¨h1, h2, h3, h4, h5, h6, h7β© := ha
exact β¨h1, h2, fun hp => by simp only [AState.beginGroup, Concrete.newSaveLevel]; rw [h3 hp],
SaveRel.group h4, h5, h6, h7β©
/-- Every effect of a command is covered by the abstract effect of a
covering command. -/
theorem effect_sound (L : Limits) {a : AState} {s s' : State} {k : CCmd} {k' : ACmd}
(ha : a.Ξ³ s) (hk : k'.covers k) (h : Effect s k s') : β b, b β aeffect L a k' β§ b.Ξ³ s' := by
cases h with
| @assign glob l x =>
cases k' with
| assign g l' v =>
obtain β¨e1, e2, hvβ© := hk; subst e1 e2
refine β¨_, List.mem_singleton.mpr rfl, ?_β©
cases g
Β· exact assign_local_sound ha hv
Β· exact assign_global_sound ha hv
| _ => cases hk
| @begin c ctx =>
cases k' with
| begin c' => subst hk; exact β¨_, List.mem_singleton.mpr rfl, beginGroup_sound ha _ _β©
| _ => cases hk
| @close c E ins ctx crest hu =>
cases k' with
| close c' => subst hk; exact endGroup_sound L ha hu
| _ => cases hk
| none =>
cases k' with
| none => exact β¨_, List.mem_singleton.mpr rfl, haβ©
| _ => cases hk
/-- The families with a transfer are covered, given the abstract reading. -/
theorem fam_covered (L : Limits) (Read : State β State β CCmd β Prop)
(read : AState β List (AState Γ ACmd)) (hread : ReadCovers Read read) :
Covered AState.Ξ³ (AStep L read) (fun s s' => β s1 k, Read s s1 k β§ Effect s1 k s') := by
intro a s s' ha β¨s1, k, hr, heβ©
obtain β¨a1, k', hm, h1, hkβ© := hread a s s1 k ha hr
obtain β¨b, hb, hΞ³β© := effect_sound L h1 hk he
exact β¨b, Star.single β¨a1, k', hm, hbβ©, hΞ³β©
/-- The abstract run covers every concrete run (by induction on the
concrete trace), given local soundness of the abstract transfer T for
job 1's Step. -/
theorem run_covered (E : Concrete.Engine) (T : AState β AState β Prop)
(hloc : Covered AState.Ξ³ T (Concrete.Step E)) :
β s s', Concrete.Steps E s s' β β a, a.Ξ³ s β β a', Star T a a' β§ a'.Ξ³ s' := by
intro s s' h
induction h with
| refl => intro a ha; exact β¨a, Star.refl, haβ©
| step h1 _ ih =>
intro a ha
obtain β¨a1, ht, h1'β© := hloc a _ _ ha h1
obtain β¨a', ht', h'β© := ih a1 h1'
exact β¨a', Star.trans ht ht', h'β©
/-- The run theorem by families: a step is a read command with a modelled
effect, or of another family Other whose coverage is still an obligation
(PROOF-STATUS lists them). -/
theorem run_covered_families (E : Concrete.Engine) (L : Limits)
(Read : State β State β CCmd β Prop) (read : AState β List (AState Γ ACmd))
(hread : ReadCovers Read read) (Other : State β State β Prop)
(hother : Covered AState.Ξ³ (AStep L read) Other)
(hsplit : β s s', Concrete.Step E s s' β
(β s1 k, Read s s1 k β§ Effect s1 k s') β¨ Other s s') :
β s s', Concrete.Steps E s s' β β a, a.Ξ³ s β β a', Star (AStep L read) a a' β§ a'.Ξ³ s' :=
run_covered E (AStep L read) (fun a s s' ha h =>
covered_or (fam_covered L Read read hread) hother a s s' ha (hsplit s s' h))
/-- Paths: a transfer that maps a path to its successor paths. -/
theorem paths_covered (E : Concrete.Engine) (trans : AState β List AState)
(hloc : β a s s', a.Ξ³ s β Concrete.Step E s s' β β a', a' β trans a β§ a'.Ξ³ s') :
β s s', Concrete.Steps E s s' β β a, a.Ξ³ s β
β a', Star (fun x y => y β trans x) a a' β§ a'.Ξ³ s' := by
apply run_covered E
intro a s s' ha hs
obtain β¨a', h1, h2β© := hloc a s s' ha hs
exact β¨a', Star.single h1, h2β©
/-! ### The budget: never hang, and stay sound
Machine::charge pays every step from limits.steps. Modelled: one
counter per run, in the state, that each abstract step decreases; at zero
the run is cut to a state covering everything (none). A budget per run,
not per step or per line (the Rust's line guard is not modelled). Past the
cut bΞ³ is True: nothing is claimed of the rest of the run. Sound only if
the cut really is a top (P6 in PROOF-STATUS.md). -/
inductive BStep {A : Type} (T : A β A β Prop) : Nat Γ Option A β Nat Γ Option A β Prop where
| step {n : Nat} {a a' : A} : T a a' β BStep T (n + 1, some a) (n, some a')
| cut {a : A} : BStep T (0, some a) (0, none)
def bΞ³ {A C : Type} (Ξ³ : A β C β Prop) : Nat Γ Option A β C β Prop
| (_, some a), c => Ξ³ a c
| (_, none), _ => True
def bΞΌ {A : Type} : Nat Γ Option A β Nat
| (n, some _) => n + 1
| (_, none) => 0
theorem bstep_dec {A : Type} (T : A β A β Prop) (x y : Nat Γ Option A)
(h : BStep T x y) : bΞΌ y < bΞΌ x := by
cases h <;> simp only [bΞΌ] <;> omega
/-- An abstract run under the budget: it reaches its end, or the cut. -/
theorem budget_star {A : Type} (T : A β A β Prop) {a b : A} (h : Star T a b) :
β n, β y, Star (BStep T) (n, some a) y β§ ((β m, y = (m, some b)) β¨ y.2 = none) := by
induction h with
| refl => intro n; exact β¨(n, some _), Star.refl, Or.inl β¨n, rflβ©β©
| tail _ hbc ih =>
intro n
obtain β¨y, hy, hbβ© := ih n
rcases hb with β¨m, eβ© | e
Β· subst e
cases m with
| zero => exact β¨(0, none), Star.tail hy BStep.cut, Or.inr rflβ©
| succ m => exact β¨(m, some _), Star.tail hy (BStep.step hbc), Or.inl β¨m, rflβ©β©
Β· exact β¨y, hy, Or.inr eβ©
theorem budget_covers {A C : Type} (Ξ³ : A β C β Prop) (Step : C β C β Prop)
(T : A β A β Prop) (hloc : Covered Ξ³ T Step) :
β x c c', bΞ³ Ξ³ x c β Step c c' β β y, Star (BStep T) x y β§ bΞ³ Ξ³ y c' := by
intro x c c' hx hs
obtain β¨n, oaβ© := x
cases oa with
| none => exact β¨(n, none), Star.refl, True.introβ©
| some a =>
obtain β¨a', ht, hΞ³β© := hloc a c c' hx hs
obtain β¨y, hy, hbβ© := budget_star T ht n
refine β¨y, hy, ?_β©
rcases hb with β¨m, eβ© | e
Β· subst e; exact hΞ³
Β· obtain β¨m, oβ© := y; simp only at e; subst e; exact True.intro
/-- The main theorem: with one budget per run the abstract run covers every
concrete run, and it terminates (no infinite abstract run, and no run
longer than the budget). The cut goes to a state that covers everything:
the budget never makes the run unsound, only coarser, and after the cut
nothing is claimed. Conditional on hloc. -/
theorem sound_and_terminating {A C : Type} (Ξ³ : A β C β Prop) (Step : C β C β Prop)
(T : A β A β Prop) (hloc : Covered Ξ³ T Step) :
(β cβ c, Star Step cβ c β β xβ, bΞ³ Ξ³ xβ cβ β β x, Star (BStep T) xβ x β§ bΞ³ Ξ³ x c) β§
WellFounded (fun y x => BStep T x y) β§
(β n x y, StarN (BStep T) x n y β n + bΞΌ y β€ bΞΌ x) :=
β¨simulation_star Step (BStep T) (bΞ³ Ξ³) (budget_covers Ξ³ Step T hloc),
wf_of_budget (BStep T) bΞΌ (bstep_dec T),
run_length_le_budget (BStep T) bΞΌ (bstep_dec T)β©
/- The main theorem for SaTeX's model is in Satex.Global: no free
assumption, a fixed reader. -/
end Satex.Soundness