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