src/ZF/Tools/inductive_package.ML
author paulson
Mon, 28 Dec 1998 16:58:00 +0100
changeset 6051 7d457fc538e7
child 6092 d9db67970c73
permissions -rw-r--r--
revised inductive definition package
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6051
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     1
(*  Title:      ZF/inductive_package.ML
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     2
    ID:         $Id$
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     5
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     6
Fixedpoint definition module -- for Inductive/Coinductive Definitions
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     7
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     8
The functor will be instantiated for normal sums/products (inductive defs)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
     9
                         and non-standard sums/products (coinductive defs)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    10
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    11
Sums are used only for mutual recursion;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    12
Products are used only to derive "streamlined" induction rules for relations
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    13
*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    14
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    15
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    16
type inductive_result =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    17
   {defs       : thm list,             (*definitions made in thy*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    18
    bnd_mono   : thm,                  (*monotonicity for the lfp definition*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    19
    dom_subset : thm,                  (*inclusion of recursive set in dom*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    20
    intrs      : thm list,             (*introduction rules*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    21
    elim       : thm,                  (*case analysis theorem*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    22
    mk_cases   : thm list -> string -> thm,    (*generates case theorems*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    23
    induct     : thm,                  (*main induction rule*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    24
    mutual_induct : thm};              (*mutual induction rule*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    25
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    26
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    27
(*Functor's result signature*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    28
signature INDUCTIVE_PACKAGE =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    29
  sig 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    30
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    31
  (*Insert definitions for the recursive sets, which
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    32
     must *already* be declared as constants in parent theory!*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    33
  val add_inductive_i : 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    34
      bool ->
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    35
      term list * term * term list * thm list * thm list * thm list * thm list
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    36
      -> theory -> theory * inductive_result
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    37
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    38
  val add_inductive : 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    39
      string list * string * string list * 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    40
      thm list * thm list * thm list * thm list
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    41
      -> theory -> theory * inductive_result
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    42
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    43
  end;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    44
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    45
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    46
(*Declares functions to add fixedpoint/constructor defs to a theory.
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    47
  Recursive sets must *already* be declared as constants.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    48
functor Add_inductive_def_Fun 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    49
    (structure Fp: FP and Pr : PR and CP: CARTPROD and Su : SU)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    50
 : INDUCTIVE_PACKAGE =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    51
struct
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    52
open Logic Ind_Syntax;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    53
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    54
(*internal version, accepting terms*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    55
fun add_inductive_i verbose (rec_tms, dom_sum, intr_tms, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    56
			     monos, con_defs, type_intrs, type_elims) thy = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    57
 let
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    58
  val dummy = (*has essential ancestors?*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    59
      Theory.requires thy "Inductive" "(co)inductive definitions" 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    60
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    61
  val sign = sign_of thy;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    62
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    63
  (*recT and rec_params should agree for all mutually recursive components*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    64
  val rec_hds = map head_of rec_tms;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    65
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    66
  val dummy = assert_all is_Const rec_hds
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    67
	  (fn t => "Recursive set not previously declared as constant: " ^ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    68
		   Sign.string_of_term sign t);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    69
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    70
  (*Now we know they are all Consts, so get their names, type and params*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    71
  val rec_names = map (#1 o dest_Const) rec_hds
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    72
  and (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    73
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    74
  val rec_base_names = map Sign.base_name rec_names;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    75
  val dummy = assert_all Syntax.is_identifier rec_base_names
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    76
    (fn a => "Base name of recursive set not an identifier: " ^ a);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    77
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    78
  local (*Checking the introduction rules*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    79
    val intr_sets = map (#2 o rule_concl_msg sign) intr_tms;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    80
    fun intr_ok set =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    81
	case head_of set of Const(a,recT) => a mem rec_names | _ => false;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    82
  in
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    83
    val dummy =  assert_all intr_ok intr_sets
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    84
       (fn t => "Conclusion of rule does not name a recursive set: " ^ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    85
		Sign.string_of_term sign t);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    86
  end;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    87
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    88
  val dummy = assert_all is_Free rec_params
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    89
      (fn t => "Param in recursion term not a free variable: " ^
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    90
	       Sign.string_of_term sign t);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    91
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    92
  (*** Construct the fixedpoint definition ***)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    93
  val mk_variant = variant (foldr add_term_names (intr_tms,[]));
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    94
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    95
  val z' = mk_variant"z" and X' = mk_variant"X" and w' = mk_variant"w";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    96
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    97
  fun dest_tprop (Const("Trueprop",_) $ P) = P
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    98
    | dest_tprop Q = error ("Ill-formed premise of introduction rule: " ^ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
    99
			    Sign.string_of_term sign Q);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   100
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   101
  (*Makes a disjunct from an introduction rule*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   102
  fun fp_part intr = (*quantify over rule's free vars except parameters*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   103
    let val prems = map dest_tprop (strip_imp_prems intr)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   104
	val dummy = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   105
	val exfrees = term_frees intr \\ rec_params
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   106
	val zeq = FOLogic.mk_eq (Free(z',iT), #1 (rule_concl intr))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   107
    in foldr FOLogic.mk_exists
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   108
	     (exfrees, fold_bal (app FOLogic.conj) (zeq::prems)) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   109
    end;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   110
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   111
  (*The Part(A,h) terms -- compose injections to make h*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   112
  fun mk_Part (Bound 0) = Free(X',iT) (*no mutual rec, no Part needed*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   113
    | mk_Part h         = Part_const $ Free(X',iT) $ Abs(w',iT,h);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   114
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   115
  (*Access to balanced disjoint sums via injections*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   116
  val parts = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   117
      map mk_Part (accesses_bal (ap Su.inl, ap Su.inr, Bound 0) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   118
				(length rec_tms));
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   119
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   120
  (*replace each set by the corresponding Part(A,h)*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   121
  val part_intrs = map (subst_free (rec_tms ~~ parts) o fp_part) intr_tms;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   122
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   123
  val fp_abs = absfree(X', iT, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   124
		   mk_Collect(z', dom_sum, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   125
			      fold_bal (app FOLogic.disj) part_intrs));
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   126
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   127
  val fp_rhs = Fp.oper $ dom_sum $ fp_abs
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   128
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   129
  val dummy = seq (fn rec_hd => deny (rec_hd occs fp_rhs) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   130
			     "Illegal occurrence of recursion operator")
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   131
	   rec_hds;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   132
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   133
  (*** Make the new theory ***)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   134
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   135
  (*A key definition:
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   136
    If no mutual recursion then it equals the one recursive set.
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   137
    If mutual recursion then it differs from all the recursive sets. *)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   138
  val big_rec_base_name = space_implode "_" rec_base_names;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   139
  val big_rec_name = Sign.intern_const sign big_rec_base_name;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   140
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   141
  
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   142
  val dummy =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   143
      if verbose then
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   144
	  writeln ((if #1 (dest_Const Fp.oper) = "lfp" then "Inductive" 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   145
		    else "Coinductive") ^ " definition " ^ big_rec_name)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   146
      else ();
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   147
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   148
  (*Forbid the inductive definition structure from clashing with a theory
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   149
    name.  This restriction may become obsolete as ML is de-emphasized.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   150
  val dummy = deny (big_rec_base_name mem (Sign.stamp_names_of sign))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   151
	       ("Definition " ^ big_rec_base_name ^ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   152
		" would clash with the theory of the same name!");
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   153
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   154
  (*Big_rec... is the union of the mutually recursive sets*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   155
  val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   156
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   157
  (*The individual sets must already be declared*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   158
  val axpairs = map Logic.mk_defpair 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   159
	((big_rec_tm, fp_rhs) ::
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   160
	 (case parts of 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   161
	     [_] => []                        (*no mutual recursion*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   162
	   | _ => rec_tms ~~          (*define the sets as Parts*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   163
		  map (subst_atomic [(Free(X',iT),big_rec_tm)]) parts));
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   164
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   165
  (*tracing: print the fixedpoint definition*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   166
  val dummy = if !Ind_Syntax.trace then
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   167
	      seq (writeln o Sign.string_of_term sign o #2) axpairs
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   168
	  else ()
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   169
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   170
  (*add definitions of the inductive sets*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   171
  val thy1 = thy |> Theory.add_path big_rec_base_name
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   172
                 |> PureThy.add_defs_i (map Attribute.none axpairs)  
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   173
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   174
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   175
  (*fetch fp definitions from the theory*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   176
  val big_rec_def::part_rec_defs = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   177
    map (get_def thy1)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   178
	(case rec_names of [_] => rec_names 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   179
			 | _   => big_rec_base_name::rec_names);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   180
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   181
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   182
  val sign1 = sign_of thy1;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   183
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   184
  (********)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   185
  val dummy = writeln "  Proving monotonicity...";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   186
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   187
  val bnd_mono = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   188
      prove_goalw_cterm [] 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   189
	(cterm_of sign1
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   190
		  (FOLogic.mk_Trueprop (Fp.bnd_mono $ dom_sum $ fp_abs)))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   191
	(fn _ =>
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   192
	 [rtac (Collect_subset RS bnd_monoI) 1,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   193
	  REPEAT (ares_tac (basic_monos @ monos) 1)]);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   194
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   195
  val dom_subset = standard (big_rec_def RS Fp.subs);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   196
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   197
  val unfold = standard ([big_rec_def, bnd_mono] MRS Fp.Tarski);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   198
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   199
  (********)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   200
  val dummy = writeln "  Proving the introduction rules...";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   201
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   202
  (*Mutual recursion?  Helps to derive subset rules for the 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   203
    individual sets.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   204
  val Part_trans =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   205
      case rec_names of
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   206
	   [_] => asm_rl
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   207
	 | _   => standard (Part_subset RS subset_trans);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   208
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   209
  (*To type-check recursive occurrences of the inductive sets, possibly
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   210
    enclosed in some monotonic operator M.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   211
  val rec_typechecks = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   212
     [dom_subset] RL (asm_rl :: ([Part_trans] RL monos)) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   213
     RL [subsetD];
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   214
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   215
  (*Type-checking is hardest aspect of proof;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   216
    disjIn selects the correct disjunct after unfolding*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   217
  fun intro_tacsf disjIn prems = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   218
    [(*insert prems and underlying sets*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   219
     cut_facts_tac prems 1,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   220
     DETERM (stac unfold 1),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   221
     REPEAT (resolve_tac [Part_eqI,CollectI] 1),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   222
     (*Now 2-3 subgoals: typechecking, the disjunction, perhaps equality.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   223
     rtac disjIn 2,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   224
     (*Not ares_tac, since refl must be tried before equality assumptions;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   225
       backtracking may occur if the premises have extra variables!*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   226
     DEPTH_SOLVE_1 (resolve_tac [refl,exI,conjI] 2 APPEND assume_tac 2),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   227
     (*Now solve the equations like Tcons(a,f) = Inl(?b4)*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   228
     rewrite_goals_tac con_defs,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   229
     REPEAT (rtac refl 2),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   230
     (*Typechecking; this can fail*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   231
     if !Ind_Syntax.trace then print_tac "The typechecking subgoal:"
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   232
     else all_tac,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   233
     REPEAT (FIRSTGOAL (        dresolve_tac rec_typechecks
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   234
			ORELSE' eresolve_tac (asm_rl::PartE::SigmaE2::
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   235
					      type_elims)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   236
			ORELSE' hyp_subst_tac)),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   237
     if !Ind_Syntax.trace then print_tac "The subgoal after monos, type_elims:"
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   238
     else all_tac,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   239
     DEPTH_SOLVE (swap_res_tac (SigmaI::subsetI::type_intrs) 1)];
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   240
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   241
  (*combines disjI1 and disjI2 to get the corresponding nested disjunct...*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   242
  val mk_disj_rls = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   243
      let fun f rl = rl RS disjI1
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   244
	  and g rl = rl RS disjI2
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   245
      in  accesses_bal(f, g, asm_rl)  end;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   246
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   247
  fun prove_intr (ct, tacsf) = prove_goalw_cterm part_rec_defs ct tacsf;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   248
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   249
  val intrs = ListPair.map prove_intr
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   250
		(map (cterm_of sign1) intr_tms,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   251
		 map intro_tacsf (mk_disj_rls(length intr_tms)))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   252
	       handle SIMPLIFIER (msg,thm) => (print_thm thm; error msg);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   253
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   254
  (********)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   255
  val dummy = writeln "  Proving the elimination rule...";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   256
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   257
  (*Breaks down logical connectives in the monotonic function*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   258
  val basic_elim_tac =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   259
      REPEAT (SOMEGOAL (eresolve_tac (Ind_Syntax.elim_rls @ Su.free_SEs)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   260
		ORELSE' bound_hyp_subst_tac))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   261
      THEN prune_params_tac
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   262
	  (*Mutual recursion: collapse references to Part(D,h)*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   263
      THEN fold_tac part_rec_defs;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   264
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   265
  (*Elimination*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   266
  val elim = rule_by_tactic basic_elim_tac 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   267
		 (unfold RS Ind_Syntax.equals_CollectD)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   268
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   269
  (*Applies freeness of the given constructors, which *must* be unfolded by
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   270
      the given defs.  Cannot simply use the local con_defs because  
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   271
      con_defs=[] for inference systems. 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   272
    String s should have the form t:Si where Si is an inductive set*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   273
  fun mk_cases defs s = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   274
      rule_by_tactic (rewrite_goals_tac defs THEN 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   275
		      basic_elim_tac THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   276
		      fold_tac defs)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   277
	 (assume_read (theory_of_thm elim) s
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   278
	              (*Don't use thy1: it will be stale*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   279
	  RS  elim)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   280
      |> standard;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   281
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   282
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   283
  fun induction_rules raw_induct thy =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   284
   let
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   285
     val dummy = writeln "  Proving the induction rule...";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   286
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   287
     (*** Prove the main induction rule ***)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   288
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   289
     val pred_name = "P";            (*name for predicate variables*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   290
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   291
     (*Used to make induction rules;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   292
	ind_alist = [(rec_tm1,pred1),...] associates predicates with rec ops
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   293
	prem is a premise of an intr rule*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   294
     fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   295
		      (Const("op :",_)$t$X), iprems) =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   296
	  (case gen_assoc (op aconv) (ind_alist, X) of
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   297
	       Some pred => prem :: FOLogic.mk_Trueprop (pred $ t) :: iprems
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   298
	     | None => (*possibly membership in M(rec_tm), for M monotone*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   299
		 let fun mk_sb (rec_tm,pred) = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   300
			     (rec_tm, Ind_Syntax.Collect_const$rec_tm$pred)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   301
		 in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   302
       | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   303
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   304
     (*Make a premise of the induction rule.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   305
     fun induct_prem ind_alist intr =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   306
       let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   307
	   val iprems = foldr (add_induct_prem ind_alist)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   308
			      (Logic.strip_imp_prems intr,[])
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   309
	   val (t,X) = Ind_Syntax.rule_concl intr
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   310
	   val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   311
	   val concl = FOLogic.mk_Trueprop (pred $ t)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   312
       in list_all_free (quantfrees, Logic.list_implies (iprems,concl)) end
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   313
       handle Bind => error"Recursion term not found in conclusion";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   314
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   315
     (*Minimizes backtracking by delivering the correct premise to each goal.
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   316
       Intro rules with extra Vars in premises still cause some backtracking *)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   317
     fun ind_tac [] 0 = all_tac
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   318
       | ind_tac(prem::prems) i = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   319
	     DEPTH_SOLVE_1 (ares_tac [prem, refl] i) THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   320
	     ind_tac prems (i-1);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   321
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   322
     val pred = Free(pred_name, Ind_Syntax.iT --> FOLogic.oT);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   323
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   324
     val ind_prems = map (induct_prem (map (rpair pred) rec_tms)) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   325
			 intr_tms;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   326
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   327
     val dummy = if !Ind_Syntax.trace then 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   328
		 (writeln "ind_prems = ";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   329
		  seq (writeln o Sign.string_of_term sign1) ind_prems;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   330
		  writeln "raw_induct = "; print_thm raw_induct)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   331
	     else ();
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   332
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   333
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   334
     (*We use a MINIMAL simpset. Even FOL_ss contains too many simpules.  
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   335
       If the premises get simplified, then the proofs could fail.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   336
     val min_ss = empty_ss
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   337
	   setmksimps (map mk_eq o ZF_atomize o gen_all)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   338
	   setSolver  (fn prems => resolve_tac (triv_rls@prems) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   339
				   ORELSE' assume_tac
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   340
				   ORELSE' etac FalseE);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   341
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   342
     val quant_induct = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   343
	 prove_goalw_cterm part_rec_defs 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   344
	   (cterm_of sign1 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   345
	    (Logic.list_implies 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   346
	     (ind_prems, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   347
	      FOLogic.mk_Trueprop (Ind_Syntax.mk_all_imp(big_rec_tm,pred)))))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   348
	   (fn prems =>
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   349
	    [rtac (impI RS allI) 1,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   350
	     DETERM (etac raw_induct 1),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   351
	     (*Push Part inside Collect*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   352
	     full_simp_tac (min_ss addsimps [Part_Collect]) 1,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   353
	     (*This CollectE and disjE separates out the introduction rules*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   354
	     REPEAT (FIRSTGOAL (eresolve_tac [CollectE, disjE])),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   355
	     (*Now break down the individual cases.  No disjE here in case
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   356
	       some premise involves disjunction.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   357
	     REPEAT (FIRSTGOAL (eresolve_tac [CollectE, exE, conjE] 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   358
				ORELSE' hyp_subst_tac)),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   359
	     ind_tac (rev prems) (length prems) ]);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   360
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   361
     val dummy = if !Ind_Syntax.trace then 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   362
		 (writeln "quant_induct = "; print_thm quant_induct)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   363
	     else ();
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   364
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   365
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   366
     (*** Prove the simultaneous induction rule ***)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   367
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   368
     (*Make distinct predicates for each inductive set*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   369
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   370
     (*The components of the element type, several if it is a product*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   371
     val elem_type = CP.pseudo_type dom_sum;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   372
     val elem_factors = CP.factors elem_type;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   373
     val elem_frees = mk_frees "za" elem_factors;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   374
     val elem_tuple = CP.mk_tuple Pr.pair elem_type elem_frees;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   375
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   376
     (*Given a recursive set and its domain, return the "fsplit" predicate
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   377
       and a conclusion for the simultaneous induction rule.
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   378
       NOTE.  This will not work for mutually recursive predicates.  Previously
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   379
       a summand 'domt' was also an argument, but this required the domain of
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   380
       mutual recursion to invariably be a disjoint sum.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   381
     fun mk_predpair rec_tm = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   382
       let val rec_name = (#1 o dest_Const o head_of) rec_tm
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   383
	   val pfree = Free(pred_name ^ "_" ^ Sign.base_name rec_name,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   384
			    elem_factors ---> FOLogic.oT)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   385
	   val qconcl = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   386
	     foldr FOLogic.mk_all
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   387
	       (elem_frees, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   388
		FOLogic.imp $ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   389
		(Ind_Syntax.mem_const $ elem_tuple $ rec_tm)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   390
		      $ (list_comb (pfree, elem_frees)))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   391
       in  (CP.ap_split elem_type FOLogic.oT pfree, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   392
	    qconcl)  
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   393
       end;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   394
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   395
     val (preds,qconcls) = split_list (map mk_predpair rec_tms);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   396
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   397
     (*Used to form simultaneous induction lemma*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   398
     fun mk_rec_imp (rec_tm,pred) = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   399
	 FOLogic.imp $ (Ind_Syntax.mem_const $ Bound 0 $ rec_tm) $ 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   400
			  (pred $ Bound 0);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   401
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   402
     (*To instantiate the main induction rule*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   403
     val induct_concl = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   404
	 FOLogic.mk_Trueprop
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   405
	   (Ind_Syntax.mk_all_imp
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   406
	    (big_rec_tm,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   407
	     Abs("z", Ind_Syntax.iT, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   408
		 fold_bal (app FOLogic.conj) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   409
		 (ListPair.map mk_rec_imp (rec_tms, preds)))))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   410
     and mutual_induct_concl =
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   411
      FOLogic.mk_Trueprop(fold_bal (app FOLogic.conj) qconcls);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   412
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   413
     val dummy = if !Ind_Syntax.trace then 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   414
		 (writeln ("induct_concl = " ^
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   415
			   Sign.string_of_term sign1 induct_concl);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   416
		  writeln ("mutual_induct_concl = " ^
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   417
			   Sign.string_of_term sign1 mutual_induct_concl))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   418
	     else ();
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   419
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   420
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   421
     val lemma_tac = FIRST' [eresolve_tac [asm_rl, conjE, PartE, mp],
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   422
			     resolve_tac [allI, impI, conjI, Part_eqI],
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   423
			     dresolve_tac [spec, mp, Pr.fsplitD]];
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   424
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   425
     val need_mutual = length rec_names > 1;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   426
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   427
     val lemma = (*makes the link between the two induction rules*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   428
       if need_mutual then
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   429
	  (writeln "  Proving the mutual induction rule...";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   430
	   prove_goalw_cterm part_rec_defs 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   431
		 (cterm_of sign1 (Logic.mk_implies (induct_concl,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   432
						   mutual_induct_concl)))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   433
		 (fn prems =>
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   434
		  [cut_facts_tac prems 1, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   435
		   REPEAT (rewrite_goals_tac [Pr.split_eq] THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   436
			   lemma_tac 1)]))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   437
       else (writeln "  [ No mutual induction rule needed ]";
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   438
	     TrueI);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   439
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   440
     val dummy = if !Ind_Syntax.trace then 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   441
		 (writeln "lemma = "; print_thm lemma)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   442
	     else ();
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   443
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   444
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   445
     (*Mutual induction follows by freeness of Inl/Inr.*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   446
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   447
     (*Simplification largely reduces the mutual induction rule to the 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   448
       standard rule*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   449
     val mut_ss = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   450
	 min_ss addsimps [Su.distinct, Su.distinct', Su.inl_iff, Su.inr_iff];
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   451
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   452
     val all_defs = con_defs @ part_rec_defs;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   453
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   454
     (*Removes Collects caused by M-operators in the intro rules.  It is very
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   455
       hard to simplify
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   456
	 list({v: tf. (v : t --> P_t(v)) & (v : f --> P_f(v))}) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   457
       where t==Part(tf,Inl) and f==Part(tf,Inr) to  list({v: tf. P_t(v)}).
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   458
       Instead the following rules extract the relevant conjunct.
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   459
     *)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   460
     val cmonos = [subset_refl RS Collect_mono] RL monos
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   461
		   RLN (2,[rev_subsetD]);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   462
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   463
     (*Minimizes backtracking by delivering the correct premise to each goal*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   464
     fun mutual_ind_tac [] 0 = all_tac
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   465
       | mutual_ind_tac(prem::prems) i = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   466
	   DETERM
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   467
	    (SELECT_GOAL 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   468
	       (
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   469
		(*Simplify the assumptions and goal by unfolding Part and
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   470
		  using freeness of the Sum constructors; proves all but one
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   471
		  conjunct by contradiction*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   472
		rewrite_goals_tac all_defs  THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   473
		simp_tac (mut_ss addsimps [Part_iff]) 1  THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   474
		IF_UNSOLVED (*simp_tac may have finished it off!*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   475
		  ((*simplify assumptions*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   476
		   (*some risk of excessive simplification here -- might have
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   477
		     to identify the bare minimum set of rewrites*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   478
		   full_simp_tac 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   479
		      (mut_ss addsimps conj_simps @ imp_simps @ quant_simps) 1
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   480
		   THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   481
		   (*unpackage and use "prem" in the corresponding place*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   482
		   REPEAT (rtac impI 1)  THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   483
		   rtac (rewrite_rule all_defs prem) 1  THEN
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   484
		   (*prem must not be REPEATed below: could loop!*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   485
		   DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   486
					   eresolve_tac (conjE::mp::cmonos))))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   487
	       ) i)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   488
	    THEN mutual_ind_tac prems (i-1);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   489
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   490
     val mutual_induct_fsplit = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   491
       if need_mutual then
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   492
	 prove_goalw_cterm []
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   493
	       (cterm_of sign1
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   494
		(Logic.list_implies 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   495
		   (map (induct_prem (rec_tms~~preds)) intr_tms,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   496
		    mutual_induct_concl)))
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   497
	       (fn prems =>
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   498
		[rtac (quant_induct RS lemma) 1,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   499
		 mutual_ind_tac (rev prems) (length prems)])
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   500
       else TrueI;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   501
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   502
     (** Uncurrying the predicate in the ordinary induction rule **)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   503
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   504
     (*instantiate the variable to a tuple, if it is non-trivial, in order to
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   505
       allow the predicate to be "opened up".
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   506
       The name "x.1" comes from the "RS spec" !*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   507
     val inst = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   508
	 case elem_frees of [_] => I
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   509
	    | _ => instantiate ([], [(cterm_of sign1 (Var(("x",1), Ind_Syntax.iT)), 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   510
				      cterm_of sign1 elem_tuple)]);
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   511
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   512
     (*strip quantifier and the implication*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   513
     val induct0 = inst (quant_induct RS spec RSN (2,rev_mp));
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   514
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   515
     val Const ("Trueprop", _) $ (pred_var $ _) = concl_of induct0
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   516
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   517
     val induct = CP.split_rule_var(pred_var, elem_type-->FOLogic.oT, induct0) 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   518
		  |> standard
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   519
     and mutual_induct = CP.remove_split mutual_induct_fsplit
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   520
    in
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   521
      (thy
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   522
	|> PureThy.add_tthms 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   523
	    [(("induct", Attribute.tthm_of induct), []),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   524
	     (("mutual_induct", Attribute.tthm_of mutual_induct), [])],
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   525
       induct, mutual_induct)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   526
    end;  (*of induction_rules*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   527
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   528
  val raw_induct = standard ([big_rec_def, bnd_mono] MRS Fp.induct)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   529
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   530
  val (thy2, induct, mutual_induct) = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   531
      if #1 (dest_Const Fp.oper) = "lfp" then induction_rules raw_induct thy1
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   532
      else (thy1, raw_induct, TrueI)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   533
  and defs = big_rec_def :: part_rec_defs
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   534
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   535
 in
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   536
   (thy2
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   537
	 |> PureThy.add_tthms
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   538
	      [(("bnd_mono", Attribute.tthm_of bnd_mono), []),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   539
	       (("dom_subset", Attribute.tthm_of dom_subset), []),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   540
	       (("elim", Attribute.tthm_of elim), [])]
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   541
	 |> PureThy.add_tthmss
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   542
	       [(("defs", Attribute.tthms_of defs), []),
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   543
		(("intrs", Attribute.tthms_of intrs), [])]
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   544
         |> Theory.parent_path,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   545
    {defs = defs,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   546
     bnd_mono = bnd_mono,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   547
     dom_subset = dom_subset,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   548
     intrs = intrs,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   549
     elim = elim,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   550
     mk_cases = mk_cases,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   551
     induct = induct,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   552
     mutual_induct = mutual_induct})
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   553
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   554
 end;
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   555
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   556
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   557
(*external version, accepting strings*)
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   558
fun add_inductive (srec_tms, sdom_sum, sintrs, monos,
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   559
		     con_defs, type_intrs, type_elims) thy = 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   560
  let val rec_tms = map (readtm (sign_of thy) Ind_Syntax.iT) srec_tms
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   561
      and dom_sum = readtm (sign_of thy) Ind_Syntax.iT sdom_sum
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   562
      and intr_tms = map (readtm (sign_of thy) propT) sintrs
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   563
  in  
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   564
    add_inductive_i true (rec_tms, dom_sum, intr_tms, 
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   565
			  monos, con_defs, type_intrs, type_elims) thy
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   566
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   567
  end
7d457fc538e7 revised inductive definition package
paulson
parents:
diff changeset
   568
end;