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