src/Pure/drule.ML
author wenzelm
Mon Jul 24 23:47:14 2000 +0200 (2000-07-24)
changeset 9418 96973ec6fda4
parent 9288 06a55195741b
child 9455 f23722b4fbe7
permissions -rw-r--r--
Drule.merge_rules;
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
wenzelm@9288
     9
infix 0 RS RSN RL RLN MRS MRL OF COMP;
clasohm@0
    10
wenzelm@5903
    11
signature BASIC_DRULE =
wenzelm@3766
    12
sig
wenzelm@4285
    13
  val dest_implies      : cterm -> cterm * cterm
wenzelm@8328
    14
  val skip_flexpairs    : cterm -> cterm
wenzelm@8328
    15
  val strip_imp_prems   : cterm -> cterm list
wenzelm@8328
    16
  val cprems_of         : thm -> cterm list
wenzelm@8328
    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)
wenzelm@7636
    23
  val strip_shyps_warning : thm -> thm
wenzelm@8328
    24
  val forall_intr_list  : cterm list -> thm -> thm
wenzelm@8328
    25
  val forall_intr_frees : thm -> thm
wenzelm@8328
    26
  val forall_intr_vars  : thm -> thm
wenzelm@8328
    27
  val forall_elim_list  : cterm list -> thm -> thm
wenzelm@8328
    28
  val forall_elim_var   : int -> thm -> thm
wenzelm@8328
    29
  val forall_elim_vars  : int -> thm -> thm
wenzelm@8328
    30
  val freeze_thaw       : thm -> thm * (thm -> thm)
wenzelm@8328
    31
  val implies_elim_list : thm -> thm list -> thm
wenzelm@8328
    32
  val implies_intr_list : cterm list -> thm -> thm
paulson@8129
    33
  val instantiate       :
paulson@8129
    34
    (indexname * ctyp) list * (cterm * cterm) list -> thm -> thm
wenzelm@8328
    35
  val zero_var_indexes  : thm -> thm
wenzelm@8328
    36
  val standard          : thm -> thm
paulson@4610
    37
  val rotate_prems      : int -> thm -> thm
wenzelm@8328
    38
  val assume_ax         : theory -> string -> thm
wenzelm@8328
    39
  val RSN               : thm * (int * thm) -> thm
wenzelm@8328
    40
  val RS                : thm * thm -> thm
wenzelm@8328
    41
  val RLN               : thm list * (int * thm list) -> thm list
wenzelm@8328
    42
  val RL                : thm list * thm list -> thm list
wenzelm@8328
    43
  val MRS               : thm list * thm -> thm
wenzelm@8328
    44
  val MRL               : thm list list * thm list -> thm list
wenzelm@9288
    45
  val OF                : thm * thm list -> thm
wenzelm@8328
    46
  val compose           : thm * int * thm -> thm list
wenzelm@8328
    47
  val COMP              : thm * thm -> thm
clasohm@0
    48
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
wenzelm@8328
    49
  val read_instantiate  : (string*string)list -> thm -> thm
wenzelm@8328
    50
  val cterm_instantiate : (cterm*cterm)list -> thm -> thm
wenzelm@8328
    51
  val weak_eq_thm       : thm * thm -> bool
wenzelm@8328
    52
  val eq_thm_sg         : thm * thm -> bool
wenzelm@8328
    53
  val size_of_thm       : thm -> int
wenzelm@8328
    54
  val reflexive_thm     : thm
wenzelm@8328
    55
  val symmetric_thm     : thm
wenzelm@8328
    56
  val transitive_thm    : thm
paulson@2004
    57
  val refl_implies      : thm
nipkow@4679
    58
  val symmetric_fun     : thm -> thm
wenzelm@8328
    59
  val rewrite_rule_aux  : (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
wenzelm@8328
    60
  val rewrite_thm       : bool * bool * bool
nipkow@4713
    61
                          -> (meta_simpset -> thm -> thm option)
nipkow@4713
    62
                          -> meta_simpset -> thm -> thm
wenzelm@8328
    63
  val rewrite_cterm     : bool * bool * bool
wenzelm@5079
    64
                          -> (meta_simpset -> thm -> thm option)
wenzelm@5079
    65
                          -> meta_simpset -> cterm -> thm
wenzelm@4285
    66
  val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
wenzelm@8328
    67
  val rewrite_goal_rule : bool* bool * bool
nipkow@4713
    68
                          -> (meta_simpset -> thm -> thm option)
nipkow@4713
    69
                          -> meta_simpset -> int -> thm -> thm
wenzelm@8328
    70
  val equal_abs_elim    : cterm  -> thm -> thm
wenzelm@4285
    71
  val equal_abs_elim_list: cterm list -> thm -> thm
wenzelm@4285
    72
  val flexpair_abs_elim_list: cterm list -> thm -> thm
wenzelm@8328
    73
  val asm_rl            : thm
wenzelm@8328
    74
  val cut_rl            : thm
wenzelm@8328
    75
  val revcut_rl         : thm
wenzelm@8328
    76
  val thin_rl           : thm
wenzelm@4285
    77
  val triv_forall_equality: thm
nipkow@1756
    78
  val swap_prems_rl     : thm
wenzelm@4285
    79
  val equal_intr_rule   : thm
paulson@8550
    80
  val inst              : string -> string -> thm -> thm
wenzelm@8328
    81
  val instantiate'      : ctyp option list -> cterm option list -> thm -> thm
wenzelm@8328
    82
  val incr_indexes      : int -> thm -> thm
wenzelm@8328
    83
  val incr_indexes_wrt  : int list -> ctyp list -> cterm list -> thm list -> thm -> thm
wenzelm@5903
    84
end;
wenzelm@5903
    85
wenzelm@5903
    86
signature DRULE =
wenzelm@5903
    87
sig
wenzelm@5903
    88
  include BASIC_DRULE
wenzelm@8328
    89
  val compose_single    : thm * int * thm -> thm
wenzelm@9418
    90
  val merge_rules	: thm list * thm list -> thm list
wenzelm@8328
    91
  val triv_goal         : thm
wenzelm@8328
    92
  val rev_triv_goal     : thm
wenzelm@8328
    93
  val freeze_all        : thm -> thm
paulson@5311
    94
  val mk_triv_goal      : cterm -> thm
wenzelm@8328
    95
  val mk_cgoal          : cterm -> cterm
wenzelm@8328
    96
  val assume_goal       : cterm -> thm
wenzelm@8328
    97
  val tvars_of_terms    : term list -> (indexname * sort) list
wenzelm@8328
    98
  val vars_of_terms     : term list -> (indexname * typ) list
wenzelm@8328
    99
  val tvars_of          : thm -> (indexname * sort) list
wenzelm@8328
   100
  val vars_of           : thm -> (indexname * typ) list
wenzelm@8328
   101
  val unvarifyT         : thm -> thm
wenzelm@8328
   102
  val unvarify          : thm -> thm
wenzelm@8605
   103
  val tvars_intr_list	: string list -> thm -> thm
wenzelm@8328
   104
  val rule_attribute    : ('a -> thm -> thm) -> 'a attribute
wenzelm@8365
   105
  val tag_rule          : tag -> thm -> thm
wenzelm@8496
   106
  val untag_rule        : string -> thm -> thm
wenzelm@8328
   107
  val tag               : tag -> 'a attribute
wenzelm@8496
   108
  val untag             : string -> 'a attribute
wenzelm@8328
   109
  val tag_lemma         : 'a attribute
wenzelm@8328
   110
  val tag_assumption    : 'a attribute
wenzelm@8328
   111
  val tag_internal      : 'a attribute
wenzelm@3766
   112
end;
clasohm@0
   113
wenzelm@5903
   114
structure Drule: DRULE =
clasohm@0
   115
struct
clasohm@0
   116
wenzelm@3991
   117
lcp@708
   118
(** some cterm->cterm operations: much faster than calling cterm_of! **)
lcp@708
   119
paulson@2004
   120
(** SAME NAMES as in structure Logic: use compound identifiers! **)
paulson@2004
   121
clasohm@1703
   122
(*dest_implies for cterms. Note T=prop below*)
paulson@2004
   123
fun dest_implies ct =
wenzelm@8328
   124
    case term_of ct of
wenzelm@8328
   125
        (Const("==>", _) $ _ $ _) =>
wenzelm@8328
   126
            let val (ct1,ct2) = dest_comb ct
wenzelm@8328
   127
            in  (#2 (dest_comb ct1), ct2)  end
paulson@2004
   128
      | _ => raise TERM ("dest_implies", [term_of ct]) ;
clasohm@1703
   129
clasohm@1703
   130
lcp@708
   131
(*Discard flexflex pairs; return a cterm*)
paulson@2004
   132
fun skip_flexpairs ct =
lcp@708
   133
    case term_of ct of
wenzelm@8328
   134
        (Const("==>", _) $ (Const("=?=",_)$_$_) $ _) =>
wenzelm@8328
   135
            skip_flexpairs (#2 (dest_implies ct))
lcp@708
   136
      | _ => ct;
lcp@708
   137
lcp@708
   138
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
paulson@2004
   139
fun strip_imp_prems ct =
paulson@2004
   140
    let val (cA,cB) = dest_implies ct
paulson@2004
   141
    in  cA :: strip_imp_prems cB  end
lcp@708
   142
    handle TERM _ => [];
lcp@708
   143
paulson@2004
   144
(* A1==>...An==>B  goes to B, where B is not an implication *)
paulson@2004
   145
fun strip_imp_concl ct =
wenzelm@8328
   146
    case term_of ct of (Const("==>", _) $ _ $ _) =>
wenzelm@8328
   147
        strip_imp_concl (#2 (dest_comb ct))
paulson@2004
   148
  | _ => ct;
paulson@2004
   149
lcp@708
   150
(*The premises of a theorem, as a cterm list*)
paulson@2004
   151
val cprems_of = strip_imp_prems o skip_flexpairs o cprop_of;
lcp@708
   152
lcp@708
   153
lcp@229
   154
(** reading of instantiations **)
lcp@229
   155
lcp@229
   156
fun absent ixn =
lcp@229
   157
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
lcp@229
   158
lcp@229
   159
fun inst_failure ixn =
lcp@229
   160
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
lcp@229
   161
nipkow@4281
   162
fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
nipkow@4281
   163
let val {tsig,...} = Sign.rep_sg sign
nipkow@4281
   164
    fun split([],tvs,vs) = (tvs,vs)
wenzelm@4691
   165
      | split((sv,st)::l,tvs,vs) = (case Symbol.explode sv of
wenzelm@4691
   166
                  "'"::cs => split(l,(Syntax.indexname cs,st)::tvs,vs)
wenzelm@4691
   167
                | cs => split(l,tvs,(Syntax.indexname cs,st)::vs));
nipkow@4281
   168
    val (tvs,vs) = split(insts,[],[]);
nipkow@4281
   169
    fun readT((a,i),st) =
nipkow@4281
   170
        let val ixn = ("'" ^ a,i);
nipkow@4281
   171
            val S = case rsorts ixn of Some S => S | None => absent ixn;
nipkow@4281
   172
            val T = Sign.read_typ (sign,sorts) st;
nipkow@4281
   173
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
nipkow@4281
   174
           else inst_failure ixn
nipkow@4281
   175
        end
nipkow@4281
   176
    val tye = map readT tvs;
nipkow@4281
   177
    fun mkty(ixn,st) = (case rtypes ixn of
nipkow@4281
   178
                          Some T => (ixn,(st,typ_subst_TVars tye T))
nipkow@4281
   179
                        | None => absent ixn);
nipkow@4281
   180
    val ixnsTs = map mkty vs;
nipkow@4281
   181
    val ixns = map fst ixnsTs
nipkow@4281
   182
    and sTs  = map snd ixnsTs
nipkow@4281
   183
    val (cts,tye2) = read_def_cterms(sign,types,sorts) used false sTs;
nipkow@4281
   184
    fun mkcVar(ixn,T) =
nipkow@4281
   185
        let val U = typ_subst_TVars tye2 T
nipkow@4281
   186
        in cterm_of sign (Var(ixn,U)) end
nipkow@4281
   187
    val ixnTs = ListPair.zip(ixns, map snd sTs)
nipkow@4281
   188
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) (tye2 @ tye),
nipkow@4281
   189
    ListPair.zip(map mkcVar ixnTs,cts))
nipkow@4281
   190
end;
lcp@229
   191
lcp@229
   192
wenzelm@252
   193
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   194
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   195
     type variables) when reading another term.
clasohm@0
   196
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   197
***)
clasohm@0
   198
clasohm@0
   199
fun types_sorts thm =
clasohm@0
   200
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   201
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   202
        val vars = map dest_Var (term_vars big);
wenzelm@252
   203
        val frees = map dest_Free (term_frees big);
wenzelm@252
   204
        val tvars = term_tvars big;
wenzelm@252
   205
        val tfrees = term_tfrees big;
wenzelm@252
   206
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   207
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   208
    in (typ,sort) end;
clasohm@0
   209
wenzelm@7636
   210
clasohm@0
   211
(** Standardization of rules **)
clasohm@0
   212
wenzelm@7636
   213
(*Strip extraneous shyps as far as possible*)
wenzelm@7636
   214
fun strip_shyps_warning thm =
wenzelm@7636
   215
  let
wenzelm@7636
   216
    val str_of_sort = Sign.str_of_sort (Thm.sign_of_thm thm);
wenzelm@7636
   217
    val thm' = Thm.strip_shyps thm;
wenzelm@7636
   218
    val xshyps = Thm.extra_shyps thm';
wenzelm@7636
   219
  in
wenzelm@7636
   220
    if null xshyps then ()
wenzelm@7636
   221
    else warning ("Pending sort hypotheses: " ^ commas (map str_of_sort xshyps));
wenzelm@7636
   222
    thm'
wenzelm@7636
   223
  end;
wenzelm@7636
   224
clasohm@0
   225
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   226
fun forall_intr_list [] th = th
clasohm@0
   227
  | forall_intr_list (y::ys) th =
wenzelm@252
   228
        let val gth = forall_intr_list ys th
wenzelm@252
   229
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   230
clasohm@0
   231
(*Generalization over all suitable Free variables*)
clasohm@0
   232
fun forall_intr_frees th =
clasohm@0
   233
    let val {prop,sign,...} = rep_thm th
clasohm@0
   234
    in  forall_intr_list
wenzelm@4440
   235
         (map (cterm_of sign) (sort (make_ord atless) (term_frees prop)))
clasohm@0
   236
         th
clasohm@0
   237
    end;
clasohm@0
   238
wenzelm@7898
   239
val forall_elim_var = PureThy.forall_elim_var;
wenzelm@7898
   240
val forall_elim_vars = PureThy.forall_elim_vars;
clasohm@0
   241
clasohm@0
   242
(*Specialization over a list of cterms*)
clasohm@0
   243
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   244
clasohm@0
   245
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   246
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   247
clasohm@0
   248
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   249
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   250
clasohm@0
   251
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   252
fun zero_var_indexes th =
clasohm@0
   253
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   254
        val vars = term_vars prop
clasohm@0
   255
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   256
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   257
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
paulson@2266
   258
        val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
wenzelm@8328
   259
                     (inrs, nms')
wenzelm@252
   260
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   261
        fun varpairs([],[]) = []
wenzelm@252
   262
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   263
                let val T' = typ_subst_TVars tye T
wenzelm@252
   264
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   265
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   266
                end
wenzelm@252
   267
          | varpairs _ = raise TERM("varpairs", []);
paulson@8129
   268
    in Thm.instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   269
clasohm@0
   270
clasohm@0
   271
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   272
    all generality expressed by Vars having index 0.*)
clasohm@0
   273
fun standard th =
wenzelm@1218
   274
  let val {maxidx,...} = rep_thm th
wenzelm@1237
   275
  in
wenzelm@1218
   276
    th |> implies_intr_hyps
paulson@1412
   277
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
wenzelm@7636
   278
       |> strip_shyps_warning
paulson@1412
   279
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
wenzelm@1218
   280
  end;
wenzelm@1218
   281
clasohm@0
   282
wenzelm@8328
   283
(*Convert all Vars in a theorem to Frees.  Also return a function for
paulson@4610
   284
  reversing that operation.  DOES NOT WORK FOR TYPE VARIABLES.
paulson@4610
   285
  Similar code in type/freeze_thaw*)
paulson@4610
   286
fun freeze_thaw th =
paulson@7248
   287
 let val fth = freezeT th
paulson@7248
   288
     val {prop,sign,...} = rep_thm fth
paulson@7248
   289
 in
paulson@7248
   290
   case term_vars prop of
paulson@7248
   291
       [] => (fth, fn x => x)
paulson@7248
   292
     | vars =>
wenzelm@8328
   293
         let fun newName (Var(ix,_), (pairs,used)) =
wenzelm@8328
   294
                   let val v = variant used (string_of_indexname ix)
wenzelm@8328
   295
                   in  ((ix,v)::pairs, v::used)  end;
wenzelm@8328
   296
             val (alist, _) = foldr newName
wenzelm@8328
   297
                                (vars, ([], add_term_names (prop, [])))
wenzelm@8328
   298
             fun mk_inst (Var(v,T)) =
wenzelm@8328
   299
                 (cterm_of sign (Var(v,T)),
wenzelm@8328
   300
                  cterm_of sign (Free(the (assoc(alist,v)), T)))
wenzelm@8328
   301
             val insts = map mk_inst vars
wenzelm@8328
   302
             fun thaw th' =
wenzelm@8328
   303
                 th' |> forall_intr_list (map #2 insts)
wenzelm@8328
   304
                     |> forall_elim_list (map #1 insts)
wenzelm@8328
   305
         in  (Thm.instantiate ([],insts) fth, thaw)  end
paulson@7248
   306
 end;
paulson@4610
   307
paulson@4610
   308
paulson@7248
   309
(*Rotates a rule's premises to the left by k*)
paulson@7248
   310
val rotate_prems = permute_prems 0;
paulson@4610
   311
paulson@4610
   312
wenzelm@252
   313
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   314
  Generalizes over Free variables,
clasohm@0
   315
  creates the assumption, and then strips quantifiers.
clasohm@0
   316
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   317
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   318
fun assume_ax thy sP =
wenzelm@6390
   319
    let val sign = Theory.sign_of thy
paulson@4610
   320
        val prop = Logic.close_form (term_of (read_cterm sign (sP, propT)))
lcp@229
   321
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   322
wenzelm@252
   323
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   324
fun tha RSN (i,thb) =
wenzelm@4270
   325
  case Seq.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   326
      ([th],_) => th
clasohm@0
   327
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   328
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   329
clasohm@0
   330
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   331
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   332
clasohm@0
   333
(*For joining lists of rules*)
wenzelm@252
   334
fun thas RLN (i,thbs) =
clasohm@0
   335
  let val resolve = biresolution false (map (pair false) thas) i
wenzelm@4270
   336
      fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
paulson@2672
   337
  in  List.concat (map resb thbs)  end;
clasohm@0
   338
clasohm@0
   339
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   340
lcp@11
   341
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   342
  makes proof trees*)
wenzelm@252
   343
fun rls MRS bottom_rl =
lcp@11
   344
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   345
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   346
  in  rs_aux 1 rls  end;
lcp@11
   347
lcp@11
   348
(*As above, but for rule lists*)
wenzelm@252
   349
fun rlss MRL bottom_rls =
lcp@11
   350
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   351
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   352
  in  rs_aux 1 rlss  end;
lcp@11
   353
wenzelm@9288
   354
(*A version of MRS with more appropriate argument order*)
wenzelm@9288
   355
fun bottom_rl OF rls = rls MRS bottom_rl;
wenzelm@9288
   356
wenzelm@252
   357
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   358
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   359
  ALWAYS deletes premise i *)
wenzelm@252
   360
fun compose(tha,i,thb) =
wenzelm@4270
   361
    Seq.list_of (bicompose false (false,tha,0) i thb);
clasohm@0
   362
wenzelm@6946
   363
fun compose_single (tha,i,thb) =
wenzelm@6946
   364
  (case compose (tha,i,thb) of
wenzelm@6946
   365
    [th] => th
wenzelm@6946
   366
  | _ => raise THM ("compose: unique result expected", i, [tha,thb]));
wenzelm@6946
   367
clasohm@0
   368
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   369
fun tha COMP thb =
clasohm@0
   370
    case compose(tha,1,thb) of
wenzelm@252
   371
        [th] => th
clasohm@0
   372
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   373
wenzelm@4016
   374
(** theorem equality **)
clasohm@0
   375
clasohm@0
   376
(*Do the two theorems have the same signature?*)
wenzelm@252
   377
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
wenzelm@9418
   378
fun merge_rules (ths1, ths2) = Library.generic_merge Thm.eq_thm I I ths1 ths2;
clasohm@0
   379
clasohm@0
   380
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   381
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   382
clasohm@0
   383
lcp@1194
   384
(** Mark Staples's weaker version of eq_thm: ignores variable renaming and
lcp@1194
   385
    (some) type variable renaming **)
lcp@1194
   386
lcp@1194
   387
 (* Can't use term_vars, because it sorts the resulting list of variable names.
lcp@1194
   388
    We instead need the unique list noramlised by the order of appearance
lcp@1194
   389
    in the term. *)
lcp@1194
   390
fun term_vars' (t as Var(v,T)) = [t]
lcp@1194
   391
  | term_vars' (Abs(_,_,b)) = term_vars' b
lcp@1194
   392
  | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
lcp@1194
   393
  | term_vars' _ = [];
lcp@1194
   394
lcp@1194
   395
fun forall_intr_vars th =
lcp@1194
   396
  let val {prop,sign,...} = rep_thm th;
lcp@1194
   397
      val vars = distinct (term_vars' prop);
lcp@1194
   398
  in forall_intr_list (map (cterm_of sign) vars) th end;
lcp@1194
   399
wenzelm@1237
   400
fun weak_eq_thm (tha,thb) =
lcp@1194
   401
    eq_thm(forall_intr_vars (freezeT tha), forall_intr_vars (freezeT thb));
lcp@1194
   402
lcp@1194
   403
lcp@1194
   404
clasohm@0
   405
(*** Meta-Rewriting Rules ***)
clasohm@0
   406
wenzelm@6390
   407
val proto_sign = Theory.sign_of ProtoPure.thy;
paulson@4610
   408
paulson@4610
   409
fun read_prop s = read_cterm proto_sign (s, propT);
paulson@4610
   410
wenzelm@7404
   411
fun store_thm name thm = hd (PureThy.smart_store_thms (name, [standard thm]));
wenzelm@4016
   412
clasohm@0
   413
val reflexive_thm =
paulson@4610
   414
  let val cx = cterm_of proto_sign (Var(("x",0),TVar(("'a",0),logicS)))
wenzelm@4016
   415
  in store_thm "reflexive" (Thm.reflexive cx) end;
clasohm@0
   416
clasohm@0
   417
val symmetric_thm =
paulson@4610
   418
  let val xy = read_prop "x::'a::logic == y"
wenzelm@8328
   419
  in store_thm "symmetric"
paulson@4610
   420
      (Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy)))
paulson@4610
   421
   end;
clasohm@0
   422
clasohm@0
   423
val transitive_thm =
paulson@4610
   424
  let val xy = read_prop "x::'a::logic == y"
paulson@4610
   425
      val yz = read_prop "y::'a::logic == z"
clasohm@0
   426
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
paulson@4610
   427
  in store_thm "transitive" (Thm.implies_intr yz (Thm.transitive xythm yzthm))
paulson@4610
   428
  end;
clasohm@0
   429
nipkow@4679
   430
fun symmetric_fun thm = thm RS symmetric_thm;
nipkow@4679
   431
lcp@229
   432
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   433
paulson@4610
   434
val refl_implies = reflexive (cterm_of proto_sign implies);
clasohm@0
   435
clasohm@0
   436
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   437
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   438
fun goals_conv pred cv =
lcp@229
   439
  let fun gconv i ct =
paulson@2004
   440
        let val (A,B) = dest_implies ct
lcp@229
   441
            val (thA,j) = case term_of A of
lcp@229
   442
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   443
                | _ => (if pred i then cv A else reflexive A, i+1)
paulson@2004
   444
        in  combination (combination refl_implies thA) (gconv j B) end
lcp@229
   445
        handle TERM _ => reflexive ct
clasohm@0
   446
  in gconv 1 end;
clasohm@0
   447
clasohm@0
   448
(*Use a conversion to transform a theorem*)
lcp@229
   449
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   450
clasohm@0
   451
(*rewriting conversion*)
lcp@229
   452
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   453
clasohm@0
   454
(*Rewrite a theorem*)
wenzelm@3575
   455
fun rewrite_rule_aux _ []   th = th
wenzelm@3575
   456
  | rewrite_rule_aux prover thms th =
nipkow@4713
   457
      fconv_rule (rew_conv (true,false,false) prover (Thm.mss_of thms)) th;
clasohm@0
   458
wenzelm@3555
   459
fun rewrite_thm mode prover mss = fconv_rule (rew_conv mode prover mss);
wenzelm@5079
   460
fun rewrite_cterm mode prover mss = Thm.rewrite_cterm mode mss prover;
wenzelm@3555
   461
clasohm@0
   462
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
wenzelm@3575
   463
fun rewrite_goals_rule_aux _ []   th = th
wenzelm@3575
   464
  | rewrite_goals_rule_aux prover thms th =
nipkow@4713
   465
      fconv_rule (goals_conv (K true) (rew_conv (true, true, false) prover
wenzelm@3575
   466
        (Thm.mss_of thms))) th;
clasohm@0
   467
clasohm@0
   468
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   469
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   470
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   471
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   472
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   473
clasohm@0
   474
clasohm@0
   475
(*** Some useful meta-theorems ***)
clasohm@0
   476
clasohm@0
   477
(*The rule V/V, obtains assumption solving for eresolve_tac*)
wenzelm@7380
   478
val asm_rl = store_thm "asm_rl" (trivial(read_prop "PROP ?psi"));
wenzelm@7380
   479
val _ = store_thm "_" asm_rl;
clasohm@0
   480
clasohm@0
   481
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@4016
   482
val cut_rl =
wenzelm@4016
   483
  store_thm "cut_rl"
paulson@4610
   484
    (trivial(read_prop "PROP ?psi ==> PROP ?theta"));
clasohm@0
   485
wenzelm@252
   486
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   487
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   488
val revcut_rl =
paulson@4610
   489
  let val V = read_prop "PROP V"
paulson@4610
   490
      and VW = read_prop "PROP V ==> PROP W";
wenzelm@4016
   491
  in
wenzelm@4016
   492
    store_thm "revcut_rl"
wenzelm@4016
   493
      (implies_intr V (implies_intr VW (implies_elim (assume VW) (assume V))))
clasohm@0
   494
  end;
clasohm@0
   495
lcp@668
   496
(*for deleting an unwanted assumption*)
lcp@668
   497
val thin_rl =
paulson@4610
   498
  let val V = read_prop "PROP V"
paulson@4610
   499
      and W = read_prop "PROP W";
wenzelm@4016
   500
  in  store_thm "thin_rl" (implies_intr V (implies_intr W (assume W)))
lcp@668
   501
  end;
lcp@668
   502
clasohm@0
   503
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   504
val triv_forall_equality =
paulson@4610
   505
  let val V  = read_prop "PROP V"
paulson@4610
   506
      and QV = read_prop "!!x::'a. PROP V"
wenzelm@8086
   507
      and x  = read_cterm proto_sign ("x", TypeInfer.logicT);
wenzelm@4016
   508
  in
wenzelm@4016
   509
    store_thm "triv_forall_equality"
wenzelm@4016
   510
      (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@4016
   511
        (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   512
  end;
clasohm@0
   513
nipkow@1756
   514
(* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
nipkow@1756
   515
   (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
nipkow@1756
   516
   `thm COMP swap_prems_rl' swaps the first two premises of `thm'
nipkow@1756
   517
*)
nipkow@1756
   518
val swap_prems_rl =
paulson@4610
   519
  let val cmajor = read_prop "PROP PhiA ==> PROP PhiB ==> PROP Psi";
nipkow@1756
   520
      val major = assume cmajor;
paulson@4610
   521
      val cminor1 = read_prop "PROP PhiA";
nipkow@1756
   522
      val minor1 = assume cminor1;
paulson@4610
   523
      val cminor2 = read_prop "PROP PhiB";
nipkow@1756
   524
      val minor2 = assume cminor2;
wenzelm@4016
   525
  in store_thm "swap_prems_rl"
nipkow@1756
   526
       (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
nipkow@1756
   527
         (implies_elim (implies_elim major minor1) minor2))))
nipkow@1756
   528
  end;
nipkow@1756
   529
nipkow@3653
   530
(* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
nipkow@3653
   531
   ==> PROP ?phi == PROP ?psi
wenzelm@8328
   532
   Introduction rule for == as a meta-theorem.
nipkow@3653
   533
*)
nipkow@3653
   534
val equal_intr_rule =
paulson@4610
   535
  let val PQ = read_prop "PROP phi ==> PROP psi"
paulson@4610
   536
      and QP = read_prop "PROP psi ==> PROP phi"
wenzelm@4016
   537
  in
wenzelm@4016
   538
    store_thm "equal_intr_rule"
wenzelm@4016
   539
      (implies_intr PQ (implies_intr QP (equal_intr (assume PQ) (assume QP))))
nipkow@3653
   540
  end;
nipkow@3653
   541
wenzelm@4285
   542
paulson@8129
   543
(*** Instantiate theorem th, reading instantiations under signature sg ****)
paulson@8129
   544
paulson@8129
   545
(*Version that normalizes the result: Thm.instantiate no longer does that*)
paulson@8129
   546
fun instantiate instpair th = Thm.instantiate instpair th  COMP   asm_rl;
paulson@8129
   547
paulson@8129
   548
fun read_instantiate_sg sg sinsts th =
paulson@8129
   549
    let val ts = types_sorts th;
paulson@8129
   550
        val used = add_term_tvarnames(#prop(rep_thm th),[]);
paulson@8129
   551
    in  instantiate (read_insts sg ts ts used sinsts) th  end;
paulson@8129
   552
paulson@8129
   553
(*Instantiate theorem th, reading instantiations under theory of th*)
paulson@8129
   554
fun read_instantiate sinsts th =
paulson@8129
   555
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
paulson@8129
   556
paulson@8129
   557
paulson@8129
   558
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
paulson@8129
   559
  Instantiates distinct Vars by terms, inferring type instantiations. *)
paulson@8129
   560
local
paulson@8129
   561
  fun add_types ((ct,cu), (sign,tye,maxidx)) =
paulson@8129
   562
    let val {sign=signt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
paulson@8129
   563
        and {sign=signu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
paulson@8129
   564
        val maxi = Int.max(maxidx, Int.max(maxt, maxu));
paulson@8129
   565
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
paulson@8129
   566
        val (tye',maxi') = Type.unify (#tsig(Sign.rep_sg sign')) maxi tye (T,U)
paulson@8129
   567
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
paulson@8129
   568
    in  (sign', tye', maxi')  end;
paulson@8129
   569
in
paulson@8129
   570
fun cterm_instantiate ctpairs0 th =
berghofe@8406
   571
  let val (sign,tye,_) = foldr add_types (ctpairs0, (#sign(rep_thm th), Vartab.empty, 0))
paulson@8129
   572
      val tsig = #tsig(Sign.rep_sg sign);
berghofe@8406
   573
      fun instT(ct,cu) = let val inst = subst_TVars_Vartab tye
paulson@8129
   574
                         in (cterm_fun inst ct, cterm_fun inst cu) end
paulson@8129
   575
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
berghofe@8406
   576
  in  instantiate (map ctyp2 (Vartab.dest tye), map instT ctpairs0) th  end
paulson@8129
   577
  handle TERM _ =>
paulson@8129
   578
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
paulson@8129
   579
       | TYPE (msg, _, _) => raise THM(msg, 0, [th])
paulson@8129
   580
end;
paulson@8129
   581
paulson@8129
   582
paulson@8129
   583
(** Derived rules mainly for METAHYPS **)
paulson@8129
   584
paulson@8129
   585
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
paulson@8129
   586
fun equal_abs_elim ca eqth =
paulson@8129
   587
  let val {sign=signa, t=a, ...} = rep_cterm ca
paulson@8129
   588
      and combth = combination eqth (reflexive ca)
paulson@8129
   589
      val {sign,prop,...} = rep_thm eqth
paulson@8129
   590
      val (abst,absu) = Logic.dest_equals prop
paulson@8129
   591
      val cterm = cterm_of (Sign.merge (sign,signa))
paulson@8129
   592
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
paulson@8129
   593
           (transitive combth (beta_conversion (cterm (absu$a))))
paulson@8129
   594
  end
paulson@8129
   595
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
paulson@8129
   596
paulson@8129
   597
(*Calling equal_abs_elim with multiple terms*)
paulson@8129
   598
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
paulson@8129
   599
paulson@8129
   600
local
paulson@8129
   601
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
paulson@8129
   602
  fun err th = raise THM("flexpair_inst: ", 0, [th])
paulson@8129
   603
  fun flexpair_inst def th =
paulson@8129
   604
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
paulson@8129
   605
        val cterm = cterm_of sign
paulson@8129
   606
        fun cvar a = cterm(Var((a,0),alpha))
paulson@8129
   607
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
paulson@8129
   608
                   def
paulson@8129
   609
    in  equal_elim def' th
paulson@8129
   610
    end
paulson@8129
   611
    handle THM _ => err th | Bind => err th
paulson@8129
   612
in
paulson@8129
   613
val flexpair_intr = flexpair_inst (symmetric ProtoPure.flexpair_def)
paulson@8129
   614
and flexpair_elim = flexpair_inst ProtoPure.flexpair_def
paulson@8129
   615
end;
paulson@8129
   616
paulson@8129
   617
(*Version for flexflex pairs -- this supports lifting.*)
paulson@8129
   618
fun flexpair_abs_elim_list cts =
paulson@8129
   619
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
paulson@8129
   620
paulson@8129
   621
paulson@8129
   622
(*** GOAL (PROP A) <==> PROP A ***)
wenzelm@4789
   623
wenzelm@4789
   624
local
wenzelm@4789
   625
  val A = read_prop "PROP A";
wenzelm@4789
   626
  val G = read_prop "GOAL (PROP A)";
wenzelm@4789
   627
  val (G_def, _) = freeze_thaw ProtoPure.Goal_def;
wenzelm@4789
   628
in
wenzelm@4789
   629
  val triv_goal = store_thm "triv_goal" (Thm.equal_elim (Thm.symmetric G_def) (Thm.assume A));
wenzelm@4789
   630
  val rev_triv_goal = store_thm "rev_triv_goal" (Thm.equal_elim G_def (Thm.assume G));
wenzelm@4789
   631
end;
wenzelm@4789
   632
wenzelm@6995
   633
val mk_cgoal = Thm.capply (Thm.cterm_of proto_sign (Const ("Goal", propT --> propT)));
wenzelm@6995
   634
fun assume_goal ct = Thm.assume (mk_cgoal ct) RS rev_triv_goal;
wenzelm@6995
   635
wenzelm@4789
   636
wenzelm@4285
   637
wenzelm@5688
   638
(** variations on instantiate **)
wenzelm@4285
   639
paulson@8550
   640
(*shorthand for instantiating just one variable in the current theory*)
paulson@8550
   641
fun inst x t = read_instantiate_sg (sign_of (the_context())) [(x,t)];
paulson@8550
   642
paulson@8550
   643
wenzelm@4285
   644
(* collect vars *)
wenzelm@4285
   645
wenzelm@4285
   646
val add_tvarsT = foldl_atyps (fn (vs, TVar v) => v ins vs | (vs, _) => vs);
wenzelm@4285
   647
val add_tvars = foldl_types add_tvarsT;
wenzelm@4285
   648
val add_vars = foldl_aterms (fn (vs, Var v) => v ins vs | (vs, _) => vs);
wenzelm@4285
   649
wenzelm@5903
   650
fun tvars_of_terms ts = rev (foldl add_tvars ([], ts));
wenzelm@5903
   651
fun vars_of_terms ts = rev (foldl add_vars ([], ts));
wenzelm@5903
   652
wenzelm@5903
   653
fun tvars_of thm = tvars_of_terms [#prop (Thm.rep_thm thm)];
wenzelm@5903
   654
fun vars_of thm = vars_of_terms [#prop (Thm.rep_thm thm)];
wenzelm@4285
   655
wenzelm@4285
   656
wenzelm@4285
   657
(* instantiate by left-to-right occurrence of variables *)
wenzelm@4285
   658
wenzelm@4285
   659
fun instantiate' cTs cts thm =
wenzelm@4285
   660
  let
wenzelm@4285
   661
    fun err msg =
wenzelm@4285
   662
      raise TYPE ("instantiate': " ^ msg,
wenzelm@4285
   663
        mapfilter (apsome Thm.typ_of) cTs,
wenzelm@4285
   664
        mapfilter (apsome Thm.term_of) cts);
wenzelm@4285
   665
wenzelm@4285
   666
    fun inst_of (v, ct) =
wenzelm@4285
   667
      (Thm.cterm_of (#sign (Thm.rep_cterm ct)) (Var v), ct)
wenzelm@4285
   668
        handle TYPE (msg, _, _) => err msg;
wenzelm@4285
   669
wenzelm@4285
   670
    fun zip_vars _ [] = []
wenzelm@4285
   671
      | zip_vars (_ :: vs) (None :: opt_ts) = zip_vars vs opt_ts
wenzelm@4285
   672
      | zip_vars (v :: vs) (Some t :: opt_ts) = (v, t) :: zip_vars vs opt_ts
wenzelm@4285
   673
      | zip_vars [] _ = err "more instantiations than variables in thm";
wenzelm@4285
   674
wenzelm@4285
   675
    (*instantiate types first!*)
wenzelm@4285
   676
    val thm' =
wenzelm@4285
   677
      if forall is_none cTs then thm
wenzelm@4285
   678
      else Thm.instantiate (zip_vars (map fst (tvars_of thm)) cTs, []) thm;
wenzelm@4285
   679
    in
wenzelm@4285
   680
      if forall is_none cts then thm'
wenzelm@4285
   681
      else Thm.instantiate ([], map inst_of (zip_vars (vars_of thm') cts)) thm'
wenzelm@4285
   682
    end;
wenzelm@4285
   683
wenzelm@4285
   684
wenzelm@5688
   685
(* unvarify(T) *)
wenzelm@5688
   686
wenzelm@5688
   687
(*assume thm in standard form, i.e. no frees, 0 var indexes*)
wenzelm@5688
   688
wenzelm@5688
   689
fun unvarifyT thm =
wenzelm@5688
   690
  let
wenzelm@5688
   691
    val cT = Thm.ctyp_of (Thm.sign_of_thm thm);
wenzelm@5688
   692
    val tfrees = map (fn ((x, _), S) => Some (cT (TFree (x, S)))) (tvars_of thm);
wenzelm@5688
   693
  in instantiate' tfrees [] thm end;
wenzelm@5688
   694
wenzelm@5688
   695
fun unvarify raw_thm =
wenzelm@5688
   696
  let
wenzelm@5688
   697
    val thm = unvarifyT raw_thm;
wenzelm@5688
   698
    val ct = Thm.cterm_of (Thm.sign_of_thm thm);
wenzelm@5688
   699
    val frees = map (fn ((x, _), T) => Some (ct (Free (x, T)))) (vars_of thm);
wenzelm@5688
   700
  in instantiate' [] frees thm end;
wenzelm@5688
   701
wenzelm@5688
   702
wenzelm@8605
   703
(* tvars_intr_list *)
wenzelm@8605
   704
wenzelm@8605
   705
fun tfrees_of thm =
wenzelm@8605
   706
  let val {hyps, prop, ...} = Thm.rep_thm thm
wenzelm@8605
   707
  in foldr Term.add_term_tfree_names (prop :: hyps, []) end;
wenzelm@8605
   708
wenzelm@8605
   709
fun tvars_intr_list tfrees thm =
wenzelm@8605
   710
  Thm.varifyT' (tfrees_of thm \\ tfrees) thm;
wenzelm@8605
   711
wenzelm@8605
   712
wenzelm@6435
   713
(* increment var indexes *)
wenzelm@6435
   714
wenzelm@6435
   715
fun incr_indexes 0 thm = thm
wenzelm@6435
   716
  | incr_indexes inc thm =
wenzelm@6435
   717
      let
wenzelm@6435
   718
        val sign = Thm.sign_of_thm thm;
wenzelm@6435
   719
wenzelm@6435
   720
        fun inc_tvar ((x, i), S) = Some (Thm.ctyp_of sign (TVar ((x, i + inc), S)));
wenzelm@6435
   721
        fun inc_var ((x, i), T) = Some (Thm.cterm_of sign (Var ((x, i + inc), T)));
wenzelm@6930
   722
        val thm' = instantiate' (map inc_tvar (tvars_of thm)) [] thm;
wenzelm@6930
   723
        val thm'' = instantiate' [] (map inc_var (vars_of thm')) thm';
wenzelm@6930
   724
      in thm'' end;
wenzelm@6435
   725
wenzelm@6435
   726
fun incr_indexes_wrt is cTs cts thms =
wenzelm@6435
   727
  let
wenzelm@6435
   728
    val maxidx =
wenzelm@6435
   729
      foldl Int.max (~1, is @
wenzelm@6435
   730
        map (maxidx_of_typ o #T o Thm.rep_ctyp) cTs @
wenzelm@6435
   731
        map (#maxidx o Thm.rep_cterm) cts @
wenzelm@6435
   732
        map (#maxidx o Thm.rep_thm) thms);
wenzelm@6435
   733
  in incr_indexes (maxidx + 1) end;
wenzelm@6435
   734
wenzelm@6435
   735
wenzelm@8328
   736
(* freeze_all *)
wenzelm@8328
   737
wenzelm@8328
   738
(*freeze all (T)Vars; assumes thm in standard form*)
wenzelm@8328
   739
wenzelm@8328
   740
fun freeze_all_TVars thm =
wenzelm@8328
   741
  (case tvars_of thm of
wenzelm@8328
   742
    [] => thm
wenzelm@8328
   743
  | tvars =>
wenzelm@8328
   744
      let val cert = Thm.ctyp_of (Thm.sign_of_thm thm)
wenzelm@8328
   745
      in instantiate' (map (fn ((x, _), S) => Some (cert (TFree (x, S)))) tvars) [] thm end);
wenzelm@8328
   746
wenzelm@8328
   747
fun freeze_all_Vars thm =
wenzelm@8328
   748
  (case vars_of thm of
wenzelm@8328
   749
    [] => thm
wenzelm@8328
   750
  | vars =>
wenzelm@8328
   751
      let val cert = Thm.cterm_of (Thm.sign_of_thm thm)
wenzelm@8328
   752
      in instantiate' [] (map (fn ((x, _), T) => Some (cert (Free (x, T)))) vars) thm end);
wenzelm@8328
   753
wenzelm@8328
   754
val freeze_all = freeze_all_Vars o freeze_all_TVars;
wenzelm@8328
   755
wenzelm@8328
   756
wenzelm@5688
   757
(* mk_triv_goal *)
wenzelm@5688
   758
wenzelm@5688
   759
(*make an initial proof state, "PROP A ==> (PROP A)" *)
paulson@5311
   760
fun mk_triv_goal ct = instantiate' [] [Some ct] triv_goal;
paulson@5311
   761
wenzelm@5688
   762
wenzelm@6086
   763
wenzelm@6086
   764
(** basic attributes **)
wenzelm@6086
   765
wenzelm@6086
   766
(* dependent rules *)
wenzelm@6086
   767
wenzelm@6086
   768
fun rule_attribute f (x, thm) = (x, (f x thm));
wenzelm@6086
   769
wenzelm@6086
   770
wenzelm@6086
   771
(* add / delete tags *)
wenzelm@6086
   772
wenzelm@6086
   773
fun map_tags f thm =
wenzelm@6086
   774
  Thm.put_name_tags (Thm.name_of_thm thm, f (#2 (Thm.get_name_tags thm))) thm;
wenzelm@6086
   775
wenzelm@8365
   776
fun tag_rule tg = map_tags (fn tgs => if tg mem tgs then tgs else tgs @ [tg]);
wenzelm@8496
   777
fun untag_rule s = map_tags (filter_out (equal s o #1));
wenzelm@8365
   778
wenzelm@8365
   779
fun tag tg x = rule_attribute (K (tag_rule tg)) x;
wenzelm@8496
   780
fun untag s x = rule_attribute (K (untag_rule s)) x;
wenzelm@6086
   781
wenzelm@6086
   782
fun simple_tag name x = tag (name, []) x;
wenzelm@6086
   783
wenzelm@6086
   784
fun tag_lemma x = simple_tag "lemma" x;
wenzelm@6086
   785
fun tag_assumption x = simple_tag "assumption" x;
wenzelm@6086
   786
fun tag_internal x = simple_tag "internal" x;
wenzelm@6086
   787
wenzelm@6086
   788
clasohm@0
   789
end;
wenzelm@252
   790
wenzelm@5903
   791
wenzelm@5903
   792
structure BasicDrule: BASIC_DRULE = Drule;
wenzelm@5903
   793
open BasicDrule;