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