9742
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
%
|
|
4 |
\isamarkupsubsection{Case expressions}
|
|
5 |
%
|
|
6 |
\begin{isamarkuptext}%
|
|
7 |
\label{sec:case-expressions}
|
|
8 |
HOL also features \isaindexbold{case}-expressions for analyzing
|
|
9 |
elements of a datatype. For example,
|
|
10 |
\begin{quote}
|
|
11 |
|
|
12 |
\begin{isabelle}%
|
|
13 |
case\ \mbox{xs}\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ \isadigit{1}\ {\isacharbar}\ \mbox{y}\ {\isacharhash}\ \mbox{ys}\ {\isasymRightarrow}\ \mbox{y}
|
|
14 |
\end{isabelle}%
|
|
15 |
|
|
16 |
\end{quote}
|
|
17 |
evaluates to \isa{\isadigit{1}} if \isa{\mbox{xs}} is \isa{{\isacharbrackleft}{\isacharbrackright}} and to \isa{\mbox{y}} if
|
|
18 |
\isa{\mbox{xs}} is \isa{\mbox{y}\ {\isacharhash}\ \mbox{ys}}. (Since the result in both branches must be of
|
|
19 |
the same type, it follows that \isa{\mbox{y}} is of type \isa{nat} and hence
|
|
20 |
that \isa{\mbox{xs}} is of type \isa{nat\ list}.)
|
|
21 |
|
|
22 |
In general, if $e$ is a term of the datatype $t$ defined in
|
|
23 |
\S\ref{sec:general-datatype} above, the corresponding
|
|
24 |
\isa{case}-expression analyzing $e$ is
|
|
25 |
\[
|
|
26 |
\begin{array}{rrcl}
|
|
27 |
\isa{case}~e~\isa{of} & C@1~x@ {11}~\dots~x@ {1k@1} & \To & e@1 \\
|
|
28 |
\vdots \\
|
|
29 |
\mid & C@m~x@ {m1}~\dots~x@ {mk@m} & \To & e@m
|
|
30 |
\end{array}
|
|
31 |
\]
|
|
32 |
|
|
33 |
\begin{warn}
|
|
34 |
\emph{All} constructors must be present, their order is fixed, and nested
|
|
35 |
patterns are not supported. Violating these restrictions results in strange
|
|
36 |
error messages.
|
|
37 |
\end{warn}
|
|
38 |
\noindent
|
|
39 |
Nested patterns can be simulated by nested \isa{case}-expressions: instead
|
|
40 |
of
|
|
41 |
% case xs of [] => 1 | [x] => x | x#(y#zs) => y
|
|
42 |
\begin{isabelle}
|
|
43 |
~~~case~xs~of~[]~{\isasymRightarrow}~1~|~[x]~{\isasymRightarrow}~x~|~x~\#~y~\#~zs~{\isasymRightarrow}~y
|
|
44 |
\end{isabelle}
|
|
45 |
write
|
|
46 |
\begin{quote}
|
|
47 |
|
|
48 |
\begin{isabelle}%
|
|
49 |
case\ \mbox{xs}\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ \isadigit{1}\isanewline
|
|
50 |
{\isacharbar}\ \mbox{x}\ {\isacharhash}\ \mbox{ys}\ {\isasymRightarrow}\ case\ \mbox{ys}\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ \mbox{x}\ {\isacharbar}\ \mbox{y}\ {\isacharhash}\ \mbox{zs}\ {\isasymRightarrow}\ \mbox{y}
|
|
51 |
\end{isabelle}%
|
|
52 |
|
|
53 |
\end{quote}
|
|
54 |
|
|
55 |
Note that \isa{case}-expressions may need to be enclosed in parentheses to
|
|
56 |
indicate their scope%
|
|
57 |
\end{isamarkuptext}%
|
|
58 |
%
|
|
59 |
\isamarkupsubsection{Structural induction and case distinction}
|
|
60 |
%
|
|
61 |
\begin{isamarkuptext}%
|
|
62 |
\indexbold{structural induction}
|
|
63 |
\indexbold{induction!structural}
|
|
64 |
\indexbold{case distinction}
|
|
65 |
Almost all the basic laws about a datatype are applied automatically during
|
|
66 |
simplification. Only induction is invoked by hand via \isaindex{induct_tac},
|
|
67 |
which works for any datatype. In some cases, induction is overkill and a case
|
|
68 |
distinction over all constructors of the datatype suffices. This is performed
|
|
69 |
by \isaindexbold{case_tac}. A trivial example:%
|
|
70 |
\end{isamarkuptext}%
|
|
71 |
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y{\isacharhash}ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequote}\isanewline
|
|
72 |
\isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac\ xs{\isacharparenright}%
|
|
73 |
\begin{isamarkuptxt}%
|
|
74 |
\noindent
|
|
75 |
results in the proof state
|
|
76 |
\begin{isabelle}
|
|
77 |
~1.~xs~=~[]~{\isasymLongrightarrow}~(case~xs~of~[]~{\isasymRightarrow}~[]~|~y~\#~ys~{\isasymRightarrow}~xs)~=~xs\isanewline
|
|
78 |
~2.~{\isasymAnd}a~list.~xs=a\#list~{\isasymLongrightarrow}~(case~xs~of~[]~{\isasymRightarrow}~[]~|~y\#ys~{\isasymRightarrow}~xs)~=~xs%
|
|
79 |
\end{isabelle}
|
|
80 |
which is solved automatically:%
|
|
81 |
\end{isamarkuptxt}%
|
|
82 |
\isacommand{by}{\isacharparenleft}auto{\isacharparenright}%
|
|
83 |
\begin{isamarkuptext}%
|
|
84 |
Note that we do not need to give a lemma a name if we do not intend to refer
|
|
85 |
to it explicitly in the future.%
|
|
86 |
\end{isamarkuptext}%
|
|
87 |
\end{isabellebody}%
|
|
88 |
%%% Local Variables:
|
|
89 |
%%% mode: latex
|
|
90 |
%%% TeX-master: "root"
|
|
91 |
%%% End:
|