9742
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
10267
|
3 |
\def\isabellecontext{case{\isacharunderscore}exprs}%
|
17056
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
|
10 |
%
|
|
11 |
\endisatagtheory
|
|
12 |
{\isafoldtheory}%
|
|
13 |
%
|
|
14 |
\isadelimtheory
|
|
15 |
%
|
|
16 |
\endisadelimtheory
|
9742
|
17 |
%
|
|
18 |
\begin{isamarkuptext}%
|
25340
|
19 |
\subsection{Case Expressions}
|
11456
|
20 |
\label{sec:case-expressions}\index{*case expressions}%
|
|
21 |
HOL also features \isa{case}-expressions for analyzing
|
9742
|
22 |
elements of a datatype. For example,
|
|
23 |
\begin{isabelle}%
|
12699
|
24 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ y%
|
9924
|
25 |
\end{isabelle}
|
12699
|
26 |
evaluates to \isa{{\isacharbrackleft}{\isacharbrackright}} if \isa{xs} is \isa{{\isacharbrackleft}{\isacharbrackright}} and to \isa{y} if
|
9792
|
27 |
\isa{xs} is \isa{y\ {\isacharhash}\ ys}. (Since the result in both branches must be of
|
12699
|
28 |
the same type, it follows that \isa{y} is of type \isa{{\isacharprime}a\ list} and hence
|
|
29 |
that \isa{xs} is of type \isa{{\isacharprime}a\ list\ list}.)
|
9742
|
30 |
|
25340
|
31 |
In general, case expressions are of the form
|
9742
|
32 |
\[
|
25340
|
33 |
\begin{array}{c}
|
|
34 |
\isa{case}~e~\isa{of}\ pattern@1~\isa{{\isasymRightarrow}}~e@1\ \isa{{\isacharbar}}\ \dots\
|
|
35 |
\isa{{\isacharbar}}~pattern@m~\isa{{\isasymRightarrow}}~e@m
|
9742
|
36 |
\end{array}
|
|
37 |
\]
|
25340
|
38 |
Like in functional programming, patterns are expressions consisting of
|
|
39 |
datatype constructors (e.g. \isa{{\isacharbrackleft}{\isacharbrackright}} and \isa{{\isacharhash}})
|
|
40 |
and variables, including the wildcard ``\verb$_$''.
|
|
41 |
Not all cases need to be covered and the order of cases matters.
|
|
42 |
However, one is well-advised not to wallow in complex patterns because
|
|
43 |
complex case distinctions tend to induce complex proofs.
|
9742
|
44 |
|
|
45 |
\begin{warn}
|
25340
|
46 |
Internally Isabelle only knows about exhaustive case expressions with
|
|
47 |
non-nested patterns: $pattern@i$ must be of the form
|
|
48 |
$C@i~x@ {i1}~\dots~x@ {ik@i}$ and $C@1, \dots, C@m$ must be exactly the
|
|
49 |
constructors of the type of $e$.
|
|
50 |
%
|
|
51 |
More complex case expressions are automatically
|
|
52 |
translated into the simpler form upon parsing but are not translated
|
|
53 |
back for printing. This may lead to surprising output.
|
9742
|
54 |
\end{warn}
|
|
55 |
|
25340
|
56 |
\begin{warn}
|
|
57 |
Like \isa{if}, \isa{case}-expressions may need to be enclosed in
|
|
58 |
parentheses to indicate their scope.
|
|
59 |
\end{warn}
|
|
60 |
|
|
61 |
\subsection{Structural Induction and Case Distinction}
|
10824
|
62 |
\label{sec:struct-ind-case}
|
11456
|
63 |
\index{case distinctions}\index{induction!structural}%
|
|
64 |
Induction is invoked by \methdx{induct_tac}, as we have seen above;
|
|
65 |
it works for any datatype. In some cases, induction is overkill and a case
|
|
66 |
distinction over all constructors of the datatype suffices. This is performed
|
|
67 |
by \methdx{case_tac}. Here is a trivial example:%
|
9742
|
68 |
\end{isamarkuptext}%
|
17175
|
69 |
\isamarkuptrue%
|
|
70 |
\isacommand{lemma}\isamarkupfalse%
|
|
71 |
\ {\isachardoublequoteopen}{\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y{\isacharhash}ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
|
17056
|
72 |
%
|
|
73 |
\isadelimproof
|
|
74 |
%
|
|
75 |
\endisadelimproof
|
|
76 |
%
|
|
77 |
\isatagproof
|
17175
|
78 |
\isacommand{apply}\isamarkupfalse%
|
|
79 |
{\isacharparenleft}case{\isacharunderscore}tac\ xs{\isacharparenright}%
|
16069
|
80 |
\begin{isamarkuptxt}%
|
|
81 |
\noindent
|
|
82 |
results in the proof state
|
|
83 |
\begin{isabelle}%
|
|
84 |
\ {\isadigit{1}}{\isachardot}\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymLongrightarrow}\ {\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs\isanewline
|
|
85 |
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
|
|
86 |
\isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }xs\ {\isacharequal}\ a\ {\isacharhash}\ list\ {\isasymLongrightarrow}\ {\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs%
|
|
87 |
\end{isabelle}
|
|
88 |
which is solved automatically:%
|
|
89 |
\end{isamarkuptxt}%
|
17175
|
90 |
\isamarkuptrue%
|
|
91 |
\isacommand{apply}\isamarkupfalse%
|
17181
|
92 |
{\isacharparenleft}auto{\isacharparenright}%
|
17056
|
93 |
\endisatagproof
|
|
94 |
{\isafoldproof}%
|
|
95 |
%
|
|
96 |
\isadelimproof
|
|
97 |
%
|
|
98 |
\endisadelimproof
|
11866
|
99 |
%
|
9742
|
100 |
\begin{isamarkuptext}%
|
|
101 |
Note that we do not need to give a lemma a name if we do not intend to refer
|
10824
|
102 |
to it explicitly in the future.
|
11456
|
103 |
Other basic laws about a datatype are applied automatically during
|
|
104 |
simplification, so no special methods are provided for them.
|
10824
|
105 |
|
|
106 |
\begin{warn}
|
|
107 |
Induction is only allowed on free (or \isasymAnd-bound) variables that
|
|
108 |
should not occur among the assumptions of the subgoal; see
|
|
109 |
\S\ref{sec:ind-var-in-prems} for details. Case distinction
|
|
110 |
(\isa{case{\isacharunderscore}tac}) works for arbitrary terms, which need to be
|
|
111 |
quoted if they are non-atomic. However, apart from \isa{{\isasymAnd}}-bound
|
|
112 |
variables, the terms must not contain variables that are bound outside.
|
|
113 |
For example, given the goal \isa{{\isasymforall}xs{\isachardot}\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}y\ ys{\isachardot}\ xs\ {\isacharequal}\ y\ {\isacharhash}\ ys{\isacharparenright}},
|
|
114 |
\isa{case{\isacharunderscore}tac\ xs} will not work as expected because Isabelle interprets
|
|
115 |
the \isa{xs} as a new free variable distinct from the bound
|
|
116 |
\isa{xs} in the goal.
|
|
117 |
\end{warn}%
|
9742
|
118 |
\end{isamarkuptext}%
|
17175
|
119 |
\isamarkuptrue%
|
17056
|
120 |
%
|
|
121 |
\isadelimtheory
|
|
122 |
%
|
|
123 |
\endisadelimtheory
|
|
124 |
%
|
|
125 |
\isatagtheory
|
|
126 |
%
|
|
127 |
\endisatagtheory
|
|
128 |
{\isafoldtheory}%
|
|
129 |
%
|
|
130 |
\isadelimtheory
|
|
131 |
%
|
|
132 |
\endisadelimtheory
|
9742
|
133 |
\end{isabellebody}%
|
|
134 |
%%% Local Variables:
|
|
135 |
%%% mode: latex
|
|
136 |
%%% TeX-master: "root"
|
|
137 |
%%% End:
|