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 `ε`. -/ scoped notation:max "ε" => ([] : Bytes) /-- Formal language `Lang := Bytes → Prop` with set membership `w ∈ L`. -/ abbrev Lang := Bytes → Prop instance : Membership Bytes Lang where mem L w := L w /-- R-PAT-4: Inductive matching relation `Matches p s`. s ∈ ℒ(ε) ⟺ s = ε s ∈ ℒ(* p') ⟺ ∃ i, s[i..] ∈ ℒ(p') c s' ∈ ℒ(e p') ⟺ (e matches c) ∧ s' ∈ ℒ(p') -/ 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) /-- Language `ℒ(p) = { s | Matches p s }` recognised by pattern `p`. -/ scoped notation:max "ℒ(" p ")" => (Matches p : Lang) /-- Executable matcher `⟦p⟧(s) ∈ Bool` 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 /-- Soundness: ∀ p s, ⟦p⟧(s) = true → s ∈ ℒ(p) -/ 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₂) /-- Completeness: ∀ p s, s ∈ ℒ(p) → ⟦p⟧(s) = true -/ 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] /-- Equivalence: ∀ p s, ⟦p⟧(s) = true ⟺ s ∈ ℒ(p) -/ theorem matchBool_iff : ∀ (p : Pattern) (s : Bytes), ⟦p⟧(s) = true ↔ s ∈ ℒ(p) := fun p s => ⟨matchBool_sound p s, matchBool_complete⟩ /-- Search for the smallest `m ≥ k` such that `s[..m] ∈ ℒ(p)`. -/ def findShortestFrom (p : Pattern) (s : Bytes) (k : Nat) : Option Nat := if _ : k ≤ s.length then if ⟦p⟧(s.take k) then some k else findShortestFrom p s (k + 1) else none termination_by s.length + 1 - k /-- R-EXP-8 (`${x#p}`): remove the shortest prefix of `s` in `ℒ(p)`. -/ def trimShortestPrefix (p : Pattern) (s : Bytes) : Bytes := match findShortestFrom p s 0 with | some k => s.drop k | none => s /-- Soundness and minimality of `findShortestFrom`: ∀ p s k m, findShortestFrom p s k = some m → k ≤ m ≤ |s| ∧ s[..m] ∈ ℒ(p) ∧ ∀ j ∈ [k, m), s[..j] ∉ ℒ(p) -/ theorem findShortestFrom_spec (p : Pattern) (s : Bytes) (k m : Nat) : findShortestFrom p s k = some m → k ≤ m ∧ m ≤ s.length ∧ s.take m ∈ ℒ(p) ∧ ∀ j, k ≤ j → j < m → s.take j ∉ ℒ(p) := by unfold findShortestFrom split · rename_i hle split · rename_i hm intro heq injection heq with hkm subst hkm refine ⟨Nat.le_refl _, hle, (matchBool_iff p _).mp hm, ?_⟩ intro j hkj hjk omega · rename_i hnm intro hrec have ih := findShortestFrom_spec p s (k + 1) m hrec refine ⟨by omega, ih.2.1, ih.2.2.1, ?_⟩ intro j hkj hjm hmatch by_cases hjk : j = k · subst hjk exact hnm ((matchBool_iff p _).mpr hmatch) · exact ih.2.2.2 j (by omega) hjm hmatch · intro h contradiction termination_by s.length + 1 - k /-- No-match completeness: ∀ p s k, findShortestFrom p s k = none → ∀ j ∈ [k, |s|], s[..j] ∉ ℒ(p) -/ theorem findShortestFrom_none (p : Pattern) (s : Bytes) (k : Nat) : findShortestFrom p s k = none → ∀ j, k ≤ j → j ≤ s.length → s.take j ∉ ℒ(p) := by unfold findShortestFrom split · rename_i hle split · intro h contradiction · rename_i hnm intro hrec j hkj hjle hmatch by_cases hjk : j = k · subst hjk exact hnm ((matchBool_iff p _).mpr hmatch) · exact findShortestFrom_none p s (k + 1) hrec j (by omega) hjle hmatch · rename_i hgt intro _ j hkj hjle omega termination_by s.length + 1 - k /-- R-EXP-8 (`${x#p}`): Either there exists a shortest matching prefix `m ≤ |s|` with `s[..m] ∈ ℒ(p)` and `trimShortestPrefix p s = s[m..]`, or `∀ j ∈ {0..|s|}, s[..j] ∉ ℒ(p)` and `s` is unchanged. -/ 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 ∈ List.range (s.length + 1), s.take j ∉ ℒ(p)) ∧ trimShortestPrefix p s = s) := by intro p s unfold trimShortestPrefix cases h : findShortestFrom p s 0 with | some m => left have hspec := findShortestFrom_spec p s 0 m h refine ⟨m, hspec.2.1, hspec.2.2.1, ?_, rfl⟩ intro j hj exact hspec.2.2.2 j (Nat.zero_le j) hj | none => right refine ⟨?_, rfl⟩ intro j hj have hle : j ≤ s.length := by simp [List.mem_range] at hj omega exact findShortestFrom_none p s 0 h j (Nat.zero_le j) hle end ZashSpec.Patterns