src/HOL/indrule.ML
author clasohm
Fri Mar 03 12:02:25 1995 +0100 (1995-03-03)
changeset 923 ff1574a81019
child 1190 9d1bdce3a38e
permissions -rw-r--r--
new version of HOL with curried function application
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 domTs = summands(elem_type);
clasohm@923
    30
val big_rec_name = space_implode "_" rec_names;
clasohm@923
    31
val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
clasohm@923
    32
clasohm@923
    33
val _ = writeln "  Proving the induction rules...";
clasohm@923
    34
clasohm@923
    35
(*** Prove the main induction rule ***)
clasohm@923
    36
clasohm@923
    37
val pred_name = "P";		(*name for predicate variables*)
clasohm@923
    38
clasohm@923
    39
val big_rec_def::part_rec_defs = Intr_elim.defs;
clasohm@923
    40
clasohm@923
    41
(*Used to express induction rules: adds induction hypotheses.
clasohm@923
    42
   ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
clasohm@923
    43
   prem is a premise of an intr rule*)
clasohm@923
    44
fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
clasohm@923
    45
		 (Const("op :",_)$t$X), iprems) =
clasohm@923
    46
     (case gen_assoc (op aconv) (ind_alist, X) of
clasohm@923
    47
	  Some pred => prem :: mk_Trueprop (pred $ t) :: iprems
clasohm@923
    48
	| None => (*possibly membership in M(rec_tm), for M monotone*)
clasohm@923
    49
	    let fun mk_sb (rec_tm,pred) = 
clasohm@923
    50
		 (case binder_types (fastype_of pred) of
clasohm@923
    51
		      [T] => (rec_tm, 
clasohm@923
    52
			      Int_const T $ rec_tm $ (Collect_const T $ pred))
clasohm@923
    53
		    | _ => error 
clasohm@923
    54
		      "Bug: add_induct_prem called with non-unary predicate")
clasohm@923
    55
	    in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
clasohm@923
    56
  | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
clasohm@923
    57
clasohm@923
    58
(*Make a premise of the induction rule.*)
clasohm@923
    59
fun induct_prem ind_alist intr =
clasohm@923
    60
  let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
clasohm@923
    61
      val iprems = foldr (add_induct_prem ind_alist)
clasohm@923
    62
			 (strip_imp_prems intr,[])
clasohm@923
    63
      val (t,X) = rule_concl intr
clasohm@923
    64
      val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
clasohm@923
    65
      val concl = mk_Trueprop (pred $ t)
clasohm@923
    66
  in list_all_free (quantfrees, list_implies (iprems,concl)) end
clasohm@923
    67
  handle Bind => error"Recursion term not found in conclusion";
clasohm@923
    68
clasohm@923
    69
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
    70
fun ind_tac [] 0 = all_tac
clasohm@923
    71
  | ind_tac(prem::prems) i = 
clasohm@923
    72
	DEPTH_SOLVE_1 (ares_tac [Part_eqI, prem, refl] i) THEN
clasohm@923
    73
	ind_tac prems (i-1);
clasohm@923
    74
clasohm@923
    75
val pred = Free(pred_name, elem_type --> boolT);
clasohm@923
    76
clasohm@923
    77
val ind_prems = map (induct_prem (map (rpair pred) rec_tms)) intr_tms;
clasohm@923
    78
clasohm@923
    79
val quant_induct = 
clasohm@923
    80
    prove_goalw_cterm part_rec_defs 
clasohm@923
    81
      (cterm_of sign (list_implies (ind_prems, 
clasohm@923
    82
				    mk_Trueprop (mk_all_imp(big_rec_tm,pred)))))
clasohm@923
    83
      (fn prems =>
clasohm@923
    84
       [rtac (impI RS allI) 1,
clasohm@923
    85
	etac raw_induct 1,
clasohm@923
    86
	REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE] 
clasohm@923
    87
			   ORELSE' hyp_subst_tac)),
clasohm@923
    88
	REPEAT (FIRSTGOAL (eresolve_tac [PartE, CollectE])),
clasohm@923
    89
	ind_tac (rev prems) (length prems)])
clasohm@923
    90
    handle e => print_sign_exn sign e;
clasohm@923
    91
clasohm@923
    92
(*** Prove the simultaneous induction rule ***)
clasohm@923
    93
clasohm@923
    94
(*Make distinct predicates for each inductive set.
clasohm@923
    95
  Splits cartesian products in domT, IF nested to the right! *)
clasohm@923
    96
clasohm@923
    97
(*Given a recursive set and its domain, return the "split" predicate
clasohm@923
    98
  and a conclusion for the simultaneous induction rule*)
clasohm@923
    99
fun mk_predpair (rec_tm,domT) = 
clasohm@923
   100
  let val rec_name = (#1 o dest_Const o head_of) rec_tm
clasohm@923
   101
      val T = factors domT ---> boolT
clasohm@923
   102
      val pfree = Free(pred_name ^ "_" ^ rec_name, T)
clasohm@923
   103
      val frees = mk_frees "za" (binder_types T)
clasohm@923
   104
      val qconcl = 
clasohm@923
   105
	foldr mk_all (frees, 
clasohm@923
   106
		      imp $ (mk_mem (foldr1 mk_Pair frees, rec_tm))
clasohm@923
   107
			  $ (list_comb (pfree,frees)))
clasohm@923
   108
  in  (ap_split boolT pfree (binder_types T), 
clasohm@923
   109
      qconcl)  
clasohm@923
   110
  end;
clasohm@923
   111
clasohm@923
   112
val (preds,qconcls) = split_list (map mk_predpair (rec_tms~~domTs));
clasohm@923
   113
clasohm@923
   114
(*Used to form simultaneous induction lemma*)
clasohm@923
   115
fun mk_rec_imp (rec_tm,pred) = 
clasohm@923
   116
    imp $ (mk_mem (Bound 0, rec_tm)) $  (pred $ Bound 0);
clasohm@923
   117
clasohm@923
   118
(*To instantiate the main induction rule*)
clasohm@923
   119
val induct_concl = 
clasohm@923
   120
 mk_Trueprop(mk_all_imp(big_rec_tm,
clasohm@923
   121
		     Abs("z", elem_type, 
clasohm@923
   122
			 fold_bal (app conj) 
clasohm@923
   123
			          (map mk_rec_imp (rec_tms~~preds)))))
clasohm@923
   124
and mutual_induct_concl = mk_Trueprop(fold_bal (app conj) qconcls);
clasohm@923
   125
clasohm@923
   126
val lemma = (*makes the link between the two induction rules*)
clasohm@923
   127
    prove_goalw_cterm part_rec_defs 
clasohm@923
   128
	  (cterm_of sign (mk_implies (induct_concl,mutual_induct_concl)))
clasohm@923
   129
	  (fn prems =>
clasohm@923
   130
	   [cut_facts_tac prems 1,
clasohm@923
   131
	    REPEAT (eresolve_tac [asm_rl, conjE, PartE, mp] 1
clasohm@923
   132
	     ORELSE resolve_tac [allI, impI, conjI, Part_eqI, refl] 1
clasohm@923
   133
	     ORELSE dresolve_tac [spec, mp, splitD] 1)])
clasohm@923
   134
    handle e => print_sign_exn sign e;
clasohm@923
   135
clasohm@923
   136
(*Mutual induction follows by freeness of Inl/Inr.*)
clasohm@923
   137
clasohm@923
   138
(*Removes Collects caused by M-operators in the intro rules*)
clasohm@923
   139
val cmonos = [subset_refl RS Int_Collect_mono] RL monos RLN (2,[rev_subsetD]);
clasohm@923
   140
clasohm@923
   141
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
   142
fun mutual_ind_tac [] 0 = all_tac
clasohm@923
   143
  | mutual_ind_tac(prem::prems) i = 
clasohm@923
   144
      DETERM
clasohm@923
   145
       (SELECT_GOAL 
clasohm@923
   146
	  ((*unpackage and use "prem" in the corresponding place*)
clasohm@923
   147
	   REPEAT (FIRSTGOAL
clasohm@923
   148
		   (etac conjE ORELSE' eq_mp_tac ORELSE' 
clasohm@923
   149
		    ares_tac [impI, conjI]))
clasohm@923
   150
	   (*prem is not allowed in the REPEAT, lest it loop!*)
clasohm@923
   151
	   THEN TRYALL (rtac prem)
clasohm@923
   152
	   THEN REPEAT
clasohm@923
   153
		  (FIRSTGOAL (ares_tac [impI] ORELSE' 
clasohm@923
   154
			      eresolve_tac (mp::cmonos)))
clasohm@923
   155
	   (*prove remaining goals by contradiction*)
clasohm@923
   156
	   THEN rewrite_goals_tac (con_defs@part_rec_defs)
clasohm@923
   157
	   THEN DEPTH_SOLVE (eresolve_tac (PartE :: sumprod_free_SEs) 1))
clasohm@923
   158
	  i)
clasohm@923
   159
	THEN mutual_ind_tac prems (i-1);
clasohm@923
   160
clasohm@923
   161
val mutual_induct_split = 
clasohm@923
   162
    prove_goalw_cterm []
clasohm@923
   163
	  (cterm_of sign
clasohm@923
   164
	   (list_implies (map (induct_prem (rec_tms~~preds)) intr_tms,
clasohm@923
   165
			  mutual_induct_concl)))
clasohm@923
   166
	  (fn prems =>
clasohm@923
   167
	   [rtac (quant_induct RS lemma) 1,
clasohm@923
   168
	    mutual_ind_tac (rev prems) (length prems)])
clasohm@923
   169
    handle e => print_sign_exn sign e;
clasohm@923
   170
clasohm@923
   171
(*Attempts to remove all occurrences of split*)
clasohm@923
   172
val split_tac =
clasohm@923
   173
    REPEAT (SOMEGOAL (FIRST' [rtac splitI, 
clasohm@923
   174
			      dtac splitD,
clasohm@923
   175
			      etac splitE,
clasohm@923
   176
			      bound_hyp_subst_tac]))
clasohm@923
   177
    THEN prune_params_tac;
clasohm@923
   178
clasohm@923
   179
(*strip quantifier*)
clasohm@923
   180
val induct = standard (quant_induct RS spec RSN (2,rev_mp));
clasohm@923
   181
clasohm@923
   182
val mutual_induct = rule_by_tactic split_tac mutual_induct_split;
clasohm@923
   183
clasohm@923
   184
end;