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