9742
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
10267
|
3 |
\def\isabellecontext{case{\isacharunderscore}exprs}%
|
9742
|
4 |
%
|
10878
|
5 |
\isamarkupsubsection{Case Expressions%
|
10397
|
6 |
}
|
9742
|
7 |
%
|
|
8 |
\begin{isamarkuptext}%
|
|
9 |
\label{sec:case-expressions}
|
11428
|
10 |
HOL also features \sdx{case}-expressions for analyzing
|
9742
|
11 |
elements of a datatype. For example,
|
|
12 |
\begin{isabelle}%
|
10187
|
13 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ y%
|
9924
|
14 |
\end{isabelle}
|
10187
|
15 |
evaluates to \isa{{\isadigit{1}}} if \isa{xs} is \isa{{\isacharbrackleft}{\isacharbrackright}} and to \isa{y} if
|
9792
|
16 |
\isa{xs} is \isa{y\ {\isacharhash}\ ys}. (Since the result in both branches must be of
|
|
17 |
the same type, it follows that \isa{y} is of type \isa{nat} and hence
|
|
18 |
that \isa{xs} is of type \isa{nat\ list}.)
|
9742
|
19 |
|
|
20 |
In general, if $e$ is a term of the datatype $t$ defined in
|
|
21 |
\S\ref{sec:general-datatype} above, the corresponding
|
|
22 |
\isa{case}-expression analyzing $e$ is
|
|
23 |
\[
|
|
24 |
\begin{array}{rrcl}
|
|
25 |
\isa{case}~e~\isa{of} & C@1~x@ {11}~\dots~x@ {1k@1} & \To & e@1 \\
|
|
26 |
\vdots \\
|
|
27 |
\mid & C@m~x@ {m1}~\dots~x@ {mk@m} & \To & e@m
|
|
28 |
\end{array}
|
|
29 |
\]
|
|
30 |
|
|
31 |
\begin{warn}
|
|
32 |
\emph{All} constructors must be present, their order is fixed, and nested
|
|
33 |
patterns are not supported. Violating these restrictions results in strange
|
|
34 |
error messages.
|
|
35 |
\end{warn}
|
|
36 |
\noindent
|
|
37 |
Nested patterns can be simulated by nested \isa{case}-expressions: instead
|
|
38 |
of
|
|
39 |
\begin{isabelle}%
|
10187
|
40 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}{\isachargreater}\ {\isadigit{1}}\ {\isacharbar}\ {\isacharbrackleft}x{\isacharbrackright}\ {\isacharequal}{\isachargreater}\ x\ {\isacharbar}\ x\ {\isacharhash}\ {\isacharparenleft}y\ {\isacharhash}\ zs{\isacharparenright}\ {\isacharequal}{\isachargreater}\ y%
|
9924
|
41 |
\end{isabelle}
|
9834
|
42 |
write
|
|
43 |
\begin{isabelle}%
|
10187
|
44 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isadigit{1}}\isanewline
|
10950
|
45 |
\isaindent{\ \ \ \ \ }{\isacharbar}\ x\ {\isacharhash}\ ys\ {\isasymRightarrow}\ case\ ys\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ x\ {\isacharbar}\ y\ {\isacharhash}\ zs\ {\isasymRightarrow}\ y%
|
9924
|
46 |
\end{isabelle}
|
9742
|
47 |
|
|
48 |
Note that \isa{case}-expressions may need to be enclosed in parentheses to
|
|
49 |
indicate their scope%
|
|
50 |
\end{isamarkuptext}%
|
|
51 |
%
|
10878
|
52 |
\isamarkupsubsection{Structural Induction and Case Distinction%
|
10397
|
53 |
}
|
9742
|
54 |
%
|
|
55 |
\begin{isamarkuptext}%
|
10824
|
56 |
\label{sec:struct-ind-case}
|
9742
|
57 |
\indexbold{structural induction}
|
|
58 |
\indexbold{induction!structural}
|
|
59 |
\indexbold{case distinction}
|
|
60 |
Almost all the basic laws about a datatype are applied automatically during
|
11428
|
61 |
simplification. Only induction is invoked by hand via \methdx{induct_tac},
|
9742
|
62 |
which works for any datatype. In some cases, induction is overkill and a case
|
|
63 |
distinction over all constructors of the datatype suffices. This is performed
|
11428
|
64 |
by \methdx{case_tac}. A trivial example:%
|
9742
|
65 |
\end{isamarkuptext}%
|
|
66 |
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y{\isacharhash}ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequote}\isanewline
|
|
67 |
\isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac\ xs{\isacharparenright}%
|
|
68 |
\begin{isamarkuptxt}%
|
|
69 |
\noindent
|
|
70 |
results in the proof state
|
10420
|
71 |
\begin{isabelle}%
|
|
72 |
\ {\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
|
|
73 |
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
|
10950
|
74 |
\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%
|
9742
|
75 |
\end{isabelle}
|
|
76 |
which is solved automatically:%
|
|
77 |
\end{isamarkuptxt}%
|
10171
|
78 |
\isacommand{apply}{\isacharparenleft}auto{\isacharparenright}%
|
9742
|
79 |
\begin{isamarkuptext}%
|
|
80 |
Note that we do not need to give a lemma a name if we do not intend to refer
|
10824
|
81 |
to it explicitly in the future.
|
|
82 |
|
|
83 |
\begin{warn}
|
|
84 |
Induction is only allowed on free (or \isasymAnd-bound) variables that
|
|
85 |
should not occur among the assumptions of the subgoal; see
|
|
86 |
\S\ref{sec:ind-var-in-prems} for details. Case distinction
|
|
87 |
(\isa{case{\isacharunderscore}tac}) works for arbitrary terms, which need to be
|
|
88 |
quoted if they are non-atomic. However, apart from \isa{{\isasymAnd}}-bound
|
|
89 |
variables, the terms must not contain variables that are bound outside.
|
|
90 |
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}},
|
|
91 |
\isa{case{\isacharunderscore}tac\ xs} will not work as expected because Isabelle interprets
|
|
92 |
the \isa{xs} as a new free variable distinct from the bound
|
|
93 |
\isa{xs} in the goal.
|
|
94 |
\end{warn}%
|
9742
|
95 |
\end{isamarkuptext}%
|
|
96 |
\end{isabellebody}%
|
|
97 |
%%% Local Variables:
|
|
98 |
%%% mode: latex
|
|
99 |
%%% TeX-master: "root"
|
|
100 |
%%% End:
|