src/Pure/drule.ML
author paulson
Sat Feb 07 14:39:35 1998 +0100 (1998-02-07)
changeset 4610 b1322be47244
parent 4588 42bf47c1de1f
child 4679 24917efb31b5
permissions -rw-r--r--
Tidying; rotate_prems; moved freeze_thaw from tactic.ML
wenzelm@252
     1
(*  Title:      Pure/drule.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@252
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
wenzelm@3766
     6
Derived rules and other operations on theorems.
clasohm@0
     7
*)
clasohm@0
     8
lcp@11
     9
infix 0 RS RSN RL RLN MRS MRL COMP;
clasohm@0
    10
clasohm@0
    11
signature DRULE =
wenzelm@3766
    12
sig
wenzelm@4285
    13
  val dest_implies      : cterm -> cterm * cterm
wenzelm@4285
    14
  val skip_flexpairs	: cterm -> cterm
wenzelm@4285
    15
  val strip_imp_prems	: cterm -> cterm list
clasohm@1460
    16
  val cprems_of		: thm -> cterm list
wenzelm@4285
    17
  val read_insts	:
wenzelm@4285
    18
          Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
wenzelm@4285
    19
                  -> (indexname -> typ option) * (indexname -> sort option)
wenzelm@4285
    20
                  -> string list -> (string*string)list
wenzelm@4285
    21
                  -> (indexname*ctyp)list * (cterm*cterm)list
wenzelm@4285
    22
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
clasohm@1460
    23
  val forall_intr_list	: cterm list -> thm -> thm
clasohm@1460
    24
  val forall_intr_frees	: thm -> thm
clasohm@1460
    25
  val forall_intr_vars	: thm -> thm
clasohm@1460
    26
  val forall_elim_list	: cterm list -> thm -> thm
clasohm@1460
    27
  val forall_elim_var	: int -> thm -> thm
clasohm@1460
    28
  val forall_elim_vars	: int -> thm -> thm
paulson@4610
    29
  val freeze_thaw	: thm -> thm * (thm -> thm)
clasohm@1460
    30
  val implies_elim_list	: thm -> thm list -> thm
clasohm@1460
    31
  val implies_intr_list	: cterm list -> thm -> thm
wenzelm@4285
    32
  val zero_var_indexes	: thm -> thm
wenzelm@4285
    33
  val standard		: thm -> thm
paulson@4610
    34
  val rotate_prems      : int -> thm -> thm
wenzelm@4285
    35
  val assume_ax		: theory -> string -> thm
wenzelm@4285
    36
  val RSN		: thm * (int * thm) -> thm
wenzelm@4285
    37
  val RS		: thm * thm -> thm
wenzelm@4285
    38
  val RLN		: thm list * (int * thm list) -> thm list
wenzelm@4285
    39
  val RL		: thm list * thm list -> thm list
wenzelm@4285
    40
  val MRS		: thm list * thm -> thm
clasohm@1460
    41
  val MRL		: thm list list * thm list -> thm list
wenzelm@4285
    42
  val compose		: thm * int * thm -> thm list
wenzelm@4285
    43
  val COMP		: thm * thm -> thm
clasohm@0
    44
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
wenzelm@4285
    45
  val read_instantiate	: (string*string)list -> thm -> thm
wenzelm@4285
    46
  val cterm_instantiate	: (cterm*cterm)list -> thm -> thm
wenzelm@4285
    47
  val weak_eq_thm	: thm * thm -> bool
wenzelm@4285
    48
  val eq_thm_sg		: thm * thm -> bool
wenzelm@4285
    49
  val size_of_thm	: thm -> int
clasohm@1460
    50
  val reflexive_thm	: thm
wenzelm@4285
    51
  val symmetric_thm	: thm
wenzelm@4285
    52
  val transitive_thm	: thm
paulson@2004
    53
  val refl_implies      : thm
wenzelm@3575
    54
  val rewrite_rule_aux	: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
wenzelm@3555
    55
  val rewrite_thm	: bool * bool -> (meta_simpset -> thm -> thm option)
wenzelm@3555
    56
	-> meta_simpset -> thm -> thm
wenzelm@4285
    57
  val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
wenzelm@4285
    58
  val rewrite_goal_rule	: bool * bool -> (meta_simpset -> thm -> thm option)
wenzelm@4285
    59
        -> meta_simpset -> int -> thm -> thm
wenzelm@4285
    60
wenzelm@4285
    61
  val equal_abs_elim	: cterm  -> thm -> thm
wenzelm@4285
    62
  val equal_abs_elim_list: cterm list -> thm -> thm
wenzelm@4285
    63
  val flexpair_abs_elim_list: cterm list -> thm -> thm
wenzelm@4285
    64
  val asm_rl		: thm
wenzelm@4285
    65
  val cut_rl		: thm
wenzelm@4285
    66
  val revcut_rl		: thm
wenzelm@4285
    67
  val thin_rl		: thm
wenzelm@4285
    68
  val triv_forall_equality: thm
nipkow@1756
    69
  val swap_prems_rl     : thm
wenzelm@4285
    70
  val equal_intr_rule   : thm
wenzelm@4285
    71
  val instantiate': ctyp option list -> cterm option list -> thm -> thm
wenzelm@3766
    72
end;
clasohm@0
    73
paulson@1499
    74
structure Drule : DRULE =
clasohm@0
    75
struct
clasohm@0
    76
wenzelm@3991
    77
lcp@708
    78
(** some cterm->cterm operations: much faster than calling cterm_of! **)
lcp@708
    79
paulson@2004
    80
(** SAME NAMES as in structure Logic: use compound identifiers! **)
paulson@2004
    81
clasohm@1703
    82
(*dest_implies for cterms. Note T=prop below*)
paulson@2004
    83
fun dest_implies ct =
paulson@2004
    84
    case term_of ct of 
paulson@2004
    85
	(Const("==>", _) $ _ $ _) => 
paulson@2004
    86
	    let val (ct1,ct2) = dest_comb ct
paulson@2004
    87
	    in  (#2 (dest_comb ct1), ct2)  end	     
paulson@2004
    88
      | _ => raise TERM ("dest_implies", [term_of ct]) ;
clasohm@1703
    89
clasohm@1703
    90
lcp@708
    91
(*Discard flexflex pairs; return a cterm*)
paulson@2004
    92
fun skip_flexpairs ct =
lcp@708
    93
    case term_of ct of
clasohm@1460
    94
	(Const("==>", _) $ (Const("=?=",_)$_$_) $ _) =>
paulson@2004
    95
	    skip_flexpairs (#2 (dest_implies ct))
lcp@708
    96
      | _ => ct;
lcp@708
    97
lcp@708
    98
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
paulson@2004
    99
fun strip_imp_prems ct =
paulson@2004
   100
    let val (cA,cB) = dest_implies ct
paulson@2004
   101
    in  cA :: strip_imp_prems cB  end
lcp@708
   102
    handle TERM _ => [];
lcp@708
   103
paulson@2004
   104
(* A1==>...An==>B  goes to B, where B is not an implication *)
paulson@2004
   105
fun strip_imp_concl ct =
paulson@2004
   106
    case term_of ct of (Const("==>", _) $ _ $ _) => 
paulson@2004
   107
	strip_imp_concl (#2 (dest_comb ct))
paulson@2004
   108
  | _ => ct;
paulson@2004
   109
lcp@708
   110
(*The premises of a theorem, as a cterm list*)
paulson@2004
   111
val cprems_of = strip_imp_prems o skip_flexpairs o cprop_of;
lcp@708
   112
lcp@708
   113
lcp@229
   114
(** reading of instantiations **)
lcp@229
   115
lcp@229
   116
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
lcp@229
   117
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
lcp@229
   118
lcp@229
   119
fun absent ixn =
lcp@229
   120
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
lcp@229
   121
lcp@229
   122
fun inst_failure ixn =
lcp@229
   123
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
lcp@229
   124
nipkow@4281
   125
fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
nipkow@4281
   126
let val {tsig,...} = Sign.rep_sg sign
nipkow@4281
   127
    fun split([],tvs,vs) = (tvs,vs)
nipkow@4281
   128
      | split((sv,st)::l,tvs,vs) = (case explode sv of
nipkow@4281
   129
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
nipkow@4281
   130
                | cs => split(l,tvs,(indexname cs,st)::vs));
nipkow@4281
   131
    val (tvs,vs) = split(insts,[],[]);
nipkow@4281
   132
    fun readT((a,i),st) =
nipkow@4281
   133
        let val ixn = ("'" ^ a,i);
nipkow@4281
   134
            val S = case rsorts ixn of Some S => S | None => absent ixn;
nipkow@4281
   135
            val T = Sign.read_typ (sign,sorts) st;
nipkow@4281
   136
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
nipkow@4281
   137
           else inst_failure ixn
nipkow@4281
   138
        end
nipkow@4281
   139
    val tye = map readT tvs;
nipkow@4281
   140
    fun mkty(ixn,st) = (case rtypes ixn of
nipkow@4281
   141
                          Some T => (ixn,(st,typ_subst_TVars tye T))
nipkow@4281
   142
                        | None => absent ixn);
nipkow@4281
   143
    val ixnsTs = map mkty vs;
nipkow@4281
   144
    val ixns = map fst ixnsTs
nipkow@4281
   145
    and sTs  = map snd ixnsTs
nipkow@4281
   146
    val (cts,tye2) = read_def_cterms(sign,types,sorts) used false sTs;
nipkow@4281
   147
    fun mkcVar(ixn,T) =
nipkow@4281
   148
        let val U = typ_subst_TVars tye2 T
nipkow@4281
   149
        in cterm_of sign (Var(ixn,U)) end
nipkow@4281
   150
    val ixnTs = ListPair.zip(ixns, map snd sTs)
nipkow@4281
   151
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) (tye2 @ tye),
nipkow@4281
   152
    ListPair.zip(map mkcVar ixnTs,cts))
nipkow@4281
   153
end;
lcp@229
   154
lcp@229
   155
wenzelm@252
   156
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   157
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   158
     type variables) when reading another term.
clasohm@0
   159
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   160
***)
clasohm@0
   161
clasohm@0
   162
fun types_sorts thm =
clasohm@0
   163
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   164
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   165
        val vars = map dest_Var (term_vars big);
wenzelm@252
   166
        val frees = map dest_Free (term_frees big);
wenzelm@252
   167
        val tvars = term_tvars big;
wenzelm@252
   168
        val tfrees = term_tfrees big;
wenzelm@252
   169
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   170
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   171
    in (typ,sort) end;
clasohm@0
   172
clasohm@0
   173
(** Standardization of rules **)
clasohm@0
   174
clasohm@0
   175
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   176
fun forall_intr_list [] th = th
clasohm@0
   177
  | forall_intr_list (y::ys) th =
wenzelm@252
   178
        let val gth = forall_intr_list ys th
wenzelm@252
   179
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   180
clasohm@0
   181
(*Generalization over all suitable Free variables*)
clasohm@0
   182
fun forall_intr_frees th =
clasohm@0
   183
    let val {prop,sign,...} = rep_thm th
clasohm@0
   184
    in  forall_intr_list
wenzelm@4440
   185
         (map (cterm_of sign) (sort (make_ord atless) (term_frees prop)))
clasohm@0
   186
         th
clasohm@0
   187
    end;
clasohm@0
   188
clasohm@0
   189
(*Replace outermost quantified variable by Var of given index.
clasohm@0
   190
    Could clash with Vars already present.*)
wenzelm@252
   191
fun forall_elim_var i th =
clasohm@0
   192
    let val {prop,sign,...} = rep_thm th
clasohm@0
   193
    in case prop of
wenzelm@252
   194
          Const("all",_) $ Abs(a,T,_) =>
wenzelm@252
   195
              forall_elim (cterm_of sign (Var((a,i), T)))  th
wenzelm@252
   196
        | _ => raise THM("forall_elim_var", i, [th])
clasohm@0
   197
    end;
clasohm@0
   198
clasohm@0
   199
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@252
   200
fun forall_elim_vars i th =
clasohm@0
   201
    forall_elim_vars i (forall_elim_var i th)
wenzelm@252
   202
        handle THM _ => th;
clasohm@0
   203
clasohm@0
   204
(*Specialization over a list of cterms*)
clasohm@0
   205
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   206
clasohm@0
   207
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   208
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   209
clasohm@0
   210
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   211
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   212
clasohm@0
   213
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   214
fun zero_var_indexes th =
clasohm@0
   215
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   216
        val vars = term_vars prop
clasohm@0
   217
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   218
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   219
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
paulson@2266
   220
        val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
paulson@2266
   221
	             (inrs, nms')
wenzelm@252
   222
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   223
        fun varpairs([],[]) = []
wenzelm@252
   224
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   225
                let val T' = typ_subst_TVars tye T
wenzelm@252
   226
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   227
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   228
                end
wenzelm@252
   229
          | varpairs _ = raise TERM("varpairs", []);
clasohm@0
   230
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   231
clasohm@0
   232
clasohm@0
   233
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   234
    all generality expressed by Vars having index 0.*)
clasohm@0
   235
fun standard th =
wenzelm@1218
   236
  let val {maxidx,...} = rep_thm th
wenzelm@1237
   237
  in
wenzelm@1218
   238
    th |> implies_intr_hyps
paulson@1412
   239
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
wenzelm@1439
   240
       |> Thm.strip_shyps |> Thm.implies_intr_shyps
paulson@1412
   241
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
wenzelm@1218
   242
  end;
wenzelm@1218
   243
clasohm@0
   244
paulson@4610
   245
(*Convert all Vars in a theorem to Frees.  Also return a function for 
paulson@4610
   246
  reversing that operation.  DOES NOT WORK FOR TYPE VARIABLES.
paulson@4610
   247
  Similar code in type/freeze_thaw*)
paulson@4610
   248
fun freeze_thaw th =
paulson@4610
   249
  let val fth = freezeT th
paulson@4610
   250
      val {prop,sign,...} = rep_thm fth
paulson@4610
   251
      val used = add_term_names (prop, [])
paulson@4610
   252
      and vars = term_vars prop
paulson@4610
   253
      fun newName (Var(ix,_), (pairs,used)) = 
paulson@4610
   254
	    let val v = variant used (string_of_indexname ix)
paulson@4610
   255
	    in  ((ix,v)::pairs, v::used)  end;
paulson@4610
   256
      val (alist, _) = foldr newName (vars, ([], used))
paulson@4610
   257
      fun mk_inst (Var(v,T)) = 
paulson@4610
   258
	  (cterm_of sign (Var(v,T)),
paulson@4610
   259
	   cterm_of sign (Free(the (assoc(alist,v)), T)))
paulson@4610
   260
      val insts = map mk_inst vars
paulson@4610
   261
      fun thaw th' = 
paulson@4610
   262
	  th' |> forall_intr_list (map #2 insts)
paulson@4610
   263
	      |> forall_elim_list (map #1 insts)
paulson@4610
   264
  in  (instantiate ([],insts) fth, thaw)  end;
paulson@4610
   265
paulson@4610
   266
paulson@4610
   267
(*Rotates a rule's premises to the left by k.  Does nothing if k=0 or
paulson@4610
   268
  if k equals the number of premises.  Useful, for instance, with etac.
paulson@4610
   269
  Similar to tactic/defer_tac*)
paulson@4610
   270
fun rotate_prems k rl = 
paulson@4610
   271
    let val (rl',thaw) = freeze_thaw rl
paulson@4610
   272
	val hyps = strip_imp_prems (adjust_maxidx (cprop_of rl'))
paulson@4610
   273
	val hyps' = List.drop(hyps, k)
paulson@4610
   274
    in  implies_elim_list rl' (map assume hyps)
paulson@4610
   275
        |> implies_intr_list (hyps' @ List.take(hyps, k))
paulson@4610
   276
        |> thaw |> varifyT
paulson@4610
   277
    end;
paulson@4610
   278
paulson@4610
   279
wenzelm@252
   280
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   281
  Generalizes over Free variables,
clasohm@0
   282
  creates the assumption, and then strips quantifiers.
clasohm@0
   283
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   284
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   285
fun assume_ax thy sP =
clasohm@0
   286
    let val sign = sign_of thy
paulson@4610
   287
        val prop = Logic.close_form (term_of (read_cterm sign (sP, propT)))
lcp@229
   288
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   289
wenzelm@252
   290
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   291
fun tha RSN (i,thb) =
wenzelm@4270
   292
  case Seq.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   293
      ([th],_) => th
clasohm@0
   294
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   295
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   296
clasohm@0
   297
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   298
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   299
clasohm@0
   300
(*For joining lists of rules*)
wenzelm@252
   301
fun thas RLN (i,thbs) =
clasohm@0
   302
  let val resolve = biresolution false (map (pair false) thas) i
wenzelm@4270
   303
      fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
paulson@2672
   304
  in  List.concat (map resb thbs)  end;
clasohm@0
   305
clasohm@0
   306
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   307
lcp@11
   308
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   309
  makes proof trees*)
wenzelm@252
   310
fun rls MRS bottom_rl =
lcp@11
   311
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   312
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   313
  in  rs_aux 1 rls  end;
lcp@11
   314
lcp@11
   315
(*As above, but for rule lists*)
wenzelm@252
   316
fun rlss MRL bottom_rls =
lcp@11
   317
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   318
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   319
  in  rs_aux 1 rlss  end;
lcp@11
   320
wenzelm@252
   321
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   322
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   323
  ALWAYS deletes premise i *)
wenzelm@252
   324
fun compose(tha,i,thb) =
wenzelm@4270
   325
    Seq.list_of (bicompose false (false,tha,0) i thb);
clasohm@0
   326
clasohm@0
   327
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   328
fun tha COMP thb =
clasohm@0
   329
    case compose(tha,1,thb) of
wenzelm@252
   330
        [th] => th
clasohm@0
   331
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   332
clasohm@0
   333
(*Instantiate theorem th, reading instantiations under signature sg*)
clasohm@0
   334
fun read_instantiate_sg sg sinsts th =
clasohm@0
   335
    let val ts = types_sorts th;
nipkow@952
   336
        val used = add_term_tvarnames(#prop(rep_thm th),[]);
nipkow@952
   337
    in  instantiate (read_insts sg ts ts used sinsts) th  end;
clasohm@0
   338
clasohm@0
   339
(*Instantiate theorem th, reading instantiations under theory of th*)
clasohm@0
   340
fun read_instantiate sinsts th =
clasohm@0
   341
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
clasohm@0
   342
clasohm@0
   343
clasohm@0
   344
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
clasohm@0
   345
  Instantiates distinct Vars by terms, inferring type instantiations. *)
clasohm@0
   346
local
nipkow@1435
   347
  fun add_types ((ct,cu), (sign,tye,maxidx)) =
paulson@2152
   348
    let val {sign=signt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
paulson@2152
   349
        and {sign=signu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
paulson@2152
   350
        val maxi = Int.max(maxidx, Int.max(maxt, maxu));
clasohm@0
   351
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
nipkow@1435
   352
        val (tye',maxi') = Type.unify (#tsig(Sign.rep_sg sign')) maxi tye (T,U)
wenzelm@252
   353
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
nipkow@1435
   354
    in  (sign', tye', maxi')  end;
clasohm@0
   355
in
wenzelm@252
   356
fun cterm_instantiate ctpairs0 th =
nipkow@1435
   357
  let val (sign,tye,_) = foldr add_types (ctpairs0, (#sign(rep_thm th),[],0))
clasohm@0
   358
      val tsig = #tsig(Sign.rep_sg sign);
clasohm@0
   359
      fun instT(ct,cu) = let val inst = subst_TVars tye
wenzelm@252
   360
                         in (cterm_fun inst ct, cterm_fun inst cu) end
lcp@229
   361
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
clasohm@0
   362
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
wenzelm@252
   363
  handle TERM _ =>
clasohm@0
   364
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
wenzelm@4057
   365
       | TYPE (msg, _, _) => raise THM("cterm_instantiate: " ^ msg, 0, [th])
clasohm@0
   366
end;
clasohm@0
   367
clasohm@0
   368
wenzelm@4016
   369
(** theorem equality **)
clasohm@0
   370
clasohm@0
   371
(*Do the two theorems have the same signature?*)
wenzelm@252
   372
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
clasohm@0
   373
clasohm@0
   374
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   375
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   376
clasohm@0
   377
lcp@1194
   378
(** Mark Staples's weaker version of eq_thm: ignores variable renaming and
lcp@1194
   379
    (some) type variable renaming **)
lcp@1194
   380
lcp@1194
   381
 (* Can't use term_vars, because it sorts the resulting list of variable names.
lcp@1194
   382
    We instead need the unique list noramlised by the order of appearance
lcp@1194
   383
    in the term. *)
lcp@1194
   384
fun term_vars' (t as Var(v,T)) = [t]
lcp@1194
   385
  | term_vars' (Abs(_,_,b)) = term_vars' b
lcp@1194
   386
  | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
lcp@1194
   387
  | term_vars' _ = [];
lcp@1194
   388
lcp@1194
   389
fun forall_intr_vars th =
lcp@1194
   390
  let val {prop,sign,...} = rep_thm th;
lcp@1194
   391
      val vars = distinct (term_vars' prop);
lcp@1194
   392
  in forall_intr_list (map (cterm_of sign) vars) th end;
lcp@1194
   393
wenzelm@1237
   394
fun weak_eq_thm (tha,thb) =
lcp@1194
   395
    eq_thm(forall_intr_vars (freezeT tha), forall_intr_vars (freezeT thb));
lcp@1194
   396
lcp@1194
   397
lcp@1194
   398
clasohm@0
   399
(*** Meta-Rewriting Rules ***)
clasohm@0
   400
paulson@4610
   401
val proto_sign = sign_of ProtoPure.thy;
paulson@4610
   402
paulson@4610
   403
fun read_prop s = read_cterm proto_sign (s, propT);
paulson@4610
   404
wenzelm@4016
   405
fun store_thm name thm = PureThy.smart_store_thm (name, standard thm);
wenzelm@4016
   406
clasohm@0
   407
val reflexive_thm =
paulson@4610
   408
  let val cx = cterm_of proto_sign (Var(("x",0),TVar(("'a",0),logicS)))
wenzelm@4016
   409
  in store_thm "reflexive" (Thm.reflexive cx) end;
clasohm@0
   410
clasohm@0
   411
val symmetric_thm =
paulson@4610
   412
  let val xy = read_prop "x::'a::logic == y"
paulson@4610
   413
  in store_thm "symmetric" 
paulson@4610
   414
      (Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy)))
paulson@4610
   415
   end;
clasohm@0
   416
clasohm@0
   417
val transitive_thm =
paulson@4610
   418
  let val xy = read_prop "x::'a::logic == y"
paulson@4610
   419
      val yz = read_prop "y::'a::logic == z"
clasohm@0
   420
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
paulson@4610
   421
  in store_thm "transitive" (Thm.implies_intr yz (Thm.transitive xythm yzthm))
paulson@4610
   422
  end;
clasohm@0
   423
lcp@229
   424
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   425
paulson@4610
   426
val refl_implies = reflexive (cterm_of proto_sign implies);
clasohm@0
   427
clasohm@0
   428
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   429
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   430
fun goals_conv pred cv =
lcp@229
   431
  let fun gconv i ct =
paulson@2004
   432
        let val (A,B) = dest_implies ct
lcp@229
   433
            val (thA,j) = case term_of A of
lcp@229
   434
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   435
                | _ => (if pred i then cv A else reflexive A, i+1)
paulson@2004
   436
        in  combination (combination refl_implies thA) (gconv j B) end
lcp@229
   437
        handle TERM _ => reflexive ct
clasohm@0
   438
  in gconv 1 end;
clasohm@0
   439
clasohm@0
   440
(*Use a conversion to transform a theorem*)
lcp@229
   441
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   442
clasohm@0
   443
(*rewriting conversion*)
lcp@229
   444
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   445
clasohm@0
   446
(*Rewrite a theorem*)
wenzelm@3575
   447
fun rewrite_rule_aux _ []   th = th
wenzelm@3575
   448
  | rewrite_rule_aux prover thms th =
wenzelm@3575
   449
      fconv_rule (rew_conv (true,false) prover (Thm.mss_of thms)) th;
clasohm@0
   450
wenzelm@3555
   451
fun rewrite_thm mode prover mss = fconv_rule (rew_conv mode prover mss);
wenzelm@3555
   452
clasohm@0
   453
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
wenzelm@3575
   454
fun rewrite_goals_rule_aux _ []   th = th
wenzelm@3575
   455
  | rewrite_goals_rule_aux prover thms th =
wenzelm@3575
   456
      fconv_rule (goals_conv (K true) (rew_conv (true, true) prover
wenzelm@3575
   457
        (Thm.mss_of thms))) th;
clasohm@0
   458
clasohm@0
   459
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   460
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   461
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   462
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   463
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   464
clasohm@0
   465
clasohm@0
   466
(** Derived rules mainly for METAHYPS **)
clasohm@0
   467
clasohm@0
   468
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
clasohm@0
   469
fun equal_abs_elim ca eqth =
lcp@229
   470
  let val {sign=signa, t=a, ...} = rep_cterm ca
clasohm@0
   471
      and combth = combination eqth (reflexive ca)
clasohm@0
   472
      val {sign,prop,...} = rep_thm eqth
clasohm@0
   473
      val (abst,absu) = Logic.dest_equals prop
lcp@229
   474
      val cterm = cterm_of (Sign.merge (sign,signa))
clasohm@0
   475
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
clasohm@0
   476
           (transitive combth (beta_conversion (cterm (absu$a))))
clasohm@0
   477
  end
clasohm@0
   478
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
clasohm@0
   479
clasohm@0
   480
(*Calling equal_abs_elim with multiple terms*)
clasohm@0
   481
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
clasohm@0
   482
clasohm@0
   483
local
clasohm@0
   484
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
clasohm@0
   485
  fun err th = raise THM("flexpair_inst: ", 0, [th])
clasohm@0
   486
  fun flexpair_inst def th =
clasohm@0
   487
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
wenzelm@252
   488
        val cterm = cterm_of sign
wenzelm@252
   489
        fun cvar a = cterm(Var((a,0),alpha))
wenzelm@252
   490
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
wenzelm@252
   491
                   def
clasohm@0
   492
    in  equal_elim def' th
clasohm@0
   493
    end
clasohm@0
   494
    handle THM _ => err th | bind => err th
clasohm@0
   495
in
wenzelm@3991
   496
val flexpair_intr = flexpair_inst (symmetric ProtoPure.flexpair_def)
wenzelm@3991
   497
and flexpair_elim = flexpair_inst ProtoPure.flexpair_def
clasohm@0
   498
end;
clasohm@0
   499
clasohm@0
   500
(*Version for flexflex pairs -- this supports lifting.*)
wenzelm@252
   501
fun flexpair_abs_elim_list cts =
clasohm@0
   502
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
clasohm@0
   503
clasohm@0
   504
clasohm@0
   505
(*** Some useful meta-theorems ***)
clasohm@0
   506
clasohm@0
   507
(*The rule V/V, obtains assumption solving for eresolve_tac*)
wenzelm@4016
   508
val asm_rl =
paulson@4610
   509
  store_thm "asm_rl" (trivial(read_prop "PROP ?psi"));
clasohm@0
   510
clasohm@0
   511
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@4016
   512
val cut_rl =
wenzelm@4016
   513
  store_thm "cut_rl"
paulson@4610
   514
    (trivial(read_prop "PROP ?psi ==> PROP ?theta"));
clasohm@0
   515
wenzelm@252
   516
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   517
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   518
val revcut_rl =
paulson@4610
   519
  let val V = read_prop "PROP V"
paulson@4610
   520
      and VW = read_prop "PROP V ==> PROP W";
wenzelm@4016
   521
  in
wenzelm@4016
   522
    store_thm "revcut_rl"
wenzelm@4016
   523
      (implies_intr V (implies_intr VW (implies_elim (assume VW) (assume V))))
clasohm@0
   524
  end;
clasohm@0
   525
lcp@668
   526
(*for deleting an unwanted assumption*)
lcp@668
   527
val thin_rl =
paulson@4610
   528
  let val V = read_prop "PROP V"
paulson@4610
   529
      and W = read_prop "PROP W";
wenzelm@4016
   530
  in  store_thm "thin_rl" (implies_intr V (implies_intr W (assume W)))
lcp@668
   531
  end;
lcp@668
   532
clasohm@0
   533
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   534
val triv_forall_equality =
paulson@4610
   535
  let val V  = read_prop "PROP V"
paulson@4610
   536
      and QV = read_prop "!!x::'a. PROP V"
paulson@4610
   537
      and x  = read_cterm proto_sign ("x", TFree("'a",logicS));
wenzelm@4016
   538
  in
wenzelm@4016
   539
    store_thm "triv_forall_equality"
wenzelm@4016
   540
      (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@4016
   541
        (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   542
  end;
clasohm@0
   543
nipkow@1756
   544
(* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
nipkow@1756
   545
   (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
nipkow@1756
   546
   `thm COMP swap_prems_rl' swaps the first two premises of `thm'
nipkow@1756
   547
*)
nipkow@1756
   548
val swap_prems_rl =
paulson@4610
   549
  let val cmajor = read_prop "PROP PhiA ==> PROP PhiB ==> PROP Psi";
nipkow@1756
   550
      val major = assume cmajor;
paulson@4610
   551
      val cminor1 = read_prop "PROP PhiA";
nipkow@1756
   552
      val minor1 = assume cminor1;
paulson@4610
   553
      val cminor2 = read_prop "PROP PhiB";
nipkow@1756
   554
      val minor2 = assume cminor2;
wenzelm@4016
   555
  in store_thm "swap_prems_rl"
nipkow@1756
   556
       (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
nipkow@1756
   557
         (implies_elim (implies_elim major minor1) minor2))))
nipkow@1756
   558
  end;
nipkow@1756
   559
nipkow@3653
   560
(* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
nipkow@3653
   561
   ==> PROP ?phi == PROP ?psi
paulson@4610
   562
   Introduction rule for == as a meta-theorem.  
nipkow@3653
   563
*)
nipkow@3653
   564
val equal_intr_rule =
paulson@4610
   565
  let val PQ = read_prop "PROP phi ==> PROP psi"
paulson@4610
   566
      and QP = read_prop "PROP psi ==> PROP phi"
wenzelm@4016
   567
  in
wenzelm@4016
   568
    store_thm "equal_intr_rule"
wenzelm@4016
   569
      (implies_intr PQ (implies_intr QP (equal_intr (assume PQ) (assume QP))))
nipkow@3653
   570
  end;
nipkow@3653
   571
wenzelm@4285
   572
wenzelm@4285
   573
wenzelm@4285
   574
(** instantiate' rule **)
wenzelm@4285
   575
wenzelm@4285
   576
(* collect vars *)
wenzelm@4285
   577
wenzelm@4285
   578
val add_tvarsT = foldl_atyps (fn (vs, TVar v) => v ins vs | (vs, _) => vs);
wenzelm@4285
   579
val add_tvars = foldl_types add_tvarsT;
wenzelm@4285
   580
val add_vars = foldl_aterms (fn (vs, Var v) => v ins vs | (vs, _) => vs);
wenzelm@4285
   581
wenzelm@4285
   582
fun tvars_of thm = rev (add_tvars ([], #prop (Thm.rep_thm thm)));
wenzelm@4285
   583
fun vars_of thm = rev (add_vars ([], #prop (Thm.rep_thm thm)));
wenzelm@4285
   584
wenzelm@4285
   585
wenzelm@4285
   586
(* instantiate by left-to-right occurrence of variables *)
wenzelm@4285
   587
wenzelm@4285
   588
fun instantiate' cTs cts thm =
wenzelm@4285
   589
  let
wenzelm@4285
   590
    fun err msg =
wenzelm@4285
   591
      raise TYPE ("instantiate': " ^ msg,
wenzelm@4285
   592
        mapfilter (apsome Thm.typ_of) cTs,
wenzelm@4285
   593
        mapfilter (apsome Thm.term_of) cts);
wenzelm@4285
   594
wenzelm@4285
   595
    fun inst_of (v, ct) =
wenzelm@4285
   596
      (Thm.cterm_of (#sign (Thm.rep_cterm ct)) (Var v), ct)
wenzelm@4285
   597
        handle TYPE (msg, _, _) => err msg;
wenzelm@4285
   598
wenzelm@4285
   599
    fun zip_vars _ [] = []
wenzelm@4285
   600
      | zip_vars (_ :: vs) (None :: opt_ts) = zip_vars vs opt_ts
wenzelm@4285
   601
      | zip_vars (v :: vs) (Some t :: opt_ts) = (v, t) :: zip_vars vs opt_ts
wenzelm@4285
   602
      | zip_vars [] _ = err "more instantiations than variables in thm";
wenzelm@4285
   603
wenzelm@4285
   604
    (*instantiate types first!*)
wenzelm@4285
   605
    val thm' =
wenzelm@4285
   606
      if forall is_none cTs then thm
wenzelm@4285
   607
      else Thm.instantiate (zip_vars (map fst (tvars_of thm)) cTs, []) thm;
wenzelm@4285
   608
    in
wenzelm@4285
   609
      if forall is_none cts then thm'
wenzelm@4285
   610
      else Thm.instantiate ([], map inst_of (zip_vars (vars_of thm') cts)) thm'
wenzelm@4285
   611
    end;
wenzelm@4285
   612
wenzelm@4285
   613
clasohm@0
   614
end;
wenzelm@252
   615
paulson@1499
   616
open Drule;