212 |
212 |
213 \begin{exercise}\label{ex:Tree} |
213 \begin{exercise}\label{ex:Tree} |
214 \input{Misc/document/Tree.tex}% |
214 \input{Misc/document/Tree.tex}% |
215 \end{exercise} |
215 \end{exercise} |
216 |
216 |
217 \subsection{Case expressions} |
217 \input{Misc/document/case_exprs.tex} |
218 \label{sec:case-expressions} |
|
219 |
|
220 HOL also features \isaindexbold{case}-expressions for analyzing |
|
221 elements of a datatype. For example, |
|
222 % case xs of [] => 0 | y#ys => y |
|
223 \begin{isabellepar}% |
|
224 ~~~case~xs~of~[]~{\isasymRightarrow}~0~|~y~\#~ys~{\isasymRightarrow}~y |
|
225 \end{isabellepar}% |
|
226 evaluates to \isa{0} if \isa{xs} is \isa{[]} and to \isa{y} if |
|
227 \isa{xs} is \isa{y\#ys}. (Since the result in both branches must be of |
|
228 the same type, it follows that \isa{y::nat} and hence |
|
229 \isa{xs::(nat)list}.) |
|
230 |
|
231 In general, if $e$ is a term of the datatype $t$ defined in |
|
232 \S\ref{sec:general-datatype} above, the corresponding |
|
233 \isa{case}-expression analyzing $e$ is |
|
234 \[ |
|
235 \begin{array}{rrcl} |
|
236 \isa{case}~e~\isa{of} & C@1~x@{11}~\dots~x@{1k@1} & \To & e@1 \\ |
|
237 \vdots \\ |
|
238 \mid & C@m~x@{m1}~\dots~x@{mk@m} & \To & e@m |
|
239 \end{array} |
|
240 \] |
|
241 |
|
242 \begin{warn} |
|
243 {\em All} constructors must be present, their order is fixed, and nested |
|
244 patterns are not supported. Violating these restrictions results in strange |
|
245 error messages. |
|
246 \end{warn} |
|
247 \noindent |
|
248 Nested patterns can be simulated by nested \isa{case}-expressions: instead |
|
249 of |
|
250 % case xs of [] => 0 | [x] => x | x#(y#zs) => y |
|
251 \begin{isabellepar}% |
|
252 ~~~case~xs~of~[]~{\isasymRightarrow}~0~|~[x]~{\isasymRightarrow}~x~|~x~\#~y~\#~zs~{\isasymRightarrow}~y |
|
253 \end{isabellepar}% |
|
254 write |
|
255 % term(latex xsymbols symbols)"case xs of [] => 0 | x#ys => (case ys of [] => x | y#zs => y)"; |
|
256 \begin{isabellepar}% |
|
257 ~~~case~xs~of~[]~{\isasymRightarrow}~0~|~x~\#~ys~{\isasymRightarrow}~(case~ys~of~[]~{\isasymRightarrow}~x~|~y~\#~zs~{\isasymRightarrow}~y)% |
|
258 \end{isabellepar}% |
|
259 Note that \isa{case}-expressions should be enclosed in parentheses to |
|
260 indicate their scope. |
|
261 |
|
262 \subsection{Structural induction and case distinction} |
|
263 \indexbold{structural induction} |
|
264 \indexbold{induction!structural} |
|
265 \indexbold{case distinction} |
|
266 |
|
267 Almost all the basic laws about a datatype are applied automatically during |
|
268 simplification. Only induction is invoked by hand via \isaindex{induct_tac}, |
|
269 which works for any datatype. In some cases, induction is overkill and a case |
|
270 distinction over all constructors of the datatype suffices. This is performed |
|
271 by \isaindexbold{case_tac}. A trivial example: |
|
272 |
|
273 \input{Misc/document/cases.tex}% |
|
274 |
|
275 Note that we do not need to give a lemma a name if we do not intend to refer |
|
276 to it explicitly in the future. |
|
277 |
218 |
278 \begin{warn} |
219 \begin{warn} |
279 Induction is only allowed on free (or \isasymAnd-bound) variables that |
220 Induction is only allowed on free (or \isasymAnd-bound) variables that |
280 should not occur among the assumptions of the subgoal; see |
221 should not occur among the assumptions of the subgoal; see |
281 \S\ref{sec:ind-var-in-prems} for details. Case distinction |
222 \S\ref{sec:ind-var-in-prems} for details. Case distinction |
618 \input{Misc/document/arith4.tex}% |
558 \input{Misc/document/arith4.tex}% |
619 is not proved by simplification and requires \isa{arith}. |
559 is not proved by simplification and requires \isa{arith}. |
620 |
560 |
621 \subsubsection{Permutative rewrite rules} |
561 \subsubsection{Permutative rewrite rules} |
622 |
562 |
623 A rewrite rule is {\bf permutative} if the left-hand side and right-hand side |
563 A rewrite rule is \textbf{permutative} if the left-hand side and right-hand |
624 are the same up to renaming of variables. The most common permutative rule |
564 side are the same up to renaming of variables. The most common permutative |
625 is commutativity: $x+y = y+x$. Another example is $(x-y)-z = (x-z)-y$. Such |
565 rule is commutativity: $x+y = y+x$. Another example is $(x-y)-z = (x-z)-y$. |
626 rules are problematic because once they apply, they can be used forever. |
566 Such rules are problematic because once they apply, they can be used forever. |
627 The simplifier is aware of this danger and treats permutative rules |
567 The simplifier is aware of this danger and treats permutative rules |
628 separately. For details see~\cite{isabelle-ref}. |
568 separately. For details see~\cite{isabelle-ref}. |
629 |
569 |
630 \subsubsection{Tracing} |
570 \subsubsection{Tracing} |
631 \indexbold{tracing the simplifier} |
571 \indexbold{tracing the simplifier} |