thys2/Journal/Paper.thy
author Chengsong
Thu, 04 Nov 2021 13:52:17 +0000
changeset 376 664322da08fe
parent 372 78cc255e286f
child 378 ee53acaf2420
permissions -rw-r--r--
ordering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
367
Chengsong
parents:
diff changeset
     1
(*<*)
Chengsong
parents:
diff changeset
     2
theory Paper
Chengsong
parents:
diff changeset
     3
imports 
Chengsong
parents:
diff changeset
     4
   "../Lexer"
Chengsong
parents:
diff changeset
     5
   "../Simplifying" 
Chengsong
parents:
diff changeset
     6
   "../Positions"
Chengsong
parents:
diff changeset
     7
Chengsong
parents:
diff changeset
     8
   "../SizeBound" 
Chengsong
parents:
diff changeset
     9
   "HOL-Library.LaTeXsugar"
Chengsong
parents:
diff changeset
    10
begin
Chengsong
parents:
diff changeset
    11
Chengsong
parents:
diff changeset
    12
lemma Suc_0_fold:
Chengsong
parents:
diff changeset
    13
  "Suc 0 = 1"
Chengsong
parents:
diff changeset
    14
by simp
Chengsong
parents:
diff changeset
    15
Chengsong
parents:
diff changeset
    16
Chengsong
parents:
diff changeset
    17
Chengsong
parents:
diff changeset
    18
declare [[show_question_marks = false]]
Chengsong
parents:
diff changeset
    19
Chengsong
parents:
diff changeset
    20
syntax (latex output)
Chengsong
parents:
diff changeset
    21
  "_Collect" :: "pttrn => bool => 'a set"              ("(1{_ \<^latex>\<open>\\mbox{\\boldmath$\\mid$}\<close> _})")
Chengsong
parents:
diff changeset
    22
  "_CollectIn" :: "pttrn => 'a set => bool => 'a set"   ("(1{_ \<in> _ |e _})")
Chengsong
parents:
diff changeset
    23
Chengsong
parents:
diff changeset
    24
syntax
Chengsong
parents:
diff changeset
    25
  "_Not_Ex" :: "idts \<Rightarrow> bool \<Rightarrow> bool"  ("(3\<nexists>_.a./ _)" [0, 10] 10)
Chengsong
parents:
diff changeset
    26
  "_Not_Ex1" :: "pttrn \<Rightarrow> bool \<Rightarrow> bool"  ("(3\<nexists>!_.a./ _)" [0, 10] 10)
Chengsong
parents:
diff changeset
    27
Chengsong
parents:
diff changeset
    28
Chengsong
parents:
diff changeset
    29
abbreviation 
Chengsong
parents:
diff changeset
    30
  "der_syn r c \<equiv> der c r"
Chengsong
parents:
diff changeset
    31
Chengsong
parents:
diff changeset
    32
abbreviation 
Chengsong
parents:
diff changeset
    33
  "ders_syn r s \<equiv> ders s r"
Chengsong
parents:
diff changeset
    34
Chengsong
parents:
diff changeset
    35
  abbreviation 
Chengsong
parents:
diff changeset
    36
  "bder_syn r c \<equiv> bder c r"
Chengsong
parents:
diff changeset
    37
Chengsong
parents:
diff changeset
    38
abbreviation 
Chengsong
parents:
diff changeset
    39
  "bders_syn r s \<equiv> bders r s"
Chengsong
parents:
diff changeset
    40
Chengsong
parents:
diff changeset
    41
Chengsong
parents:
diff changeset
    42
abbreviation
Chengsong
parents:
diff changeset
    43
  "nprec v1 v2 \<equiv> \<not>(v1 :\<sqsubset>val v2)"
Chengsong
parents:
diff changeset
    44
Chengsong
parents:
diff changeset
    45
Chengsong
parents:
diff changeset
    46
Chengsong
parents:
diff changeset
    47
Chengsong
parents:
diff changeset
    48
notation (latex output)
Chengsong
parents:
diff changeset
    49
  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
Chengsong
parents:
diff changeset
    50
  Cons ("_\<^latex>\<open>\\mbox{$\\,$}\<close>::\<^latex>\<open>\\mbox{$\\,$}\<close>_" [75,73] 73) and  
Chengsong
parents:
diff changeset
    51
Chengsong
parents:
diff changeset
    52
  ZERO ("\<^bold>0" 81) and 
Chengsong
parents:
diff changeset
    53
  ONE ("\<^bold>1" 81) and 
Chengsong
parents:
diff changeset
    54
  CH ("_" [1000] 80) and
Chengsong
parents:
diff changeset
    55
  ALT ("_ + _" [77,77] 78) and
Chengsong
parents:
diff changeset
    56
  SEQ ("_ \<cdot> _" [77,77] 78) and
Chengsong
parents:
diff changeset
    57
  STAR ("_\<^sup>\<star>" [79] 78) and
Chengsong
parents:
diff changeset
    58
  
Chengsong
parents:
diff changeset
    59
  val.Void ("Empty" 78) and
Chengsong
parents:
diff changeset
    60
  val.Char ("Char _" [1000] 78) and
Chengsong
parents:
diff changeset
    61
  val.Left ("Left _" [79] 78) and
Chengsong
parents:
diff changeset
    62
  val.Right ("Right _" [1000] 78) and
Chengsong
parents:
diff changeset
    63
  val.Seq ("Seq _ _" [79,79] 78) and
Chengsong
parents:
diff changeset
    64
  val.Stars ("Stars _" [79] 78) and
Chengsong
parents:
diff changeset
    65
Chengsong
parents:
diff changeset
    66
  L ("L'(_')" [10] 78) and
Chengsong
parents:
diff changeset
    67
  LV ("LV _ _" [80,73] 78) and
Chengsong
parents:
diff changeset
    68
  der_syn ("_\\_" [79, 1000] 76) and  
Chengsong
parents:
diff changeset
    69
  ders_syn ("_\\_" [79, 1000] 76) and
Chengsong
parents:
diff changeset
    70
  flat ("|_|" [75] 74) and
Chengsong
parents:
diff changeset
    71
  flats ("|_|" [72] 74) and
Chengsong
parents:
diff changeset
    72
  Sequ ("_ @ _" [78,77] 63) and
Chengsong
parents:
diff changeset
    73
  injval ("inj _ _ _" [79,77,79] 76) and 
Chengsong
parents:
diff changeset
    74
  mkeps ("mkeps _" [79] 76) and 
Chengsong
parents:
diff changeset
    75
  length ("len _" [73] 73) and
Chengsong
parents:
diff changeset
    76
  intlen ("len _" [73] 73) and
Chengsong
parents:
diff changeset
    77
  set ("_" [73] 73) and
Chengsong
parents:
diff changeset
    78
 
Chengsong
parents:
diff changeset
    79
  Prf ("_ : _" [75,75] 75) and
Chengsong
parents:
diff changeset
    80
  Posix ("'(_, _') \<rightarrow> _" [63,75,75] 75) and
Chengsong
parents:
diff changeset
    81
 
Chengsong
parents:
diff changeset
    82
  lexer ("lexer _ _" [78,78] 77) and 
Chengsong
parents:
diff changeset
    83
  F_RIGHT ("F\<^bsub>Right\<^esub> _") and
Chengsong
parents:
diff changeset
    84
  F_LEFT ("F\<^bsub>Left\<^esub> _") and  
Chengsong
parents:
diff changeset
    85
  F_ALT ("F\<^bsub>Alt\<^esub> _ _") and
Chengsong
parents:
diff changeset
    86
  F_SEQ1 ("F\<^bsub>Seq1\<^esub> _ _") and
Chengsong
parents:
diff changeset
    87
  F_SEQ2 ("F\<^bsub>Seq2\<^esub> _ _") and
Chengsong
parents:
diff changeset
    88
  F_SEQ ("F\<^bsub>Seq\<^esub> _ _") and
Chengsong
parents:
diff changeset
    89
  simp_SEQ ("simp\<^bsub>Seq\<^esub> _ _" [1000, 1000] 1) and
Chengsong
parents:
diff changeset
    90
  simp_ALT ("simp\<^bsub>Alt\<^esub> _ _" [1000, 1000] 1) and
Chengsong
parents:
diff changeset
    91
  slexer ("lexer\<^sup>+" 1000) and
Chengsong
parents:
diff changeset
    92
Chengsong
parents:
diff changeset
    93
  at ("_\<^latex>\<open>\\mbox{$\\downharpoonleft$}\<close>\<^bsub>_\<^esub>") and
Chengsong
parents:
diff changeset
    94
  lex_list ("_ \<prec>\<^bsub>lex\<^esub> _") and
Chengsong
parents:
diff changeset
    95
  PosOrd ("_ \<prec>\<^bsub>_\<^esub> _" [77,77,77] 77) and
Chengsong
parents:
diff changeset
    96
  PosOrd_ex ("_ \<prec> _" [77,77] 77) and
Chengsong
parents:
diff changeset
    97
  PosOrd_ex_eq ("_ \<^latex>\<open>\\mbox{$\\preccurlyeq$}\<close> _" [77,77] 77) and
Chengsong
parents:
diff changeset
    98
  pflat_len ("\<parallel>_\<parallel>\<^bsub>_\<^esub>") and
Chengsong
parents:
diff changeset
    99
  nprec ("_ \<^latex>\<open>\\mbox{$\\not\\prec$}\<close> _" [77,77] 77) and
Chengsong
parents:
diff changeset
   100
Chengsong
parents:
diff changeset
   101
  bder_syn ("_\<^latex>\<open>\\mbox{$\\bbslash$}\<close>_" [79, 1000] 76) and  
Chengsong
parents:
diff changeset
   102
  bders_syn ("_\<^latex>\<open>\\mbox{$\\bbslash$}\<close>_" [79, 1000] 76) and
Chengsong
parents:
diff changeset
   103
  intern ("_\<^latex>\<open>\\mbox{$^\\uparrow$}\<close>" [900] 80) and
Chengsong
parents:
diff changeset
   104
  erase ("_\<^latex>\<open>\\mbox{$^\\downarrow$}\<close>" [1000] 74) and
Chengsong
parents:
diff changeset
   105
  bnullable ("nullable\<^latex>\<open>\\mbox{$_b$}\<close> _" [1000] 80) and
Chengsong
parents:
diff changeset
   106
  bmkeps ("mkeps\<^latex>\<open>\\mbox{$_b$}\<close> _" [1000] 80) and
Chengsong
parents:
diff changeset
   107
  blexer ("lexer\<^latex>\<open>\\mbox{$_b$}\<close> _ _" [77, 77] 80) and
Chengsong
parents:
diff changeset
   108
  code ("code _" [79] 74) and
Chengsong
parents:
diff changeset
   109
Chengsong
parents:
diff changeset
   110
  DUMMY ("\<^latex>\<open>\\underline{\\hspace{2mm}}\<close>")
Chengsong
parents:
diff changeset
   111
Chengsong
parents:
diff changeset
   112
Chengsong
parents:
diff changeset
   113
definition 
Chengsong
parents:
diff changeset
   114
  "match r s \<equiv> nullable (ders s r)"
Chengsong
parents:
diff changeset
   115
Chengsong
parents:
diff changeset
   116
Chengsong
parents:
diff changeset
   117
lemma LV_STAR_ONE_empty: 
Chengsong
parents:
diff changeset
   118
  shows "LV (STAR ONE) [] = {Stars []}"
Chengsong
parents:
diff changeset
   119
by(auto simp add: LV_def elim: Prf.cases intro: Prf.intros)
Chengsong
parents:
diff changeset
   120
Chengsong
parents:
diff changeset
   121
Chengsong
parents:
diff changeset
   122
Chengsong
parents:
diff changeset
   123
(*
Chengsong
parents:
diff changeset
   124
comments not implemented
Chengsong
parents:
diff changeset
   125
Chengsong
parents:
diff changeset
   126
p9. The condition "not exists s3 s4..." appears often enough (in particular in
Chengsong
parents:
diff changeset
   127
the proof of Lemma 3) to warrant a definition.
Chengsong
parents:
diff changeset
   128
Chengsong
parents:
diff changeset
   129
*)
Chengsong
parents:
diff changeset
   130
Chengsong
parents:
diff changeset
   131
Chengsong
parents:
diff changeset
   132
(*>*)
Chengsong
parents:
diff changeset
   133
371
Chengsong
parents: 367
diff changeset
   134
section\<open>Core of the proof\<close>
367
Chengsong
parents:
diff changeset
   135
text \<open>
371
Chengsong
parents: 367
diff changeset
   136
This paper builds on previous work by Ausaf and Urban using 
367
Chengsong
parents:
diff changeset
   137
regular expression'd bit-coded derivatives to do lexing that 
371
Chengsong
parents: 367
diff changeset
   138
is both fast and satisfies the POSIX specification.
367
Chengsong
parents:
diff changeset
   139
In their work, a bit-coded algorithm introduced by Sulzmann and Lu
Chengsong
parents:
diff changeset
   140
was formally verified in Isabelle, by a very clever use of
Chengsong
parents:
diff changeset
   141
flex function and retrieve to carefully mimic the way a value is 
Chengsong
parents:
diff changeset
   142
built up by the injection funciton.
Chengsong
parents:
diff changeset
   143
Chengsong
parents:
diff changeset
   144
In the previous work, Ausaf and Urban established the below equality:
Chengsong
parents:
diff changeset
   145
\begin{lemma}
371
Chengsong
parents: 367
diff changeset
   146
@{thm [mode=IfThen] MAIN_decode}
Chengsong
parents: 367
diff changeset
   147
\end{lemma}
Chengsong
parents: 367
diff changeset
   148
Chengsong
parents: 367
diff changeset
   149
This lemma establishes a link with the lexer without bit-codes.
Chengsong
parents: 367
diff changeset
   150
Chengsong
parents: 367
diff changeset
   151
With it we get the correctness of bit-coded algorithm.
Chengsong
parents: 367
diff changeset
   152
\begin{lemma}
Chengsong
parents: 367
diff changeset
   153
@{thm [mode=IfThen] blexer_correctness}
367
Chengsong
parents:
diff changeset
   154
\end{lemma}
Chengsong
parents:
diff changeset
   155
371
Chengsong
parents: 367
diff changeset
   156
However what is not certain is whether we can add simplification
Chengsong
parents: 367
diff changeset
   157
to the bit-coded algorithm, without breaking the correct lexing output.
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   158
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   159
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   160
The reason that we do need to add a simplification phase
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   161
after each derivative step of  $\textit{blexer}$ is
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   162
because it produces intermediate
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   163
regular expressions that can grow exponentially.
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   164
For example, the regular expression $(a+aa)^*$ after taking
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   165
derivative against just 10 $a$s will have size 8192.
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   166
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   167
%TODO: add figure for this?
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   168
376
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   169
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   170
Therefore, we insert a simplification phase
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   171
after each derivation step, as defined below:
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   172
\begin{lemma}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   173
@{thm blexer_simp_def}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   174
\end{lemma}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   175
376
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   176
The simplification function is given as follows:
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   177
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   178
\begin{center}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   179
  \begin{tabular}{lcl}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   180
  @{thm (lhs) bsimp.simps(1)[of  "bs" "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{thm (rhs) bsimp.simps(1)[of  "bs" "r\<^sub>1" "r\<^sub>2"]}\\
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   181
  @{thm (lhs) bsimp.simps(2)} & $\dn$ & @{thm (rhs) bsimp.simps(2)}\\
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   182
  @{thm (lhs) bsimp.simps(3)} & $\dn$ & @{thm (rhs) bsimp.simps(3)}\\
376
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   183
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   184
\end{tabular}
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   185
\end{center}
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   186
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   187
And the two helper functions are:
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   188
\begin{center}
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   189
  \begin{tabular}{lcl}
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   190
  @{thm (lhs) bsimp_AALTs.simps(2)[of  "bs\<^sub>1" "r" ]} & $\dn$ & @{thm (rhs) bsimp.simps(1)[of  "bs\<^sub>1" "r" ]}\\
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   191
  @{thm (lhs) bsimp_AALTs.simps(2)} & $\dn$ & @{thm (rhs) bsimp.simps(2)}\\
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   192
  @{thm (lhs) bsimp_AALTs.simps(3)} & $\dn$ & @{thm (rhs) bsimp.simps(3)}\\
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   193
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   194
\end{tabular}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   195
\end{center}
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   196
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   197
371
Chengsong
parents: 367
diff changeset
   198
This might sound trivial in the case of producing a YES/NO answer,
Chengsong
parents: 367
diff changeset
   199
but once we require a lexing output to be produced (which is required
Chengsong
parents: 367
diff changeset
   200
in applications like compiler front-end, malicious attack domain extraction, 
Chengsong
parents: 367
diff changeset
   201
etc.), it is not straightforward if we still extract what is needed according
Chengsong
parents: 367
diff changeset
   202
to the POSIX standard.
Chengsong
parents: 367
diff changeset
   203
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   204
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   205
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   206
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   207
371
Chengsong
parents: 367
diff changeset
   208
By simplification, we mean specifically the following rules:
Chengsong
parents: 367
diff changeset
   209
Chengsong
parents: 367
diff changeset
   210
\begin{center}
Chengsong
parents: 367
diff changeset
   211
  \begin{tabular}{lcl}
Chengsong
parents: 367
diff changeset
   212
  @{thm[mode=Axiom] rrewrite.intros(1)[of "bs" "r\<^sub>2"]}\\
Chengsong
parents: 367
diff changeset
   213
  @{thm[mode=Axiom] rrewrite.intros(2)[of "bs" "r\<^sub>1"]}\\
Chengsong
parents: 367
diff changeset
   214
  @{thm[mode=Axiom] rrewrite.intros(3)[of  "bs" "bs\<^sub>1" "r\<^sub>1"]}\\
Chengsong
parents: 367
diff changeset
   215
  @{thm[mode=Rule] rrewrite.intros(4)[of  "r\<^sub>1" "r\<^sub>2" "bs" "r\<^sub>3"]}\\
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   216
  @{thm[mode=Rule] rrewrite.intros(5)[of "r\<^sub>3" "r\<^sub>4" "bs" "r\<^sub>1"]}\\
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   217
  @{thm[mode=Rule] rrewrite.intros(6)[of "r" "r'" "bs" "rs\<^sub>1" "rs\<^sub>2"]}\\
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   218
  @{thm[mode=Axiom] rrewrite.intros(7)[of "bs" "rs\<^sub>a" "rs\<^sub>b"]}\\
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   219
  @{thm[mode=Axiom] rrewrite.intros(8)[of "bs" "rs\<^sub>a" "bs\<^sub>1" "rs\<^sub>1" "rs\<^sub>b"]}\\
376
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   220
  @{thm[mode=Axiom] rrewrite.intros(9)[of "bs" "bs\<^sub>1" "rs"]}\\
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   221
  @{thm[mode=Axiom] rrewrite.intros(10)[of "bs" ]}\\
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   222
  @{thm[mode=Axiom] rrewrite.intros(11)[of "bs" "r\<^sub>1"]}\\
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   223
  @{thm[mode=Rule] rrewrite.intros(12)[of "a\<^sub>1" "a\<^sub>2" "bs" "rs\<^sub>a" "rs\<^sub>b" "rs\<^sub>c"]}\\
371
Chengsong
parents: 367
diff changeset
   224
Chengsong
parents: 367
diff changeset
   225
  \end{tabular}
Chengsong
parents: 367
diff changeset
   226
\end{center}
Chengsong
parents: 367
diff changeset
   227
Chengsong
parents: 367
diff changeset
   228
Chengsong
parents: 367
diff changeset
   229
And these can be made compact by the following simplification function:
Chengsong
parents: 367
diff changeset
   230
Chengsong
parents: 367
diff changeset
   231
where the function $\mathit{bsimp_AALTs}$
Chengsong
parents: 367
diff changeset
   232
Chengsong
parents: 367
diff changeset
   233
The core idea of the proof is that two regular expressions,
Chengsong
parents: 367
diff changeset
   234
if "isomorphic" up to a finite number of rewrite steps, will
Chengsong
parents: 367
diff changeset
   235
remain "isomorphic" when we take the same sequence of
Chengsong
parents: 367
diff changeset
   236
derivatives on both of them.
Chengsong
parents: 367
diff changeset
   237
This can be expressed by the following rewrite relation lemma:
Chengsong
parents: 367
diff changeset
   238
\begin{lemma}
Chengsong
parents: 367
diff changeset
   239
@{thm [mode=IfThen] central}
Chengsong
parents: 367
diff changeset
   240
\end{lemma}
Chengsong
parents: 367
diff changeset
   241
Chengsong
parents: 367
diff changeset
   242
This isomorphic relation implies a property that leads to the 
Chengsong
parents: 367
diff changeset
   243
correctness result: 
Chengsong
parents: 367
diff changeset
   244
if two (nullable) regular expressions are "rewritable" in many steps
Chengsong
parents: 367
diff changeset
   245
from one another, 
372
78cc255e286f some more writing
Chengsong
parents: 371
diff changeset
   246
then a call to function $\textit{bmkeps}$ gives the same
371
Chengsong
parents: 367
diff changeset
   247
bit-sequence :
Chengsong
parents: 367
diff changeset
   248
\begin{lemma}
Chengsong
parents: 367
diff changeset
   249
@{thm [mode=IfThen] rewrites_bmkeps}
Chengsong
parents: 367
diff changeset
   250
\end{lemma}
Chengsong
parents: 367
diff changeset
   251
Chengsong
parents: 367
diff changeset
   252
Given the same bit-sequence, the decode function
Chengsong
parents: 367
diff changeset
   253
will give out the same value, which is the output
Chengsong
parents: 367
diff changeset
   254
of both lexers:
Chengsong
parents: 367
diff changeset
   255
\begin{lemma}
Chengsong
parents: 367
diff changeset
   256
@{thm blexer_def}
Chengsong
parents: 367
diff changeset
   257
\end{lemma}
Chengsong
parents: 367
diff changeset
   258
Chengsong
parents: 367
diff changeset
   259
\begin{lemma}
Chengsong
parents: 367
diff changeset
   260
@{thm blexer_simp_def}
Chengsong
parents: 367
diff changeset
   261
\end{lemma}
Chengsong
parents: 367
diff changeset
   262
Chengsong
parents: 367
diff changeset
   263
And that yields the correctness result:
Chengsong
parents: 367
diff changeset
   264
\begin{lemma}
Chengsong
parents: 367
diff changeset
   265
@{thm blexersimp_correctness}
Chengsong
parents: 367
diff changeset
   266
\end{lemma}
Chengsong
parents: 367
diff changeset
   267
376
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   268
The nice thing about the aove
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   269
\<close>
371
Chengsong
parents: 367
diff changeset
   270
376
664322da08fe ordering
Chengsong
parents: 372
diff changeset
   271
371
Chengsong
parents: 367
diff changeset
   272
Chengsong
parents: 367
diff changeset
   273
section \<open>Introduction\<close>
Chengsong
parents: 367
diff changeset
   274
Chengsong
parents: 367
diff changeset
   275
Chengsong
parents: 367
diff changeset
   276
text \<open>
Chengsong
parents: 367
diff changeset
   277
367
Chengsong
parents:
diff changeset
   278
Chengsong
parents:
diff changeset
   279
Brzozowski \cite{Brzozowski1964} introduced the notion of the {\em
Chengsong
parents:
diff changeset
   280
derivative} @{term "der c r"} of a regular expression \<open>r\<close> w.r.t.\
Chengsong
parents:
diff changeset
   281
a character~\<open>c\<close>, and showed that it gave a simple solution to the
Chengsong
parents:
diff changeset
   282
problem of matching a string @{term s} with a regular expression @{term
Chengsong
parents:
diff changeset
   283
r}: if the derivative of @{term r} w.r.t.\ (in succession) all the
Chengsong
parents:
diff changeset
   284
characters of the string matches the empty string, then @{term r}
Chengsong
parents:
diff changeset
   285
matches @{term s} (and {\em vice versa}). The derivative has the
Chengsong
parents:
diff changeset
   286
property (which may almost be regarded as its specification) that, for
Chengsong
parents:
diff changeset
   287
every string @{term s} and regular expression @{term r} and character
Chengsong
parents:
diff changeset
   288
@{term c}, one has @{term "cs \<in> L(r)"} if and only if \mbox{@{term "s \<in> L(der c r)"}}. 
Chengsong
parents:
diff changeset
   289
The beauty of Brzozowski's derivatives is that
Chengsong
parents:
diff changeset
   290
they are neatly expressible in any functional language, and easily
Chengsong
parents:
diff changeset
   291
definable and reasoned about in theorem provers---the definitions just
Chengsong
parents:
diff changeset
   292
consist of inductive datatypes and simple recursive functions. A
Chengsong
parents:
diff changeset
   293
mechanised correctness proof of Brzozowski's matcher in for example HOL4
Chengsong
parents:
diff changeset
   294
has been mentioned by Owens and Slind~\cite{Owens2008}. Another one in
Chengsong
parents:
diff changeset
   295
Isabelle/HOL is part of the work by Krauss and Nipkow \cite{Krauss2011}.
Chengsong
parents:
diff changeset
   296
And another one in Coq is given by Coquand and Siles \cite{Coquand2012}.
Chengsong
parents:
diff changeset
   297
Chengsong
parents:
diff changeset
   298
If a regular expression matches a string, then in general there is more
Chengsong
parents:
diff changeset
   299
than one way of how the string is matched. There are two commonly used
Chengsong
parents:
diff changeset
   300
disambiguation strategies to generate a unique answer: one is called
Chengsong
parents:
diff changeset
   301
GREEDY matching \cite{Frisch2004} and the other is POSIX
Chengsong
parents:
diff changeset
   302
matching~\cite{POSIX,Kuklewicz,OkuiSuzuki2010,Sulzmann2014,Vansummeren2006}.
Chengsong
parents:
diff changeset
   303
For example consider the string @{term xy} and the regular expression
Chengsong
parents:
diff changeset
   304
\mbox{@{term "STAR (ALT (ALT x y) xy)"}}. Either the string can be
Chengsong
parents:
diff changeset
   305
matched in two `iterations' by the single letter-regular expressions
Chengsong
parents:
diff changeset
   306
@{term x} and @{term y}, or directly in one iteration by @{term xy}. The
Chengsong
parents:
diff changeset
   307
first case corresponds to GREEDY matching, which first matches with the
Chengsong
parents:
diff changeset
   308
left-most symbol and only matches the next symbol in case of a mismatch
Chengsong
parents:
diff changeset
   309
(this is greedy in the sense of preferring instant gratification to
Chengsong
parents:
diff changeset
   310
delayed repletion). The second case is POSIX matching, which prefers the
Chengsong
parents:
diff changeset
   311
longest match.
Chengsong
parents:
diff changeset
   312
Chengsong
parents:
diff changeset
   313
In the context of lexing, where an input string needs to be split up
Chengsong
parents:
diff changeset
   314
into a sequence of tokens, POSIX is the more natural disambiguation
Chengsong
parents:
diff changeset
   315
strategy for what programmers consider basic syntactic building blocks
Chengsong
parents:
diff changeset
   316
in their programs.  These building blocks are often specified by some
Chengsong
parents:
diff changeset
   317
regular expressions, say \<open>r\<^bsub>key\<^esub>\<close> and \<open>r\<^bsub>id\<^esub>\<close> for recognising keywords and identifiers,
Chengsong
parents:
diff changeset
   318
respectively. There are a few underlying (informal) rules behind
Chengsong
parents:
diff changeset
   319
tokenising a string in a POSIX \cite{POSIX} fashion:
Chengsong
parents:
diff changeset
   320
Chengsong
parents:
diff changeset
   321
\begin{itemize} 
Chengsong
parents:
diff changeset
   322
\item[$\bullet$] \emph{The Longest Match Rule} (or \emph{``{M}aximal {M}unch {R}ule''}):
Chengsong
parents:
diff changeset
   323
The longest initial substring matched by any regular expression is taken as
Chengsong
parents:
diff changeset
   324
next token.\smallskip
Chengsong
parents:
diff changeset
   325
Chengsong
parents:
diff changeset
   326
\item[$\bullet$] \emph{Priority Rule:}
Chengsong
parents:
diff changeset
   327
For a particular longest initial substring, the first (leftmost) regular expression
Chengsong
parents:
diff changeset
   328
that can match determines the token.\smallskip
Chengsong
parents:
diff changeset
   329
Chengsong
parents:
diff changeset
   330
\item[$\bullet$] \emph{Star Rule:} A subexpression repeated by ${}^\star$ shall 
Chengsong
parents:
diff changeset
   331
not match an empty string unless this is the only match for the repetition.\smallskip
Chengsong
parents:
diff changeset
   332
Chengsong
parents:
diff changeset
   333
\item[$\bullet$] \emph{Empty String Rule:} An empty string shall be considered to 
Chengsong
parents:
diff changeset
   334
be longer than no match at all.
Chengsong
parents:
diff changeset
   335
\end{itemize}
Chengsong
parents:
diff changeset
   336
Chengsong
parents:
diff changeset
   337
\noindent Consider for example a regular expression \<open>r\<^bsub>key\<^esub>\<close> for recognising keywords such as \<open>if\<close>,
Chengsong
parents:
diff changeset
   338
\<open>then\<close> and so on; and \<open>r\<^bsub>id\<^esub>\<close>
Chengsong
parents:
diff changeset
   339
recognising identifiers (say, a single character followed by
Chengsong
parents:
diff changeset
   340
characters or numbers).  Then we can form the regular expression
Chengsong
parents:
diff changeset
   341
\<open>(r\<^bsub>key\<^esub> + r\<^bsub>id\<^esub>)\<^sup>\<star>\<close>
Chengsong
parents:
diff changeset
   342
and use POSIX matching to tokenise strings, say \<open>iffoo\<close> and
Chengsong
parents:
diff changeset
   343
\<open>if\<close>.  For \<open>iffoo\<close> we obtain by the Longest Match Rule
Chengsong
parents:
diff changeset
   344
a single identifier token, not a keyword followed by an
Chengsong
parents:
diff changeset
   345
identifier. For \<open>if\<close> we obtain by the Priority Rule a keyword
Chengsong
parents:
diff changeset
   346
token, not an identifier token---even if \<open>r\<^bsub>id\<^esub>\<close>
Chengsong
parents:
diff changeset
   347
matches also. By the Star Rule we know \<open>(r\<^bsub>key\<^esub> +
Chengsong
parents:
diff changeset
   348
r\<^bsub>id\<^esub>)\<^sup>\<star>\<close> matches \<open>iffoo\<close>,
Chengsong
parents:
diff changeset
   349
respectively \<open>if\<close>, in exactly one `iteration' of the star. The
Chengsong
parents:
diff changeset
   350
Empty String Rule is for cases where, for example, the regular expression 
Chengsong
parents:
diff changeset
   351
\<open>(a\<^sup>\<star>)\<^sup>\<star>\<close> matches against the
Chengsong
parents:
diff changeset
   352
string \<open>bc\<close>. Then the longest initial matched substring is the
Chengsong
parents:
diff changeset
   353
empty string, which is matched by both the whole regular expression
Chengsong
parents:
diff changeset
   354
and the parenthesised subexpression.
Chengsong
parents:
diff changeset
   355
Chengsong
parents:
diff changeset
   356
Chengsong
parents:
diff changeset
   357
One limitation of Brzozowski's matcher is that it only generates a
Chengsong
parents:
diff changeset
   358
YES/NO answer for whether a string is being matched by a regular
Chengsong
parents:
diff changeset
   359
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this matcher
Chengsong
parents:
diff changeset
   360
to allow generation not just of a YES/NO answer but of an actual
Chengsong
parents:
diff changeset
   361
matching, called a [lexical] {\em value}. Assuming a regular
Chengsong
parents:
diff changeset
   362
expression matches a string, values encode the information of
Chengsong
parents:
diff changeset
   363
\emph{how} the string is matched by the regular expression---that is,
Chengsong
parents:
diff changeset
   364
which part of the string is matched by which part of the regular
Chengsong
parents:
diff changeset
   365
expression. For this consider again the string \<open>xy\<close> and
Chengsong
parents:
diff changeset
   366
the regular expression \mbox{\<open>(x + (y + xy))\<^sup>\<star>\<close>}
Chengsong
parents:
diff changeset
   367
(this time fully parenthesised). We can view this regular expression
Chengsong
parents:
diff changeset
   368
as tree and if the string \<open>xy\<close> is matched by two Star
Chengsong
parents:
diff changeset
   369
`iterations', then the \<open>x\<close> is matched by the left-most
Chengsong
parents:
diff changeset
   370
alternative in this tree and the \<open>y\<close> by the right-left alternative. This
Chengsong
parents:
diff changeset
   371
suggests to record this matching as
Chengsong
parents:
diff changeset
   372
Chengsong
parents:
diff changeset
   373
\begin{center}
Chengsong
parents:
diff changeset
   374
@{term "Stars [Left(Char x), Right(Left(Char y))]"}
Chengsong
parents:
diff changeset
   375
\end{center}
Chengsong
parents:
diff changeset
   376
Chengsong
parents:
diff changeset
   377
\noindent where @{const Stars}, \<open>Left\<close>, \<open>Right\<close> and \<open>Char\<close> are constructors for values. \<open>Stars\<close> records how many
Chengsong
parents:
diff changeset
   378
iterations were used; \<open>Left\<close>, respectively \<open>Right\<close>, which
Chengsong
parents:
diff changeset
   379
alternative is used. This `tree view' leads naturally to the idea that
Chengsong
parents:
diff changeset
   380
regular expressions act as types and values as inhabiting those types
Chengsong
parents:
diff changeset
   381
(see, for example, \cite{HosoyaVouillonPierce2005}).  The value for
Chengsong
parents:
diff changeset
   382
matching \<open>xy\<close> in a single `iteration', i.e.~the POSIX value,
Chengsong
parents:
diff changeset
   383
would look as follows
Chengsong
parents:
diff changeset
   384
Chengsong
parents:
diff changeset
   385
\begin{center}
Chengsong
parents:
diff changeset
   386
@{term "Stars [Seq (Char x) (Char y)]"}
Chengsong
parents:
diff changeset
   387
\end{center}
Chengsong
parents:
diff changeset
   388
Chengsong
parents:
diff changeset
   389
\noindent where @{const Stars} has only a single-element list for the
Chengsong
parents:
diff changeset
   390
single iteration and @{const Seq} indicates that @{term xy} is matched 
Chengsong
parents:
diff changeset
   391
by a sequence regular expression.
Chengsong
parents:
diff changeset
   392
Chengsong
parents:
diff changeset
   393
%, which we will in what follows 
Chengsong
parents:
diff changeset
   394
%write more formally as @{term "SEQ x y"}.
Chengsong
parents:
diff changeset
   395
Chengsong
parents:
diff changeset
   396
Chengsong
parents:
diff changeset
   397
Sulzmann and Lu give a simple algorithm to calculate a value that
Chengsong
parents:
diff changeset
   398
appears to be the value associated with POSIX matching.  The challenge
Chengsong
parents:
diff changeset
   399
then is to specify that value, in an algorithm-independent fashion,
Chengsong
parents:
diff changeset
   400
and to show that Sulzmann and Lu's derivative-based algorithm does
Chengsong
parents:
diff changeset
   401
indeed calculate a value that is correct according to the
Chengsong
parents:
diff changeset
   402
specification.  The answer given by Sulzmann and Lu
Chengsong
parents:
diff changeset
   403
\cite{Sulzmann2014} is to define a relation (called an ``order
Chengsong
parents:
diff changeset
   404
relation'') on the set of values of @{term r}, and to show that (once
Chengsong
parents:
diff changeset
   405
a string to be matched is chosen) there is a maximum element and that
Chengsong
parents:
diff changeset
   406
it is computed by their derivative-based algorithm. This proof idea is
Chengsong
parents:
diff changeset
   407
inspired by work of Frisch and Cardelli \cite{Frisch2004} on a GREEDY
Chengsong
parents:
diff changeset
   408
regular expression matching algorithm. However, we were not able to
Chengsong
parents:
diff changeset
   409
establish transitivity and totality for the ``order relation'' by
Chengsong
parents:
diff changeset
   410
Sulzmann and Lu.  There are some inherent problems with their approach
Chengsong
parents:
diff changeset
   411
(of which some of the proofs are not published in
Chengsong
parents:
diff changeset
   412
\cite{Sulzmann2014}); perhaps more importantly, we give in this paper
Chengsong
parents:
diff changeset
   413
a simple inductive (and algorithm-independent) definition of what we
Chengsong
parents:
diff changeset
   414
call being a {\em POSIX value} for a regular expression @{term r} and
Chengsong
parents:
diff changeset
   415
a string @{term s}; we show that the algorithm by Sulzmann and Lu
Chengsong
parents:
diff changeset
   416
computes such a value and that such a value is unique. Our proofs are
Chengsong
parents:
diff changeset
   417
both done by hand and checked in Isabelle/HOL.  The experience of
Chengsong
parents:
diff changeset
   418
doing our proofs has been that this mechanical checking was absolutely
Chengsong
parents:
diff changeset
   419
essential: this subject area has hidden snares. This was also noted by
Chengsong
parents:
diff changeset
   420
Kuklewicz \cite{Kuklewicz} who found that nearly all POSIX matching
Chengsong
parents:
diff changeset
   421
implementations are ``buggy'' \cite[Page 203]{Sulzmann2014} and by
Chengsong
parents:
diff changeset
   422
Grathwohl et al \cite[Page 36]{CrashCourse2014} who wrote:
Chengsong
parents:
diff changeset
   423
Chengsong
parents:
diff changeset
   424
\begin{quote}
Chengsong
parents:
diff changeset
   425
\it{}``The POSIX strategy is more complicated than the greedy because of 
Chengsong
parents:
diff changeset
   426
the dependence on information about the length of matched strings in the 
Chengsong
parents:
diff changeset
   427
various subexpressions.''
Chengsong
parents:
diff changeset
   428
\end{quote}
Chengsong
parents:
diff changeset
   429
Chengsong
parents:
diff changeset
   430
Chengsong
parents:
diff changeset
   431
Chengsong
parents:
diff changeset
   432
\noindent {\bf Contributions:} We have implemented in Isabelle/HOL the
Chengsong
parents:
diff changeset
   433
derivative-based regular expression matching algorithm of
Chengsong
parents:
diff changeset
   434
Sulzmann and Lu \cite{Sulzmann2014}. We have proved the correctness of this
Chengsong
parents:
diff changeset
   435
algorithm according to our specification of what a POSIX value is (inspired
Chengsong
parents:
diff changeset
   436
by work of Vansummeren \cite{Vansummeren2006}). Sulzmann
Chengsong
parents:
diff changeset
   437
and Lu sketch in \cite{Sulzmann2014} an informal correctness proof: but to
Chengsong
parents:
diff changeset
   438
us it contains unfillable gaps.\footnote{An extended version of
Chengsong
parents:
diff changeset
   439
\cite{Sulzmann2014} is available at the website of its first author; this
Chengsong
parents:
diff changeset
   440
extended version already includes remarks in the appendix that their
Chengsong
parents:
diff changeset
   441
informal proof contains gaps, and possible fixes are not fully worked out.}
Chengsong
parents:
diff changeset
   442
Our specification of a POSIX value consists of a simple inductive definition
Chengsong
parents:
diff changeset
   443
that given a string and a regular expression uniquely determines this value.
Chengsong
parents:
diff changeset
   444
We also show that our definition is equivalent to an ordering 
Chengsong
parents:
diff changeset
   445
of values based on positions by Okui and Suzuki \cite{OkuiSuzuki2010}.
Chengsong
parents:
diff changeset
   446
Chengsong
parents:
diff changeset
   447
%Derivatives as calculated by Brzozowski's method are usually more complex
Chengsong
parents:
diff changeset
   448
%regular expressions than the initial one; various optimisations are
Chengsong
parents:
diff changeset
   449
%possible. We prove the correctness when simplifications of @{term "ALT ZERO r"}, 
Chengsong
parents:
diff changeset
   450
%@{term "ALT r ZERO"}, @{term "SEQ ONE r"} and @{term "SEQ r ONE"} to
Chengsong
parents:
diff changeset
   451
%@{term r} are applied. 
Chengsong
parents:
diff changeset
   452
Chengsong
parents:
diff changeset
   453
We extend our results to ??? Bitcoded version??
Chengsong
parents:
diff changeset
   454
Chengsong
parents:
diff changeset
   455
\<close>
Chengsong
parents:
diff changeset
   456
371
Chengsong
parents: 367
diff changeset
   457
Chengsong
parents: 367
diff changeset
   458
Chengsong
parents: 367
diff changeset
   459
367
Chengsong
parents:
diff changeset
   460
section \<open>Preliminaries\<close>
Chengsong
parents:
diff changeset
   461
Chengsong
parents:
diff changeset
   462
text \<open>\noindent Strings in Isabelle/HOL are lists of characters with
Chengsong
parents:
diff changeset
   463
the empty string being represented by the empty list, written @{term
Chengsong
parents:
diff changeset
   464
"[]"}, and list-cons being written as @{term "DUMMY # DUMMY"}. Often
Chengsong
parents:
diff changeset
   465
we use the usual bracket notation for lists also for strings; for
Chengsong
parents:
diff changeset
   466
example a string consisting of just a single character @{term c} is
Chengsong
parents:
diff changeset
   467
written @{term "[c]"}. We use the usual definitions for 
Chengsong
parents:
diff changeset
   468
\emph{prefixes} and \emph{strict prefixes} of strings.  By using the
Chengsong
parents:
diff changeset
   469
type @{type char} for characters we have a supply of finitely many
Chengsong
parents:
diff changeset
   470
characters roughly corresponding to the ASCII character set. Regular
Chengsong
parents:
diff changeset
   471
expressions are defined as usual as the elements of the following
Chengsong
parents:
diff changeset
   472
inductive datatype:
Chengsong
parents:
diff changeset
   473
Chengsong
parents:
diff changeset
   474
  \begin{center}
Chengsong
parents:
diff changeset
   475
  \<open>r :=\<close>
Chengsong
parents:
diff changeset
   476
  @{const "ZERO"} $\mid$
Chengsong
parents:
diff changeset
   477
  @{const "ONE"} $\mid$
Chengsong
parents:
diff changeset
   478
  @{term "CH c"} $\mid$
Chengsong
parents:
diff changeset
   479
  @{term "ALT r\<^sub>1 r\<^sub>2"} $\mid$
Chengsong
parents:
diff changeset
   480
  @{term "SEQ r\<^sub>1 r\<^sub>2"} $\mid$
Chengsong
parents:
diff changeset
   481
  @{term "STAR r"} 
Chengsong
parents:
diff changeset
   482
  \end{center}
Chengsong
parents:
diff changeset
   483
Chengsong
parents:
diff changeset
   484
  \noindent where @{const ZERO} stands for the regular expression that does
Chengsong
parents:
diff changeset
   485
  not match any string, @{const ONE} for the regular expression that matches
Chengsong
parents:
diff changeset
   486
  only the empty string and @{term c} for matching a character literal. The
Chengsong
parents:
diff changeset
   487
  language of a regular expression is also defined as usual by the
Chengsong
parents:
diff changeset
   488
  recursive function @{term L} with the six clauses:
Chengsong
parents:
diff changeset
   489
Chengsong
parents:
diff changeset
   490
  \begin{center}
Chengsong
parents:
diff changeset
   491
  \begin{tabular}{l@ {\hspace{4mm}}rcl}
Chengsong
parents:
diff changeset
   492
  \textit{(1)} & @{thm (lhs) L.simps(1)} & $\dn$ & @{thm (rhs) L.simps(1)}\\
Chengsong
parents:
diff changeset
   493
  \textit{(2)} & @{thm (lhs) L.simps(2)} & $\dn$ & @{thm (rhs) L.simps(2)}\\
Chengsong
parents:
diff changeset
   494
  \textit{(3)} & @{thm (lhs) L.simps(3)} & $\dn$ & @{thm (rhs) L.simps(3)}\\
Chengsong
parents:
diff changeset
   495
  \textit{(4)} & @{thm (lhs) L.simps(4)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & 
Chengsong
parents:
diff changeset
   496
        @{thm (rhs) L.simps(4)[of "r\<^sub>1" "r\<^sub>2"]}\\
Chengsong
parents:
diff changeset
   497
  \textit{(5)} & @{thm (lhs) L.simps(5)[of "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & 
Chengsong
parents:
diff changeset
   498
        @{thm (rhs) L.simps(5)[of "r\<^sub>1" "r\<^sub>2"]}\\
Chengsong
parents:
diff changeset
   499
  \textit{(6)} & @{thm (lhs) L.simps(6)} & $\dn$ & @{thm (rhs) L.simps(6)}\\
Chengsong
parents:
diff changeset
   500
  \end{tabular}
Chengsong
parents:
diff changeset
   501
  \end{center}
Chengsong
parents:
diff changeset
   502
  
Chengsong
parents:
diff changeset
   503
  \noindent In clause \textit{(4)} we use the operation @{term "DUMMY ;;
Chengsong
parents:
diff changeset
   504
  DUMMY"} for the concatenation of two languages (it is also list-append for
Chengsong
parents:
diff changeset
   505
  strings). We use the star-notation for regular expressions and for
Chengsong
parents:
diff changeset
   506
  languages (in the last clause above). The star for languages is defined
Chengsong
parents:
diff changeset
   507
  inductively by two clauses: \<open>(i)\<close> the empty string being in
Chengsong
parents:
diff changeset
   508
  the star of a language and \<open>(ii)\<close> if @{term "s\<^sub>1"} is in a
Chengsong
parents:
diff changeset
   509
  language and @{term "s\<^sub>2"} in the star of this language, then also @{term
Chengsong
parents:
diff changeset
   510
  "s\<^sub>1 @ s\<^sub>2"} is in the star of this language. It will also be convenient
Chengsong
parents:
diff changeset
   511
  to use the following notion of a \emph{semantic derivative} (or \emph{left
Chengsong
parents:
diff changeset
   512
  quotient}) of a language defined as
Chengsong
parents:
diff changeset
   513
  %
Chengsong
parents:
diff changeset
   514
  \begin{center}
Chengsong
parents:
diff changeset
   515
  @{thm Der_def}\;.
Chengsong
parents:
diff changeset
   516
  \end{center}
Chengsong
parents:
diff changeset
   517
 
Chengsong
parents:
diff changeset
   518
  \noindent
Chengsong
parents:
diff changeset
   519
  For semantic derivatives we have the following equations (for example
Chengsong
parents:
diff changeset
   520
  mechanically proved in \cite{Krauss2011}):
Chengsong
parents:
diff changeset
   521
  %
Chengsong
parents:
diff changeset
   522
  \begin{equation}\label{SemDer}
Chengsong
parents:
diff changeset
   523
  \begin{array}{lcl}
Chengsong
parents:
diff changeset
   524
  @{thm (lhs) Der_null}  & \dn & @{thm (rhs) Der_null}\\
Chengsong
parents:
diff changeset
   525
  @{thm (lhs) Der_empty}  & \dn & @{thm (rhs) Der_empty}\\
Chengsong
parents:
diff changeset
   526
  @{thm (lhs) Der_char}  & \dn & @{thm (rhs) Der_char}\\
Chengsong
parents:
diff changeset
   527
  @{thm (lhs) Der_union}  & \dn & @{thm (rhs) Der_union}\\
Chengsong
parents:
diff changeset
   528
  @{thm (lhs) Der_Sequ}  & \dn & @{thm (rhs) Der_Sequ}\\
Chengsong
parents:
diff changeset
   529
  @{thm (lhs) Der_star}  & \dn & @{thm (rhs) Der_star}
Chengsong
parents:
diff changeset
   530
  \end{array}
Chengsong
parents:
diff changeset
   531
  \end{equation}
Chengsong
parents:
diff changeset
   532
Chengsong
parents:
diff changeset
   533
Chengsong
parents:
diff changeset
   534
  \noindent \emph{\Brz's derivatives} of regular expressions
Chengsong
parents:
diff changeset
   535
  \cite{Brzozowski1964} can be easily defined by two recursive functions:
Chengsong
parents:
diff changeset
   536
  the first is from regular expressions to booleans (implementing a test
Chengsong
parents:
diff changeset
   537
  when a regular expression can match the empty string), and the second
Chengsong
parents:
diff changeset
   538
  takes a regular expression and a character to a (derivative) regular
Chengsong
parents:
diff changeset
   539
  expression:
Chengsong
parents:
diff changeset
   540
Chengsong
parents:
diff changeset
   541
  \begin{center}
Chengsong
parents:
diff changeset
   542
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   543
  @{thm (lhs) nullable.simps(1)} & $\dn$ & @{thm (rhs) nullable.simps(1)}\\
Chengsong
parents:
diff changeset
   544
  @{thm (lhs) nullable.simps(2)} & $\dn$ & @{thm (rhs) nullable.simps(2)}\\
Chengsong
parents:
diff changeset
   545
  @{thm (lhs) nullable.simps(3)} & $\dn$ & @{thm (rhs) nullable.simps(3)}\\
Chengsong
parents:
diff changeset
   546
  @{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"]}\\
Chengsong
parents:
diff changeset
   547
  @{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"]}\\
Chengsong
parents:
diff changeset
   548
  @{thm (lhs) nullable.simps(6)} & $\dn$ & @{thm (rhs) nullable.simps(6)}\medskip\\
Chengsong
parents:
diff changeset
   549
Chengsong
parents:
diff changeset
   550
%  \end{tabular}
Chengsong
parents:
diff changeset
   551
%  \end{center}
Chengsong
parents:
diff changeset
   552
Chengsong
parents:
diff changeset
   553
%  \begin{center}
Chengsong
parents:
diff changeset
   554
%  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   555
Chengsong
parents:
diff changeset
   556
  @{thm (lhs) der.simps(1)} & $\dn$ & @{thm (rhs) der.simps(1)}\\
Chengsong
parents:
diff changeset
   557
  @{thm (lhs) der.simps(2)} & $\dn$ & @{thm (rhs) der.simps(2)}\\
Chengsong
parents:
diff changeset
   558
  @{thm (lhs) der.simps(3)} & $\dn$ & @{thm (rhs) der.simps(3)}\\
Chengsong
parents:
diff changeset
   559
  @{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"]}\\
Chengsong
parents:
diff changeset
   560
  @{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"]}\\
Chengsong
parents:
diff changeset
   561
  @{thm (lhs) der.simps(6)} & $\dn$ & @{thm (rhs) der.simps(6)}
Chengsong
parents:
diff changeset
   562
  \end{tabular}
Chengsong
parents:
diff changeset
   563
  \end{center}
Chengsong
parents:
diff changeset
   564
 
Chengsong
parents:
diff changeset
   565
  \noindent
Chengsong
parents:
diff changeset
   566
  We may extend this definition to give derivatives w.r.t.~strings:
Chengsong
parents:
diff changeset
   567
Chengsong
parents:
diff changeset
   568
  \begin{center}
Chengsong
parents:
diff changeset
   569
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   570
  @{thm (lhs) ders.simps(1)} & $\dn$ & @{thm (rhs) ders.simps(1)}\\
Chengsong
parents:
diff changeset
   571
  @{thm (lhs) ders.simps(2)} & $\dn$ & @{thm (rhs) ders.simps(2)}\\
Chengsong
parents:
diff changeset
   572
  \end{tabular}
Chengsong
parents:
diff changeset
   573
  \end{center}
Chengsong
parents:
diff changeset
   574
Chengsong
parents:
diff changeset
   575
  \noindent Given the equations in \eqref{SemDer}, it is a relatively easy
Chengsong
parents:
diff changeset
   576
  exercise in mechanical reasoning to establish that
Chengsong
parents:
diff changeset
   577
Chengsong
parents:
diff changeset
   578
  \begin{proposition}\label{derprop}\mbox{}\\ 
Chengsong
parents:
diff changeset
   579
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
   580
  \textit{(1)} & @{thm (lhs) nullable_correctness} if and only if
Chengsong
parents:
diff changeset
   581
  @{thm (rhs) nullable_correctness}, and \\ 
Chengsong
parents:
diff changeset
   582
  \textit{(2)} & @{thm[mode=IfThen] der_correctness}.
Chengsong
parents:
diff changeset
   583
  \end{tabular}
Chengsong
parents:
diff changeset
   584
  \end{proposition}
Chengsong
parents:
diff changeset
   585
Chengsong
parents:
diff changeset
   586
  \noindent With this in place it is also very routine to prove that the
Chengsong
parents:
diff changeset
   587
  regular expression matcher defined as
Chengsong
parents:
diff changeset
   588
  %
Chengsong
parents:
diff changeset
   589
  \begin{center}
Chengsong
parents:
diff changeset
   590
  @{thm match_def}
Chengsong
parents:
diff changeset
   591
  \end{center}
Chengsong
parents:
diff changeset
   592
Chengsong
parents:
diff changeset
   593
  \noindent gives a positive answer if and only if @{term "s \<in> L r"}.
Chengsong
parents:
diff changeset
   594
  Consequently, this regular expression matching algorithm satisfies the
Chengsong
parents:
diff changeset
   595
  usual specification for regular expression matching. While the matcher
Chengsong
parents:
diff changeset
   596
  above calculates a provably correct YES/NO answer for whether a regular
Chengsong
parents:
diff changeset
   597
  expression matches a string or not, the novel idea of Sulzmann and Lu
Chengsong
parents:
diff changeset
   598
  \cite{Sulzmann2014} is to append another phase to this algorithm in order
Chengsong
parents:
diff changeset
   599
  to calculate a [lexical] value. We will explain the details next.
Chengsong
parents:
diff changeset
   600
Chengsong
parents:
diff changeset
   601
\<close>
Chengsong
parents:
diff changeset
   602
Chengsong
parents:
diff changeset
   603
section \<open>POSIX Regular Expression Matching\label{posixsec}\<close>
Chengsong
parents:
diff changeset
   604
Chengsong
parents:
diff changeset
   605
text \<open>
Chengsong
parents:
diff changeset
   606
Chengsong
parents:
diff changeset
   607
  There have been many previous works that use values for encoding 
Chengsong
parents:
diff changeset
   608
  \emph{how} a regular expression matches a string.
Chengsong
parents:
diff changeset
   609
  The clever idea by Sulzmann and Lu \cite{Sulzmann2014} is to 
Chengsong
parents:
diff changeset
   610
  define a function on values that mirrors (but inverts) the
Chengsong
parents:
diff changeset
   611
  construction of the derivative on regular expressions. \emph{Values}
Chengsong
parents:
diff changeset
   612
  are defined as the inductive datatype
Chengsong
parents:
diff changeset
   613
Chengsong
parents:
diff changeset
   614
  \begin{center}
Chengsong
parents:
diff changeset
   615
  \<open>v :=\<close>
Chengsong
parents:
diff changeset
   616
  @{const "Void"} $\mid$
Chengsong
parents:
diff changeset
   617
  @{term "val.Char c"} $\mid$
Chengsong
parents:
diff changeset
   618
  @{term "Left v"} $\mid$
Chengsong
parents:
diff changeset
   619
  @{term "Right v"} $\mid$
Chengsong
parents:
diff changeset
   620
  @{term "Seq v\<^sub>1 v\<^sub>2"} $\mid$ 
Chengsong
parents:
diff changeset
   621
  @{term "Stars vs"} 
Chengsong
parents:
diff changeset
   622
  \end{center}  
Chengsong
parents:
diff changeset
   623
Chengsong
parents:
diff changeset
   624
  \noindent where we use @{term vs} to stand for a list of
Chengsong
parents:
diff changeset
   625
  values. (This is similar to the approach taken by Frisch and
Chengsong
parents:
diff changeset
   626
  Cardelli for GREEDY matching \cite{Frisch2004}, and Sulzmann and Lu
Chengsong
parents:
diff changeset
   627
  for POSIX matching \cite{Sulzmann2014}). The string underlying a
Chengsong
parents:
diff changeset
   628
  value can be calculated by the @{const flat} function, written
Chengsong
parents:
diff changeset
   629
  @{term "flat DUMMY"} and defined as:
Chengsong
parents:
diff changeset
   630
Chengsong
parents:
diff changeset
   631
  \begin{center}
Chengsong
parents:
diff changeset
   632
  \begin{tabular}[t]{lcl}
Chengsong
parents:
diff changeset
   633
  @{thm (lhs) flat.simps(1)} & $\dn$ & @{thm (rhs) flat.simps(1)}\\
Chengsong
parents:
diff changeset
   634
  @{thm (lhs) flat.simps(2)} & $\dn$ & @{thm (rhs) flat.simps(2)}\\
Chengsong
parents:
diff changeset
   635
  @{thm (lhs) flat.simps(3)} & $\dn$ & @{thm (rhs) flat.simps(3)}\\
Chengsong
parents:
diff changeset
   636
  @{thm (lhs) flat.simps(4)} & $\dn$ & @{thm (rhs) flat.simps(4)}
Chengsong
parents:
diff changeset
   637
  \end{tabular}\hspace{14mm}
Chengsong
parents:
diff changeset
   638
  \begin{tabular}[t]{lcl}
Chengsong
parents:
diff changeset
   639
  @{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"]}\\
Chengsong
parents:
diff changeset
   640
  @{thm (lhs) flat.simps(6)} & $\dn$ & @{thm (rhs) flat.simps(6)}\\
Chengsong
parents:
diff changeset
   641
  @{thm (lhs) flat.simps(7)} & $\dn$ & @{thm (rhs) flat.simps(7)}\\
Chengsong
parents:
diff changeset
   642
  \end{tabular}
Chengsong
parents:
diff changeset
   643
  \end{center}
Chengsong
parents:
diff changeset
   644
Chengsong
parents:
diff changeset
   645
  \noindent We will sometimes refer to the underlying string of a
Chengsong
parents:
diff changeset
   646
  value as \emph{flattened value}.  We will also overload our notation and 
Chengsong
parents:
diff changeset
   647
  use @{term "flats vs"} for flattening a list of values and concatenating
Chengsong
parents:
diff changeset
   648
  the resulting strings.
Chengsong
parents:
diff changeset
   649
  
Chengsong
parents:
diff changeset
   650
  Sulzmann and Lu define
Chengsong
parents:
diff changeset
   651
  inductively an \emph{inhabitation relation} that associates values to
Chengsong
parents:
diff changeset
   652
  regular expressions. We define this relation as
Chengsong
parents:
diff changeset
   653
  follows:\footnote{Note that the rule for @{term Stars} differs from
Chengsong
parents:
diff changeset
   654
  our earlier paper \cite{AusafDyckhoffUrban2016}. There we used the
Chengsong
parents:
diff changeset
   655
  original definition by Sulzmann and Lu which does not require that
Chengsong
parents:
diff changeset
   656
  the values @{term "v \<in> set vs"} flatten to a non-empty
Chengsong
parents:
diff changeset
   657
  string. The reason for introducing the more restricted version of
Chengsong
parents:
diff changeset
   658
  lexical values is convenience later on when reasoning about an
Chengsong
parents:
diff changeset
   659
  ordering relation for values.}
Chengsong
parents:
diff changeset
   660
Chengsong
parents:
diff changeset
   661
  \begin{center}
Chengsong
parents:
diff changeset
   662
  \begin{tabular}{c@ {\hspace{12mm}}c}\label{prfintros}
Chengsong
parents:
diff changeset
   663
  \\[-8mm]
Chengsong
parents:
diff changeset
   664
  @{thm[mode=Axiom] Prf.intros(4)} & 
Chengsong
parents:
diff changeset
   665
  @{thm[mode=Axiom] Prf.intros(5)[of "c"]}\\[4mm]
Chengsong
parents:
diff changeset
   666
  @{thm[mode=Rule] Prf.intros(2)[of "v\<^sub>1" "r\<^sub>1" "r\<^sub>2"]} &
Chengsong
parents:
diff changeset
   667
  @{thm[mode=Rule] Prf.intros(3)[of "v\<^sub>2" "r\<^sub>1" "r\<^sub>2"]}\\[4mm]
Chengsong
parents:
diff changeset
   668
  @{thm[mode=Rule] Prf.intros(1)[of "v\<^sub>1" "r\<^sub>1" "v\<^sub>2" "r\<^sub>2"]}  &
Chengsong
parents:
diff changeset
   669
  @{thm[mode=Rule] Prf.intros(6)[of "vs"]}
Chengsong
parents:
diff changeset
   670
  \end{tabular}
Chengsong
parents:
diff changeset
   671
  \end{center}
Chengsong
parents:
diff changeset
   672
Chengsong
parents:
diff changeset
   673
  \noindent where in the clause for @{const "Stars"} we use the
Chengsong
parents:
diff changeset
   674
  notation @{term "v \<in> set vs"} for indicating that \<open>v\<close> is a
Chengsong
parents:
diff changeset
   675
  member in the list \<open>vs\<close>.  We require in this rule that every
Chengsong
parents:
diff changeset
   676
  value in @{term vs} flattens to a non-empty string. The idea is that
Chengsong
parents:
diff changeset
   677
  @{term "Stars"}-values satisfy the informal Star Rule (see Introduction)
Chengsong
parents:
diff changeset
   678
  where the $^\star$ does not match the empty string unless this is
Chengsong
parents:
diff changeset
   679
  the only match for the repetition.  Note also that no values are
Chengsong
parents:
diff changeset
   680
  associated with the regular expression @{term ZERO}, and that the
Chengsong
parents:
diff changeset
   681
  only value associated with the regular expression @{term ONE} is
Chengsong
parents:
diff changeset
   682
  @{term Void}.  It is routine to establish how values ``inhabiting''
Chengsong
parents:
diff changeset
   683
  a regular expression correspond to the language of a regular
Chengsong
parents:
diff changeset
   684
  expression, namely
Chengsong
parents:
diff changeset
   685
Chengsong
parents:
diff changeset
   686
  \begin{proposition}\label{inhabs}
Chengsong
parents:
diff changeset
   687
  @{thm L_flat_Prf}
Chengsong
parents:
diff changeset
   688
  \end{proposition}
Chengsong
parents:
diff changeset
   689
Chengsong
parents:
diff changeset
   690
  \noindent
Chengsong
parents:
diff changeset
   691
  Given a regular expression \<open>r\<close> and a string \<open>s\<close>, we define the 
Chengsong
parents:
diff changeset
   692
  set of all \emph{Lexical Values} inhabited by \<open>r\<close> with the underlying string 
Chengsong
parents:
diff changeset
   693
  being \<open>s\<close>:\footnote{Okui and Suzuki refer to our lexical values 
Chengsong
parents:
diff changeset
   694
  as \emph{canonical values} in \cite{OkuiSuzuki2010}. The notion of \emph{non-problematic
Chengsong
parents:
diff changeset
   695
  values} by Cardelli and Frisch \cite{Frisch2004} is related, but not identical
Chengsong
parents:
diff changeset
   696
  to our lexical values.}
Chengsong
parents:
diff changeset
   697
  
Chengsong
parents:
diff changeset
   698
  \begin{center}
Chengsong
parents:
diff changeset
   699
  @{thm LV_def}
Chengsong
parents:
diff changeset
   700
  \end{center}
Chengsong
parents:
diff changeset
   701
Chengsong
parents:
diff changeset
   702
  \noindent The main property of @{term "LV r s"} is that it is alway finite.
Chengsong
parents:
diff changeset
   703
Chengsong
parents:
diff changeset
   704
  \begin{proposition}
Chengsong
parents:
diff changeset
   705
  @{thm LV_finite}
Chengsong
parents:
diff changeset
   706
  \end{proposition}
Chengsong
parents:
diff changeset
   707
Chengsong
parents:
diff changeset
   708
  \noindent This finiteness property does not hold in general if we
Chengsong
parents:
diff changeset
   709
  remove the side-condition about @{term "flat v \<noteq> []"} in the
Chengsong
parents:
diff changeset
   710
  @{term Stars}-rule above. For example using Sulzmann and Lu's
Chengsong
parents:
diff changeset
   711
  less restrictive definition, @{term "LV (STAR ONE) []"} would contain
Chengsong
parents:
diff changeset
   712
  infinitely many values, but according to our more restricted
Chengsong
parents:
diff changeset
   713
  definition only a single value, namely @{thm LV_STAR_ONE_empty}.
Chengsong
parents:
diff changeset
   714
Chengsong
parents:
diff changeset
   715
  If a regular expression \<open>r\<close> matches a string \<open>s\<close>, then
Chengsong
parents:
diff changeset
   716
  generally the set @{term "LV r s"} is not just a singleton set.  In
Chengsong
parents:
diff changeset
   717
  case of POSIX matching the problem is to calculate the unique lexical value
Chengsong
parents:
diff changeset
   718
  that satisfies the (informal) POSIX rules from the Introduction.
Chengsong
parents:
diff changeset
   719
  Graphically the POSIX value calculation algorithm by Sulzmann and Lu
Chengsong
parents:
diff changeset
   720
  can be illustrated by the picture in Figure~\ref{Sulz} where the
Chengsong
parents:
diff changeset
   721
  path from the left to the right involving @{term
Chengsong
parents:
diff changeset
   722
  derivatives}/@{const nullable} is the first phase of the algorithm
Chengsong
parents:
diff changeset
   723
  (calculating successive \Brz's derivatives) and @{const
Chengsong
parents:
diff changeset
   724
  mkeps}/\<open>inj\<close>, the path from right to left, the second
Chengsong
parents:
diff changeset
   725
  phase. This picture shows the steps required when a regular
Chengsong
parents:
diff changeset
   726
  expression, say \<open>r\<^sub>1\<close>, matches the string @{term
Chengsong
parents:
diff changeset
   727
  "[a,b,c]"}. We first build the three derivatives (according to
Chengsong
parents:
diff changeset
   728
  @{term a}, @{term b} and @{term c}). We then use @{const nullable}
Chengsong
parents:
diff changeset
   729
  to find out whether the resulting derivative regular expression
Chengsong
parents:
diff changeset
   730
  @{term "r\<^sub>4"} can match the empty string. If yes, we call the
Chengsong
parents:
diff changeset
   731
  function @{const mkeps} that produces a value @{term "v\<^sub>4"}
Chengsong
parents:
diff changeset
   732
  for how @{term "r\<^sub>4"} can match the empty string (taking into
Chengsong
parents:
diff changeset
   733
  account the POSIX constraints in case there are several ways). This
Chengsong
parents:
diff changeset
   734
  function is defined by the clauses:
Chengsong
parents:
diff changeset
   735
Chengsong
parents:
diff changeset
   736
\begin{figure}[t]
Chengsong
parents:
diff changeset
   737
\begin{center}
Chengsong
parents:
diff changeset
   738
\begin{tikzpicture}[scale=2,node distance=1.3cm,
Chengsong
parents:
diff changeset
   739
                    every node/.style={minimum size=6mm}]
Chengsong
parents:
diff changeset
   740
\node (r1)  {@{term "r\<^sub>1"}};
Chengsong
parents:
diff changeset
   741
\node (r2) [right=of r1]{@{term "r\<^sub>2"}};
Chengsong
parents:
diff changeset
   742
\draw[->,line width=1mm](r1)--(r2) node[above,midway] {@{term "der a DUMMY"}};
Chengsong
parents:
diff changeset
   743
\node (r3) [right=of r2]{@{term "r\<^sub>3"}};
Chengsong
parents:
diff changeset
   744
\draw[->,line width=1mm](r2)--(r3) node[above,midway] {@{term "der b DUMMY"}};
Chengsong
parents:
diff changeset
   745
\node (r4) [right=of r3]{@{term "r\<^sub>4"}};
Chengsong
parents:
diff changeset
   746
\draw[->,line width=1mm](r3)--(r4) node[above,midway] {@{term "der c DUMMY"}};
Chengsong
parents:
diff changeset
   747
\draw (r4) node[anchor=west] {\;\raisebox{3mm}{@{term nullable}}};
Chengsong
parents:
diff changeset
   748
\node (v4) [below=of r4]{@{term "v\<^sub>4"}};
Chengsong
parents:
diff changeset
   749
\draw[->,line width=1mm](r4) -- (v4);
Chengsong
parents:
diff changeset
   750
\node (v3) [left=of v4] {@{term "v\<^sub>3"}};
Chengsong
parents:
diff changeset
   751
\draw[->,line width=1mm](v4)--(v3) node[below,midway] {\<open>inj r\<^sub>3 c\<close>};
Chengsong
parents:
diff changeset
   752
\node (v2) [left=of v3]{@{term "v\<^sub>2"}};
Chengsong
parents:
diff changeset
   753
\draw[->,line width=1mm](v3)--(v2) node[below,midway] {\<open>inj r\<^sub>2 b\<close>};
Chengsong
parents:
diff changeset
   754
\node (v1) [left=of v2] {@{term "v\<^sub>1"}};
Chengsong
parents:
diff changeset
   755
\draw[->,line width=1mm](v2)--(v1) node[below,midway] {\<open>inj r\<^sub>1 a\<close>};
Chengsong
parents:
diff changeset
   756
\draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{@{term "mkeps"}}};
Chengsong
parents:
diff changeset
   757
\end{tikzpicture}
Chengsong
parents:
diff changeset
   758
\end{center}
Chengsong
parents:
diff changeset
   759
\mbox{}\\[-13mm]
Chengsong
parents:
diff changeset
   760
Chengsong
parents:
diff changeset
   761
\caption{The two phases of the algorithm by Sulzmann \& Lu \cite{Sulzmann2014},
Chengsong
parents:
diff changeset
   762
matching the string @{term "[a,b,c]"}. The first phase (the arrows from 
Chengsong
parents:
diff changeset
   763
left to right) is \Brz's matcher building successive derivatives. If the 
Chengsong
parents:
diff changeset
   764
last regular expression is @{term nullable}, then the functions of the 
Chengsong
parents:
diff changeset
   765
second phase are called (the top-down and right-to-left arrows): first 
Chengsong
parents:
diff changeset
   766
@{term mkeps} calculates a value @{term "v\<^sub>4"} witnessing
Chengsong
parents:
diff changeset
   767
how the empty string has been recognised by @{term "r\<^sub>4"}. After
Chengsong
parents:
diff changeset
   768
that the function @{term inj} ``injects back'' the characters of the string into
Chengsong
parents:
diff changeset
   769
the values.
Chengsong
parents:
diff changeset
   770
\label{Sulz}}
Chengsong
parents:
diff changeset
   771
\end{figure} 
Chengsong
parents:
diff changeset
   772
Chengsong
parents:
diff changeset
   773
  \begin{center}
Chengsong
parents:
diff changeset
   774
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   775
  @{thm (lhs) mkeps.simps(1)} & $\dn$ & @{thm (rhs) mkeps.simps(1)}\\
Chengsong
parents:
diff changeset
   776
  @{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"]}\\
Chengsong
parents:
diff changeset
   777
  @{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"]}\\
Chengsong
parents:
diff changeset
   778
  @{thm (lhs) mkeps.simps(4)} & $\dn$ & @{thm (rhs) mkeps.simps(4)}\\
Chengsong
parents:
diff changeset
   779
  \end{tabular}
Chengsong
parents:
diff changeset
   780
  \end{center}
Chengsong
parents:
diff changeset
   781
Chengsong
parents:
diff changeset
   782
  \noindent Note that this function needs only to be partially defined,
Chengsong
parents:
diff changeset
   783
  namely only for regular expressions that are nullable. In case @{const
Chengsong
parents:
diff changeset
   784
  nullable} fails, the string @{term "[a,b,c]"} cannot be matched by @{term
Chengsong
parents:
diff changeset
   785
  "r\<^sub>1"} and the null value @{term "None"} is returned. Note also how this function
Chengsong
parents:
diff changeset
   786
  makes some subtle choices leading to a POSIX value: for example if an
Chengsong
parents:
diff changeset
   787
  alternative regular expression, say @{term "ALT r\<^sub>1 r\<^sub>2"}, can
Chengsong
parents:
diff changeset
   788
  match the empty string and furthermore @{term "r\<^sub>1"} can match the
Chengsong
parents:
diff changeset
   789
  empty string, then we return a \<open>Left\<close>-value. The \<open>Right\<close>-value will only be returned if @{term "r\<^sub>1"} cannot match the empty
Chengsong
parents:
diff changeset
   790
  string.
Chengsong
parents:
diff changeset
   791
Chengsong
parents:
diff changeset
   792
  The most interesting idea from Sulzmann and Lu \cite{Sulzmann2014} is
Chengsong
parents:
diff changeset
   793
  the construction of a value for how @{term "r\<^sub>1"} can match the
Chengsong
parents:
diff changeset
   794
  string @{term "[a,b,c]"} from the value how the last derivative, @{term
Chengsong
parents:
diff changeset
   795
  "r\<^sub>4"} in Fig.~\ref{Sulz}, can match the empty string. Sulzmann and
Chengsong
parents:
diff changeset
   796
  Lu achieve this by stepwise ``injecting back'' the characters into the
Chengsong
parents:
diff changeset
   797
  values thus inverting the operation of building derivatives, but on the level
Chengsong
parents:
diff changeset
   798
  of values. The corresponding function, called @{term inj}, takes three
Chengsong
parents:
diff changeset
   799
  arguments, a regular expression, a character and a value. For example in
Chengsong
parents:
diff changeset
   800
  the first (or right-most) @{term inj}-step in Fig.~\ref{Sulz} the regular
Chengsong
parents:
diff changeset
   801
  expression @{term "r\<^sub>3"}, the character @{term c} from the last
Chengsong
parents:
diff changeset
   802
  derivative step and @{term "v\<^sub>4"}, which is the value corresponding
Chengsong
parents:
diff changeset
   803
  to the derivative regular expression @{term "r\<^sub>4"}. The result is
Chengsong
parents:
diff changeset
   804
  the new value @{term "v\<^sub>3"}. The final result of the algorithm is
Chengsong
parents:
diff changeset
   805
  the value @{term "v\<^sub>1"}. The @{term inj} function is defined by recursion on regular
Chengsong
parents:
diff changeset
   806
  expressions and by analysing the shape of values (corresponding to 
Chengsong
parents:
diff changeset
   807
  the derivative regular expressions).
Chengsong
parents:
diff changeset
   808
  %
Chengsong
parents:
diff changeset
   809
  \begin{center}
Chengsong
parents:
diff changeset
   810
  \begin{tabular}{l@ {\hspace{5mm}}lcl}
Chengsong
parents:
diff changeset
   811
  \textit{(1)} & @{thm (lhs) injval.simps(1)} & $\dn$ & @{thm (rhs) injval.simps(1)}\\
Chengsong
parents:
diff changeset
   812
  \textit{(2)} & @{thm (lhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]} & $\dn$ & 
Chengsong
parents:
diff changeset
   813
      @{thm (rhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]}\\
Chengsong
parents:
diff changeset
   814
  \textit{(3)} & @{thm (lhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ & 
Chengsong
parents:
diff changeset
   815
      @{thm (rhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
   816
  \textit{(4)} & @{thm (lhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ 
Chengsong
parents:
diff changeset
   817
      & @{thm (rhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
   818
  \textit{(5)} & @{thm (lhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ 
Chengsong
parents:
diff changeset
   819
      & @{thm (rhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
   820
  \textit{(6)} & @{thm (lhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ 
Chengsong
parents:
diff changeset
   821
      & @{thm (rhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
   822
  \textit{(7)} & @{thm (lhs) injval.simps(7)[of "r" "c" "v" "vs"]} & $\dn$ 
Chengsong
parents:
diff changeset
   823
      & @{thm (rhs) injval.simps(7)[of "r" "c" "v" "vs"]}\\
Chengsong
parents:
diff changeset
   824
  \end{tabular}
Chengsong
parents:
diff changeset
   825
  \end{center}
Chengsong
parents:
diff changeset
   826
Chengsong
parents:
diff changeset
   827
  \noindent To better understand what is going on in this definition it
Chengsong
parents:
diff changeset
   828
  might be instructive to look first at the three sequence cases (clauses
Chengsong
parents:
diff changeset
   829
  \textit{(4)} -- \textit{(6)}). In each case we need to construct an ``injected value'' for
Chengsong
parents:
diff changeset
   830
  @{term "SEQ r\<^sub>1 r\<^sub>2"}. This must be a value of the form @{term
Chengsong
parents:
diff changeset
   831
  "Seq DUMMY DUMMY"}\,. Recall the clause of the \<open>derivative\<close>-function
Chengsong
parents:
diff changeset
   832
  for sequence regular expressions:
Chengsong
parents:
diff changeset
   833
Chengsong
parents:
diff changeset
   834
  \begin{center}
Chengsong
parents:
diff changeset
   835
  @{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"]}
Chengsong
parents:
diff changeset
   836
  \end{center}
Chengsong
parents:
diff changeset
   837
Chengsong
parents:
diff changeset
   838
  \noindent Consider first the \<open>else\<close>-branch where the derivative is @{term
Chengsong
parents:
diff changeset
   839
  "SEQ (der c r\<^sub>1) r\<^sub>2"}. The corresponding value must therefore
Chengsong
parents:
diff changeset
   840
  be of the form @{term "Seq v\<^sub>1 v\<^sub>2"}, which matches the left-hand
Chengsong
parents:
diff changeset
   841
  side in clause~\textit{(4)} of @{term inj}. In the \<open>if\<close>-branch the derivative is an
Chengsong
parents:
diff changeset
   842
  alternative, namely @{term "ALT (SEQ (der c r\<^sub>1) r\<^sub>2) (der c
Chengsong
parents:
diff changeset
   843
  r\<^sub>2)"}. This means we either have to consider a \<open>Left\<close>- or
Chengsong
parents:
diff changeset
   844
  \<open>Right\<close>-value. In case of the \<open>Left\<close>-value we know further it
Chengsong
parents:
diff changeset
   845
  must be a value for a sequence regular expression. Therefore the pattern
Chengsong
parents:
diff changeset
   846
  we match in the clause \textit{(5)} is @{term "Left (Seq v\<^sub>1 v\<^sub>2)"},
Chengsong
parents:
diff changeset
   847
  while in \textit{(6)} it is just @{term "Right v\<^sub>2"}. One more interesting
Chengsong
parents:
diff changeset
   848
  point is in the right-hand side of clause \textit{(6)}: since in this case the
Chengsong
parents:
diff changeset
   849
  regular expression \<open>r\<^sub>1\<close> does not ``contribute'' to
Chengsong
parents:
diff changeset
   850
  matching the string, that means it only matches the empty string, we need to
Chengsong
parents:
diff changeset
   851
  call @{const mkeps} in order to construct a value for how @{term "r\<^sub>1"}
Chengsong
parents:
diff changeset
   852
  can match this empty string. A similar argument applies for why we can
Chengsong
parents:
diff changeset
   853
  expect in the left-hand side of clause \textit{(7)} that the value is of the form
Chengsong
parents:
diff changeset
   854
  @{term "Seq v (Stars vs)"}---the derivative of a star is @{term "SEQ (der c r)
Chengsong
parents:
diff changeset
   855
  (STAR r)"}. Finally, the reason for why we can ignore the second argument
Chengsong
parents:
diff changeset
   856
  in clause \textit{(1)} of @{term inj} is that it will only ever be called in cases
Chengsong
parents:
diff changeset
   857
  where @{term "c=d"}, but the usual linearity restrictions in patterns do
Chengsong
parents:
diff changeset
   858
  not allow us to build this constraint explicitly into our function
Chengsong
parents:
diff changeset
   859
  definition.\footnote{Sulzmann and Lu state this clause as @{thm (lhs)
Chengsong
parents:
diff changeset
   860
  injval.simps(1)[of "c" "c"]} $\dn$ @{thm (rhs) injval.simps(1)[of "c"]},
Chengsong
parents:
diff changeset
   861
  but our deviation is harmless.}
Chengsong
parents:
diff changeset
   862
Chengsong
parents:
diff changeset
   863
  The idea of the @{term inj}-function to ``inject'' a character, say
Chengsong
parents:
diff changeset
   864
  @{term c}, into a value can be made precise by the first part of the
Chengsong
parents:
diff changeset
   865
  following lemma, which shows that the underlying string of an injected
Chengsong
parents:
diff changeset
   866
  value has a prepended character @{term c}; the second part shows that
Chengsong
parents:
diff changeset
   867
  the underlying string of an @{const mkeps}-value is always the empty
Chengsong
parents:
diff changeset
   868
  string (given the regular expression is nullable since otherwise
Chengsong
parents:
diff changeset
   869
  \<open>mkeps\<close> might not be defined).
Chengsong
parents:
diff changeset
   870
Chengsong
parents:
diff changeset
   871
  \begin{lemma}\mbox{}\smallskip\\\label{Prf_injval_flat}
Chengsong
parents:
diff changeset
   872
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
   873
  (1) & @{thm[mode=IfThen] Prf_injval_flat}\\
Chengsong
parents:
diff changeset
   874
  (2) & @{thm[mode=IfThen] mkeps_flat}
Chengsong
parents:
diff changeset
   875
  \end{tabular}
Chengsong
parents:
diff changeset
   876
  \end{lemma}
Chengsong
parents:
diff changeset
   877
Chengsong
parents:
diff changeset
   878
  \begin{proof}
Chengsong
parents:
diff changeset
   879
  Both properties are by routine inductions: the first one can, for example,
Chengsong
parents:
diff changeset
   880
  be proved by induction over the definition of @{term derivatives}; the second by
Chengsong
parents:
diff changeset
   881
  an induction on @{term r}. There are no interesting cases.\qed
Chengsong
parents:
diff changeset
   882
  \end{proof}
Chengsong
parents:
diff changeset
   883
Chengsong
parents:
diff changeset
   884
  Having defined the @{const mkeps} and \<open>inj\<close> function we can extend
Chengsong
parents:
diff changeset
   885
  \Brz's matcher so that a value is constructed (assuming the
Chengsong
parents:
diff changeset
   886
  regular expression matches the string). The clauses of the Sulzmann and Lu lexer are
Chengsong
parents:
diff changeset
   887
Chengsong
parents:
diff changeset
   888
  \begin{center}
Chengsong
parents:
diff changeset
   889
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   890
  @{thm (lhs) lexer.simps(1)} & $\dn$ & @{thm (rhs) lexer.simps(1)}\\
Chengsong
parents:
diff changeset
   891
  @{thm (lhs) lexer.simps(2)} & $\dn$ & \<open>case\<close> @{term "lexer (der c r) s"} \<open>of\<close>\\
Chengsong
parents:
diff changeset
   892
                     & & \phantom{$|$} @{term "None"}  \<open>\<Rightarrow>\<close> @{term None}\\
Chengsong
parents:
diff changeset
   893
                     & & $|$ @{term "Some v"} \<open>\<Rightarrow>\<close> @{term "Some (injval r c v)"}                          
Chengsong
parents:
diff changeset
   894
  \end{tabular}
Chengsong
parents:
diff changeset
   895
  \end{center}
Chengsong
parents:
diff changeset
   896
Chengsong
parents:
diff changeset
   897
  \noindent If the regular expression does not match the string, @{const None} is
Chengsong
parents:
diff changeset
   898
  returned. If the regular expression \emph{does}
Chengsong
parents:
diff changeset
   899
  match the string, then @{const Some} value is returned. One important
Chengsong
parents:
diff changeset
   900
  virtue of this algorithm is that it can be implemented with ease in any
Chengsong
parents:
diff changeset
   901
  functional programming language and also in Isabelle/HOL. In the remaining
Chengsong
parents:
diff changeset
   902
  part of this section we prove that this algorithm is correct.
Chengsong
parents:
diff changeset
   903
Chengsong
parents:
diff changeset
   904
  The well-known idea of POSIX matching is informally defined by some
Chengsong
parents:
diff changeset
   905
  rules such as the Longest Match and Priority Rules (see
Chengsong
parents:
diff changeset
   906
  Introduction); as correctly argued in \cite{Sulzmann2014}, this
Chengsong
parents:
diff changeset
   907
  needs formal specification. Sulzmann and Lu define an ``ordering
Chengsong
parents:
diff changeset
   908
  relation'' between values and argue that there is a maximum value,
Chengsong
parents:
diff changeset
   909
  as given by the derivative-based algorithm.  In contrast, we shall
Chengsong
parents:
diff changeset
   910
  introduce a simple inductive definition that specifies directly what
Chengsong
parents:
diff changeset
   911
  a \emph{POSIX value} is, incorporating the POSIX-specific choices
Chengsong
parents:
diff changeset
   912
  into the side-conditions of our rules. Our definition is inspired by
Chengsong
parents:
diff changeset
   913
  the matching relation given by Vansummeren~\cite{Vansummeren2006}. 
Chengsong
parents:
diff changeset
   914
  The relation we define is ternary and
Chengsong
parents:
diff changeset
   915
  written as \mbox{@{term "s \<in> r \<rightarrow> v"}}, relating
Chengsong
parents:
diff changeset
   916
  strings, regular expressions and values; the inductive rules are given in 
Chengsong
parents:
diff changeset
   917
  Figure~\ref{POSIXrules}.
Chengsong
parents:
diff changeset
   918
  We can prove that given a string @{term s} and regular expression @{term
Chengsong
parents:
diff changeset
   919
   r}, the POSIX value @{term v} is uniquely determined by @{term "s \<in> r \<rightarrow> v"}.
Chengsong
parents:
diff changeset
   920
Chengsong
parents:
diff changeset
   921
  %
Chengsong
parents:
diff changeset
   922
  \begin{figure}[t]
Chengsong
parents:
diff changeset
   923
  \begin{center}
Chengsong
parents:
diff changeset
   924
  \begin{tabular}{c}
Chengsong
parents:
diff changeset
   925
  @{thm[mode=Axiom] Posix.intros(1)}\<open>P\<close>@{term "ONE"} \qquad
Chengsong
parents:
diff changeset
   926
  @{thm[mode=Axiom] Posix.intros(2)}\<open>P\<close>@{term "c"}\medskip\\
Chengsong
parents:
diff changeset
   927
  @{thm[mode=Rule] Posix.intros(3)[of "s" "r\<^sub>1" "v" "r\<^sub>2"]}\<open>P+L\<close>\qquad
Chengsong
parents:
diff changeset
   928
  @{thm[mode=Rule] Posix.intros(4)[of "s" "r\<^sub>2" "v" "r\<^sub>1"]}\<open>P+R\<close>\medskip\\
Chengsong
parents:
diff changeset
   929
  $\mprset{flushleft}
Chengsong
parents:
diff changeset
   930
   \inferrule
Chengsong
parents:
diff changeset
   931
   {@{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
Chengsong
parents:
diff changeset
   932
    @{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"]} \\\\
Chengsong
parents:
diff changeset
   933
    @{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"]}}
Chengsong
parents:
diff changeset
   934
   {@{thm (concl) Posix.intros(5)[of "s\<^sub>1" "r\<^sub>1" "v\<^sub>1" "s\<^sub>2" "r\<^sub>2" "v\<^sub>2"]}}$\<open>PS\<close>\\
Chengsong
parents:
diff changeset
   935
  @{thm[mode=Axiom] Posix.intros(7)}\<open>P[]\<close>\medskip\\
Chengsong
parents:
diff changeset
   936
  $\mprset{flushleft}
Chengsong
parents:
diff changeset
   937
   \inferrule
Chengsong
parents:
diff changeset
   938
   {@{thm (prem 1) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad
Chengsong
parents:
diff changeset
   939
    @{thm (prem 2) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad
Chengsong
parents:
diff changeset
   940
    @{thm (prem 3) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \\\\
Chengsong
parents:
diff changeset
   941
    @{thm (prem 4) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}
Chengsong
parents:
diff changeset
   942
   {@{thm (concl) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}$\<open>P\<star>\<close>
Chengsong
parents:
diff changeset
   943
  \end{tabular}
Chengsong
parents:
diff changeset
   944
  \end{center}
Chengsong
parents:
diff changeset
   945
  \caption{Our inductive definition of POSIX values.}\label{POSIXrules}
Chengsong
parents:
diff changeset
   946
  \end{figure}
Chengsong
parents:
diff changeset
   947
Chengsong
parents:
diff changeset
   948
   
Chengsong
parents:
diff changeset
   949
Chengsong
parents:
diff changeset
   950
  \begin{theorem}\mbox{}\smallskip\\\label{posixdeterm}
Chengsong
parents:
diff changeset
   951
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
   952
  (1) & If @{thm (prem 1) Posix1(1)} then @{thm (concl)
Chengsong
parents:
diff changeset
   953
  Posix1(1)} and @{thm (concl) Posix1(2)}.\\
Chengsong
parents:
diff changeset
   954
  (2) & @{thm[mode=IfThen] Posix_determ(1)[of _ _ "v" "v'"]}
Chengsong
parents:
diff changeset
   955
  \end{tabular}
Chengsong
parents:
diff changeset
   956
  \end{theorem}
Chengsong
parents:
diff changeset
   957
Chengsong
parents:
diff changeset
   958
  \begin{proof} Both by induction on the definition of @{term "s \<in> r \<rightarrow> v"}. 
Chengsong
parents:
diff changeset
   959
  The second parts follows by a case analysis of @{term "s \<in> r \<rightarrow> v'"} and
Chengsong
parents:
diff changeset
   960
  the first part.\qed
Chengsong
parents:
diff changeset
   961
  \end{proof}
Chengsong
parents:
diff changeset
   962
Chengsong
parents:
diff changeset
   963
  \noindent
Chengsong
parents:
diff changeset
   964
  We claim that our @{term "s \<in> r \<rightarrow> v"} relation captures the idea behind the four
Chengsong
parents:
diff changeset
   965
  informal POSIX rules shown in the Introduction: Consider for example the
Chengsong
parents:
diff changeset
   966
  rules \<open>P+L\<close> and \<open>P+R\<close> where the POSIX value for a string
Chengsong
parents:
diff changeset
   967
  and an alternative regular expression, that is @{term "(s, ALT r\<^sub>1 r\<^sub>2)"},
Chengsong
parents:
diff changeset
   968
  is specified---it is always a \<open>Left\<close>-value, \emph{except} when the
Chengsong
parents:
diff changeset
   969
  string to be matched is not in the language of @{term "r\<^sub>1"}; only then it
Chengsong
parents:
diff changeset
   970
  is a \<open>Right\<close>-value (see the side-condition in \<open>P+R\<close>).
Chengsong
parents:
diff changeset
   971
  Interesting is also the rule for sequence regular expressions (\<open>PS\<close>). The first two premises state that @{term "v\<^sub>1"} and @{term "v\<^sub>2"}
Chengsong
parents:
diff changeset
   972
  are the POSIX values for @{term "(s\<^sub>1, r\<^sub>1)"} and @{term "(s\<^sub>2, r\<^sub>2)"}
Chengsong
parents:
diff changeset
   973
  respectively. Consider now the third premise and note that the POSIX value
Chengsong
parents:
diff changeset
   974
  of this rule should match the string \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}}. According to the
Chengsong
parents:
diff changeset
   975
  Longest Match Rule, we want that the @{term "s\<^sub>1"} is the longest initial
Chengsong
parents:
diff changeset
   976
  split of \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}} such that @{term "s\<^sub>2"} is still recognised
Chengsong
parents:
diff changeset
   977
  by @{term "r\<^sub>2"}. Let us assume, contrary to the third premise, that there
Chengsong
parents:
diff changeset
   978
  \emph{exist} an @{term "s\<^sub>3"} and @{term "s\<^sub>4"} such that @{term "s\<^sub>2"}
Chengsong
parents:
diff changeset
   979
  can be split up into a non-empty string @{term "s\<^sub>3"} and a possibly empty
Chengsong
parents:
diff changeset
   980
  string @{term "s\<^sub>4"}. Moreover the longer string @{term "s\<^sub>1 @ s\<^sub>3"} can be
Chengsong
parents:
diff changeset
   981
  matched by \<open>r\<^sub>1\<close> and the shorter @{term "s\<^sub>4"} can still be
Chengsong
parents:
diff changeset
   982
  matched by @{term "r\<^sub>2"}. In this case @{term "s\<^sub>1"} would \emph{not} be the
Chengsong
parents:
diff changeset
   983
  longest initial split of \mbox{@{term "s\<^sub>1 @ s\<^sub>2"}} and therefore @{term "Seq v\<^sub>1
Chengsong
parents:
diff changeset
   984
  v\<^sub>2"} cannot be a POSIX value for @{term "(s\<^sub>1 @ s\<^sub>2, SEQ r\<^sub>1 r\<^sub>2)"}. 
Chengsong
parents:
diff changeset
   985
  The main point is that our side-condition ensures the Longest 
Chengsong
parents:
diff changeset
   986
  Match Rule is satisfied.
Chengsong
parents:
diff changeset
   987
Chengsong
parents:
diff changeset
   988
  A similar condition is imposed on the POSIX value in the \<open>P\<star>\<close>-rule. Also there we want that @{term "s\<^sub>1"} is the longest initial
Chengsong
parents:
diff changeset
   989
  split of @{term "s\<^sub>1 @ s\<^sub>2"} and furthermore the corresponding value
Chengsong
parents:
diff changeset
   990
  @{term v} cannot be flattened to the empty string. In effect, we require
Chengsong
parents:
diff changeset
   991
  that in each ``iteration'' of the star, some non-empty substring needs to
Chengsong
parents:
diff changeset
   992
  be ``chipped'' away; only in case of the empty string we accept @{term
Chengsong
parents:
diff changeset
   993
  "Stars []"} as the POSIX value. Indeed we can show that our POSIX values
Chengsong
parents:
diff changeset
   994
  are lexical values which exclude those \<open>Stars\<close> that contain subvalues 
Chengsong
parents:
diff changeset
   995
  that flatten to the empty string.
Chengsong
parents:
diff changeset
   996
Chengsong
parents:
diff changeset
   997
  \begin{lemma}\label{LVposix}
Chengsong
parents:
diff changeset
   998
  @{thm [mode=IfThen] Posix_LV}
Chengsong
parents:
diff changeset
   999
  \end{lemma}
Chengsong
parents:
diff changeset
  1000
Chengsong
parents:
diff changeset
  1001
  \begin{proof}
Chengsong
parents:
diff changeset
  1002
  By routine induction on @{thm (prem 1) Posix_LV}.\qed 
Chengsong
parents:
diff changeset
  1003
  \end{proof}
Chengsong
parents:
diff changeset
  1004
Chengsong
parents:
diff changeset
  1005
  \noindent
Chengsong
parents:
diff changeset
  1006
  Next is the lemma that shows the function @{term "mkeps"} calculates
Chengsong
parents:
diff changeset
  1007
  the POSIX value for the empty string and a nullable regular expression.
Chengsong
parents:
diff changeset
  1008
Chengsong
parents:
diff changeset
  1009
  \begin{lemma}\label{lemmkeps}
Chengsong
parents:
diff changeset
  1010
  @{thm[mode=IfThen] Posix_mkeps}
Chengsong
parents:
diff changeset
  1011
  \end{lemma}
Chengsong
parents:
diff changeset
  1012
Chengsong
parents:
diff changeset
  1013
  \begin{proof}
Chengsong
parents:
diff changeset
  1014
  By routine induction on @{term r}.\qed 
Chengsong
parents:
diff changeset
  1015
  \end{proof}
Chengsong
parents:
diff changeset
  1016
Chengsong
parents:
diff changeset
  1017
  \noindent
Chengsong
parents:
diff changeset
  1018
  The central lemma for our POSIX relation is that the \<open>inj\<close>-function
Chengsong
parents:
diff changeset
  1019
  preserves POSIX values.
Chengsong
parents:
diff changeset
  1020
Chengsong
parents:
diff changeset
  1021
  \begin{lemma}\label{Posix2}
Chengsong
parents:
diff changeset
  1022
  @{thm[mode=IfThen] Posix_injval}
Chengsong
parents:
diff changeset
  1023
  \end{lemma}
Chengsong
parents:
diff changeset
  1024
Chengsong
parents:
diff changeset
  1025
  \begin{proof}
Chengsong
parents:
diff changeset
  1026
  By induction on \<open>r\<close>. We explain two cases.
Chengsong
parents:
diff changeset
  1027
Chengsong
parents:
diff changeset
  1028
  \begin{itemize}
Chengsong
parents:
diff changeset
  1029
  \item[$\bullet$] Case @{term "r = ALT r\<^sub>1 r\<^sub>2"}. There are
Chengsong
parents:
diff changeset
  1030
  two subcases, namely \<open>(a)\<close> \mbox{@{term "v = Left v'"}} and @{term
Chengsong
parents:
diff changeset
  1031
  "s \<in> der c r\<^sub>1 \<rightarrow> v'"}; and \<open>(b)\<close> @{term "v = Right v'"}, @{term
Chengsong
parents:
diff changeset
  1032
  "s \<notin> L (der c r\<^sub>1)"} and @{term "s \<in> der c r\<^sub>2 \<rightarrow> v'"}. In \<open>(a)\<close> we
Chengsong
parents:
diff changeset
  1033
  know @{term "s \<in> der c r\<^sub>1 \<rightarrow> v'"}, from which we can infer @{term "(c # s)
Chengsong
parents:
diff changeset
  1034
  \<in> r\<^sub>1 \<rightarrow> injval r\<^sub>1 c v'"} by induction hypothesis and hence @{term "(c #
Chengsong
parents:
diff changeset
  1035
  s) \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> injval (ALT r\<^sub>1 r\<^sub>2) c (Left v')"} as needed. Similarly
Chengsong
parents:
diff changeset
  1036
  in subcase \<open>(b)\<close> where, however, in addition we have to use
Chengsong
parents:
diff changeset
  1037
  Proposition~\ref{derprop}(2) in order to infer @{term "c # s \<notin> L r\<^sub>1"} from @{term
Chengsong
parents:
diff changeset
  1038
  "s \<notin> L (der c r\<^sub>1)"}.\smallskip
Chengsong
parents:
diff changeset
  1039
Chengsong
parents:
diff changeset
  1040
  \item[$\bullet$] Case @{term "r = SEQ r\<^sub>1 r\<^sub>2"}. There are three subcases:
Chengsong
parents:
diff changeset
  1041
  
Chengsong
parents:
diff changeset
  1042
  \begin{quote}
Chengsong
parents:
diff changeset
  1043
  \begin{description}
Chengsong
parents:
diff changeset
  1044
  \item[\<open>(a)\<close>] @{term "v = Left (Seq v\<^sub>1 v\<^sub>2)"} and @{term "nullable r\<^sub>1"} 
Chengsong
parents:
diff changeset
  1045
  \item[\<open>(b)\<close>] @{term "v = Right v\<^sub>1"} and @{term "nullable r\<^sub>1"} 
Chengsong
parents:
diff changeset
  1046
  \item[\<open>(c)\<close>] @{term "v = Seq v\<^sub>1 v\<^sub>2"} and @{term "\<not> nullable r\<^sub>1"} 
Chengsong
parents:
diff changeset
  1047
  \end{description}
Chengsong
parents:
diff changeset
  1048
  \end{quote}
Chengsong
parents:
diff changeset
  1049
Chengsong
parents:
diff changeset
  1050
  \noindent For \<open>(a)\<close> we know @{term "s\<^sub>1 \<in> der c r\<^sub>1 \<rightarrow> v\<^sub>1"} and
Chengsong
parents:
diff changeset
  1051
  @{term "s\<^sub>2 \<in> r\<^sub>2 \<rightarrow> v\<^sub>2"} as well as
Chengsong
parents:
diff changeset
  1052
  %
Chengsong
parents:
diff changeset
  1053
  \[@{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)"}\]
Chengsong
parents:
diff changeset
  1054
Chengsong
parents:
diff changeset
  1055
  \noindent From the latter we can infer by Proposition~\ref{derprop}(2):
Chengsong
parents:
diff changeset
  1056
  %
Chengsong
parents:
diff changeset
  1057
  \[@{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)"}\]
Chengsong
parents:
diff changeset
  1058
Chengsong
parents:
diff changeset
  1059
  \noindent We can use the induction hypothesis for \<open>r\<^sub>1\<close> to obtain
Chengsong
parents:
diff changeset
  1060
  @{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
Chengsong
parents:
diff changeset
  1061
  @{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 \<open>(c)\<close>
Chengsong
parents:
diff changeset
  1062
  is similar.
Chengsong
parents:
diff changeset
  1063
Chengsong
parents:
diff changeset
  1064
  For \<open>(b)\<close> we know @{term "s \<in> der c r\<^sub>2 \<rightarrow> v\<^sub>1"} and 
Chengsong
parents:
diff changeset
  1065
  @{term "s\<^sub>1 @ s\<^sub>2 \<notin> L (SEQ (der c r\<^sub>1) r\<^sub>2)"}. From the former
Chengsong
parents:
diff changeset
  1066
  we have @{term "(c # s) \<in> r\<^sub>2 \<rightarrow> (injval r\<^sub>2 c v\<^sub>1)"} by induction hypothesis
Chengsong
parents:
diff changeset
  1067
  for @{term "r\<^sub>2"}. From the latter we can infer
Chengsong
parents:
diff changeset
  1068
  %
Chengsong
parents:
diff changeset
  1069
  \[@{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)"}\]
Chengsong
parents:
diff changeset
  1070
Chengsong
parents:
diff changeset
  1071
  \noindent By Lemma~\ref{lemmkeps} we know @{term "[] \<in> r\<^sub>1 \<rightarrow> (mkeps r\<^sub>1)"}
Chengsong
parents:
diff changeset
  1072
  holds. Putting this all together, we can conclude with @{term "(c #
Chengsong
parents:
diff changeset
  1073
  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.
Chengsong
parents:
diff changeset
  1074
Chengsong
parents:
diff changeset
  1075
  Finally suppose @{term "r = STAR r\<^sub>1"}. This case is very similar to the
Chengsong
parents:
diff changeset
  1076
  sequence case, except that we need to also ensure that @{term "flat (injval r\<^sub>1
Chengsong
parents:
diff changeset
  1077
  c v\<^sub>1) \<noteq> []"}. This follows from @{term "(c # s\<^sub>1)
Chengsong
parents:
diff changeset
  1078
  \<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
Chengsong
parents:
diff changeset
  1079
  r\<^sub>1 \<rightarrow> v\<^sub>1"} and the induction hypothesis).\qed
Chengsong
parents:
diff changeset
  1080
  \end{itemize}
Chengsong
parents:
diff changeset
  1081
  \end{proof}
Chengsong
parents:
diff changeset
  1082
Chengsong
parents:
diff changeset
  1083
  \noindent
Chengsong
parents:
diff changeset
  1084
  With Lemma~\ref{Posix2} in place, it is completely routine to establish
Chengsong
parents:
diff changeset
  1085
  that the Sulzmann and Lu lexer satisfies our specification (returning
Chengsong
parents:
diff changeset
  1086
  the null value @{term "None"} iff the string is not in the language of the regular expression,
Chengsong
parents:
diff changeset
  1087
  and returning a unique POSIX value iff the string \emph{is} in the language):
Chengsong
parents:
diff changeset
  1088
Chengsong
parents:
diff changeset
  1089
  \begin{theorem}\mbox{}\smallskip\\\label{lexercorrect}
Chengsong
parents:
diff changeset
  1090
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
  1091
  (1) & @{thm (lhs) lexer_correct_None} if and only if @{thm (rhs) lexer_correct_None}\\
Chengsong
parents:
diff changeset
  1092
  (2) & @{thm (lhs) lexer_correct_Some} if and only if @{thm (rhs) lexer_correct_Some}\\
Chengsong
parents:
diff changeset
  1093
  \end{tabular}
Chengsong
parents:
diff changeset
  1094
  \end{theorem}
Chengsong
parents:
diff changeset
  1095
Chengsong
parents:
diff changeset
  1096
  \begin{proof}
Chengsong
parents:
diff changeset
  1097
  By induction on @{term s} using Lemma~\ref{lemmkeps} and \ref{Posix2}.\qed  
Chengsong
parents:
diff changeset
  1098
  \end{proof}
Chengsong
parents:
diff changeset
  1099
Chengsong
parents:
diff changeset
  1100
  \noindent In \textit{(2)} we further know by Theorem~\ref{posixdeterm} that the
Chengsong
parents:
diff changeset
  1101
  value returned by the lexer must be unique.   A simple corollary 
Chengsong
parents:
diff changeset
  1102
  of our two theorems is:
Chengsong
parents:
diff changeset
  1103
Chengsong
parents:
diff changeset
  1104
  \begin{corollary}\mbox{}\smallskip\\\label{lexercorrectcor}
Chengsong
parents:
diff changeset
  1105
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
  1106
  (1) & @{thm (lhs) lexer_correctness(2)} if and only if @{thm (rhs) lexer_correctness(2)}\\ 
Chengsong
parents:
diff changeset
  1107
  (2) & @{thm (lhs) lexer_correctness(1)} if and only if @{thm (rhs) lexer_correctness(1)}\\
Chengsong
parents:
diff changeset
  1108
  \end{tabular}
Chengsong
parents:
diff changeset
  1109
  \end{corollary}
Chengsong
parents:
diff changeset
  1110
Chengsong
parents:
diff changeset
  1111
  \noindent This concludes our correctness proof. Note that we have
Chengsong
parents:
diff changeset
  1112
  not changed the algorithm of Sulzmann and Lu,\footnote{All
Chengsong
parents:
diff changeset
  1113
  deviations we introduced are harmless.} but introduced our own
Chengsong
parents:
diff changeset
  1114
  specification for what a correct result---a POSIX value---should be.
Chengsong
parents:
diff changeset
  1115
  In the next section we show that our specification coincides with
Chengsong
parents:
diff changeset
  1116
  another one given by Okui and Suzuki using a different technique.
Chengsong
parents:
diff changeset
  1117
Chengsong
parents:
diff changeset
  1118
\<close>
Chengsong
parents:
diff changeset
  1119
Chengsong
parents:
diff changeset
  1120
section \<open>Ordering of Values according to Okui and Suzuki\<close>
Chengsong
parents:
diff changeset
  1121
Chengsong
parents:
diff changeset
  1122
text \<open>
Chengsong
parents:
diff changeset
  1123
  
Chengsong
parents:
diff changeset
  1124
  While in the previous section we have defined POSIX values directly
Chengsong
parents:
diff changeset
  1125
  in terms of a ternary relation (see inference rules in Figure~\ref{POSIXrules}),
Chengsong
parents:
diff changeset
  1126
  Sulzmann and Lu took a different approach in \cite{Sulzmann2014}:
Chengsong
parents:
diff changeset
  1127
  they introduced an ordering for values and identified POSIX values
Chengsong
parents:
diff changeset
  1128
  as the maximal elements.  An extended version of \cite{Sulzmann2014}
Chengsong
parents:
diff changeset
  1129
  is available at the website of its first author; this includes more
Chengsong
parents:
diff changeset
  1130
  details of their proofs, but which are evidently not in final form
Chengsong
parents:
diff changeset
  1131
  yet. Unfortunately, we were not able to verify claims that their
Chengsong
parents:
diff changeset
  1132
  ordering has properties such as being transitive or having maximal
Chengsong
parents:
diff changeset
  1133
  elements. 
Chengsong
parents:
diff changeset
  1134
 
Chengsong
parents:
diff changeset
  1135
  Okui and Suzuki \cite{OkuiSuzuki2010,OkuiSuzukiTech} described
Chengsong
parents:
diff changeset
  1136
  another ordering of values, which they use to establish the
Chengsong
parents:
diff changeset
  1137
  correctness of their automata-based algorithm for POSIX matching.
Chengsong
parents:
diff changeset
  1138
  Their ordering resembles some aspects of the one given by Sulzmann
Chengsong
parents:
diff changeset
  1139
  and Lu, but overall is quite different. To begin with, Okui and
Chengsong
parents:
diff changeset
  1140
  Suzuki identify POSIX values as minimal, rather than maximal,
Chengsong
parents:
diff changeset
  1141
  elements in their ordering. A more substantial difference is that
Chengsong
parents:
diff changeset
  1142
  the ordering by Okui and Suzuki uses \emph{positions} in order to
Chengsong
parents:
diff changeset
  1143
  identify and compare subvalues. Positions are lists of natural
Chengsong
parents:
diff changeset
  1144
  numbers. This allows them to quite naturally formalise the Longest
Chengsong
parents:
diff changeset
  1145
  Match and Priority rules of the informal POSIX standard.  Consider
Chengsong
parents:
diff changeset
  1146
  for example the value @{term v}
Chengsong
parents:
diff changeset
  1147
Chengsong
parents:
diff changeset
  1148
  \begin{center}
Chengsong
parents:
diff changeset
  1149
  @{term "v == Stars [Seq (Char x) (Char y), Char z]"}
Chengsong
parents:
diff changeset
  1150
  \end{center}
Chengsong
parents:
diff changeset
  1151
Chengsong
parents:
diff changeset
  1152
  \noindent
Chengsong
parents:
diff changeset
  1153
  At position \<open>[0,1]\<close> of this value is the
Chengsong
parents:
diff changeset
  1154
  subvalue \<open>Char y\<close> and at position \<open>[1]\<close> the
Chengsong
parents:
diff changeset
  1155
  subvalue @{term "Char z"}.  At the `root' position, or empty list
Chengsong
parents:
diff changeset
  1156
  @{term "[]"}, is the whole value @{term v}. Positions such as \<open>[0,1,0]\<close> or \<open>[2]\<close> are outside of \<open>v\<close>. If it exists, the subvalue of @{term v} at a position \<open>p\<close>, written @{term "at v p"}, can be recursively defined by
Chengsong
parents:
diff changeset
  1157
  
Chengsong
parents:
diff changeset
  1158
  \begin{center}
Chengsong
parents:
diff changeset
  1159
  \begin{tabular}{r@ {\hspace{0mm}}lcl}
Chengsong
parents:
diff changeset
  1160
  @{term v} &  \<open>\<downharpoonleft>\<^bsub>[]\<^esub>\<close> & \<open>\<equiv>\<close>& @{thm (rhs) at.simps(1)}\\
Chengsong
parents:
diff changeset
  1161
  @{term "Left v"} & \<open>\<downharpoonleft>\<^bsub>0::ps\<^esub>\<close> & \<open>\<equiv>\<close>& @{thm (rhs) at.simps(2)}\\
Chengsong
parents:
diff changeset
  1162
  @{term "Right v"} & \<open>\<downharpoonleft>\<^bsub>1::ps\<^esub>\<close> & \<open>\<equiv>\<close> & 
Chengsong
parents:
diff changeset
  1163
  @{thm (rhs) at.simps(3)[simplified Suc_0_fold]}\\
Chengsong
parents:
diff changeset
  1164
  @{term "Seq v\<^sub>1 v\<^sub>2"} & \<open>\<downharpoonleft>\<^bsub>0::ps\<^esub>\<close> & \<open>\<equiv>\<close> & 
Chengsong
parents:
diff changeset
  1165
  @{thm (rhs) at.simps(4)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \\
Chengsong
parents:
diff changeset
  1166
  @{term "Seq v\<^sub>1 v\<^sub>2"} & \<open>\<downharpoonleft>\<^bsub>1::ps\<^esub>\<close>
Chengsong
parents:
diff changeset
  1167
  & \<open>\<equiv>\<close> & 
Chengsong
parents:
diff changeset
  1168
  @{thm (rhs) at.simps(5)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2", simplified Suc_0_fold]} \\
Chengsong
parents:
diff changeset
  1169
  @{term "Stars vs"} & \<open>\<downharpoonleft>\<^bsub>n::ps\<^esub>\<close> & \<open>\<equiv>\<close>& @{thm (rhs) at.simps(6)}\\
Chengsong
parents:
diff changeset
  1170
  \end{tabular} 
Chengsong
parents:
diff changeset
  1171
  \end{center}
Chengsong
parents:
diff changeset
  1172
Chengsong
parents:
diff changeset
  1173
  \noindent In the last clause we use Isabelle's notation @{term "vs ! n"} for the
Chengsong
parents:
diff changeset
  1174
  \<open>n\<close>th element in a list.  The set of positions inside a value \<open>v\<close>,
Chengsong
parents:
diff changeset
  1175
  written @{term "Pos v"}, is given by 
Chengsong
parents:
diff changeset
  1176
Chengsong
parents:
diff changeset
  1177
  \begin{center}
Chengsong
parents:
diff changeset
  1178
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1179
  @{thm (lhs) Pos.simps(1)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(1)}\\
Chengsong
parents:
diff changeset
  1180
  @{thm (lhs) Pos.simps(2)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(2)}\\
Chengsong
parents:
diff changeset
  1181
  @{thm (lhs) Pos.simps(3)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(3)}\\
Chengsong
parents:
diff changeset
  1182
  @{thm (lhs) Pos.simps(4)} & \<open>\<equiv>\<close> & @{thm (rhs) Pos.simps(4)}\\
Chengsong
parents:
diff changeset
  1183
  @{thm (lhs) Pos.simps(5)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}
Chengsong
parents:
diff changeset
  1184
  & \<open>\<equiv>\<close> 
Chengsong
parents:
diff changeset
  1185
  & @{thm (rhs) Pos.simps(5)[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1186
  @{thm (lhs) Pos_stars} & \<open>\<equiv>\<close> & @{thm (rhs) Pos_stars}\\
Chengsong
parents:
diff changeset
  1187
  \end{tabular}
Chengsong
parents:
diff changeset
  1188
  \end{center}
Chengsong
parents:
diff changeset
  1189
Chengsong
parents:
diff changeset
  1190
  \noindent 
Chengsong
parents:
diff changeset
  1191
  whereby \<open>len\<close> in the last clause stands for the length of a list. Clearly
Chengsong
parents:
diff changeset
  1192
  for every position inside a value there exists a subvalue at that position.
Chengsong
parents:
diff changeset
  1193
 
Chengsong
parents:
diff changeset
  1194
Chengsong
parents:
diff changeset
  1195
  To help understanding the ordering of Okui and Suzuki, consider again 
Chengsong
parents:
diff changeset
  1196
  the earlier value
Chengsong
parents:
diff changeset
  1197
  \<open>v\<close> and compare it with the following \<open>w\<close>:
Chengsong
parents:
diff changeset
  1198
Chengsong
parents:
diff changeset
  1199
  \begin{center}
Chengsong
parents:
diff changeset
  1200
  \begin{tabular}{l}
Chengsong
parents:
diff changeset
  1201
  @{term "v == Stars [Seq (Char x) (Char y), Char z]"}\\
Chengsong
parents:
diff changeset
  1202
  @{term "w == Stars [Char x, Char y, Char z]"}  
Chengsong
parents:
diff changeset
  1203
  \end{tabular}
Chengsong
parents:
diff changeset
  1204
  \end{center}
Chengsong
parents:
diff changeset
  1205
Chengsong
parents:
diff changeset
  1206
  \noindent Both values match the string \<open>xyz\<close>, that means if
Chengsong
parents:
diff changeset
  1207
  we flatten these values at their respective root position, we obtain
Chengsong
parents:
diff changeset
  1208
  \<open>xyz\<close>. However, at position \<open>[0]\<close>, \<open>v\<close> matches
Chengsong
parents:
diff changeset
  1209
  \<open>xy\<close> whereas \<open>w\<close> matches only the shorter \<open>x\<close>. So
Chengsong
parents:
diff changeset
  1210
  according to the Longest Match Rule, we should prefer \<open>v\<close>,
Chengsong
parents:
diff changeset
  1211
  rather than \<open>w\<close> as POSIX value for string \<open>xyz\<close> (and
Chengsong
parents:
diff changeset
  1212
  corresponding regular expression). In order to
Chengsong
parents:
diff changeset
  1213
  formalise this idea, Okui and Suzuki introduce a measure for
Chengsong
parents:
diff changeset
  1214
  subvalues at position \<open>p\<close>, called the \emph{norm} of \<open>v\<close>
Chengsong
parents:
diff changeset
  1215
  at position \<open>p\<close>. We can define this measure in Isabelle as an
Chengsong
parents:
diff changeset
  1216
  integer as follows
Chengsong
parents:
diff changeset
  1217
  
Chengsong
parents:
diff changeset
  1218
  \begin{center}
Chengsong
parents:
diff changeset
  1219
  @{thm pflat_len_def}
Chengsong
parents:
diff changeset
  1220
  \end{center}
Chengsong
parents:
diff changeset
  1221
Chengsong
parents:
diff changeset
  1222
  \noindent where we take the length of the flattened value at
Chengsong
parents:
diff changeset
  1223
  position \<open>p\<close>, provided the position is inside \<open>v\<close>; if
Chengsong
parents:
diff changeset
  1224
  not, then the norm is \<open>-1\<close>. The default for outside
Chengsong
parents:
diff changeset
  1225
  positions is crucial for the POSIX requirement of preferring a
Chengsong
parents:
diff changeset
  1226
  \<open>Left\<close>-value over a \<open>Right\<close>-value (if they can match the
Chengsong
parents:
diff changeset
  1227
  same string---see the Priority Rule from the Introduction). For this
Chengsong
parents:
diff changeset
  1228
  consider
Chengsong
parents:
diff changeset
  1229
Chengsong
parents:
diff changeset
  1230
  \begin{center}
Chengsong
parents:
diff changeset
  1231
  @{term "v == Left (Char x)"} \qquad and \qquad @{term "w == Right (Char x)"}
Chengsong
parents:
diff changeset
  1232
  \end{center}
Chengsong
parents:
diff changeset
  1233
Chengsong
parents:
diff changeset
  1234
  \noindent Both values match \<open>x\<close>. At position \<open>[0]\<close>
Chengsong
parents:
diff changeset
  1235
  the norm of @{term v} is \<open>1\<close> (the subvalue matches \<open>x\<close>),
Chengsong
parents:
diff changeset
  1236
  but the norm of \<open>w\<close> is \<open>-1\<close> (the position is outside
Chengsong
parents:
diff changeset
  1237
  \<open>w\<close> according to how we defined the `inside' positions of
Chengsong
parents:
diff changeset
  1238
  \<open>Left\<close>- and \<open>Right\<close>-values).  Of course at position
Chengsong
parents:
diff changeset
  1239
  \<open>[1]\<close>, the norms @{term "pflat_len v [1]"} and @{term
Chengsong
parents:
diff changeset
  1240
  "pflat_len w [1]"} are reversed, but the point is that subvalues
Chengsong
parents:
diff changeset
  1241
  will be analysed according to lexicographically ordered
Chengsong
parents:
diff changeset
  1242
  positions. According to this ordering, the position \<open>[0]\<close>
Chengsong
parents:
diff changeset
  1243
  takes precedence over \<open>[1]\<close> and thus also \<open>v\<close> will be 
Chengsong
parents:
diff changeset
  1244
  preferred over \<open>w\<close>.  The lexicographic ordering of positions, written
Chengsong
parents:
diff changeset
  1245
  @{term "DUMMY \<sqsubset>lex DUMMY"}, can be conveniently formalised
Chengsong
parents:
diff changeset
  1246
  by three inference rules
Chengsong
parents:
diff changeset
  1247
Chengsong
parents:
diff changeset
  1248
  \begin{center}
Chengsong
parents:
diff changeset
  1249
  \begin{tabular}{ccc}
Chengsong
parents:
diff changeset
  1250
  @{thm [mode=Axiom] lex_list.intros(1)}\hspace{1cm} &
Chengsong
parents:
diff changeset
  1251
  @{thm [mode=Rule] lex_list.intros(3)[where ?p1.0="p\<^sub>1" and ?p2.0="p\<^sub>2" and
Chengsong
parents:
diff changeset
  1252
                                            ?ps1.0="ps\<^sub>1" and ?ps2.0="ps\<^sub>2"]}\hspace{1cm} &
Chengsong
parents:
diff changeset
  1253
  @{thm [mode=Rule] lex_list.intros(2)[where ?ps1.0="ps\<^sub>1" and ?ps2.0="ps\<^sub>2"]}
Chengsong
parents:
diff changeset
  1254
  \end{tabular}
Chengsong
parents:
diff changeset
  1255
  \end{center}
Chengsong
parents:
diff changeset
  1256
Chengsong
parents:
diff changeset
  1257
  With the norm and lexicographic order in place,
Chengsong
parents:
diff changeset
  1258
  we can state the key definition of Okui and Suzuki
Chengsong
parents:
diff changeset
  1259
  \cite{OkuiSuzuki2010}: a value @{term "v\<^sub>1"} is \emph{smaller at position \<open>p\<close>} than
Chengsong
parents:
diff changeset
  1260
  @{term "v\<^sub>2"}, written @{term "v\<^sub>1 \<sqsubset>val p v\<^sub>2"}, 
Chengsong
parents:
diff changeset
  1261
  if and only if  $(i)$ the norm at position \<open>p\<close> is
Chengsong
parents:
diff changeset
  1262
  greater in @{term "v\<^sub>1"} (that is the string @{term "flat (at v\<^sub>1 p)"} is longer 
Chengsong
parents:
diff changeset
  1263
  than @{term "flat (at v\<^sub>2 p)"}) and $(ii)$ all subvalues at 
Chengsong
parents:
diff changeset
  1264
  positions that are inside @{term "v\<^sub>1"} or @{term "v\<^sub>2"} and that are
Chengsong
parents:
diff changeset
  1265
  lexicographically smaller than \<open>p\<close>, we have the same norm, namely
Chengsong
parents:
diff changeset
  1266
Chengsong
parents:
diff changeset
  1267
 \begin{center}
Chengsong
parents:
diff changeset
  1268
 \begin{tabular}{c}
Chengsong
parents:
diff changeset
  1269
 @{thm (lhs) PosOrd_def[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} 
Chengsong
parents:
diff changeset
  1270
 \<open>\<equiv>\<close> 
Chengsong
parents:
diff changeset
  1271
 $\begin{cases}
Chengsong
parents:
diff changeset
  1272
 (i) & @{term "pflat_len v\<^sub>1 p > pflat_len v\<^sub>2 p"}   \quad\text{and}\smallskip \\
Chengsong
parents:
diff changeset
  1273
 (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)"}
Chengsong
parents:
diff changeset
  1274
 \end{cases}$
Chengsong
parents:
diff changeset
  1275
 \end{tabular}
Chengsong
parents:
diff changeset
  1276
 \end{center}
Chengsong
parents:
diff changeset
  1277
Chengsong
parents:
diff changeset
  1278
 \noindent The position \<open>p\<close> in this definition acts as the
Chengsong
parents:
diff changeset
  1279
  \emph{first distinct position} of \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close>, where both values match strings of different length
Chengsong
parents:
diff changeset
  1280
  \cite{OkuiSuzuki2010}.  Since at \<open>p\<close> the values \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close> match different strings, the
Chengsong
parents:
diff changeset
  1281
  ordering is irreflexive. Derived from the definition above
Chengsong
parents:
diff changeset
  1282
  are the following two orderings:
Chengsong
parents:
diff changeset
  1283
  
Chengsong
parents:
diff changeset
  1284
  \begin{center}
Chengsong
parents:
diff changeset
  1285
  \begin{tabular}{l}
Chengsong
parents:
diff changeset
  1286
  @{thm PosOrd_ex_def[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1287
  @{thm PosOrd_ex_eq_def[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}
Chengsong
parents:
diff changeset
  1288
  \end{tabular}
Chengsong
parents:
diff changeset
  1289
  \end{center}
Chengsong
parents:
diff changeset
  1290
Chengsong
parents:
diff changeset
  1291
 While we encountered a number of obstacles for establishing properties like
Chengsong
parents:
diff changeset
  1292
 transitivity for the ordering of Sulzmann and Lu (and which we failed
Chengsong
parents:
diff changeset
  1293
 to overcome), it is relatively straightforward to establish this
Chengsong
parents:
diff changeset
  1294
 property for the orderings
Chengsong
parents:
diff changeset
  1295
 @{term "DUMMY :\<sqsubset>val DUMMY"} and @{term "DUMMY :\<sqsubseteq>val DUMMY"}  
Chengsong
parents:
diff changeset
  1296
 by Okui and Suzuki.
Chengsong
parents:
diff changeset
  1297
Chengsong
parents:
diff changeset
  1298
 \begin{lemma}[Transitivity]\label{transitivity}
Chengsong
parents:
diff changeset
  1299
 @{thm [mode=IfThen] PosOrd_trans[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and ?v3.0="v\<^sub>3"]} 
Chengsong
parents:
diff changeset
  1300
 \end{lemma}
Chengsong
parents:
diff changeset
  1301
Chengsong
parents:
diff changeset
  1302
 \begin{proof} From the assumption we obtain two positions \<open>p\<close>
Chengsong
parents:
diff changeset
  1303
 and \<open>q\<close>, where the values \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close> (respectively \<open>v\<^sub>2\<close> and \<open>v\<^sub>3\<close>) are `distinct'.  Since \<open>\<prec>\<^bsub>lex\<^esub>\<close> is trichotomous, we need to consider
Chengsong
parents:
diff changeset
  1304
 three cases, namely @{term "p = q"}, @{term "p \<sqsubset>lex q"} and
Chengsong
parents:
diff changeset
  1305
 @{term "q \<sqsubset>lex p"}. Let us look at the first case.  Clearly
Chengsong
parents:
diff changeset
  1306
 @{term "pflat_len v\<^sub>2 p < pflat_len v\<^sub>1 p"} and @{term
Chengsong
parents:
diff changeset
  1307
 "pflat_len v\<^sub>3 p < pflat_len v\<^sub>2 p"} imply @{term
Chengsong
parents:
diff changeset
  1308
 "pflat_len v\<^sub>3 p < pflat_len v\<^sub>1 p"}.  It remains to show
Chengsong
parents:
diff changeset
  1309
 that for a @{term "p' \<in> Pos v\<^sub>1 \<union> Pos v\<^sub>3"}
Chengsong
parents:
diff changeset
  1310
 with @{term "p' \<sqsubset>lex p"} that @{term "pflat_len v\<^sub>1
Chengsong
parents:
diff changeset
  1311
 p' = pflat_len v\<^sub>3 p'"} holds.  Suppose @{term "p' \<in> Pos
Chengsong
parents:
diff changeset
  1312
 v\<^sub>1"}, then we can infer from the first assumption that @{term
Chengsong
parents:
diff changeset
  1313
 "pflat_len v\<^sub>1 p' = pflat_len v\<^sub>2 p'"}.  But this means
Chengsong
parents:
diff changeset
  1314
 that @{term "p'"} must be in @{term "Pos v\<^sub>2"} too (the norm
Chengsong
parents:
diff changeset
  1315
 cannot be \<open>-1\<close> given @{term "p' \<in> Pos v\<^sub>1"}).  
Chengsong
parents:
diff changeset
  1316
 Hence we can use the second assumption and
Chengsong
parents:
diff changeset
  1317
 infer @{term "pflat_len v\<^sub>2 p' = pflat_len v\<^sub>3 p'"},
Chengsong
parents:
diff changeset
  1318
 which concludes this case with @{term "v\<^sub>1 :\<sqsubset>val
Chengsong
parents:
diff changeset
  1319
 v\<^sub>3"}.  The reasoning in the other cases is similar.\qed
Chengsong
parents:
diff changeset
  1320
 \end{proof}
Chengsong
parents:
diff changeset
  1321
Chengsong
parents:
diff changeset
  1322
 \noindent 
Chengsong
parents:
diff changeset
  1323
 The proof for $\preccurlyeq$ is similar and omitted.
Chengsong
parents:
diff changeset
  1324
 It is also straightforward to show that \<open>\<prec>\<close> and
Chengsong
parents:
diff changeset
  1325
 $\preccurlyeq$ are partial orders.  Okui and Suzuki furthermore show that they
Chengsong
parents:
diff changeset
  1326
 are linear orderings for lexical values \cite{OkuiSuzuki2010} of a given
Chengsong
parents:
diff changeset
  1327
 regular expression and given string, but we have not formalised this in Isabelle. It is
Chengsong
parents:
diff changeset
  1328
 not essential for our results. What we are going to show below is
Chengsong
parents:
diff changeset
  1329
 that for a given \<open>r\<close> and \<open>s\<close>, the orderings have a unique
Chengsong
parents:
diff changeset
  1330
 minimal element on the set @{term "LV r s"}, which is the POSIX value
Chengsong
parents:
diff changeset
  1331
 we defined in the previous section. We start with two properties that
Chengsong
parents:
diff changeset
  1332
 show how the length of a flattened value relates to the \<open>\<prec>\<close>-ordering.
Chengsong
parents:
diff changeset
  1333
Chengsong
parents:
diff changeset
  1334
 \begin{proposition}\mbox{}\smallskip\\\label{ordlen}
Chengsong
parents:
diff changeset
  1335
 \begin{tabular}{@ {}ll}
Chengsong
parents:
diff changeset
  1336
 (1) &
Chengsong
parents:
diff changeset
  1337
 @{thm [mode=IfThen] PosOrd_shorterE[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1338
 (2) &
Chengsong
parents:
diff changeset
  1339
 @{thm [mode=IfThen] PosOrd_shorterI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} 
Chengsong
parents:
diff changeset
  1340
 \end{tabular} 
Chengsong
parents:
diff changeset
  1341
 \end{proposition}
Chengsong
parents:
diff changeset
  1342
 
Chengsong
parents:
diff changeset
  1343
 \noindent Both properties follow from the definition of the ordering. Note that
Chengsong
parents:
diff changeset
  1344
 \textit{(2)} entails that a value, say @{term "v\<^sub>2"}, whose underlying 
Chengsong
parents:
diff changeset
  1345
 string is a strict prefix of another flattened value, say @{term "v\<^sub>1"}, then
Chengsong
parents:
diff changeset
  1346
 @{term "v\<^sub>1"} must be smaller than @{term "v\<^sub>2"}. For our proofs it
Chengsong
parents:
diff changeset
  1347
 will be useful to have the following properties---in each case the underlying strings 
Chengsong
parents:
diff changeset
  1348
 of the compared values are the same: 
Chengsong
parents:
diff changeset
  1349
Chengsong
parents:
diff changeset
  1350
  \begin{proposition}\mbox{}\smallskip\\\label{ordintros}
Chengsong
parents:
diff changeset
  1351
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
  1352
  \textit{(1)} & 
Chengsong
parents:
diff changeset
  1353
  @{thm [mode=IfThen] PosOrd_Left_Right[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1354
  \textit{(2)} & If
Chengsong
parents:
diff changeset
  1355
  @{thm (prem 1) PosOrd_Left_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;then\;
Chengsong
parents:
diff changeset
  1356
  @{thm (lhs) PosOrd_Left_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;iff\;
Chengsong
parents:
diff changeset
  1357
  @{thm (rhs) PosOrd_Left_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1358
  \textit{(3)} & If
Chengsong
parents:
diff changeset
  1359
  @{thm (prem 1) PosOrd_Right_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;then\;
Chengsong
parents:
diff changeset
  1360
  @{thm (lhs) PosOrd_Right_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]} \;iff\;
Chengsong
parents:
diff changeset
  1361
  @{thm (rhs) PosOrd_Right_eq[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1362
  \textit{(4)} & If
Chengsong
parents:
diff changeset
  1363
  @{thm (prem 1) PosOrd_Seq_eq[where ?v2.0="v\<^sub>2" and ?w2.0="w\<^sub>2"]} \;then\;
Chengsong
parents:
diff changeset
  1364
  @{thm (lhs) PosOrd_Seq_eq[where ?v2.0="v\<^sub>2" and ?w2.0="w\<^sub>2"]} \;iff\;
Chengsong
parents:
diff changeset
  1365
  @{thm (rhs) PosOrd_Seq_eq[where ?v2.0="v\<^sub>2" and ?w2.0="w\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1366
  \textit{(5)} & If
Chengsong
parents:
diff changeset
  1367
  @{thm (prem 2) PosOrd_SeqI1[simplified, where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and
Chengsong
parents:
diff changeset
  1368
                                    ?w1.0="w\<^sub>1" and ?w2.0="w\<^sub>2"]} \;and\;
Chengsong
parents:
diff changeset
  1369
  @{thm (prem 1) PosOrd_SeqI1[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and
Chengsong
parents:
diff changeset
  1370
                                    ?w1.0="w\<^sub>1" and ?w2.0="w\<^sub>2"]} \;then\;
Chengsong
parents:
diff changeset
  1371
  @{thm (concl) PosOrd_SeqI1[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and
Chengsong
parents:
diff changeset
  1372
                                   ?w1.0="w\<^sub>1" and ?w2.0="w\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1373
  \textit{(6)} & If
Chengsong
parents:
diff changeset
  1374
  @{thm (prem 1) PosOrd_Stars_append_eq[where ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;then\;
Chengsong
parents:
diff changeset
  1375
  @{thm (lhs) PosOrd_Stars_append_eq[where ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;iff\;
Chengsong
parents:
diff changeset
  1376
  @{thm (rhs) PosOrd_Stars_append_eq[where ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]}\\  
Chengsong
parents:
diff changeset
  1377
  
Chengsong
parents:
diff changeset
  1378
  \textit{(7)} & If
Chengsong
parents:
diff changeset
  1379
  @{thm (prem 2) PosOrd_StarsI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and
Chengsong
parents:
diff changeset
  1380
                            ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;and\;
Chengsong
parents:
diff changeset
  1381
  @{thm (prem 1) PosOrd_StarsI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and
Chengsong
parents:
diff changeset
  1382
                            ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]} \;then\;
Chengsong
parents:
diff changeset
  1383
   @{thm (concl) PosOrd_StarsI[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2" and
Chengsong
parents:
diff changeset
  1384
                            ?vs1.0="vs\<^sub>1" and ?vs2.0="vs\<^sub>2"]}\\
Chengsong
parents:
diff changeset
  1385
  \end{tabular} 
Chengsong
parents:
diff changeset
  1386
  \end{proposition}
Chengsong
parents:
diff changeset
  1387
Chengsong
parents:
diff changeset
  1388
  \noindent One might prefer that statements \textit{(4)} and \textit{(5)} 
Chengsong
parents:
diff changeset
  1389
  (respectively \textit{(6)} and \textit{(7)})
Chengsong
parents:
diff changeset
  1390
  are combined into a single \textit{iff}-statement (like the ones for \<open>Left\<close> and \<open>Right\<close>). Unfortunately this cannot be done easily: such
Chengsong
parents:
diff changeset
  1391
  a single statement would require an additional assumption about the
Chengsong
parents:
diff changeset
  1392
  two values @{term "Seq v\<^sub>1 v\<^sub>2"} and @{term "Seq w\<^sub>1 w\<^sub>2"}
Chengsong
parents:
diff changeset
  1393
  being inhabited by the same regular expression. The
Chengsong
parents:
diff changeset
  1394
  complexity of the proofs involved seems to not justify such a
Chengsong
parents:
diff changeset
  1395
  `cleaner' single statement. The statements given are just the properties that
Chengsong
parents:
diff changeset
  1396
  allow us to establish our theorems without any difficulty. The proofs 
Chengsong
parents:
diff changeset
  1397
  for Proposition~\ref{ordintros} are routine.
Chengsong
parents:
diff changeset
  1398
 
Chengsong
parents:
diff changeset
  1399
Chengsong
parents:
diff changeset
  1400
  Next we establish how Okui and Suzuki's orderings relate to our
Chengsong
parents:
diff changeset
  1401
  definition of POSIX values.  Given a \<open>POSIX\<close> value \<open>v\<^sub>1\<close>
Chengsong
parents:
diff changeset
  1402
  for \<open>r\<close> and \<open>s\<close>, then any other lexical value \<open>v\<^sub>2\<close> in @{term "LV r s"} is greater or equal than \<open>v\<^sub>1\<close>, namely:
Chengsong
parents:
diff changeset
  1403
Chengsong
parents:
diff changeset
  1404
Chengsong
parents:
diff changeset
  1405
  \begin{theorem}\label{orderone}
Chengsong
parents:
diff changeset
  1406
  @{thm [mode=IfThen] Posix_PosOrd[where ?v1.0="v\<^sub>1" and ?v2.0="v\<^sub>2"]}
Chengsong
parents:
diff changeset
  1407
  \end{theorem}
Chengsong
parents:
diff changeset
  1408
Chengsong
parents:
diff changeset
  1409
  \begin{proof} By induction on our POSIX rules. By
Chengsong
parents:
diff changeset
  1410
  Theorem~\ref{posixdeterm} and the definition of @{const LV}, it is clear
Chengsong
parents:
diff changeset
  1411
  that \<open>v\<^sub>1\<close> and \<open>v\<^sub>2\<close> have the same
Chengsong
parents:
diff changeset
  1412
  underlying string @{term s}.  The three base cases are
Chengsong
parents:
diff changeset
  1413
  straightforward: for example for @{term "v\<^sub>1 = Void"}, we have
Chengsong
parents:
diff changeset
  1414
  that @{term "v\<^sub>2 \<in> LV ONE []"} must also be of the form
Chengsong
parents:
diff changeset
  1415
  \mbox{@{term "v\<^sub>2 = Void"}}. Therefore we have @{term
Chengsong
parents:
diff changeset
  1416
  "v\<^sub>1 :\<sqsubseteq>val v\<^sub>2"}.  The inductive cases for
Chengsong
parents:
diff changeset
  1417
  \<open>r\<close> being of the form @{term "ALT r\<^sub>1 r\<^sub>2"} and
Chengsong
parents:
diff changeset
  1418
  @{term "SEQ r\<^sub>1 r\<^sub>2"} are as follows:
Chengsong
parents:
diff changeset
  1419
Chengsong
parents:
diff changeset
  1420
Chengsong
parents:
diff changeset
  1421
  \begin{itemize} 
Chengsong
parents:
diff changeset
  1422
Chengsong
parents:
diff changeset
  1423
  \item[$\bullet$] Case \<open>P+L\<close> with @{term "s \<in> (ALT r\<^sub>1 r\<^sub>2)
Chengsong
parents:
diff changeset
  1424
  \<rightarrow> (Left w\<^sub>1)"}: In this case the value 
Chengsong
parents:
diff changeset
  1425
  @{term "v\<^sub>2"} is either of the
Chengsong
parents:
diff changeset
  1426
  form @{term "Left w\<^sub>2"} or @{term "Right w\<^sub>2"}. In the
Chengsong
parents:
diff changeset
  1427
  latter case we can immediately conclude with \mbox{@{term "v\<^sub>1
Chengsong
parents:
diff changeset
  1428
  :\<sqsubseteq>val v\<^sub>2"}} since a \<open>Left\<close>-value with the
Chengsong
parents:
diff changeset
  1429
  same underlying string \<open>s\<close> is always smaller than a
Chengsong
parents:
diff changeset
  1430
  \<open>Right\<close>-value by Proposition~\ref{ordintros}\textit{(1)}.  
Chengsong
parents:
diff changeset
  1431
  In the former case we have @{term "w\<^sub>2
Chengsong
parents:
diff changeset
  1432
  \<in> LV r\<^sub>1 s"} and can use the induction hypothesis to infer
Chengsong
parents:
diff changeset
  1433
  @{term "w\<^sub>1 :\<sqsubseteq>val w\<^sub>2"}. Because @{term
Chengsong
parents:
diff changeset
  1434
  "w\<^sub>1"} and @{term "w\<^sub>2"} have the same underlying string
Chengsong
parents:
diff changeset
  1435
  \<open>s\<close>, we can conclude with @{term "Left w\<^sub>1
Chengsong
parents:
diff changeset
  1436
  :\<sqsubseteq>val Left w\<^sub>2"} using
Chengsong
parents:
diff changeset
  1437
  Proposition~\ref{ordintros}\textit{(2)}.\smallskip
Chengsong
parents:
diff changeset
  1438
Chengsong
parents:
diff changeset
  1439
  \item[$\bullet$] Case \<open>P+R\<close> with @{term "s \<in> (ALT r\<^sub>1 r\<^sub>2)
Chengsong
parents:
diff changeset
  1440
  \<rightarrow> (Right w\<^sub>1)"}: This case similar to the previous
Chengsong
parents:
diff changeset
  1441
  case, except that we additionally know @{term "s \<notin> L
Chengsong
parents:
diff changeset
  1442
  r\<^sub>1"}. This is needed when @{term "v\<^sub>2"} is of the form
Chengsong
parents:
diff changeset
  1443
  \mbox{@{term "Left w\<^sub>2"}}. Since \mbox{@{term "flat v\<^sub>2 = flat
Chengsong
parents:
diff changeset
  1444
  w\<^sub>2"} \<open>= s\<close>} and @{term "\<Turnstile> w\<^sub>2 :
Chengsong
parents:
diff changeset
  1445
  r\<^sub>1"}, we can derive a contradiction for \mbox{@{term "s \<notin> L
Chengsong
parents:
diff changeset
  1446
  r\<^sub>1"}} using
Chengsong
parents:
diff changeset
  1447
  Proposition~\ref{inhabs}. So also in this case \mbox{@{term "v\<^sub>1
Chengsong
parents:
diff changeset
  1448
  :\<sqsubseteq>val v\<^sub>2"}}.\smallskip
Chengsong
parents:
diff changeset
  1449
Chengsong
parents:
diff changeset
  1450
  \item[$\bullet$] Case \<open>PS\<close> with @{term "(s\<^sub>1 @
Chengsong
parents:
diff changeset
  1451
  s\<^sub>2) \<in> (SEQ r\<^sub>1 r\<^sub>2) \<rightarrow> (Seq
Chengsong
parents:
diff changeset
  1452
  w\<^sub>1 w\<^sub>2)"}: We can assume @{term "v\<^sub>2 = Seq
Chengsong
parents:
diff changeset
  1453
  (u\<^sub>1) (u\<^sub>2)"} with @{term "\<Turnstile> u\<^sub>1 :
Chengsong
parents:
diff changeset
  1454
  r\<^sub>1"} and \mbox{@{term "\<Turnstile> u\<^sub>2 :
Chengsong
parents:
diff changeset
  1455
  r\<^sub>2"}}. We have @{term "s\<^sub>1 @ s\<^sub>2 = (flat
Chengsong
parents:
diff changeset
  1456
  u\<^sub>1) @ (flat u\<^sub>2)"}.  By the side-condition of the
Chengsong
parents:
diff changeset
  1457
  \<open>PS\<close>-rule we know that either @{term "s\<^sub>1 = flat
Chengsong
parents:
diff changeset
  1458
  u\<^sub>1"} or that @{term "flat u\<^sub>1"} is a strict prefix of
Chengsong
parents:
diff changeset
  1459
  @{term "s\<^sub>1"}. In the latter case we can infer @{term
Chengsong
parents:
diff changeset
  1460
  "w\<^sub>1 :\<sqsubset>val u\<^sub>1"} by
Chengsong
parents:
diff changeset
  1461
  Proposition~\ref{ordlen}\textit{(2)} and from this @{term "v\<^sub>1
Chengsong
parents:
diff changeset
  1462
  :\<sqsubseteq>val v\<^sub>2"} by Proposition~\ref{ordintros}\textit{(5)}
Chengsong
parents:
diff changeset
  1463
  (as noted above @{term "v\<^sub>1"} and @{term "v\<^sub>2"} must have the
Chengsong
parents:
diff changeset
  1464
  same underlying string).
Chengsong
parents:
diff changeset
  1465
  In the former case we know
Chengsong
parents:
diff changeset
  1466
  @{term "u\<^sub>1 \<in> LV r\<^sub>1 s\<^sub>1"} and @{term
Chengsong
parents:
diff changeset
  1467
  "u\<^sub>2 \<in> LV r\<^sub>2 s\<^sub>2"}. With this we can use the
Chengsong
parents:
diff changeset
  1468
  induction hypotheses to infer @{term "w\<^sub>1 :\<sqsubseteq>val
Chengsong
parents:
diff changeset
  1469
  u\<^sub>1"} and @{term "w\<^sub>2 :\<sqsubseteq>val u\<^sub>2"}. By
Chengsong
parents:
diff changeset
  1470
  Proposition~\ref{ordintros}\textit{(4,5)} we can again infer 
Chengsong
parents:
diff changeset
  1471
  @{term "v\<^sub>1 :\<sqsubseteq>val
Chengsong
parents:
diff changeset
  1472
  v\<^sub>2"}.
Chengsong
parents:
diff changeset
  1473
Chengsong
parents:
diff changeset
  1474
  \end{itemize}
Chengsong
parents:
diff changeset
  1475
Chengsong
parents:
diff changeset
  1476
  \noindent The case for \<open>P\<star>\<close> is similar to the \<open>PS\<close>-case and omitted.\qed
Chengsong
parents:
diff changeset
  1477
  \end{proof}
Chengsong
parents:
diff changeset
  1478
Chengsong
parents:
diff changeset
  1479
  \noindent This theorem shows that our \<open>POSIX\<close> value for a
Chengsong
parents:
diff changeset
  1480
  regular expression \<open>r\<close> and string @{term s} is in fact a
Chengsong
parents:
diff changeset
  1481
  minimal element of the values in \<open>LV r s\<close>. By
Chengsong
parents:
diff changeset
  1482
  Proposition~\ref{ordlen}\textit{(2)} we also know that any value in
Chengsong
parents:
diff changeset
  1483
  \<open>LV r s'\<close>, with @{term "s'"} being a strict prefix, cannot be
Chengsong
parents:
diff changeset
  1484
  smaller than \<open>v\<^sub>1\<close>. The next theorem shows the
Chengsong
parents:
diff changeset
  1485
  opposite---namely any minimal element in @{term "LV r s"} must be a
Chengsong
parents:
diff changeset
  1486
  \<open>POSIX\<close> value. This can be established by induction on \<open>r\<close>, but the proof can be drastically simplified by using the fact
Chengsong
parents:
diff changeset
  1487
  from the previous section about the existence of a \<open>POSIX\<close> value
Chengsong
parents:
diff changeset
  1488
  whenever a string @{term "s \<in> L r"}.
Chengsong
parents:
diff changeset
  1489
Chengsong
parents:
diff changeset
  1490
Chengsong
parents:
diff changeset
  1491
  \begin{theorem}
Chengsong
parents:
diff changeset
  1492
  @{thm [mode=IfThen] PosOrd_Posix[where ?v1.0="v\<^sub>1"]} 
Chengsong
parents:
diff changeset
  1493
  \end{theorem}
Chengsong
parents:
diff changeset
  1494
Chengsong
parents:
diff changeset
  1495
  \begin{proof} 
Chengsong
parents:
diff changeset
  1496
  If @{thm (prem 1) PosOrd_Posix[where ?v1.0="v\<^sub>1"]} then 
Chengsong
parents:
diff changeset
  1497
  @{term "s \<in> L r"} by Proposition~\ref{inhabs}. Hence by Theorem~\ref{lexercorrect}(2) 
Chengsong
parents:
diff changeset
  1498
  there exists a
Chengsong
parents:
diff changeset
  1499
  \<open>POSIX\<close> value @{term "v\<^sub>P"} with @{term "s \<in> r \<rightarrow> v\<^sub>P"}
Chengsong
parents:
diff changeset
  1500
  and by Lemma~\ref{LVposix} we also have \mbox{@{term "v\<^sub>P \<in> LV r s"}}.
Chengsong
parents:
diff changeset
  1501
  By Theorem~\ref{orderone} we therefore have 
Chengsong
parents:
diff changeset
  1502
  @{term "v\<^sub>P :\<sqsubseteq>val v\<^sub>1"}. If @{term "v\<^sub>P = v\<^sub>1"} then
Chengsong
parents:
diff changeset
  1503
  we are done. Otherwise we have @{term "v\<^sub>P :\<sqsubset>val v\<^sub>1"}, which 
Chengsong
parents:
diff changeset
  1504
  however contradicts the second assumption about @{term "v\<^sub>1"} being the smallest
Chengsong
parents:
diff changeset
  1505
  element in @{term "LV r s"}. So we are done in this case too.\qed
Chengsong
parents:
diff changeset
  1506
  \end{proof}
Chengsong
parents:
diff changeset
  1507
Chengsong
parents:
diff changeset
  1508
  \noindent
Chengsong
parents:
diff changeset
  1509
  From this we can also show 
Chengsong
parents:
diff changeset
  1510
  that if @{term "LV r s"} is non-empty (or equivalently @{term "s \<in> L r"}) then 
Chengsong
parents:
diff changeset
  1511
  it has a unique minimal element:
Chengsong
parents:
diff changeset
  1512
Chengsong
parents:
diff changeset
  1513
  \begin{corollary}
Chengsong
parents:
diff changeset
  1514
  @{thm [mode=IfThen] Least_existence1}
Chengsong
parents:
diff changeset
  1515
  \end{corollary}
Chengsong
parents:
diff changeset
  1516
Chengsong
parents:
diff changeset
  1517
Chengsong
parents:
diff changeset
  1518
Chengsong
parents:
diff changeset
  1519
  \noindent To sum up, we have shown that the (unique) minimal elements 
Chengsong
parents:
diff changeset
  1520
  of the ordering by Okui and Suzuki are exactly the \<open>POSIX\<close>
Chengsong
parents:
diff changeset
  1521
  values we defined inductively in Section~\ref{posixsec}. This provides
Chengsong
parents:
diff changeset
  1522
  an independent confirmation that our ternary relation formalises the
Chengsong
parents:
diff changeset
  1523
  informal POSIX rules. 
Chengsong
parents:
diff changeset
  1524
Chengsong
parents:
diff changeset
  1525
\<close>
Chengsong
parents:
diff changeset
  1526
Chengsong
parents:
diff changeset
  1527
section \<open>Bitcoded Lexing\<close>
Chengsong
parents:
diff changeset
  1528
Chengsong
parents:
diff changeset
  1529
Chengsong
parents:
diff changeset
  1530
Chengsong
parents:
diff changeset
  1531
Chengsong
parents:
diff changeset
  1532
text \<open>
Chengsong
parents:
diff changeset
  1533
Chengsong
parents:
diff changeset
  1534
Incremental calculation of the value. To simplify the proof we first define the function
Chengsong
parents:
diff changeset
  1535
@{const flex} which calculates the ``iterated'' injection function. With this we can 
Chengsong
parents:
diff changeset
  1536
rewrite the lexer as
Chengsong
parents:
diff changeset
  1537
Chengsong
parents:
diff changeset
  1538
\begin{center}
Chengsong
parents:
diff changeset
  1539
@{thm lexer_flex}
Chengsong
parents:
diff changeset
  1540
\end{center}
Chengsong
parents:
diff changeset
  1541
Chengsong
parents:
diff changeset
  1542
Chengsong
parents:
diff changeset
  1543
\<close>
Chengsong
parents:
diff changeset
  1544
Chengsong
parents:
diff changeset
  1545
section \<open>Optimisations\<close>
Chengsong
parents:
diff changeset
  1546
Chengsong
parents:
diff changeset
  1547
text \<open>
Chengsong
parents:
diff changeset
  1548
Chengsong
parents:
diff changeset
  1549
  Derivatives as calculated by \Brz's method are usually more complex
Chengsong
parents:
diff changeset
  1550
  regular expressions than the initial one; the result is that the
Chengsong
parents:
diff changeset
  1551
  derivative-based matching and lexing algorithms are often abysmally slow.
Chengsong
parents:
diff changeset
  1552
  However, various optimisations are possible, such as the simplifications
Chengsong
parents:
diff changeset
  1553
  of @{term "ALT ZERO r"}, @{term "ALT r ZERO"}, @{term "SEQ ONE r"} and
Chengsong
parents:
diff changeset
  1554
  @{term "SEQ r ONE"} to @{term r}. These simplifications can speed up the
Chengsong
parents:
diff changeset
  1555
  algorithms considerably, as noted in \cite{Sulzmann2014}. One of the
Chengsong
parents:
diff changeset
  1556
  advantages of having a simple specification and correctness proof is that
Chengsong
parents:
diff changeset
  1557
  the latter can be refined to prove the correctness of such simplification
Chengsong
parents:
diff changeset
  1558
  steps. While the simplification of regular expressions according to 
Chengsong
parents:
diff changeset
  1559
  rules like
Chengsong
parents:
diff changeset
  1560
Chengsong
parents:
diff changeset
  1561
  \begin{equation}\label{Simpl}
Chengsong
parents:
diff changeset
  1562
  \begin{array}{lcllcllcllcl}
Chengsong
parents:
diff changeset
  1563
  @{term "ALT ZERO r"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\
Chengsong
parents:
diff changeset
  1564
  @{term "ALT r ZERO"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\
Chengsong
parents:
diff changeset
  1565
  @{term "SEQ ONE r"}  & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\
Chengsong
parents:
diff changeset
  1566
  @{term "SEQ r ONE"}  & \<open>\<Rightarrow>\<close> & @{term r}
Chengsong
parents:
diff changeset
  1567
  \end{array}
Chengsong
parents:
diff changeset
  1568
  \end{equation}
Chengsong
parents:
diff changeset
  1569
Chengsong
parents:
diff changeset
  1570
  \noindent is well understood, there is an obstacle with the POSIX value
Chengsong
parents:
diff changeset
  1571
  calculation algorithm by Sulzmann and Lu: if we build a derivative regular
Chengsong
parents:
diff changeset
  1572
  expression and then simplify it, we will calculate a POSIX value for this
Chengsong
parents:
diff changeset
  1573
  simplified derivative regular expression, \emph{not} for the original (unsimplified)
Chengsong
parents:
diff changeset
  1574
  derivative regular expression. Sulzmann and Lu \cite{Sulzmann2014} overcome this obstacle by
Chengsong
parents:
diff changeset
  1575
  not just calculating a simplified regular expression, but also calculating
Chengsong
parents:
diff changeset
  1576
  a \emph{rectification function} that ``repairs'' the incorrect value.
Chengsong
parents:
diff changeset
  1577
  
Chengsong
parents:
diff changeset
  1578
  The rectification functions can be (slightly clumsily) implemented  in
Chengsong
parents:
diff changeset
  1579
  Isabelle/HOL as follows using some auxiliary functions:
Chengsong
parents:
diff changeset
  1580
Chengsong
parents:
diff changeset
  1581
  \begin{center}
Chengsong
parents:
diff changeset
  1582
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1583
  @{thm (lhs) F_RIGHT.simps(1)} & $\dn$ & \<open>Right (f v)\<close>\\
Chengsong
parents:
diff changeset
  1584
  @{thm (lhs) F_LEFT.simps(1)} & $\dn$ & \<open>Left (f v)\<close>\\
Chengsong
parents:
diff changeset
  1585
  
Chengsong
parents:
diff changeset
  1586
  @{thm (lhs) F_ALT.simps(1)} & $\dn$ & \<open>Right (f\<^sub>2 v)\<close>\\
Chengsong
parents:
diff changeset
  1587
  @{thm (lhs) F_ALT.simps(2)} & $\dn$ & \<open>Left (f\<^sub>1 v)\<close>\\
Chengsong
parents:
diff changeset
  1588
  
Chengsong
parents:
diff changeset
  1589
  @{thm (lhs) F_SEQ1.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 ()) (f\<^sub>2 v)\<close>\\
Chengsong
parents:
diff changeset
  1590
  @{thm (lhs) F_SEQ2.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v) (f\<^sub>2 ())\<close>\\
Chengsong
parents:
diff changeset
  1591
  @{thm (lhs) F_SEQ.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)\<close>\medskip\\
Chengsong
parents:
diff changeset
  1592
  %\end{tabular}
Chengsong
parents:
diff changeset
  1593
  %
Chengsong
parents:
diff changeset
  1594
  %\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1595
  @{term "simp_ALT (ZERO, DUMMY) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_RIGHT f\<^sub>2)"}\\
Chengsong
parents:
diff changeset
  1596
  @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (ZERO, DUMMY)"} & $\dn$ & @{term "(r\<^sub>1, F_LEFT f\<^sub>1)"}\\
Chengsong
parents:
diff changeset
  1597
  @{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)"}\\
Chengsong
parents:
diff changeset
  1598
  @{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)"}\\
Chengsong
parents:
diff changeset
  1599
  @{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)"}\\
Chengsong
parents:
diff changeset
  1600
  @{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)"}\\
Chengsong
parents:
diff changeset
  1601
  \end{tabular}
Chengsong
parents:
diff changeset
  1602
  \end{center}
Chengsong
parents:
diff changeset
  1603
Chengsong
parents:
diff changeset
  1604
  \noindent
Chengsong
parents:
diff changeset
  1605
  The functions \<open>simp\<^bsub>Alt\<^esub>\<close> and \<open>simp\<^bsub>Seq\<^esub>\<close> encode the simplification rules
Chengsong
parents:
diff changeset
  1606
  in \eqref{Simpl} and compose the rectification functions (simplifications can occur
Chengsong
parents:
diff changeset
  1607
  deep inside the regular expression). The main simplification function is then 
Chengsong
parents:
diff changeset
  1608
Chengsong
parents:
diff changeset
  1609
  \begin{center}
Chengsong
parents:
diff changeset
  1610
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1611
  @{term "simp (ALT r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_ALT (simp r\<^sub>1) (simp r\<^sub>2)"}\\
Chengsong
parents:
diff changeset
  1612
  @{term "simp (SEQ r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_SEQ (simp r\<^sub>1) (simp r\<^sub>2)"}\\
Chengsong
parents:
diff changeset
  1613
  @{term "simp r"} & $\dn$ & @{term "(r, id)"}\\
Chengsong
parents:
diff changeset
  1614
  \end{tabular}
Chengsong
parents:
diff changeset
  1615
  \end{center} 
Chengsong
parents:
diff changeset
  1616
Chengsong
parents:
diff changeset
  1617
  \noindent where @{term "id"} stands for the identity function. The
Chengsong
parents:
diff changeset
  1618
  function @{const simp} returns a simplified regular expression and a corresponding
Chengsong
parents:
diff changeset
  1619
  rectification function. Note that we do not simplify under stars: this
Chengsong
parents:
diff changeset
  1620
  seems to slow down the algorithm, rather than speed it up. The optimised
Chengsong
parents:
diff changeset
  1621
  lexer is then given by the clauses:
Chengsong
parents:
diff changeset
  1622
  
Chengsong
parents:
diff changeset
  1623
  \begin{center}
Chengsong
parents:
diff changeset
  1624
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1625
  @{thm (lhs) slexer.simps(1)} & $\dn$ & @{thm (rhs) slexer.simps(1)}\\
Chengsong
parents:
diff changeset
  1626
  @{thm (lhs) slexer.simps(2)} & $\dn$ & 
Chengsong
parents:
diff changeset
  1627
                         \<open>let (r\<^sub>s, f\<^sub>r) = simp (r \<close>$\backslash$\<open> c) in\<close>\\
Chengsong
parents:
diff changeset
  1628
                     & & \<open>case\<close> @{term "slexer r\<^sub>s s"} \<open>of\<close>\\
Chengsong
parents:
diff changeset
  1629
                     & & \phantom{$|$} @{term "None"}  \<open>\<Rightarrow>\<close> @{term None}\\
Chengsong
parents:
diff changeset
  1630
                     & & $|$ @{term "Some v"} \<open>\<Rightarrow>\<close> \<open>Some (inj r c (f\<^sub>r v))\<close>                          
Chengsong
parents:
diff changeset
  1631
  \end{tabular}
Chengsong
parents:
diff changeset
  1632
  \end{center}
Chengsong
parents:
diff changeset
  1633
Chengsong
parents:
diff changeset
  1634
  \noindent
Chengsong
parents:
diff changeset
  1635
  In the second clause we first calculate the derivative @{term "der c r"}
Chengsong
parents:
diff changeset
  1636
  and then simpli
Chengsong
parents:
diff changeset
  1637
Chengsong
parents:
diff changeset
  1638
text \<open>
Chengsong
parents:
diff changeset
  1639
Chengsong
parents:
diff changeset
  1640
Incremental calculation of the value. To simplify the proof we first define the function
Chengsong
parents:
diff changeset
  1641
@{const flex} which calculates the ``iterated'' injection function. With this we can 
Chengsong
parents:
diff changeset
  1642
rewrite the lexer as
Chengsong
parents:
diff changeset
  1643
Chengsong
parents:
diff changeset
  1644
\begin{center}
Chengsong
parents:
diff changeset
  1645
@{thm lexer_flex}
Chengsong
parents:
diff changeset
  1646
\end{center}
Chengsong
parents:
diff changeset
  1647
Chengsong
parents:
diff changeset
  1648
\begin{center}
Chengsong
parents:
diff changeset
  1649
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1650
  @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\
Chengsong
parents:
diff changeset
  1651
  @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\
Chengsong
parents:
diff changeset
  1652
  @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\
Chengsong
parents:
diff changeset
  1653
  @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}\\
Chengsong
parents:
diff changeset
  1654
  @{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"]}\\
Chengsong
parents:
diff changeset
  1655
  @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\
Chengsong
parents:
diff changeset
  1656
  @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)}
Chengsong
parents:
diff changeset
  1657
\end{tabular}
Chengsong
parents:
diff changeset
  1658
\end{center}
Chengsong
parents:
diff changeset
  1659
Chengsong
parents:
diff changeset
  1660
\begin{center}
Chengsong
parents:
diff changeset
  1661
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1662
  @{term areg} & $::=$ & @{term "AZERO"}\\
Chengsong
parents:
diff changeset
  1663
               & $\mid$ & @{term "AONE bs"}\\
Chengsong
parents:
diff changeset
  1664
               & $\mid$ & @{term "ACHAR bs c"}\\
Chengsong
parents:
diff changeset
  1665
               & $\mid$ & @{term "AALT bs r1 r2"}\\
Chengsong
parents:
diff changeset
  1666
               & $\mid$ & @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}\\
Chengsong
parents:
diff changeset
  1667
               & $\mid$ & @{term "ASTAR bs r"}
Chengsong
parents:
diff changeset
  1668
\end{tabular}
Chengsong
parents:
diff changeset
  1669
\end{center}
Chengsong
parents:
diff changeset
  1670
Chengsong
parents:
diff changeset
  1671
Chengsong
parents:
diff changeset
  1672
\begin{center}
Chengsong
parents:
diff changeset
  1673
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1674
  @{thm (lhs) intern.simps(1)} & $\dn$ & @{thm (rhs) intern.simps(1)}\\
Chengsong
parents:
diff changeset
  1675
  @{thm (lhs) intern.simps(2)} & $\dn$ & @{thm (rhs) intern.simps(2)}\\
Chengsong
parents:
diff changeset
  1676
  @{thm (lhs) intern.simps(3)} & $\dn$ & @{thm (rhs) intern.simps(3)}\\
Chengsong
parents:
diff changeset
  1677
  @{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"]}\\
Chengsong
parents:
diff changeset
  1678
  @{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"]}\\
Chengsong
parents:
diff changeset
  1679
  @{thm (lhs) intern.simps(6)} & $\dn$ & @{thm (rhs) intern.simps(6)}\\
Chengsong
parents:
diff changeset
  1680
\end{tabular}
Chengsong
parents:
diff changeset
  1681
\end{center}
Chengsong
parents:
diff changeset
  1682
Chengsong
parents:
diff changeset
  1683
\begin{center}
Chengsong
parents:
diff changeset
  1684
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1685
  @{thm (lhs) erase.simps(1)} & $\dn$ & @{thm (rhs) erase.simps(1)}\\
Chengsong
parents:
diff changeset
  1686
  @{thm (lhs) erase.simps(2)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(2)[of bs]}\\
Chengsong
parents:
diff changeset
  1687
  @{thm (lhs) erase.simps(3)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(3)[of bs]}\\
Chengsong
parents:
diff changeset
  1688
  @{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"]}\\
Chengsong
parents:
diff changeset
  1689
  @{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"]}\\
Chengsong
parents:
diff changeset
  1690
  @{thm (lhs) erase.simps(6)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(6)[of bs]}\\
Chengsong
parents:
diff changeset
  1691
\end{tabular}
Chengsong
parents:
diff changeset
  1692
\end{center}
Chengsong
parents:
diff changeset
  1693
Chengsong
parents:
diff changeset
  1694
Some simple facts about erase
Chengsong
parents:
diff changeset
  1695
Chengsong
parents:
diff changeset
  1696
\begin{lemma}\mbox{}\\
Chengsong
parents:
diff changeset
  1697
@{thm erase_bder}\\
Chengsong
parents:
diff changeset
  1698
@{thm erase_intern}
Chengsong
parents:
diff changeset
  1699
\end{lemma}
Chengsong
parents:
diff changeset
  1700
Chengsong
parents:
diff changeset
  1701
\begin{center}
Chengsong
parents:
diff changeset
  1702
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1703
  @{thm (lhs) bnullable.simps(1)} & $\dn$ & @{thm (rhs) bnullable.simps(1)}\\
Chengsong
parents:
diff changeset
  1704
  @{thm (lhs) bnullable.simps(2)} & $\dn$ & @{thm (rhs) bnullable.simps(2)}\\
Chengsong
parents:
diff changeset
  1705
  @{thm (lhs) bnullable.simps(3)} & $\dn$ & @{thm (rhs) bnullable.simps(3)}\\
Chengsong
parents:
diff changeset
  1706
  @{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"]}\\
Chengsong
parents:
diff changeset
  1707
  @{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"]}\\
Chengsong
parents:
diff changeset
  1708
  @{thm (lhs) bnullable.simps(6)} & $\dn$ & @{thm (rhs) bnullable.simps(6)}\medskip\\
Chengsong
parents:
diff changeset
  1709
Chengsong
parents:
diff changeset
  1710
%  \end{tabular}
Chengsong
parents:
diff changeset
  1711
%  \end{center}
Chengsong
parents:
diff changeset
  1712
Chengsong
parents:
diff changeset
  1713
%  \begin{center}
Chengsong
parents:
diff changeset
  1714
%  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1715
Chengsong
parents:
diff changeset
  1716
  @{thm (lhs) bder.simps(1)} & $\dn$ & @{thm (rhs) bder.simps(1)}\\
Chengsong
parents:
diff changeset
  1717
  @{thm (lhs) bder.simps(2)} & $\dn$ & @{thm (rhs) bder.simps(2)}\\
Chengsong
parents:
diff changeset
  1718
  @{thm (lhs) bder.simps(3)} & $\dn$ & @{thm (rhs) bder.simps(3)}\\
Chengsong
parents:
diff changeset
  1719
  @{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"]}\\
Chengsong
parents:
diff changeset
  1720
  @{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"]}\\
Chengsong
parents:
diff changeset
  1721
  @{thm (lhs) bder.simps(6)} & $\dn$ & @{thm (rhs) bder.simps(6)}
Chengsong
parents:
diff changeset
  1722
  \end{tabular}
Chengsong
parents:
diff changeset
  1723
  \end{center}
Chengsong
parents:
diff changeset
  1724
Chengsong
parents:
diff changeset
  1725
Chengsong
parents:
diff changeset
  1726
\begin{center}
Chengsong
parents:
diff changeset
  1727
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1728
  @{thm (lhs) bmkeps.simps(1)} & $\dn$ & @{thm (rhs) bmkeps.simps(1)}\\
Chengsong
parents:
diff changeset
  1729
  @{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"]}\\
Chengsong
parents:
diff changeset
  1730
  @{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"]}\\
Chengsong
parents:
diff changeset
  1731
  @{thm (lhs) bmkeps.simps(4)} & $\dn$ & @{thm (rhs) bmkeps.simps(4)}\medskip\\
Chengsong
parents:
diff changeset
  1732
\end{tabular}
Chengsong
parents:
diff changeset
  1733
\end{center}
Chengsong
parents:
diff changeset
  1734
Chengsong
parents:
diff changeset
  1735
Chengsong
parents:
diff changeset
  1736
@{thm [mode=IfThen] bder_retrieve}
Chengsong
parents:
diff changeset
  1737
Chengsong
parents:
diff changeset
  1738
By induction on \<open>r\<close>
Chengsong
parents:
diff changeset
  1739
Chengsong
parents:
diff changeset
  1740
\begin{theorem}[Main Lemma]\mbox{}\\
Chengsong
parents:
diff changeset
  1741
@{thm [mode=IfThen] MAIN_decode}
Chengsong
parents:
diff changeset
  1742
\end{theorem}
Chengsong
parents:
diff changeset
  1743
Chengsong
parents:
diff changeset
  1744
\noindent
Chengsong
parents:
diff changeset
  1745
Definition of the bitcoded lexer
Chengsong
parents:
diff changeset
  1746
Chengsong
parents:
diff changeset
  1747
@{thm blexer_def}
Chengsong
parents:
diff changeset
  1748
Chengsong
parents:
diff changeset
  1749
Chengsong
parents:
diff changeset
  1750
\begin{theorem}
Chengsong
parents:
diff changeset
  1751
@{thm blexer_correctness}
Chengsong
parents:
diff changeset
  1752
\end{theorem}
Chengsong
parents:
diff changeset
  1753
Chengsong
parents:
diff changeset
  1754
\<close>
Chengsong
parents:
diff changeset
  1755
Chengsong
parents:
diff changeset
  1756
section \<open>Optimisations\<close>
Chengsong
parents:
diff changeset
  1757
Chengsong
parents:
diff changeset
  1758
text \<open>
Chengsong
parents:
diff changeset
  1759
Chengsong
parents:
diff changeset
  1760
  Derivatives as calculated by \Brz's method are usually more complex
Chengsong
parents:
diff changeset
  1761
  regular expressions than the initial one; the result is that the
Chengsong
parents:
diff changeset
  1762
  derivative-based matching and lexing algorithms are often abysmally slow.
Chengsong
parents:
diff changeset
  1763
  However, various optimisations are possible, such as the simplifications
Chengsong
parents:
diff changeset
  1764
  of @{term "ALT ZERO r"}, @{term "ALT r ZERO"}, @{term "SEQ ONE r"} and
Chengsong
parents:
diff changeset
  1765
  @{term "SEQ r ONE"} to @{term r}. These simplifications can speed up the
Chengsong
parents:
diff changeset
  1766
  algorithms considerably, as noted in \cite{Sulzmann2014}. One of the
Chengsong
parents:
diff changeset
  1767
  advantages of having a simple specification and correctness proof is that
Chengsong
parents:
diff changeset
  1768
  the latter can be refined to prove the correctness of such simplification
Chengsong
parents:
diff changeset
  1769
  steps. While the simplification of regular expressions according to 
Chengsong
parents:
diff changeset
  1770
  rules like
Chengsong
parents:
diff changeset
  1771
Chengsong
parents:
diff changeset
  1772
  \begin{equation}\label{Simpl}
Chengsong
parents:
diff changeset
  1773
  \begin{array}{lcllcllcllcl}
Chengsong
parents:
diff changeset
  1774
  @{term "ALT ZERO r"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\
Chengsong
parents:
diff changeset
  1775
  @{term "ALT r ZERO"} & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\
Chengsong
parents:
diff changeset
  1776
  @{term "SEQ ONE r"}  & \<open>\<Rightarrow>\<close> & @{term r} \hspace{8mm}%\\
Chengsong
parents:
diff changeset
  1777
  @{term "SEQ r ONE"}  & \<open>\<Rightarrow>\<close> & @{term r}
Chengsong
parents:
diff changeset
  1778
  \end{array}
Chengsong
parents:
diff changeset
  1779
  \end{equation}
Chengsong
parents:
diff changeset
  1780
Chengsong
parents:
diff changeset
  1781
  \noindent is well understood, there is an obstacle with the POSIX value
Chengsong
parents:
diff changeset
  1782
  calculation algorithm by Sulzmann and Lu: if we build a derivative regular
Chengsong
parents:
diff changeset
  1783
  expression and then simplify it, we will calculate a POSIX value for this
Chengsong
parents:
diff changeset
  1784
  simplified derivative regular expression, \emph{not} for the original (unsimplified)
Chengsong
parents:
diff changeset
  1785
  derivative regular expression. Sulzmann and Lu \cite{Sulzmann2014} overcome this obstacle by
Chengsong
parents:
diff changeset
  1786
  not just calculating a simplified regular expression, but also calculating
Chengsong
parents:
diff changeset
  1787
  a \emph{rectification function} that ``repairs'' the incorrect value.
Chengsong
parents:
diff changeset
  1788
  
Chengsong
parents:
diff changeset
  1789
  The rectification functions can be (slightly clumsily) implemented  in
Chengsong
parents:
diff changeset
  1790
  Isabelle/HOL as follows using some auxiliary functions:
Chengsong
parents:
diff changeset
  1791
Chengsong
parents:
diff changeset
  1792
  \begin{center}
Chengsong
parents:
diff changeset
  1793
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1794
  @{thm (lhs) F_RIGHT.simps(1)} & $\dn$ & \<open>Right (f v)\<close>\\
Chengsong
parents:
diff changeset
  1795
  @{thm (lhs) F_LEFT.simps(1)} & $\dn$ & \<open>Left (f v)\<close>\\
Chengsong
parents:
diff changeset
  1796
  
Chengsong
parents:
diff changeset
  1797
  @{thm (lhs) F_ALT.simps(1)} & $\dn$ & \<open>Right (f\<^sub>2 v)\<close>\\
Chengsong
parents:
diff changeset
  1798
  @{thm (lhs) F_ALT.simps(2)} & $\dn$ & \<open>Left (f\<^sub>1 v)\<close>\\
Chengsong
parents:
diff changeset
  1799
  
Chengsong
parents:
diff changeset
  1800
  @{thm (lhs) F_SEQ1.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 ()) (f\<^sub>2 v)\<close>\\
Chengsong
parents:
diff changeset
  1801
  @{thm (lhs) F_SEQ2.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v) (f\<^sub>2 ())\<close>\\
Chengsong
parents:
diff changeset
  1802
  @{thm (lhs) F_SEQ.simps(1)} & $\dn$ & \<open>Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)\<close>\medskip\\
Chengsong
parents:
diff changeset
  1803
  %\end{tabular}
Chengsong
parents:
diff changeset
  1804
  %
Chengsong
parents:
diff changeset
  1805
  %\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1806
  @{term "simp_ALT (ZERO, DUMMY) (r\<^sub>2, f\<^sub>2)"} & $\dn$ & @{term "(r\<^sub>2, F_RIGHT f\<^sub>2)"}\\
Chengsong
parents:
diff changeset
  1807
  @{term "simp_ALT (r\<^sub>1, f\<^sub>1) (ZERO, DUMMY)"} & $\dn$ & @{term "(r\<^sub>1, F_LEFT f\<^sub>1)"}\\
Chengsong
parents:
diff changeset
  1808
  @{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)"}\\
Chengsong
parents:
diff changeset
  1809
  @{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)"}\\
Chengsong
parents:
diff changeset
  1810
  @{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)"}\\
Chengsong
parents:
diff changeset
  1811
  @{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)"}\\
Chengsong
parents:
diff changeset
  1812
  \end{tabular}
Chengsong
parents:
diff changeset
  1813
  \end{center}
Chengsong
parents:
diff changeset
  1814
Chengsong
parents:
diff changeset
  1815
  \noindent
Chengsong
parents:
diff changeset
  1816
  The functions \<open>simp\<^bsub>Alt\<^esub>\<close> and \<open>simp\<^bsub>Seq\<^esub>\<close> encode the simplification rules
Chengsong
parents:
diff changeset
  1817
  in \eqref{Simpl} and compose the rectification functions (simplifications can occur
Chengsong
parents:
diff changeset
  1818
  deep inside the regular expression). The main simplification function is then 
Chengsong
parents:
diff changeset
  1819
Chengsong
parents:
diff changeset
  1820
  \begin{center}
Chengsong
parents:
diff changeset
  1821
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1822
  @{term "simp (ALT r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_ALT (simp r\<^sub>1) (simp r\<^sub>2)"}\\
Chengsong
parents:
diff changeset
  1823
  @{term "simp (SEQ r\<^sub>1 r\<^sub>2)"} & $\dn$ & @{term "simp_SEQ (simp r\<^sub>1) (simp r\<^sub>2)"}\\
Chengsong
parents:
diff changeset
  1824
  @{term "simp r"} & $\dn$ & @{term "(r, id)"}\\
Chengsong
parents:
diff changeset
  1825
  \end{tabular}
Chengsong
parents:
diff changeset
  1826
  \end{center} 
Chengsong
parents:
diff changeset
  1827
Chengsong
parents:
diff changeset
  1828
  \noindent where @{term "id"} stands for the identity function. The
Chengsong
parents:
diff changeset
  1829
  function @{const simp} returns a simplified regular expression and a corresponding
Chengsong
parents:
diff changeset
  1830
  rectification function. Note that we do not simplify under stars: this
Chengsong
parents:
diff changeset
  1831
  seems to slow down the algorithm, rather than speed it up. The optimised
Chengsong
parents:
diff changeset
  1832
  lexer is then given by the clauses:
Chengsong
parents:
diff changeset
  1833
  
Chengsong
parents:
diff changeset
  1834
  \begin{center}
Chengsong
parents:
diff changeset
  1835
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1836
  @{thm (lhs) slexer.simps(1)} & $\dn$ & @{thm (rhs) slexer.simps(1)}\\
Chengsong
parents:
diff changeset
  1837
  @{thm (lhs) slexer.simps(2)} & $\dn$ & 
Chengsong
parents:
diff changeset
  1838
                         \<open>let (r\<^sub>s, f\<^sub>r) = simp (r \<close>$\backslash$\<open> c) in\<close>\\
Chengsong
parents:
diff changeset
  1839
                     & & \<open>case\<close> @{term "slexer r\<^sub>s s"} \<open>of\<close>\\
Chengsong
parents:
diff changeset
  1840
                     & & \phantom{$|$} @{term "None"}  \<open>\<Rightarrow>\<close> @{term None}\\
Chengsong
parents:
diff changeset
  1841
                     & & $|$ @{term "Some v"} \<open>\<Rightarrow>\<close> \<open>Some (inj r c (f\<^sub>r v))\<close>                          
Chengsong
parents:
diff changeset
  1842
  \end{tabular}
Chengsong
parents:
diff changeset
  1843
  \end{center}
Chengsong
parents:
diff changeset
  1844
Chengsong
parents:
diff changeset
  1845
  \noindent
Chengsong
parents:
diff changeset
  1846
  In the second clause we first calculate the derivative @{term "der c r"}
Chengsong
parents:
diff changeset
  1847
  and then simplify the result. This gives us a simplified derivative
Chengsong
parents:
diff changeset
  1848
  \<open>r\<^sub>s\<close> and a rectification function \<open>f\<^sub>r\<close>. The lexer
Chengsong
parents:
diff changeset
  1849
  is then recursively called with the simplified derivative, but before
Chengsong
parents:
diff changeset
  1850
  we inject the character @{term c} into the value @{term v}, we need to rectify
Chengsong
parents:
diff changeset
  1851
  @{term v} (that is construct @{term "f\<^sub>r v"}). Before we can establish the correctness
Chengsong
parents:
diff changeset
  1852
  of @{term "slexer"}, we need to show that simplification preserves the language
Chengsong
parents:
diff changeset
  1853
  and simplification preserves our POSIX relation once the value is rectified
Chengsong
parents:
diff changeset
  1854
  (recall @{const "simp"} generates a (regular expression, rectification function) pair):
Chengsong
parents:
diff changeset
  1855
Chengsong
parents:
diff changeset
  1856
  \begin{lemma}\mbox{}\smallskip\\\label{slexeraux}
Chengsong
parents:
diff changeset
  1857
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
  1858
  (1) & @{thm L_fst_simp[symmetric]}\\
Chengsong
parents:
diff changeset
  1859
  (2) & @{thm[mode=IfThen] Posix_simp}
Chengsong
parents:
diff changeset
  1860
  \end{tabular}
Chengsong
parents:
diff changeset
  1861
  \end{lemma}
Chengsong
parents:
diff changeset
  1862
Chengsong
parents:
diff changeset
  1863
  \begin{proof} Both are by induction on \<open>r\<close>. There is no
Chengsong
parents:
diff changeset
  1864
  interesting case for the first statement. For the second statement,
Chengsong
parents:
diff changeset
  1865
  of interest are the @{term "r = ALT r\<^sub>1 r\<^sub>2"} and @{term "r = SEQ r\<^sub>1
Chengsong
parents:
diff changeset
  1866
  r\<^sub>2"} cases. In each case we have to analyse four subcases whether
Chengsong
parents:
diff changeset
  1867
  @{term "fst (simp r\<^sub>1)"} and @{term "fst (simp r\<^sub>2)"} equals @{const
Chengsong
parents:
diff changeset
  1868
  ZERO} (respectively @{const ONE}). For example for @{term "r = ALT
Chengsong
parents:
diff changeset
  1869
  r\<^sub>1 r\<^sub>2"}, consider the subcase @{term "fst (simp r\<^sub>1) = ZERO"} and
Chengsong
parents:
diff changeset
  1870
  @{term "fst (simp r\<^sub>2) \<noteq> ZERO"}. By assumption we know @{term "s \<in>
Chengsong
parents:
diff changeset
  1871
  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"}
Chengsong
parents:
diff changeset
  1872
  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"}
Chengsong
parents:
diff changeset
  1873
  we know @{term "L (fst (simp r\<^sub>1)) = {}"}. By the first statement
Chengsong
parents:
diff changeset
  1874
  @{term "L r\<^sub>1"} is the empty set, meaning (**) @{term "s \<notin> L r\<^sub>1"}.
Chengsong
parents:
diff changeset
  1875
  Taking (*) and (**) together gives by the \mbox{\<open>P+R\<close>}-rule 
Chengsong
parents:
diff changeset
  1876
  @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> Right (snd (simp r\<^sub>2) v)"}. In turn this
Chengsong
parents:
diff changeset
  1877
  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.
Chengsong
parents:
diff changeset
  1878
  The other cases are similar.\qed
Chengsong
parents:
diff changeset
  1879
  \end{proof}
Chengsong
parents:
diff changeset
  1880
Chengsong
parents:
diff changeset
  1881
  \noindent We can now prove relatively straightforwardly that the
Chengsong
parents:
diff changeset
  1882
  optimised lexer produces the expected result:
Chengsong
parents:
diff changeset
  1883
Chengsong
parents:
diff changeset
  1884
  \begin{theorem}
Chengsong
parents:
diff changeset
  1885
  @{thm slexer_correctness}
Chengsong
parents:
diff changeset
  1886
  \end{theorem}
Chengsong
parents:
diff changeset
  1887
Chengsong
parents:
diff changeset
  1888
  \begin{proof} By induction on @{term s} generalising over @{term
Chengsong
parents:
diff changeset
  1889
  r}. The case @{term "[]"} is trivial. For the cons-case suppose the
Chengsong
parents:
diff changeset
  1890
  string is of the form @{term "c # s"}. By induction hypothesis we
Chengsong
parents:
diff changeset
  1891
  know @{term "slexer r s = lexer r s"} holds for all @{term r} (in
Chengsong
parents:
diff changeset
  1892
  particular for @{term "r"} being the derivative @{term "der c
Chengsong
parents:
diff changeset
  1893
  r"}). Let @{term "r\<^sub>s"} be the simplified derivative regular expression, that is @{term
Chengsong
parents:
diff changeset
  1894
  "fst (simp (der c r))"}, and @{term "f\<^sub>r"} be the rectification
Chengsong
parents:
diff changeset
  1895
  function, that is @{term "snd (simp (der c r))"}.  We distinguish the cases
Chengsong
parents:
diff changeset
  1896
  whether (*) @{term "s \<in> L (der c r)"} or not. In the first case we
Chengsong
parents:
diff changeset
  1897
  have by Theorem~\ref{lexercorrect}(2) a value @{term "v"} so that @{term
Chengsong
parents:
diff changeset
  1898
  "lexer (der c r) s = Some v"} and @{term "s \<in> der c r \<rightarrow> v"} hold.
Chengsong
parents:
diff changeset
  1899
  By Lemma~\ref{slexeraux}(1) we can also infer from~(*) that @{term "s
Chengsong
parents:
diff changeset
  1900
  \<in> L r\<^sub>s"} holds.  Hence we know by Theorem~\ref{lexercorrect}(2) that
Chengsong
parents:
diff changeset
  1901
  there exists a @{term "v'"} with @{term "lexer r\<^sub>s s = Some v'"} and
Chengsong
parents:
diff changeset
  1902
  @{term "s \<in> r\<^sub>s \<rightarrow> v'"}. From the latter we know by
Chengsong
parents:
diff changeset
  1903
  Lemma~\ref{slexeraux}(2) that @{term "s \<in> der c r \<rightarrow> (f\<^sub>r v')"} holds.
Chengsong
parents:
diff changeset
  1904
  By the uniqueness of the POSIX relation (Theorem~\ref{posixdeterm}) we
Chengsong
parents:
diff changeset
  1905
  can infer that @{term v} is equal to @{term "f\<^sub>r v'"}---that is the 
Chengsong
parents:
diff changeset
  1906
  rectification function applied to @{term "v'"}
Chengsong
parents:
diff changeset
  1907
  produces the original @{term "v"}.  Now the case follows by the
Chengsong
parents:
diff changeset
  1908
  definitions of @{const lexer} and @{const slexer}.
Chengsong
parents:
diff changeset
  1909
Chengsong
parents:
diff changeset
  1910
  In the second case where @{term "s \<notin> L (der c r)"} we have that
Chengsong
parents:
diff changeset
  1911
  @{term "lexer (der c r) s = None"} by Theorem~\ref{lexercorrect}(1).  We
Chengsong
parents:
diff changeset
  1912
  also know by Lemma~\ref{slexeraux}(1) that @{term "s \<notin> L r\<^sub>s"}. Hence
Chengsong
parents:
diff changeset
  1913
  @{term "lexer r\<^sub>s s = None"} by Theorem~\ref{lexercorrect}(1) and
Chengsong
parents:
diff changeset
  1914
  by IH then also @{term "slexer r\<^sub>s s = None"}. With this we can
Chengsong
parents:
diff changeset
  1915
  conclude in this case too.\qed   
Chengsong
parents:
diff changeset
  1916
Chengsong
parents:
diff changeset
  1917
  \end{proof} 
Chengsong
parents:
diff changeset
  1918
Chengsong
parents:
diff changeset
  1919
\<close>
Chengsong
parents:
diff changeset
  1920
fy the result. This gives us a simplified derivative
Chengsong
parents:
diff changeset
  1921
  \<open>r\<^sub>s\<close> and a rectification function \<open>f\<^sub>r\<close>. The lexer
Chengsong
parents:
diff changeset
  1922
  is then recursively called with the simplified derivative, but before
Chengsong
parents:
diff changeset
  1923
  we inject the character @{term c} into the value @{term v}, we need to rectify
Chengsong
parents:
diff changeset
  1924
  @{term v} (that is construct @{term "f\<^sub>r v"}). Before we can establish the correctness
Chengsong
parents:
diff changeset
  1925
  of @{term "slexer"}, we need to show that simplification preserves the language
Chengsong
parents:
diff changeset
  1926
  and simplification preserves our POSIX relation once the value is rectified
Chengsong
parents:
diff changeset
  1927
  (recall @{const "simp"} generates a (regular expression, rectification function) pair):
Chengsong
parents:
diff changeset
  1928
Chengsong
parents:
diff changeset
  1929
  \begin{lemma}\mbox{}\smallskip\\\label{slexeraux}
Chengsong
parents:
diff changeset
  1930
  \begin{tabular}{ll}
Chengsong
parents:
diff changeset
  1931
  (1) & @{thm L_fst_simp[symmetric]}\\
Chengsong
parents:
diff changeset
  1932
  (2) & @{thm[mode=IfThen] Posix_simp}
Chengsong
parents:
diff changeset
  1933
  \end{tabular}
Chengsong
parents:
diff changeset
  1934
  \end{lemma}
Chengsong
parents:
diff changeset
  1935
Chengsong
parents:
diff changeset
  1936
  \begin{proof} Both are by induction on \<open>r\<close>. There is no
Chengsong
parents:
diff changeset
  1937
  interesting case for the first statement. For the second statement,
Chengsong
parents:
diff changeset
  1938
  of interest are the @{term "r = ALT r\<^sub>1 r\<^sub>2"} and @{term "r = SEQ r\<^sub>1
Chengsong
parents:
diff changeset
  1939
  r\<^sub>2"} cases. In each case we have to analyse four subcases whether
Chengsong
parents:
diff changeset
  1940
  @{term "fst (simp r\<^sub>1)"} and @{term "fst (simp r\<^sub>2)"} equals @{const
Chengsong
parents:
diff changeset
  1941
  ZERO} (respectively @{const ONE}). For example for @{term "r = ALT
Chengsong
parents:
diff changeset
  1942
  r\<^sub>1 r\<^sub>2"}, consider the subcase @{term "fst (simp r\<^sub>1) = ZERO"} and
Chengsong
parents:
diff changeset
  1943
  @{term "fst (simp r\<^sub>2) \<noteq> ZERO"}. By assumption we know @{term "s \<in>
Chengsong
parents:
diff changeset
  1944
  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"}
Chengsong
parents:
diff changeset
  1945
  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"}
Chengsong
parents:
diff changeset
  1946
  we know @{term "L (fst (simp r\<^sub>1)) = {}"}. By the first statement
Chengsong
parents:
diff changeset
  1947
  @{term "L r\<^sub>1"} is the empty set, meaning (**) @{term "s \<notin> L r\<^sub>1"}.
Chengsong
parents:
diff changeset
  1948
  Taking (*) and (**) together gives by the \mbox{\<open>P+R\<close>}-rule 
Chengsong
parents:
diff changeset
  1949
  @{term "s \<in> ALT r\<^sub>1 r\<^sub>2 \<rightarrow> Right (snd (simp r\<^sub>2) v)"}. In turn this
Chengsong
parents:
diff changeset
  1950
  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.
Chengsong
parents:
diff changeset
  1951
  The other cases are similar.\qed
Chengsong
parents:
diff changeset
  1952
  \end{proof}
Chengsong
parents:
diff changeset
  1953
Chengsong
parents:
diff changeset
  1954
  \noindent We can now prove relatively straightforwardly that the
Chengsong
parents:
diff changeset
  1955
  optimised lexer produces the expected result:
Chengsong
parents:
diff changeset
  1956
Chengsong
parents:
diff changeset
  1957
  \begin{theorem}
Chengsong
parents:
diff changeset
  1958
  @{thm slexer_correctness}
Chengsong
parents:
diff changeset
  1959
  \end{theorem}
Chengsong
parents:
diff changeset
  1960
Chengsong
parents:
diff changeset
  1961
  \begin{proof} By induction on @{term s} generalising over @{term
Chengsong
parents:
diff changeset
  1962
  r}. The case @{term "[]"} is trivial. For the cons-case suppose the
Chengsong
parents:
diff changeset
  1963
  string is of the form @{term "c # s"}. By induction hypothesis we
Chengsong
parents:
diff changeset
  1964
  know @{term "slexer r s = lexer r s"} holds for all @{term r} (in
Chengsong
parents:
diff changeset
  1965
  particular for @{term "r"} being the derivative @{term "der c
Chengsong
parents:
diff changeset
  1966
  r"}). Let @{term "r\<^sub>s"} be the simplified derivative regular expression, that is @{term
Chengsong
parents:
diff changeset
  1967
  "fst (simp (der c r))"}, and @{term "f\<^sub>r"} be the rectification
Chengsong
parents:
diff changeset
  1968
  function, that is @{term "snd (simp (der c r))"}.  We distinguish the cases
Chengsong
parents:
diff changeset
  1969
  whether (*) @{term "s \<in> L (der c r)"} or not. In the first case we
Chengsong
parents:
diff changeset
  1970
  have by Theorem~\ref{lexercorrect}(2) a value @{term "v"} so that @{term
Chengsong
parents:
diff changeset
  1971
  "lexer (der c r) s = Some v"} and @{term "s \<in> der c r \<rightarrow> v"} hold.
Chengsong
parents:
diff changeset
  1972
  By Lemma~\ref{slexeraux}(1) we can also infer from~(*) that @{term "s
Chengsong
parents:
diff changeset
  1973
  \<in> L r\<^sub>s"} holds.  Hence we know by Theorem~\ref{lexercorrect}(2) that
Chengsong
parents:
diff changeset
  1974
  there exists a @{term "v'"} with @{term "lexer r\<^sub>s s = Some v'"} and
Chengsong
parents:
diff changeset
  1975
  @{term "s \<in> r\<^sub>s \<rightarrow> v'"}. From the latter we know by
Chengsong
parents:
diff changeset
  1976
  Lemma~\ref{slexeraux}(2) that @{term "s \<in> der c r \<rightarrow> (f\<^sub>r v')"} holds.
Chengsong
parents:
diff changeset
  1977
  By the uniqueness of the POSIX relation (Theorem~\ref{posixdeterm}) we
Chengsong
parents:
diff changeset
  1978
  can infer that @{term v} is equal to @{term "f\<^sub>r v'"}---that is the 
Chengsong
parents:
diff changeset
  1979
  rectification function applied to @{term "v'"}
Chengsong
parents:
diff changeset
  1980
  produces the original @{term "v"}.  Now the case follows by the
Chengsong
parents:
diff changeset
  1981
  definitions of @{const lexer} and @{const slexer}.
Chengsong
parents:
diff changeset
  1982
Chengsong
parents:
diff changeset
  1983
  In the second case where @{term "s \<notin> L (der c r)"} we have that
Chengsong
parents:
diff changeset
  1984
  @{term "lexer (der c r) s = None"} by Theorem~\ref{lexercorrect}(1).  We
Chengsong
parents:
diff changeset
  1985
  also know by Lemma~\ref{slexeraux}(1) that @{term "s \<notin> L r\<^sub>s"}. Hence
Chengsong
parents:
diff changeset
  1986
  @{term "lexer r\<^sub>s s = None"} by Theorem~\ref{lexercorrect}(1) and
Chengsong
parents:
diff changeset
  1987
  by IH then also @{term "slexer r\<^sub>s s = None"}. With this we can
Chengsong
parents:
diff changeset
  1988
  conclude in this case too.\qed   
Chengsong
parents:
diff changeset
  1989
Chengsong
parents:
diff changeset
  1990
  \end{proof} 
Chengsong
parents:
diff changeset
  1991
Chengsong
parents:
diff changeset
  1992
\<close>
Chengsong
parents:
diff changeset
  1993
Chengsong
parents:
diff changeset
  1994
Chengsong
parents:
diff changeset
  1995
section \<open>HERE\<close>
Chengsong
parents:
diff changeset
  1996
Chengsong
parents:
diff changeset
  1997
text \<open>
Chengsong
parents:
diff changeset
  1998
Chengsong
parents:
diff changeset
  1999
  \begin{lemma}
Chengsong
parents:
diff changeset
  2000
  @{thm [mode=IfThen] bder_retrieve}
Chengsong
parents:
diff changeset
  2001
  \end{lemma}
Chengsong
parents:
diff changeset
  2002
Chengsong
parents:
diff changeset
  2003
  \begin{proof}
Chengsong
parents:
diff changeset
  2004
  By induction on the definition of @{term "erase r"}. The cases for rule 1) and 2) are
Chengsong
parents:
diff changeset
  2005
  straightforward as @{term "der c ZERO"} and @{term "der c ONE"} are both equal to 
Chengsong
parents:
diff changeset
  2006
  @{term ZERO}. This means @{term "\<Turnstile> v : ZERO"} cannot hold. Similarly in case of rule 3)
Chengsong
parents:
diff changeset
  2007
  where @{term r} is of the form @{term "ACHAR d"} with @{term "c = d"}. Then by assumption
Chengsong
parents:
diff changeset
  2008
  we know @{term "\<Turnstile> v : ONE"}, which implies @{term "v = Void"}. The equation follows by 
Chengsong
parents:
diff changeset
  2009
  simplification of left- and right-hand side. In  case @{term "c \<noteq> d"} we have again
Chengsong
parents:
diff changeset
  2010
  @{term "\<Turnstile> v : ZERO"}, which cannot  hold. 
Chengsong
parents:
diff changeset
  2011
Chengsong
parents:
diff changeset
  2012
  For rule 4a) we have again @{term "\<Turnstile> v : ZERO"}. The property holds by IH for rule 4b).
Chengsong
parents:
diff changeset
  2013
  The  induction hypothesis is 
Chengsong
parents:
diff changeset
  2014
  \[
Chengsong
parents:
diff changeset
  2015
  @{term "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"}
Chengsong
parents:
diff changeset
  2016
  \]
Chengsong
parents:
diff changeset
  2017
  which is what left- and right-hand side simplify to.  The slightly more interesting case
Chengsong
parents:
diff changeset
  2018
  is for 4c). By assumption  we have 
Chengsong
parents:
diff changeset
  2019
  @{term "\<Turnstile> v : ALT (der c (erase r\<^sub>1)) (der c (erase (AALTs bs (r\<^sub>2 # rs))))"}. This means we 
Chengsong
parents:
diff changeset
  2020
  have either (*) @{term "\<Turnstile> v1 : der c (erase r\<^sub>1)"} with @{term "v = Left v1"} or
Chengsong
parents:
diff changeset
  2021
  (**) @{term "\<Turnstile> v2 : der c (erase (AALTs bs (r\<^sub>2 # rs)))"} with @{term "v = Right v2"}.
Chengsong
parents:
diff changeset
  2022
  The former  case is straightforward by simplification. The second case is \ldots TBD.
Chengsong
parents:
diff changeset
  2023
Chengsong
parents:
diff changeset
  2024
  Rule 5) TBD.
Chengsong
parents:
diff changeset
  2025
Chengsong
parents:
diff changeset
  2026
  Finally for rule 6) the reasoning is as follows:   By assumption we  have
Chengsong
parents:
diff changeset
  2027
  @{term "\<Turnstile> v : SEQ (der c (erase r)) (STAR (erase r))"}. This means we also have
Chengsong
parents:
diff changeset
  2028
  @{term "v = Seq v1 v2"}, @{term "\<Turnstile> v1 : der c (erase r)"}  and @{term "v2 = Stars vs"}.
Chengsong
parents:
diff changeset
  2029
  We want to prove
Chengsong
parents:
diff changeset
  2030
  \begin{align}
Chengsong
parents:
diff changeset
  2031
  & @{term "retrieve (ASEQ bs (fuse [Z] (bder c r)) (ASTAR [] r)) v"}\\
Chengsong
parents:
diff changeset
  2032
  &= @{term "retrieve (ASTAR bs r) (injval (STAR (erase r)) c v)"}
Chengsong
parents:
diff changeset
  2033
  \end{align}
Chengsong
parents:
diff changeset
  2034
  The right-hand side @{term inj}-expression is equal to 
Chengsong
parents:
diff changeset
  2035
  @{term "Stars (injval (erase r) c v1 # vs)"}, which means the @{term  retrieve}-expression
Chengsong
parents:
diff changeset
  2036
  simplifies to 
Chengsong
parents:
diff changeset
  2037
  \[
Chengsong
parents:
diff changeset
  2038
  @{term "bs @ [Z] @ retrieve r (injval (erase r) c v1) @ retrieve (ASTAR [] r) (Stars vs)"}
Chengsong
parents:
diff changeset
  2039
  \]
Chengsong
parents:
diff changeset
  2040
  The left-hand side (3) above simplifies to 
Chengsong
parents:
diff changeset
  2041
  \[
Chengsong
parents:
diff changeset
  2042
  @{term "bs @ retrieve (fuse [Z] (bder c r)) v1 @ retrieve (ASTAR [] r) (Stars vs)"} 
Chengsong
parents:
diff changeset
  2043
  \]
Chengsong
parents:
diff changeset
  2044
  We can move out the @{term "fuse  [Z]"} and then use the IH to show that left-hand side
Chengsong
parents:
diff changeset
  2045
  and right-hand side are equal. This completes the proof. 
Chengsong
parents:
diff changeset
  2046
  \end{proof}   
Chengsong
parents:
diff changeset
  2047
Chengsong
parents:
diff changeset
  2048
   
Chengsong
parents:
diff changeset
  2049
Chengsong
parents:
diff changeset
  2050
  \bibliographystyle{plain}
Chengsong
parents:
diff changeset
  2051
  \bibliography{root}
Chengsong
parents:
diff changeset
  2052
Chengsong
parents:
diff changeset
  2053
\<close>
Chengsong
parents:
diff changeset
  2054
(*<*)
Chengsong
parents:
diff changeset
  2055
end
Chengsong
parents:
diff changeset
  2056
(*>*)
Chengsong
parents:
diff changeset
  2057
Chengsong
parents:
diff changeset
  2058
(*
Chengsong
parents:
diff changeset
  2059
Chengsong
parents:
diff changeset
  2060
\begin{center}
Chengsong
parents:
diff changeset
  2061
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2062
  @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\
Chengsong
parents:
diff changeset
  2063
  @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\
Chengsong
parents:
diff changeset
  2064
  @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\
Chengsong
parents:
diff changeset
  2065
  @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}\\
Chengsong
parents:
diff changeset
  2066
  @{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"]}\\
Chengsong
parents:
diff changeset
  2067
  @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\
Chengsong
parents:
diff changeset
  2068
  @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)}
Chengsong
parents:
diff changeset
  2069
\end{tabular}
Chengsong
parents:
diff changeset
  2070
\end{center}
Chengsong
parents:
diff changeset
  2071
Chengsong
parents:
diff changeset
  2072
\begin{center}
Chengsong
parents:
diff changeset
  2073
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2074
  @{term areg} & $::=$ & @{term "AZERO"}\\
Chengsong
parents:
diff changeset
  2075
               & $\mid$ & @{term "AONE bs"}\\
Chengsong
parents:
diff changeset
  2076
               & $\mid$ & @{term "ACHAR bs c"}\\
Chengsong
parents:
diff changeset
  2077
               & $\mid$ & @{term "AALT bs r1 r2"}\\
Chengsong
parents:
diff changeset
  2078
               & $\mid$ & @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}\\
Chengsong
parents:
diff changeset
  2079
               & $\mid$ & @{term "ASTAR bs r"}
Chengsong
parents:
diff changeset
  2080
\end{tabular}
Chengsong
parents:
diff changeset
  2081
\end{center}
Chengsong
parents:
diff changeset
  2082
Chengsong
parents:
diff changeset
  2083
Chengsong
parents:
diff changeset
  2084
\begin{center}
Chengsong
parents:
diff changeset
  2085
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2086
  @{thm (lhs) intern.simps(1)} & $\dn$ & @{thm (rhs) intern.simps(1)}\\
Chengsong
parents:
diff changeset
  2087
  @{thm (lhs) intern.simps(2)} & $\dn$ & @{thm (rhs) intern.simps(2)}\\
Chengsong
parents:
diff changeset
  2088
  @{thm (lhs) intern.simps(3)} & $\dn$ & @{thm (rhs) intern.simps(3)}\\
Chengsong
parents:
diff changeset
  2089
  @{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"]}\\
Chengsong
parents:
diff changeset
  2090
  @{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"]}\\
Chengsong
parents:
diff changeset
  2091
  @{thm (lhs) intern.simps(6)} & $\dn$ & @{thm (rhs) intern.simps(6)}\\
Chengsong
parents:
diff changeset
  2092
\end{tabular}
Chengsong
parents:
diff changeset
  2093
\end{center}
Chengsong
parents:
diff changeset
  2094
Chengsong
parents:
diff changeset
  2095
\begin{center}
Chengsong
parents:
diff changeset
  2096
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2097
  @{thm (lhs) erase.simps(1)} & $\dn$ & @{thm (rhs) erase.simps(1)}\\
Chengsong
parents:
diff changeset
  2098
  @{thm (lhs) erase.simps(2)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(2)[of bs]}\\
Chengsong
parents:
diff changeset
  2099
  @{thm (lhs) erase.simps(3)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(3)[of bs]}\\
Chengsong
parents:
diff changeset
  2100
  @{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"]}\\
Chengsong
parents:
diff changeset
  2101
  @{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"]}\\
Chengsong
parents:
diff changeset
  2102
  @{thm (lhs) erase.simps(6)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(6)[of bs]}\\
Chengsong
parents:
diff changeset
  2103
\end{tabular}
Chengsong
parents:
diff changeset
  2104
\end{center}
Chengsong
parents:
diff changeset
  2105
Chengsong
parents:
diff changeset
  2106
Some simple facts about erase
Chengsong
parents:
diff changeset
  2107
Chengsong
parents:
diff changeset
  2108
\begin{lemma}\mbox{}\\
Chengsong
parents:
diff changeset
  2109
@{thm erase_bder}\\
Chengsong
parents:
diff changeset
  2110
@{thm erase_intern}
Chengsong
parents:
diff changeset
  2111
\end{lemma}
Chengsong
parents:
diff changeset
  2112
Chengsong
parents:
diff changeset
  2113
\begin{center}
Chengsong
parents:
diff changeset
  2114
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2115
  @{thm (lhs) bnullable.simps(1)} & $\dn$ & @{thm (rhs) bnullable.simps(1)}\\
Chengsong
parents:
diff changeset
  2116
  @{thm (lhs) bnullable.simps(2)} & $\dn$ & @{thm (rhs) bnullable.simps(2)}\\
Chengsong
parents:
diff changeset
  2117
  @{thm (lhs) bnullable.simps(3)} & $\dn$ & @{thm (rhs) bnullable.simps(3)}\\
Chengsong
parents:
diff changeset
  2118
  @{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"]}\\
Chengsong
parents:
diff changeset
  2119
  @{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"]}\\
Chengsong
parents:
diff changeset
  2120
  @{thm (lhs) bnullable.simps(6)} & $\dn$ & @{thm (rhs) bnullable.simps(6)}\medskip\\
Chengsong
parents:
diff changeset
  2121
Chengsong
parents:
diff changeset
  2122
%  \end{tabular}
Chengsong
parents:
diff changeset
  2123
%  \end{center}
Chengsong
parents:
diff changeset
  2124
Chengsong
parents:
diff changeset
  2125
%  \begin{center}
Chengsong
parents:
diff changeset
  2126
%  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2127
Chengsong
parents:
diff changeset
  2128
  @{thm (lhs) bder.simps(1)} & $\dn$ & @{thm (rhs) bder.simps(1)}\\
Chengsong
parents:
diff changeset
  2129
  @{thm (lhs) bder.simps(2)} & $\dn$ & @{thm (rhs) bder.simps(2)}\\
Chengsong
parents:
diff changeset
  2130
  @{thm (lhs) bder.simps(3)} & $\dn$ & @{thm (rhs) bder.simps(3)}\\
Chengsong
parents:
diff changeset
  2131
  @{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"]}\\
Chengsong
parents:
diff changeset
  2132
  @{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"]}\\
Chengsong
parents:
diff changeset
  2133
  @{thm (lhs) bder.simps(6)} & $\dn$ & @{thm (rhs) bder.simps(6)}
Chengsong
parents:
diff changeset
  2134
  \end{tabular}
Chengsong
parents:
diff changeset
  2135
  \end{center}
Chengsong
parents:
diff changeset
  2136
Chengsong
parents:
diff changeset
  2137
Chengsong
parents:
diff changeset
  2138
\begin{center}
Chengsong
parents:
diff changeset
  2139
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2140
  @{thm (lhs) bmkeps.simps(1)} & $\dn$ & @{thm (rhs) bmkeps.simps(1)}\\
Chengsong
parents:
diff changeset
  2141
  @{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"]}\\
Chengsong
parents:
diff changeset
  2142
  @{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"]}\\
Chengsong
parents:
diff changeset
  2143
  @{thm (lhs) bmkeps.simps(4)} & $\dn$ & @{thm (rhs) bmkeps.simps(4)}\medskip\\
Chengsong
parents:
diff changeset
  2144
\end{tabular}
Chengsong
parents:
diff changeset
  2145
\end{center}
Chengsong
parents:
diff changeset
  2146
Chengsong
parents:
diff changeset
  2147
Chengsong
parents:
diff changeset
  2148
@{thm [mode=IfThen] bder_retrieve}
Chengsong
parents:
diff changeset
  2149
Chengsong
parents:
diff changeset
  2150
By induction on \<open>r\<close>
Chengsong
parents:
diff changeset
  2151
Chengsong
parents:
diff changeset
  2152
\begin{theorem}[Main Lemma]\mbox{}\\
Chengsong
parents:
diff changeset
  2153
@{thm [mode=IfThen] MAIN_decode}
Chengsong
parents:
diff changeset
  2154
\end{theorem}
Chengsong
parents:
diff changeset
  2155
Chengsong
parents:
diff changeset
  2156
\noindent
Chengsong
parents:
diff changeset
  2157
Definition of the bitcoded lexer
Chengsong
parents:
diff changeset
  2158
Chengsong
parents:
diff changeset
  2159
@{thm blexer_def}
Chengsong
parents:
diff changeset
  2160
Chengsong
parents:
diff changeset
  2161
Chengsong
parents:
diff changeset
  2162
\begin{theorem}
Chengsong
parents:
diff changeset
  2163
@{thm blexer_correctness}
Chengsong
parents:
diff changeset
  2164
\end{theorem}
Chengsong
parents:
diff changeset
  2165
Chengsong
parents:
diff changeset
  2166
Chengsong
parents:
diff changeset
  2167
Chengsong
parents:
diff changeset
  2168
Chengsong
parents:
diff changeset
  2169
Chengsong
parents:
diff changeset
  2170
\begin{center}
Chengsong
parents:
diff changeset
  2171
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2172
  @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\
Chengsong
parents:
diff changeset
  2173
  @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\
Chengsong
parents:
diff changeset
  2174
  @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\
Chengsong
parents:
diff changeset
  2175
  @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}\\
Chengsong
parents:
diff changeset
  2176
  @{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"]}\\
Chengsong
parents:
diff changeset
  2177
  @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\
Chengsong
parents:
diff changeset
  2178
  @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)}
Chengsong
parents:
diff changeset
  2179
\end{tabular}
Chengsong
parents:
diff changeset
  2180
\end{center}
Chengsong
parents:
diff changeset
  2181
Chengsong
parents:
diff changeset
  2182
\begin{center}
Chengsong
parents:
diff changeset
  2183
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2184
  @{term areg} & $::=$ & @{term "AZERO"}\\
Chengsong
parents:
diff changeset
  2185
               & $\mid$ & @{term "AONE bs"}\\
Chengsong
parents:
diff changeset
  2186
               & $\mid$ & @{term "ACHAR bs c"}\\
Chengsong
parents:
diff changeset
  2187
               & $\mid$ & @{term "AALT bs r1 r2"}\\
Chengsong
parents:
diff changeset
  2188
               & $\mid$ & @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}\\
Chengsong
parents:
diff changeset
  2189
               & $\mid$ & @{term "ASTAR bs r"}
Chengsong
parents:
diff changeset
  2190
\end{tabular}
Chengsong
parents:
diff changeset
  2191
\end{center}
Chengsong
parents:
diff changeset
  2192
Chengsong
parents:
diff changeset
  2193
Chengsong
parents:
diff changeset
  2194
\begin{center}
Chengsong
parents:
diff changeset
  2195
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2196
  @{thm (lhs) intern.simps(1)} & $\dn$ & @{thm (rhs) intern.simps(1)}\\
Chengsong
parents:
diff changeset
  2197
  @{thm (lhs) intern.simps(2)} & $\dn$ & @{thm (rhs) intern.simps(2)}\\
Chengsong
parents:
diff changeset
  2198
  @{thm (lhs) intern.simps(3)} & $\dn$ & @{thm (rhs) intern.simps(3)}\\
Chengsong
parents:
diff changeset
  2199
  @{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"]}\\
Chengsong
parents:
diff changeset
  2200
  @{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"]}\\
Chengsong
parents:
diff changeset
  2201
  @{thm (lhs) intern.simps(6)} & $\dn$ & @{thm (rhs) intern.simps(6)}\\
Chengsong
parents:
diff changeset
  2202
\end{tabular}
Chengsong
parents:
diff changeset
  2203
\end{center}
Chengsong
parents:
diff changeset
  2204
Chengsong
parents:
diff changeset
  2205
\begin{center}
Chengsong
parents:
diff changeset
  2206
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2207
  @{thm (lhs) erase.simps(1)} & $\dn$ & @{thm (rhs) erase.simps(1)}\\
Chengsong
parents:
diff changeset
  2208
  @{thm (lhs) erase.simps(2)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(2)[of bs]}\\
Chengsong
parents:
diff changeset
  2209
  @{thm (lhs) erase.simps(3)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(3)[of bs]}\\
Chengsong
parents:
diff changeset
  2210
  @{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"]}\\
Chengsong
parents:
diff changeset
  2211
  @{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"]}\\
Chengsong
parents:
diff changeset
  2212
  @{thm (lhs) erase.simps(6)[of bs]} & $\dn$ & @{thm (rhs) erase.simps(6)[of bs]}\\
Chengsong
parents:
diff changeset
  2213
\end{tabular}
Chengsong
parents:
diff changeset
  2214
\end{center}
Chengsong
parents:
diff changeset
  2215
Chengsong
parents:
diff changeset
  2216
Some simple facts about erase
Chengsong
parents:
diff changeset
  2217
Chengsong
parents:
diff changeset
  2218
\begin{lemma}\mbox{}\\
Chengsong
parents:
diff changeset
  2219
@{thm erase_bder}\\
Chengsong
parents:
diff changeset
  2220
@{thm erase_intern}
Chengsong
parents:
diff changeset
  2221
\end{lemma}
Chengsong
parents:
diff changeset
  2222
Chengsong
parents:
diff changeset
  2223
\begin{center}
Chengsong
parents:
diff changeset
  2224
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2225
  @{thm (lhs) bnullable.simps(1)} & $\dn$ & @{thm (rhs) bnullable.simps(1)}\\
Chengsong
parents:
diff changeset
  2226
  @{thm (lhs) bnullable.simps(2)} & $\dn$ & @{thm (rhs) bnullable.simps(2)}\\
Chengsong
parents:
diff changeset
  2227
  @{thm (lhs) bnullable.simps(3)} & $\dn$ & @{thm (rhs) bnullable.simps(3)}\\
Chengsong
parents:
diff changeset
  2228
  @{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"]}\\
Chengsong
parents:
diff changeset
  2229
  @{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"]}\\
Chengsong
parents:
diff changeset
  2230
  @{thm (lhs) bnullable.simps(6)} & $\dn$ & @{thm (rhs) bnullable.simps(6)}\medskip\\
Chengsong
parents:
diff changeset
  2231
Chengsong
parents:
diff changeset
  2232
%  \end{tabular}
Chengsong
parents:
diff changeset
  2233
%  \end{center}
Chengsong
parents:
diff changeset
  2234
Chengsong
parents:
diff changeset
  2235
%  \begin{center}
Chengsong
parents:
diff changeset
  2236
%  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2237
Chengsong
parents:
diff changeset
  2238
  @{thm (lhs) bder.simps(1)} & $\dn$ & @{thm (rhs) bder.simps(1)}\\
Chengsong
parents:
diff changeset
  2239
  @{thm (lhs) bder.simps(2)} & $\dn$ & @{thm (rhs) bder.simps(2)}\\
Chengsong
parents:
diff changeset
  2240
  @{thm (lhs) bder.simps(3)} & $\dn$ & @{thm (rhs) bder.simps(3)}\\
Chengsong
parents:
diff changeset
  2241
  @{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"]}\\
Chengsong
parents:
diff changeset
  2242
  @{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"]}\\
Chengsong
parents:
diff changeset
  2243
  @{thm (lhs) bder.simps(6)} & $\dn$ & @{thm (rhs) bder.simps(6)}
Chengsong
parents:
diff changeset
  2244
  \end{tabular}
Chengsong
parents:
diff changeset
  2245
  \end{center}
Chengsong
parents:
diff changeset
  2246
Chengsong
parents:
diff changeset
  2247
Chengsong
parents:
diff changeset
  2248
\begin{center}
Chengsong
parents:
diff changeset
  2249
  \begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  2250
  @{thm (lhs) bmkeps.simps(1)} & $\dn$ & @{thm (rhs) bmkeps.simps(1)}\\
Chengsong
parents:
diff changeset
  2251
  @{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"]}\\
Chengsong
parents:
diff changeset
  2252
  @{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"]}\\
Chengsong
parents:
diff changeset
  2253
  @{thm (lhs) bmkeps.simps(4)} & $\dn$ & @{thm (rhs) bmkeps.simps(4)}\medskip\\
Chengsong
parents:
diff changeset
  2254
\end{tabular}
Chengsong
parents:
diff changeset
  2255
\end{center}
Chengsong
parents:
diff changeset
  2256
Chengsong
parents:
diff changeset
  2257
Chengsong
parents:
diff changeset
  2258
@{thm [mode=IfThen] bder_retrieve}
Chengsong
parents:
diff changeset
  2259
Chengsong
parents:
diff changeset
  2260
By induction on \<open>r\<close>
Chengsong
parents:
diff changeset
  2261
Chengsong
parents:
diff changeset
  2262
\begin{theorem}[Main Lemma]\mbox{}\\
Chengsong
parents:
diff changeset
  2263
@{thm [mode=IfThen] MAIN_decode}
Chengsong
parents:
diff changeset
  2264
\end{theorem}
Chengsong
parents:
diff changeset
  2265
Chengsong
parents:
diff changeset
  2266
\noindent
Chengsong
parents:
diff changeset
  2267
Definition of the bitcoded lexer
Chengsong
parents:
diff changeset
  2268
Chengsong
parents:
diff changeset
  2269
@{thm blexer_def}
Chengsong
parents:
diff changeset
  2270
Chengsong
parents:
diff changeset
  2271
Chengsong
parents:
diff changeset
  2272
\begin{theorem}
Chengsong
parents:
diff changeset
  2273
@{thm blexer_correctness}
Chengsong
parents:
diff changeset
  2274
\end{theorem}
Chengsong
parents:
diff changeset
  2275
Chengsong
parents:
diff changeset
  2276
\<close>
Chengsong
parents:
diff changeset
  2277
\<close>*)