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