doc-src/TutorialI/Datatype/document/Nested.tex
author nipkow
Fri, 01 Sep 2000 19:09:44 +0200
changeset 9792 bbefb6ce5cb2
parent 9722 a5f86aed785b
child 9834 109b11c4e77e
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
     1
%
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
     2
\begin{isabellebody}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     3
%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     4
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     5
So far, all datatypes had the property that on the right-hand side of their
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     6
definition they occurred only at the top-level, i.e.\ directly below a
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     7
constructor. This is not the case any longer for the following model of terms
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     8
where function symbols can be applied to a list of arguments:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     9
\end{isamarkuptext}%
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    10
\isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}{\isachardoublequote}term{\isachardoublequote}\ {\isacharequal}\ Var\ {\isacharprime}a\ {\isacharbar}\ App\ {\isacharprime}b\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term\ list{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    11
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    12
\noindent
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    13
Note that we need to quote \isa{term} on the left to avoid confusion with
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    14
the command \isacommand{term}.
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    15
Parameter \isa{{\isacharprime}a} is the type of variables and \isa{{\isacharprime}b} the type of
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    16
function symbols.
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    17
A mathematical term like $f(x,g(y))$ becomes \isa{App\ f\ {\isacharbrackleft}term{\isachardot}Var\ x{\isacharcomma}\ App\ g\ {\isacharbrackleft}term{\isachardot}Var\ y{\isacharbrackright}{\isacharbrackright}}, where \isa{f}, \isa{g}, \isa{x}, \isa{y} are
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    18
suitable values, e.g.\ numbers or strings.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    19
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    20
What complicates the definition of \isa{term} is the nested occurrence of
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    21
\isa{term} inside \isa{list} on the right-hand side. In principle,
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    22
nested recursion can be eliminated in favour of mutual recursion by unfolding
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    23
the offending datatypes, here \isa{list}. The result for \isa{term}
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    24
would be something like
8751
9ed0548177fb *** empty log message ***
nipkow
parents: 8749
diff changeset
    25
\medskip
9ed0548177fb *** empty log message ***
nipkow
parents: 8749
diff changeset
    26
9ed0548177fb *** empty log message ***
nipkow
parents: 8749
diff changeset
    27
\input{Datatype/document/unfoldnested.tex}
9ed0548177fb *** empty log message ***
nipkow
parents: 8749
diff changeset
    28
\medskip
9ed0548177fb *** empty log message ***
nipkow
parents: 8749
diff changeset
    29
9ed0548177fb *** empty log message ***
nipkow
parents: 8749
diff changeset
    30
\noindent
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    31
Although we do not recommend this unfolding to the user, it shows how to
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    32
simulate nested recursion by mutual recursion.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    33
Now we return to the initial definition of \isa{term} using
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    34
nested recursion.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    35
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    36
Let us define a substitution function on terms. Because terms involve term
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    37
lists, we need to define two substitution functions simultaneously:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    38
\end{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    39
\isacommand{consts}\isanewline
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    40
subst\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isasymRightarrow}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term\ \ \ \ \ \ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term{\isachardoublequote}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    41
substs{\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isasymRightarrow}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term\ list\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term\ list{\isachardoublequote}\isanewline
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    42
\isanewline
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    43
\isacommand{primrec}\isanewline
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    44
\ \ {\isachardoublequote}subst\ s\ {\isacharparenleft}Var\ x{\isacharparenright}\ {\isacharequal}\ s\ x{\isachardoublequote}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    45
\ \ subst{\isacharunderscore}App{\isacharcolon}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    46
\ \ {\isachardoublequote}subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}substs\ s\ ts{\isacharparenright}{\isachardoublequote}\isanewline
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    47
\isanewline
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    48
\ \ {\isachardoublequote}substs\ s\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    49
\ \ {\isachardoublequote}substs\ s\ {\isacharparenleft}t\ {\isacharhash}\ ts{\isacharparenright}\ {\isacharequal}\ subst\ s\ t\ {\isacharhash}\ substs\ s\ ts{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    50
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    51
\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    52
You should ignore the label \isa{subst{\isacharunderscore}App} for the moment.
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    53
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    54
Similarly, when proving a statement about terms inductively, we need
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    55
to prove a related statement about term lists simultaneously. For example,
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    56
the fact that the identity substitution does not change a term needs to be
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    57
strengthened and proved as follows:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    58
\end{isamarkuptext}%
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    59
\isacommand{lemma}\ {\isachardoublequote}subst\ \ Var\ t\ \ {\isacharequal}\ {\isacharparenleft}t\ {\isacharcolon}{\isacharcolon}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term{\isacharparenright}\ \ {\isasymand}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    60
\ \ \ \ \ \ \ \ substs\ Var\ ts\ {\isacharequal}\ {\isacharparenleft}ts{\isacharcolon}{\isacharcolon}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}term\ list{\isacharparenright}{\isachardoublequote}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
    61
\isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ t\ \isakeyword{and}\ ts{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    62
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    63
\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    64
Note that \isa{term{\isachardot}Var} is the identity substitution because by definition it
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    65
leaves variables unchanged: \isa{subst\ term{\isachardot}Var\ {\isacharparenleft}term{\isachardot}Var\ x{\isacharparenright}\ {\isacharequal}\ term{\isachardot}Var\ x}. Note also
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    66
that the type annotations are necessary because otherwise there is nothing in
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    67
the goal to enforce that both halves of the goal talk about the same type
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    68
parameters \isa{{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}b{\isacharparenright}}. As a result, induction would fail
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    69
because the two halves of the goal would be unrelated.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    70
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    71
\begin{exercise}
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    72
The fact that substitution distributes over composition can be expressed
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    73
roughly as follows:
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    74
\begin{quote}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    75
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    76
\begin{isabelle}%
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    77
subst\ {\isacharparenleft}f\ o\ g{\isacharparenright}\ t\ {\isacharequal}\ subst\ f\ {\isacharparenleft}subst\ g\ t{\isacharparenright}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    78
\end{isabelle}%
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    79
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    80
\end{quote}
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    81
Correct this statement (you will find that it does not type-check),
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    82
strengthen it, and prove it. (Note: \isaindexbold{o} is function composition;
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    83
its definition is found in theorem \isa{o{\isacharunderscore}def}).
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    84
\end{exercise}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    85
\begin{exercise}\label{ex:trev-trev}
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    86
  Define a function \isa{trev} of type \isa{{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ term\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ term}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    87
that recursively reverses the order of arguments of all function symbols in a
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    88
  term. Prove that \isa{trev\ {\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t}.
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    89
\end{exercise}
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    90
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    91
The experienced functional programmer may feel that our above definition of
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    92
\isa{subst} is unnecessarily complicated in that \isa{substs} is
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    93
completely unnecessary. The \isa{App}-case can be defined directly as
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    94
\begin{quote}
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    95
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    96
\begin{isabelle}%
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    97
subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}map\ {\isacharparenleft}subst\ s{\isacharparenright}\ ts{\isacharparenright}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    98
\end{isabelle}%
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
    99
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   100
\end{quote}
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   101
where \isa{map} is the standard list function such that
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
   102
\isa{map\ f\ {\isacharbrackleft}x\isadigit{1}{\isacharcomma}{\isachardot}{\isachardot}{\isachardot}{\isacharcomma}xn{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}f\ x\isadigit{1}{\isacharcomma}{\isachardot}{\isachardot}{\isachardot}{\isacharcomma}f\ xn{\isacharbrackright}}. This is true, but Isabelle
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
   103
insists on the above fixed format. Fortunately, we can easily \emph{prove}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
   104
that the suggested equation holds:%
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   105
\end{isamarkuptext}%
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
   106
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}map\ {\isacharparenleft}subst\ s{\isacharparenright}\ ts{\isacharparenright}{\isachardoublequote}\isanewline
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
   107
\isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ ts{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   108
\begin{isamarkuptext}%
9689
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   109
\noindent
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   110
What is more, we can now disable the old defining equation as a
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   111
simplification rule:%
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   112
\end{isamarkuptext}%
9698
f0740137a65d updated;
wenzelm
parents: 9689
diff changeset
   113
\isacommand{lemmas}\ {\isacharbrackleft}simp\ del{\isacharbrackright}\ {\isacharequal}\ subst{\isacharunderscore}App%
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   114
\begin{isamarkuptext}%
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   115
\noindent
9689
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   116
The advantage is that now we have replaced \isa{substs} by
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   117
\isa{map}, we can profit from the large number of pre-proved lemmas
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   118
about \isa{map}.  Unfortunately inductive proofs about type
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   119
\isa{term} are still awkward because they expect a conjunction. One
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   120
could derive a new induction principle as well (see
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   121
\S\ref{sec:derive-ind}), but turns out to be simpler to define
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   122
functions by \isacommand{recdef} instead of \isacommand{primrec}.
751fde5307e4 *** empty log message ***
nipkow
parents: 9644
diff changeset
   123
The details are explained in \S\ref{sec:advanced-recdef} below.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   124
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   125
Of course, you may also combine mutual and nested recursion. For example,
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   126
constructor \isa{Sum} in \S\ref{sec:datatype-mut-rec} could take a list of
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
   127
expressions as its argument: \isa{Sum}~\isa{{\isachardoublequote}{\isacharprime}a\ aexp\ list{\isachardoublequote}}.%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   128
\end{isamarkuptext}%
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
   129
\end{isabellebody}%
9145
9f7b8de5bfaf updated;
wenzelm
parents: 8751
diff changeset
   130
%%% Local Variables:
9f7b8de5bfaf updated;
wenzelm
parents: 8751
diff changeset
   131
%%% mode: latex
9f7b8de5bfaf updated;
wenzelm
parents: 8751
diff changeset
   132
%%% TeX-master: "root"
9f7b8de5bfaf updated;
wenzelm
parents: 8751
diff changeset
   133
%%% End: