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