indrule.ML
changeset 128 89669c58e506
child 136 0a43cf458998
equal deleted inserted replaced
127:d9527f97246e 128:89669c58e506
       
     1 (*  Title: 	HOL/indrule.ML
       
     2     ID:         $Id$
       
     3     Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
       
     4     Copyright   1994  University of Cambridge
       
     5 
       
     6 Induction rule module -- for Inductive/Coinductive Definitions
       
     7 
       
     8 Proves a strong induction rule and a mutual induction rule
       
     9 *)
       
    10 
       
    11 signature INDRULE =
       
    12   sig
       
    13   val induct        : thm			(*main induction rule*)
       
    14   val mutual_induct : thm			(*mutual induction rule*)
       
    15   end;
       
    16 
       
    17 
       
    18 functor Indrule_Fun
       
    19     (structure Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end and
       
    20 	 Intr_elim: INTR_ELIM) : INDRULE  =
       
    21 struct
       
    22 open Logic Ind_Syntax Inductive Intr_elim;
       
    23 
       
    24 val sign = sign_of thy;
       
    25 
       
    26 val (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
       
    27 
       
    28 val elem_type = dest_set (body_type recT);
       
    29 val domTs = summands(elem_type);
       
    30 val big_rec_name = space_implode "_" rec_names;
       
    31 val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
       
    32 
       
    33 val _ = writeln "  Proving the induction rules...";
       
    34 
       
    35 (*** Prove the main induction rule ***)
       
    36 
       
    37 val pred_name = "P";		(*name for predicate variables*)
       
    38 
       
    39 val big_rec_def::part_rec_defs = Intr_elim.defs;
       
    40 
       
    41 (*Used to express induction rules: adds induction hypotheses.
       
    42    ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
       
    43    prem is a premise of an intr rule*)
       
    44 fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
       
    45 		 (Const("op :",_)$t$X), iprems) =
       
    46      (case gen_assoc (op aconv) (ind_alist, X) of
       
    47 	  Some pred => prem :: mk_tprop (pred $ t) :: iprems
       
    48 	| None => (*possibly membership in M(rec_tm), for M monotone*)
       
    49 	    let fun mk_sb (rec_tm,pred) = 
       
    50 		 (case binder_types (fastype_of pred) of
       
    51 		      [T] => (rec_tm, 
       
    52 			      Int_const T $ rec_tm $ (Collect_const T $ pred))
       
    53 		    | _ => error 
       
    54 		      "Bug: add_induct_prem called with non-unary predicate")
       
    55 	    in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
       
    56   | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
       
    57 
       
    58 (*Make a premise of the induction rule.*)
       
    59 fun induct_prem ind_alist intr =
       
    60   let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
       
    61       val iprems = foldr (add_induct_prem ind_alist)
       
    62 			 (strip_imp_prems intr,[])
       
    63       val (t,X) = rule_concl intr
       
    64       val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
       
    65       val concl = mk_tprop (pred $ t)
       
    66   in list_all_free (quantfrees, list_implies (iprems,concl)) end
       
    67   handle Bind => error"Recursion term not found in conclusion";
       
    68 
       
    69 (*Avoids backtracking by delivering the correct premise to each goal*)
       
    70 fun ind_tac [] 0 = all_tac
       
    71   | ind_tac(prem::prems) i = REPEAT (ares_tac [Part_eqI,prem] i) THEN
       
    72 			     ind_tac prems (i-1);
       
    73 
       
    74 val pred = Free(pred_name, elem_type --> boolT);
       
    75 
       
    76 val ind_prems = map (induct_prem (map (rpair pred) rec_tms)) intr_tms;
       
    77 
       
    78 val quant_induct = 
       
    79     prove_goalw_cterm part_rec_defs 
       
    80       (cterm_of sign (list_implies (ind_prems, 
       
    81 				    mk_tprop (mk_all_imp(big_rec_tm,pred)))))
       
    82       (fn prems =>
       
    83        [rtac (impI RS allI) 1,
       
    84 	etac raw_induct 1,
       
    85 	REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE, 
       
    86 					 ssubst])),
       
    87 	REPEAT (FIRSTGOAL (eresolve_tac [PartE, CollectE])),
       
    88 	ind_tac (rev prems) (length prems)])
       
    89     handle e => print_sign_exn sign e;
       
    90 
       
    91 (*** Prove the simultaneous induction rule ***)
       
    92 
       
    93 (*Make distinct predicates for each inductive set;
       
    94   Cartesian products in domT should nest ONLY to the left! *)
       
    95 
       
    96 (*Given a recursive set and its domain, return the "split" predicate
       
    97   and a conclusion for the simultaneous induction rule*)
       
    98 fun mk_predpair (rec_tm,domT) = 
       
    99   let val rec_name = (#1 o dest_Const o head_of) rec_tm
       
   100       val T = factors domT ---> boolT
       
   101       val pfree = Free(pred_name ^ "_" ^ rec_name, T)
       
   102       val frees = mk_frees "za" (binder_types T)
       
   103       val qconcl = 
       
   104 	foldr mk_all (frees, 
       
   105 		      imp $ (mk_mem (foldr1 mk_Pair frees, rec_tm))
       
   106 			  $ (list_comb (pfree,frees)))
       
   107   in  (ap_split boolT pfree (binder_types T), 
       
   108       qconcl)  
       
   109   end;
       
   110 
       
   111 val (preds,qconcls) = split_list (map mk_predpair (rec_tms~~domTs));
       
   112 
       
   113 (*Used to form simultaneous induction lemma*)
       
   114 fun mk_rec_imp (rec_tm,pred) = 
       
   115     imp $ (mk_mem (Bound 0, rec_tm)) $  (pred $ Bound 0);
       
   116 
       
   117 (*To instantiate the main induction rule*)
       
   118 val induct_concl = 
       
   119  mk_tprop(mk_all_imp(big_rec_tm,
       
   120 		     Abs("z", elem_type, 
       
   121 			 fold_bal (app conj) 
       
   122 			          (map mk_rec_imp (rec_tms~~preds)))))
       
   123 and mutual_induct_concl = mk_tprop(fold_bal (app conj) qconcls);
       
   124 
       
   125 val lemma = (*makes the link between the two induction rules*)
       
   126     prove_goalw_cterm part_rec_defs 
       
   127 	  (cterm_of sign (mk_implies (induct_concl,mutual_induct_concl)))
       
   128 	  (fn prems =>
       
   129 	   [cut_facts_tac prems 1,
       
   130 	    REPEAT (eresolve_tac [asm_rl, conjE, PartE, mp] 1
       
   131 	     ORELSE resolve_tac [allI, impI, conjI, Part_eqI] 1
       
   132 	     ORELSE dresolve_tac [spec, mp, splitD] 1)])
       
   133     handle e => print_sign_exn sign e;
       
   134 
       
   135 (*Mutual induction follows by freeness of Inl/Inr.*)
       
   136 
       
   137 (*Removes Collects caused by M-operators in the intro rules*)
       
   138 val cmonos = [subset_refl RS Int_Collect_mono] RL monos RLN (2,[rev_subsetD]);
       
   139 
       
   140 (*Avoids backtracking by delivering the correct premise to each goal*)
       
   141 fun mutual_ind_tac [] 0 = all_tac
       
   142   | mutual_ind_tac(prem::prems) i = 
       
   143       SELECT_GOAL 
       
   144 	((*unpackage and use "prem" in the corresponding place*)
       
   145 	 REPEAT (FIRSTGOAL
       
   146 		    (eresolve_tac ([conjE,mp]@cmonos) ORELSE'
       
   147 		     ares_tac [prem,impI,conjI]))
       
   148 	 (*prove remaining goals by contradiction*)
       
   149 	 THEN rewrite_goals_tac (con_defs@part_rec_defs)
       
   150 	 THEN REPEAT (eresolve_tac (PartE :: sumprod_free_SEs) 1))
       
   151 	i  THEN mutual_ind_tac prems (i-1);
       
   152 
       
   153 val mutual_induct_split = 
       
   154     prove_goalw_cterm []
       
   155 	  (cterm_of sign
       
   156 	   (list_implies (map (induct_prem (rec_tms~~preds)) intr_tms,
       
   157 			  mutual_induct_concl)))
       
   158 	  (fn prems =>
       
   159 	   [rtac (quant_induct RS lemma) 1,
       
   160 	    mutual_ind_tac (rev prems) (length prems)])
       
   161     handle e => print_sign_exn sign e;
       
   162 
       
   163 (*Attempts to remove all occurrences of split*)
       
   164 val split_tac =
       
   165     REPEAT (SOMEGOAL (FIRST' [rtac splitI, 
       
   166 			      dtac splitD,
       
   167 			      etac splitE,
       
   168 			      bound_hyp_subst_tac]))
       
   169     THEN prune_params_tac;
       
   170 
       
   171 (*strip quantifier*)
       
   172 val induct = standard (quant_induct RS spec RSN (2,rev_mp));
       
   173 
       
   174 val mutual_induct = rule_by_tactic split_tac mutual_induct_split;
       
   175 
       
   176 end;