ProgTutorial/Package/Ind_Prelims.thy
changeset 295 24c68350d059
parent 244 dc95a56b1953
child 346 0fea8b7a14a1
equal deleted inserted replaced
294:ee9d53fbb56b 295:24c68350d059
     7 text {*
     7 text {*
     8   The user will just give a specification of inductive predicate(s) and
     8   The user will just give a specification of inductive predicate(s) and
     9   expects from the package to produce a convenient reasoning
     9   expects from the package to produce a convenient reasoning
    10   infrastructure. This infrastructure needs to be derived from the definition
    10   infrastructure. This infrastructure needs to be derived from the definition
    11   that correspond to the specified predicate(s). Before we start with
    11   that correspond to the specified predicate(s). Before we start with
    12   explaining all parts of the package, let us first give some examples 
    12   explaining all parts of the package, let us first give some examples showing
    13   showing how to define inductive predicates and then also how
    13   how to define inductive predicates and then also how to generate a reasoning
    14   to generate a reasoning infrastructure for them. From the examples 
    14   infrastructure for them. From the examples we will figure out a general
    15   we will figure out a general method for
    15   method for defining inductive predicates. This is usually the first step in
    16   defining inductive predicates.  The aim in this section is \emph{not} to
    16   writing a package for Isabelle. The aim in this section is \emph{not} to
    17   write proofs that are as beautiful as possible, but as close as possible to
    17   write proofs that are as beautiful as possible, but as close as possible to
    18   the ML-implementation we will develop in later sections.
    18   the ML-implementation we will develop in later sections.
       
    19 
    19 
    20 
    20   We first consider the transitive closure of a relation @{text R}. The 
    21   We first consider the transitive closure of a relation @{text R}. The 
    21   ``pencil-and-paper'' specification for the transitive closure is:
    22   ``pencil-and-paper'' specification for the transitive closure is:
    22 
    23 
    23   \begin{center}\small
    24   \begin{center}\small
    24   @{prop[mode=Axiom] "trcl R x x"} \hspace{5mm}
    25   @{prop[mode=Axiom] "trcl R x x"} \hspace{5mm}
    25   @{prop[mode=Rule] "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"}
    26   @{prop[mode=Rule] "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"}
    26   \end{center}
    27   \end{center}
    27 
    28 
    28   The package has to make an appropriate definition for @{term "trcl"}. 
    29   As mentioned before, the package has to make an appropriate definition for
    29   Since an inductively
    30   @{term "trcl"}. Since an inductively defined predicate is the least
    30   defined predicate is the least predicate closed under a collection of
    31   predicate closed under a collection of introduction rules, the predicate
    31   introduction rules, the predicate @{text "trcl R x y"} can be defined so
    32   @{text "trcl R x y"} can be defined so that it holds if and only if @{text
    32   that it holds if and only if @{text "P x y"} holds for every predicate
    33   "P x y"} holds for every predicate @{text P} closed under the rules
    33   @{text P} closed under the rules above. This gives rise to the definition
    34   above. This gives rise to the definition
    34 *}
    35 *}
    35 
    36 
    36 definition "trcl \<equiv> 
    37 definition "trcl \<equiv> 
    37      \<lambda>R x y. \<forall>P. (\<forall>x. P x x) 
    38      \<lambda>R x y. \<forall>P. (\<forall>x. P x x) 
    38                   \<longrightarrow> (\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z) \<longrightarrow> P x y"
    39                   \<longrightarrow> (\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z) \<longrightarrow> P x y"
    39 
    40 
    40 text {*
    41 text {*
    41   We have to use the object implication @{text "\<longrightarrow>"} and object quantification
    42   Note we have to use the object implication @{text "\<longrightarrow>"} and object
    42   @{text "\<forall>"} for stating this definition (there is no other way for
    43   quantification @{text "\<forall>"} for stating this definition (there is no other
    43   definitions in HOL). However, the introduction rules and induction
    44   way for definitions in HOL). However, the introduction rules and induction
    44   principles associated with the transitive closure should use the meta-connectives, 
    45   principles associated with the transitive closure should use the
    45   since they simplify the reasoning for the user.
    46   meta-connectives, since they simplify the reasoning for the user.
    46 
    47 
    47 
    48 
    48   With this definition, the proof of the induction principle for @{term trcl}
    49   With this definition, the proof of the induction principle for @{term trcl}
    49   is almost immediate. It suffices to convert all the meta-level
    50   is almost immediate. It suffices to convert all the meta-level
    50   connectives in the lemma to object-level connectives using the
    51   connectives in the lemma to object-level connectives using the
   174   @{text blast}, @{text auto} and the like in automated proofs. The reason is
   175   @{text blast}, @{text auto} and the like in automated proofs. The reason is
   175   that you do not have precise control over them (the user can, for example,
   176   that you do not have precise control over them (the user can, for example,
   176   declare new intro- or simplification rules that can throw automatic tactics
   177   declare new intro- or simplification rules that can throw automatic tactics
   177   off course) and also it is very hard to debug proofs involving automatic
   178   off course) and also it is very hard to debug proofs involving automatic
   178   tactics whenever something goes wrong. Therefore if possible, automatic 
   179   tactics whenever something goes wrong. Therefore if possible, automatic 
   179   tactics should be avoided or be constrained sufficiently.
   180   tactics in packages should be avoided or be constrained sufficiently.
   180 
   181 
   181   The method of defining inductive predicates by impredicative quantification
   182   The method of defining inductive predicates by impredicative quantification
   182   also generalises to mutually inductive predicates. The next example defines
   183   also generalises to mutually inductive predicates. The next example defines
   183   the predicates @{text even} and @{text odd} given by
   184   the predicates @{text even} and @{text odd} given by
   184 
   185 
   243 	           THEN mp, THEN mp, THEN mp, OF r1, OF r2, OF r3])
   244 	           THEN mp, THEN mp, THEN mp, OF r1, OF r2, OF r3])
   244     done
   245     done
   245 qed
   246 qed
   246 
   247 
   247 text {*
   248 text {*
   248   The interesting lines are 7 to 15. The assumptions fall into two categories:
   249   The interesting lines are 7 to 15. Again, the assumptions fall into two categories:
   249   @{text p1} corresponds to the premise of the introduction rule; @{text "r1"}
   250   @{text p1} corresponds to the premise of the introduction rule; @{text "r1"}
   250   to @{text "r3"} come from the definition of @{text "even"}.
   251   to @{text "r3"} come from the definition of @{text "even"}.
   251   In Line 13, we apply the assumption @{text "r2"} (since we prove the second
   252   In Line 13, we apply the assumption @{text "r2"} (since we prove the second
   252   introduction rule). In Lines 14 and 15 we apply assumption @{text "p1"} (if
   253   introduction rule). In Lines 14 and 15 we apply assumption @{text "p1"} (if
   253   the second introduction rule had more premises we have to do that for all
   254   the second introduction rule had more premises we have to do that for all
   260 
   261 
   261   \begin{center}\small
   262   \begin{center}\small
   262   \mbox{\inferrule{@{term "\<And>y. R y x \<Longrightarrow> accpart R y"}}{@{term "accpart R x"}}}
   263   \mbox{\inferrule{@{term "\<And>y. R y x \<Longrightarrow> accpart R y"}}{@{term "accpart R x"}}}
   263   \end{center}
   264   \end{center}
   264 
   265 
       
   266   The interesting point of this definition is that it contains a quantification
       
   267   that ranges only over the premise and the premise has also a precondition.
   265   The definition of @{text "accpart"} is:
   268   The definition of @{text "accpart"} is:
   266 *}
   269 *}
   267 
   270 
   268 definition "accpart \<equiv> \<lambda>R x. \<forall>P. (\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x) \<longrightarrow> P x"
   271 definition "accpart \<equiv> \<lambda>R x. \<forall>P. (\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x) \<longrightarrow> P x"
   269 
   272 
   292       done
   295       done
   293   qed
   296   qed
   294 qed
   297 qed
   295 
   298 
   296 text {*
   299 text {*
   297   As you can see, there are now two subproofs. The assumptions fall again into
   300   As you can see, there are now two subproofs. The assumptions fall as usual into
   298   two categories (Lines 7 to 9). In Line 11, applying the assumption @{text
   301   two categories (Lines 7 to 9). In Line 11, applying the assumption @{text
   299   "r1"} generates a goal state with the new local assumption @{term "R y x"},
   302   "r1"} generates a goal state with the new local assumption @{term "R y x"},
   300   named @{text "r1_prem"} in the second subproof (Line 14). This local assumption is
   303   named @{text "r1_prem"} in the second subproof (Line 14). This local assumption is
   301   used to solve the goal @{term "P y"} with the help of assumption @{text
   304   used to solve the goal @{term "P y"} with the help of assumption @{text
   302   "p1"}.
   305   "p1"}.