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