doc-src/Tutorial/fp.tex
changeset 7569 1d9263172b54
parent 6691 8a1b5f9d8420
child 7587 ee0b835ca8fa
equal deleted inserted replaced
7568:436c87ac2fac 7569:1d9263172b54
   567 has to provide the clue.
   567 has to provide the clue.
   568 The necessity of universal quantification (\texttt{!t e}) in the two lemmas
   568 The necessity of universal quantification (\texttt{!t e}) in the two lemmas
   569 is explained in \S\ref{sec:InductionHeuristics}
   569 is explained in \S\ref{sec:InductionHeuristics}
   570 
   570 
   571 \begin{exercise}
   571 \begin{exercise}
   572   We strengthen the definition of a {\em normal\/} If-expression as follows:
   572   We strengthen the definition of a \texttt{normal} If-expression as follows:
   573   the first argument of all \texttt{IF}s must be a variable. Adapt the above
   573   the first argument of all \texttt{IF}s must be a variable. Adapt the above
   574   development to this changed requirement. (Hint: you may need to formulate
   574   development to this changed requirement. (Hint: you may need to formulate
   575   some of the goals as implications (\texttt{-->}) rather than equalities
   575   some of the goals as implications (\texttt{-->}) rather than equalities
   576   (\texttt{=}).)
   576   (\texttt{=}).)
   577 \end{exercise}
   577 \end{exercise}
  1397 nested recursion can be eliminated in favour of mutual recursion by unfolding
  1397 nested recursion can be eliminated in favour of mutual recursion by unfolding
  1398 the offending datatypes, here \texttt{list}. The result for \texttt{term}
  1398 the offending datatypes, here \texttt{list}. The result for \texttt{term}
  1399 would be something like
  1399 would be something like
  1400 \begin{ttbox}
  1400 \begin{ttbox}
  1401 \input{Datatype/tunfoldeddata}\end{ttbox}
  1401 \input{Datatype/tunfoldeddata}\end{ttbox}
  1402 Although we do not recommend this unfolding to the user, this is how Isabelle
  1402 Although we do not recommend this unfolding to the user, it shows how to
  1403 deals with nested recursion internally. Strictly speaking, this information
  1403 simulate nested recursion by mutual recursion.
  1404 is irrelevant at the user level (and might change in the future), but it
  1404 Now we return to the initial definition of \texttt{term} using
  1405 motivates why \texttt{primrec} and induction work for nested types the way
       
  1406 they do. We now return to the initial definition of \texttt{term} using
       
  1407 nested recursion.
  1405 nested recursion.
  1408 
  1406 
  1409 Let us define a substitution function on terms. Because terms involve term
  1407 Let us define a substitution function on terms. Because terms involve term
  1410 lists, we need to define two substitution functions simultaneously:
  1408 lists, we need to define two substitution functions simultaneously:
  1411 \begin{ttbox}
  1409 \begin{ttbox}
  1435 Correct this statement (you will find that it does not type-check),
  1433 Correct this statement (you will find that it does not type-check),
  1436 strengthen it and prove it. (Note: \texttt{o} is function composition; its
  1434 strengthen it and prove it. (Note: \texttt{o} is function composition; its
  1437 definition is found in the theorem \texttt{o_def}).
  1435 definition is found in the theorem \texttt{o_def}).
  1438 \end{exercise}
  1436 \end{exercise}
  1439 
  1437 
  1440 If you feel that the \texttt{App}-equation in the definition of substitution
  1438 Returning to the definition of \texttt{subst}, we have to admit that it does
  1441 is overly complicated, you are right: the simpler
  1439 not really need the auxiliary \texttt{substs} function. The \texttt{App}-case
       
  1440 can directly be expressed as
  1442 \begin{ttbox}
  1441 \begin{ttbox}
  1443 \input{Datatype/appmap}\end{ttbox}
  1442 \input{Datatype/appmap}\end{ttbox}
  1444 would have done the job. Unfortunately, Isabelle insists on the more verbose
  1443 Although Isabelle insists on the more verbose version, we can easily {\em
  1445 equation given above. Nevertheless, we can easily {\em prove} that the
  1444   prove} that the \texttt{map}-equation holds: simply by induction on
  1446 \texttt{map}-equation holds: simply by induction on \texttt{ts} followed by
  1445 \texttt{ts} followed by \texttt{Auto_tac}.
  1447 \texttt{Auto_tac}.
       
  1448 
  1446 
  1449 %FIXME: forward pointer to section where better induction principles are
  1447 %FIXME: forward pointer to section where better induction principles are
  1450 %derived?
  1448 %derived?
  1451 
  1449 
  1452 \begin{exercise}
  1450 \begin{exercise}
  1465 
  1463 
  1466 \subsection{The limits of nested recursion}
  1464 \subsection{The limits of nested recursion}
  1467 
  1465 
  1468 How far can we push nested recursion? By the unfolding argument above, we can
  1466 How far can we push nested recursion? By the unfolding argument above, we can
  1469 reduce nested to mutual recursion provided the nested recursion only involves
  1467 reduce nested to mutual recursion provided the nested recursion only involves
  1470 previously defined datatypes. Isabelle is a bit more generous and also permits
  1468 previously defined datatypes. The \texttt{data} example above involves
  1471 products as in the \texttt{data} example above.
  1469 products, which is fine because products are also datatypes.
  1472 However, functions are most emphatically not allowed:
  1470 However, functions are most emphatically not allowed:
  1473 \begin{ttbox}
  1471 \begin{ttbox}
  1474 datatype t = C (t => bool)
  1472 datatype t = C (t => bool)
  1475 \end{ttbox}
  1473 \end{ttbox}
  1476 is a real can of worms: in HOL it must be ruled out because it requires a
  1474 is a real can of worms: in HOL it must be ruled out because it requires a
  1647 
  1645 
  1648 Although many total functions have a natural primitive recursive definition,
  1646 Although many total functions have a natural primitive recursive definition,
  1649 this is not always the case. Arbitrary total recursive functions can be
  1647 this is not always the case. Arbitrary total recursive functions can be
  1650 defined by means of \texttt{recdef}: you can use full pattern-matching,
  1648 defined by means of \texttt{recdef}: you can use full pattern-matching,
  1651 recursion need not involve datatypes, and termination is proved by showing
  1649 recursion need not involve datatypes, and termination is proved by showing
  1652 that each recursive call makes the argument smaller in a suitable (user
  1650 that the arguments of all recursive calls are smaller in a suitable (user
  1653 supplied) sense.
  1651 supplied) sense.
  1654 
  1652 
  1655 \subsection{Defining recursive functions}
  1653 \subsection{Defining recursive functions}
  1656 
  1654 
  1657 Here is a simple example, the Fibonacci function:
  1655 Here is a simple example, the Fibonacci function: