13 % |
13 % |
14 \isadelimtheory |
14 \isadelimtheory |
15 % |
15 % |
16 \endisadelimtheory |
16 \endisadelimtheory |
17 % |
17 % |
18 \isamarkupsubsection{Case Expressions% |
|
19 } |
|
20 \isamarkuptrue% |
|
21 % |
|
22 \begin{isamarkuptext}% |
18 \begin{isamarkuptext}% |
|
19 \subsection{Case Expressions} |
23 \label{sec:case-expressions}\index{*case expressions}% |
20 \label{sec:case-expressions}\index{*case expressions}% |
24 HOL also features \isa{case}-expressions for analyzing |
21 HOL also features \isa{case}-expressions for analyzing |
25 elements of a datatype. For example, |
22 elements of a datatype. For example, |
26 \begin{isabelle}% |
23 \begin{isabelle}% |
27 \ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ y% |
24 \ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ y% |
29 evaluates to \isa{{\isacharbrackleft}{\isacharbrackright}} if \isa{xs} is \isa{{\isacharbrackleft}{\isacharbrackright}} and to \isa{y} if |
26 evaluates to \isa{{\isacharbrackleft}{\isacharbrackright}} if \isa{xs} is \isa{{\isacharbrackleft}{\isacharbrackright}} and to \isa{y} if |
30 \isa{xs} is \isa{y\ {\isacharhash}\ ys}. (Since the result in both branches must be of |
27 \isa{xs} is \isa{y\ {\isacharhash}\ ys}. (Since the result in both branches must be of |
31 the same type, it follows that \isa{y} is of type \isa{{\isacharprime}a\ list} and hence |
28 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}.) |
29 that \isa{xs} is of type \isa{{\isacharprime}a\ list\ list}.) |
33 |
30 |
34 In general, if $e$ is a term of the datatype $t$ defined in |
31 In general, case expressions are of the form |
35 \S\ref{sec:general-datatype} above, the corresponding |
|
36 \isa{case}-expression analyzing $e$ is |
|
37 \[ |
32 \[ |
38 \begin{array}{rrcl} |
33 \begin{array}{c} |
39 \isa{case}~e~\isa{of} & C@1~x@ {11}~\dots~x@ {1k@1} & \To & e@1 \\ |
34 \isa{case}~e~\isa{of}\ pattern@1~\isa{{\isasymRightarrow}}~e@1\ \isa{{\isacharbar}}\ \dots\ |
40 \vdots \\ |
35 \isa{{\isacharbar}}~pattern@m~\isa{{\isasymRightarrow}}~e@m |
41 \mid & C@m~x@ {m1}~\dots~x@ {mk@m} & \To & e@m |
|
42 \end{array} |
36 \end{array} |
43 \] |
37 \] |
|
38 Like in functional programming, patterns are expressions consisting of |
|
39 datatype constructors (e.g. \isa{{\isacharbrackleft}{\isacharbrackright}} and \isa{{\isacharhash}}) |
|
40 and variables, including the wildcard ``\verb$_$''. |
|
41 Not all cases need to be covered and the order of cases matters. |
|
42 However, one is well-advised not to wallow in complex patterns because |
|
43 complex case distinctions tend to induce complex proofs. |
44 |
44 |
45 \begin{warn} |
45 \begin{warn} |
46 \emph{All} constructors must be present, their order is fixed, and nested |
46 Internally Isabelle only knows about exhaustive case expressions with |
47 patterns are not supported. Violating these restrictions results in strange |
47 non-nested patterns: $pattern@i$ must be of the form |
48 error messages. |
48 $C@i~x@ {i1}~\dots~x@ {ik@i}$ and $C@1, \dots, C@m$ must be exactly the |
|
49 constructors of the type of $e$. |
|
50 % |
|
51 More complex case expressions are automatically |
|
52 translated into the simpler form upon parsing but are not translated |
|
53 back for printing. This may lead to surprising output. |
49 \end{warn} |
54 \end{warn} |
50 \noindent |
|
51 Nested patterns can be simulated by nested \isa{case}-expressions: instead |
|
52 of |
|
53 \begin{isabelle}% |
|
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% |
|
55 \end{isabelle} |
|
56 write |
|
57 \begin{isabelle}% |
|
58 \ \ \ \ \ case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ {\isacharbrackleft}x{\isacharbrackright}\ {\isasymRightarrow}\ x\ {\isacharbar}\ x\ {\isacharhash}\ y\ {\isacharhash}\ zs\ {\isasymRightarrow}\ y% |
|
59 \end{isabelle} |
|
60 |
55 |
61 Note that \isa{case}-expressions may need to be enclosed in parentheses to |
56 \begin{warn} |
62 indicate their scope% |
57 Like \isa{if}, \isa{case}-expressions may need to be enclosed in |
63 \end{isamarkuptext}% |
58 parentheses to indicate their scope. |
64 \isamarkuptrue% |
59 \end{warn} |
65 % |
60 |
66 \isamarkupsubsection{Structural Induction and Case Distinction% |
61 \subsection{Structural Induction and Case Distinction} |
67 } |
|
68 \isamarkuptrue% |
|
69 % |
|
70 \begin{isamarkuptext}% |
|
71 \label{sec:struct-ind-case} |
62 \label{sec:struct-ind-case} |
72 \index{case distinctions}\index{induction!structural}% |
63 \index{case distinctions}\index{induction!structural}% |
73 Induction is invoked by \methdx{induct_tac}, as we have seen above; |
64 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 |
65 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 |
66 distinction over all constructors of the datatype suffices. This is performed |