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