src/HOL/indrule.ML
author lcp
Tue Jul 25 17:00:53 1995 +0200 (1995-07-25)
changeset 1190 9d1bdce3a38e
parent 923 ff1574a81019
child 1264 3eb91524b938
permissions -rw-r--r--
Old version of mutual induction never worked. Now ensures that
all predicates get the SAME type. Updated mutual_ind_tac from ZF version
clasohm@923
     1
(*  Title: 	HOL/indrule.ML
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@923
     4
    Copyright   1994  University of Cambridge
clasohm@923
     5
clasohm@923
     6
Induction rule module -- for Inductive/Coinductive Definitions
clasohm@923
     7
clasohm@923
     8
Proves a strong induction rule and a mutual induction rule
clasohm@923
     9
*)
clasohm@923
    10
clasohm@923
    11
signature INDRULE =
clasohm@923
    12
  sig
clasohm@923
    13
  val induct        : thm			(*main induction rule*)
clasohm@923
    14
  val mutual_induct : thm			(*mutual induction rule*)
clasohm@923
    15
  end;
clasohm@923
    16
clasohm@923
    17
clasohm@923
    18
functor Indrule_Fun
clasohm@923
    19
    (structure Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end and
clasohm@923
    20
	 Intr_elim: INTR_ELIM) : INDRULE  =
clasohm@923
    21
struct
clasohm@923
    22
open Logic Ind_Syntax Inductive Intr_elim;
clasohm@923
    23
clasohm@923
    24
val sign = sign_of thy;
clasohm@923
    25
clasohm@923
    26
val (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
clasohm@923
    27
clasohm@923
    28
val elem_type = dest_setT (body_type recT);
clasohm@923
    29
val big_rec_name = space_implode "_" rec_names;
clasohm@923
    30
val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
clasohm@923
    31
lcp@1190
    32
val _ = writeln "  Proving the induction rule...";
clasohm@923
    33
clasohm@923
    34
(*** Prove the main induction rule ***)
clasohm@923
    35
clasohm@923
    36
val pred_name = "P";		(*name for predicate variables*)
clasohm@923
    37
clasohm@923
    38
val big_rec_def::part_rec_defs = Intr_elim.defs;
clasohm@923
    39
clasohm@923
    40
(*Used to express induction rules: adds induction hypotheses.
clasohm@923
    41
   ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
clasohm@923
    42
   prem is a premise of an intr rule*)
clasohm@923
    43
fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
clasohm@923
    44
		 (Const("op :",_)$t$X), iprems) =
clasohm@923
    45
     (case gen_assoc (op aconv) (ind_alist, X) of
clasohm@923
    46
	  Some pred => prem :: mk_Trueprop (pred $ t) :: iprems
clasohm@923
    47
	| None => (*possibly membership in M(rec_tm), for M monotone*)
clasohm@923
    48
	    let fun mk_sb (rec_tm,pred) = 
clasohm@923
    49
		 (case binder_types (fastype_of pred) of
clasohm@923
    50
		      [T] => (rec_tm, 
clasohm@923
    51
			      Int_const T $ rec_tm $ (Collect_const T $ pred))
clasohm@923
    52
		    | _ => error 
clasohm@923
    53
		      "Bug: add_induct_prem called with non-unary predicate")
clasohm@923
    54
	    in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
clasohm@923
    55
  | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
clasohm@923
    56
clasohm@923
    57
(*Make a premise of the induction rule.*)
clasohm@923
    58
fun induct_prem ind_alist intr =
clasohm@923
    59
  let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
clasohm@923
    60
      val iprems = foldr (add_induct_prem ind_alist)
clasohm@923
    61
			 (strip_imp_prems intr,[])
clasohm@923
    62
      val (t,X) = rule_concl intr
clasohm@923
    63
      val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
clasohm@923
    64
      val concl = mk_Trueprop (pred $ t)
clasohm@923
    65
  in list_all_free (quantfrees, list_implies (iprems,concl)) end
clasohm@923
    66
  handle Bind => error"Recursion term not found in conclusion";
clasohm@923
    67
clasohm@923
    68
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
    69
fun ind_tac [] 0 = all_tac
clasohm@923
    70
  | ind_tac(prem::prems) i = 
clasohm@923
    71
	DEPTH_SOLVE_1 (ares_tac [Part_eqI, prem, refl] i) THEN
clasohm@923
    72
	ind_tac prems (i-1);
clasohm@923
    73
clasohm@923
    74
val pred = Free(pred_name, elem_type --> boolT);
clasohm@923
    75
clasohm@923
    76
val ind_prems = map (induct_prem (map (rpair pred) rec_tms)) intr_tms;
clasohm@923
    77
lcp@1190
    78
(*Debugging code...
lcp@1190
    79
val _ = writeln "ind_prems = ";
lcp@1190
    80
val _ = seq (writeln o Sign.string_of_term sign) ind_prems;
lcp@1190
    81
*)
lcp@1190
    82
clasohm@923
    83
val quant_induct = 
clasohm@923
    84
    prove_goalw_cterm part_rec_defs 
clasohm@923
    85
      (cterm_of sign (list_implies (ind_prems, 
lcp@1190
    86
				mk_Trueprop (mk_all_imp (big_rec_tm,pred)))))
clasohm@923
    87
      (fn prems =>
clasohm@923
    88
       [rtac (impI RS allI) 1,
lcp@1190
    89
	DETERM (etac raw_induct 1),
lcp@1190
    90
	asm_full_simp_tac (HOL_ss addsimps [Part_Collect]) 1,
clasohm@923
    91
	REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE] 
clasohm@923
    92
			   ORELSE' hyp_subst_tac)),
clasohm@923
    93
	ind_tac (rev prems) (length prems)])
clasohm@923
    94
    handle e => print_sign_exn sign e;
clasohm@923
    95
clasohm@923
    96
(*** Prove the simultaneous induction rule ***)
clasohm@923
    97
clasohm@923
    98
(*Make distinct predicates for each inductive set.
lcp@1190
    99
  Splits cartesian products in elem_type, IF nested to the right! *)
clasohm@923
   100
lcp@1190
   101
(*Given a recursive set, return the "split" predicate
clasohm@923
   102
  and a conclusion for the simultaneous induction rule*)
lcp@1190
   103
fun mk_predpair rec_tm = 
clasohm@923
   104
  let val rec_name = (#1 o dest_Const o head_of) rec_tm
lcp@1190
   105
      val T = factors elem_type ---> boolT
clasohm@923
   106
      val pfree = Free(pred_name ^ "_" ^ rec_name, T)
clasohm@923
   107
      val frees = mk_frees "za" (binder_types T)
clasohm@923
   108
      val qconcl = 
clasohm@923
   109
	foldr mk_all (frees, 
clasohm@923
   110
		      imp $ (mk_mem (foldr1 mk_Pair frees, rec_tm))
clasohm@923
   111
			  $ (list_comb (pfree,frees)))
clasohm@923
   112
  in  (ap_split boolT pfree (binder_types T), 
clasohm@923
   113
      qconcl)  
clasohm@923
   114
  end;
clasohm@923
   115
lcp@1190
   116
val (preds,qconcls) = split_list (map mk_predpair rec_tms);
clasohm@923
   117
clasohm@923
   118
(*Used to form simultaneous induction lemma*)
clasohm@923
   119
fun mk_rec_imp (rec_tm,pred) = 
clasohm@923
   120
    imp $ (mk_mem (Bound 0, rec_tm)) $  (pred $ Bound 0);
clasohm@923
   121
clasohm@923
   122
(*To instantiate the main induction rule*)
clasohm@923
   123
val induct_concl = 
clasohm@923
   124
 mk_Trueprop(mk_all_imp(big_rec_tm,
clasohm@923
   125
		     Abs("z", elem_type, 
clasohm@923
   126
			 fold_bal (app conj) 
clasohm@923
   127
			          (map mk_rec_imp (rec_tms~~preds)))))
clasohm@923
   128
and mutual_induct_concl = mk_Trueprop(fold_bal (app conj) qconcls);
clasohm@923
   129
clasohm@923
   130
val lemma = (*makes the link between the two induction rules*)
clasohm@923
   131
    prove_goalw_cterm part_rec_defs 
clasohm@923
   132
	  (cterm_of sign (mk_implies (induct_concl,mutual_induct_concl)))
clasohm@923
   133
	  (fn prems =>
clasohm@923
   134
	   [cut_facts_tac prems 1,
clasohm@923
   135
	    REPEAT (eresolve_tac [asm_rl, conjE, PartE, mp] 1
clasohm@923
   136
	     ORELSE resolve_tac [allI, impI, conjI, Part_eqI, refl] 1
clasohm@923
   137
	     ORELSE dresolve_tac [spec, mp, splitD] 1)])
clasohm@923
   138
    handle e => print_sign_exn sign e;
clasohm@923
   139
clasohm@923
   140
(*Mutual induction follows by freeness of Inl/Inr.*)
clasohm@923
   141
lcp@1190
   142
(*Simplification largely reduces the mutual induction rule to the 
lcp@1190
   143
  standard rule*)
lcp@1190
   144
val mut_ss = set_ss addsimps [Inl_Inr_eq, Inr_Inl_eq, Inl_eq, Inr_eq];
lcp@1190
   145
lcp@1190
   146
val all_defs = con_defs@part_rec_defs;
lcp@1190
   147
clasohm@923
   148
(*Removes Collects caused by M-operators in the intro rules*)
clasohm@923
   149
val cmonos = [subset_refl RS Int_Collect_mono] RL monos RLN (2,[rev_subsetD]);
clasohm@923
   150
clasohm@923
   151
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
   152
fun mutual_ind_tac [] 0 = all_tac
clasohm@923
   153
  | mutual_ind_tac(prem::prems) i = 
clasohm@923
   154
      DETERM
clasohm@923
   155
       (SELECT_GOAL 
lcp@1190
   156
	  (
lcp@1190
   157
	   (*Simplify the assumptions and goal by unfolding Part and
lcp@1190
   158
	     using freeness of the Sum constructors; proves all but one
lcp@1190
   159
             conjunct by contradiction*)
lcp@1190
   160
	   rewrite_goals_tac all_defs  THEN
lcp@1190
   161
	   simp_tac (mut_ss addsimps [Part_def]) 1  THEN
lcp@1190
   162
	   IF_UNSOLVED (*simp_tac may have finished it off!*)
lcp@1190
   163
	     ((*simplify assumptions, but don't accept new rewrite rules!*)
lcp@1190
   164
	      asm_full_simp_tac (mut_ss setmksimps K[]) 1  THEN
lcp@1190
   165
	      (*unpackage and use "prem" in the corresponding place*)
lcp@1190
   166
	      REPEAT (rtac impI 1)  THEN
lcp@1190
   167
	      rtac (rewrite_rule all_defs prem) 1  THEN
lcp@1190
   168
	      (*prem must not be REPEATed below: could loop!*)
lcp@1190
   169
	      DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
lcp@1190
   170
				      eresolve_tac (conjE::mp::cmonos))))
lcp@1190
   171
	  ) i)
lcp@1190
   172
       THEN mutual_ind_tac prems (i-1);
lcp@1190
   173
lcp@1190
   174
val _ = writeln "  Proving the mutual induction rule...";
clasohm@923
   175
clasohm@923
   176
val mutual_induct_split = 
clasohm@923
   177
    prove_goalw_cterm []
clasohm@923
   178
	  (cterm_of sign
clasohm@923
   179
	   (list_implies (map (induct_prem (rec_tms~~preds)) intr_tms,
clasohm@923
   180
			  mutual_induct_concl)))
clasohm@923
   181
	  (fn prems =>
clasohm@923
   182
	   [rtac (quant_induct RS lemma) 1,
clasohm@923
   183
	    mutual_ind_tac (rev prems) (length prems)])
clasohm@923
   184
    handle e => print_sign_exn sign e;
clasohm@923
   185
clasohm@923
   186
(*Attempts to remove all occurrences of split*)
clasohm@923
   187
val split_tac =
clasohm@923
   188
    REPEAT (SOMEGOAL (FIRST' [rtac splitI, 
clasohm@923
   189
			      dtac splitD,
clasohm@923
   190
			      etac splitE,
clasohm@923
   191
			      bound_hyp_subst_tac]))
clasohm@923
   192
    THEN prune_params_tac;
clasohm@923
   193
clasohm@923
   194
(*strip quantifier*)
clasohm@923
   195
val induct = standard (quant_induct RS spec RSN (2,rev_mp));
clasohm@923
   196
clasohm@923
   197
val mutual_induct = rule_by_tactic split_tac mutual_induct_split;
clasohm@923
   198
clasohm@923
   199
end;