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