doc-src/Nitpick/nitpick.tex
changeset 35180 c57dba973391
parent 35178 29a0e3be0be1
child 35183 8580ba651489
equal deleted inserted replaced
35179:4b198af5beb5 35180:c57dba973391
  1329 \postw
  1329 \postw
  1330 
  1330 
  1331 and this time \textit{arith} can finish off the subgoals.
  1331 and this time \textit{arith} can finish off the subgoals.
  1332 
  1332 
  1333 A similar technique can be employed for structural induction. The
  1333 A similar technique can be employed for structural induction. The
  1334 following mini-formalization of full binary trees will serve as illustration:
  1334 following mini formalization of full binary trees will serve as illustration:
  1335 
  1335 
  1336 \prew
  1336 \prew
  1337 \textbf{datatype} $\kern1pt'a$~\textit{bin\_tree} = $\textit{Leaf}~{\kern1pt'a}$ $\mid$ $\textit{Branch}$ ``\kern1pt$'a$ \textit{bin\_tree}'' ``\kern1pt$'a$ \textit{bin\_tree}'' \\[2\smallskipamount]
  1337 \textbf{datatype} $\kern1pt'a$~\textit{bin\_tree} = $\textit{Leaf}~{\kern1pt'a}$ $\mid$ $\textit{Branch}$ ``\kern1pt$'a$ \textit{bin\_tree}'' ``\kern1pt$'a$ \textit{bin\_tree}'' \\[2\smallskipamount]
  1338 \textbf{primrec}~\textit{labels}~\textbf{where} \\
  1338 \textbf{primrec}~\textit{labels}~\textbf{where} \\
  1339 ``$\textit{labels}~(\textit{Leaf}~a) = \{a\}$'' $\mid$ \\
  1339 ``$\textit{labels}~(\textit{Leaf}~a) = \{a\}$'' $\mid$ \\
  1348 tree, and \textit{swap} exchanges two labels. Intuitively, if two distinct
  1348 tree, and \textit{swap} exchanges two labels. Intuitively, if two distinct
  1349 labels $a$ and $b$ occur in a tree $t$, they should also occur in the tree
  1349 labels $a$ and $b$ occur in a tree $t$, they should also occur in the tree
  1350 obtained by swapping $a$ and $b$:
  1350 obtained by swapping $a$ and $b$:
  1351 
  1351 
  1352 \prew
  1352 \prew
  1353 \textbf{lemma} $``\lbrakk a \in \textit{labels}~t;\, b \in \textit{labels}~t;\, a \not= b\rbrakk {}$ \\
  1353 \textbf{lemma} $``\{a, b\} \subseteq \textit{labels}~t \,\Longrightarrow\, \textit{labels}~(\textit{swap}~t~a~b) = \textit{labels}~t$''
  1354 \phantom{\textbf{lemma} ``}$\,{\Longrightarrow}{\;\,} \textit{labels}~(\textit{swap}~t~a~b) = \textit{labels}~t$''
       
  1355 \postw
  1354 \postw
  1356 
  1355 
  1357 Nitpick can't find any counterexample, so we proceed with induction
  1356 Nitpick can't find any counterexample, so we proceed with induction
  1358 (this time favoring a more structured style):
  1357 (this time favoring a more structured style):
  1359 
  1358 
  1368 following suggestion:
  1367 following suggestion:
  1369 
  1368 
  1370 \prew
  1369 \prew
  1371 \slshape
  1370 \slshape
  1372 Hint: To check that the induction hypothesis is general enough, try this command:
  1371 Hint: To check that the induction hypothesis is general enough, try this command:
  1373 \textbf{nitpick}~[\textit{non\_std} ``${\kern1pt'a}~\textit{bin\_tree}$'', \textit{show\_consts}].
  1372 \textbf{nitpick}~[\textit{non\_std} ``${\kern1pt'a}~\textit{bin\_tree}$'', \textit{show\_all}].
  1374 \postw
  1373 \postw
  1375 
  1374 
  1376 If we follow the hint, we get a ``nonstandard'' counterexample for the step:
  1375 If we follow the hint, we get a ``nonstandard'' counterexample for the step:
  1377 
  1376 
  1378 \prew
  1377 \prew
  1379 \slshape Nitpick found a nonstandard counterexample for \textit{card} $'a$ = 4: \\[2\smallskipamount]
  1378 \slshape Nitpick found a nonstandard counterexample for \textit{card} $'a$ = 3: \\[2\smallskipamount]
  1380 \hbox{}\qquad Free variables: \nopagebreak \\
  1379 \hbox{}\qquad Free variables: \nopagebreak \\
  1381 \hbox{}\qquad\qquad $a = a_1$ \\
  1380 \hbox{}\qquad\qquad $a = a_1$ \\
  1382 \hbox{}\qquad\qquad $b = a_2$ \\
  1381 \hbox{}\qquad\qquad $b = a_2$ \\
  1383 \hbox{}\qquad\qquad $t = \xi_1$ \\
  1382 \hbox{}\qquad\qquad $t = \xi_1$ \\
  1384 \hbox{}\qquad\qquad $u = \xi_2$ \\
  1383 \hbox{}\qquad\qquad $u = \xi_2$ \\
       
  1384 \hbox{}\qquad Datatype: \nopagebreak \\
       
  1385 \hbox{}\qquad\qquad $\alpha~\textit{btree} = \{\xi_1 \mathbin{=} \textit{Branch}~\xi_1~\xi_1,\> \xi_2 \mathbin{=} \textit{Branch}~\xi_2~\xi_2,\> \textit{Branch}~\xi_1~\xi_2\}$ \\
  1385 \hbox{}\qquad {\slshape Constants:} \nopagebreak \\
  1386 \hbox{}\qquad {\slshape Constants:} \nopagebreak \\
  1386 \hbox{}\qquad\qquad $\textit{labels} = \undef
  1387 \hbox{}\qquad\qquad $\textit{labels} = \undef
  1387     (\!\begin{aligned}[t]%
  1388     (\!\begin{aligned}[t]%
  1388     & \xi_1 := \{a_1, a_4, a_3^\Q\},\> \xi_2 := \{a_2, a_3^\Q\}, \\[-2pt] %% TYPESETTING
  1389     & \xi_1 := \{a_2, a_3\},\> \xi_2 := \{a_1\},\> \\[-2pt]
  1389     & \textit{Branch}~\xi_1~\xi_2 := \{a_1, a_2, a_4, a_3^\Q\}, \\[-2pt]
  1390     & \textit{Branch}~\xi_1~\xi_2 := \{a_1, a_2, a_3\})\end{aligned}$ \\
  1390     & \textit{Branch}~\xi_2~\xi_2 := \{a_2, a_3^\Q\})\end{aligned}$ \\
       
  1391 \hbox{}\qquad\qquad $\lambda x_1.\> \textit{swap}~x_1~a~b = \undef
  1391 \hbox{}\qquad\qquad $\lambda x_1.\> \textit{swap}~x_1~a~b = \undef
  1392     (\!\begin{aligned}[t]%
  1392     (\!\begin{aligned}[t]%
  1393     & \xi_1 := \xi_2,\> \xi_2 := \xi_2, \\[-2pt]
  1393     & \xi_1 := \xi_2,\> \xi_2 := \xi_2, \\[-2pt]
  1394     & \textit{Branch}~\xi_1~\xi_2 := \textit{Branch}~\xi_2~\xi_2, \\[-2pt]
  1394     & \textit{Branch}~\xi_1~\xi_2 := \xi_2)\end{aligned}$ \\[2\smallskipamount]
  1395     & \textit{Branch}~\xi_2~\xi_2 := \textit{Branch}~\xi_2~\xi_2)\end{aligned}$ \\[2\smallskipamount]
       
  1396 The existence of a nonstandard model suggests that the induction hypothesis is not general enough or perhaps
  1395 The existence of a nonstandard model suggests that the induction hypothesis is not general enough or perhaps
  1397 even wrong. See the ``Inductive Properties'' section of the Nitpick manual for details (``\textit{isabelle doc nitpick}'').
  1396 even wrong. See the ``Inductive Properties'' section of the Nitpick manual for details (``\textit{isabelle doc nitpick}'').
  1398 \postw
  1397 \postw
  1399 
  1398 
  1400 Reading the Nitpick manual is a most excellent idea.
  1399 Reading the Nitpick manual is a most excellent idea.
  1406 \footnote{Notice the similarity between allowing nonstandard trees here and
  1405 \footnote{Notice the similarity between allowing nonstandard trees here and
  1407 allowing unreachable states in the preceding example (by removing the ``$n \in
  1406 allowing unreachable states in the preceding example (by removing the ``$n \in
  1408 \textit{reach\/}$'' assumption). In both cases, we effectively enlarge the
  1407 \textit{reach\/}$'' assumption). In both cases, we effectively enlarge the
  1409 set of objects over which the induction is performed while doing the step
  1408 set of objects over which the induction is performed while doing the step
  1410 in order to test the induction hypothesis's strength.}
  1409 in order to test the induction hypothesis's strength.}
  1411 The new trees are so nonstandard that we know nothing about them, except what
  1410 Unlike standard trees, these new trees contain cycles. We will see later that
  1412 the induction hypothesis states and what can be proved about all trees without
  1411 every property of acyclic trees that can be proved without using induction also
  1413 relying on induction or case distinction. The key observation is,
  1412 holds for cyclic trees. Hence,
  1414 %
  1413 %
  1415 \begin{quote}
  1414 \begin{quote}
  1416 \textsl{If the induction
  1415 \textsl{If the induction
  1417 hypothesis is strong enough, the induction step will hold even for nonstandard
  1416 hypothesis is strong enough, the induction step will hold even for nonstandard
  1418 objects, and Nitpick won't find any nonstandard counterexample.}
  1417 objects, and Nitpick won't find any nonstandard counterexample.}
  1419 \end{quote}
  1418 \end{quote}
  1420 %
  1419 %
  1421 But here, Nitpick did find some nonstandard trees $t = \xi_1$
  1420 But here the tool find some nonstandard trees $t = \xi_1$
  1422 and $u = \xi_2$ such that $a \in \textit{labels}~t$, $b \notin
  1421 and $u = \xi_2$ such that $a \notin \textit{labels}~t$, $b \in
  1423 \textit{labels}~t$, $a \notin \textit{labels}~u$, and $b \in \textit{labels}~u$.
  1422 \textit{labels}~t$, $a \in \textit{labels}~u$, and $b \notin \textit{labels}~u$.
  1424 Because neither tree contains both $a$ and $b$, the induction hypothesis tells
  1423 Because neither tree contains both $a$ and $b$, the induction hypothesis tells
  1425 us nothing about the labels of $\textit{swap}~t~a~b$ and $\textit{swap}~u~a~b$,
  1424 us nothing about the labels of $\textit{swap}~t~a~b$ and $\textit{swap}~u~a~b$,
  1426 and as a result we know nothing about the labels of the tree
  1425 and as a result we know nothing about the labels of the tree
  1427 $\textit{swap}~(\textit{Branch}~t~u)~a~b$, which by definition equals
  1426 $\textit{swap}~(\textit{Branch}~t~u)~a~b$, which by definition equals
  1428 $\textit{Branch}$ $(\textit{swap}~t~a~b)$ $(\textit{swap}~u~a~b)$, whose
  1427 $\textit{Branch}$ $(\textit{swap}~t~a~b)$ $(\textit{swap}~u~a~b)$, whose