| 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}%
 | 
| 22795 |     58 | \ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ {\isacharbrackleft}x{\isacharbrackright}\ {\isasymRightarrow}\ x\ {\isacharbar}\ x\ {\isacharhash}\ y\ {\isacharhash}\ zs\ {\isasymRightarrow}\ y%
 | 
| 9924 |     59 | \end{isabelle}
 | 
| 9742 |     60 | 
 | 
|  |     61 | Note that \isa{case}-expressions may need to be enclosed in parentheses to
 | 
|  |     62 | indicate their scope%
 | 
|  |     63 | \end{isamarkuptext}%
 | 
| 11866 |     64 | \isamarkuptrue%
 | 
| 9742 |     65 | %
 | 
| 10878 |     66 | \isamarkupsubsection{Structural Induction and Case Distinction%
 | 
| 10397 |     67 | }
 | 
| 11866 |     68 | \isamarkuptrue%
 | 
| 9742 |     69 | %
 | 
|  |     70 | \begin{isamarkuptext}%
 | 
| 10824 |     71 | \label{sec:struct-ind-case}
 | 
| 11456 |     72 | \index{case distinctions}\index{induction!structural}%
 | 
|  |     73 | Induction is invoked by \methdx{induct_tac}, as we have seen above; 
 | 
|  |     74 | it works for any datatype.  In some cases, induction is overkill and a case
 | 
|  |     75 | distinction over all constructors of the datatype suffices.  This is performed
 | 
|  |     76 | by \methdx{case_tac}.  Here is a trivial example:%
 | 
| 9742 |     77 | \end{isamarkuptext}%
 | 
| 17175 |     78 | \isamarkuptrue%
 | 
|  |     79 | \isacommand{lemma}\isamarkupfalse%
 | 
|  |     80 | \ {\isachardoublequoteopen}{\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y{\isacharhash}ys\ {\isasymRightarrow}\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
 | 
| 17056 |     81 | %
 | 
|  |     82 | \isadelimproof
 | 
|  |     83 | %
 | 
|  |     84 | \endisadelimproof
 | 
|  |     85 | %
 | 
|  |     86 | \isatagproof
 | 
| 17175 |     87 | \isacommand{apply}\isamarkupfalse%
 | 
|  |     88 | {\isacharparenleft}case{\isacharunderscore}tac\ xs{\isacharparenright}%
 | 
| 16069 |     89 | \begin{isamarkuptxt}%
 | 
|  |     90 | \noindent
 | 
|  |     91 | results in the proof state
 | 
|  |     92 | \begin{isabelle}%
 | 
|  |     93 | \ {\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
 | 
|  |     94 | \ {\isadigit{2}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
 | 
|  |     95 | \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%
 | 
|  |     96 | \end{isabelle}
 | 
|  |     97 | which is solved automatically:%
 | 
|  |     98 | \end{isamarkuptxt}%
 | 
| 17175 |     99 | \isamarkuptrue%
 | 
|  |    100 | \isacommand{apply}\isamarkupfalse%
 | 
| 17181 |    101 | {\isacharparenleft}auto{\isacharparenright}%
 | 
| 17056 |    102 | \endisatagproof
 | 
|  |    103 | {\isafoldproof}%
 | 
|  |    104 | %
 | 
|  |    105 | \isadelimproof
 | 
|  |    106 | %
 | 
|  |    107 | \endisadelimproof
 | 
| 11866 |    108 | %
 | 
| 9742 |    109 | \begin{isamarkuptext}%
 | 
|  |    110 | Note that we do not need to give a lemma a name if we do not intend to refer
 | 
| 10824 |    111 | to it explicitly in the future.
 | 
| 11456 |    112 | Other basic laws about a datatype are applied automatically during
 | 
|  |    113 | simplification, so no special methods are provided for them.
 | 
| 10824 |    114 | 
 | 
|  |    115 | \begin{warn}
 | 
|  |    116 |   Induction is only allowed on free (or \isasymAnd-bound) variables that
 | 
|  |    117 |   should not occur among the assumptions of the subgoal; see
 | 
|  |    118 |   \S\ref{sec:ind-var-in-prems} for details. Case distinction
 | 
|  |    119 |   (\isa{case{\isacharunderscore}tac}) works for arbitrary terms, which need to be
 | 
|  |    120 |   quoted if they are non-atomic. However, apart from \isa{{\isasymAnd}}-bound
 | 
|  |    121 |   variables, the terms must not contain variables that are bound outside.
 | 
|  |    122 |   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}},
 | 
|  |    123 |   \isa{case{\isacharunderscore}tac\ xs} will not work as expected because Isabelle interprets
 | 
|  |    124 |   the \isa{xs} as a new free variable distinct from the bound
 | 
|  |    125 |   \isa{xs} in the goal.
 | 
|  |    126 | \end{warn}%
 | 
| 9742 |    127 | \end{isamarkuptext}%
 | 
| 17175 |    128 | \isamarkuptrue%
 | 
| 17056 |    129 | %
 | 
|  |    130 | \isadelimtheory
 | 
|  |    131 | %
 | 
|  |    132 | \endisadelimtheory
 | 
|  |    133 | %
 | 
|  |    134 | \isatagtheory
 | 
|  |    135 | %
 | 
|  |    136 | \endisatagtheory
 | 
|  |    137 | {\isafoldtheory}%
 | 
|  |    138 | %
 | 
|  |    139 | \isadelimtheory
 | 
|  |    140 | %
 | 
|  |    141 | \endisadelimtheory
 | 
| 9742 |    142 | \end{isabellebody}%
 | 
|  |    143 | %%% Local Variables:
 | 
|  |    144 | %%% mode: latex
 | 
|  |    145 | %%% TeX-master: "root"
 | 
|  |    146 | %%% End:
 |