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