/- zash specification, chapter Patterns (R-PAT-4) and trimming (R-EXP-9). NOT YET COMPILED: written without a Lean toolchain at hand. -/ namespace ZashSpec.Patterns inductive PatElem where | lit (b : UInt8) | any | star deriving Repr, DecidableEq abbrev Pattern := List PatElem abbrev Bytes := List UInt8 /-- Empty byte string `ε`. (Not `∈`: that would hijack membership.) -/ scoped notation:max "ε" => ([] : Bytes) /-- Formal language `Lang := Bytes → Prop`, with `w ∈ L`. -/ abbrev Lang := Bytes → Prop instance : Membership Bytes Lang where mem L w := L w /-- R-PAT-4: the matching relation. -/ inductive Matches : Pattern → Bytes → Prop where | nil : Matches [] ε | lit (b : UInt8) (p : Pattern) (s : Bytes) : Matches p s → Matches (.lit b :: p) (b :: s) | any (c : UInt8) (p : Pattern) (s : Bytes) : Matches p s → Matches (.any :: p) (c :: s) | star_empty (p : Pattern) (s : Bytes) : Matches p s → Matches (.star :: p) s | star_cons (c : UInt8) (p : Pattern) (s : Bytes) : Matches (.star :: p) s → Matches (.star :: p) (c :: s) scoped notation:max "ℒ(" p ")" => (Matches p : Lang) /-- Executable matcher for R-PAT-4. -/ def matchBool : Pattern → Bytes → Bool | [], [] => true | [], _ :: _ => false | .lit b :: p, c :: s => b == c && matchBool p s | .lit _ :: _, [] => false | .any :: p, _ :: s => matchBool p s | .any :: _, [] => false | .star :: p, [] => matchBool p [] | .star :: p, c :: s => matchBool p (c :: s) || matchBool (.star :: p) s scoped notation:max "⟦" p "⟧(" s ")" => matchBool p s /-! ## 1. The matcher agrees with the relation (unchanged from your file) -/ theorem matchBool_sound : ∀ (p : Pattern) (s : Bytes), ⟦p⟧(s) = true → s ∈ ℒ(p) := by intro p s induction p generalizing s with | nil => cases s with | nil => intro _; exact .nil | cons _ _ => simp [matchBool] | cons e p ih => cases e with | lit b => cases s with | nil => simp [matchBool] | cons c s => simp [matchBool] intro hbc hrec subst hbc exact .lit b p s (ih s hrec) | any => cases s with | nil => simp [matchBool] | cons c s => simp [matchBool] intro hrec exact .any c p s (ih s hrec) | star => induction s with | nil => simp [matchBool] intro hrec exact .star_empty p ε (ih ε hrec) | cons c s ihs => simp [matchBool] intro h cases h with | inl h₁ => exact .star_empty p (c :: s) (ih (c :: s) h₁) | inr h₂ => exact .star_cons c p s (ihs h₂) theorem matchBool_complete : ∀ {p : Pattern} {s : Bytes}, s ∈ ℒ(p) → ⟦p⟧(s) = true := by intro p s h induction h with | nil => simp [matchBool] | lit b p s _ ih => simp [matchBool, ih] | any c p s _ ih => simp [matchBool, ih] | star_empty p s _ ih => cases s with | nil => simp [matchBool, ih] | cons c s => simp [matchBool, ih] | star_cons c p s _ ih => simp [matchBool, ih] theorem matchBool_iff (p : Pattern) (s : Bytes) : ⟦p⟧(s) = true ↔ s ∈ ℒ(p) := ⟨matchBool_sound p s, matchBool_complete⟩ /-- The `false` side, which every "no shorter match" clause needs. -/ theorem not_mem_of_false {p : Pattern} {s : Bytes} (h : ⟦p⟧(s) = false) : s ∉ ℒ(p) := by intro hm rw [matchBool_complete hm] at h contradiction /-! ## 2. Two generic searches over `0..n` All four trims are "find the least or the greatest index whose cut matches". Proving that once, about an arbitrary `q : Nat → Bool`, means each trim's spec is a few lines and never repeats an induction. -/ /-- Least `m` with `k ≤ m ≤ n` and `q m`. -/ def firstFrom (q : Nat → Bool) (n k : Nat) : Option Nat := if _ : k ≤ n then if q k then some k else firstFrom q n (k + 1) else none termination_by n + 1 - k theorem firstFrom_some (q : Nat → Bool) (n k m : Nat) : firstFrom q n k = some m → k ≤ m ∧ m ≤ n ∧ q m = true ∧ ∀ j, k ≤ j → j < m → q j = false := by unfold firstFrom split · rename_i hle split · rename_i hq intro h injection h with h subst h exact ⟨Nat.le_refl _, hle, hq, fun j h1 h2 => by omega⟩ · rename_i hq intro h obtain ⟨h1, h2, h3, h4⟩ := firstFrom_some q n (k + 1) m h refine ⟨by omega, h2, h3, ?_⟩ intro j hkj hjm by_cases hjk : j = k · subst hjk; simpa using hq · exact h4 j (by omega) hjm · intro h; contradiction termination_by n + 1 - k theorem firstFrom_none (q : Nat → Bool) (n k : Nat) : firstFrom q n k = none → ∀ j, k ≤ j → j ≤ n → q j = false := by unfold firstFrom split · rename_i hle split · intro h; contradiction · rename_i hq intro h j hkj hjn by_cases hjk : j = k · subst hjk; simpa using hq · exact firstFrom_none q n (k + 1) h j (by omega) hjn · intro _ j hkj hjn omega termination_by n + 1 - k /-- Greatest `m ≤ k` with `q m`. Structural recursion: no `termination_by`. -/ def lastUpTo (q : Nat → Bool) : Nat → Option Nat | 0 => if q 0 then some 0 else none | k + 1 => if q (k + 1) then some (k + 1) else lastUpTo q k theorem lastUpTo_some (q : Nat → Bool) : ∀ k m, lastUpTo q k = some m → m ≤ k ∧ q m = true ∧ ∀ j, m < j → j ≤ k → q j = false := by intro k induction k with | zero => intro m h simp only [lastUpTo] at h split at h · rename_i hq injection h with h subst h exact ⟨Nat.le_refl _, hq, fun j h1 h2 => by omega⟩ · contradiction | succ k ih => intro m h simp only [lastUpTo] at h split at h · rename_i hq injection h with h subst h exact ⟨Nat.le_refl _, hq, fun j h1 h2 => by omega⟩ · rename_i hq obtain ⟨h1, h2, h3⟩ := ih m h refine ⟨by omega, h2, ?_⟩ intro j hmj hjk by_cases hj : j = k + 1 · subst hj; simpa using hq · exact h3 j hmj (by omega) theorem lastUpTo_none (q : Nat → Bool) : ∀ k, lastUpTo q k = none → ∀ j, j ≤ k → q j = false := by intro k induction k with | zero => intro h j hj simp only [lastUpTo] at h split at h · contradiction · rename_i hq have : j = 0 := by omega subst this simpa using hq | succ k ih => intro h j hj simp only [lastUpTo] at h split at h · contradiction · rename_i hq by_cases hjk : j = k + 1 · subst hjk; simpa using hq · exact ih h j (by omega) /-! ## 3. R-EXP-9: the four trims For suffixes the index `m` is the length of the suffix removed, so the candidate is `s.drop (s.length - m)` and the result `s.take (s.length - m)`. -/ /-- `${x#p}` -/ def trimShortestPrefix (p : Pattern) (s : Bytes) : Bytes := match firstFrom (fun m => ⟦p⟧(s.take m)) s.length 0 with | some m => s.drop m | none => s /-- `${x##p}` -/ def trimLongestPrefix (p : Pattern) (s : Bytes) : Bytes := match lastUpTo (fun m => ⟦p⟧(s.take m)) s.length with | some m => s.drop m | none => s /-- `${x%p}` -/ def trimShortestSuffix (p : Pattern) (s : Bytes) : Bytes := match firstFrom (fun m => ⟦p⟧(s.drop (s.length - m))) s.length 0 with | some m => s.take (s.length - m) | none => s /-- `${x%%p}` -/ def trimLongestSuffix (p : Pattern) (s : Bytes) : Bytes := match lastUpTo (fun m => ⟦p⟧(s.drop (s.length - m))) s.length with | some m => s.take (s.length - m) | none => s theorem trimShortestPrefix_spec (p : Pattern) (s : Bytes) : (∃ m ≤ s.length, s.take m ∈ ℒ(p) ∧ (∀ j < m, s.take j ∉ ℒ(p)) ∧ trimShortestPrefix p s = s.drop m) ∨ ((∀ j ≤ s.length, s.take j ∉ ℒ(p)) ∧ trimShortestPrefix p s = s) := by unfold trimShortestPrefix cases h : firstFrom (fun m => ⟦p⟧(s.take m)) s.length 0 with | some m => obtain ⟨_, hmn, hq, hmin⟩ := firstFrom_some _ _ _ _ h exact Or.inl ⟨m, hmn, matchBool_sound p (s.take m) hq, fun j hj => not_mem_of_false (hmin j (Nat.zero_le j) hj), rfl⟩ | none => exact Or.inr ⟨fun j hj => not_mem_of_false (firstFrom_none _ _ _ h j (Nat.zero_le j) hj), rfl⟩ theorem trimLongestSuffix_spec (p : Pattern) (s : Bytes) : (∃ m ≤ s.length, s.drop (s.length - m) ∈ ℒ(p) ∧ (∀ j, m < j → j ≤ s.length → s.drop (s.length - j) ∉ ℒ(p)) ∧ trimLongestSuffix p s = s.take (s.length - m)) ∨ ((∀ j ≤ s.length, s.drop (s.length - j) ∉ ℒ(p)) ∧ trimLongestSuffix p s = s) := by unfold trimLongestSuffix cases h : lastUpTo (fun m => ⟦p⟧(s.drop (s.length - m))) s.length with | some m => obtain ⟨hmn, hq, hmax⟩ := lastUpTo_some _ _ _ h exact Or.inl ⟨m, hmn, matchBool_sound p (s.drop (s.length - m)) hq, fun j hmj hjn => not_mem_of_false (hmax j hmj hjn), rfl⟩ | none => exact Or.inr ⟨fun j hj => not_mem_of_false (lastUpTo_none _ _ h j hj), rfl⟩ /-- Exercise 1: same shape as `trimLongestSuffix_spec`, with `take`/`drop` swapped back. Use `lastUpTo_some` and `lastUpTo_none`. -/ theorem trimLongestPrefix_spec (p : Pattern) (s : Bytes) : (∃ m ≤ s.length, s.take m ∈ ℒ(p) ∧ (∀ j, m < j → j ≤ s.length → s.take j ∉ ℒ(p)) ∧ trimLongestPrefix p s = s.drop m) ∨ ((∀ j ≤ s.length, s.take j ∉ ℒ(p)) ∧ trimLongestPrefix p s = s) := by sorry /-- Exercise 2: same shape as `trimShortestPrefix_spec`, with suffixes. Use `firstFrom_some` and `firstFrom_none`. -/ theorem trimShortestSuffix_spec (p : Pattern) (s : Bytes) : (∃ m ≤ s.length, s.drop (s.length - m) ∈ ℒ(p) ∧ (∀ j < m, s.drop (s.length - j) ∉ ℒ(p)) ∧ trimShortestSuffix p s = s.take (s.length - m)) ∨ ((∀ j ≤ s.length, s.drop (s.length - j) ∉ ℒ(p)) ∧ trimShortestSuffix p s = s) := by sorry /-! ## 4. Toward the single-pass matcher: concatenation The bitmask algorithm keeps, after each byte, the set of pattern positions `i` such that the input so far matches the first `i` elements. Its invariant is stated with `p.take i`, and proving the step case needs matching to split over `++`. These two lemmas are that tool. -/ theorem Matches.append {p q : Pattern} {s t : Bytes} (hp : Matches p s) (hq : Matches q t) : Matches (p ++ q) (s ++ t) := by induction hp with | nil => simpa using hq | lit b p s _ ih => exact .lit b (p ++ q) (s ++ t) ih | any c p s _ ih => exact .any c (p ++ q) (s ++ t) ih | star_empty p s _ ih => exact .star_empty (p ++ q) (s ++ t) ih | star_cons c p s _ ih => exact .star_cons c (p ++ q) (s ++ t) ih /-- Exercise 3 (the hard direction). Hint: `induction p generalizing s`; `nil` is `⟨[], s, rfl, .nil, h⟩`; for `lit` and `any` use `cases h` and put the head byte on the front of the first part; for `star`, do an inner `induction s` exactly as in `matchBool_sound`. -/ theorem Matches.split {p q : Pattern} {s : Bytes} (h : Matches (p ++ q) s) : ∃ s₁ s₂, s = s₁ ++ s₂ ∧ Matches p s₁ ∧ Matches q s₂ := by sorry end ZashSpec.Patterns