src/HOL/indrule.ML
changeset 1465 5d7a7e439cec
parent 1424 ccb3c5ff6707
child 1653 1a2ffa2fbf7d
equal deleted inserted replaced
1464:a608f83e3421 1465:5d7a7e439cec
     1 (*  Title: 	HOL/indrule.ML
     1 (*  Title:      HOL/indrule.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1994  University of Cambridge
     4     Copyright   1994  University of Cambridge
     5 
     5 
     6 Induction rule module -- for Inductive/Coinductive Definitions
     6 Induction rule module -- for Inductive/Coinductive Definitions
     7 
     7 
     8 Proves a strong induction rule and a mutual induction rule
     8 Proves a strong induction rule and a mutual induction rule
     9 *)
     9 *)
    10 
    10 
    11 signature INDRULE =
    11 signature INDRULE =
    12   sig
    12   sig
    13   val induct        : thm			(*main induction rule*)
    13   val induct        : thm                       (*main induction rule*)
    14   val mutual_induct : thm			(*mutual induction rule*)
    14   val mutual_induct : thm                       (*mutual induction rule*)
    15   end;
    15   end;
    16 
    16 
    17 
    17 
    18 functor Indrule_Fun
    18 functor Indrule_Fun
    19     (structure Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end and
    19     (structure Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end and
    20 	 Intr_elim: sig include INTR_ELIM INTR_ELIM_AUX end) : INDRULE  =
    20          Intr_elim: sig include INTR_ELIM INTR_ELIM_AUX end) : INDRULE  =
    21 let
    21 let
    22 
    22 
    23 val sign = sign_of Inductive.thy;
    23 val sign = sign_of Inductive.thy;
    24 
    24 
    25 val (Const(_,recT),rec_params) = strip_comb (hd Inductive.rec_tms);
    25 val (Const(_,recT),rec_params) = strip_comb (hd Inductive.rec_tms);
    30 
    30 
    31 val _ = writeln "  Proving the induction rule...";
    31 val _ = writeln "  Proving the induction rule...";
    32 
    32 
    33 (*** Prove the main induction rule ***)
    33 (*** Prove the main induction rule ***)
    34 
    34 
    35 val pred_name = "P";		(*name for predicate variables*)
    35 val pred_name = "P";            (*name for predicate variables*)
    36 
    36 
    37 val big_rec_def::part_rec_defs = Intr_elim.defs;
    37 val big_rec_def::part_rec_defs = Intr_elim.defs;
    38 
    38 
    39 (*Used to express induction rules: adds induction hypotheses.
    39 (*Used to express induction rules: adds induction hypotheses.
    40    ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
    40    ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
    41    prem is a premise of an intr rule*)
    41    prem is a premise of an intr rule*)
    42 fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
    42 fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
    43 		 (Const("op :",_)$t$X), iprems) =
    43                  (Const("op :",_)$t$X), iprems) =
    44      (case gen_assoc (op aconv) (ind_alist, X) of
    44      (case gen_assoc (op aconv) (ind_alist, X) of
    45 	  Some pred => prem :: Ind_Syntax.mk_Trueprop (pred $ t) :: iprems
    45           Some pred => prem :: Ind_Syntax.mk_Trueprop (pred $ t) :: iprems
    46 	| None => (*possibly membership in M(rec_tm), for M monotone*)
    46         | None => (*possibly membership in M(rec_tm), for M monotone*)
    47 	    let fun mk_sb (rec_tm,pred) = 
    47             let fun mk_sb (rec_tm,pred) = 
    48 		 (case binder_types (fastype_of pred) of
    48                  (case binder_types (fastype_of pred) of
    49 		      [T] => (rec_tm, 
    49                       [T] => (rec_tm, 
    50 			      Ind_Syntax.Int_const T $ rec_tm $ 
    50                               Ind_Syntax.Int_const T $ rec_tm $ 
    51 			        (Ind_Syntax.Collect_const T $ pred))
    51                                 (Ind_Syntax.Collect_const T $ pred))
    52 		    | _ => error 
    52                     | _ => error 
    53 		      "Bug: add_induct_prem called with non-unary predicate")
    53                       "Bug: add_induct_prem called with non-unary predicate")
    54 	    in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
    54             in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
    55   | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
    55   | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
    56 
    56 
    57 (*Make a premise of the induction rule.*)
    57 (*Make a premise of the induction rule.*)
    58 fun induct_prem ind_alist intr =
    58 fun induct_prem ind_alist intr =
    59   let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
    59   let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
    60       val iprems = foldr (add_induct_prem ind_alist)
    60       val iprems = foldr (add_induct_prem ind_alist)
    61 			 (Logic.strip_imp_prems intr,[])
    61                          (Logic.strip_imp_prems intr,[])
    62       val (t,X) = Ind_Syntax.rule_concl intr
    62       val (t,X) = Ind_Syntax.rule_concl intr
    63       val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
    63       val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
    64       val concl = Ind_Syntax.mk_Trueprop (pred $ t)
    64       val concl = Ind_Syntax.mk_Trueprop (pred $ t)
    65   in list_all_free (quantfrees, Logic.list_implies (iprems,concl)) end
    65   in list_all_free (quantfrees, Logic.list_implies (iprems,concl)) end
    66   handle Bind => error"Recursion term not found in conclusion";
    66   handle Bind => error"Recursion term not found in conclusion";
    67 
    67 
    68 (*Avoids backtracking by delivering the correct premise to each goal*)
    68 (*Avoids backtracking by delivering the correct premise to each goal*)
    69 fun ind_tac [] 0 = all_tac
    69 fun ind_tac [] 0 = all_tac
    70   | ind_tac(prem::prems) i = 
    70   | ind_tac(prem::prems) i = 
    71 	DEPTH_SOLVE_1 (ares_tac [Part_eqI, prem, refl] i) THEN
    71         DEPTH_SOLVE_1 (ares_tac [Part_eqI, prem, refl] i) THEN
    72 	ind_tac prems (i-1);
    72         ind_tac prems (i-1);
    73 
    73 
    74 val pred = Free(pred_name, elem_type --> Ind_Syntax.boolT);
    74 val pred = Free(pred_name, elem_type --> Ind_Syntax.boolT);
    75 
    75 
    76 val ind_prems = map (induct_prem (map (rpair pred) Inductive.rec_tms)) 
    76 val ind_prems = map (induct_prem (map (rpair pred) Inductive.rec_tms)) 
    77                     Inductive.intr_tms;
    77                     Inductive.intr_tms;
    83 
    83 
    84 val quant_induct = 
    84 val quant_induct = 
    85     prove_goalw_cterm part_rec_defs 
    85     prove_goalw_cterm part_rec_defs 
    86       (cterm_of sign 
    86       (cterm_of sign 
    87        (Logic.list_implies (ind_prems, 
    87        (Logic.list_implies (ind_prems, 
    88 			    Ind_Syntax.mk_Trueprop (Ind_Syntax.mk_all_imp 
    88                             Ind_Syntax.mk_Trueprop (Ind_Syntax.mk_all_imp 
    89 						    (big_rec_tm,pred)))))
    89                                                     (big_rec_tm,pred)))))
    90       (fn prems =>
    90       (fn prems =>
    91        [rtac (impI RS allI) 1,
    91        [rtac (impI RS allI) 1,
    92 	DETERM (etac Intr_elim.raw_induct 1),
    92         DETERM (etac Intr_elim.raw_induct 1),
    93 	asm_full_simp_tac (!simpset addsimps [Part_Collect]) 1,
    93         asm_full_simp_tac (!simpset addsimps [Part_Collect]) 1,
    94 	REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE] 
    94         REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE] 
    95 			   ORELSE' hyp_subst_tac)),
    95                            ORELSE' hyp_subst_tac)),
    96 	ind_tac (rev prems) (length prems)])
    96         ind_tac (rev prems) (length prems)])
    97     handle e => print_sign_exn sign e;
    97     handle e => print_sign_exn sign e;
    98 
    98 
    99 (*** Prove the simultaneous induction rule ***)
    99 (*** Prove the simultaneous induction rule ***)
   100 
   100 
   101 (*Make distinct predicates for each inductive set.
   101 (*Make distinct predicates for each inductive set.
   107   let val rec_name = (#1 o dest_Const o head_of) rec_tm
   107   let val rec_name = (#1 o dest_Const o head_of) rec_tm
   108       val T = Ind_Syntax.factors elem_type ---> Ind_Syntax.boolT
   108       val T = Ind_Syntax.factors elem_type ---> Ind_Syntax.boolT
   109       val pfree = Free(pred_name ^ "_" ^ rec_name, T)
   109       val pfree = Free(pred_name ^ "_" ^ rec_name, T)
   110       val frees = mk_frees "za" (binder_types T)
   110       val frees = mk_frees "za" (binder_types T)
   111       val qconcl = 
   111       val qconcl = 
   112 	foldr Ind_Syntax.mk_all 
   112         foldr Ind_Syntax.mk_all 
   113 	  (frees, 
   113           (frees, 
   114 	   Ind_Syntax.imp $ (Ind_Syntax.mk_mem 
   114            Ind_Syntax.imp $ (Ind_Syntax.mk_mem 
   115 			     (foldr1 Ind_Syntax.mk_Pair frees, rec_tm))
   115                              (foldr1 Ind_Syntax.mk_Pair frees, rec_tm))
   116 	        $ (list_comb (pfree,frees)))
   116                 $ (list_comb (pfree,frees)))
   117   in  (Ind_Syntax.ap_split Ind_Syntax.boolT pfree (binder_types T), 
   117   in  (Ind_Syntax.ap_split Ind_Syntax.boolT pfree (binder_types T), 
   118       qconcl)  
   118       qconcl)  
   119   end;
   119   end;
   120 
   120 
   121 val (preds,qconcls) = split_list (map mk_predpair Inductive.rec_tms);
   121 val (preds,qconcls) = split_list (map mk_predpair Inductive.rec_tms);
   127 (*To instantiate the main induction rule*)
   127 (*To instantiate the main induction rule*)
   128 val induct_concl = 
   128 val induct_concl = 
   129     Ind_Syntax.mk_Trueprop
   129     Ind_Syntax.mk_Trueprop
   130       (Ind_Syntax.mk_all_imp
   130       (Ind_Syntax.mk_all_imp
   131        (big_rec_tm,
   131        (big_rec_tm,
   132 	Abs("z", elem_type, 
   132         Abs("z", elem_type, 
   133 	    fold_bal (app Ind_Syntax.conj) 
   133             fold_bal (app Ind_Syntax.conj) 
   134 	    (map mk_rec_imp (Inductive.rec_tms~~preds)))))
   134             (map mk_rec_imp (Inductive.rec_tms~~preds)))))
   135 and mutual_induct_concl = 
   135 and mutual_induct_concl = 
   136     Ind_Syntax.mk_Trueprop (fold_bal (app Ind_Syntax.conj) qconcls);
   136     Ind_Syntax.mk_Trueprop (fold_bal (app Ind_Syntax.conj) qconcls);
   137 
   137 
   138 val lemma = (*makes the link between the two induction rules*)
   138 val lemma = (*makes the link between the two induction rules*)
   139     prove_goalw_cterm part_rec_defs 
   139     prove_goalw_cterm part_rec_defs 
   140 	  (cterm_of sign (Logic.mk_implies (induct_concl,
   140           (cterm_of sign (Logic.mk_implies (induct_concl,
   141 					    mutual_induct_concl)))
   141                                             mutual_induct_concl)))
   142 	  (fn prems =>
   142           (fn prems =>
   143 	   [cut_facts_tac prems 1,
   143            [cut_facts_tac prems 1,
   144 	    REPEAT (eresolve_tac [asm_rl, conjE, PartE, mp] 1
   144             REPEAT (eresolve_tac [asm_rl, conjE, PartE, mp] 1
   145 	     ORELSE resolve_tac [allI, impI, conjI, Part_eqI, refl] 1
   145              ORELSE resolve_tac [allI, impI, conjI, Part_eqI, refl] 1
   146 	     ORELSE dresolve_tac [spec, mp, splitD] 1)])
   146              ORELSE dresolve_tac [spec, mp, splitD] 1)])
   147     handle e => print_sign_exn sign e;
   147     handle e => print_sign_exn sign e;
   148 
   148 
   149 (*Mutual induction follows by freeness of Inl/Inr.*)
   149 (*Mutual induction follows by freeness of Inl/Inr.*)
   150 
   150 
   151 (*Simplification largely reduces the mutual induction rule to the 
   151 (*Simplification largely reduces the mutual induction rule to the 
   162 (*Avoids backtracking by delivering the correct premise to each goal*)
   162 (*Avoids backtracking by delivering the correct premise to each goal*)
   163 fun mutual_ind_tac [] 0 = all_tac
   163 fun mutual_ind_tac [] 0 = all_tac
   164   | mutual_ind_tac(prem::prems) i = 
   164   | mutual_ind_tac(prem::prems) i = 
   165       DETERM
   165       DETERM
   166        (SELECT_GOAL 
   166        (SELECT_GOAL 
   167 	  (
   167           (
   168 	   (*Simplify the assumptions and goal by unfolding Part and
   168            (*Simplify the assumptions and goal by unfolding Part and
   169 	     using freeness of the Sum constructors; proves all but one
   169              using freeness of the Sum constructors; proves all but one
   170              conjunct by contradiction*)
   170              conjunct by contradiction*)
   171 	   rewrite_goals_tac all_defs  THEN
   171            rewrite_goals_tac all_defs  THEN
   172 	   simp_tac (mut_ss addsimps [Part_def]) 1  THEN
   172            simp_tac (mut_ss addsimps [Part_def]) 1  THEN
   173 	   IF_UNSOLVED (*simp_tac may have finished it off!*)
   173            IF_UNSOLVED (*simp_tac may have finished it off!*)
   174 	     ((*simplify assumptions, but don't accept new rewrite rules!*)
   174              ((*simplify assumptions, but don't accept new rewrite rules!*)
   175 	      asm_full_simp_tac (mut_ss setmksimps K[]) 1  THEN
   175               asm_full_simp_tac (mut_ss setmksimps K[]) 1  THEN
   176 	      (*unpackage and use "prem" in the corresponding place*)
   176               (*unpackage and use "prem" in the corresponding place*)
   177 	      REPEAT (rtac impI 1)  THEN
   177               REPEAT (rtac impI 1)  THEN
   178 	      rtac (rewrite_rule all_defs prem) 1  THEN
   178               rtac (rewrite_rule all_defs prem) 1  THEN
   179 	      (*prem must not be REPEATed below: could loop!*)
   179               (*prem must not be REPEATed below: could loop!*)
   180 	      DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
   180               DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
   181 				      eresolve_tac (conjE::mp::cmonos))))
   181                                       eresolve_tac (conjE::mp::cmonos))))
   182 	  ) i)
   182           ) i)
   183        THEN mutual_ind_tac prems (i-1);
   183        THEN mutual_ind_tac prems (i-1);
   184 
   184 
   185 val _ = writeln "  Proving the mutual induction rule...";
   185 val _ = writeln "  Proving the mutual induction rule...";
   186 
   186 
   187 val mutual_induct_split = 
   187 val mutual_induct_split = 
   188     prove_goalw_cterm []
   188     prove_goalw_cterm []
   189 	  (cterm_of sign
   189           (cterm_of sign
   190 	   (Logic.list_implies (map (induct_prem (Inductive.rec_tms ~~ preds)) 
   190            (Logic.list_implies (map (induct_prem (Inductive.rec_tms ~~ preds)) 
   191 			      Inductive.intr_tms,
   191                               Inductive.intr_tms,
   192 			  mutual_induct_concl)))
   192                           mutual_induct_concl)))
   193 	  (fn prems =>
   193           (fn prems =>
   194 	   [rtac (quant_induct RS lemma) 1,
   194            [rtac (quant_induct RS lemma) 1,
   195 	    mutual_ind_tac (rev prems) (length prems)])
   195             mutual_ind_tac (rev prems) (length prems)])
   196     handle e => print_sign_exn sign e;
   196     handle e => print_sign_exn sign e;
   197 
   197 
   198 (*Attempts to remove all occurrences of split*)
   198 (*Attempts to remove all occurrences of split*)
   199 val split_tac =
   199 val split_tac =
   200     REPEAT (SOMEGOAL (FIRST' [rtac splitI, 
   200     REPEAT (SOMEGOAL (FIRST' [rtac splitI, 
   201 			      dtac splitD,
   201                               dtac splitD,
   202 			      etac splitE,
   202                               etac splitE,
   203 			      bound_hyp_subst_tac]))
   203                               bound_hyp_subst_tac]))
   204     THEN prune_params_tac;
   204     THEN prune_params_tac;
   205 
   205 
   206 in
   206 in
   207   struct
   207   struct
   208   (*strip quantifier*)
   208   (*strip quantifier*)
   209   val induct = standard (quant_induct RS spec RSN (2,rev_mp));
   209   val induct = standard (quant_induct RS spec RSN (2,rev_mp));
   210 
   210 
   211   val mutual_induct = 
   211   val mutual_induct = 
   212       if length Intr_elim.rec_names > 1 orelse
   212       if length Intr_elim.rec_names > 1 orelse
   213 	 length (Ind_Syntax.factors elem_type) > 1
   213          length (Ind_Syntax.factors elem_type) > 1
   214       then rule_by_tactic split_tac mutual_induct_split
   214       then rule_by_tactic split_tac mutual_induct_split
   215       else TrueI;
   215       else TrueI;
   216   end
   216   end
   217 end;
   217 end;