ProgTutorial/Package/Ind_Code.thy
changeset 256 1fb8d62c88a0
parent 250 ab9e09076462
child 294 ee9d53fbb56b
equal deleted inserted replaced
255:ef1da1abee46 256:1fb8d62c88a0
    19 
    19 
    20   @{text [display] "pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
    20   @{text [display] "pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
    21 
    21 
    22   and then ``register'' the definition inside a local theory. 
    22   and then ``register'' the definition inside a local theory. 
    23   To do the latter, we use the following wrapper for the function
    23   To do the latter, we use the following wrapper for the function
    24   @{ML LocalTheory.define}. The wrapper takes a predicate name, a syntax
    24   @{ML [index] define in LocalTheory}. The wrapper takes a predicate name, a syntax
    25   annotation and a term representing the right-hand side of the definition.
    25   annotation and a term representing the right-hand side of the definition.
    26 *}
    26 *}
    27 
    27 
    28 ML %linenosgray{*fun make_defn ((predname, mx), trm) lthy =
    28 ML %linenosgray{*fun make_defn ((predname, mx), trm) lthy =
    29 let 
    29 let 
    33   (thm, lthy') 
    33   (thm, lthy') 
    34 end*}
    34 end*}
    35 
    35 
    36 text {*
    36 text {*
    37   It returns the definition (as a theorem) and the local theory in which the
    37   It returns the definition (as a theorem) and the local theory in which the
    38   definition has been made. In Line 4, @{ML internalK in Thm} is a flag
    38   definition has been made. In Line 4, @{ML [index] internalK in Thm} is a flag
    39   attached to the theorem (others possibile flags are @{ML definitionK in Thm}
    39   attached to the theorem (others possibile flags are @{ML [index] definitionK in Thm}
    40   and @{ML axiomK in Thm}). These flags just classify theorems and have no
    40   and @{ML [index] axiomK in Thm}). These flags just classify theorems and have no
    41   significant meaning, except for tools that, for example, find theorems in
    41   significant meaning, except for tools that, for example, find theorems in
    42   the theorem database.\footnote{FIXME: put in the section about theorems.} We
    42   the theorem database.\footnote{FIXME: put in the section about theorems.} We
    43   also use @{ML empty_binding in Attrib} in Line 3, since the definitions for
    43   also use @{ML [index] empty_binding in Attrib} in Line 3, since the definitions for
    44   our inductive predicates are not meant to be seen by the user and therefore
    44   our inductive predicates are not meant to be seen by the user and therefore
    45   do not need to have any theorem attributes. A testcase for this function is
    45   do not need to have any theorem attributes. A testcase for this function is
    46 *}
    46 *}
    47 
    47 
    48 local_setup %gray {* fn lthy =>
    48 local_setup %gray {* fn lthy =>
   168   The user will state the introduction rules using meta-implications and
   168   The user will state the introduction rules using meta-implications and
   169   meta-quanti\-fications. In Line 4, we transform these introduction rules
   169   meta-quanti\-fications. In Line 4, we transform these introduction rules
   170   into the object logic (since definitions cannot be stated with
   170   into the object logic (since definitions cannot be stated with
   171   meta-connectives). To do this transformation we have to obtain the theory
   171   meta-connectives). To do this transformation we have to obtain the theory
   172   behind the local theory (Line 3); with this theory we can use the function
   172   behind the local theory (Line 3); with this theory we can use the function
   173   @{ML ObjectLogic.atomize_term} to make the transformation (Line 4). The call
   173   @{ML [index] atomize_term in ObjectLogic} to make the transformation (Line 4). The call
   174   to @{ML defn_aux} in Line 5 produces all right-hand sides of the
   174   to @{ML defn_aux} in Line 5 produces all right-hand sides of the
   175   definitions. The actual definitions are then made in Line 7.  The result of
   175   definitions. The actual definitions are then made in Line 7.  The result of
   176   the function is a list of theorems and a local theory (the theorems are
   176   the function is a list of theorems and a local theory (the theorems are
   177   registered with the local theory). A testcase for this function is
   177   registered with the local theory). A testcase for this function is
   178 *}
   178 *}
   435   the new local theory @{text "lthy'"}. From the local theory we extract
   435   the new local theory @{text "lthy'"}. From the local theory we extract
   436   the ambient theory in Line 6. We need this theory in order to certify 
   436   the ambient theory in Line 6. We need this theory in order to certify 
   437   the new predicates. In Line 8, we construct the types of these new predicates
   437   the new predicates. In Line 8, we construct the types of these new predicates
   438   using the given argument types. Next we turn them into terms and subsequently
   438   using the given argument types. Next we turn them into terms and subsequently
   439   certify them (Line 9 and 10). We can now produce the substituted introduction rules 
   439   certify them (Line 9 and 10). We can now produce the substituted introduction rules 
   440   (Line 11) using the function @{ML subst_free}. Line 14 and 15 just iterate 
   440   (Line 11) using the function @{ML [index] subst_free}. Line 14 and 15 just iterate 
   441   the proofs for all predicates.
   441   the proofs for all predicates.
   442   From this we obtain a list of theorems. Finally we need to export the 
   442   From this we obtain a list of theorems. Finally we need to export the 
   443   fixed variables @{text "Ps"} to obtain the schematic variables @{text "?Ps"} 
   443   fixed variables @{text "Ps"} to obtain the schematic variables @{text "?Ps"} 
   444   (Line 16).
   444   (Line 16).
   445 
   445 
   577   first two) and assumptions from the definition of the predicate (assumption
   577   first two) and assumptions from the definition of the predicate (assumption
   578   three to six). We need to treat these parameters and assumptions
   578   three to six). We need to treat these parameters and assumptions
   579   differently. In the code below we will therefore separate them into @{text
   579   differently. In the code below we will therefore separate them into @{text
   580   "params1"} and @{text params2}, respectively @{text "prems1"} and @{text
   580   "params1"} and @{text params2}, respectively @{text "prems1"} and @{text
   581   "prems2"}. To do this separation, it is best to open a subproof with the
   581   "prems2"}. To do this separation, it is best to open a subproof with the
   582   tactic @{ML SUBPROOF}, since this tactic provides us with the parameters (as
   582   tactic @{ML [index] SUBPROOF}, since this tactic provides us with the parameters (as
   583   list of @{ML_type cterm}s) and the assumptions (as list of @{ML_type thm}s). 
   583   list of @{ML_type cterm}s) and the assumptions (as list of @{ML_type thm}s). 
   584   The problem we have to overcome with @{ML SUBPROOF}
   584   The problem we have to overcome with @{ML SUBPROOF}
   585   is, however, that this tactic always expects us to completely discharge the
   585   is, however, that this tactic always expects us to completely discharge the
   586   goal (see Section~\ref{sec:simpletacs}). This is inconvenient for our
   586   goal (see Section~\ref{sec:simpletacs}). This is inconvenient for our
   587   gradual explanation of the proof here. To circumvent this inconvenience we
   587   gradual explanation of the proof here. To circumvent this inconvenience we
   625   First we calculate the values for @{text "params1/2"} and @{text "prems1/2"}
   625   First we calculate the values for @{text "params1/2"} and @{text "prems1/2"}
   626   from @{text "params"} and @{text "prems"}, respectively. To better see what is
   626   from @{text "params"} and @{text "prems"}, respectively. To better see what is
   627   going in our example, we will print out these values using the printing
   627   going in our example, we will print out these values using the printing
   628   function in Figure~\ref{fig:chopprint}. Since the tactic @{ML SUBPROOF} will
   628   function in Figure~\ref{fig:chopprint}. Since the tactic @{ML SUBPROOF} will
   629   supply us the @{text "params"} and @{text "prems"} as lists, we can 
   629   supply us the @{text "params"} and @{text "prems"} as lists, we can 
   630   separate them using the function @{ML chop}. 
   630   separate them using the function @{ML [index] chop}. 
   631 *}
   631 *}
   632 
   632 
   633 ML{*fun chop_test_tac preds rules =
   633 ML{*fun chop_test_tac preds rules =
   634   SUBPROOF_test (fn {params, prems, context, ...} =>
   634   SUBPROOF_test (fn {params, prems, context, ...} =>
   635   let
   635   let
   689 
   689 
   690   @{term [display] "\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam a t)"}
   690   @{term [display] "\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam a t)"}
   691 
   691 
   692   To use this premise with @{ML rtac}, we need to instantiate its 
   692   To use this premise with @{ML rtac}, we need to instantiate its 
   693   quantifiers (with @{text params1}) and transform it into rule 
   693   quantifiers (with @{text params1}) and transform it into rule 
   694   format (using @{ML "ObjectLogic.rulify"}. So we can modify the 
   694   format (using @{ML [index] rulify in ObjectLogic}. So we can modify the 
   695   subproof as follows:
   695   subproof as follows:
   696 *}
   696 *}
   697 
   697 
   698 ML %linenosgray{*fun apply_prem_tac i preds rules =
   698 ML %linenosgray{*fun apply_prem_tac i preds rules =
   699   SUBPROOF_test (fn {params, prems, context, ...} =>
   699   SUBPROOF_test (fn {params, prems, context, ...} =>
   852 
   852 
   853 text {*
   853 text {*
   854   In Line 4 we use the @{text prems} from the @{ML SUBPROOF} and resolve 
   854   In Line 4 we use the @{text prems} from the @{ML SUBPROOF} and resolve 
   855   them with @{text prem}. In the simple cases, that is where the @{text prem} 
   855   them with @{text prem}. In the simple cases, that is where the @{text prem} 
   856   comes from a non-recursive premise of the rule, @{text prems} will be 
   856   comes from a non-recursive premise of the rule, @{text prems} will be 
   857   just the empty list and the function @{ML MRS} does nothing. Similarly, in the 
   857   just the empty list and the function @{ML [index] MRS} does nothing. Similarly, in the 
   858   cases where the recursive premises of the rule do not have preconditions. 
   858   cases where the recursive premises of the rule do not have preconditions. 
   859   In case there are preconditions, then Line 4 discharges them. After
   859   In case there are preconditions, then Line 4 discharges them. After
   860   that we can proceed as before, i.e., check whether the outermost
   860   that we can proceed as before, i.e., check whether the outermost
   861   connective is @{text "\<forall>"}.
   861   connective is @{text "\<forall>"}.
   862   
   862   
   930 in
   930 in
   931   map_index intros_aux rules
   931   map_index intros_aux rules
   932 end*}
   932 end*}
   933 
   933 
   934 text {*
   934 text {*
   935   The iteration is done with the function @{ML map_index} since we
   935   The iteration is done with the function @{ML [index] map_index} since we
   936   need the introduction rule together with its number (counted from
   936   need the introduction rule together with its number (counted from
   937   @{text 0}). This completes the code for the functions deriving the
   937   @{text 0}). This completes the code for the functions deriving the
   938   reasoning infrastructure. It remains to implement some administrative
   938   reasoning infrastructure. It remains to implement some administrative
   939   code that strings everything together.
   939   code that strings everything together.
   940 *}
   940 *}
   943 
   943 
   944 text {* 
   944 text {* 
   945   We have produced various theorems (definitions, induction principles and
   945   We have produced various theorems (definitions, induction principles and
   946   introduction rules), but apart from the definitions, we have not yet 
   946   introduction rules), but apart from the definitions, we have not yet 
   947   registered them with the theorem database. This is what the functions 
   947   registered them with the theorem database. This is what the functions 
   948   @{ML LocalTheory.note} does. 
   948   @{ML [index] note in LocalTheory} does. 
   949 
   949 
   950 
   950 
   951   For convenience, we use the following 
   951   For convenience, we use the following 
   952   three wrappers this function:
   952   three wrappers this function:
   953 *}
   953 *}
  1025   @{text "mut_name.inducts"}, respectively (see previous paragraph).
  1025   @{text "mut_name.inducts"}, respectively (see previous paragraph).
  1026   
  1026   
  1027   Line 20 add further every introduction rule under its own name
  1027   Line 20 add further every introduction rule under its own name
  1028   (given by the user).\footnote{FIXME: what happens if the user did not give
  1028   (given by the user).\footnote{FIXME: what happens if the user did not give
  1029   any name.} Line 21 registers the induction principles. For this we have
  1029   any name.} Line 21 registers the induction principles. For this we have
  1030   to use some specific attributes. The first @{ML "case_names" in RuleCases} 
  1030   to use some specific attributes. The first @{ML [index] case_names in RuleCases} 
  1031   corresponds to the case names that are used by Isar to reference the proof
  1031   corresponds to the case names that are used by Isar to reference the proof
  1032   obligations in the induction. The second @{ML "consumes 1" in RuleCases}
  1032   obligations in the induction. The second @{ML "consumes 1" in RuleCases}
  1033   indicates that the first premise of the induction principle (namely
  1033   indicates that the first premise of the induction principle (namely
  1034   the predicate over which the induction proceeds) is eliminated. 
  1034   the predicate over which the induction proceeds) is eliminated. 
  1035 
  1035