summaryrefslogtreecommitdiffhomepage
path: root/formal/zash_spec/ZashSpec/Patterns.lean
blob: 205c58dc41eb5663d47ae2f2e42657673427ed6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/-
  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