thys3/Paper.thy
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 01 May 2022 11:50:46 +0100
changeset 498 ab626b60ee64
parent 496 f493a20feeb3
child 499 6a100d32314c
permissions -rw-r--r--
fixed tiny typo in the paper
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
496
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
(*<*)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
theory Paper
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
  imports
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
   "Posix.FBound" 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
   "HOL-Library.LaTeXsugar"
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
begin
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
declare [[show_question_marks = false]]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
notation (latex output)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  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
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  Cons ("_\<^latex>\<open>\\mbox{$\\,$}\<close>::\<^latex>\<open>\\mbox{$\\,$}\<close>_" [75,73] 73) 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
abbreviation 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  "der_syn r c \<equiv> der c r"
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
abbreviation 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
 "ders_syn r s \<equiv> ders s r"  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
abbreviation 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
  "bder_syn r c \<equiv> bder c r"  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
notation (latex output)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  der_syn ("_\\_" [79, 1000] 76) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  ders_syn ("_\\_" [79, 1000] 76) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  bder_syn ("_\\_" [79, 1000] 76) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  bders ("_\\_" [79, 1000] 76) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  bders_simp ("_\\\<^sub>b\<^sub>s\<^sub>i\<^sub>m\<^sub>p _" [79, 1000] 76) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  ZERO ("\<^bold>0" 81) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  ONE ("\<^bold>1" 81) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  CH ("_" [1000] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  ALT ("_ + _" [77,77] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  SEQ ("_ \<cdot> _" [77,77] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  STAR ("_\<^sup>*" [79] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  val.Void ("Empty" 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
  val.Char ("Char _" [1000] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  val.Left ("Left _" [79] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  val.Right ("Right _" [1000] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  val.Seq ("Seq _ _" [79,79] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  val.Stars ("Stars _" [79] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  Prf ("\<turnstile> _ : _" [75,75] 75) and  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  Posix ("'(_, _') \<rightarrow> _" [63,75,75] 75) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  flat ("|_|" [75] 74) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  flats ("|_|" [72] 74) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  injval ("inj _ _ _" [79,77,79] 76) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  mkeps ("mkeps _" [79] 76) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  length ("len _" [73] 73) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
  set ("_" [73] 73) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
  AZERO ("ZERO" 81) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  AONE ("ONE _" [79] 78) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  ACHAR ("CHAR _ _" [79, 79] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  AALTs ("ALTs _ _" [77,77] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
  ASEQ ("SEQ _ _ _" [79, 79,79] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  ASTAR ("STAR _ _" [79, 79] 78) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  code ("code _" [79] 74) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  intern ("_\<^latex>\<open>\\mbox{$^\\uparrow$}\<close>" [900] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  erase ("_\<^latex>\<open>\\mbox{$^\\downarrow$}\<close>" [1000] 74) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
  bnullable ("bnullable _" [1000] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  bsimp_AALTs ("bsimpALT _ _" [10,1000] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  bsimp_ASEQ ("bsimpSEQ _ _ _" [10,1000,1000] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  bmkeps ("bmkeps _" [1000] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  eq1 ("_ \<approx> _" [77,77] 76) and 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  srewrite ("_\<^latex>\<open>\\mbox{$\\,\\stackrel{s}{\\leadsto}$}\<close> _" [71, 71] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  rrewrites ("_ \<^latex>\<open>\\mbox{$\\,\\leadsto^*$}\<close> _" [71, 71] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  srewrites ("_ \<^latex>\<open>\\mbox{$\\,\\stackrel{s}{\\leadsto}^*$}\<close> _" [71, 71] 80) and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  blexer_simp ("blexer\<^sup>+" 1000) 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
lemma better_retrieve:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
   shows "rs \<noteq> Nil ==> retrieve (AALTs bs (r#rs)) (Left v) = bs @ retrieve r v"
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
   and   "rs \<noteq> Nil ==> retrieve (AALTs bs (r#rs)) (Right v) = bs @ retrieve (AALTs [] rs) v"
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  apply (metis list.exhaust retrieve.simps(4))
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  by (metis list.exhaust retrieve.simps(5))
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
(*>*)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
section {* Introduction *}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
text {*
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
In the last fifteen or so years, Brzozowski's derivatives of regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
expressions have sparked quite a bit of interest in the functional
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
programming and theorem prover communities.  The beauty of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
Brzozowski's derivatives \cite{Brzozowski1964} is that they are neatly
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
expressible in any functional language, and easily definable and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
reasoned about in theorem provers---the definitions just consist of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
inductive datatypes and simple recursive functions.  Derivatives of a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
regular expression, written @{term "der c r"}, give a simple solution
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
to the problem of matching a string @{term s} with a regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
expression @{term r}: if the derivative of @{term r} w.r.t.\ (in
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
succession) all the characters of the string matches the empty string,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
then @{term r} matches @{term s} (and {\em vice versa}).  We are aware
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
of a mechanised correctness proof of Brzozowski's derivative-based matcher in HOL4 by
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
Owens and Slind~\cite{Owens2008}. Another one in Isabelle/HOL is part
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
of the work by Krauss and Nipkow~\cite{Krauss2011}.  And another one
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
in Coq is given by Coquand and Siles \cite{Coquand2012}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
Also Ribeiro and Du Bois give one in Agda~\cite{RibeiroAgda2017}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
However, there are two difficulties with derivative-based matchers:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
First, Brzozowski's original matcher only generates a yes/no answer
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
for whether a regular expression matches a string or not.  This is too
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
little information in the context of lexing where separate tokens must
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
be identified and also classified (for example as keywords
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
or identifiers).  Sulzmann and Lu~\cite{Sulzmann2014} overcome this
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
difficulty by cleverly extending Brzozowski's matching
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
algorithm. Their extended version generates additional information on
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
\emph{how} a regular expression matches a string following the POSIX
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
rules for regular expression matching. They achieve this by adding a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
second ``phase'' to Brzozowski's algorithm involving an injection
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
function.  In our own earlier work we provided the formal
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
specification of what POSIX matching means and proved in Isabelle/HOL
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
the correctness
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
of Sulzmann and Lu's extended algorithm accordingly
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
\cite{AusafDyckhoffUrban2016}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
The second difficulty is that Brzozowski's derivatives can 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
grow to arbitrarily big sizes. For example if we start with the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
regular expression \mbox{@{text "(a + aa)\<^sup>*"}} and take
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
successive derivatives according to the character $a$, we end up with
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
a sequence of ever-growing derivatives like 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
\def\ll{\stackrel{\_\backslash{} a}{\longrightarrow}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
\begin{tabular}{rll}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
$(a + aa)^*$ & $\ll$ & $(\ONE + \ONE{}a) \cdot (a + aa)^*$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
& $\ll$ & $(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* \;+\; (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
& $\ll$ & $(\ZERO + \ZERO{}a + \ZERO) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^* \;+\; $\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
& & $\qquad(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
& $\ll$ & \ldots \hspace{15mm}(regular expressions of sizes 98, 169, 283, 468, 767, \ldots)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
\end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
\noindent where after around 35 steps we run out of memory on a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
typical computer (we shall define shortly the precise details of our
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
regular expressions and the derivative operation).  Clearly, the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
notation involving $\ZERO$s and $\ONE$s already suggests
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
simplification rules that can be applied to regular regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
expressions, for example $\ZERO{}\,r \Rightarrow \ZERO$, $\ONE{}\,r
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
\Rightarrow r$, $\ZERO{} + r \Rightarrow r$ and $r + r \Rightarrow
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
r$. While such simple-minded simplifications have been proved in our
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
earlier work to preserve the correctness of Sulzmann and Lu's
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
algorithm \cite{AusafDyckhoffUrban2016}, they unfortunately do
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
\emph{not} help with limiting the growth of the derivatives shown
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
above: the growth is slowed, but the derivatives can still grow rather
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
quickly beyond any finite bound.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
Sulzmann and Lu overcome this ``growth problem'' in a second algorithm
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
\cite{Sulzmann2014} where they introduce bitcoded
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
regular expressions. In this version, POSIX values are
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
represented as bitsequences and such sequences are incrementally generated
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
when derivatives are calculated. The compact representation
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
of bitsequences and regular expressions allows them to define a more
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
``aggressive'' simplification method that keeps the size of the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
derivatives finitely bounded no matter what the length of the string is.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
They make some informal claims about the correctness and linear behaviour
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
of this version, but do not provide any supporting proof arguments, not
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
even ``pencil-and-paper'' arguments. They write about their bitcoded
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
\emph{incremental parsing method} (that is the algorithm to be formalised
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
in this paper):
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
\begin{quote}\it
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
  ``Correctness Claim: We further claim that the incremental parsing
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  method [..] in combination with the simplification steps [..]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  yields POSIX parse trees. We have tested this claim
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  extensively [..] but yet
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
  have to work out all proof details.'' \cite[Page 14]{Sulzmann2014}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
\end{quote}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
\noindent{}\textbf{Contributions:} We have implemented in Isabelle/HOL
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
the derivative-based lexing algorithm of Sulzmann and Lu
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
\cite{Sulzmann2014} where regular expressions are annotated with
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
bitsequences. We define the crucial simplification function as a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
recursive function, without the need of a fix-point operation. One objective of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
the simplification function is to remove duplicates of regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
expressions.  For this Sulzmann and Lu use in their paper the standard
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
@{text nub} function from Haskell's list library, but this function
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
does not achieve the intended objective with bitcoded regular expressions.  The
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
reason is that in the bitcoded setting, each copy generally has a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
different bitcode annotation---so @{text nub} would never ``fire''.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
Inspired by Scala's library for lists, we shall instead use a @{text
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
distinctWith} function that finds duplicates under an ``erasing'' function
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
which deletes bitcodes before comparing regular expressions.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
We shall also introduce our own argument and definitions for
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
establishing the correctness of the bitcoded algorithm when 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
simplifications are included. Finally we
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
establish that the size of derivatives can be finitely bounded.\medskip%\footnote{ In this paper, we shall first briefly introduce the basic notions
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
%of regular expressions and describe the definition
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
%of POSIX lexing from our earlier work \cite{AusafDyckhoffUrban2016}. This serves
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
%as a reference point for what correctness means in our Isabelle/HOL proofs. We shall then prove
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
%the correctness for the bitcoded algorithm without simplification, and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
%after that extend the proof to include simplification.}\smallskip
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
%\mbox{}\\[-10mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
\noindent In this paper, we shall first briefly introduce the basic notions
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
of regular expressions and describe the definition
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
of POSIX lexing from our earlier work \cite{AusafDyckhoffUrban2016}. This serves
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
as a reference point for what correctness means in our Isabelle/HOL proofs. We shall then prove
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
the correctness for the bitcoded algorithm without simplification, and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
after that extend the proof to include simplification.\mbox{}\\[-6mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
*}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
section {* Background *}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
text {*
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
  In our Isabelle/HOL formalisation strings are lists of characters with
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
  the empty string being represented by the empty list, written $[]$,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
  and list-cons being written as $\_\!::\!\_\,$; string
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
  concatenation is $\_ \,@\, \_\,$. We often use the usual
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
  bracket notation for lists also for strings; for example a string
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  consisting of just a single character $c$ is written $[c]$.   
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  Our regular expressions are defined as usual as the following inductive
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
  datatype:\\[-4mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  @{text "r ::="} \;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
  @{const "ZERO"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
  @{const "ONE"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  @{term "CH c"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
  @{term "ALT r\<^sub>1 r\<^sub>2"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
  @{term "SEQ r\<^sub>1 r\<^sub>2"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
  @{term "STAR r"} 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
  \noindent where @{const ZERO} stands for the regular expression that does
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
  not match any string, @{const ONE} for the regular expression that matches
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
  only the empty string and @{term c} for matching a character literal.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
  The constructors $+$ and $\cdot$ represent alternatives and sequences, respectively.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
  We sometimes omit the $\cdot$ in a sequence regular expression for brevity. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
  The
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
  \emph{language} of a regular expression, written $L(r)$, is defined as usual
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
  and we omit giving the definition here (see for example \cite{AusafDyckhoffUrban2016}).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
  Central to Brzozowski's regular expression matcher are two functions
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
  called @{text nullable} and \emph{derivative}. The latter is written
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
  $r\backslash c$ for the derivative of the regular expression $r$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
  w.r.t.~the character $c$. Both functions are defined by recursion over
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
  regular expressions.  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
\begin{tabular}{cc}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
  \begin{tabular}{r@ {\hspace{2mm}}c@ {\hspace{2mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
  @{thm (lhs) der.simps(1)} & $\dn$ & @{thm (rhs) der.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
  @{thm (lhs) der.simps(2)} & $\dn$ & @{thm (rhs) der.simps(2)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
  @{thm (lhs) der.simps(3)} & $\dn$ & @{thm (rhs) der.simps(3)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
  @{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"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
  @{thm (lhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]} & $\dn$ & @{text "if"} @{term "nullable(r\<^sub>1)"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
  & & @{text "then"} @{term "ALT (SEQ (der c r\<^sub>1) r\<^sub>2) (der c r\<^sub>2)"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
  & & @{text "else"} @{term "SEQ (der c r\<^sub>1) r\<^sub>2"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
  % & & @{thm (rhs) der.simps(5)[of c "r\<^sub>1" "r\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
  @{thm (lhs) der.simps(6)} & $\dn$ & @{thm (rhs) der.simps(6)}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
  \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
  @{thm (lhs) nullable.simps(1)} & $\dn$ & @{thm (rhs) nullable.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
  @{thm (lhs) nullable.simps(2)} & $\dn$ & @{thm (rhs) nullable.simps(2)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
  @{thm (lhs) nullable.simps(3)} & $\dn$ & @{thm (rhs) nullable.simps(3)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
  @{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"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
  @{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"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
  @{thm (lhs) nullable.simps(6)} & $\dn$ & @{thm (rhs) nullable.simps(6)}%\medskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
  \end{tabular}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
\end{tabular}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
  We can extend this definition to give derivatives w.r.t.~strings,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
  namely @{thm (lhs) ders.simps(1)}$\dn$@{thm (rhs) ders.simps(1)}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
  and @{thm (lhs) ders.simps(2)}$\dn$@{thm (rhs) ders.simps(2)}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
  Using @{text nullable} and the derivative operation, we can
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
define a simple regular expression matcher, namely
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
$@{text "match s r"} \;\dn\; @{term nullable}(r\backslash s)$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
This is essentially Brzozowski's algorithm from 1964. Its
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
main virtue is that the algorithm can be easily implemented as a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   286
functional program (either in a functional programming language or in
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
a theorem prover). The correctness of @{text match} amounts to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
establishing the property:%\footnote{It is a fun exercise to formally prove this property in a theorem prover.}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   289
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   290
\begin{proposition}\label{matchcorr} 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   291
@{text "match s r"} \;\;\text{if and only if}\;\; $s \in L(r)$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
\end{proposition}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   293
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
It is a fun exercise to formally prove this property in a theorem prover.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   296
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   297
The novel idea of Sulzmann and Lu is to extend this algorithm for 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   298
lexing, where it is important to find out which part of the string
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   299
is matched by which part of the regular expression.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   300
For this Sulzmann and Lu presented two lexing algorithms in their paper
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   301
  \cite{Sulzmann2014}. The first algorithm consists of two phases: first a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   302
  matching phase (which is Brzozowski's algorithm) and then a value
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   303
  construction phase. The values encode \emph{how} a regular expression
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   304
  matches a string. \emph{Values} are defined as the inductive datatype
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   305
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   306
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   307
  @{text "v ::="}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   308
  @{const "Void"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   309
  @{term "val.Char c"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   310
  @{term "Left v"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   311
  @{term "Right v"} $\mid$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   312
  @{term "Seq v\<^sub>1 v\<^sub>2"} $\mid$ 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   313
  @{term "Stars vs"} 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   314
  \end{center}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   315
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   316
  \noindent where we use @{term vs} to stand for a list of values. The
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   317
  string underlying a value can be calculated by a @{const flat}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   318
  function, written @{term "flat DUMMY"}. It traverses a value and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   319
  collects the characters contained in it \cite{AusafDyckhoffUrban2016}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   320
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   321
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   322
  Sulzmann and Lu also define inductively an
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   323
  inhabitation relation that associates values to regular expressions. This
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   324
  is defined by the following six rules for the values:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   325
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   326
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   327
  \begin{tabular}{@ {\hspace{-12mm}}c@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   328
  \begin{tabular}{@ {}c@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   329
  \\[-8mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   330
  @{thm[mode=Axiom] Prf.intros(4)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   331
  @{thm[mode=Axiom] Prf.intros(5)[of "c"]}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   332
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   333
  \quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   334
  @{thm[mode=Rule] Prf.intros(2)[of "v\<^sub>1" "r\<^sub>1" "r\<^sub>2"]}\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   335
  @{thm[mode=Rule] Prf.intros(3)[of "v\<^sub>2" "r\<^sub>2" "r\<^sub>1"]}\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   336
  @{thm[mode=Rule] Prf.intros(1)[of "v\<^sub>1" "r\<^sub>1" "v\<^sub>2" "r\<^sub>2"]}\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   337
  @{thm[mode=Rule] Prf.intros(6)[of "vs" "r"]}\mbox{}\hspace{-10mm}\mbox{}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   338
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   339
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   340
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   341
  \noindent Note that no values are associated with the regular expression
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   342
  @{term ZERO}, since it cannot match any string.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   343
  It is routine to establish how values ``inhabiting'' a regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   344
  expression correspond to the language of a regular expression, namely
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   345
  @{thm L_flat_Prf}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   346
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   347
  In general there is more than one value inhabited by a regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   348
  expression (meaning regular expressions can typically match more
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   349
  than one string). But even when fixing a string from the language of the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   350
  regular expression, there are generally more than one way of how the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   351
  regular expression can match this string. POSIX lexing is about
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   352
  identifying the unique value for a given regular expression and a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   353
  string that satisfies the informal POSIX rules (see
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   354
  \cite{POSIX,Kuklewicz,OkuiSuzuki2010,Sulzmann2014,Vansummeren2006}).\footnote{POSIX
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   355
	lexing acquired its name from the fact that the corresponding
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   356
	rules were described as part of the POSIX specification for
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   357
	Unix-like operating systems \cite{POSIX}.} Sometimes these
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   358
  informal rules are called \emph{maximal munch rule} and \emph{rule priority}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   359
  One contribution of our earlier paper is to give a convenient
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   360
 specification for what POSIX values are (the inductive rules are shown in
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   361
  Figure~\ref{POSIXrules}).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   362
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   363
\begin{figure}[t]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   364
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   365
  \begin{tabular}{@ {}c@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   366
  @{thm[mode=Axiom] Posix.intros(1)}\<open>P\<close>@{term "ONE"} \quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   367
  @{thm[mode=Axiom] Posix.intros(2)}\<open>P\<close>@{term "c"}\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   368
  @{thm[mode=Rule] Posix.intros(3)[of "s" "r\<^sub>1" "v" "r\<^sub>2"]}\<open>P+L\<close>\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   369
  @{thm[mode=Rule] Posix.intros(4)[of "s" "r\<^sub>2" "v" "r\<^sub>1"]}\<open>P+R\<close>\medskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   370
  $\mprset{flushleft}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   371
   \inferrule
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   372
   {@{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
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   373
    @{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"]} \\\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   374
    @{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"]}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   375
   {@{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>\medskip\smallskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   376
  @{thm[mode=Axiom] Posix.intros(7)}\<open>P[]\<close>\qquad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   377
  $\mprset{flushleft}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   378
   \inferrule
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   379
   {@{thm (prem 1) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   380
    @{thm (prem 2) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \qquad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   381
    @{thm (prem 3) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]} \\\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   382
    @{thm (prem 4) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   383
   {@{thm (concl) Posix.intros(6)[of "s\<^sub>1" "r" "v" "s\<^sub>2" "vs"]}}$\<open>P\<star>\<close>\\[-4mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   384
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   385
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   386
  \caption{The inductive definition of POSIX values taken from our earlier paper \cite{AusafDyckhoffUrban2016}. The ternary relation, written $(s, r) \rightarrow v$, formalises the notion
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   387
  of given a string $s$ and a regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   388
  expression $r$ what is the unique value $v$ that satisfies the informal POSIX constraints for
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   389
  regular expression matching.}\label{POSIXrules}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   390
  \end{figure}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   391
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   392
  The clever idea by Sulzmann and Lu \cite{Sulzmann2014} in their first algorithm is to define
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   393
  an injection function on values that mirrors (but inverts) the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   394
  construction of the derivative on regular expressions. Essentially it
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   395
  injects back a character into a value.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   396
  For this they define two functions called @{text mkeps} and @{text inj}:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   397
 %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   398
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   399
  \begin{tabular}{@ {}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   400
  \begin{tabular}{@ {}lcl@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   401
  @{thm (lhs) mkeps.simps(1)} & $\dn$ & @{thm (rhs) mkeps.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   402
  @{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"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   403
  @{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"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   404
  @{thm (lhs) mkeps.simps(4)} & $\dn$ & @{thm (rhs) mkeps.simps(4)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   405
  \end{tabular}\smallskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   406
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   407
  \begin{tabular}{@ {}cc@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   408
  \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   409
  @{thm (lhs) injval.simps(1)} & $\dn$ & @{thm (rhs) injval.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   410
  @{thm (lhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]} & $\dn$ & 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   411
      @{thm (rhs) injval.simps(2)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   412
  @{thm (lhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$ & 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   413
      @{thm (rhs) injval.simps(3)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   414
  @{thm (lhs) injval.simps(7)[of "r" "c" "v" "vs"]} & $\dn$ 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   415
      & @{thm (rhs) injval.simps(7)[of "r" "c" "v" "vs"]}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   416
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   417
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   418
  \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   419
  @{thm (lhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   420
      & @{thm (rhs) injval.simps(4)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   421
  @{thm (lhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]} & $\dn$ 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   422
      & @{thm (rhs) injval.simps(5)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>1" "v\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   423
  @{thm (lhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]} & $\dn$\\ 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   424
  \multicolumn{3}{@ {}r@ {}}{@{thm (rhs) injval.simps(6)[of "r\<^sub>1" "r\<^sub>2" "c" "v\<^sub>2"]}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   425
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   426
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   427
  \end{tabular}\smallskip
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   428
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   429
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   430
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   431
  The function @{text mkeps} is run when the last derivative is nullable, that is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   432
  the string to be matched is in the language of the regular expression. It generates
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   433
  a value for how the last derivative can match the empty string. The injection function
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   434
  then calculates the corresponding value for each intermediate derivative until
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   435
  a value for the original regular expression is generated.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   436
  Graphically the algorithm by
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   437
  Sulzmann and Lu can be illustrated by the following picture %in Figure~\ref{Sulz}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   438
  where the path from the left to the right involving @{term derivatives}/@{const
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   439
  nullable} is the first phase of the algorithm (calculating successive
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   440
  \Brz's derivatives) and @{const mkeps}/@{text inj}, the path from right to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   441
  left, the second phase.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   442
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   443
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   444
\begin{tikzpicture}[scale=0.99,node distance=9mm,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   445
                    every node/.style={minimum size=6mm}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   446
\node (r1)  {@{term "r\<^sub>1"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   447
\node (r2) [right=of r1]{@{term "r\<^sub>2"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   448
\draw[->,line width=1mm](r1)--(r2) node[above,midway] {@{term "der a DUMMY"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   449
\node (r3) [right=of r2]{@{term "r\<^sub>3"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   450
\draw[->,line width=1mm](r2)--(r3) node[above,midway] {@{term "der b DUMMY"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   451
\node (r4) [right=of r3]{@{term "r\<^sub>4"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   452
\draw[->,line width=1mm](r3)--(r4) node[above,midway] {@{term "der c DUMMY"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   453
\draw (r4) node[anchor=west] {\;\raisebox{3mm}{@{term nullable}}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   454
\node (v4) [below=of r4]{@{term "v\<^sub>4"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   455
\draw[->,line width=1mm](r4) -- (v4);
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   456
\node (v3) [left=of v4] {@{term "v\<^sub>3"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   457
\draw[->,line width=1mm](v4)--(v3) node[below,midway] {\<open>inj r\<^sub>3 c\<close>};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   458
\node (v2) [left=of v3]{@{term "v\<^sub>2"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   459
\draw[->,line width=1mm](v3)--(v2) node[below,midway] {\<open>inj r\<^sub>2 b\<close>};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   460
\node (v1) [left=of v2] {@{term "v\<^sub>1"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   461
\draw[->,line width=1mm](v2)--(v1) node[below,midway] {\<open>inj r\<^sub>1 a\<close>};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   462
\draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{@{term "mkeps"}}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   463
\end{tikzpicture}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   464
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   465
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   466
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   467
  The picture shows the steps required when a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   468
  regular expression, say @{text "r\<^sub>1"}, matches the string @{term
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   469
  "[a,b,c]"}. The first lexing algorithm by Sulzmann and Lu can be defined as:\\[-8mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   470
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   471
%  \begin{figure}[t]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   472
%\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   473
%\begin{tikzpicture}[scale=1,node distance=1cm,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   474
%                    every node/.style={minimum size=6mm}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   475
%\node (r1)  {@{term "r\<^sub>1"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   476
%\node (r2) [right=of r1]{@{term "r\<^sub>2"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   477
%\draw[->,line width=1mm](r1)--(r2) node[above,midway] {@{term "der a DUMMY"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   478
%\node (r3) [right=of r2]{@{term "r\<^sub>3"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   479
%\draw[->,line width=1mm](r2)--(r3) node[above,midway] {@{term "der b DUMMY"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   480
%\node (r4) [right=of r3]{@{term "r\<^sub>4"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   481
%\draw[->,line width=1mm](r3)--(r4) node[above,midway] {@{term "der c DUMMY"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   482
%\draw (r4) node[anchor=west] {\;\raisebox{3mm}{@{term nullable}}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   483
%\node (v4) [below=of r4]{@{term "v\<^sub>4"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   484
%\draw[->,line width=1mm](r4) -- (v4);
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   485
%\node (v3) [left=of v4] {@{term "v\<^sub>3"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   486
%\draw[->,line width=1mm](v4)--(v3) node[below,midway] {\<open>inj r\<^sub>3 c\<close>};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   487
%\node (v2) [left=of v3]{@{term "v\<^sub>2"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   488
%\draw[->,line width=1mm](v3)--(v2) node[below,midway] {\<open>inj r\<^sub>2 b\<close>};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   489
%\node (v1) [left=of v2] {@{term "v\<^sub>1"}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   490
%\draw[->,line width=1mm](v2)--(v1) node[below,midway] {\<open>inj r\<^sub>1 a\<close>};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   491
%\draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{@{term "mkeps"}}};
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   492
%\end{tikzpicture}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   493
%\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   494
%\mbox{}\\[-13mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   495
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   496
%\caption{The two phases of the first algorithm by Sulzmann \& Lu \cite{Sulzmann2014},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   497
%matching the string @{term "[a,b,c]"}. The first phase (the arrows from 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   498
%left to right) is \Brz's matcher building successive derivatives. If the 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   499
%last regular expression is @{term nullable}, then the functions of the 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   500
%second phase are called (the top-down and right-to-left arrows): first 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   501
%@{term mkeps} calculates a value @{term "v\<^sub>4"} witnessing
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   502
%how the empty string has been recognised by @{term "r\<^sub>4"}. After
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   503
%that the function @{term inj} ``injects back'' the characters of the string into
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   504
%the values. The value @{term "v\<^sub>1"} is the result of the algorithm representing
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   505
%the POSIX value for this string and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   506
%regular expression.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   507
%\label{Sulz}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   508
%\end{figure} 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   509
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   510
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   511
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   512
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   513
  \begin{tabular}{lcl}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   514
  @{thm (lhs) lexer.simps(1)} & $\dn$ & @{thm (rhs) lexer.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   515
  @{thm (lhs) lexer.simps(2)} & $\dn$ & @{text "case"} @{term "lexer (der c r) s"} @{text of}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   516
                     @{term "None"}  @{text "\<Rightarrow>"} @{term None}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   517
                     & & \hspace{24mm}$|\;$ @{term "Some v"} @{text "\<Rightarrow>"} @{term "Some (injval r c v)"}                          
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   518
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   519
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   520
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   521
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   522
We have shown in our earlier paper \cite{AusafDyckhoffUrban2016} that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   523
this algorithm is correct, that is it generates POSIX values. The
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   524
central property we established relates the derivative operation to the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   525
injection function.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   526
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   527
  \begin{proposition}\label{Posix2}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   528
	\textit{If} $(s,\; r\backslash c) \rightarrow v$ \textit{then} $(c :: s,\; r) \rightarrow$ \textit{inj} $r\; c\; v$. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   529
\end{proposition}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   530
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   531
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   532
  With this in place we were able to prove:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   533
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   534
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   535
  \begin{proposition}\mbox{}\smallskip\\\label{lexercorrect}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   536
  \begin{tabular}{ll}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   537
  (1) & @{thm (lhs) lexer_correct_None} if and only if @{thm (rhs) lexer_correct_None}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   538
  (2) & @{thm (lhs) lexer_correct_Some} if and only if @{thm (rhs) lexer_correct_Some}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   539
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   540
  \end{proposition}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   541
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   542
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   543
  In fact we have shown that, in the success case, the generated POSIX value $v$ is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   544
  unique and in the failure case that there is no POSIX value $v$ that satisfies
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   545
  $(s, r) \rightarrow v$. While the algorithm is correct, it is excruciatingly
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   546
  slow in cases where the derivatives grow arbitrarily (recall the example from the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   547
  Introduction). However it can be used as a convenient reference point for the correctness
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   548
  proof of the second algorithm by Sulzmann and Lu, which we shall describe next.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   549
  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   550
*}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   551
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   552
section {* Bitcoded Regular Expressions and Derivatives *}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   553
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   554
text {*
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   555
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   556
  In the second part of their paper \cite{Sulzmann2014},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   557
  Sulzmann and Lu describe another algorithm that also generates POSIX
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   558
  values but dispenses with the second phase where characters are
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   559
  injected ``back'' into values. For this they annotate bitcodes to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   560
  regular expressions, which we define in Isabelle/HOL as the datatype
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   561
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   562
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   563
  @{term breg} $\;::=\;$ @{term "AZERO"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   564
               $\;\mid\;$ @{term "AONE bs"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   565
               $\;\mid\;$ @{term "ACHAR bs c"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   566
               $\;\mid\;$ @{term "AALTs bs rs"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   567
               $\;\mid\;$ @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   568
               $\;\mid\;$ @{term "ASTAR bs r"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   569
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   570
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   571
  \noindent where @{text bs} stands for bitsequences; @{text r},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   572
  @{text "r\<^sub>1"} and @{text "r\<^sub>2"} for bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   573
  expressions; and @{text rs} for lists of bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   574
  expressions. The binary alternative @{text "ALT bs r\<^sub>1 r\<^sub>2"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   575
  is just an abbreviation for \mbox{@{text "ALTs bs [r\<^sub>1, r\<^sub>2]"}}. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   576
  For bitsequences we use lists made up of the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   577
  constants @{text Z} and @{text S}.  The idea with bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   578
  expressions is to incrementally generate the value information (for
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   579
  example @{text Left} and @{text Right}) as bitsequences. For this 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   580
  Sulzmann and Lu define a coding
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   581
  function for how values can be coded into bitsequences.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   582
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   583
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   584
  \begin{tabular}{cc}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   585
  \begin{tabular}{lcl}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   586
  @{thm (lhs) code.simps(1)} & $\dn$ & @{thm (rhs) code.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   587
  @{thm (lhs) code.simps(2)} & $\dn$ & @{thm (rhs) code.simps(2)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   588
  @{thm (lhs) code.simps(3)} & $\dn$ & @{thm (rhs) code.simps(3)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   589
  @{thm (lhs) code.simps(4)} & $\dn$ & @{thm (rhs) code.simps(4)}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   590
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   591
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   592
  \begin{tabular}{lcl}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   593
  @{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"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   594
  @{thm (lhs) code.simps(6)} & $\dn$ & @{thm (rhs) code.simps(6)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   595
  @{thm (lhs) code.simps(7)} & $\dn$ & @{thm (rhs) code.simps(7)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   596
  \mbox{\phantom{XX}}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   597
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   598
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   599
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   600
   
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   601
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   602
  As can be seen, this coding is ``lossy'' in the sense that we do not
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   603
  record explicitly character values and also not sequence values (for
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   604
  them we just append two bitsequences). However, the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   605
  different alternatives for @{text Left}, respectively @{text Right}, are recorded as @{text Z} and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   606
  @{text S} followed by some bitsequence. Similarly, we use @{text Z} to indicate
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   607
  if there is still a value coming in the list of @{text Stars}, whereas @{text S}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   608
  indicates the end of the list. The lossiness makes the process of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   609
  decoding a bit more involved, but the point is that if we have a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   610
  regular expression \emph{and} a bitsequence of a corresponding value,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   611
  then we can always decode the value accurately (see Fig.~\ref{decode}).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   612
  The function \textit{decode} checks whether all of the bitsequence is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   613
  consumed and returns the corresponding value as @{term "Some v"}; otherwise
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   614
  it fails with @{text "None"}. We can establish that for a value $v$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   615
  inhabited by a regular expression $r$, the decoding of its
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   616
  bitsequence never fails.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   617
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   618
  %The decoding can be
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   619
  %defined by using two functions called $\textit{decode}'$ and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   620
  %\textit{decode}:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   621
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   622
  \begin{figure}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   623
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   624
  \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   625
  \multicolumn{3}{@ {}l}{$\textit{decode}'\,bs\,(\ONE)$ $\;\dn\;$ $(\Empty, bs)$ \quad\qquad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   626
  $\textit{decode}'\,bs\,(c)$ $\;\dn\;$ $(\Char\,c, bs)$}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   627
  $\textit{decode}'\,(\Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   628
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   629
       (\Left\,v, bs_1)$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   630
  $\textit{decode}'\,(\S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   631
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   632
       (\Right\,v, bs_1)$\\                           
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   633
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   634
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   635
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   636
        \hspace{2mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   637
  $\textit{decode}'\,(\Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   638
  $\textit{decode}'\,(\S\!::\!bs)\,(r^*)$ & $\dn$ & 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   639
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   640
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   641
        \hspace{2mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\medskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   642
  \multicolumn{3}{l}{$\textit{decode}\,bs\,r$ $\dn$ 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   643
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   644
     $\;\,\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   645
       \textit{else}\;\textit{None}$}\\[-4mm]   
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   646
  \end{tabular}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   647
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   648
  \caption{Two functions, called $\textit{decode}'$ and \textit{decode}, for decoding a value from a bitsequence with the help of a regular expression.\\[-5mm]}\label{decode}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   649
  \end{figure}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   650
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   651
  %\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   652
  %The function \textit{decode} checks whether all of the bitsequence is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   653
  %consumed and returns the corresponding value as @{term "Some v"}; otherwise
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   654
  %it fails with @{text "None"}. We can establish that for a value $v$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   655
  %inhabited by a regular expression $r$, the decoding of its
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   656
  %bitsequence never fails.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   657
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   658
\begin{lemma}\label{codedecode}\it
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   659
  If $\;\vdash v : r$ then
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   660
  $\;\textit{decode}\,(\textit{code}\, v)\,r = \textit{Some}\, v$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   661
\end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   662
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   663
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   664
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   665
  Sulzmann and Lu define the function \emph{internalise}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   666
  in order to transform (standard) regular expressions into annotated
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   667
  regular expressions. We write this operation as $r^\uparrow$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   668
  This internalisation uses the following
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   669
  \emph{fuse} function.	     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   670
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   671
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   672
  \begin{tabular}{@ {}cc@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   673
  \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   674
  $\textit{fuse}\,bs\,(\textit{ZERO})$ & $\dn$ & $\textit{ZERO}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   675
  $\textit{fuse}\,bs\,(\textit{ONE}\,bs')$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   676
     $\textit{ONE}\,(bs\,@\,bs')$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   677
  $\textit{fuse}\,bs\,(\textit{CHAR}\,bs'\,c)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   678
     $\textit{CHAR}\,(bs\,@\,bs')\,c$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   679
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   680
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   681
  \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   682
  $\textit{fuse}\,bs\,(\textit{ALTs}\,bs'\,rs)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   683
     $\textit{ALTs}\,(bs\,@\,bs')\,rs$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   684
  $\textit{fuse}\,bs\,(\textit{SEQ}\,bs'\,r_1\,r_2)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   685
     $\textit{SEQ}\,(bs\,@\,bs')\,r_1\,r_2$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   686
  $\textit{fuse}\,bs\,(\textit{STAR}\,bs'\,r)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   687
     $\textit{STAR}\,(bs\,@\,bs')\,r$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   688
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   689
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   690
  \end{center}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   691
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   692
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   693
  A regular expression can then be \emph{internalised} into a bitcoded
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   694
  regular expression as follows:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   695
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   696
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   697
  \begin{tabular}{@ {}ccc@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   698
  \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   699
  $(\ZERO)^\uparrow$ & $\dn$ & $\textit{ZERO}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   700
  $(\ONE)^\uparrow$ & $\dn$ & $\textit{ONE}\,[]$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   701
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   702
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   703
  \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   704
  $(c)^\uparrow$ & $\dn$ & $\textit{CHAR}\,[]\,c$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   705
  $(r^*)^\uparrow$ & $\dn$ & $\textit{STAR}\;[]\,r^\uparrow$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   706
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   707
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   708
  \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   709
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   710
         $\textit{ALT}\;[]\,(\textit{fuse}\,[\Z]\,r_1^\uparrow)\,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   711
                            (\textit{fuse}\,[\S]\,r_2^\uparrow)$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   712
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   713
         $\textit{SEQ}\;[]\,r_1^\uparrow\,r_2^\uparrow$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   714
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   715
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   716
  \end{center}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   717
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   718
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   719
  There is also an \emph{erase}-function, written $r^\downarrow$, which
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   720
  transforms a bitcoded regular expression into a (standard) regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   721
  expression by just erasing the annotated bitsequences. We omit the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   722
  straightforward definition. For defining the algorithm, we also need
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   723
  the functions \textit{bnullable} and \textit{bmkeps}(\textit{s}), which are the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   724
  ``lifted'' versions of \textit{nullable} and \textit{mkeps} acting on
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   725
  bitcoded regular expressions.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   726
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   727
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   728
  \begin{tabular}{@ {}c@ {\hspace{2mm}}c@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   729
  \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   730
  $\textit{bnullable}\,(\textit{ZERO})$ & $\dn$ & $\textit{False}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   731
  $\textit{bnullable}\,(\textit{ONE}\,bs)$ & $\dn$ & $\textit{True}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   732
  $\textit{bnullable}\,(\textit{CHAR}\,bs\,c)$ & $\dn$ & $\textit{False}$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   733
  $\textit{bnullable}\,(\textit{ALTs}\,bs\,\rs)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   734
     $\exists\, r \in \rs. \,\textit{bnullable}\,r$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   735
  $\textit{bnullable}\,(\textit{SEQ}\,bs\,r_1\,r_2)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   736
     $\textit{bnullable}\,r_1\wedge \textit{bnullable}\,r_2$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   737
  $\textit{bnullable}\,(\textit{STAR}\,bs\,r)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   738
     $\textit{True}$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   739
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   740
  &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   741
  \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   742
  $\textit{bmkeps}\,(\textit{ONE}\,bs)$ & $\dn$ & $bs$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   743
  $\textit{bmkeps}\,(\textit{ALTs}\,bs\,\rs)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   744
  $bs\,@\,\textit{bmkepss}\,\rs$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   745
  $\textit{bmkeps}\,(\textit{SEQ}\,bs\,r_1\,r_2)$ & $\dn$ &\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   746
  \multicolumn{3}{r}{$bs \,@\,\textit{bmkeps}\,r_1\,@\, \textit{bmkeps}\,r_2$}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   747
  $\textit{bmkeps}\,(\textit{STAR}\,bs\,r)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   748
     $bs \,@\, [\S]$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   749
  $\textit{bmkepss}\,(r\!::\!\rs)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   750
     $\textit{if}\;\textit{bnullable}\,r$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   751
  & &$\textit{then}\;\textit{bmkeps}\,r$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   752
  & &$\textit{else}\;\textit{bmkepss}\,\rs$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   753
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   754
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   755
  \end{center}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   756
 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   757
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   758
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   759
  The key function in the bitcoded algorithm is the derivative of a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   760
  bitcoded regular expression. This derivative function calculates the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   761
  derivative but at the same time also the incremental part of the bitsequences
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   762
  that contribute to constructing a POSIX value.	
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   763
  % 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   764
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   765
  \begin{tabular}{@ {}lcl@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   766
  \multicolumn{3}{@ {}l}{$(\textit{ZERO})\backslash c$ $\;\dn\;$ $\textit{ZERO}$ \quad\qquad  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   767
  $(\textit{ONE}\;bs)\backslash c$ $\;\dn\;$ $\textit{ZERO}$}\\  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   768
  $(\textit{CHAR}\;bs\,d)\backslash c$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   769
        $\textit{if}\;c=d\; \;\textit{then}\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   770
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   771
  $(\textit{ALTs}\;bs\,\rs)\backslash c$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   772
        $\textit{ALTs}\,bs\,(\mathit{map}\,(\_\backslash c)\,\rs)$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   773
  $(\textit{SEQ}\;bs\,r_1\,r_2)\backslash c$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   774
     $\textit{if}\;\textit{bnullable}\,r_1$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   775
  & &$\textit{then}\;\textit{ALT}\,bs\;(\textit{SEQ}\,[]\,(r_1\backslash c)\,r_2)$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   776
  $\;(\textit{fuse}\,(\textit{bmkeps}\,r_1)\,(r_2\backslash c))$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   777
  & &$\textit{else}\;\textit{SEQ}\,bs\,(r_1\backslash c)\,r_2$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   778
  $(\textit{STAR}\,bs\,r)\backslash c$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   779
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\backslash c))\,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   780
       (\textit{STAR}\,[]\,r)$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   781
  \end{tabular}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   782
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   783
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   784
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   785
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   786
  This function can also be extended to strings, written $r\backslash s$,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   787
  just like the standard derivative.  We omit the details. Finally we
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   788
  can define Sulzmann and Lu's bitcoded lexer, which we call \textit{blexer}:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   789
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   790
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   791
\begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   792
  $\textit{blexer}\;r\,s$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   793
      $\textit{let}\;r_{der} = (r^\uparrow)\backslash s\;\textit{in}$                
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   794
  $\;\;\textit{if}\; \textit{bnullable}(r_{der}) \;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,r_{der})\,r
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   795
       \;\;\textit{else}\;\textit{None}$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   796
\end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   797
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   798
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   799
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   800
This bitcoded lexer first internalises the regular expression $r$ and then
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   801
builds the bitcoded derivative according to $s$. If the derivative is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   802
(b)nullable the string is in the language of $r$ and it extracts the bitsequence using the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   803
$\textit{bmkeps}$ function. Finally it decodes the bitsequence into a value.  If
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   804
the derivative is \emph{not} nullable, then $\textit{None}$ is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   805
returned. We can show that this way of calculating a value
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   806
generates the same result as \textit{lexer}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   807
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   808
Before we can proceed we need to define a helper function, called
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   809
\textit{retrieve}, which Sulzmann and Lu introduced for the correctness proof.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   810
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   811
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   812
  \begin{tabular}{lcl}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   813
  @{thm (lhs) retrieve.simps(1)} & $\dn$ & @{thm (rhs) retrieve.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   814
  @{thm (lhs) retrieve.simps(2)} & $\dn$ & @{thm (rhs) retrieve.simps(2)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   815
  @{thm (lhs) retrieve.simps(3)} & $\dn$ & @{thm (rhs) retrieve.simps(3)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   816
  @{thm (lhs) better_retrieve(1)} & $\dn$ & @{thm (rhs) better_retrieve(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   817
  @{thm (lhs) better_retrieve(2)} & $\dn$ & @{thm (rhs) better_retrieve(2)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   818
  @{thm (lhs) retrieve.simps(6)[of _ "r\<^sub>1" "r\<^sub>2" "v\<^sub>1" "v\<^sub>2"]}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   819
      & $\dn$ & @{thm (rhs) retrieve.simps(6)[of _ "r\<^sub>1" "r\<^sub>2" "v\<^sub>1" "v\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   820
  @{thm (lhs) retrieve.simps(7)} & $\dn$ & @{thm (rhs) retrieve.simps(7)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   821
  @{thm (lhs) retrieve.simps(8)} & $\dn$ & @{thm (rhs) retrieve.simps(8)}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   822
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   823
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   824
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   825
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   826
The idea behind this function is to retrieve a possibly partial
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   827
bitsequence from a bitcoded regular expression, where the retrieval is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   828
guided by a value.  For example if the value is $\Left$ then we
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   829
descend into the left-hand side of an alternative in order to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   830
assemble the bitcode. Similarly for
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   831
$\Right$. The property we can show is that for a given $v$ and $r$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   832
with $\vdash v : r$, the retrieved bitsequence from the internalised
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   833
regular expression is equal to the bitcoded version of $v$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   834
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   835
\begin{lemma}\label{retrievecode}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   836
If $\vdash v : r$ then $\textit{code}\, v = \textit{retrieve}\,(r^\uparrow)\,v$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   837
\end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   838
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   839
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   840
We also need some auxiliary facts about how the bitcoded operations
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   841
relate to the ``standard'' operations on regular expressions. For
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   842
example if we build a bitcoded derivative and erase the result, this
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   843
is the same as if we first erase the bitcoded regular expression and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   844
then perform the ``standard'' derivative operation.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   845
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   846
\begin{lemma}\label{bnullable}\mbox{}\smallskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   847
  \begin{tabular}{ll}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   848
\textit{(1)} & $(r\backslash s)^\downarrow = (r^\downarrow)\backslash s$\\    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   849
\textit{(2)} & $\textit{bnullable}(r)$ iff $\textit{nullable}(r^\downarrow)$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   850
\textit{(3)} & $\textit{bmkeps}(r) = \textit{retrieve}\,r\,(\textit{mkeps}\,(r^\downarrow))$ provided $\textit{nullable}(r^\downarrow)$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   851
\end{tabular}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   852
\end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   853
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   854
%\begin{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   855
%  All properties are by induction on annotated regular expressions.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   856
%  %There are no interesting cases.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   857
%\end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   858
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   859
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   860
The only difficulty left for the correctness proof is that the bitcoded algorithm
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   861
has only a ``forward phase'' where POSIX values are generated incrementally.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   862
We can achieve the same effect with @{text lexer} (which has two phases) by stacking up injection
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   863
functions during the forward phase. An auxiliary function, called $\textit{flex}$,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   864
allows us to recast the rules of $\lexer$ in terms of a single
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   865
phase and stacked up injection functions.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   866
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   867
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   868
\begin{tabular}{@ {}l@ {}c@ {}l@ {\hspace{10mm}}l@ {}c@ {}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   869
  $\textit{flex}\;r\,f\,[]$ & $\dn$ & $f$ & 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   870
  $\textit{flex}\;r\,f\,(c\!::\!s)$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   871
  $\textit{flex}\,(r\backslash c)\,(\lambda v.\,f\,(\inj\,r\,c\,v))\,s$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   872
\end{tabular}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   873
\end{center}    
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   874
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   875
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   876
The point of this function is that when
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   877
reaching the end of the string, we just need to apply the stacked up
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   878
injection functions to the value generated by @{text mkeps}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   879
Using this function we can recast the success case in @{text lexer} 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   880
as follows:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   881
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   882
\begin{lemma}\label{flex}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   883
If @{text "lexer r s = Some v"} \;then\; @{text "v = "}$\,\textit{flex}\,r\,id\,s\,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   884
      (\mkeps (r\backslash s))$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   885
\end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   886
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   887
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   888
Note we did not redefine \textit{lexer}, we just established that the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   889
value generated by \textit{lexer} can also be obtained by a different
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   890
method. While this different method is not efficient (we essentially
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   891
need to traverse the string $s$ twice, once for building the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   892
derivative $r\backslash s$ and another time for stacking up injection
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   893
functions), it helps us with proving
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   894
that incrementally building up values in @{text blexer} generates the same result.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   895
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   896
This brings us to our main lemma in this section: if we calculate a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   897
derivative, say $r\backslash s$, and have a value, say $v$, inhabited
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   898
by this derivative, then we can produce the result @{text lexer} generates
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   899
by applying this value to the stacked-up injection functions
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   900
that $\textit{flex}$ assembles. The lemma establishes that this is the same
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   901
value as if we build the annotated derivative $r^\uparrow\backslash s$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   902
and then retrieve the corresponding bitcoded version, followed by a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   903
decoding step.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   904
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   905
\begin{lemma}[Main Lemma]\label{mainlemma}\it
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   906
If $\vdash v : r\backslash s$ then 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   907
$\textit{Some}\,(\textit{flex}\,r\,\textit{id}\,s\,v) =
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   908
  \textit{decode}(\textit{retrieve}\,(r^\uparrow \backslash s)\,v)\,r$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   909
\end{lemma}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   910
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   911
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   912
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   913
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   914
%With this lemma in place,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   915
We can then prove the correctness of \textit{blexer}---it indeed
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   916
produces the same result as \textit{lexer}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   917
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   918
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   919
\begin{theorem}\label{thmone}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   920
$\textit{lexer}\,r\,s = \textit{blexer}\,r\,s$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   921
\end{theorem}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   922
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   923
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   924
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   925
\noindent This establishes that the bitcoded algorithm \emph{without}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   926
simplification produces correct results. This was
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   927
only conjectured by Sulzmann and Lu in their paper
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   928
\cite{Sulzmann2014}. The next step is to add simplifications.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   929
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   930
*}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   931
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   932
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   933
section {* Simplification *}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   934
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   935
text {*
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   936
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   937
     Derivatives as calculated by Brzozowski’s method are usually more
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   938
     complex regular expressions than the initial one; the result is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   939
     that derivative-based matching and lexing algorithms are
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   940
     often abysmally slow if the ``growth problem'' is not addressed. As Sulzmann and Lu wrote, various
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   941
     optimisations are possible, such as the simplifications
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   942
     $\ZERO{}\,r \Rightarrow \ZERO$, $\ONE{}\,r \Rightarrow r$,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   943
     $\ZERO{} + r \Rightarrow r$ and $r + r \Rightarrow r$. While these
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   944
     simplifications can considerably speed up the two algorithms  in many
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   945
     cases, they do not solve fundamentally the growth problem with
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   946
     derivatives. To see this let us return to the example from the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   947
     Introduction that shows the derivatives for \mbox{@{text "(a + aa)\<^sup>*"}}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   948
     If we delete in the 3rd step all $\ZERO{}s$ and $\ONE$s according to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   949
     the simplification rules shown above we obtain
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   950
     %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   951
     %%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   952
     %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   953
     \begin{equation}\def\xll{\xrightarrow{\_\backslash{} [a, a, a]}}\label{derivex}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   954
     (a + aa)^* \quad\xll\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   955
      \underbrace{\mbox{$(\ONE + a) \cdot (a + aa)^*$}}_{r} \;+\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   956
     ((a + aa)^* + \underbrace{\mbox{$(\ONE + a) \cdot (a + aa)^*$}}_{r})
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   957
     \end{equation}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   958
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   959
     \noindent This is a simpler derivative, but unfortunately we
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   960
     cannot make any further simplifications. This is a problem because
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   961
     the outermost alternatives contains two copies of the same
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   962
     regular expression (underlined with $r$). These copies will
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   963
     spawn new copies in later derivative steps and they in turn even more copies. This
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   964
     destroys any hope of taming the size of the derivatives.  But the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   965
     second copy of $r$ in \eqref{derivex} will never contribute to a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   966
     value, because POSIX lexing will always prefer matching a string
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   967
     with the first copy. So it could be safely removed without affecting the correctness of the algorithm.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   968
     The dilemma with the simple-minded
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   969
     simplification rules above is that the rule $r + r \Rightarrow r$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   970
     will never be applicable because as can be seen in this example the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   971
     regular expressions are not next to each other but separated by another regular expression.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   972
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   973
     But here is where Sulzmann and Lu's representation of generalised
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   974
     alternatives in the bitcoded algorithm shines: in @{term
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   975
     "ALTs bs rs"} we can define a more aggressive simplification by
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   976
     recursively simplifying all regular expressions in @{text rs} and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   977
     then analyse the resulting list and remove any duplicates.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   978
     Another advantage with the bitsequences in  bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   979
     expressions is that they can be easily modified such that simplification does not
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   980
     interfere with the value constructions. For example we can ``flatten'', or
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   981
     de-nest, or spill out, @{text ALTs} as follows
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   982
     %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   983
     \[
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   984
     @{term "ALTs bs\<^sub>1 ((ALTs bs\<^sub>2 rs\<^sub>2) # rs\<^sub>1)"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   985
     \quad\xrightarrow{bsimp}\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   986
     @{term "ALTs bs\<^sub>1 ((map (fuse bs\<^sub>2) rs\<^sub>2) # rs\<^sub>1)"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   987
     \]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   988
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   989
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   990
     where we just need to fuse the bitsequence that has accumulated in @{text "bs\<^sub>2"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   991
     to the alternatives in @{text "rs\<^sub>2"}. As we shall show below this will
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   992
     ensure that the correct value corresponding to the original (unsimplified)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   993
     regular expression can still be extracted. %In this way the value construction
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   994
     %is not affected by simplification. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   995
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   996
     However there is one problem with the definition for the more
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   997
     aggressive simplification rules described by Sulzmann and Lu. Recasting
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   998
     their definition with our syntax they define the step of removing
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   999
     duplicates as
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1000
     %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1001
     \[ @{text "bsimp (ALTs bs rs)"} \dn @{text "ALTs
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1002
     bs (nub (map bsimp rs))"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1003
     \]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1004
   
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1005
     \noindent where they first recursively simplify the regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1006
     expressions in @{text rs} (using @{text map}) and then use
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1007
     Haskell's @{text nub}-function to remove potential
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1008
     duplicates. While this makes sense when considering the example
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1009
     shown in \eqref{derivex}, @{text nub} is the inappropriate
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1010
     function in the case of bitcoded regular expressions. The reason
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1011
     is that in general the elements in @{text rs} will have a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1012
     different annotated bitsequence and in this way @{text nub}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1013
     will never find a duplicate to be removed. One correct way to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1014
     handle this situation is to first \emph{erase} the regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1015
     expressions when comparing potential duplicates. This is inspired
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1016
     by Scala's list functions of the form \mbox{@{text "distinctWith rs eq
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1017
     acc"}} where @{text eq} is an user-defined equivalence relation that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1018
     compares two elements in @{text rs}. We define this function in
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1019
     Isabelle/HOL as
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1020
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1021
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1022
     \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {\hspace{1mm}}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1023
     @{thm (lhs) distinctWith.simps(1)} & $\dn$ & @{thm (rhs) distinctWith.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1024
     @{thm (lhs) distinctWith.simps(2)} & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1025
     @{text "if (\<exists> y \<in> acc. eq x y)"} & @{text "then distinctWith xs eq acc"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1026
     & & & @{text "else x :: distinctWith xs eq ({x} \<union> acc)"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1027
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1028
     \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1029
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1030
     \noindent where we scan the list from left to right (because we
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1031
     have to remove later copies). In @{text distinctWith}, @{text eq} is intended to be an
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1032
     equivalence relation for annotated regular expressions and @{text acc} is an accumulator for annotated regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1033
     expressions---essentially a set of regular expressions that we have already seen
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1034
     while scanning the list. Therefore we delete an element, say @{text x},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1035
     from the list provided a @{text "y"} with @{text "y"} being equivalent to @{text x} is already in the accumulator;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1036
     otherwise we keep @{text x} and scan the rest of the list but 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1037
     add @{text "x"} as another ``seen'' element to @{text acc}. We will use
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1038
     @{term distinctWith} where @{text eq} is an equivalence that deletes bitsequences from bitcoded regular expressions
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1039
     before comparing the components. One way to define this in Isabelle/HOL is by the following recursive function from
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1040
     annotated regular expressions to @{text bool}:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1041
     %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1042
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1043
     \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {\hspace{1mm}}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1044
     @{thm (lhs) eq1.simps(1)} & $\dn$ & @{thm (rhs) eq1.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1045
     @{thm (lhs) eq1.simps(2)[of DUMMY DUMMY]} & $\dn$ & @{thm (rhs) eq1.simps(2)[of DUMMY DUMMY]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1046
     @{thm (lhs) eq1.simps(3)[of DUMMY "c" DUMMY "d"]} & $\dn$ & @{thm (rhs) eq1.simps(3)[of DUMMY "c" DUMMY "d"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1047
     @{thm (lhs) eq1.simps(4)[of DUMMY "r\<^sub>1\<^sub>1" "r\<^sub>1\<^sub>2" DUMMY "r\<^sub>2\<^sub>1" "r\<^sub>2\<^sub>2"]} & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1048
     @{thm (rhs) eq1.simps(4)[of DUMMY "r\<^sub>1\<^sub>1" "r\<^sub>1\<^sub>2" DUMMY "r\<^sub>2\<^sub>1" "r\<^sub>2\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1049
     @{thm (lhs) eq1.simps(5)[of DUMMY DUMMY]} & $\dn$ & @{thm (rhs) eq1.simps(5)[of DUMMY DUMMY]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1050
     @{thm (lhs) eq1.simps(6)[of DUMMY "r\<^sub>1" "rs\<^sub>1" DUMMY "r\<^sub>2" "rs\<^sub>2"]} & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1051
     @{thm (rhs) eq1.simps(6)[of DUMMY "r\<^sub>1" "rs\<^sub>1" DUMMY "r\<^sub>2" "rs\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1052
     @{thm (lhs) eq1.simps(7)[of DUMMY "r\<^sub>1" DUMMY "r\<^sub>2"]} & $\dn$ & @{thm (rhs) eq1.simps(7)[of DUMMY "r\<^sub>1" DUMMY "r\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1053
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1054
     \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1055
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1056
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1057
     where all other cases are set to @{text False}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1058
     This equivalence is clearly a computationally more expensive operation than @{text nub},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1059
     but is needed in order to make the removal of unnecessary copies
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1060
     to work properly.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1061
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1062
     Our simplification function depends on three more helper functions, one is called
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1063
     @{text flts} and analyses lists of regular expressions coming from alternatives.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1064
     It is defined as follows:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1065
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1066
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1067
     \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1068
     @{thm (lhs) flts.simps(1)} & $\dn$ & @{thm (rhs) flts.simps(1)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1069
     @{thm (lhs) flts.simps(2)} & $\dn$ & @{thm (rhs) flts.simps(2)}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1070
     @{thm (lhs) flts.simps(3)[of "bs'" "rs'"]} & $\dn$ & @{thm (rhs) flts.simps(3)[of "bs'" "rs'"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1071
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1072
     \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1073
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1074
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1075
     The second clause of @{text flts} removes all instances of @{text ZERO} in alternatives and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1076
     the third ``de-nests'' alternatives (but retaining the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1077
     bitsequence @{text "bs'"} accumulated in the inner alternative). There are
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1078
     some corner cases to be considered when the resulting list inside an alternative is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1079
     empty or a singleton list. We take care of those cases in the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1080
     @{text "bsimpALTs"} function; similarly we define a helper function that simplifies
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1081
     sequences according to the usual rules about @{text ZERO}s and @{text ONE}s:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1082
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1083
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1084
     \begin{tabular}{c@ {\hspace{5mm}}c}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1085
     \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1086
     @{text "bsimpALTs bs []"}  & $\dn$ & @{text "ZERO"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1087
     @{text "bsimpALTs bs [r]"} & $\dn$ & @{text "fuse bs r"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1088
     @{text "bsimpALTs bs rs"}  & $\dn$ & @{text "ALTs bs rs"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1089
     \mbox{}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1090
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1091
     &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1092
     \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1093
     @{text "bsimpSEQ bs _ ZERO"}  & $\dn$ & @{text "ZERO"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1094
     @{text "bsimpSEQ bs ZERO _"} & $\dn$ & @{text "ZERO"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1095
     @{text "bsimpSEQ bs\<^sub>1 (ONE bs\<^sub>2) r\<^sub>2"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1096
         & $\dn$ & @{text "fuse (bs\<^sub>1 @ bs\<^sub>2) r\<^sub>2"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1097
     @{text "bsimpSEQ bs r\<^sub>1 r\<^sub>2"} & $\dn$ &  @{text "SEQ bs r\<^sub>1 r\<^sub>2"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1098
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1099
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1100
     \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1101
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1102
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1103
     With this in place we can define our simplification function as
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1104
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1105
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1106
     \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1107
     @{thm (lhs) bsimp.simps(1)[of "bs" "r\<^sub>1" "r\<^sub>2"]} & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1108
         @{thm (rhs) bsimp.simps(1)[of "bs" "r\<^sub>1" "r\<^sub>2"]}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1109
     @{thm (lhs) bsimp.simps(2)[of "bs" _]} & $\dn$ & @{text "bsimpALTs bs (distinctWith (flts (map bsimp rs)) \<approx> \<emptyset>)"}\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1110
     @{text "bsimp r"} & $\dn$ & @{text r}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1111
     \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1112
     \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1113
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1114
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1115
     We believe our recursive function @{term bsimp} simplifies regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1116
     expressions as intended by Sulzmann and Lu. There is no point in applying the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1117
     @{text bsimp} function repeatedly (like the simplification in their paper which needs to be
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1118
     applied until a fixpoint is reached) because we can show that @{term bsimp} is idempotent,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1119
     that is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1120
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1121
     \begin{proposition}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1122
     @{term "bsimp (bsimp r) = bsimp r"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1123
     \end{proposition}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1124
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1125
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1126
     This can be proved by induction on @{text r} but requires a detailed analysis
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1127
     that the de-nesting of alternatives always results in a flat list of regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1128
     expressions. We omit the details since it does not concern the correctness proof.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1129
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1130
     Next we can include simplification after each derivative step leading to the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1131
     following notion of bitcoded derivatives:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1132
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1133
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1134
      \begin{tabular}{cc}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1135
      \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1136
      @{thm (lhs) bders_simp.simps(1)} & $\dn$ & @{thm (rhs) bders_simp.simps(1)}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1137
      \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1138
      &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1139
      \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1140
      @{thm (lhs) bders_simp.simps(2)} & $\dn$ & @{thm (rhs) bders_simp.simps(2)}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1141
      \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1142
      \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1143
      \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1144
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1145
      \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1146
      and use it in the improved lexing algorithm defined as
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1147
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1148
     \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1149
\begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1150
  $\textit{blexer}^+\;r\,s$ & $\dn$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1151
      $\textit{let}\;r_{der} = (r^\uparrow)\backslash_{bsimp}\, s\;\textit{in}$                
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1152
      $\;\;\textit{if}\; \textit{bnullable}(r_{der}) \;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,r_{der})\,r
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1153
       \;\;\textit{else}\;\textit{None}$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1154
\end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1155
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1156
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1157
       \noindent The remaining task is to show that @{term blexer} and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1158
       @{term "blexer_simp"} generate the same answers.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1159
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1160
       When we first
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1161
       attempted this proof we encountered a problem with the idea
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1162
       in Sulzmann and Lu's paper where the argument seems to be to appeal
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1163
       again to the @{text retrieve}-function defined for the unsimplified version
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1164
       of the algorithm. But
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1165
       this does not work, because desirable properties such as
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1166
     %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1167
     \[
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1168
     @{text "retrieve r v = retrieve (bsimp r) v"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1169
     \]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1170
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1171
     \noindent do not hold under simplification---this property
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1172
     essentially purports that we can retrieve the same value from a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1173
     simplified version of the regular expression. To start with @{text retrieve}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1174
     depends on the fact that the value @{text v} corresponds to the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1175
     structure of the regular expression @{text r}---but the whole point of simplification
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1176
     is to ``destroy'' this structure by making the regular expression simpler.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1177
     To see this consider the regular expression @{term "r = ALT r' ZERO"} and a corresponding
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1178
     value @{text "v = Left v'"}. If we annotate bitcodes to @{text "r"}, then 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1179
     we can use @{text retrieve} with @{text r} and @{text v} in order to extract a corresponding
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1180
     bitsequence. The reason that this works is that @{text r} is an alternative
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1181
     regular expression and @{text v} a corresponding @{text "Left"}-value. However, if we simplify
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1182
     @{text r}, then @{text v} does not correspond to the shape of the regular 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1183
     expression anymore. So unless one can somehow
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1184
     synchronise the change in the simplified regular expressions with
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1185
     the original POSIX value, there is no hope of appealing to @{text retrieve} in the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1186
     correctness argument for @{term blexer_simp}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1187
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1188
     We found it more helpful to introduce the rewriting systems shown in
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1189
     Figure~\ref{SimpRewrites}. The idea is to generate 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1190
     simplified regular expressions in small steps (unlike the @{text bsimp}-function which
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1191
     does the same in a big step), and show that each of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1192
     the small steps preserves the bitcodes that lead to the final POSIX value.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1193
     The rewrite system is organised such that $\leadsto$ is for bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1194
     expressions and $\stackrel{s}{\leadsto}$ for lists of bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1195
     expressions. The former essentially implements the simplifications of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1196
     @{text "bsimpSEQ"} and @{text flts}; while the latter implements the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1197
     simplifications in @{text "bsimpALTs"}. We can show that any bitcoded
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1198
     regular expression reduces in zero or more steps to the simplified
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1199
     regular expression generated by @{text bsimp}:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1200
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1201
     \begin{lemma}\label{lemone}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1202
     @{thm[mode=IfThen] rewrites_to_bsimp}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1203
     \end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1204
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1205
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1206
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1207
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1208
     We can show that this rewrite system preserves @{term bnullable}, that 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1209
     is simplification does not affect nullability:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1210
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1211
     \begin{lemma}\label{lembnull}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1212
     @{thm[mode=IfThen] bnullable0(1)[of "r\<^sub>1" "r\<^sub>2"]}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1213
     \end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1214
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1215
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1216
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1217
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1218
     From this, we can show that @{text bmkeps} will produce the same bitsequence
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1219
     as long as one of the bitcoded regular expressions in $\leadsto$ is nullable (this lemma
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1220
     establishes the missing fact we were not able to establish using @{text retrieve}, as suggested
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1221
     in the paper by Sulzmannn and Lu). 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1222
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1223
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1224
     \begin{lemma}\label{lemthree}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1225
     @{thm[mode=IfThen] rewrite_bmkeps_aux(1)[of "r\<^sub>1" "r\<^sub>2"]}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1226
     \end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1227
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1228
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1229
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1230
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1231
     Crucial is also the fact that derivative steps and simplification steps can be interleaved,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1232
     which is shown by the fact that $\leadsto$ is preserved under derivatives.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1233
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1234
     \begin{lemma}\label{bderlem}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1235
     @{thm[mode=IfThen] rewrite_preserves_bder(1)[of "r\<^sub>1" "r\<^sub>2"]}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1236
     \end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1237
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1238
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1239
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1240
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1241
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1242
     Using this fact together with Lemma~\ref{lemone} allows us to prove the central lemma
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1243
     that the unsimplified
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1244
     derivative (with a string @{term s}) reduces to the simplified derivative (with the same string).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1245
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1246
     \begin{lemma}\label{lemtwo}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1247
     @{thm[mode=IfThen] central}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1248
     \end{lemma}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1249
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1250
     %\begin{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1251
     %By reverse induction on @{term s} generalising over @{text r}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1252
     %\end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1253
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1254
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1255
     With these lemmas in place we can finally establish that @{term "blexer_simp"} and @{term "blexer"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1256
     generate the same value, and using Theorem~\ref{thmone} from the previous section that this value
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1257
     is indeed the POSIX value.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1258
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1259
     \begin{theorem}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1260
     @{thm[mode=IfThen] main_blexer_simp}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1261
     \end{theorem}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1262
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1263
     %\begin{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1264
     %By unfolding the definitions and using Lemmas~\ref{lemtwo} and \ref{lemthree}. 	
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1265
     %\end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1266
     
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1267
     \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1268
     This means that if the algorithm is called with a regular expression @{term r} and a string
498
ab626b60ee64 fixed tiny typo in the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 496
diff changeset
  1269
     @{term s} with $@{term s} \in L(@{term r})$, it will return @{term "Some v"} for the unique
496
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1270
     @{term v} we defined by the POSIX relation $(@{term s}, @{term r}) \rightarrow @{term v}$; otherwise
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1271
     the algorithm returns @{term "None"} when $s \not\in L(r)$ and no such @{text v} exists.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1272
     This completes the correctness proof for the second POSIX lexing algorithm by Sulzmann and Lu.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1273
     The interesting point of this algorithm is that the sizes of derivatives do not grow arbitrarily big but
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1274
     can be finitely bounded, which
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1275
     we shall show next.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1276
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1277
   \begin{figure}[t]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1278
  \begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1279
  \begin{tabular}{@ {\hspace{-8mm}}c@ {}}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1280
  @{thm[mode=Axiom] bs1[of _ "r\<^sub>2"]}$S\ZERO{}_l$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1281
  @{thm[mode=Axiom] bs2[of _ "r\<^sub>1"]}$S\ZERO{}_r$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1282
  @{thm[mode=Axiom] bs3[of "bs\<^sub>1" "bs\<^sub>2"]}$S\ONE$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1283
  @{thm[mode=Rule] bs4[of "r\<^sub>1" "r\<^sub>2" _ "r\<^sub>3"]}SL\qquad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1284
  @{thm[mode=Rule] bs5[of "r\<^sub>3" "r\<^sub>4" _ "r\<^sub>1"]}SR\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1285
  @{thm[mode=Axiom] bs6}$A0$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1286
  @{thm[mode=Axiom] bs7}$A1$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1287
  @{thm[mode=Rule] bs10[of "rs\<^sub>1" "rs\<^sub>2"]}$AL$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1288
  @{thm[mode=Rule] ss2[of "rs\<^sub>1" "rs\<^sub>2"]}$LT$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1289
  @{thm[mode=Rule] ss3[of "r\<^sub>1" "r\<^sub>2"]}$LH$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1290
  @{thm[mode=Axiom] ss4}$L\ZERO$\quad
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1291
  @{thm[mode=Axiom] ss5[of "bs" "rs\<^sub>1" "rs\<^sub>2"]}$LS$\medskip\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1292
  @{thm[mode=Rule] ss6[of "r\<^sub>2" "r\<^sub>1" "rs\<^sub>1" "rs\<^sub>2" "rs\<^sub>3"]}$LD$\\[-5mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1293
  \end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1294
  \end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1295
  \caption{The rewrite rules that generate simplified regular expressions
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1296
  in small steps: @{term "rrewrite r\<^sub>1 r\<^sub>2"} is for bitcoded regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1297
  expressions and @{term "srewrite rs\<^sub>1 rs\<^sub>2"} for \emph{lists} of bitcoded
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1298
  expressions. Interesting is the $LD$ rule that allows copies of regular
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1299
  expressions to be removed provided a regular expression earlier in the list can
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1300
  match the same strings.}\label{SimpRewrites}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1301
  \end{figure}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1302
*}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1303
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1304
section {* Finiteness of Derivatives *}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1305
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1306
text {*
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1307
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1308
In this section let us sketch our argument for why the size of the simplified
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1309
derivatives with the aggressive simplification function can be finitely bounded. Suppose
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1310
we have a size function for bitcoded regular expressions, written
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1311
$\llbracket r\rrbracket$, which counts the number of nodes if we regard $r$ as a tree
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1312
(we omit the precise definition; ditto for lists $\llbracket r\!s\rrbracket$). For this we show that for every $r$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1313
there exists a bound $N$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1314
such that 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1315
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1316
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1317
$\forall s. \; \llbracket@{term "bders_simp r s"}\rrbracket \leq N$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1318
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1319
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1320
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1321
We prove this by induction on $r$. The base cases for @{term AZERO},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1322
@{term "AONE bs"} and @{term "ACHAR bs c"} are straightforward. The interesting case is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1323
for sequences of the form @{term "ASEQ bs r\<^sub>1 r\<^sub>2"}. In this case our induction
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1324
hypotheses state $\exists N_1. \forall s. \; \llbracket{}@{term "bders_simp r\<^sub>1 s"}\rrbracket \leq N_1$ and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1325
$\exists N_2. \forall s. \; \llbracket@{term "bders_simp r\<^sub>2 s"}\rrbracket \leq N_2$. We can reason as follows
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1326
%
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1327
\begin{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1328
\begin{tabular}{lcll}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1329
& & $ \llbracket@{term "bders_simp (ASEQ bs r\<^sub>1 r\<^sub>2) s"}\rrbracket$\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1330
& $ = $ & $\llbracket bsimp\,(\textit{ALTs}\;bs\;((@{term "ASEQ [] (bders_simp r\<^sub>1 s) r\<^sub>2"}) ::
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1331
    [@{term "bders_simp r\<^sub>2 s'"} \;|\; s' \in \textit{Suffix}(@{text r}_1, s)]))\rrbracket $ & (1) \\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1332
& $\leq$ &
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1333
    $\llbracket\textit{distinctWith}\,((@{term "ASEQ [] (bders_simp r\<^sub>1 s) r\<^sub>2"}) ::
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1334
    [@{term "bders_simp r\<^sub>2 s'"} \;|\; s' \in \textit{Suffix}(@{text r}_1, s)])\,\approx\;@{term "{}"}\rrbracket + 1 $ & (2) \\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1335
& $\leq$ & $\llbracket@{term "ASEQ [] (bders_simp r\<^sub>1 s) r\<^sub>2"}\rrbracket +
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1336
             \llbracket\textit{distinctWith}\,[@{term "bders_simp r\<^sub>2 s'"} \;|\; s' \in \textit{Suffix}(@{text r}_1, s)]\,\approx\;@{term "{}"}\rrbracket + 1 $ & (3) \\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1337
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 +
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1338
      \llbracket\textit{distinctWith}\,[@{term "bders_simp r\<^sub>2 s'"} \;|\; s' \in \textit{Suffix}(@{text r}_1, s)]\,\approx\;@{term "{}"}\rrbracket$ & (4)\\
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1339
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 + l_{N_{2}} * N_{2}$ & (5)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1340
\end{tabular}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1341
\end{center}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1342
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1343
% tell Chengsong about Indian paper of closed forms of derivatives
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1344
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1345
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1346
where in (1) the $\textit{Suffix}(@{text "r"}_1, s)$ are the all the suffixes of $s$ where @{term "bders_simp r\<^sub>1 s'"} is nullable ($s'$ being a suffix of $s$).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1347
In (3) we know that  $\llbracket@{term "ASEQ [] (bders_simp r\<^sub>1 s) r\<^sub>2"}\rrbracket$ is 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1348
bounded by $N_1 + \llbracket{}r_2\rrbracket + 1$. In (5) we know the list comprehension contains only regular expressions of size smaller
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1349
than $N_2$. The list length after @{text distinctWith} is bounded by a number, which we call $l_{N_2}$. It stands
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1350
for the number of distinct regular expressions smaller than $N_2$ (there can only be finitely many of them).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1351
We reason similarly for @{text STAR}.\medskip
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1352
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1353
\noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1354
Clearly we give in this finiteness argument (Step (5)) a very loose bound that is
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1355
far from the actual bound we can expect. We can do better than this, but this does not improve
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1356
the finiteness property we are proving. If we are interested in a polynomial bound,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1357
one would hope to obtain a similar tight bound as for partial
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1358
derivatives introduced by Antimirov \cite{Antimirov95}. After all the idea with
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1359
@{text distinctWith} is to maintain a ``set'' of alternatives (like the sets in
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1360
partial derivatives). Unfortunately to obtain the exact same bound would mean
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1361
we need to introduce simplifications, such as
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1362
 $(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1363
which exist for partial derivatives. However, if we introduce them in our
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1364
setting we would lose the POSIX property of our calculated values. We leave better
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1365
bounds for future work.\\[-6.5mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1366
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1367
*}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1368
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1369
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1370
section {* Conclusion *}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1371
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1372
text {*
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1373
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1374
   We set out in this work to prove in Isabelle/HOL the correctness of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1375
   the second POSIX lexing algorithm by Sulzmann and Lu
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1376
   \cite{Sulzmann2014}. This follows earlier work where we established
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1377
   the correctness of the first algorithm
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1378
   \cite{AusafDyckhoffUrban2016}. In the earlier work we needed to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1379
   introduce our own specification about what POSIX values are,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1380
   because the informal definition given by Sulzmann and Lu did not
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1381
   stand up to a formal proof. Also for the second algorithm we needed
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1382
   to introduce our own definitions and proof ideas in order to establish the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1383
   correctness.  Our interest in the second algorithm 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1384
   lies in the fact that by using bitcoded regular expressions and an aggressive
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1385
   simplification method there is a chance that the derivatives
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1386
   can be kept universally small  (we established in this paper that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1387
   they can be kept finitely bounded for any string). This is important if one is after
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1388
   an efficient POSIX lexing algorithm based on derivatives.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1389
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1390
   Having proved the correctness of the POSIX lexing algorithm, which
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1391
   lessons have we learned? Well, we feel this is a very good example
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1392
   where formal proofs give further insight into the matter at
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1393
   hand. For example it is very hard to see a problem with @{text nub}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1394
   vs @{text distinctWith} with only experimental data---one would still
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1395
   see the correct result but find that simplification does not simplify in well-chosen, but not
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1396
   obscure, examples. We found that from an implementation
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1397
   point-of-view it is really important to have the formal proofs of
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1398
   the corresponding properties at hand.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1399
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1400
   We have also developed a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1401
   healthy suspicion when experimental data is used to back up
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1402
   efficiency claims. For example Sulzmann and Lu write about their
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1403
   equivalent of @{term blexer_simp} \textit{``...we can incrementally compute
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1404
   bitcoded parse trees in linear time in the size of the input''}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1405
   \cite[Page 14]{Sulzmann2014}. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1406
   Given the growth of the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1407
   derivatives in some cases even after aggressive simplification, this
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1408
   is a hard to believe claim. A similar claim about a theoretical runtime
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1409
   of @{text "O(n\<^sup>2)"} is made for the Verbatim lexer, which calculates
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1410
   tokens according to POSIX rules~\cite{verbatim}. For this Verbatim uses Brzozowski's
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1411
   derivatives like in our work. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1412
   The authors write: \textit{``The results of our empirical tests [..] confirm that Verbatim has
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1413
   @{text "O(n\<^sup>2)"} time complexity.''} \cite[Section~VII]{verbatim}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1414
   While their correctness proof for Verbatim is formalised in Coq, the claim about
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1415
   the runtime complexity is only supported by some emperical evidence obtained
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1416
   by using the code extraction facilities of Coq.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1417
   Given our observation with the ``growth problem'' of derivatives,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1418
   we
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1419
   tried out their extracted OCaml code with the example
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1420
   \mbox{@{text "(a + aa)\<^sup>*"}} as a single lexing rule, and it took for us around 5 minutes to tokenise a
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1421
   string of 40 $a$'s and that increased to approximately 19 minutes when the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1422
   string is 50 $a$'s long. Taking into account that derivatives are not simplified in the Verbatim
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1423
   lexer, such numbers are not surprising. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1424
   Clearly our result of having finite
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1425
   derivatives might sound rather weak in this context but we think such effeciency claims
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1426
   really require further scrutiny.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1427
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1428
   The contribution of this paper is to make sure
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1429
   derivatives do not grow arbitrarily big (universially) In the example \mbox{@{text "(a + aa)\<^sup>*"}},
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1430
   \emph{all} derivatives have a size of 17 or less. The result is that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1431
   lexing a string of, say, 50\,000 a's with the regular expression \mbox{@{text "(a + aa)\<^sup>*"}} takes approximately
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1432
   10 seconds with our Scala implementation
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1433
   of the presented algorithm. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1434
   \smallskip
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1435
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1436
   \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1437
   Our Isabelle code including the results from Sec.~5 is available from \url{https://github.com/urbanchr/posix}.\\[-10mm]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1438
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1439
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1440
%%\bibliographystyle{plain}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1441
\bibliography{root}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1442
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1443
\appendix
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1444
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1445
\section{Some Proofs}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1446
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1447
While we have formalised \emph{all} results in Isabelle/HOL, below we shall give some
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1448
rough details of our reasoning in ``English''.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1449
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1450
\begin{proof}[Proof of Lemma~\ref{codedecode}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1451
  This follows from the property that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1452
  $\textit{decode}'\,((\textit{code}\,v) \,@\, bs)\,r = (v, bs)$ holds
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1453
  for any bit-sequence $bs$ and $\vdash v : r$. This property can be
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1454
  easily proved by induction on $\vdash v : r$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1455
\end{proof}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1456
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1457
\begin{proof}[Proof of Lemma~\ref{mainlemma}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1458
  This can be proved by induction on $s$ and generalising over
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1459
  $v$. The interesting point is that we need to prove this in the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1460
  reverse direction for $s$. This means instead of cases $[]$ and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1461
  $c\!::\!s$, we have cases $[]$ and $s\,@\,[c]$ where we unravel the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1462
  string from the back.\footnote{Isabelle/HOL provides an induction principle
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1463
    for this way of performing the induction.}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1464
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1465
  The case for $[]$ is routine using Lemmas~\ref{codedecode}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1466
  and~\ref{retrievecode}. In the case $s\,@\,[c]$, we can infer from
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1467
  the assumption that $\vdash v : (r\backslash s)\backslash c$
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1468
  holds. Hence by Prop.~\ref{Posix2} we know that 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1469
  (*) $\vdash \inj\,(r\backslash s)\,c\,v : r\backslash s$ holds too.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1470
  By definition of $\textit{flex}$ we can unfold the left-hand side
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1471
  to be
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1472
  \[
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1473
    \textit{Some}\,(\textit{flex}\;r\,\textit{id}\,(s\,@\,[c])\,v) =
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1474
    \textit{Some}\,(\textit{flex}\;r\,\textit{id}\,s\,(\inj\,(r\backslash s)\,c\,v))  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1475
  \]  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1476
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1477
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1478
  By IH and (*) we can rewrite the right-hand side to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1479
  $\textit{decode}\,(\textit{retrieve}\,(r^\uparrow\backslash s)\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1480
    (\inj\,(r\backslash s)\,c\,\,v))\,r$ which is equal to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1481
  $\textit{decode}\,(\textit{retrieve}\, (r^\uparrow\backslash
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1482
  (s\,@\,[c]))\,v)\,r$ as required. The last rewrite step is possible
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1483
  because we generalised over $v$ in our induction.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1484
\end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1485
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1486
\begin{proof}[Proof of Theorem~\ref{thmone}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1487
  We can first expand both sides using Lem.~\ref{flex} and the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1488
  definition of \textit{blexer}. This gives us two
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1489
  \textit{if}-statements, which we need to show to be equal. By 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1490
  Lemma~\ref{bnullable}\textit{(2)} we know the \textit{if}-tests coincide:
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1491
  $\textit{bnullable}(r^\uparrow\backslash s) \;\textit{iff}\;
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1492
   \nullable(r\backslash s)$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1493
  For the \textit{if}-branch suppose $r_d \dn r^\uparrow\backslash s$ and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1494
  $d \dn r\backslash s$. We have (*) @{text "nullable d"}. We can then show
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1495
  by Lemma~\ref{bnullable}\textit{(3)} that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1496
  %
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1497
  \[
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1498
    \textit{decode}(\textit{bmkeps}\:r_d)\,r =
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1499
    \textit{decode}(\textit{retrieve}\,r_d\,(\textit{mkeps}\,d))\,r
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1500
  \]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1501
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1502
  \noindent
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1503
  where the right-hand side is equal to
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1504
  $\textit{Some}\,(\textit{flex}\,r\,\textit{id}\,s\,(\textit{mkeps}\,
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1505
  d))$ by Lemma~\ref{mainlemma} (we know
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1506
  $\vdash \textit{mkeps}\,d : d$ by (*)).  This shows the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1507
  \textit{if}-branches return the same value. In the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1508
  \textit{else}-branches both \textit{lexer} and \textit{blexer} return
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1509
  \textit{None}. Therefore we can conclude the proof.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1510
\end{proof}  
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1511
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1512
\begin{proof}[Proof of Lemma~\ref{lemone}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1513
     By induction on @{text r}. For this we can use the properties
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1514
     @{thm fltsfrewrites} and @{text "rs"}$\;\stackrel{s}{\leadsto}^*$@{text "distinctWith rs \<approx>"} @{term "{}"}. The latter uses
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1515
     repeated applications of the $LD$ rule which allows the removal
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1516
     of duplicates that can recognise the same strings. 
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1517
     \end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1518
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1519
    \begin{proof}[Proof of Lemma~\ref{lembnull}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1520
     Straightforward mutual induction on the definition of $\leadsto$ and $\stackrel{s}{\leadsto}$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1521
     The only interesting case is the rule $LD$ where the property holds since by the side-conditions of that rule the empty string will
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1522
     be in both @{text "L (rs\<^sub>a @ [r\<^sub>1] @ rs\<^sub>b @ [r\<^sub>2] @ rs\<^sub>c)"} and
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1523
     @{text "L (rs\<^sub>a @ [r\<^sub>1] @ rs\<^sub>b @ rs\<^sub>c)"}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1524
     \end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1525
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1526
 \begin{proof}[Proof of Lemma \ref{lemthree}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1527
     By straightforward mutual induction on the definition of $\leadsto$ and $\stackrel{s}{\leadsto}$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1528
     Again the only interesting case is the rule $LD$ where we need to ensure that
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1529
     @{text "bmkeps (rs\<^sub>a @ [r\<^sub>1] @ rs\<^sub>b @ [r\<^sub>2] @ rs\<^sub>c) =
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1530
        bmkeps (rs\<^sub>a @ [r\<^sub>1] @ rs\<^sub>b @ rs\<^sub>c)"} holds.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1531
     This is indeed the case because according to the POSIX rules the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1532
     generated bitsequence is determined by the first alternative that can match the
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1533
     string (in this case being nullable).
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1534
     \end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1535
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1536
     \begin{proof}[Proof of Lemma~\ref{bderlem}]
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1537
     By straightforward mutual induction on the definition of $\leadsto$ and $\stackrel{s}{\leadsto}$.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1538
     The case for $LD$ holds because @{term "L (erase (bder c r\<^sub>2)) \<subseteq> L (erase (bder c r\<^sub>1))"}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1539
     if and only if @{term "L (erase (r\<^sub>2)) \<subseteq> L (erase (r\<^sub>1))"}.
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1540
     \end{proof}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1541
*}
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1542
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1543
(*<*)
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1544
end
f493a20feeb3 updated to include the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
  1545
(*>*)