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