coursework/cw05.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 14 Sep 2016 16:51:04 +0100
changeset 419 4110ab35e5d8
parent 333 8890852e18b7
child 500 c502933be072
permissions -rw-r--r--
updated courseworks
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
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
     9
\noindent This coursework is worth 20\% and is due on 13 December at
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    10
16:00. You are asked to prove the correctness of the regular
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    11
expression matcher from the lectures using the Isabelle theorem
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    12
prover. You need to submit a theory file containing this proof. The
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    13
Isabelle theorem prover is available from
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
\begin{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
\url{http://isabelle.in.tum.de}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
\end{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    19
\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
    20
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
    21
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
    22
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
    23
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
    24
at
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
\begin{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
\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
    28
\end{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
\noindent
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
and also a longer (free) book at
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
\begin{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
\url{http://www.concrete-semantics.org}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
\end{center}
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    37
\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
    38
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
    39
JEdit is documented in
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    40
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    41
\begin{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    42
\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
    43
\end{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    44
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    45
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    46
\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
    47
please feel free to contact me (christian.urban@kcl.ac.uk). I
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    48
am one of the main developers of Isabelle and have used it for
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    49
approximately the 16 years. One of the success stories of
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    50
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
    51
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
    52
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
    53
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
    54
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
    55
needed. 
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    58
\subsection*{The Task}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    59
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    60
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
    61
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
    62
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
    63
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
    64
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
    65
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
    66
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
    67
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
    68
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
    69
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
    70
datatype:
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    71
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    72
\begin{lstlisting}[language={},numbers=none]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    73
datatype rexp =
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    74
  ZERO
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    75
| ONE
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    76
| CHAR char
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    77
| SEQ rexp rexp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    78
| ALT rexp rexp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    79
| STAR rexp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    80
\end{lstlisting}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    81
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    82
\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
    83
usual:
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    84
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    85
\begin{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    86
\begin{tabular}{rcl@{\hspace{10mm}}l}
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    87
$L(\ZERO)$  & $\dn$ & $\varnothing$   & \pcode{ZERO}\\
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    88
$L(\ONE)$     & $\dn$ & $\{[]\}$        & \pcode{ONE}\\ 
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    89
$L(c)$            & $\dn$ & $\{[c]\}$       & \pcode{CHAR}\\
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    90
$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
    91
$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
    92
$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
    93
\end{tabular}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    94
\end{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    95
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
    96
\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
    97
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
    98
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
    99
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
   100
therefore 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   101
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   102
\begin{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   103
\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
   104
\end{center}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   105
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   106
\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
   107
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
   108
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
   109
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
   110
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
   111
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
   112
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
   113
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
   114
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
   115
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
   116
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   117
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
   118
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
   119
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   120
\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
   121
theorem 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   122
  "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
   123
\end{lstlisting}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   124
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   125
\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
   126
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
   127
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
   128
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
   129
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
   130
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
   131
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   132
\begin{figure}[p]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   133
\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
   134
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   135
  nullable :: "rexp $\Rightarrow$ bool"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   136
where
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   137
  "nullable ZERO = False"
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   138
| "nullable ONE = True"
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   139
| "nullable (CHAR _) = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   140
| "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
   141
| "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
   142
| "nullable (STAR _) = True"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   143
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   144
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   145
  der :: "char $\Rightarrow$ rexp $\Rightarrow$ rexp"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   146
where
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   147
  "der c ZERO = ZERO"
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   148
| "der c ONE = ZERO"
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   149
| "der c (CHAR d) = (if c = d then ONE else ZERO)"
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   150
| "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
   151
| "der c (SEQ r1 r2) = 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   152
     (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
   153
                       else SEQ (der c r1) r2)"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   154
| "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
   155
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   156
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   157
  ders :: "rexp $\Rightarrow$ string $\Rightarrow$ rexp"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   158
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   159
  "ders r [] = r"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   160
| "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
   161
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   162
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   163
  matches :: "rexp $\Rightarrow$ string $\Rightarrow$ bool"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   164
where
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   165
  "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
   166
\end{lstlisting}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   167
\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
   168
Isabelle.\label{matcher}}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   169
\end{figure}
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   170
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   171
\begin{figure}[p]
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   172
\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
   173
fun 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   174
  zeroable :: "rexp $\Rightarrow$ bool"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   175
where
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   176
  "zeroable ZERO = True"
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   177
| "zeroable ONE = False"
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   178
| "zeroable (CHAR _) = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   179
| "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
   180
| "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
   181
| "zeroable (STAR _) = False"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   182
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   183
lemma
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   184
  "zeroable r $\longleftrightarrow$ L r = {}"
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   185
proof (induct)
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   186
  case (ZERO)
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   187
  have "zeroable ZERO" "L ZERO = {}" by simp_all
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   188
  then show "zeroable ZERO $\longleftrightarrow$ (L ZERO = {})" by simp
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   189
next
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   190
  case (ONE)
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   191
  have "$\neg$ zeroable ONE" "L ONE = {[]}" by simp_all
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   192
  then show "zeroable ONE $\longleftrightarrow$ (L ONE = {})" by simp
260
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   193
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   194
  case (CHAR c)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   195
  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
   196
  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
   197
next 
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   198
  case (ALT r1 r2)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   199
  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
   200
  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
   201
  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
   202
    using ih1 ih2 by simp
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   203
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   204
  case (SEQ r1 r2)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   205
  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
   206
  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
   207
  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
   208
    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
   209
next
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   210
  case (STAR r)
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   211
  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
   212
  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
   213
    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
   214
qed
65d1ea0e989f updated cws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 253
diff changeset
   215
\end{lstlisting}
317
a61b50c5d57f updated all
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
   216
\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
   217
\end{figure}
253
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
\end{document}
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
%%% Local Variables: 
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
%%% mode: latex
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
%%% TeX-master: t
75c469893514 added coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
%%% End: