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
|
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
|