src/ZF/indrule.ML
author lcp
Thu Aug 18 17:41:40 1994 +0200 (1994-08-18 ago)
changeset 543 e961b2092869
parent 516 1957113f0d7d
child 590 800603278425
permissions -rw-r--r--
ZF/ind_syntax/unvarifyT, unvarify: moved to Pure/logic.ML
ZF/ind_syntax/prove_term: deleted

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