doc-src/TutorialI/Inductive/advanced-examples.tex
author nipkow
Thu, 25 Jan 2001 15:31:31 +0100
changeset 10978 5eebea8f359f
parent 10889 aed0a0450797
child 11147 d848c6693185
permissions -rw-r--r--
*** empty log message ***
paulson@10879
     1
% $Id$
paulson@10879
     2
This section describes advanced features of inductive definitions. 
paulson@10879
     3
The premises of introduction rules may contain universal quantifiers and
paulson@10879
     4
monotonic functions.  Theorems may be proved by rule inversion.
paulson@10879
     5
paulson@10879
     6
\subsection{Universal Quantifiers in Introduction Rules}
paulson@10879
     7
\label{sec:gterm-datatype}
paulson@10879
     8
paulson@10879
     9
As a running example, this section develops the theory of \textbf{ground
paulson@10879
    10
terms}: terms constructed from constant and function 
paulson@10879
    11
symbols but not variables. To simplify matters further, we regard a
paulson@10879
    12
constant as a function applied to the null argument  list.  Let us declare a
paulson@10879
    13
datatype \isa{gterm} for the type of ground  terms. It is a type constructor
paulson@10879
    14
whose argument is a type of  function symbols. 
paulson@10879
    15
\begin{isabelle}
paulson@10879
    16
\isacommand{datatype}\ 'f\ gterm\ =\ Apply\ 'f\ "'f\ gterm\ list"
paulson@10879
    17
\end{isabelle}
paulson@10879
    18
To try it out, we declare a datatype of some integer operations: 
paulson@10879
    19
integer constants, the unary minus operator and the addition 
paulson@10879
    20
operator. 
paulson@10879
    21
\begin{isabelle}
paulson@10879
    22
\isacommand{datatype}\ integer_op\ =\ Number\ int\ |\ UnaryMinus\ |\ Plus
paulson@10879
    23
\end{isabelle}
paulson@10879
    24
Now the type \isa{integer\_op gterm} denotes the ground 
paulson@10879
    25
terms built over those symbols.
paulson@10879
    26
paulson@10879
    27
The type constructor \texttt{gterm} can be generalized to a function 
paulson@10879
    28
over sets.  It returns 
paulson@10879
    29
the set of ground terms that can be formed over a set \isa{F} of function symbols. For
paulson@10879
    30
example,  we could consider the set of ground terms formed from the finite 
paulson@10889
    31
set \isa{\isacharbraceleft Number 2, UnaryMinus,
paulson@10889
    32
Plus\isacharbraceright}.
paulson@10879
    33
paulson@10879
    34
This concept is inductive. If we have a list \isa{args} of ground terms 
paulson@10879
    35
over~\isa{F} and a function symbol \isa{f} in \isa{F}, then we 
paulson@10879
    36
can apply \isa{f} to  \isa{args} to obtain another ground term. 
paulson@10879
    37
The only difficulty is that the argument list may be of any length. Hitherto, 
paulson@10879
    38
each rule in an inductive definition referred to the inductively 
paulson@10879
    39
defined set a fixed number of times, typically once or twice. 
paulson@10879
    40
A universal quantifier in the premise of the introduction rule 
paulson@10879
    41
expresses that every element of \isa{args} belongs
paulson@10879
    42
to our inductively defined set: is a ground term 
paulson@10879
    43
over~\isa{F}.  The function {\isa{set}} denotes the set of elements in a given 
paulson@10879
    44
list. 
paulson@10879
    45
\begin{isabelle}
paulson@10879
    46
\isacommand{consts}\ gterms\ ::\ "'f\ set\ \isasymRightarrow \ 'f\ gterm\ set"\isanewline
paulson@10879
    47
\isacommand{inductive}\ "gterms\ F"\isanewline
paulson@10879
    48
\isakeyword{intros}\isanewline
paulson@10879
    49
step[intro!]:\ "\isasymlbrakk \isasymforall t\ \isasymin \ set\ args.\ t\ \isasymin \ gterms\ F;\ \ f\ \isasymin \ F\isasymrbrakk \isanewline
paulson@10879
    50
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ (Apply\ f\ args)\ \isasymin \ gterms\
paulson@10879
    51
F"
paulson@10879
    52
\end{isabelle}
paulson@10879
    53
paulson@10879
    54
To demonstrate a proof from this definition, let us 
paulson@10879
    55
show that the function \isa{gterms}
paulson@10879
    56
is \textbf{monotonic}.  We shall need this concept shortly.
paulson@10879
    57
\begin{isabelle}
paulson@10879
    58
\isacommand{lemma}\ gterms_mono:\ "F\isasymsubseteq G\ \isasymLongrightarrow \ gterms\ F\ \isasymsubseteq \ gterms\ G"\isanewline
paulson@10879
    59
\isacommand{apply}\ clarify\isanewline
paulson@10879
    60
\isacommand{apply}\ (erule\ gterms.induct)\isanewline
paulson@10879
    61
\isacommand{apply}\ blast\isanewline
paulson@10879
    62
\isacommand{done}
paulson@10879
    63
\end{isabelle}
paulson@10879
    64
Intuitively, this theorem says that
paulson@10879
    65
enlarging the set of function symbols enlarges the set of ground 
paulson@10879
    66
terms. The proof is a trivial rule induction.
paulson@10879
    67
First we use the \isa{clarify} method to assume the existence of an element of
paulson@10879
    68
\isa{terms~F}.  (We could have used \isa{intro subsetI}.)  We then
paulson@10879
    69
apply rule induction. Here is the resulting subgoal: 
paulson@10879
    70
\begin{isabelle}
paulson@10879
    71
\ 1.\ \isasymAnd x\ args\ f.\isanewline
paulson@10879
    72
\ \ \ \ \ \ \ \isasymlbrakk F\ \isasymsubseteq \ G;\ \isasymforall t\isasymin set\ args.\ t\ \isasymin \ gterms\ F\ \isasymand \ t\ \isasymin \ gterms\ G;\ f\ \isasymin \ F\isasymrbrakk \isanewline
paulson@10879
    73
\ \ \ \ \ \ \ \isasymLongrightarrow \ Apply\ f\ args\ \isasymin \ gterms\ G%
paulson@10879
    74
\end{isabelle}
paulson@10879
    75
%
paulson@10879
    76
The assumptions state that \isa{f} belongs 
paulson@10879
    77
to~\isa{F}, which is included in~\isa{G}, and that every element of the list \isa{args} is
paulson@10879
    78
a ground term over~\isa{G}.  The \isa{blast} method finds this chain of reasoning easily.  
paulson@10879
    79
paulson@10879
    80
\begin{warn}
paulson@10879
    81
Why do we call this function \isa{gterms} instead 
paulson@10879
    82
of {\isa{gterm}}?  A constant may have the same name as a type.  However,
paulson@10879
    83
name  clashes could arise in the theorems that Isabelle generates. 
paulson@10879
    84
Our choice of names keeps {\isa{gterms.induct}} separate from 
paulson@10879
    85
{\isa{gterm.induct}}.
paulson@10879
    86
\end{warn}
paulson@10879
    87
paulson@10879
    88
paulson@10879
    89
\subsection{Rule Inversion}\label{sec:rule-inversion}
paulson@10879
    90
paulson@10879
    91
Case analysis on an inductive definition is called \textbf{rule inversion}. 
paulson@10879
    92
It is frequently used in proofs about operational semantics.  It can be
paulson@10879
    93
highly effective when it is applied automatically.  Let us look at how rule
paulson@10879
    94
inversion is done in Isabelle.
paulson@10879
    95
paulson@10879
    96
Recall that \isa{even} is the minimal set closed under these two rules:
paulson@10879
    97
\begin{isabelle}
paulson@10879
    98
0\ \isasymin \ even\isanewline
paulson@10879
    99
n\ \isasymin \ even\ \isasymLongrightarrow \ (Suc\ (Suc\ n))\ \isasymin
paulson@10879
   100
\ even
paulson@10879
   101
\end{isabelle}
paulson@10879
   102
Minimality means that \isa{even} contains only the elements that these
paulson@10879
   103
rules force it to contain.  If we are told that \isa{a}
paulson@10879
   104
belongs to
paulson@10879
   105
\isa{even} then there are only two possibilities.  Either \isa{a} is \isa{0}
paulson@10879
   106
or else \isa{a} has the form \isa{Suc(Suc~n)}, for an arbitrary \isa{n}
paulson@10879
   107
that belongs to
paulson@10879
   108
\isa{even}.  That is the gist of the \isa{cases} rule, which Isabelle proves
paulson@10879
   109
for us when it accepts an inductive definition:
paulson@10879
   110
\begin{isabelle}
paulson@10879
   111
\isasymlbrakk a\ \isasymin \ even;\isanewline
paulson@10879
   112
\ a\ =\ 0\ \isasymLongrightarrow \ P;\isanewline
paulson@10879
   113
\ \isasymAnd n.\ \isasymlbrakk a\ =\ Suc(Suc\ n);\ n\ \isasymin \
paulson@10879
   114
even\isasymrbrakk \ \isasymLongrightarrow \ P\isasymrbrakk \
paulson@10879
   115
\isasymLongrightarrow \ P
paulson@10879
   116
\rulename{even.cases}
paulson@10879
   117
\end{isabelle}
paulson@10879
   118
paulson@10879
   119
This general rule is less useful than instances of it for
paulson@10879
   120
specific patterns.  For example, if \isa{a} has the form
paulson@10879
   121
\isa{Suc(Suc~n)} then the first case becomes irrelevant, while the second
paulson@10879
   122
case tells us that \isa{n} belongs to \isa{even}.  Isabelle will generate
paulson@10879
   123
this instance for us:
paulson@10879
   124
\begin{isabelle}
paulson@10879
   125
\isacommand{inductive\_cases}\ Suc_Suc_cases\ [elim!]:
paulson@10879
   126
\ "Suc(Suc\ n)\ \isasymin \ even"
paulson@10879
   127
\end{isabelle}
paulson@10879
   128
The \isacommand{inductive\_cases} command generates an instance of the
paulson@10879
   129
\isa{cases} rule for the supplied pattern and gives it the supplied name:
paulson@10879
   130
%
paulson@10879
   131
\begin{isabelle}
paulson@10879
   132
\isasymlbrakk Suc\ (Suc\ n)\ \isasymin \ even;\ n\ \isasymin \ even\
paulson@10879
   133
\isasymLongrightarrow \ P\isasymrbrakk \ \isasymLongrightarrow \ P%
paulson@10879
   134
\rulename{Suc_Suc_cases}
paulson@10879
   135
\end{isabelle}
paulson@10879
   136
%
paulson@10879
   137
Applying this as an elimination rule yields one case where \isa{even.cases}
paulson@10879
   138
would yield two.  Rule inversion works well when the conclusions of the
paulson@10879
   139
introduction rules involve datatype constructors like \isa{Suc} and \isa{\#}
nipkow@10978
   140
(list ``cons''); freeness reasoning discards all but one or two cases.
paulson@10879
   141
paulson@10879
   142
In the \isacommand{inductive\_cases} command we supplied an
paulson@10879
   143
attribute, \isa{elim!}, indicating that this elimination rule can be applied
paulson@10879
   144
aggressively.  The original
paulson@10879
   145
\isa{cases} rule would loop if used in that manner because the
paulson@10879
   146
pattern~\isa{a} matches everything.
paulson@10879
   147
paulson@10879
   148
The rule \isa{Suc_Suc_cases} is equivalent to the following implication:
paulson@10879
   149
\begin{isabelle}
paulson@10879
   150
Suc (Suc\ n)\ \isasymin \ even\ \isasymLongrightarrow \ n\ \isasymin \
paulson@10879
   151
even
paulson@10879
   152
\end{isabelle}
paulson@10879
   153
%
paulson@10879
   154
In {\S}\ref{sec:gen-rule-induction} we devoted some effort to proving precisely
paulson@10879
   155
this result.  Yet we could have obtained it by a one-line declaration. 
paulson@10879
   156
This example also justifies the terminology \textbf{rule inversion}: the new
paulson@10879
   157
rule inverts the introduction rule \isa{even.step}.
paulson@10879
   158
paulson@10879
   159
For one-off applications of rule inversion, use the \isa{ind_cases} method. 
paulson@10879
   160
Here is an example:
paulson@10879
   161
\begin{isabelle}
paulson@10879
   162
\isacommand{apply}\ (ind_cases\ "Suc(Suc\ n)\ \isasymin \ even")
paulson@10879
   163
\end{isabelle}
paulson@10879
   164
The specified instance of the \isa{cases} rule is generated, applied, and
paulson@10879
   165
discarded.
paulson@10879
   166
paulson@10879
   167
\medskip
paulson@10879
   168
Let us try rule inversion on our ground terms example:
paulson@10879
   169
\begin{isabelle}
paulson@10879
   170
\isacommand{inductive\_cases}\ gterm_Apply_elim\ [elim!]:\ "Apply\ f\ args\
paulson@10879
   171
\isasymin\ gterms\ F"
paulson@10879
   172
\end{isabelle}
paulson@10879
   173
%
paulson@10879
   174
Here is the result.  No cases are discarded (there was only one to begin
paulson@10879
   175
with) but the rule applies specifically to the pattern \isa{Apply\ f\ args}.
paulson@10879
   176
It can be applied repeatedly as an elimination rule without looping, so we
paulson@10879
   177
have given the
paulson@10879
   178
\isa{elim!}\ attribute. 
paulson@10879
   179
\begin{isabelle}
paulson@10879
   180
\isasymlbrakk Apply\ f\ args\ \isasymin \ gterms\ F;\isanewline
paulson@10879
   181
\ \isasymlbrakk
paulson@10879
   182
\isasymforall t\isasymin set\ args.\ t\ \isasymin \ gterms\ F;\ f\ \isasymin
paulson@10879
   183
\ F\isasymrbrakk \ \isasymLongrightarrow \ P\isasymrbrakk\isanewline
paulson@10879
   184
\isasymLongrightarrow \ P%
paulson@10879
   185
\rulename{gterm_Apply_elim}
paulson@10879
   186
\end{isabelle}
paulson@10879
   187
paulson@10879
   188
This rule replaces an assumption about \isa{Apply\ f\ args} by 
paulson@10879
   189
assumptions about \isa{f} and~\isa{args}.  Here is a proof in which this
paulson@10879
   190
inference is essential.  We show that if \isa{t} is a ground term over both
paulson@10879
   191
of the sets
paulson@10879
   192
\isa{F} and~\isa{G} then it is also a ground term over their intersection,
paulson@10879
   193
\isa{F\isasyminter G}.
paulson@10879
   194
\begin{isabelle}
paulson@10879
   195
\isacommand{lemma}\ gterms_IntI\ [rule_format]:\isanewline
paulson@10879
   196
\ \ \ \ \ "t\ \isasymin \ gterms\ F\ \isasymLongrightarrow \ t\ \isasymin \ gterms\ G\ \isasymlongrightarrow \ t\ \isasymin \ gterms\ (F\isasyminter G)"\isanewline
paulson@10879
   197
\isacommand{apply}\ (erule\ gterms.induct)\isanewline
paulson@10879
   198
\isacommand{apply}\ blast\isanewline
paulson@10879
   199
\isacommand{done}
paulson@10879
   200
\end{isabelle}
paulson@10879
   201
%
paulson@10879
   202
The proof begins with rule induction over the definition of
paulson@10879
   203
\isa{gterms}, which leaves a single subgoal:  
paulson@10879
   204
\begin{isabelle}
paulson@10879
   205
1.\ \isasymAnd args\ f.\isanewline
paulson@10879
   206
\ \ \ \ \ \ \isasymlbrakk \isasymforall t\isasymin set\ args.\ t\ \isasymin \ gterms\ F\ \isasymand\isanewline
paulson@10879
   207
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (t\ \isasymin \ gterms\ G\ \isasymlongrightarrow \ t\ \isasymin \ gterms\ (F\ \isasyminter \ G));\isanewline
paulson@10879
   208
\ \ \ \ \ \ \ f\ \isasymin \ F\isasymrbrakk \isanewline
paulson@10879
   209
\ \ \ \ \ \ \isasymLongrightarrow \ Apply\ f\ args\ \isasymin \ gterms\ G\ \isasymlongrightarrow \ Apply\ f\ args\ \isasymin \ gterms\ (F\ \isasyminter \ G)
paulson@10879
   210
\end{isabelle}
paulson@10879
   211
%
paulson@10879
   212
The induction hypothesis states that every element of \isa{args} belongs to 
paulson@10879
   213
\isa{gterms\ (F\ \isasyminter \ G)} --- provided it already belongs to
paulson@10879
   214
\isa{gterms\ G}.  How do we meet that condition?  
paulson@10879
   215
paulson@10879
   216
By assuming (as we may) the formula \isa{Apply\ f\ args\ \isasymin \ gterms\
paulson@10879
   217
G}.  Rule inversion, in the form of \isa{gterm_Apply_elim}, infers that every
paulson@10879
   218
element of \isa{args} belongs to 
paulson@10879
   219
\isa{gterms~G}.  It also yields \isa{f\ \isasymin \ G}, which will allow us
paulson@10879
   220
to conclude \isa{f\ \isasymin \ F\ \isasyminter \ G}.  All of this reasoning
paulson@10879
   221
is done by \isa{blast}.
paulson@10879
   222
paulson@10879
   223
\medskip
paulson@10879
   224
paulson@10879
   225
To summarize, every inductive definition produces a \isa{cases} rule.  The
paulson@10879
   226
\isacommand{inductive\_cases} command stores an instance of the \isa{cases}
paulson@10879
   227
rule for a given pattern.  Within a proof, the \isa{ind_cases} method
paulson@10879
   228
applies an instance of the \isa{cases}
paulson@10879
   229
rule.
paulson@10879
   230
paulson@10879
   231
paulson@10879
   232
\subsection{Continuing the Ground Terms Example}
paulson@10879
   233
paulson@10879
   234
Call a term \textbf{well-formed} if each symbol occurring in it has 
paulson@10879
   235
the correct number of arguments. To formalize this concept, we 
paulson@10879
   236
introduce a function mapping each symbol to its \textbf{arity}, a natural 
paulson@10879
   237
number. 
paulson@10879
   238
paulson@10879
   239
Let us define the set of well-formed ground terms. 
paulson@10879
   240
Suppose we are given a function called \isa{arity}, specifying the arities to be used.
paulson@10879
   241
In the inductive step, we have a list \isa{args} of such terms and a function 
paulson@10879
   242
symbol~\isa{f}. If the length of the list matches the function's arity 
paulson@10879
   243
then applying \isa{f} to \isa{args} yields a well-formed term. 
paulson@10879
   244
\begin{isabelle}
paulson@10879
   245
\isacommand{consts}\ well_formed_gterm\ ::\ "('f\ \isasymRightarrow \ nat)\ \isasymRightarrow \ 'f\ gterm\ set"\isanewline
paulson@10879
   246
\isacommand{inductive}\ "well_formed_gterm\ arity"\isanewline
paulson@10879
   247
\isakeyword{intros}\isanewline
paulson@10879
   248
step[intro!]:\ "\isasymlbrakk \isasymforall t\ \isasymin \ set\ args.\ t\ \isasymin \ well_formed_gterm\ arity;\ \ \isanewline
paulson@10879
   249
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ length\ args\ =\ arity\ f\isasymrbrakk \isanewline
paulson@10879
   250
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ (Apply\ f\ args)\ \isasymin \ well_formed_gterm\
paulson@10879
   251
arity"
paulson@10879
   252
\end{isabelle}
paulson@10879
   253
%
paulson@10879
   254
The inductive definition neatly captures the reasoning above.
paulson@10879
   255
It is just an elaboration of the previous one, consisting of a single 
paulson@10879
   256
introduction rule. The universal quantification over the
paulson@10879
   257
\isa{set} of arguments expresses that all of them are well-formed.
paulson@10879
   258
paulson@10879
   259
\subsection{Alternative Definition Using a Monotonic Function}
paulson@10879
   260
paulson@10879
   261
An inductive definition may refer to the inductively defined 
paulson@10879
   262
set through an arbitrary monotonic function.  To demonstrate this
paulson@10879
   263
powerful feature, let us
paulson@10879
   264
change the  inductive definition above, replacing the
paulson@10879
   265
quantifier by a use of the function \isa{lists}. This
paulson@10879
   266
function, from the Isabelle theory of lists, is analogous to the
paulson@10879
   267
function \isa{gterms} declared above: if \isa{A} is a set then
paulson@10879
   268
{\isa{lists A}} is the set of lists whose elements belong to
paulson@10879
   269
\isa{A}.  
paulson@10879
   270
paulson@10879
   271
In the inductive definition of well-formed terms, examine the one
paulson@10879
   272
introduction rule.  The first premise states that \isa{args} belongs to
paulson@10879
   273
the \isa{lists} of well-formed terms.  This formulation is more
paulson@10879
   274
direct, if more obscure, than using a universal quantifier.
paulson@10879
   275
\begin{isabelle}
paulson@10879
   276
\isacommand{consts}\ well_formed_gterm'\ ::\ "('f\ \isasymRightarrow \ nat)\ \isasymRightarrow \ 'f\ gterm\ set"\isanewline
paulson@10879
   277
\isacommand{inductive}\ "well_formed_gterm'\ arity"\isanewline
paulson@10879
   278
\isakeyword{intros}\isanewline
paulson@10879
   279
step[intro!]:\ "\isasymlbrakk args\ \isasymin \ lists\ (well_formed_gterm'\ arity);\ \ \isanewline
paulson@10879
   280
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ length\ args\ =\ arity\ f\isasymrbrakk \isanewline
paulson@10879
   281
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ (Apply\ f\ args)\ \isasymin \ well_formed_gterm'\ arity"\isanewline
paulson@10879
   282
\isakeyword{monos}\ lists_mono
paulson@10879
   283
\end{isabelle}
paulson@10879
   284
paulson@10879
   285
We must cite the theorem \isa{lists_mono} in order to justify 
paulson@10879
   286
using the function \isa{lists}. 
paulson@10879
   287
\begin{isabelle}
paulson@10879
   288
A\ \isasymsubseteq\ B\ \isasymLongrightarrow \ lists\ A\ \isasymsubseteq
paulson@10879
   289
\ lists\ B\rulename{lists_mono}
paulson@10879
   290
\end{isabelle}
paulson@10879
   291
%
paulson@10879
   292
Why must the function be monotonic?  An inductive definition describes
paulson@10879
   293
an iterative construction: each element of the set is constructed by a
paulson@10879
   294
finite number of introduction rule applications.  For example, the
paulson@10879
   295
elements of \isa{even} are constructed by finitely many applications of
paulson@10879
   296
the rules 
paulson@10879
   297
\begin{isabelle}
paulson@10879
   298
0\ \isasymin \ even\isanewline
paulson@10879
   299
n\ \isasymin \ even\ \isasymLongrightarrow \ (Suc\ (Suc\ n))\ \isasymin
paulson@10879
   300
\ even
paulson@10879
   301
\end{isabelle}
paulson@10879
   302
All references to a set in its
paulson@10879
   303
inductive definition must be positive.  Applications of an
paulson@10879
   304
introduction rule cannot invalidate previous applications, allowing the
paulson@10879
   305
construction process to converge.
paulson@10879
   306
The following pair of rules do not constitute an inductive definition:
paulson@10879
   307
\begin{isabelle}
paulson@10879
   308
0\ \isasymin \ even\isanewline
paulson@10879
   309
n\ \isasymnotin \ even\ \isasymLongrightarrow \ (Suc\ n)\ \isasymin
paulson@10879
   310
\ even
paulson@10879
   311
\end{isabelle}
paulson@10879
   312
%
paulson@10879
   313
Showing that 4 is even using these rules requires showing that 3 is not
paulson@10879
   314
even.  It is far from trivial to show that this set of rules
paulson@10879
   315
characterizes the even numbers.  
paulson@10879
   316
paulson@10879
   317
Even with its use of the function \isa{lists}, the premise of our
paulson@10879
   318
introduction rule is positive:
paulson@10879
   319
\begin{isabelle}
paulson@10879
   320
args\ \isasymin \ lists\ (well_formed_gterm'\ arity)
paulson@10879
   321
\end{isabelle}
paulson@10879
   322
To apply the rule we construct a list \isa{args} of previously
paulson@10879
   323
constructed well-formed terms.  We obtain a
paulson@10879
   324
new term, \isa{Apply\ f\ args}.  Because \isa{lists} is monotonic,
paulson@10879
   325
applications of the rule remain valid as new terms are constructed.
paulson@10879
   326
Further lists of well-formed
paulson@10879
   327
terms become available and none are taken away.
paulson@10879
   328
paulson@10879
   329
paulson@10879
   330
\subsection{A Proof of Equivalence}
paulson@10879
   331
paulson@10879
   332
We naturally hope that these two inductive definitions of ``well-formed'' 
paulson@10879
   333
coincide.  The equality can be proved by separate inclusions in 
paulson@10879
   334
each direction.  Each is a trivial rule induction. 
paulson@10879
   335
\begin{isabelle}
paulson@10879
   336
\isacommand{lemma}\ "well_formed_gterm\ arity\ \isasymsubseteq \ well_formed_gterm'\ arity"\isanewline
paulson@10879
   337
\isacommand{apply}\ clarify\isanewline
paulson@10879
   338
\isacommand{apply}\ (erule\ well_formed_gterm.induct)\isanewline
paulson@10879
   339
\isacommand{apply}\ auto\isanewline
paulson@10879
   340
\isacommand{done}
paulson@10879
   341
\end{isabelle}
paulson@10879
   342
paulson@10879
   343
The \isa{clarify} method gives
paulson@10879
   344
us an element of \isa{well_formed_gterm\ arity} on which to perform 
paulson@10879
   345
induction.  The resulting subgoal can be proved automatically:
paulson@10879
   346
\begin{isabelle}
paulson@10879
   347
{\isadigit{1}}{\isachardot}\ {\isasymAnd}x\ args\ f{\isachardot}\isanewline
paulson@10879
   348
\ \ \ \ \ \ {\isasymlbrakk}{\isasymforall}t{\isasymin}set\ args{\isachardot}\isanewline
paulson@10879
   349
\ \ \ \ \ \ \ \ \ \ t\ {\isasymin}\ well{\isacharunderscore}formed{\isacharunderscore}gterm\ arity\ {\isasymand}\ t\ {\isasymin}\ well{\isacharunderscore}formed{\isacharunderscore}gterm{\isacharprime}\ arity{\isacharsemicolon}\isanewline
paulson@10879
   350
\ \ \ \ \ \ \ length\ args\ {\isacharequal}\ arity\ f{\isasymrbrakk}\isanewline
paulson@10879
   351
\ \ \ \ \ \ {\isasymLongrightarrow}\ Apply\ f\ args\ {\isasymin}\ well{\isacharunderscore}formed{\isacharunderscore}gterm{\isacharprime}\ arity%
paulson@10879
   352
\end{isabelle}
paulson@10879
   353
%
paulson@10879
   354
This proof resembles the one given in
paulson@10879
   355
{\S}\ref{sec:gterm-datatype} above, especially in the form of the
paulson@10879
   356
induction hypothesis.  Next, we consider the opposite inclusion:
paulson@10879
   357
\begin{isabelle}
paulson@10879
   358
\isacommand{lemma}\ "well_formed_gterm'\ arity\ \isasymsubseteq \ well_formed_gterm\ arity"\isanewline
paulson@10879
   359
\isacommand{apply}\ clarify\isanewline
paulson@10879
   360
\isacommand{apply}\ (erule\ well_formed_gterm'.induct)\isanewline
paulson@10879
   361
\isacommand{apply}\ auto\isanewline
paulson@10879
   362
\isacommand{done}
paulson@10879
   363
\end{isabelle}
paulson@10879
   364
%
paulson@10879
   365
The proof script is identical, but the subgoal after applying induction may
paulson@10879
   366
be surprising:
paulson@10879
   367
\begin{isabelle}
paulson@10879
   368
1.\ \isasymAnd x\ args\ f.\isanewline
paulson@10879
   369
\ \ \ \ \ \ \isasymlbrakk args\ \isasymin \ lists\ (well_formed_gterm'\ arity\ \isasyminter\isanewline
paulson@10879
   370
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isacharbraceleft u.\ u\ \isasymin \ well_formed_gterm\ arity\isacharbraceright );\isanewline
paulson@10879
   371
\ \ \ \ \ \ \ length\ args\ =\ arity\ f\isasymrbrakk \isanewline
paulson@10879
   372
\ \ \ \ \ \ \isasymLongrightarrow \ Apply\ f\ args\ \isasymin \ well_formed_gterm\ arity%
paulson@10879
   373
\end{isabelle}
paulson@10879
   374
The induction hypothesis contains an application of \isa{lists}.  Using a
paulson@10879
   375
monotonic function in the inductive definition always has this effect.  The
paulson@10879
   376
subgoal may look uninviting, but fortunately a useful rewrite rule concerning
paulson@10879
   377
\isa{lists} is available:
paulson@10879
   378
\begin{isabelle}
paulson@10879
   379
lists\ (A\ \isasyminter \ B)\ =\ lists\ A\ \isasyminter \ lists\ B
paulson@10879
   380
\rulename{lists_Int_eq}
paulson@10879
   381
\end{isabelle}
paulson@10879
   382
%
paulson@10879
   383
Thanks to this default simplification rule, the induction hypothesis 
paulson@10879
   384
is quickly replaced by its two parts:
paulson@10879
   385
\begin{isabelle}
paulson@10879
   386
\ \ \ \ \ \ \ args\ \isasymin \ lists\ (well_formed_gterm'\ arity)\isanewline
paulson@10879
   387
\ \ \ \ \ \ \ args\ \isasymin \ lists\ (well_formed_gterm\ arity)
paulson@10879
   388
\end{isabelle}
paulson@10879
   389
Invoking the rule \isa{well_formed_gterm.step} completes the proof.  The
paulson@10879
   390
call to
paulson@10879
   391
\isa{auto} does all this work.
paulson@10879
   392
paulson@10879
   393
This example is typical of how monotonic functions can be used.  In
paulson@10879
   394
particular, a rewrite rule analogous to \isa{lists_Int_eq} holds in most
paulson@10879
   395
cases.  Monotonicity implies one direction of this set equality; we have
paulson@10879
   396
this theorem:
paulson@10879
   397
\begin{isabelle}
paulson@10879
   398
mono\ f\ \isasymLongrightarrow \ f\ (A\ \isasyminter \ B)\ \isasymsubseteq \
paulson@10879
   399
f\ A\ \isasyminter \ f\ B%
paulson@10879
   400
\rulename{mono_Int}
paulson@10879
   401
\end{isabelle}
paulson@10879
   402
paulson@10879
   403
paulson@10879
   404
To summarize: a universal quantifier in an introduction rule 
paulson@10879
   405
lets it refer to any number of instances of 
paulson@10879
   406
the inductively defined set.  A monotonic function in an introduction 
paulson@10879
   407
rule lets it refer to constructions over the inductively defined 
paulson@10879
   408
set.  Each element of an inductively defined set is created 
paulson@10879
   409
through finitely many applications of the introduction rules.  So each rule
paulson@10879
   410
must be positive, and never can it refer to \textit{infinitely} many
paulson@10879
   411
previous instances of the inductively defined set. 
paulson@10879
   412
paulson@10879
   413
paulson@10879
   414
paulson@10879
   415
\begin{exercise}
paulson@10879
   416
Prove this theorem, one direction of which was proved in
paulson@10879
   417
{\S}\ref{sec:rule-inversion} above.
paulson@10879
   418
\begin{isabelle}
paulson@10879
   419
\isacommand{lemma}\ gterms_Int_eq\ [simp]:\ "gterms\ (F\isasyminter G)\ =\
paulson@10879
   420
gterms\ F\ \isasyminter \ gterms\ G"\isanewline
paulson@10879
   421
\end{isabelle}
paulson@10879
   422
\end{exercise}
paulson@10879
   423
paulson@10879
   424
paulson@10879
   425
\begin{exercise}
paulson@10879
   426
A function mapping function symbols to their 
paulson@10879
   427
types is called a \textbf{signature}.  Given a type 
paulson@10879
   428
ranging over type symbols, we can represent a function's type by a
paulson@10879
   429
list of argument types paired with the result type. 
paulson@10879
   430
Complete this inductive definition:
paulson@10879
   431
\begin{isabelle}
paulson@10879
   432
\isacommand{consts}\ well_typed_gterm::\ "('f\ \isasymRightarrow \ 't\ list\ *\ 't)\ \isasymRightarrow \ ('f\ gterm\ *\ 't)set"\isanewline
paulson@10879
   433
\isacommand{inductive}\ "well_typed_gterm\ sig"\isanewline
paulson@10879
   434
\end{isabelle}
paulson@10879
   435
\end{exercise}