9742
|
1 |
(*<*)
|
|
2 |
theory case_exprs = Main:
|
|
3 |
(*>*)
|
|
4 |
|
10885
|
5 |
subsection{*Case Expressions*}
|
9742
|
6 |
|
|
7 |
text{*\label{sec:case-expressions}
|
11428
|
8 |
HOL also features \sdx{case}-expressions for analyzing
|
9742
|
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 |
|
10885
|
43 |
subsection{*Structural Induction and Case Distinction*}
|
9742
|
44 |
|
10824
|
45 |
text{*\label{sec:struct-ind-case}
|
9742
|
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
|
11428
|
50 |
simplification. Only induction is invoked by hand via \methdx{induct_tac},
|
9742
|
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
|
11428
|
53 |
by \methdx{case_tac}. A trivial example:
|
9742
|
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
|
10420
|
61 |
@{subgoals[display,indent=0,margin=65]}
|
9742
|
62 |
which is solved automatically:
|
|
63 |
*}
|
|
64 |
|
10171
|
65 |
apply(auto)
|
|
66 |
(*<*)done(*>*)
|
9742
|
67 |
text{*
|
|
68 |
Note that we do not need to give a lemma a name if we do not intend to refer
|
|
69 |
to it explicitly in the future.
|
10824
|
70 |
|
|
71 |
\begin{warn}
|
|
72 |
Induction is only allowed on free (or \isasymAnd-bound) variables that
|
|
73 |
should not occur among the assumptions of the subgoal; see
|
|
74 |
\S\ref{sec:ind-var-in-prems} for details. Case distinction
|
|
75 |
(@{text"case_tac"}) works for arbitrary terms, which need to be
|
|
76 |
quoted if they are non-atomic. However, apart from @{text"\<And>"}-bound
|
|
77 |
variables, the terms must not contain variables that are bound outside.
|
|
78 |
For example, given the goal @{prop"\<forall>xs. xs = [] \<or> (\<exists>y ys. xs = y#ys)"},
|
|
79 |
@{text"case_tac xs"} will not work as expected because Isabelle interprets
|
|
80 |
the @{term xs} as a new free variable distinct from the bound
|
|
81 |
@{term xs} in the goal.
|
|
82 |
\end{warn}
|
9742
|
83 |
*}
|
|
84 |
|
|
85 |
(*<*)
|
|
86 |
end
|
|
87 |
(*>*)
|