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