8
|
1 |
\documentclass{article}
|
|
2 |
\usepackage[utf8]{inputenc}
|
|
3 |
\usepackage[english]{babel}
|
|
4 |
\usepackage{listings}
|
|
5 |
\usepackage{amsthm}
|
9
|
6 |
\usepackage{hyperref}
|
8
|
7 |
\usepackage[margin=0.5in]{geometry}
|
|
8 |
|
|
9 |
\theoremstyle{theorem}
|
|
10 |
\newtheorem{theorem}{Theorem}
|
|
11 |
|
|
12 |
\theoremstyle{lemma}
|
|
13 |
\newtheorem{lemma}{Lemma}
|
|
14 |
|
9
|
15 |
\newcommand{\lemmaautorefname}{Lemma}
|
|
16 |
|
8
|
17 |
\theoremstyle{definition}
|
|
18 |
\newtheorem{definition}{Definition}
|
|
19 |
\begin{document}
|
|
20 |
This is a sketch proof for the correctness of the algorithm ders\_simp.
|
|
21 |
\section{Function Definitions}
|
|
22 |
|
|
23 |
\begin{definition}{Bits}
|
|
24 |
\begin{verbatim}
|
|
25 |
abstract class Bit
|
|
26 |
case object Z extends Bit
|
|
27 |
case object S extends Bit
|
|
28 |
case class C(c: Char) extends Bit
|
|
29 |
|
|
30 |
type Bits = List[Bit]
|
|
31 |
\end{verbatim}
|
|
32 |
\end{definition}
|
|
33 |
|
|
34 |
\begin{definition}{Annotated Regular Expressions}
|
|
35 |
\begin{verbatim}
|
|
36 |
abstract class ARexp
|
|
37 |
case object AZERO extends ARexp
|
|
38 |
case class AONE(bs: Bits) extends ARexp
|
|
39 |
case class ACHAR(bs: Bits, f: Char) extends ARexp
|
|
40 |
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp
|
|
41 |
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp
|
|
42 |
case class ASTAR(bs: Bits, r: ARexp) extends ARexp
|
|
43 |
\end{verbatim}
|
|
44 |
\end{definition}
|
|
45 |
|
|
46 |
\begin{definition}{bnullable}
|
|
47 |
\begin{verbatim}
|
|
48 |
def bnullable (r: ARexp) : Boolean = r match {
|
|
49 |
case AZERO => false
|
|
50 |
case AONE(_) => true
|
|
51 |
case ACHAR(_,_) => false
|
|
52 |
case AALTS(_, rs) => rs.exists(bnullable)
|
|
53 |
case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
|
|
54 |
case ASTAR(_, _) => true
|
|
55 |
}
|
|
56 |
\end{verbatim}
|
|
57 |
\end{definition}
|
|
58 |
|
|
59 |
\begin{definition}{ders\_simp}
|
|
60 |
\begin{verbatim}
|
|
61 |
def ders_simp(r: ARexp, s: List[Char]): ARexp = {
|
|
62 |
s match {
|
|
63 |
case Nil => r
|
|
64 |
case c::cs => ders_simp(bsimp(bder(c, r)), cs)
|
|
65 |
}
|
|
66 |
}\end{verbatim}
|
|
67 |
\end{definition}
|
|
68 |
|
|
69 |
\begin{definition}{bder}
|
|
70 |
\begin{verbatim}
|
|
71 |
def bder(c: Char, r: ARexp) : ARexp = r match {
|
|
72 |
case AZERO => AZERO
|
|
73 |
case AONE(_) => AZERO
|
|
74 |
case ACHAR(bs, f) => if (c == f) AONE(bs:::List(C(c))) else AZERO
|
|
75 |
case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
|
|
76 |
case ASEQ(bs, r1, r2) => {
|
|
77 |
if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(mkepsBC(r1), bder(c, r2)))
|
|
78 |
else ASEQ(bs, bder(c, r1), r2)
|
|
79 |
}
|
|
80 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
|
|
81 |
}
|
|
82 |
\end{verbatim}
|
|
83 |
\end{definition}
|
|
84 |
|
|
85 |
\begin{definition}{bsimp}
|
|
86 |
\begin{verbatim}
|
|
87 |
def bsimp(r: ARexp): ARexp = r match {
|
|
88 |
case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
|
|
89 |
case (AZERO, _) => AZERO
|
|
90 |
case (_, AZERO) => AZERO
|
|
91 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
|
|
92 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
|
|
93 |
}
|
|
94 |
case AALTS(bs1, rs) => {
|
|
95 |
val rs_simp = rs.map(bsimp)
|
|
96 |
val flat_res = flats(rs_simp)
|
|
97 |
val dist_res = distinctBy(flat_res, erase)
|
|
98 |
dist_res match {
|
|
99 |
case Nil => AZERO
|
|
100 |
case s :: Nil => fuse(bs1, s)
|
|
101 |
case rs => AALTS(bs1, rs)
|
|
102 |
}
|
|
103 |
}
|
|
104 |
//case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
|
|
105 |
case r => r
|
|
106 |
}
|
|
107 |
\end{verbatim}
|
|
108 |
\end{definition}
|
|
109 |
|
|
110 |
\begin{definition}{sub-parts of bsimp}
|
|
111 |
\begin{itemize}
|
|
112 |
\item{flats}\\
|
|
113 |
flattens the list.
|
|
114 |
\item{dB}\\
|
|
115 |
means distinctBy
|
|
116 |
\item{Co}\\
|
9
|
117 |
The last matching clause of the function bsimp, with a slight modification to suit later reasoning.
|
|
118 |
\begin{verbatim}
|
|
119 |
def Co(bs1, rs): ARexp = {
|
|
120 |
rs match {
|
8
|
121 |
case Nil => AZERO
|
|
122 |
case s :: Nil => fuse(bs1, s)
|
|
123 |
case rs => AALTS(bs1, rs)
|
|
124 |
}
|
9
|
125 |
\end{verbatim}
|
8
|
126 |
\end{itemize}
|
|
127 |
\end{definition}
|
|
128 |
|
|
129 |
\begin{definition}{fuse}
|
|
130 |
\begin{verbatim}
|
|
131 |
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
|
|
132 |
case AZERO => AZERO
|
|
133 |
case AONE(cs) => AONE(bs ++ cs)
|
|
134 |
case ACHAR(cs, f) => ACHAR(bs ++ cs, f)
|
|
135 |
case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
|
|
136 |
case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
|
|
137 |
case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
|
|
138 |
}
|
|
139 |
\end{verbatim}
|
|
140 |
\end{definition}
|
|
141 |
|
|
142 |
|
|
143 |
\begin{definition}{mkepsBC}
|
|
144 |
\begin{verbatim}
|
|
145 |
def mkepsBC(r: ARexp) : Bits = r match {
|
|
146 |
case AONE(bs) => bs
|
|
147 |
case AALTS(bs, rs) => {
|
|
148 |
val n = rs.indexWhere(bnullable)
|
|
149 |
bs ++ mkepsBC(rs(n))
|
|
150 |
}
|
|
151 |
case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
|
|
152 |
case ASTAR(bs, r) => bs ++ List(Z)
|
|
153 |
}
|
|
154 |
\end{verbatim}
|
|
155 |
\end{definition}
|
|
156 |
|
|
157 |
\begin{definition}{mkepsBC equicalence}
|
|
158 |
\\
|
|
159 |
Given 2 nullable annotated regular expressions r1, r2, if mkepsBC(r1) == mkepsBC(r2)
|
|
160 |
then r1 and r2 are mkepsBC equivalent, denoted as r1 $\sim_{m\epsilon}$ r2
|
|
161 |
\end{definition}
|
|
162 |
|
|
163 |
\begin{definition}{shorthand notation for ders}
|
|
164 |
\\For the sake of reducing verbosity, we sometimes use the shorthand notation
|
|
165 |
$d_{c}(r)$ for the function application bder(c, r) and $s(r)$(s here stands for simplification) for the function application bsimp(r) .
|
|
166 |
\\We omit the subscript when it is clear from context what that character is and write $d(r)$ instead of $d_{c}(r)$.
|
|
167 |
\\And we omit the parentheses when no confusion can be caused. For example ders\_simp(c, r) can be written as $s(d_{c}(r))$ or even $s d r$ as we know the derivative operation is w.r.t the character c. Here the s and d are more like operators that take an annotated regular expression as an input and return an annotated regular expression as an output
|
|
168 |
|
|
169 |
\end{definition}
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
\begin{definition}{distinctBy operation expressed in a different way--how it transforms the list}\\
|
|
174 |
Given two lists rs1 and rs2, we define the operation $--$:\\
|
|
175 |
$rs1 -- rs2 := [r \in rs1 | r \notin rs2]$
|
9
|
176 |
Note that the order each term appears in $rs_1 -- rs_2$ is preserved as in the original list.
|
8
|
177 |
\end{definition}
|
|
178 |
|
|
179 |
|
|
180 |
\section{Main Result}
|
9
|
181 |
|
|
182 |
\begin{lemma}{simplification function does not simplify an already simplified regex}\label{lma1}\\
|
8
|
183 |
bsimp(r) == bsimp(bsimp(r)) holds for any annotated regular expression r.
|
|
184 |
\end{lemma}
|
|
185 |
|
9
|
186 |
\begin{lemma}{simp and mkeps}\label{lma2}\\
|
8
|
187 |
When r is nullable, we have that
|
|
188 |
mkeps(bsimp(r)) == mkeps(r)
|
|
189 |
\end{lemma}
|
|
190 |
|
9
|
191 |
|
|
192 |
%\begin{theorem}See~\cref{lma1}.\end{theorem}
|
|
193 |
%\begin{lemma}\label{lma1}\lipsum[2]\end{lemma}
|
|
194 |
|
|
195 |
\begin{lemma}{mkeps equivalence w.r.t some syntactically different regular expressions(1 ALTS)}\label{lma3}\\
|
|
196 |
When one of the 2 regular expressions $s(r_1)$ and $s(r_2)$ is of the form ALTS(bs1, rs1), we have that $ds(ALTS(bs, r1, r2)) \sim_{m\epsilon} d(ALTS(bs, sr_1, sr_2))$
|
8
|
197 |
\end{lemma}
|
|
198 |
\begin{proof}
|
9
|
199 |
By opening up one of the alts and show no additional changes are made.\\
|
|
200 |
Details: $ds(ALTS(bs, r1, r2)) = d Co( bs, dB(flats(sr1, sr2)) )$
|
8
|
201 |
\end{proof}
|
|
202 |
|
9
|
203 |
|
|
204 |
\begin{lemma}{mkepsBC invariant manipulation of bits and notation}\label{lma7}\\
|
|
205 |
ALTS(bs, ALTS(bs1, rs1), ALTS(bs2, rs2)) $\sim_{m\epsilon}$ ALTS(bs, rs1.map(fuse(bs1, \_)) ++ rs2.map(fuse(bs2, \_)) ). \\
|
|
206 |
We also use $bs2>>rs2 $ as a shorthand notation for rs2.map(fuse(bs2,\_)).
|
|
207 |
\end{lemma}
|
|
208 |
|
|
209 |
\begin{lemma}{mkepsBC equivalence w.r.t syntactically different regular expressions(2 ALTS)}\label{lma4}\\
|
8
|
210 |
$sr_1 = ALTS(bs1, rs1)$ and $sr_2 = ALTS(bs2, rs2)$ we have $ d(sr_1 +sr_2 ) \sim_{m\epsilon} d(ALTS(bs, bs1>>rs1 ++ bs2>>rs2))$
|
|
211 |
\end{lemma}
|
|
212 |
\begin{proof}
|
|
213 |
We are just fusing bits inside here, there is no other structural change.
|
|
214 |
\end{proof}
|
|
215 |
|
9
|
216 |
\begin{lemma}{What does dB do to two already simplified ALTS}\label{lma5}\\
|
|
217 |
$d Co(ALTS(bs, dB(bs1>>rs1 ++ bs2>>rs2))) = d Co(ALTS(bs, bs1>>rs1 ++ ((bs2>>rs2)--rs1) )) $
|
8
|
218 |
\end{lemma}
|
|
219 |
\begin{proof}
|
9
|
220 |
We prove that $ dB(bs1>>rs1 ++ bs2>>rs2) = bs1>>rs1 ++ ((bs2>>rs2)--rs1)$.
|
|
221 |
|
|
222 |
|
8
|
223 |
\end{proof}
|
|
224 |
|
9
|
225 |
\begin{lemma}{after opening two previously simplified alts up into terms, length must exceed 2}\label{lma6}\\
|
|
226 |
If sr1, sr2 are of the form ALTS(bs1, rs1), ALTS(bs2, rs2) respectively, then we have that
|
|
227 |
$Co(bs, (bs1>>rs1) ++ (bs2>>rs2)--rs1) = ALTS(bs, bs1>>rs1 ++ (bs2>>rs2)--rs1)$
|
8
|
228 |
\end{lemma}
|
|
229 |
\begin{proof}
|
9
|
230 |
$Co(bs, rs) \sim_{m\epsilon} ALTS(bs, rs)$ if $rs$ is a list of length greater than or equal to 2.
|
|
231 |
As suggested by the title of this lemma, ALTS(bs1, rs1) is a result of simplification, which means that rs1 must be composed of at least 2 distinct regular terms. This alone says that $bs1>>rs1 ++ (bs2>>rs2)--rs1$ is a list of length greater than or equal to 2, as the second operand of the concatenation operator $(bs2>>rs2) -- rs1$ can only contribute a non-negative value to the overall length of the list
|
|
232 |
$bs1>>rs1 ++ (bs2>>rs2)--rs1$.
|
8
|
233 |
\end{proof}
|
|
234 |
|
|
235 |
|
9
|
236 |
\begin{lemma}{mkepsBC equivalence w.r.t syntactically different regular expressions(2 ALTS+ some deletion after derivatives)}\label{lma8}\\
|
|
237 |
$d ALTS(bs, bs1>>rs1 ++ bs2>>rs2) \sim_{m\epsilon} d ALTS(bs, bs1>>rs1 ++ ((bs2>>rs2)--rs1) ) $
|
|
238 |
\end{lemma}
|
|
239 |
\begin{proof}
|
|
240 |
Let's call $bs1>>rs1$ $rs1'$ and $bs2>>rs2$ $rs2'$. Then we need to prove $d ALTS(bs, rs1' ++rs2') \sim_{m\epsilon} d ALTS(bs, rs1'++(rs2' -- rs1') ) $.\\
|
|
241 |
We might as well omit the prime in each rs for simplicty of notation and prove $d ALTS(bs, rs1++rs2) \sim_{m\epsilon} d ALTS(bs, rs1++(rs2 -- rs1) ) $.\\
|
|
242 |
We know that the result of derivative is nullable, so there must exist an r in rs1++rs2 s.t. r is nullable.\\
|
|
243 |
If $r \in rs1$, then equivalence holds. If $r \in rs2 \wedge r \notin rs1$, equivalence holds as well. This completes the proof.
|
|
244 |
\end{proof}
|
8
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
\begin{theorem}{Correctness Result}
|
|
249 |
|
|
250 |
\begin{itemize}
|
|
251 |
|
|
252 |
\item{}
|
|
253 |
When s is a string in the language L(ar), \\
|
|
254 |
ders\_simp(ar, s) $\sim_{m\epsilon}$ ders(ar, s), \\
|
|
255 |
\item{}
|
|
256 |
when s is not a string of the language L(ar)
|
|
257 |
ders\_simp(ar, s) is not nullable
|
|
258 |
\end{itemize}
|
|
259 |
\end{theorem}
|
|
260 |
|
|
261 |
\begin{proof}{Split into 2 parts.}
|
|
262 |
\begin{itemize}
|
|
263 |
\item
|
|
264 |
|
9
|
265 |
|
8
|
266 |
When we have an annotated regular expression ar and a string s that matches ar, by the correctness of the algorithm ders, we have that ders(ar, s) is nullable, and that mkepsBC will extract the desired bits for decoding the correct value v for the matching, and v is a POSIX value. Now we prove that mkepsBC(ders\_simp(ar, s)) yields the same bitsequence.
|
|
267 |
\\
|
|
268 |
We first open up the ders\_simp function into nested alternating sequences of ders and simp.
|
|
269 |
Assume that s = $c_1...c_n$($n \geq 1$ ) where each of the $c_i$ are characters.
|
|
270 |
Then $ders\_simp(ar, s)$ = $s(d_{c_n}(...s(d_{c_1}(r))...))$ = $sdsd......sdr$. If we can prove that
|
9
|
271 |
$sdr \sim_{m\epsilon} dsr$ holds for any regular expression and any character, then we are done. This is because then we can push ders operation inside and move simp operation outside and have that $sdsd...sdr \sim_{m\epsilon} ssddsdsd...sdr \sim_{m\epsilon} ... \sim_{m\epsilon} s....sd....dr$.
|
|
272 |
Using \autoref{lma1} we have that $s...sd....dr = sd...dr$.
|
|
273 |
By \autoref{lma2}, we have $RHS \sim_{m\epsilon} d...dr$.\\
|
|
274 |
Notice that we don't actually need \autoref{lma1} here. That is because by \autoref{lma2}, we can have that $s...sd....dr \sim_{m\epsilon} sd...dr$. The equality above can be replaced by mkepsBC
|
|
275 |
equivalence without affecting the validity of the whole proof since all we want is mkepsBC equivalence, not equality.
|
|
276 |
|
|
277 |
Now we proceed to prove that $sdr \sim_{m\epsilon} dsr$. This can be reduced to proving $dr \sim_{m\epsilon} dsr$ as we know that $dr \sim_{m\epsilon} sdr$ by \autoref{lma2}.\\
|
8
|
278 |
|
|
279 |
we use an induction proof. Base cases are omitted. Here are the 3 inductive cases.
|
|
280 |
\begin{itemize}
|
|
281 |
|
|
282 |
\item{$r_1+r_2$}
|
|
283 |
$r_1+r_2$\\
|
|
284 |
The most difficult case is when $sr1$ and $sr2$ are both ALTS, so that they will be opened up in the flats function and some terms in sr2 might be deleted. Or otherwise we can use the argument that $d(r_1+r_2) = dr_1 + dr_2 \sim_{m\epsilon} dsr_1 + dsr_2 \sim_{m\epsilon} ds(r_1+r_2)$,
|
9
|
285 |
the last equivalence being established by \autoref{lma3}.
|
8
|
286 |
When $s(r_1), s(r_2)$ are both ALTS, we have to be more careful for the last equivalence step, namelly, $dsr_1 + dsr_2 \sim_{m\epsilon} ds(r_1+r_2)$. \\
|
9
|
287 |
We have that $LHS = dsr_1 + dsr_2 = d(sr_1 +sr_2)$. Since $sr_1 = ALTS(bs1, rs1)$ and $sr_2 = ALTS(bs2, rs2)$ we have $ d(sr_1 +sr_2 ) \sim_{m\epsilon} d(ALTS(bs, bs1>>rs1 ++ bs2>>rs2))$ by \autoref{lma4}.
|
|
288 |
On the other hand, $RHS = ds(ALTS(bs, r1, r2)) = d Co(bs, dB(flats(s(r1), s(r2)) ) ) = d Co(bs, dB(bs1>>rs1 ++ bs2>>rs2))$ by definition of bsimp and flats.\\
|
|
289 |
$d Co(bs, dB(bs1>>rs1 ++ bs2>>rs2)) = d Co(bs, (bs1>>rs1 ++ ((bs2>>rs2)--rs1))) $ by \autoref{lma5}.\\ $d Co(bs, (bs1>>rs1 ++ ((bs2>>rs2)--rs1) )) = d(ALTS(bs, bs1>>rs1 ++ (bs2>>rs2)--rs1))$ by \autoref{lma6}. \\
|
|
290 |
Using \autoref{lma8}, we have $d(ALTS(bs, bs1>>rs1 ++ (bs2>>rs2)--rs1)) \sim_{m\epsilon} d(ALTS(bs, bs1>>rs1 ++ bs2>>rs2)) \sim_{m\epsilon} RHS$.\\
|
|
291 |
%only the last part we don't have an equality here. We use mkeps equivalence instead because these two regexes are indeed different syntactically. However, they have even more in common than just mkeps equal. We might later augment this proof so that this equivalence relation is changed to something like regular expression equivalence.
|
8
|
292 |
This completes the proof.
|
|
293 |
\item{$r*$}\\
|
|
294 |
s(r*) = s(r).
|
|
295 |
\item{$r1.r2$}\\
|
|
296 |
using previous.
|
|
297 |
|
|
298 |
\end{itemize}
|
|
299 |
\item
|
|
300 |
Proof of second part of the theorem: use a similar structure of argument as in the first part.
|
|
301 |
\end{itemize}
|
|
302 |
\end{proof}
|
|
303 |
|
|
304 |
\end{document}
|
|
305 |
|
|
306 |
%The second part might still need some more development.
|
|
307 |
%When s is not in the set L(ar), we have that s = s1@s2 s.t. s1 $\in$ L(ar), and any longer string that is a prefix of s does not belong to L(ar).\\
|
|
308 |
%By first part of proof, we have ders(ar, s1) $\sim_{m\epsilon}$ ders\_simp(ar, s1)
|
|
309 |
%.....somehow show that by correctness, der(c, ders\_simp(ar, s1)) must be not nullable. But this will need that L(ders(ar, s1)) == L(ders\_simp(ar, s1)). By part 1 of proof we only have that for any string s1c s.t. s1c $\in$ L(ar) (which is equivalent to s $\in$ L(ders(ar, s1))), s is also in L(ders\_simp(ar, s1)). That is an inclusion, not an equality. c not in L(ders(ar, s1)) does not imply c not in L(ders\_simp(ar, s1))
|
|
310 |
%So this path stuck here.
|