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