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 |
%
|
10878
|
18 |
\isamarkupsubsection{Case Expressions%
|
10397
|
19 |
}
|
11866
|
20 |
\isamarkuptrue%
|
9742
|
21 |
%
|
|
22 |
\begin{isamarkuptext}%
|
11456
|
23 |
\label{sec:case-expressions}\index{*case expressions}%
|
|
24 |
HOL also features \isa{case}-expressions for analyzing
|
9742
|
25 |
elements of a datatype. For example,
|
|
26 |
\begin{isabelle}%
|
12699
|
27 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ y%
|
9924
|
28 |
\end{isabelle}
|
12699
|
29 |
evaluates to \isa{{\isacharbrackleft}{\isacharbrackright}} if \isa{xs} is \isa{{\isacharbrackleft}{\isacharbrackright}} and to \isa{y} if
|
9792
|
30 |
\isa{xs} is \isa{y\ {\isacharhash}\ ys}. (Since the result in both branches must be of
|
12699
|
31 |
the same type, it follows that \isa{y} is of type \isa{{\isacharprime}a\ list} and hence
|
|
32 |
that \isa{xs} is of type \isa{{\isacharprime}a\ list\ list}.)
|
9742
|
33 |
|
|
34 |
In general, if $e$ is a term of the datatype $t$ defined in
|
|
35 |
\S\ref{sec:general-datatype} above, the corresponding
|
|
36 |
\isa{case}-expression analyzing $e$ is
|
|
37 |
\[
|
|
38 |
\begin{array}{rrcl}
|
|
39 |
\isa{case}~e~\isa{of} & C@1~x@ {11}~\dots~x@ {1k@1} & \To & e@1 \\
|
|
40 |
\vdots \\
|
|
41 |
\mid & C@m~x@ {m1}~\dots~x@ {mk@m} & \To & e@m
|
|
42 |
\end{array}
|
|
43 |
\]
|
|
44 |
|
|
45 |
\begin{warn}
|
|
46 |
\emph{All} constructors must be present, their order is fixed, and nested
|
|
47 |
patterns are not supported. Violating these restrictions results in strange
|
|
48 |
error messages.
|
|
49 |
\end{warn}
|
|
50 |
\noindent
|
|
51 |
Nested patterns can be simulated by nested \isa{case}-expressions: instead
|
|
52 |
of
|
|
53 |
\begin{isabelle}%
|
12699
|
54 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}{\isachargreater}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ {\isacharbrackleft}x{\isacharbrackright}\ {\isacharequal}{\isachargreater}\ x\ {\isacharbar}\ x\ {\isacharhash}\ {\isacharparenleft}y\ {\isacharhash}\ zs{\isacharparenright}\ {\isacharequal}{\isachargreater}\ y%
|
9924
|
55 |
\end{isabelle}
|
9834
|
56 |
write
|
|
57 |
\begin{isabelle}%
|
12699
|
58 |
\ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\isanewline
|
10950
|
59 |
\isaindent{\ \ \ \ \ }{\isacharbar}\ x\ {\isacharhash}\ ys\ {\isasymRightarrow}\ case\ ys\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ x\ {\isacharbar}\ y\ {\isacharhash}\ zs\ {\isasymRightarrow}\ y%
|
9924
|
60 |
\end{isabelle}
|
9742
|
61 |
|
|
62 |
Note that \isa{case}-expressions may need to be enclosed in parentheses to
|
|
63 |
indicate their scope%
|
|
64 |
\end{isamarkuptext}%
|
11866
|
65 |
\isamarkuptrue%
|
9742
|
66 |
%
|
10878
|
67 |
\isamarkupsubsection{Structural Induction and Case Distinction%
|
10397
|
68 |
}
|
11866
|
69 |
\isamarkuptrue%
|
9742
|
70 |
%
|
|
71 |
\begin{isamarkuptext}%
|
10824
|
72 |
\label{sec:struct-ind-case}
|
11456
|
73 |
\index{case distinctions}\index{induction!structural}%
|
|
74 |
Induction is invoked by \methdx{induct_tac}, as we have seen above;
|
|
75 |
it works for any datatype. In some cases, induction is overkill and a case
|
|
76 |
distinction over all constructors of the datatype suffices. This is performed
|
|
77 |
by \methdx{case_tac}. Here is a trivial example:%
|
9742
|
78 |
\end{isamarkuptext}%
|
17175
|
79 |
\isamarkuptrue%
|
|
80 |
\isacommand{lemma}\isamarkupfalse%
|
|
81 |
\ {\isachardoublequoteopen}{\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y{\isacharhash}ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
|
17056
|
82 |
%
|
|
83 |
\isadelimproof
|
|
84 |
%
|
|
85 |
\endisadelimproof
|
|
86 |
%
|
|
87 |
\isatagproof
|
17175
|
88 |
\isacommand{apply}\isamarkupfalse%
|
|
89 |
{\isacharparenleft}case{\isacharunderscore}tac\ xs{\isacharparenright}%
|
16069
|
90 |
\begin{isamarkuptxt}%
|
|
91 |
\noindent
|
|
92 |
results in the proof state
|
|
93 |
\begin{isabelle}%
|
|
94 |
\ {\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
|
|
95 |
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
|
|
96 |
\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%
|
|
97 |
\end{isabelle}
|
|
98 |
which is solved automatically:%
|
|
99 |
\end{isamarkuptxt}%
|
17175
|
100 |
\isamarkuptrue%
|
|
101 |
\isacommand{apply}\isamarkupfalse%
|
17181
|
102 |
{\isacharparenleft}auto{\isacharparenright}%
|
17056
|
103 |
\endisatagproof
|
|
104 |
{\isafoldproof}%
|
|
105 |
%
|
|
106 |
\isadelimproof
|
|
107 |
%
|
|
108 |
\endisadelimproof
|
11866
|
109 |
%
|
9742
|
110 |
\begin{isamarkuptext}%
|
|
111 |
Note that we do not need to give a lemma a name if we do not intend to refer
|
10824
|
112 |
to it explicitly in the future.
|
11456
|
113 |
Other basic laws about a datatype are applied automatically during
|
|
114 |
simplification, so no special methods are provided for them.
|
10824
|
115 |
|
|
116 |
\begin{warn}
|
|
117 |
Induction is only allowed on free (or \isasymAnd-bound) variables that
|
|
118 |
should not occur among the assumptions of the subgoal; see
|
|
119 |
\S\ref{sec:ind-var-in-prems} for details. Case distinction
|
|
120 |
(\isa{case{\isacharunderscore}tac}) works for arbitrary terms, which need to be
|
|
121 |
quoted if they are non-atomic. However, apart from \isa{{\isasymAnd}}-bound
|
|
122 |
variables, the terms must not contain variables that are bound outside.
|
|
123 |
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}},
|
|
124 |
\isa{case{\isacharunderscore}tac\ xs} will not work as expected because Isabelle interprets
|
|
125 |
the \isa{xs} as a new free variable distinct from the bound
|
|
126 |
\isa{xs} in the goal.
|
|
127 |
\end{warn}%
|
9742
|
128 |
\end{isamarkuptext}%
|
17175
|
129 |
\isamarkuptrue%
|
17056
|
130 |
%
|
|
131 |
\isadelimtheory
|
|
132 |
%
|
|
133 |
\endisadelimtheory
|
|
134 |
%
|
|
135 |
\isatagtheory
|
|
136 |
%
|
|
137 |
\endisatagtheory
|
|
138 |
{\isafoldtheory}%
|
|
139 |
%
|
|
140 |
\isadelimtheory
|
|
141 |
%
|
|
142 |
\endisadelimtheory
|
9742
|
143 |
\end{isabellebody}%
|
|
144 |
%%% Local Variables:
|
|
145 |
%%% mode: latex
|
|
146 |
%%% TeX-master: "root"
|
|
147 |
%%% End:
|