thys/Journal/Paper.thy
author Christian Urban <urbanc@in.tum.de>
Sun, 26 Feb 2017 23:46:22 +0000
changeset 218 16af5b8bd285
child 265 d36be1e356c0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
218
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
(*<*)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
theory Paper
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
imports 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
   "../Lexer"
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
   "../Simplifying" 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
   "../Sulzmann" 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
   "~~/src/HOL/Library/LaTeXsugar"
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
begin
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
declare [[show_question_marks = false]]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
abbreviation 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
 "der_syn r c \<equiv> der c r"
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
abbreviation 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
 "ders_syn r s \<equiv> ders s r"
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
notation (latex output)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  If  ("(\<^raw:\textrm{>if\<^raw:}> (_)/ \<^raw:\textrm{>then\<^raw:}> (_)/ \<^raw:\textrm{>else\<^raw:}> (_))" 10) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  Cons ("_\<^raw:\mbox{$\,$}>::\<^raw:\mbox{$\,$}>_" [75,73] 73) and  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  ZERO ("\<^bold>0" 78) and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  ONE ("\<^bold>1" 78) and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  CHAR ("_" [1000] 80) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  ALT ("_ + _" [77,77] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  SEQ ("_ \<cdot> _" [77,77] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  STAR ("_\<^sup>\<star>" [1000] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  val.Void ("'(')" 1000) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  val.Char ("Char _" [1000] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  val.Left ("Left _" [79] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  val.Right ("Right _" [1000] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  val.Seq ("Seq _ _" [79,79] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  val.Stars ("Stars _" [79] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  L ("L'(_')" [10] 78) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  der_syn ("_\\_" [79, 1000] 76) and  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  ders_syn ("_\\_" [79, 1000] 76) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  flat ("|_|" [75] 74) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  Sequ ("_ @ _" [78,77] 63) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  injval ("inj _ _ _" [79,77,79] 76) and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  mkeps ("mkeps _" [79] 76) and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  length ("len _" [73] 73) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  Prf ("_ : _" [75,75] 75) and  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  Posix ("'(_, _') \<rightarrow> _" [63,75,75] 75) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  lexer ("lexer _ _" [78,78] 77) and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  F_RIGHT ("F\<^bsub>Right\<^esub> _") and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  F_LEFT ("F\<^bsub>Left\<^esub> _") and  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  F_ALT ("F\<^bsub>Alt\<^esub> _ _") and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  F_SEQ1 ("F\<^bsub>Seq1\<^esub> _ _") and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  F_SEQ2 ("F\<^bsub>Seq2\<^esub> _ _") and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  F_SEQ ("F\<^bsub>Seq\<^esub> _ _") and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  simp_SEQ ("simp\<^bsub>Seq\<^esub> _ _" [1000, 1000] 1) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  simp_ALT ("simp\<^bsub>Alt\<^esub> _ _" [1000, 1000] 1) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  slexer ("lexer\<^sup>+" 1000) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  ValOrd ("_ >\<^bsub>_\<^esub> _" [77,77,77] 77) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  ValOrdEq ("_ \<ge>\<^bsub>_\<^esub> _" [77,77,77] 77)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
definition 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  "match r s \<equiv> nullable (ders s r)"
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
(*
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
comments not implemented
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
p9. The condtion "not exists s3 s4..." appears often enough (in particular in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
the proof of Lemma 3) to warrant a definition.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
*)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
(*>*)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
section {* Introduction *}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
text {*
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
Brzozowski \cite{Brzozowski1964} introduced the notion of the {\em
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
derivative} @{term "der c r"} of a regular expression @{text r} w.r.t.\ a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
character~@{text c}, and showed that it gave a simple solution to the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
problem of matching a string @{term s} with a regular expression @{term r}:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
if the derivative of @{term r} w.r.t.\ (in succession) all the characters of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
the string matches the empty string, then @{term r} matches @{term s} (and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
{\em vice versa}). The derivative has the property (which may almost be
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
regarded as its specification) that, for every string @{term s} and regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
expression @{term r} and character @{term c}, one has @{term "cs \<in> L(r)"} if
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
and only if \mbox{@{term "s \<in> L(der c r)"}}. The beauty of Brzozowski's
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
derivatives is that they are neatly expressible in any functional language,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
and easily definable and reasoned about in theorem provers---the definitions
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
just consist of inductive datatypes and simple recursive functions. A
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
mechanised correctness proof of Brzozowski's matcher in for example HOL4
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
has been mentioned by Owens and Slind~\cite{Owens2008}. Another one in Isabelle/HOL is part
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
of the work by Krauss and Nipkow \cite{Krauss2011}. And another one in Coq is given
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
by Coquand and Siles \cite{Coquand2012}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
If a regular expression matches a string, then in general there is more than
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
one way of how the string is matched. There are two commonly used
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
disambiguation strategies to generate a unique answer: one is called GREEDY
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
matching \cite{Frisch2004} and the other is POSIX
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
matching~\cite{Kuklewicz,Sulzmann2014,Vansummeren2006}. For example consider
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
the string @{term xy} and the regular expression \mbox{@{term "STAR (ALT
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
(ALT x y) xy)"}}. Either the string can be matched in two `iterations' by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
the single letter-regular expressions @{term x} and @{term y}, or directly
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
in one iteration by @{term xy}. The first case corresponds to GREEDY
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
matching, which first matches with the left-most symbol and only matches the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
next symbol in case of a mismatch (this is greedy in the sense of preferring
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
instant gratification to delayed repletion). The second case is POSIX
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
matching, which prefers the longest match.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
In the context of lexing, where an input string needs to be split up into a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
sequence of tokens, POSIX is the more natural disambiguation strategy for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
what programmers consider basic syntactic building blocks in their programs.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
These building blocks are often specified by some regular expressions, say
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
@{text "r\<^bsub>key\<^esub>"} and @{text "r\<^bsub>id\<^esub>"} for recognising keywords and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
identifiers, respectively. There are two underlying (informal) rules behind
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
tokenising a string in a POSIX fashion according to a collection of regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
expressions:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
\begin{itemize} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
\item[$\bullet$] \emph{The Longest Match Rule} (or \emph{``maximal munch rule''}):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
The longest initial substring matched by any regular expression is taken as
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
next token.\smallskip
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
\item[$\bullet$] \emph{Priority Rule:}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
For a particular longest initial substring, the first regular expression
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
that can match determines the token.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
\end{itemize}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
\noindent Consider for example a regular expression @{text "r\<^bsub>key\<^esub>"} for recognising keywords
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
such as @{text "if"}, @{text "then"} and so on; and @{text "r\<^bsub>id\<^esub>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
recognising identifiers (say, a single character followed by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
characters or numbers).  Then we can form the regular expression
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
@{text "(r\<^bsub>key\<^esub> + r\<^bsub>id\<^esub>)\<^sup>\<star>"} and use POSIX matching to tokenise strings,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
say @{text "iffoo"} and @{text "if"}.  For @{text "iffoo"} we obtain
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
by the Longest Match Rule a single identifier token, not a keyword
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
followed by an identifier. For @{text "if"} we obtain by the Priority
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
Rule a keyword token, not an identifier token---even if @{text "r\<^bsub>id\<^esub>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
matches also.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
One limitation of Brzozowski's matcher is that it only generates a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
YES/NO answer for whether a string is being matched by a regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this matcher
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
to allow generation not just of a YES/NO answer but of an actual
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
matching, called a [lexical] {\em value}. They give a simple algorithm
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
to calculate a value that appears to be the value associated with
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
POSIX matching.  The challenge then is to specify that value, in an
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
algorithm-independent fashion, and to show that Sulzmann and Lu's
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
derivative-based algorithm does indeed calculate a value that is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
correct according to the specification.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
The answer given by Sulzmann and Lu \cite{Sulzmann2014} is to define a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
relation (called an ``order relation'') on the set of values of @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
r}, and to show that (once a string to be matched is chosen) there is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
a maximum element and that it is computed by their derivative-based
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
algorithm. This proof idea is inspired by work of Frisch and Cardelli
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
\cite{Frisch2004} on a GREEDY regular expression matching
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
algorithm. However, we were not able to establish transitivity and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
totality for the ``order relation'' by Sulzmann and Lu. In Section
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
\ref{argu} we identify some inherent problems with their approach (of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
which some of the proofs are not published in \cite{Sulzmann2014});
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
perhaps more importantly, we give a simple inductive (and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
algorithm-independent) definition of what we call being a {\em POSIX
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
value} for a regular expression @{term r} and a string @{term s}; we
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
show that the algorithm computes such a value and that such a value is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
unique. Our proofs are both done by hand and checked in Isabelle/HOL.  The
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
experience of doing our proofs has been that this mechanical checking
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
was absolutely essential: this subject area has hidden snares. This
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
was also noted by Kuklewicz \cite{Kuklewicz} who found that nearly all
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
POSIX matching implementations are ``buggy'' \cite[Page
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
203]{Sulzmann2014} and by Grathwohl et al \cite[Page 36]{CrashCourse2014}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
who wrote:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
\begin{quote}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
\it{}``The POSIX strategy is more complicated than the greedy because of 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
the dependence on information about the length of matched strings in the 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
various subexpressions.''
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
\end{quote}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
%\footnote{The relation @{text "\<ge>\<^bsub>r\<^esub>"} defined by Sulzmann and Lu \cite{Sulzmann2014} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
%is a relation on the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
%values for the regular expression @{term r}; but it only holds between
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
%@{term "v\<^sub>1"} and @{term "v\<^sub>2"} in cases where @{term "v\<^sub>1"} and @{term "v\<^sub>2"} have
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
%the same flattening (underlying string). So a counterexample to totality is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
%given by taking two values @{term "v\<^sub>1"} and @{term "v\<^sub>2"} for @{term r} that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
%have different flattenings (see Section~\ref{posixsec}). A different
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
%relation @{text "\<ge>\<^bsub>r,s\<^esub>"} on the set of values for @{term r}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
%with flattening @{term s} is definable by the same approach, and is indeed
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
%total; but that is not what Proposition 1 of \cite{Sulzmann2014} does.}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
\noindent {\bf Contributions:} We have implemented in Isabelle/HOL the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
derivative-based regular expression matching algorithm of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
Sulzmann and Lu \cite{Sulzmann2014}. We have proved the correctness of this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
algorithm according to our specification of what a POSIX value is (inspired
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
by work of Vansummeren \cite{Vansummeren2006}). Sulzmann
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
and Lu sketch in \cite{Sulzmann2014} an informal correctness proof: but to
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
us it contains unfillable gaps.\footnote{An extended version of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
\cite{Sulzmann2014} is available at the website of its first author; this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
extended version already includes remarks in the appendix that their
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
informal proof contains gaps, and possible fixes are not fully worked out.}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
Our specification of a POSIX value consists of a simple inductive definition
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
that given a string and a regular expression uniquely determines this value.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
Derivatives as calculated by Brzozowski's method are usually more complex
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
regular expressions than the initial one; various optimisations are
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
possible. We prove the correctness when simplifications of @{term "ALT ZERO
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
r"}, @{term "ALT r ZERO"}, @{term "SEQ ONE r"} and @{term "SEQ r ONE"} to
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
@{term r} are applied.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
section {* Preliminaries *}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
text {* \noindent Strings in Isabelle/HOL are lists of characters with the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
empty string being represented by the empty list, written @{term "[]"}, and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
list-cons being written as @{term "DUMMY # DUMMY"}. Often we use the usual
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
bracket notation for lists also for strings; for example a string consisting
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
of just a single character @{term c} is written @{term "[c]"}. By using the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
type @{type char} for characters we have a supply of finitely many
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
characters roughly corresponding to the ASCII character set. Regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
expressions are defined as usual as the elements of the following inductive
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
datatype:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
  @{text "r :="}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
  @{const "ZERO"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
  @{const "ONE"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
  @{term "CHAR c"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
  @{term "ALT r\<^sub>1 r\<^sub>2"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
  @{term "SEQ r\<^sub>1 r\<^sub>2"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
  @{term "STAR r"} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  \noindent where @{const ZERO} stands for the regular expression that does
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
  not match any string, @{const ONE} for the regular expression that matches
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  only the empty string and @{term c} for matching a character literal. The
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  language of a regular expression is also defined as usual by the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  recursive function @{term L} with the six clauses:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  \begin{tabular}{l@ {\hspace{3mm}}rcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
  (1) & @{thm (lhs) L.simps(1)} & $\dn$ & @{thm (rhs) L.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  (2) & @{thm (lhs) L.simps(2)} & $\dn$ & @{thm (rhs) L.simps(2)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  (3) & @{thm (lhs) L.simps(3)} & $\dn$ & @{thm (rhs) L.simps(3)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
  \hspace{14mm}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  \begin{tabular}{l@ {\hspace{3mm}}rcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  (4) & @{thm (lhs) L.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) L.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
  (5) & @{thm (lhs) L.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) L.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  (6) & @{thm (lhs) L.simps(6)} & $\dn$ & @{thm (rhs) L.simps(6)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
  \noindent In clause (4) we use the operation @{term "DUMMY ;;
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
  DUMMY"} for the concatenation of two languages (it is also list-append for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
  strings). We use the star-notation for regular expressions and for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  languages (in the last clause above). The star for languages is defined
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
  inductively by two clauses: @{text "(i)"} the empty string being in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
  the star of a language and @{text "(ii)"} if @{term "s\<^sub>1"} is in a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
  language and @{term "s\<^sub>2"} in the star of this language, then also @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
  "s\<^sub>1 @ s\<^sub>2"} is in the star of this language. It will also be convenient
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
  to use the following notion of a \emph{semantic derivative} (or \emph{left
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
  quotient}) of a language defined as
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
  @{thm Der_def}\;.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
  For semantic derivatives we have the following equations (for example
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  mechanically proved in \cite{Krauss2011}):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
  \begin{equation}\label{SemDer}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
  \begin{array}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
  @{thm (lhs) Der_null}  & \dn & @{thm (rhs) Der_null}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
  @{thm (lhs) Der_empty}  & \dn & @{thm (rhs) Der_empty}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
  @{thm (lhs) Der_char}  & \dn & @{thm (rhs) Der_char}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
  @{thm (lhs) Der_union}  & \dn & @{thm (rhs) Der_union}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
  @{thm (lhs) Der_Sequ}  & \dn & @{thm (rhs) Der_Sequ}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
  @{thm (lhs) Der_star}  & \dn & @{thm (rhs) Der_star}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
  \end{array}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  \end{equation}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
  \noindent \emph{\Brz's derivatives} of regular expressions
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
  \cite{Brzozowski1964} can be easily defined by two recursive functions:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
  the first is from regular expressions to booleans (implementing a test
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
  when a regular expression can match the empty string), and the second
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
  takes a regular expression and a character to a (derivative) regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
  expression:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
  @{thm (lhs) nullable.simps(1)} & $\dn$ & @{thm (rhs) nullable.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
  @{thm (lhs) nullable.simps(2)} & $\dn$ & @{thm (rhs) nullable.simps(2)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  @{thm (lhs) nullable.simps(3)} & $\dn$ & @{thm (rhs) nullable.simps(3)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
  @{thm (lhs) nullable.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) nullable.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  @{thm (lhs) nullable.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) nullable.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
  @{thm (lhs) nullable.simps(6)} & $\dn$ & @{thm (rhs) nullable.simps(6)}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
  %\end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
  %\end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
  %\begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  %\begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
  @{thm (lhs) der.simps(1)} & $\dn$ & @{thm (rhs) der.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
  @{thm (lhs) der.simps(2)} & $\dn$ & @{thm (rhs) der.simps(2)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
  @{thm (lhs) der.simps(3)} & $\dn$ & @{thm (rhs) der.simps(3)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
  @{thm (lhs) der.simps(4)[of c "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) der.simps(4)[of c "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  @{thm (lhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  @{thm (lhs) der.simps(6)} & $\dn$ & @{thm (rhs) der.simps(6)}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
  We may extend this definition to give derivatives w.r.t.~strings:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
  @{thm (lhs) ders.simps(1)} & $\dn$ & @{thm (rhs) ders.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
  @{thm (lhs) ders.simps(2)} & $\dn$ & @{thm (rhs) ders.simps(2)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  \noindent Given the equations in \eqref{SemDer}, it is a relatively easy
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
  exercise in mechanical reasoning to establish that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
  \begin{proposition}\label{derprop}\mbox{}\\ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  \begin{tabular}{ll}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  @{text "(1)"} & @{thm (lhs) nullable_correctness} if and only if
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
  @{thm (rhs) nullable_correctness}, and \\ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  @{text "(2)"} & @{thm[mode=IfThen] der_correctness}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
  \end{proposition}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
  \noindent With this in place it is also very routine to prove that the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
  regular expression matcher defined as
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
  @{thm match_def}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  \noindent gives a positive answer if and only if @{term "s \<in> L r"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
  Consequently, this regular expression matching algorithm satisfies the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
  usual specification for regular expression matching. While the matcher
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
  above calculates a provably correct YES/NO answer for whether a regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
  expression matches a string or not, the novel idea of Sulzmann and Lu
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
  \cite{Sulzmann2014} is to append another phase to this algorithm in order
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
  to calculate a [lexical] value. We will explain the details next.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
section {* POSIX Regular Expression Matching\label{posixsec} *}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
text {* 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
  The clever idea by Sulzmann and Lu \cite{Sulzmann2014} is to define
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  values for encoding \emph{how} a regular expression matches a string
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  and then define a function on values that mirrors (but inverts) the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
  construction of the derivative on regular expressions. \emph{Values}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
  are defined as the inductive datatype
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  @{text "v :="}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
  @{const "Void"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
  @{term "val.Char c"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  @{term "Left v"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  @{term "Right v"} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
  @{term "Seq v\<^sub>1 v\<^sub>2"} $\mid$ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
  @{term "Stars vs"} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  \end{center}  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
  \noindent where we use @{term vs} to stand for a list of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
  values. (This is similar to the approach taken by Frisch and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
  Cardelli for GREEDY matching \cite{Frisch2004}, and Sulzmann and Lu
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
  for POSIX matching \cite{Sulzmann2014}). The string underlying a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
  value can be calculated by the @{const flat} function, written
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
  @{term "flat DUMMY"} and defined as:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
  \begin{tabular}[t]{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
  @{thm (lhs) flat.simps(1)} & $\dn$ & @{thm (rhs) flat.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
  @{thm (lhs) flat.simps(2)} & $\dn$ & @{thm (rhs) flat.simps(2)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
  @{thm (lhs) flat.simps(3)} & $\dn$ & @{thm (rhs) flat.simps(3)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
  @{thm (lhs) flat.simps(4)} & $\dn$ & @{thm (rhs) flat.simps(4)}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
  \end{tabular}\hspace{14mm}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
  \begin{tabular}[t]{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
  @{thm (lhs) flat.simps(5)[of "v\<^sub>1" "v\<^sub>2"]} & $\dn$ & @{thm (rhs) flat.simps(5)[of "v\<^sub>1" "v\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
  @{thm (lhs) flat.simps(6)} & $\dn$ & @{thm (rhs) flat.simps(6)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
  @{thm (lhs) flat.simps(7)} & $\dn$ & @{thm (rhs) flat.simps(7)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
  \noindent Sulzmann and Lu also define inductively an inhabitation relation
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
  that associates values to regular expressions:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
  \begin{tabular}{c}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
  \\[-8mm]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
  @{thm[mode=Axiom] Prf.intros(4)} \qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
  @{thm[mode=Axiom] Prf.intros(5)[of "c"]}\\[4mm]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
  @{thm[mode=Rule] Prf.intros(2)[of "v\<^sub>1" "r\<^sub>1" "r\<^sub>2"]} \qquad 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
  @{thm[mode=Rule] Prf.intros(3)[of "v\<^sub>2" "r\<^sub>1" "r\<^sub>2"]}\\[4mm]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
  @{thm[mode=Rule] Prf.intros(1)[of "v\<^sub>1" "r\<^sub>1" "v\<^sub>2" "r\<^sub>2"]}\\[4mm]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
  @{thm[mode=Axiom] Prf.intros(6)[of "r"]} \qquad  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
  @{thm[mode=Rule] Prf.intros(7)[of "v" "r" "vs"]}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
  \noindent Note that no values are associated with the regular expression
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
  @{term ZERO}, and that the only value associated with the regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
  expression @{term ONE} is @{term Void}, pronounced (if one must) as @{text
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
  "Void"}. It is routine to establish how values ``inhabiting'' a regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
  expression correspond to the language of a regular expression, namely
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
  \begin{proposition}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
  @{thm L_flat_Prf}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  \end{proposition}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
  In general there is more than one value associated with a regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
  expression. In case of POSIX matching the problem is to calculate the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
  unique value that satisfies the (informal) POSIX rules from the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
  Introduction. Graphically the POSIX value calculation algorithm by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
  Sulzmann and Lu can be illustrated by the picture in Figure~\ref{Sulz}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
  where the path from the left to the right involving @{term derivatives}/@{const
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
  nullable} is the first phase of the algorithm (calculating successive
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
  \Brz's derivatives) and @{const mkeps}/@{text inj}, the path from right to
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
  left, the second phase. This picture shows the steps required when a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
  regular expression, say @{text "r\<^sub>1"}, matches the string @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
  "[a,b,c]"}. We first build the three derivatives (according to @{term a},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
  @{term b} and @{term c}). We then use @{const nullable} to find out
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  whether the resulting derivative regular expression @{term "r\<^sub>4"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
  can match the empty string. If yes, we call the function @{const mkeps}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
  that produces a value @{term "v\<^sub>4"} for how @{term "r\<^sub>4"} can
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  match the empty string (taking into account the POSIX constraints in case
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
  there are several ways). This function is defined by the clauses:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
\begin{figure}[t]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
\begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
\begin{tikzpicture}[scale=2,node distance=1.3cm,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
                    every node/.style={minimum size=6mm}]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
\node (r1)  {@{term "r\<^sub>1"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
\node (r2) [right=of r1]{@{term "r\<^sub>2"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
\draw[->,line width=1mm](r1)--(r2) node[above,midway] {@{term "der a DUMMY"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
\node (r3) [right=of r2]{@{term "r\<^sub>3"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
\draw[->,line width=1mm](r2)--(r3) node[above,midway] {@{term "der b DUMMY"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
\node (r4) [right=of r3]{@{term "r\<^sub>4"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
\draw[->,line width=1mm](r3)--(r4) node[above,midway] {@{term "der c DUMMY"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
\draw (r4) node[anchor=west] {\;\raisebox{3mm}{@{term nullable}}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
\node (v4) [below=of r4]{@{term "v\<^sub>4"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
\draw[->,line width=1mm](r4) -- (v4);
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
\node (v3) [left=of v4] {@{term "v\<^sub>3"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
\draw[->,line width=1mm](v4)--(v3) node[below,midway] {@{text "inj r\<^sub>3 c"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
\node (v2) [left=of v3]{@{term "v\<^sub>2"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
\draw[->,line width=1mm](v3)--(v2) node[below,midway] {@{text "inj r\<^sub>2 b"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
\node (v1) [left=of v2] {@{term "v\<^sub>1"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
\draw[->,line width=1mm](v2)--(v1) node[below,midway] {@{text "inj r\<^sub>1 a"}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
\draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{@{term "mkeps"}}};
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
\end{tikzpicture}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
\end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
\mbox{}\\[-13mm]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
\caption{The two phases of the algorithm by Sulzmann \& Lu \cite{Sulzmann2014},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
matching the string @{term "[a,b,c]"}. The first phase (the arrows from 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
left to right) is \Brz's matcher building successive derivatives. If the 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
last regular expression is @{term nullable}, then the functions of the 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
second phase are called (the top-down and right-to-left arrows): first 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
@{term mkeps} calculates a value @{term "v\<^sub>4"} witnessing
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
how the empty string has been recognised by @{term "r\<^sub>4"}. After
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
that the function @{term inj} ``injects back'' the characters of the string into
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
the values.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
\label{Sulz}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
\end{figure} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
  @{thm (lhs) mkeps.simps(1)} & $\dn$ & @{thm (rhs) mkeps.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
  @{thm (lhs) mkeps.simps(2)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) mkeps.simps(2)[of "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
  @{thm (lhs) mkeps.simps(3)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) mkeps.simps(3)[of "r\<^sub>1" "r\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
  @{thm (lhs) mkeps.simps(4)} & $\dn$ & @{thm (rhs) mkeps.simps(4)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
  \noindent Note that this function needs only to be partially defined,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
  namely only for regular expressions that are nullable. In case @{const
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
  nullable} fails, the string @{term "[a,b,c]"} cannot be matched by @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
  "r\<^sub>1"} and the null value @{term "None"} is returned. Note also how this function
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
  makes some subtle choices leading to a POSIX value: for example if an
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
  alternative regular expression, say @{term "ALT r\<^sub>1 r\<^sub>2"}, can
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
  match the empty string and furthermore @{term "r\<^sub>1"} can match the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
  empty string, then we return a @{text Left}-value. The @{text
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
  Right}-value will only be returned if @{term "r\<^sub>1"} cannot match the empty
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
  string.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
  The most interesting idea from Sulzmann and Lu \cite{Sulzmann2014} is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
  the construction of a value for how @{term "r\<^sub>1"} can match the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
  string @{term "[a,b,c]"} from the value how the last derivative, @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
  "r\<^sub>4"} in Fig.~\ref{Sulz}, can match the empty string. Sulzmann and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
  Lu achieve this by stepwise ``injecting back'' the characters into the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
  values thus inverting the operation of building derivatives, but on the level
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
  of values. The corresponding function, called @{term inj}, takes three
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
  arguments, a regular expression, a character and a value. For example in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
  the first (or right-most) @{term inj}-step in Fig.~\ref{Sulz} the regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
  expression @{term "r\<^sub>3"}, the character @{term c} from the last
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
  derivative step and @{term "v\<^sub>4"}, which is the value corresponding
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
  to the derivative regular expression @{term "r\<^sub>4"}. The result is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
  the new value @{term "v\<^sub>3"}. The final result of the algorithm is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
  the value @{term "v\<^sub>1"}. The @{term inj} function is defined by recursion on regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
  expressions and by analysing the shape of values (corresponding to 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
  the derivative regular expressions).
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
  \begin{tabular}{l@ {\hspace{5mm}}lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
  (1) & @{thm (lhs) injval.simps(1)} & $\dn$ & @{thm (rhs) injval.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
  (2) & @{thm (lhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]} & $\dn$ & 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
      @{thm (rhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
  (3) & @{thm (lhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ & 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
      @{thm (rhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
  (4) & @{thm (lhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
      & @{thm (rhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
  (5) & @{thm (lhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
      & @{thm (rhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
  (6) & @{thm (lhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
      & @{thm (rhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
  (7) & @{thm (lhs) injval.simps(7)[of "r" "c" "v" "vs"]} & $\dn$ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
      & @{thm (rhs) injval.simps(7)[of "r" "c" "v" "vs"]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
  \noindent To better understand what is going on in this definition it
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
  might be instructive to look first at the three sequence cases (clauses
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
  (4)--(6)). In each case we need to construct an ``injected value'' for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
  @{term "SEQ r\<^sub>1 r\<^sub>2"}. This must be a value of the form @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
  "Seq DUMMY DUMMY"}\,. Recall the clause of the @{text derivative}-function
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
  for sequence regular expressions:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
  @{thm (lhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]} $\dn$ @{thm (rhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
  \noindent Consider first the @{text "else"}-branch where the derivative is @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
  "SEQ (der c r\<^sub>1) r\<^sub>2"}. The corresponding value must therefore
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
  be of the form @{term "Seq v\<^sub>1 v\<^sub>2"}, which matches the left-hand
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  side in clause~(4) of @{term inj}. In the @{text "if"}-branch the derivative is an
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
  alternative, namely @{term "ALT (SEQ (der c r\<^sub>1) r\<^sub>2) (der c
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
  r\<^sub>2)"}. This means we either have to consider a @{text Left}- or
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
  @{text Right}-value. In case of the @{text Left}-value we know further it
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
  must be a value for a sequence regular expression. Therefore the pattern
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
  we match in the clause (5) is @{term "Left (Seq v\<^sub>1 v\<^sub>2)"},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
  while in (6) it is just @{term "Right v\<^sub>2"}. One more interesting
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
  point is in the right-hand side of clause (6): since in this case the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  regular expression @{text "r\<^sub>1"} does not ``contribute'' to
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
  matching the string, that means it only matches the empty string, we need to
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
  call @{const mkeps} in order to construct a value for how @{term "r\<^sub>1"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
  can match this empty string. A similar argument applies for why we can
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
  expect in the left-hand side of clause (7) that the value is of the form
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
  @{term "Seq v (Stars vs)"}---the derivative of a star is @{term "SEQ (der c r)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
  (STAR r)"}. Finally, the reason for why we can ignore the second argument
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
  in clause (1) of @{term inj} is that it will only ever be called in cases
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
  where @{term "c=d"}, but the usual linearity restrictions in patterns do
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
  not allow us to build this constraint explicitly into our function
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
  definition.\footnote{Sulzmann and Lu state this clause as @{thm (lhs)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
  injval.simps(1)[of "c" "c"]} $\dn$ @{thm (rhs) injval.simps(1)[of "c"]},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  but our deviation is harmless.}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
  The idea of the @{term inj}-function to ``inject'' a character, say
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
  @{term c}, into a value can be made precise by the first part of the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  following lemma, which shows that the underlying string of an injected
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
  value has a prepended character @{term c}; the second part shows that the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
  underlying string of an @{const mkeps}-value is always the empty string
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
  (given the regular expression is nullable since otherwise @{text mkeps}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
  might not be defined).
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
  \begin{lemma}\mbox{}\smallskip\\\label{Prf_injval_flat}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
  \begin{tabular}{ll}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
  (1) & @{thm[mode=IfThen] Prf_injval_flat}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
  (2) & @{thm[mode=IfThen] mkeps_flat}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
  \end{lemma}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
  \begin{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
  Both properties are by routine inductions: the first one can, for example,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
  be proved by induction over the definition of @{term derivatives}; the second by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
  an induction on @{term r}. There are no interesting cases.\qed
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
  \end{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
  Having defined the @{const mkeps} and @{text inj} function we can extend
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
  \Brz's matcher so that a [lexical] value is constructed (assuming the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
  regular expression matches the string). The clauses of the Sulzmann and Lu lexer are
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
  @{thm (lhs) lexer.simps(1)} & $\dn$ & @{thm (rhs) lexer.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
  @{thm (lhs) lexer.simps(2)} & $\dn$ & @{text "case"} @{term "lexer (der c r) s"} @{text of}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
                     & & \phantom{$|$} @{term "None"}  @{text "\<Rightarrow>"} @{term None}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
                     & & $|$ @{term "Some v"} @{text "\<Rightarrow>"} @{term "Some (injval r c v)"}                          
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
  \noindent If the regular expression does not match the string, @{const None} is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
  returned. If the regular expression \emph{does}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
  match the string, then @{const Some} value is returned. One important
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
  virtue of this algorithm is that it can be implemented with ease in any
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
  functional programming language and also in Isabelle/HOL. In the remaining
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
  part of this section we prove that this algorithm is correct.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
  The well-known idea of POSIX matching is informally defined by the longest
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
  match and priority rule (see Introduction); as correctly argued in \cite{Sulzmann2014}, this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
  needs formal specification. Sulzmann and Lu define an ``ordering
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
  relation'' between values and argue
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
  that there is a maximum value, as given by the derivative-based algorithm.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
  In contrast, we shall introduce a simple inductive definition that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
  specifies directly what a \emph{POSIX value} is, incorporating the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
  POSIX-specific choices into the side-conditions of our rules. Our
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
  definition is inspired by the matching relation given by Vansummeren
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
  \cite{Vansummeren2006}. The relation we define is ternary and written as
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
  \mbox{@{term "s \<in> r \<rightarrow> v"}}, relating strings, regular expressions and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
  values.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
  \begin{tabular}{c}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
  @{thm[mode=Axiom] Posix.intros(1)}@{text "P"}@{term "ONE"} \qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
  @{thm[mode=Axiom] Posix.intros(2)}@{text "P"}@{term "c"}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
  @{thm[mode=Rule] Posix.intros(3)[of "s" "r\<^sub>1" "v" "r\<^sub>2"]}@{text "P+L"}\qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
  @{thm[mode=Rule] Posix.intros(4)[of "s" "r\<^sub>2" "v" "r\<^sub>1"]}@{text "P+R"}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
  $\mprset{flushleft}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
   \inferrule
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
   {@{thm (prem 1) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]} \qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
    @{thm (prem 2) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]} \\\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
    @{thm (prem 3) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
   {@{thm (concl) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]}}$@{text "PS"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
  @{thm[mode=Axiom] Posix.intros(7)}@{text "P[]"}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
  $\mprset{flushleft}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
   \inferrule
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
   {@{thm (prem 1) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
    @{thm (prem 2) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
    @{thm (prem 3) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \\\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
    @{thm (prem 4) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
   {@{thm (concl) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}$@{text "P\<star>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
   \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
   We can prove that given a string @{term s} and regular expression @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
   r}, the POSIX value @{term v} is uniquely determined by @{term "s \<in> r \<rightarrow> v"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
  \begin{theorem}\mbox{}\smallskip\\\label{posixdeterm}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
  \begin{tabular}{ll}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
  @{text "(1)"} & If @{thm (prem 1) Posix1(1)} then @{thm (concl)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
  Posix1(1)} and @{thm (concl) Posix1(2)}.\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
  @{text "(2)"} & @{thm[mode=IfThen] Posix_determ(1)[of _ _ "v" "v'"]}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
  \end{theorem}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
  \begin{proof} Both by induction on the definition of @{term "s \<in> r \<rightarrow> v"}. 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
  The second parts follows by a case analysis of @{term "s \<in> r \<rightarrow> v'"} and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
  the first part.\qed
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
  \end{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
  We claim that our @{term "s \<in> r \<rightarrow> v"} relation captures the idea behind the two
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
  informal POSIX rules shown in the Introduction: Consider for example the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
  rules @{text "P+L"} and @{text "P+R"} where the POSIX value for a string
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
  and an alternative regular expression, that is @{term "(s, ALT r\<^sub>1 r\<^sub>2)"},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
  is specified---it is always a @{text "Left"}-value, \emph{except} when the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
  string to be matched is not in the language of @{term "r\<^sub>1"}; only then it
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
  is a @{text Right}-value (see the side-condition in @{text "P+R"}).
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
  Interesting is also the rule for sequence regular expressions (@{text
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
  "PS"}). The first two premises state that @{term "v\<^sub>1"} and @{term "v\<^sub>2"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   672
  are the POSIX values for @{term "(s\<^sub>1, r\<^sub>1)"} and @{term "(s\<^sub>2, r\<^sub>2)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
  respectively. Consider now the third premise and note that the POSIX value
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
  of this rule should match the string \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}}. According to the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
  longest match rule, we want that the @{term "s\<^sub>1"} is the longest initial
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
  split of \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}} such that @{term "s\<^sub>2"} is still recognised
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
  by @{term "r\<^sub>2"}. Let us assume, contrary to the third premise, that there
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
  \emph{exist} an @{term "s\<^sub>3"} and @{term "s\<^sub>4"} such that @{term "s\<^sub>2"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
  can be split up into a non-empty string @{term "s\<^sub>3"} and a possibly empty
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
  string @{term "s\<^sub>4"}. Moreover the longer string @{term "s\<^sub>1 @ s\<^sub>3"} can be
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
  matched by @{text "r\<^sub>1"} and the shorter @{term "s\<^sub>4"} can still be
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
  matched by @{term "r\<^sub>2"}. In this case @{term "s\<^sub>1"} would \emph{not} be the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
  longest initial split of \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}} and therefore @{term "Seq v\<^sub>1
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
  v\<^sub>2"} cannot be a POSIX value for @{term "(s\<^sub>1 @ s\<^sub>2, SEQ r\<^sub>1 r\<^sub>2)"}. 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
  The main point is that our side-condition ensures the longest 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
  match rule is satisfied.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
  A similar condition is imposed on the POSIX value in the @{text
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
  "P\<star>"}-rule. Also there we want that @{term "s\<^sub>1"} is the longest initial
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
  split of @{term "s\<^sub>1 @ s\<^sub>2"} and furthermore the corresponding value
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
  @{term v} cannot be flattened to the empty string. In effect, we require
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
  that in each ``iteration'' of the star, some non-empty substring needs to
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
  be ``chipped'' away; only in case of the empty string we accept @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
  "Stars []"} as the POSIX value.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   695
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
  Next is the lemma that shows the function @{term "mkeps"} calculates
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
  the POSIX value for the empty string and a nullable regular expression.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
  \begin{lemma}\label{lemmkeps}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
  @{thm[mode=IfThen] Posix_mkeps}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
  \end{lemma}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
  \begin{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
  By routine induction on @{term r}.\qed 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
  \end{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
  The central lemma for our POSIX relation is that the @{text inj}-function
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
  preserves POSIX values.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
  \begin{lemma}\label{Posix2}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
  @{thm[mode=IfThen] Posix_injval}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
  \end{lemma}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
  \begin{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
  By induction on @{text r}. We explain two cases.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
  \begin{itemize}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
  \item[$\bullet$] Case @{term "r = ALT r\<^sub>1 r\<^sub>2"}. There are
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
  two subcases, namely @{text "(a)"} \mbox{@{term "v = Left v'"}} and @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
  "s \<in> der c r\<^sub>1 \<rightarrow> v'"}; and @{text "(b)"} @{term "v = Right v'"}, @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
  "s \<notin> L (der c r\<^sub>1)"} and @{term "s \<in> der c r\<^sub>2 \<rightarrow> v'"}. In @{text "(a)"} we
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
  know @{term "s \<in> der c r\<^sub>1 \<rightarrow> v'"}, from which we can infer @{term "(c # s)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
  \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v'"} by induction hypothesis and hence @{term "(c #
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
  s) \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> injval (ALT r\<^sub>1 r\<^sub>2) c (Left v')"} as needed. Similarly
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
  in subcase @{text "(b)"} where, however, in addition we have to use
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
  Prop.~\ref{derprop}(2) in order to infer @{term "c # s \<notin> L r\<^sub>1"} from @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
  "s \<notin> L (der c r\<^sub>1)"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
  \item[$\bullet$] Case @{term "r = SEQ r\<^sub>1 r\<^sub>2"}. There are three subcases:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
  \begin{quote}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
  \begin{description}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
  \item[@{text "(a)"}] @{term "v = Left (Seq v\<^sub>1 v\<^sub>2)"} and @{term "nullable r\<^sub>1"} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
  \item[@{text "(b)"}] @{term "v = Right v\<^sub>1"} and @{term "nullable r\<^sub>1"} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
  \item[@{text "(c)"}] @{term "v = Seq v\<^sub>1 v\<^sub>2"} and @{term "\<not> nullable r\<^sub>1"} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
  \end{description}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
  \end{quote}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
  \noindent For @{text "(a)"} we know @{term "s\<^sub>1 \<in> der c r\<^sub>1 \<rightarrow> v\<^sub>1"} and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
  @{term "s\<^sub>2 \<in> r\<^sub>2 \<rightarrow> v\<^sub>2"} as well as
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
  \[@{term "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s\<^sub>2 \<and> s\<^sub>1 @ s\<^sub>3 \<in> L (der c r\<^sub>1) \<and> s\<^sub>4 \<in> L r\<^sub>2)"}\]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
  \noindent From the latter we can infer by Prop.~\ref{derprop}(2):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
  \[@{term "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s\<^sub>2 \<and> (c # s\<^sub>1) @ s\<^sub>3 \<in> L r\<^sub>1 \<and> s\<^sub>4 \<in> L r\<^sub>2)"}\]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
  \noindent We can use the induction hypothesis for @{text "r\<^sub>1"} to obtain
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
  @{term "(c # s\<^sub>1) \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v\<^sub>1"}. Putting this all together allows us to infer
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
  @{term "((c # s\<^sub>1) @ s\<^sub>2) \<in> SEQ r\<^sub>1 r\<^sub>2 \<rightarrow> Seq (injval r\<^sub>1 c v\<^sub>1) v\<^sub>2"}. The case @{text "(c)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
  is similar.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
  For @{text "(b)"} we know @{term "s \<in> der c r\<^sub>2 \<rightarrow> v\<^sub>1"} and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
  @{term "s\<^sub>1 @ s\<^sub>2 \<notin> L (SEQ (der c r\<^sub>1) r\<^sub>2)"}. From the former
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   756
  we have @{term "(c # s) \<in> r\<^sub>2 \<rightarrow> (injval r\<^sub>2 c v\<^sub>1)"} by induction hypothesis
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
  for @{term "r\<^sub>2"}. From the latter we can infer
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
  \[@{term "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = c # s \<and> s\<^sub>3 \<in> L r\<^sub>1 \<and> s\<^sub>4 \<in> L r\<^sub>2)"}\]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
  \noindent By Lem.~\ref{lemmkeps} we know @{term "[] \<in> r\<^sub>1 \<rightarrow> (mkeps r\<^sub>1)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
  holds. Putting this all together, we can conclude with @{term "(c #
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
  s) \<in> SEQ r\<^sub>1 r\<^sub>2 \<rightarrow> Seq (mkeps r\<^sub>1) (injval r\<^sub>2 c v\<^sub>1)"}, as required.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   765
  Finally suppose @{term "r = STAR r\<^sub>1"}. This case is very similar to the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
  sequence case, except that we need to also ensure that @{term "flat (injval r\<^sub>1
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
  c v\<^sub>1) \<noteq> []"}. This follows from @{term "(c # s\<^sub>1)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
  \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v\<^sub>1"}  (which in turn follows from @{term "s\<^sub>1 \<in> der c
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
  r\<^sub>1 \<rightarrow> v\<^sub>1"} and the induction hypothesis).\qed
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   770
  \end{itemize}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
  \end{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   773
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   774
  With Lem.~\ref{Posix2} in place, it is completely routine to establish
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
  that the Sulzmann and Lu lexer satisfies our specification (returning
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
  the null value @{term "None"} iff the string is not in the language of the regular expression,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
  and returning a unique POSIX value iff the string \emph{is} in the language):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   778
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
  \begin{theorem}\mbox{}\smallskip\\\label{lexercorrect}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   780
  \begin{tabular}{ll}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
  (1) & @{thm (lhs) lexer_correct_None} if and only if @{thm (rhs) lexer_correct_None}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
  (2) & @{thm (lhs) lexer_correct_Some} if and only if @{thm (rhs) lexer_correct_Some}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
  \end{theorem}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
  \begin{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
  By induction on @{term s} using Lem.~\ref{lemmkeps} and \ref{Posix2}.\qed  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
  \end{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
  \noindent In (2) we further know by Thm.~\ref{posixdeterm} that the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
  value returned by the lexer must be unique.   A simple corollary 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
  of our two theorems is:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   793
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
  \begin{corollary}\mbox{}\smallskip\\\label{lexercorrectcor}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   795
  \begin{tabular}{ll}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   796
  (1) & @{thm (lhs) lexer_correctness(2)} if and only if @{thm (rhs) lexer_correctness(2)}\\ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
  (2) & @{thm (lhs) lexer_correctness(1)} if and only if @{thm (rhs) lexer_correctness(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   798
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
  \end{corollary}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   801
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   802
  This concludes our
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   803
  correctness proof. Note that we have not changed the algorithm of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   804
  Sulzmann and Lu,\footnote{All deviations we introduced are
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   805
  harmless.} but introduced our own specification for what a correct
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   806
  result---a POSIX value---should be. A strong point in favour of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   807
  Sulzmann and Lu's algorithm is that it can be extended in various
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   808
  ways.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   809
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   810
*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   811
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   812
section {* Extensions and Optimisations*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   813
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   814
text {*
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   815
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   816
  If we are interested in tokenising a string, then we need to not just
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   817
  split up the string into tokens, but also ``classify'' the tokens (for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   818
  example whether it is a keyword or an identifier). This can be done with
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   819
  only minor modifications to the algorithm by introducing \emph{record
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   820
  regular expressions} and \emph{record values} (for example
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   821
  \cite{Sulzmann2014b}):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   822
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   823
  \begin{center}  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   824
  @{text "r :="}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   825
  @{text "..."} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   826
  @{text "(l : r)"} \qquad\qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   827
  @{text "v :="}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   828
  @{text "..."} $\mid$
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   829
  @{text "(l : v)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   830
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   831
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   832
  \noindent where @{text l} is a label, say a string, @{text r} a regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   833
  expression and @{text v} a value. All functions can be smoothly extended
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   834
  to these regular expressions and values. For example \mbox{@{text "(l :
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   835
  r)"}} is nullable iff @{term r} is, and so on. The purpose of the record
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   836
  regular expression is to mark certain parts of a regular expression and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   837
  then record in the calculated value which parts of the string were matched
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   838
  by this part. The label can then serve as classification for the tokens.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   839
  For this recall the regular expression @{text "(r\<^bsub>key\<^esub> + r\<^bsub>id\<^esub>)\<^sup>\<star>"} for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   840
  keywords and identifiers from the Introduction. With the record regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   841
  expression we can form \mbox{@{text "((key : r\<^bsub>key\<^esub>) + (id : r\<^bsub>id\<^esub>))\<^sup>\<star>"}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   842
  and then traverse the calculated value and only collect the underlying
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   843
  strings in record values. With this we obtain finite sequences of pairs of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   844
  labels and strings, for example
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   845
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   846
  \[@{text "(l\<^sub>1 : s\<^sub>1), ..., (l\<^sub>n : s\<^sub>n)"}\]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   847
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   848
  \noindent from which tokens with classifications (keyword-token,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   849
  identifier-token and so on) can be extracted.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   850
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   851
  Derivatives as calculated by \Brz's method are usually more complex
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   852
  regular expressions than the initial one; the result is that the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   853
  derivative-based matching and lexing algorithms are often abysmally slow.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   854
  However, various optimisations are possible, such as the simplifications
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   855
  of @{term "ALT ZERO r"}, @{term "ALT r ZERO"}, @{term "SEQ ONE r"} and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   856
  @{term "SEQ r ONE"} to @{term r}. These simplifications can speed up the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   857
  algorithms considerably, as noted in \cite{Sulzmann2014}. One of the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   858
  advantages of having a simple specification and correctness proof is that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   859
  the latter can be refined to prove the correctness of such simplification
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   860
  steps. While the simplification of regular expressions according to 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   861
  rules like
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   862
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   863
  \begin{equation}\label{Simpl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   864
  \begin{array}{lcllcllcllcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   865
  @{term "ALT ZERO r"} & @{text "\<Rightarrow>"} & @{term r} \hspace{8mm}%\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   866
  @{term "ALT r ZERO"} & @{text "\<Rightarrow>"} & @{term r} \hspace{8mm}%\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   867
  @{term "SEQ ONE r"}  & @{text "\<Rightarrow>"} & @{term r} \hspace{8mm}%\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   868
  @{term "SEQ r ONE"}  & @{text "\<Rightarrow>"} & @{term r}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   869
  \end{array}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   870
  \end{equation}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   871
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   872
  \noindent is well understood, there is an obstacle with the POSIX value
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   873
  calculation algorithm by Sulzmann and Lu: if we build a derivative regular
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   874
  expression and then simplify it, we will calculate a POSIX value for this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   875
  simplified derivative regular expression, \emph{not} for the original (unsimplified)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   876
  derivative regular expression. Sulzmann and Lu \cite{Sulzmann2014} overcome this obstacle by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   877
  not just calculating a simplified regular expression, but also calculating
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   878
  a \emph{rectification function} that ``repairs'' the incorrect value.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   879
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   880
  The rectification functions can be (slightly clumsily) implemented  in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   881
  Isabelle/HOL as follows using some auxiliary functions:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   882
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   883
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   884
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   885
  @{thm (lhs) F_RIGHT.simps(1)} & $\dn$ & @{text "Right (f v)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   886
  @{thm (lhs) F_LEFT.simps(1)} & $\dn$ & @{text "Left (f v)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   887
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   888
  @{thm (lhs) F_ALT.simps(1)} & $\dn$ & @{text "Right (f\<^sub>2 v)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   889
  @{thm (lhs) F_ALT.simps(2)} & $\dn$ & @{text "Left (f\<^sub>1 v)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   890
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   891
  @{thm (lhs) F_SEQ1.simps(1)} & $\dn$ & @{text "Seq (f\<^sub>1 ()) (f\<^sub>2 v)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   892
  @{thm (lhs) F_SEQ2.simps(1)} & $\dn$ & @{text "Seq (f\<^sub>1 v) (f\<^sub>2 ())"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   893
  @{thm (lhs) F_SEQ.simps(1)} & $\dn$ & @{text "Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)"}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   894
  %\end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   895
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   896
  %\begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   897
  @{term "simp_ALT (ZERO, DUMMY) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_RIGHT f\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   898
  @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (ZERO, DUMMY)"} & $\dn$ & @{term "(r\<^sub>1, F_LEFT f\<^sub>1)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   899
  @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(ALT r\<^sub>1 r\<^sub>2, F_ALT f\<^sub>1 f\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   900
  @{term "simp_SEQ (ONE, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_SEQ1 f\<^sub>1 f\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   901
  @{term "simp_SEQ (r\<^sub>1, f\<^sub>1) (ONE, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>1, F_SEQ2 f\<^sub>1 f\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   902
  @{term "simp_SEQ (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(SEQ r\<^sub>1 r\<^sub>2, F_SEQ f\<^sub>1 f\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   903
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   904
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   905
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   906
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   907
  The functions @{text "simp\<^bsub>Alt\<^esub>"} and @{text "simp\<^bsub>Seq\<^esub>"} encode the simplification rules
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   908
  in \eqref{Simpl} and compose the rectification functions (simplifications can occur
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   909
  deep inside the regular expression). The main simplification function is then 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   910
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   911
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   912
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   913
  @{term "simp (ALT r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_ALT (simp r\<^sub>1) (simp r\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   914
  @{term "simp (SEQ r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_SEQ (simp r\<^sub>1) (simp r\<^sub>2)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   915
  @{term "simp r"} & $\dn$ & @{term "(r, id)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   916
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   917
  \end{center} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   918
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   919
  \noindent where @{term "id"} stands for the identity function. The
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   920
  function @{const simp} returns a simplified regular expression and a corresponding
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   921
  rectification function. Note that we do not simplify under stars: this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   922
  seems to slow down the algorithm, rather than speed it up. The optimised
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   923
  lexer is then given by the clauses:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   924
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   925
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   926
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   927
  @{thm (lhs) slexer.simps(1)} & $\dn$ & @{thm (rhs) slexer.simps(1)}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   928
  @{thm (lhs) slexer.simps(2)} & $\dn$ & 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   929
                         @{text "let (r\<^sub>s, f\<^sub>r) = simp (r "}$\backslash$@{text " c) in"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   930
                     & & @{text "case"} @{term "slexer r\<^sub>s s"} @{text of}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   931
                     & & \phantom{$|$} @{term "None"}  @{text "\<Rightarrow>"} @{term None}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   932
                     & & $|$ @{term "Some v"} @{text "\<Rightarrow>"} @{text "Some (inj r c (f\<^sub>r v))"}                          
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   933
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   934
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   935
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   936
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   937
  In the second clause we first calculate the derivative @{term "der c r"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   938
  and then simplify the result. This gives us a simplified derivative
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   939
  @{text "r\<^sub>s"} and a rectification function @{text "f\<^sub>r"}. The lexer
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   940
  is then recursively called with the simplified derivative, but before
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   941
  we inject the character @{term c} into the value @{term v}, we need to rectify
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   942
  @{term v} (that is construct @{term "f\<^sub>r v"}). Before we can establish the correctness
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   943
  of @{term "slexer"}, we need to show that simplification preserves the language
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   944
  and simplification preserves our POSIX relation once the value is rectified
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   945
  (recall @{const "simp"} generates a (regular expression, rectification function) pair):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   946
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   947
  \begin{lemma}\mbox{}\smallskip\\\label{slexeraux}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   948
  \begin{tabular}{ll}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   949
  (1) & @{thm L_fst_simp[symmetric]}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   950
  (2) & @{thm[mode=IfThen] Posix_simp}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   951
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   952
  \end{lemma}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   953
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   954
  \begin{proof} Both are by induction on @{text r}. There is no
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   955
  interesting case for the first statement. For the second statement,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   956
  of interest are the @{term "r = ALT r\<^sub>1 r\<^sub>2"} and @{term "r = SEQ r\<^sub>1
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   957
  r\<^sub>2"} cases. In each case we have to analyse four subcases whether
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   958
  @{term "fst (simp r\<^sub>1)"} and @{term "fst (simp r\<^sub>2)"} equals @{const
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   959
  ZERO} (respectively @{const ONE}). For example for @{term "r = ALT
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   960
  r\<^sub>1 r\<^sub>2"}, consider the subcase @{term "fst (simp r\<^sub>1) = ZERO"} and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   961
  @{term "fst (simp r\<^sub>2) \<noteq> ZERO"}. By assumption we know @{term "s \<in>
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   962
  fst (simp (ALT r\<^sub>1 r\<^sub>2)) \<rightarrow> v"}. From this we can infer @{term "s \<in> fst (simp r\<^sub>2) \<rightarrow> v"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   963
  and by IH also (*) @{term "s \<in> r\<^sub>2 \<rightarrow> (snd (simp r\<^sub>2) v)"}. Given @{term "fst (simp r\<^sub>1) = ZERO"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   964
  we know @{term "L (fst (simp r\<^sub>1)) = {}"}. By the first statement
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   965
  @{term "L r\<^sub>1"} is the empty set, meaning (**) @{term "s \<notin> L r\<^sub>1"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   966
  Taking (*) and (**) together gives by the \mbox{@{text "P+R"}}-rule 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   967
  @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> Right (snd (simp r\<^sub>2) v)"}. In turn this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   968
  gives @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> snd (simp (ALT r\<^sub>1 r\<^sub>2)) v"} as we need to show.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   969
  The other cases are similar.\qed
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   970
  \end{proof}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   971
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   972
  \noindent We can now prove relatively straightforwardly that the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   973
  optimised lexer produces the expected result:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   974
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   975
  \begin{theorem}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   976
  @{thm slexer_correctness}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   977
  \end{theorem}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   978
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   979
  \begin{proof} By induction on @{term s} generalising over @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   980
  r}. The case @{term "[]"} is trivial. For the cons-case suppose the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   981
  string is of the form @{term "c # s"}. By induction hypothesis we
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   982
  know @{term "slexer r s = lexer r s"} holds for all @{term r} (in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   983
  particular for @{term "r"} being the derivative @{term "der c
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   984
  r"}). Let @{term "r\<^sub>s"} be the simplified derivative regular expression, that is @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   985
  "fst (simp (der c r))"}, and @{term "f\<^sub>r"} be the rectification
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   986
  function, that is @{term "snd (simp (der c r))"}.  We distinguish the cases
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   987
  whether (*) @{term "s \<in> L (der c r)"} or not. In the first case we
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   988
  have by Thm.~\ref{lexercorrect}(2) a value @{term "v"} so that @{term
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   989
  "lexer (der c r) s = Some v"} and @{term "s \<in> der c r \<rightarrow> v"} hold.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   990
  By Lem.~\ref{slexeraux}(1) we can also infer from~(*) that @{term "s
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   991
  \<in> L r\<^sub>s"} holds.  Hence we know by Thm.~\ref{lexercorrect}(2) that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   992
  there exists a @{term "v'"} with @{term "lexer r\<^sub>s s = Some v'"} and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   993
  @{term "s \<in> r\<^sub>s \<rightarrow> v'"}. From the latter we know by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   994
  Lem.~\ref{slexeraux}(2) that @{term "s \<in> der c r \<rightarrow> (f\<^sub>r v')"} holds.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   995
  By the uniqueness of the POSIX relation (Thm.~\ref{posixdeterm}) we
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   996
  can infer that @{term v} is equal to @{term "f\<^sub>r v'"}---that is the 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   997
  rectification function applied to @{term "v'"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   998
  produces the original @{term "v"}.  Now the case follows by the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   999
  definitions of @{const lexer} and @{const slexer}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1000
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1001
  In the second case where @{term "s \<notin> L (der c r)"} we have that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1002
  @{term "lexer (der c r) s = None"} by Thm.~\ref{lexercorrect}(1).  We
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1003
  also know by Lem.~\ref{slexeraux}(1) that @{term "s \<notin> L r\<^sub>s"}. Hence
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1004
  @{term "lexer r\<^sub>s s = None"} by Thm.~\ref{lexercorrect}(1) and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1005
  by IH then also @{term "slexer r\<^sub>s s = None"}. With this we can
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1006
  conclude in this case too.\qed   
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1007
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1008
  \end{proof} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1009
*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1010
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1011
section {* The Correctness Argument by Sulzmann and Lu\label{argu} *}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1012
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1013
text {*
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1014
%  \newcommand{\greedy}{\succcurlyeq_{gr}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1015
 \newcommand{\posix}{>}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1016
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1017
  An extended version of \cite{Sulzmann2014} is available at the website of
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1018
  its first author; this includes some ``proofs'', claimed in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1019
  \cite{Sulzmann2014} to be ``rigorous''. Since these are evidently not in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1020
  final form, we make no comment thereon, preferring to give general reasons
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1021
  for our belief that the approach of \cite{Sulzmann2014} is problematic.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1022
  Their central definition is an ``ordering relation'' defined by the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1023
  rules (slightly adapted to fit our notation):
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1024
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1025
\begin{center}  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1026
\begin{tabular}{@ {}c@ {\hspace{4mm}}c@ {}}	
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1027
@{thm[mode=Rule] C2[of "v\<^sub>1" "r\<^sub>1" "v\<^sub>1\<iota>" "v\<^sub>2" "r\<^sub>2" "v\<^sub>2\<iota>"]}\,(C2) &
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1028
@{thm[mode=Rule] C1[of "v\<^sub>2" "r\<^sub>2" "v\<^sub>2\<iota>" "v\<^sub>1" "r\<^sub>1"]}\,(C1)\smallskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1029
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1030
@{thm[mode=Rule] A1[of "v\<^sub>1" "v\<^sub>2" "r\<^sub>1" "r\<^sub>2"]}\,(A1) &
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1031
@{thm[mode=Rule] A2[of "v\<^sub>2" "v\<^sub>1" "r\<^sub>1" "r\<^sub>2"]}\,(A2)\smallskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1032
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1033
@{thm[mode=Rule] A3[of "v\<^sub>1" "r\<^sub>2" "v\<^sub>2" "r\<^sub>1"]}\,(A3) &
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1034
@{thm[mode=Rule] A4[of "v\<^sub>1" "r\<^sub>1" "v\<^sub>2" "r\<^sub>2"]}\,(A4)\smallskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1035
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1036
@{thm[mode=Rule] K1[of "v" "vs" "r"]}\,(K1) &
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1037
@{thm[mode=Rule] K2[of "v" "vs" "r"]}\,(K2)\smallskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1038
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1039
@{thm[mode=Rule] K3[of "v\<^sub>1" "r" "v\<^sub>2" "vs\<^sub>1" "vs\<^sub>2"]}\,(K3) &
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1040
@{thm[mode=Rule] K4[of "vs\<^sub>1" "r" "vs\<^sub>2" "v"]}\,(K4)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1041
\end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1042
\end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1043
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1044
  \noindent The idea behind the rules (A1) and (A2), for example, is that a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1045
  @{text Left}-value is bigger than a @{text Right}-value, if the underlying
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1046
  string of the @{text Left}-value is longer or of equal length to the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1047
  underlying string of the @{text Right}-value. The order is reversed,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1048
  however, if the @{text Right}-value can match a longer string than a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1049
  @{text Left}-value. In this way the POSIX value is supposed to be the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1050
  biggest value for a given string and regular expression.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1051
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1052
  Sulzmann and Lu explicitly refer to the paper \cite{Frisch2004} by Frisch
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1053
  and Cardelli from where they have taken the idea for their correctness
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1054
  proof. Frisch and Cardelli introduced a similar ordering for GREEDY
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1055
  matching and they showed that their GREEDY matching algorithm always
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1056
  produces a maximal element according to this ordering (from all possible
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1057
  solutions). The only difference between their GREEDY ordering and the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1058
  ``ordering'' by Sulzmann and Lu is that GREEDY always prefers a @{text
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1059
  Left}-value over a @{text Right}-value, no matter what the underlying
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1060
  string is. This seems to be only a very minor difference, but it has
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1061
  drastic consequences in terms of what properties both orderings enjoy.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1062
  What is interesting for our purposes is that the properties reflexivity,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1063
  totality and transitivity for this GREEDY ordering can be proved
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1064
  relatively easily by induction.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1065
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1066
  These properties of GREEDY, however, do not transfer to the POSIX
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1067
  ``ordering'' by Sulzmann and Lu, which they define as @{text "v\<^sub>1 \<ge>\<^sub>r v\<^sub>2"}. 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1068
  To start with, @{text "v\<^sub>1 \<ge>\<^sub>r v\<^sub>2"} is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1069
  not defined inductively, but as $($@{term "v\<^sub>1 = v\<^sub>2"}$)$ $\vee$ $($@{term "(v\<^sub>1 >r
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1070
  v\<^sub>2) \<and> (flat v\<^sub>1 = flat (v\<^sub>2::val))"}$)$. This means that @{term "v\<^sub>1
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1071
  >(r::rexp) (v\<^sub>2::val)"} does not necessarily imply @{term "v\<^sub>1 \<ge>(r::rexp)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1072
  (v\<^sub>2::val)"}. Moreover, transitivity does not hold in the ``usual''
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1073
  formulation, for example:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1074
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
  \begin{falsehood}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1076
  Suppose @{term "\<turnstile> v\<^sub>1 : r"}, @{term "\<turnstile> v\<^sub>2 : r"} and @{term "\<turnstile> v\<^sub>3 : r"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1077
  If @{term "v\<^sub>1 >(r::rexp) (v\<^sub>2::val)"} and @{term "v\<^sub>2 >(r::rexp) (v\<^sub>3::val)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1078
  then @{term "v\<^sub>1 >(r::rexp) (v\<^sub>3::val)"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1079
  \end{falsehood}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1080
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1081
  \noindent If formulated in this way, then there are various counter
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1082
  examples: For example let @{term r} be @{text "a + ((a + a)\<cdot>(a + \<zero>))"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1083
  then the @{term "v\<^sub>1"}, @{term "v\<^sub>2"} and @{term "v\<^sub>3"} below are values
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1084
  of @{term r}:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1085
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1086
  \begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1087
  \begin{tabular}{lcl}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1088
  @{term "v\<^sub>1"} & $=$ & @{term "Left(Char a)"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1089
  @{term "v\<^sub>2"} & $=$ & @{term "Right(Seq (Left(Char a)) (Right Void))"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1090
  @{term "v\<^sub>3"} & $=$ & @{term "Right(Seq (Right(Char a)) (Left(Char a)))"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1091
  \end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1092
  \end{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1093
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1094
  \noindent Moreover @{term "v\<^sub>1 >(r::rexp) v\<^sub>2"} and @{term "v\<^sub>2 >(r::rexp)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1095
  v\<^sub>3"}, but \emph{not} @{term "v\<^sub>1 >(r::rexp) v\<^sub>3"}! The reason is that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1096
  although @{term "v\<^sub>3"} is a @{text "Right"}-value, it can match a longer
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1097
  string, namely @{term "flat v\<^sub>3 = [a,a]"}, while @{term "flat v\<^sub>1"} (and
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1098
  @{term "flat v\<^sub>2"}) matches only @{term "[a]"}. So transitivity in this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1099
  formulation does not hold---in this example actually @{term "v\<^sub>3
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1100
  >(r::rexp) v\<^sub>1"}!
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1101
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1102
  Sulzmann and Lu ``fix'' this problem by weakening the transitivity
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1103
  property. They require in addition that the underlying strings are of the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1104
  same length. This excludes the counter example above and any
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1105
  counter-example we were able to find (by hand and by machine). Thus the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1106
  transitivity lemma should be formulated as:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1107
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1108
  \begin{conject}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1109
  Suppose @{term "\<turnstile> v\<^sub>1 : r"}, @{term "\<turnstile> v\<^sub>2 : r"} and @{term "\<turnstile> v\<^sub>3 : r"},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1110
  and also @{text "|v\<^sub>1| = |v\<^sub>2| = |v\<^sub>3|"}.\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1111
  If @{term "v\<^sub>1 >(r::rexp) (v\<^sub>2::val)"} and @{term "v\<^sub>2 >(r::rexp) (v\<^sub>3::val)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1112
  then @{term "v\<^sub>1 >(r::rexp) (v\<^sub>3::val)"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1113
  \end{conject}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1114
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1115
  \noindent While we agree with Sulzmann and Lu that this property
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1116
  probably(!) holds, proving it seems not so straightforward: although one
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1117
  begins with the assumption that the values have the same flattening, this
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1118
  cannot be maintained as one descends into the induction. This is a problem
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1119
  that occurs in a number of places in the proofs by Sulzmann and Lu.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1120
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1121
  Although they do not give an explicit proof of the transitivity property,
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1122
  they give a closely related property about the existence of maximal
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1123
  elements. They state that this can be verified by an induction on @{term r}. We
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1124
  disagree with this as we shall show next in case of transitivity. The case
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1125
  where the reasoning breaks down is the sequence case, say @{term "SEQ r\<^sub>1 r\<^sub>2"}.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1126
  The induction hypotheses in this case are
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1127
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1128
\begin{center}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1129
\begin{tabular}{@ {}c@ {\hspace{10mm}}c@ {}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1130
\begin{tabular}{@ {}l@ {\hspace{-7mm}}l@ {}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1131
IH @{term "r\<^sub>1"}:\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1132
@{text "\<forall> v\<^sub>1, v\<^sub>2, v\<^sub>3."} \\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1133
  & @{term "\<turnstile> v\<^sub>1 : r\<^sub>1"}\;@{text "\<and>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1134
    @{term "\<turnstile> v\<^sub>2 : r\<^sub>1"}\;@{text "\<and>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1135
    @{term "\<turnstile> v\<^sub>3 : r\<^sub>1"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1136
  & @{text "\<and>"} @{text "|v\<^sub>1| = |v\<^sub>2| = |v\<^sub>3|"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1137
  & @{text "\<and>"} @{term "v\<^sub>1 >(r\<^sub>1::rexp) v\<^sub>2 \<and> v\<^sub>2 >(r\<^sub>1::rexp) v\<^sub>3"}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1138
  & $\Rightarrow$ @{term "v\<^sub>1 >(r\<^sub>1::rexp) v\<^sub>3"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1139
\end{tabular} &
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1140
\begin{tabular}{@ {}l@ {\hspace{-7mm}}l@ {}}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1141
IH @{term "r\<^sub>2"}:\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1142
@{text "\<forall> v\<^sub>1, v\<^sub>2, v\<^sub>3."}\\ 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1143
  & @{term "\<turnstile> v\<^sub>1 : r\<^sub>2"}\;@{text "\<and>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1144
    @{term "\<turnstile> v\<^sub>2 : r\<^sub>2"}\;@{text "\<and>"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1145
    @{term "\<turnstile> v\<^sub>3 : r\<^sub>2"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1146
  & @{text "\<and>"} @{text "|v\<^sub>1| = |v\<^sub>2| = |v\<^sub>3|"}\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1147
  & @{text "\<and>"} @{term "v\<^sub>1 >(r\<^sub>2::rexp) v\<^sub>2 \<and> v\<^sub>2 >(r\<^sub>2::rexp) v\<^sub>3"}\medskip\\
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1148
  & $\Rightarrow$ @{term "v\<^sub>1 >(r\<^sub>2::rexp) v\<^sub>3"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1149
\end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1150
\end{tabular}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1151
\end{center} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1152
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1153
  \noindent We can assume that
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1154
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1155
  \begin{equation}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1156
  @{term "(Seq (v\<^sub>1\<^sub>l) (v\<^sub>1\<^sub>r)) >(SEQ r\<^sub>1 r\<^sub>2) (Seq (v\<^sub>2\<^sub>l) (v\<^sub>2\<^sub>r))"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1157
  \qquad\textrm{and}\qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1158
  @{term "(Seq (v\<^sub>2\<^sub>l) (v\<^sub>2\<^sub>r)) >(SEQ r\<^sub>1 r\<^sub>2) (Seq (v\<^sub>3\<^sub>l) (v\<^sub>3\<^sub>r))"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1159
  \label{assms}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1160
  \end{equation}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1161
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1162
  \noindent hold, and furthermore that the values have equal length, namely:
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1163
  %
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1164
  \begin{equation}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1165
  @{term "flat (Seq (v\<^sub>1\<^sub>l) (v\<^sub>1\<^sub>r)) = flat (Seq (v\<^sub>2\<^sub>l) (v\<^sub>2\<^sub>r))"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1166
  \qquad\textrm{and}\qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1167
  @{term "flat (Seq (v\<^sub>2\<^sub>l) (v\<^sub>2\<^sub>r)) = flat (Seq (v\<^sub>3\<^sub>l) (v\<^sub>3\<^sub>r))"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1168
  \label{lens}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1169
  \end{equation} 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1170
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1171
  \noindent We need to show that @{term "(Seq (v\<^sub>1\<^sub>l) (v\<^sub>1\<^sub>r)) >(SEQ r\<^sub>1 r\<^sub>2)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1172
  (Seq (v\<^sub>3\<^sub>l) (v\<^sub>3\<^sub>r))"} holds. We can proceed by analysing how the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1173
  assumptions in \eqref{assms} have arisen. There are four cases. Let us
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1174
  assume we are in the case where we know
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1175
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1176
  \[
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1177
  @{term "v\<^sub>1\<^sub>l >(r\<^sub>1::rexp) v\<^sub>2\<^sub>l"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1178
  \qquad\textrm{and}\qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1179
  @{term "v\<^sub>2\<^sub>l >(r\<^sub>1::rexp) v\<^sub>3\<^sub>l"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1180
  \]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1181
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1182
  \noindent and also know the corresponding inhabitation judgements. This is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1183
  exactly a case where we would like to apply the induction hypothesis
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1184
  IH~$r_1$. But we cannot! We still need to show that @{term "flat (v\<^sub>1\<^sub>l) =
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1185
  flat(v\<^sub>2\<^sub>l)"} and @{term "flat(v\<^sub>2\<^sub>l) = flat(v\<^sub>3\<^sub>l)"}. We know from
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1186
  \eqref{lens} that the lengths of the sequence values are equal, but from
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1187
  this we cannot infer anything about the lengths of the component values.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1188
  Indeed in general they will be unequal, that is
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1189
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1190
  \[
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1191
  @{term "flat(v\<^sub>1\<^sub>l) \<noteq> flat(v\<^sub>2\<^sub>l)"}  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1192
  \qquad\textrm{and}\qquad
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1193
  @{term "flat(v\<^sub>1\<^sub>r) \<noteq> flat(v\<^sub>2\<^sub>r)"}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1194
  \]
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1195
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1196
  \noindent but still \eqref{lens} will hold. Now we are stuck, since the IH
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1197
  does not apply. As said, this problem where the induction hypothesis does
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1198
  not apply arises in several places in the proof of Sulzmann and Lu, not
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1199
  just for proving transitivity.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1200
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1201
*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1202
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1203
section {* Conclusion *}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1204
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1205
text {*
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1206
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1207
  We have implemented the POSIX value calculation algorithm introduced by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1208
  Sulzmann and Lu
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1209
  \cite{Sulzmann2014}. Our implementation is nearly identical to the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1210
  original and all modifications we introduced are harmless (like our char-clause for
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1211
  @{text inj}). We have proved this algorithm to be correct, but correct
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1212
  according to our own specification of what POSIX values are. Our
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1213
  specification (inspired from work by Vansummeren \cite{Vansummeren2006}) appears to be
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1214
  much simpler than in \cite{Sulzmann2014} and our proofs are nearly always
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1215
  straightforward. We have attempted to formalise the original proof
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1216
  by Sulzmann and Lu \cite{Sulzmann2014}, but we believe it contains
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1217
  unfillable gaps. In the online version of \cite{Sulzmann2014}, the authors
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1218
  already acknowledge some small problems, but our experience suggests
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1219
  that there are more serious problems. 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1220
  
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1221
  Having proved the correctness of the POSIX lexing algorithm in
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1222
  \cite{Sulzmann2014}, which lessons have we learned? Well, this is a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1223
  perfect example for the importance of the \emph{right} definitions. We
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1224
  have (on and off) explored mechanisations as soon as first versions
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1225
  of \cite{Sulzmann2014} appeared, but have made little progress with
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1226
  turning the relatively detailed proof sketch in \cite{Sulzmann2014} into a
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1227
  formalisable proof. Having seen \cite{Vansummeren2006} and adapted the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1228
  POSIX definition given there for the algorithm by Sulzmann and Lu made all
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1229
  the difference: the proofs, as said, are nearly straightforward. The
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1230
  question remains whether the original proof idea of \cite{Sulzmann2014},
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1231
  potentially using our result as a stepping stone, can be made to work?
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1232
  Alas, we really do not know despite considerable effort.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1233
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1234
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1235
  Closely related to our work is an automata-based lexer formalised by
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1236
  Nipkow \cite{Nipkow98}. This lexer also splits up strings into longest
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1237
  initial substrings, but Nipkow's algorithm is not completely
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1238
  computational. The algorithm by Sulzmann and Lu, in contrast, can be
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1239
  implemented with ease in any functional language. A bespoke lexer for the
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1240
  Imp-language is formalised in Coq as part of the Software Foundations book
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1241
  by Pierce et al \cite{Pierce2015}. The disadvantage of such bespoke lexers is that they
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1242
  do not generalise easily to more advanced features.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1243
  Our formalisation is available from the Archive of Formal Proofs \cite{aduAFP16}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1244
  under \url{http://www.isa-afp.org/entries/Posix-Lexing.shtml}.\medskip
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1245
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1246
  \noindent
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1247
  {\bf Acknowledgements:}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1248
  We are very grateful to Martin Sulzmann for his comments on our work and 
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1249
  moreover for patiently explaining to us the details in \cite{Sulzmann2014}. We
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1250
  also received very helpful comments from James Cheney and anonymous referees.
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1251
  %  \small
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1252
  \bibliographystyle{plain}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1253
  \bibliography{root}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1254
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1255
*}
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1256
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1257
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1258
(*<*)
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1259
end
16af5b8bd285 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1260
(*>*)