src/ZF/indrule.ML
author clasohm
Thu Sep 16 12:20:38 1993 +0200 (1993-09-16)
changeset 0 a5a9c433f639
child 366 5b6e4340085b
permissions -rw-r--r--
Initial revision
clasohm@0
     1
(*  Title: 	ZF/indrule.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Induction rule module -- for Inductive/Coinductive Definitions
clasohm@0
     7
clasohm@0
     8
Proves a strong induction rule and a mutual induction rule
clasohm@0
     9
*)
clasohm@0
    10
clasohm@0
    11
signature INDRULE =
clasohm@0
    12
  sig
clasohm@0
    13
  val induct        : thm			(*main induction rule*)
clasohm@0
    14
  val mutual_induct : thm			(*mutual induction rule*)
clasohm@0
    15
  end;
clasohm@0
    16
clasohm@0
    17
clasohm@0
    18
functor Indrule_Fun (structure Ind: INDUCTIVE and 
clasohm@0
    19
		     Pr: PR and Intr_elim: INTR_ELIM) : INDRULE  =
clasohm@0
    20
struct
clasohm@0
    21
open Logic Ind Intr_elim;
clasohm@0
    22
clasohm@0
    23
val dummy = writeln "Proving the induction rules...";
clasohm@0
    24
clasohm@0
    25
(*** Prove the main induction rule ***)
clasohm@0
    26
clasohm@0
    27
val pred_name = "P";		(*name for predicate variables*)
clasohm@0
    28
clasohm@0
    29
val prove = prove_term (sign_of Intr_elim.thy);
clasohm@0
    30
clasohm@0
    31
val big_rec_def::part_rec_defs = Intr_elim.defs;
clasohm@0
    32
clasohm@0
    33
(*Used to make induction rules;
clasohm@0
    34
   ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
clasohm@0
    35
   prem is a premise of an intr rule*)
clasohm@0
    36
fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
clasohm@0
    37
		 (Const("op :",_)$t$X), iprems) =
clasohm@0
    38
     (case gen_assoc (op aconv) (ind_alist, X) of
clasohm@0
    39
	  Some pred => prem :: mk_tprop (pred $ t) :: iprems
clasohm@0
    40
	| None => (*possibly membership in M(rec_tm), for M monotone*)
clasohm@0
    41
	    let fun mk_sb (rec_tm,pred) = (rec_tm, Collect_const$rec_tm$pred)
clasohm@0
    42
	    in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
clasohm@0
    43
  | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
clasohm@0
    44
clasohm@0
    45
(*Make a premise of the induction rule.*)
clasohm@0
    46
fun induct_prem ind_alist intr =
clasohm@0
    47
  let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
clasohm@0
    48
      val iprems = foldr (add_induct_prem ind_alist)
clasohm@0
    49
			 (strip_imp_prems intr,[])
clasohm@0
    50
      val (t,X) = rule_concl intr
clasohm@0
    51
      val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
clasohm@0
    52
      val concl = mk_tprop (pred $ t)
clasohm@0
    53
  in list_all_free (quantfrees, list_implies (iprems,concl)) end
clasohm@0
    54
  handle Bind => error"Recursion term not found in conclusion";
clasohm@0
    55
clasohm@0
    56
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@0
    57
fun ind_tac [] 0 = all_tac
clasohm@0
    58
  | ind_tac(prem::prems) i = REPEAT (ares_tac [Part_eqI,prem] i) THEN
clasohm@0
    59
			     ind_tac prems (i-1);
clasohm@0
    60
clasohm@0
    61
val pred = Free(pred_name, iT-->oT);
clasohm@0
    62
clasohm@0
    63
val ind_prems = map (induct_prem (map (rpair pred) rec_tms)) intr_tms;
clasohm@0
    64
clasohm@0
    65
val quant_induct = 
clasohm@0
    66
    prove part_rec_defs 
clasohm@0
    67
      (list_implies (ind_prems, mk_tprop (mk_all_imp(big_rec_tm,pred))),
clasohm@0
    68
       fn prems =>
clasohm@0
    69
       [rtac (impI RS allI) 1,
clasohm@0
    70
	etac raw_induct 1,
clasohm@0
    71
	REPEAT (FIRSTGOAL (eresolve_tac [CollectE,exE,conjE,disjE,ssubst])),
clasohm@0
    72
	REPEAT (FIRSTGOAL (eresolve_tac [PartE,CollectE])),
clasohm@0
    73
	ind_tac (rev prems) (length prems) ]);
clasohm@0
    74
clasohm@0
    75
(*** Prove the simultaneous induction rule ***)
clasohm@0
    76
clasohm@0
    77
(*Make distinct predicates for each inductive set*)
clasohm@0
    78
clasohm@0
    79
(*Sigmas and Cartesian products may nest ONLY to the right!*)
clasohm@0
    80
fun mk_pred_typ (t $ A $ B) = 
clasohm@0
    81
        if t = Pr.sigma  then  iT --> mk_pred_typ B
clasohm@0
    82
                         else  iT --> oT
clasohm@0
    83
  | mk_pred_typ _           =  iT --> oT
clasohm@0
    84
clasohm@0
    85
(*Given a recursive set and its domain, return the "fsplit" predicate
clasohm@0
    86
  and a conclusion for the simultaneous induction rule*)
clasohm@0
    87
fun mk_predpair (rec_tm,domt) = 
clasohm@0
    88
  let val rec_name = (#1 o dest_Const o head_of) rec_tm
clasohm@0
    89
      val T = mk_pred_typ domt
clasohm@0
    90
      val pfree = Free(pred_name ^ "_" ^ rec_name, T)
clasohm@0
    91
      val frees = mk_frees "za" (binder_types T)
clasohm@0
    92
      val qconcl = 
clasohm@0
    93
	foldr mk_all (frees, 
clasohm@0
    94
		      imp $ (mem_const $ foldr1 (app Pr.pair) frees $ rec_tm)
clasohm@0
    95
			  $ (list_comb (pfree,frees)))
clasohm@0
    96
  in  (ap_split Pr.fsplit_const pfree (binder_types T), 
clasohm@0
    97
      qconcl)  
clasohm@0
    98
  end;
clasohm@0
    99
clasohm@0
   100
val (preds,qconcls) = split_list (map mk_predpair (rec_tms~~domts));
clasohm@0
   101
clasohm@0
   102
(*Used to form simultaneous induction lemma*)
clasohm@0
   103
fun mk_rec_imp (rec_tm,pred) = 
clasohm@0
   104
    imp $ (mem_const $ Bound 0 $ rec_tm) $  (pred $ Bound 0);
clasohm@0
   105
clasohm@0
   106
(*To instantiate the main induction rule*)
clasohm@0
   107
val induct_concl = 
clasohm@0
   108
 mk_tprop(mk_all_imp(big_rec_tm,
clasohm@0
   109
		     Abs("z", iT, 
clasohm@0
   110
			 fold_bal (app conj) 
clasohm@0
   111
			          (map mk_rec_imp (rec_tms~~preds)))))
clasohm@0
   112
and mutual_induct_concl = mk_tprop(fold_bal (app conj) qconcls);
clasohm@0
   113
clasohm@0
   114
val lemma = (*makes the link between the two induction rules*)
clasohm@0
   115
    prove part_rec_defs 
clasohm@0
   116
	  (mk_implies (induct_concl,mutual_induct_concl), 
clasohm@0
   117
	   fn prems =>
clasohm@0
   118
	   [cut_facts_tac prems 1,
clasohm@0
   119
	    REPEAT (eresolve_tac [asm_rl,conjE,PartE,mp] 1
clasohm@0
   120
	     ORELSE resolve_tac [allI,impI,conjI,Part_eqI] 1
clasohm@0
   121
	     ORELSE dresolve_tac [spec, mp, Pr.fsplitD] 1)]);
clasohm@0
   122
clasohm@0
   123
(*Mutual induction follows by freeness of Inl/Inr.*)
clasohm@0
   124
clasohm@0
   125
(*Removes Collects caused by M-operators in the intro rules*)
clasohm@0
   126
val cmonos = [subset_refl RS Collect_mono] RL monos RLN (2,[rev_subsetD]);
clasohm@0
   127
clasohm@0
   128
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@0
   129
fun mutual_ind_tac [] 0 = all_tac
clasohm@0
   130
  | mutual_ind_tac(prem::prems) i = 
clasohm@0
   131
      SELECT_GOAL 
clasohm@0
   132
	((*unpackage and use "prem" in the corresponding place*)
clasohm@0
   133
	 REPEAT (FIRSTGOAL
clasohm@0
   134
		    (eresolve_tac ([conjE,mp]@cmonos) ORELSE'
clasohm@0
   135
		     ares_tac [prem,impI,conjI]))
clasohm@0
   136
	 (*prove remaining goals by contradiction*)
clasohm@0
   137
	 THEN rewrite_goals_tac (con_defs@part_rec_defs)
clasohm@0
   138
	 THEN REPEAT (eresolve_tac (PartE :: sumprod_free_SEs) 1))
clasohm@0
   139
	i  THEN mutual_ind_tac prems (i-1);
clasohm@0
   140
clasohm@0
   141
val mutual_induct_fsplit = 
clasohm@0
   142
    prove []
clasohm@0
   143
	  (list_implies (map (induct_prem (rec_tms~~preds)) intr_tms,
clasohm@0
   144
			 mutual_induct_concl),
clasohm@0
   145
	   fn prems =>
clasohm@0
   146
	   [rtac (quant_induct RS lemma) 1,
clasohm@0
   147
	    mutual_ind_tac (rev prems) (length prems)]);
clasohm@0
   148
clasohm@0
   149
(*Attempts to remove all occurrences of fsplit*)
clasohm@0
   150
val fsplit_tac =
clasohm@0
   151
    REPEAT (SOMEGOAL (FIRST' [rtac Pr.fsplitI, 
clasohm@0
   152
			      dtac Pr.fsplitD,
clasohm@0
   153
			      etac Pr.fsplitE,
clasohm@0
   154
			      bound_hyp_subst_tac]))
clasohm@0
   155
    THEN prune_params_tac;
clasohm@0
   156
clasohm@0
   157
(*strip quantifier*)
clasohm@0
   158
val induct = standard (quant_induct RS spec RSN (2,rev_mp));
clasohm@0
   159
clasohm@0
   160
val mutual_induct = rule_by_tactic fsplit_tac mutual_induct_fsplit;
clasohm@0
   161
clasohm@0
   162
end;