coursework/cw05.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Nov 2015 00:11:10 +0000
changeset 386 31295bb945c6
parent 333 8890852e18b7
child 419 4110ab35e5d8
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{../style}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{../langs}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\begin{document}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
     7
\section*{Coursework (Strand 2)}
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
328
bc03ff3d347c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 317
diff changeset
     9
\noindent This coursework is worth 25\% and is due on 11
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
    10
December at 16:00. You are asked to prove the correctness of 
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
    11
the
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    12
regular expression matcher from the lectures using the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    13
Isabelle theorem prover. You need to submit a theory file
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    14
containing this proof. The Isabelle theorem prover is
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    15
available from 
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
\begin{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
\url{http://isabelle.in.tum.de}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
\end{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    21
\noindent This is an interactive theorem prover, meaning that
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    22
you can make definitions and state properties, and then help
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    23
the system with proving these properties. Sometimes the proofs
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    24
are also automatic. There is a shortish user guide for
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    25
Isabelle, called ``Programming and Proving in Isabelle/HOL''
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    26
at
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
\begin{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
\url{http://isabelle.in.tum.de/documentation.html}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
\end{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
\noindent
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
and also a longer (free) book at
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
\begin{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
\url{http://www.concrete-semantics.org}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
\end{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    39
\noindent The Isabelle theorem prover is operated through the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    40
jEdit IDE, which might not be an editor that is widely known.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    41
JEdit is documented in
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    42
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    43
\begin{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    44
\url{http://isabelle.in.tum.de/dist/Isabelle2014/doc/jedit.pdf}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    45
\end{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    46
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    47
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    48
\noindent If you need more help or you are stuck somewhere,
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    49
please feel free to contact me (christian.urban@kcl.ac.uk). I
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    50
am a main developer of Isabelle and have used it for
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    51
approximately the 14 years. One of the success stories of
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    52
Isabelle is the recent verification of a microkernel operating
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    53
system by an Australian group, see \url{http://sel4.systems}.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    54
Their operating system is the only one that has been proved
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    55
correct according to its specification and is used for
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    56
application where high assurance, security and reliability is
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    57
needed. 
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    60
\subsection*{The Task}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    61
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    62
In this coursework you are asked to prove the correctness of
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    63
the regular expression matcher from the lectures in Isabelle.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    64
For this you need to first specify what the matcher is
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    65
supposed to do and then to implement the algorithm. Finally
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    66
you need to prove that the algorithm meets the specification.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    67
The first two parts are relatively easy, because the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    68
definitions in Isabelle will look very similar to the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    69
mathematical definitions from the lectures or the Scala code
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    70
that is supplied at KEATS. For example very similar to Scala,
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    71
regular expressions are defined in Isabelle as an inductive
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    72
datatype:
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    73
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    74
\begin{lstlisting}[language={},numbers=none]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    75
datatype rexp =
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    76
  NULL
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    77
| EMPTY
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    78
| CHAR char
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    79
| SEQ rexp rexp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    80
| ALT rexp rexp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    81
| STAR rexp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    82
\end{lstlisting}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    83
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    84
\noindent The meaning of regular expressions is given as 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    85
usual:
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    86
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    87
\begin{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    88
\begin{tabular}{rcl@{\hspace{10mm}}l}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    89
$L(\varnothing)$  & $\dn$ & $\varnothing$   & \pcode{NULL}\\
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    90
$L(\epsilon)$     & $\dn$ & $\{[]\}$        & \pcode{EMPTY}\\ 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    91
$L(c)$            & $\dn$ & $\{[c]\}$       & \pcode{CHAR}\\
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    92
$L(r_1 + r_2)$     & $\dn$ & $L(r_1) \cup L(r_2)$ & \pcode{ALT}\\
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    93
$L(r_1 \cdot r_2)$ & $\dn$ & $L(r_1) \,@\, L(r_2)$ & \pcode{SEQ}\\
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    94
$L(r^*)$           & $\dn$ & $(L(r))^*$ & \pcode{STAR}\\
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    95
\end{tabular}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    96
\end{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    97
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    98
\noindent You would need to implement this function in order
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    99
to state the theorem about the correctness of the algorithm.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   100
The function $L$ should in Isabelle take a \pcode{rexp} as
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   101
input and return a set of strings. Its type is
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   102
therefore 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   103
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   104
\begin{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   105
\pcode{L} \pcode{::} \pcode{rexp} $\Rightarrow$ \pcode{string set}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   106
\end{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   107
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   108
\noindent Isabelle treats strings as an abbreviation for lists
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   109
of characters. This means you can pattern-match strings like
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   110
lists. The union operation on sets (for the \pcode{ALT}-case)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   111
is a standard definition in Isabelle, but not the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   112
concatenation operation on sets and also not the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   113
star-operation. You would have to supply these definitions.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   114
The concatenation operation can be defined in terms of the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   115
append function, written \code{_ @ _} in Isabelle, for lists.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   116
The star-operation can be defined as a ``big-union'' of 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   117
powers, like in the lectures, or directly as an inductive set.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   118
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   119
The functions for the matcher are shown in
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   120
Figure~\ref{matcher}. The theorem that needs to be proved is
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   121
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   122
\begin{lstlisting}[numbers=none,language={},keywordstyle=\color{black}\ttfamily,mathescape]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   123
theorem 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   124
  "matches r s $\longleftrightarrow$ s $\in$ L r"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   125
\end{lstlisting}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   126
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   127
\noindent which states that the function \emph{matches} is
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   128
true if and only if the string is in the language of the
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   129
regular expression. A proof for this lemma will need
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   130
side-lemmas about \pcode{nullable} and \pcode{der}. An example
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   131
proof in Isabelle that will not be relevant for the theorem
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   132
above is given in Figure~\ref{proof}.
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   133
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   134
\begin{figure}[p]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   135
\begin{lstlisting}[language={},keywordstyle=\color{black}\ttfamily,mathescape]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   136
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   137
  nullable :: "rexp $\Rightarrow$ bool"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   138
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   139
  "nullable NULL = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   140
| "nullable EMPTY = True"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   141
| "nullable (CHAR _) = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   142
| "nullable (ALT r1 r2) = (nullable(r1) $\vee$ nullable(r2))"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   143
| "nullable (SEQ r1 r2) = (nullable(r1) $\wedge$ nullable(r2))"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   144
| "nullable (STAR _) = True"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   145
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   146
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   147
  der :: "char $\Rightarrow$ rexp $\Rightarrow$ rexp"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   148
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   149
  "der c NULL = NULL"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   150
| "der c EMPTY = NULL"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   151
| "der c (CHAR d) = (if c = d then EMPTY else NULL)"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   152
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   153
| "der c (SEQ r1 r2) = 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   154
     (if (nullable r1) then ALT (SEQ (der c r1) r2) (der c r2)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   155
                       else SEQ (der c r1) r2)"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   156
| "der c (STAR r) = SEQ (der c r) (STAR r)"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   157
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   158
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   159
  ders :: "rexp $\Rightarrow$ string $\Rightarrow$ rexp"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   160
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   161
  "ders r [] = r"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   162
| "ders r (c # s) = ders (der c r) s"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   163
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   164
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   165
  matches :: "rexp $\Rightarrow$ string $\Rightarrow$ bool"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   166
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   167
  "matches r s = nullable (ders r s)" 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   168
\end{lstlisting}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   169
\caption{The definition of the matcher algorithm in 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   170
Isabelle.\label{matcher}}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   171
\end{figure}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   172
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   173
\begin{figure}[p]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   174
\begin{lstlisting}[language={},keywordstyle=\color{black}\ttfamily,mathescape]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   175
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   176
  zeroable :: "rexp $\Rightarrow$ bool"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   177
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   178
  "zeroable NULL = True"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   179
| "zeroable EMPTY = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   180
| "zeroable (CHAR _) = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   181
| "zeroable (ALT r1 r2) = (zeroable(r1) $\wedge$ zeroable(r2))"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   182
| "zeroable (SEQ r1 r2) = (zeroable(r1) $\vee$ zeroable(r2))"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   183
| "zeroable (STAR _) = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   184
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   185
lemma
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   186
  "zeroable r $\longleftrightarrow$ L r = {}"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   187
proof (induct)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   188
  case (NULL)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   189
  have "zeroable NULL" "L NULL = {}" by simp_all
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   190
  then show "zeroable NULL $\longleftrightarrow$ (L NULL = {})" by simp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   191
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   192
  case (EMPTY)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   193
  have "$\neg$ zeroable EMPTY" "L EMPTY = {[]}" by simp_all
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   194
  then show "zeroable EMPTY $\longleftrightarrow$ (L EMPTY = {})" by simp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   195
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   196
  case (CHAR c)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   197
  have "$\neg$ zeroable (CHAR c)" "L (CHAR c) = {[c]}" by simp_all
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   198
  then show "zeroable (CHAR c) $\longleftrightarrow$ (L (CHAR c) = {})" by simp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   199
next 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   200
  case (ALT r1 r2)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   201
  have ih1: "zeroable r1 $\longleftrightarrow$ L r1 = {}" by fact
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   202
  have ih2: "zeroable r2 $\longleftrightarrow$ L r2 = {}" by fact
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   203
  show "zeroable (ALT r1 r2) $\longleftrightarrow$ (L (ALT r1 r2) = {})" 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   204
    using ih1 ih2 by simp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   205
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   206
  case (SEQ r1 r2)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   207
  have ih1: "zeroable r1 $\longleftrightarrow$ L r1 = {}" by fact
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   208
  have ih2: "zeroable r2 $\longleftrightarrow$ L r2 = {}" by fact
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   209
  show "zeroable (SEQ r1 r2) $\longleftrightarrow$ (L (SEQ r1 r2) = {})" 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   210
    using ih1 ih2 by (auto simp add: Conc_def)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   211
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   212
  case (STAR r)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   213
  have "$\neg$ zeroable (STAR r)" "[] $\in$ L (r) ^ 0" by simp_all
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   214
  then show "zeroable (STAR r) $\longleftrightarrow$ (L (STAR r) = {})" 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   215
    by (simp (no_asm) add: Star_def) blast
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   216
qed
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   217
\end{lstlisting}
317
a61b50c5d57f updated all
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
   218
\caption{An Isabelle proof about the function \texttt{zeroable}.\label{proof}}
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   219
\end{figure}
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
\end{document}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
%%% Local Variables: 
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
%%% mode: latex
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
%%% TeX-master: t
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
%%% End: