9742
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
40406
|
3 |
\def\isabellecontext{case{\isaliteral{5F}{\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}%
|
40406
|
24 |
\ \ \ \ \ case\ xs\ of\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{7C}{\isacharbar}}\ y\ {\isaliteral{23}{\isacharhash}}\ ys\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ y%
|
9924
|
25 |
\end{isabelle}
|
40406
|
26 |
evaluates to \isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} if \isa{xs} is \isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} and to \isa{y} if
|
|
27 |
\isa{xs} is \isa{y\ {\isaliteral{23}{\isacharhash}}\ ys}. (Since the result in both branches must be of
|
|
28 |
the same type, it follows that \isa{y} is of type \isa{{\isaliteral{27}{\isacharprime}}a\ list} and hence
|
|
29 |
that \isa{xs} is of type \isa{{\isaliteral{27}{\isacharprime}}a\ list\ list}.)
|
9742
|
30 |
|
25340
|
31 |
In general, case expressions are of the form
|
9742
|
32 |
\[
|
25340
|
33 |
\begin{array}{c}
|
40406
|
34 |
\isa{case}~e~\isa{of}\ pattern@1~\isa{{\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}}~e@1\ \isa{{\isaliteral{7C}{\isacharbar}}}\ \dots\
|
|
35 |
\isa{{\isaliteral{7C}{\isacharbar}}}~pattern@m~\isa{{\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}}~e@m
|
9742
|
36 |
\end{array}
|
|
37 |
\]
|
25340
|
38 |
Like in functional programming, patterns are expressions consisting of
|
40406
|
39 |
datatype constructors (e.g. \isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} and \isa{{\isaliteral{23}{\isacharhash}}})
|
25340
|
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%
|
40406
|
71 |
\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{28}{\isacharparenleft}}case\ xs\ of\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{7C}{\isacharbar}}\ y{\isaliteral{23}{\isacharhash}}ys\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs{\isaliteral{22}{\isachardoublequoteclose}}\isanewline
|
17056
|
72 |
%
|
|
73 |
\isadelimproof
|
|
74 |
%
|
|
75 |
\endisadelimproof
|
|
76 |
%
|
|
77 |
\isatagproof
|
17175
|
78 |
\isacommand{apply}\isamarkupfalse%
|
40406
|
79 |
{\isaliteral{28}{\isacharparenleft}}case{\isaliteral{5F}{\isacharunderscore}}tac\ xs{\isaliteral{29}{\isacharparenright}}%
|
16069
|
80 |
\begin{isamarkuptxt}%
|
|
81 |
\noindent
|
|
82 |
results in the proof state
|
|
83 |
\begin{isabelle}%
|
40406
|
84 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ xs\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}case\ xs\ of\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{7C}{\isacharbar}}\ y\ {\isaliteral{23}{\isacharhash}}\ ys\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs\isanewline
|
|
85 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ list{\isaliteral{2E}{\isachardot}}\isanewline
|
|
86 |
\isaindent{\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }xs\ {\isaliteral{3D}{\isacharequal}}\ a\ {\isaliteral{23}{\isacharhash}}\ list\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}case\ xs\ of\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{7C}{\isacharbar}}\ y\ {\isaliteral{23}{\isacharhash}}\ ys\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs%
|
16069
|
87 |
\end{isabelle}
|
|
88 |
which is solved automatically:%
|
|
89 |
\end{isamarkuptxt}%
|
17175
|
90 |
\isamarkuptrue%
|
|
91 |
\isacommand{apply}\isamarkupfalse%
|
40406
|
92 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\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
|
40406
|
110 |
(\isa{case{\isaliteral{5F}{\isacharunderscore}}tac}) works for arbitrary terms, which need to be
|
|
111 |
quoted if they are non-atomic. However, apart from \isa{{\isaliteral{5C3C416E643E}{\isasymAnd}}}-bound
|
10824
|
112 |
variables, the terms must not contain variables that are bound outside.
|
40406
|
113 |
For example, given the goal \isa{{\isaliteral{5C3C666F72616C6C3E}{\isasymforall}}xs{\isaliteral{2E}{\isachardot}}\ xs\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6578697374733E}{\isasymexists}}y\ ys{\isaliteral{2E}{\isachardot}}\ xs\ {\isaliteral{3D}{\isacharequal}}\ y\ {\isaliteral{23}{\isacharhash}}\ ys{\isaliteral{29}{\isacharparenright}}},
|
|
114 |
\isa{case{\isaliteral{5F}{\isacharunderscore}}tac\ xs} will not work as expected because Isabelle interprets
|
10824
|
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:
|