src/Pure/drule.ML
author wenzelm
Fri Oct 24 17:13:21 1997 +0200 (1997-10-24 ago)
changeset 3991 4cb2f2422695
parent 3766 8e1794c4e81b
child 4016 90aebb69c04e
permissions -rw-r--r--
ProtoPure.thy;
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
clasohm@0
    11
signature DRULE =
wenzelm@3766
    12
sig
clasohm@1460
    13
  val asm_rl		: thm
clasohm@1460
    14
  val assume_ax		: theory -> string -> thm
clasohm@1460
    15
  val COMP		: thm * thm -> thm
clasohm@1460
    16
  val compose		: thm * int * thm -> thm list
clasohm@1460
    17
  val cprems_of		: thm -> cterm list
clasohm@1460
    18
  val cterm_instantiate	: (cterm*cterm)list -> thm -> thm
clasohm@1460
    19
  val cut_rl		: thm
clasohm@1460
    20
  val equal_abs_elim	: cterm  -> thm -> thm
lcp@229
    21
  val equal_abs_elim_list: cterm list -> thm -> thm
nipkow@3653
    22
  val equal_intr_rule   : thm
clasohm@1460
    23
  val eq_thm		: thm * thm -> bool
clasohm@1460
    24
  val same_thm		: thm * thm -> bool
clasohm@1460
    25
  val eq_thm_sg		: thm * thm -> bool
lcp@229
    26
  val flexpair_abs_elim_list: cterm list -> thm -> thm
clasohm@1460
    27
  val forall_intr_list	: cterm list -> thm -> thm
clasohm@1460
    28
  val forall_intr_frees	: thm -> thm
clasohm@1460
    29
  val forall_intr_vars	: thm -> thm
clasohm@1460
    30
  val forall_elim_list	: cterm list -> thm -> thm
clasohm@1460
    31
  val forall_elim_var	: int -> thm -> thm
clasohm@1460
    32
  val forall_elim_vars	: int -> thm -> thm
clasohm@1460
    33
  val implies_elim_list	: thm -> thm list -> thm
clasohm@1460
    34
  val implies_intr_list	: cterm list -> thm -> thm
paulson@2004
    35
  val dest_implies      : cterm -> cterm * cterm
clasohm@1460
    36
  val MRL		: thm list list * thm list -> thm list
clasohm@1460
    37
  val MRS		: thm list * thm -> thm
clasohm@1460
    38
  val read_instantiate	: (string*string)list -> thm -> thm
clasohm@0
    39
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
clasohm@1460
    40
  val read_insts	:
lcp@229
    41
          Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
lcp@229
    42
                  -> (indexname -> typ option) * (indexname -> sort option)
nipkow@949
    43
                  -> string list -> (string*string)list
lcp@229
    44
                  -> (indexname*ctyp)list * (cterm*cterm)list
clasohm@1460
    45
  val reflexive_thm	: thm
paulson@2004
    46
  val refl_implies      : thm
clasohm@1460
    47
  val revcut_rl		: thm
wenzelm@3555
    48
  val rewrite_goal_rule	: bool * bool -> (meta_simpset -> thm -> thm option)
nipkow@214
    49
        -> meta_simpset -> int -> thm -> thm
wenzelm@3575
    50
  val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
wenzelm@3575
    51
  val rewrite_rule_aux	: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
wenzelm@3555
    52
  val rewrite_thm	: bool * bool -> (meta_simpset -> thm -> thm option)
wenzelm@3555
    53
	-> meta_simpset -> thm -> thm
clasohm@1460
    54
  val RS		: thm * thm -> thm
clasohm@1460
    55
  val RSN		: thm * (int * thm) -> thm
clasohm@1460
    56
  val RL		: thm list * thm list -> thm list
clasohm@1460
    57
  val RLN		: thm list * (int * thm list) -> thm list
clasohm@1460
    58
  val size_of_thm	: thm -> int
paulson@2004
    59
  val skip_flexpairs	: cterm -> cterm
clasohm@1460
    60
  val standard		: thm -> thm
paulson@2004
    61
  val strip_imp_prems	: cterm -> cterm list
nipkow@1756
    62
  val swap_prems_rl     : thm
clasohm@1460
    63
  val symmetric_thm	: thm
clasohm@1460
    64
  val thin_rl		: thm
clasohm@1460
    65
  val transitive_thm	: thm
clasohm@0
    66
  val triv_forall_equality: thm
clasohm@0
    67
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
clasohm@1460
    68
  val zero_var_indexes	: thm -> thm
wenzelm@3766
    69
end;
clasohm@0
    70
paulson@1499
    71
structure Drule : DRULE =
clasohm@0
    72
struct
clasohm@0
    73
wenzelm@3991
    74
lcp@708
    75
(** some cterm->cterm operations: much faster than calling cterm_of! **)
lcp@708
    76
paulson@2004
    77
(** SAME NAMES as in structure Logic: use compound identifiers! **)
paulson@2004
    78
clasohm@1703
    79
(*dest_implies for cterms. Note T=prop below*)
paulson@2004
    80
fun dest_implies ct =
paulson@2004
    81
    case term_of ct of 
paulson@2004
    82
	(Const("==>", _) $ _ $ _) => 
paulson@2004
    83
	    let val (ct1,ct2) = dest_comb ct
paulson@2004
    84
	    in  (#2 (dest_comb ct1), ct2)  end	     
paulson@2004
    85
      | _ => raise TERM ("dest_implies", [term_of ct]) ;
clasohm@1703
    86
clasohm@1703
    87
lcp@708
    88
(*Discard flexflex pairs; return a cterm*)
paulson@2004
    89
fun skip_flexpairs ct =
lcp@708
    90
    case term_of ct of
clasohm@1460
    91
	(Const("==>", _) $ (Const("=?=",_)$_$_) $ _) =>
paulson@2004
    92
	    skip_flexpairs (#2 (dest_implies ct))
lcp@708
    93
      | _ => ct;
lcp@708
    94
lcp@708
    95
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
paulson@2004
    96
fun strip_imp_prems ct =
paulson@2004
    97
    let val (cA,cB) = dest_implies ct
paulson@2004
    98
    in  cA :: strip_imp_prems cB  end
lcp@708
    99
    handle TERM _ => [];
lcp@708
   100
paulson@2004
   101
(* A1==>...An==>B  goes to B, where B is not an implication *)
paulson@2004
   102
fun strip_imp_concl ct =
paulson@2004
   103
    case term_of ct of (Const("==>", _) $ _ $ _) => 
paulson@2004
   104
	strip_imp_concl (#2 (dest_comb ct))
paulson@2004
   105
  | _ => ct;
paulson@2004
   106
lcp@708
   107
(*The premises of a theorem, as a cterm list*)
paulson@2004
   108
val cprems_of = strip_imp_prems o skip_flexpairs o cprop_of;
lcp@708
   109
lcp@708
   110
lcp@229
   111
(** reading of instantiations **)
lcp@229
   112
lcp@229
   113
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
lcp@229
   114
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
lcp@229
   115
lcp@229
   116
fun absent ixn =
lcp@229
   117
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
lcp@229
   118
lcp@229
   119
fun inst_failure ixn =
lcp@229
   120
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
lcp@229
   121
nipkow@952
   122
(* this code is a bit of a mess. add_cterm could be simplified greatly if
nipkow@952
   123
   simultaneous instantiations were read or at least type checked
nipkow@952
   124
   simultaneously rather than one after the other. This would make the tricky
nipkow@952
   125
   composition of implicit type instantiations (parameter tye) superfluous.
nipkow@952
   126
*)
nipkow@949
   127
fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
lcp@229
   128
let val {tsig,...} = Sign.rep_sg sign
lcp@229
   129
    fun split([],tvs,vs) = (tvs,vs)
lcp@229
   130
      | split((sv,st)::l,tvs,vs) = (case explode sv of
lcp@229
   131
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
lcp@229
   132
                | cs => split(l,tvs,(indexname cs,st)::vs));
lcp@229
   133
    val (tvs,vs) = split(insts,[],[]);
lcp@229
   134
    fun readT((a,i),st) =
lcp@229
   135
        let val ixn = ("'" ^ a,i);
lcp@229
   136
            val S = case rsorts ixn of Some S => S | None => absent ixn;
lcp@229
   137
            val T = Sign.read_typ (sign,sorts) st;
lcp@229
   138
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
lcp@229
   139
           else inst_failure ixn
lcp@229
   140
        end
lcp@229
   141
    val tye = map readT tvs;
nipkow@949
   142
    fun add_cterm ((cts,tye,used), (ixn,st)) =
lcp@229
   143
        let val T = case rtypes ixn of
lcp@229
   144
                      Some T => typ_subst_TVars tye T
lcp@229
   145
                    | None => absent ixn;
nipkow@949
   146
            val (ct,tye2) = read_def_cterm(sign,types,sorts) used false (st,T);
nipkow@952
   147
            val cts' = (ixn,T,ct)::cts
nipkow@952
   148
            fun inst(ixn,T,ct) = (ixn,typ_subst_TVars tye2 T,ct)
nipkow@949
   149
            val used' = add_term_tvarnames(term_of ct,used);
nipkow@952
   150
        in (map inst cts',tye2 @ tye,used') end
nipkow@949
   151
    val (cterms,tye',_) = foldl add_cterm (([],tye,used), vs);
nipkow@952
   152
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye',
nipkow@952
   153
    map (fn (ixn,T,ct) => (cterm_of sign (Var(ixn,T)), ct)) cterms)
nipkow@952
   154
end;
lcp@229
   155
lcp@229
   156
wenzelm@252
   157
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   158
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   159
     type variables) when reading another term.
clasohm@0
   160
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   161
***)
clasohm@0
   162
clasohm@0
   163
fun types_sorts thm =
clasohm@0
   164
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   165
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   166
        val vars = map dest_Var (term_vars big);
wenzelm@252
   167
        val frees = map dest_Free (term_frees big);
wenzelm@252
   168
        val tvars = term_tvars big;
wenzelm@252
   169
        val tfrees = term_tfrees big;
wenzelm@252
   170
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   171
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   172
    in (typ,sort) end;
clasohm@0
   173
clasohm@0
   174
(** Standardization of rules **)
clasohm@0
   175
clasohm@0
   176
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   177
fun forall_intr_list [] th = th
clasohm@0
   178
  | forall_intr_list (y::ys) th =
wenzelm@252
   179
        let val gth = forall_intr_list ys th
wenzelm@252
   180
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   181
clasohm@0
   182
(*Generalization over all suitable Free variables*)
clasohm@0
   183
fun forall_intr_frees th =
clasohm@0
   184
    let val {prop,sign,...} = rep_thm th
clasohm@0
   185
    in  forall_intr_list
wenzelm@252
   186
         (map (cterm_of sign) (sort atless (term_frees prop)))
clasohm@0
   187
         th
clasohm@0
   188
    end;
clasohm@0
   189
clasohm@0
   190
(*Replace outermost quantified variable by Var of given index.
clasohm@0
   191
    Could clash with Vars already present.*)
wenzelm@252
   192
fun forall_elim_var i th =
clasohm@0
   193
    let val {prop,sign,...} = rep_thm th
clasohm@0
   194
    in case prop of
wenzelm@252
   195
          Const("all",_) $ Abs(a,T,_) =>
wenzelm@252
   196
              forall_elim (cterm_of sign (Var((a,i), T)))  th
wenzelm@252
   197
        | _ => raise THM("forall_elim_var", i, [th])
clasohm@0
   198
    end;
clasohm@0
   199
clasohm@0
   200
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@252
   201
fun forall_elim_vars i th =
clasohm@0
   202
    forall_elim_vars i (forall_elim_var i th)
wenzelm@252
   203
        handle THM _ => th;
clasohm@0
   204
clasohm@0
   205
(*Specialization over a list of cterms*)
clasohm@0
   206
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   207
clasohm@0
   208
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   209
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   210
clasohm@0
   211
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   212
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   213
clasohm@0
   214
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   215
fun zero_var_indexes th =
clasohm@0
   216
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   217
        val vars = term_vars prop
clasohm@0
   218
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   219
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   220
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
paulson@2266
   221
        val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
paulson@2266
   222
	             (inrs, nms')
wenzelm@252
   223
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   224
        fun varpairs([],[]) = []
wenzelm@252
   225
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   226
                let val T' = typ_subst_TVars tye T
wenzelm@252
   227
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   228
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   229
                end
wenzelm@252
   230
          | varpairs _ = raise TERM("varpairs", []);
clasohm@0
   231
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   232
clasohm@0
   233
clasohm@0
   234
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   235
    all generality expressed by Vars having index 0.*)
clasohm@0
   236
fun standard th =
wenzelm@1218
   237
  let val {maxidx,...} = rep_thm th
wenzelm@1237
   238
  in
wenzelm@1218
   239
    th |> implies_intr_hyps
paulson@1412
   240
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
wenzelm@1439
   241
       |> Thm.strip_shyps |> Thm.implies_intr_shyps
paulson@1412
   242
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
wenzelm@1218
   243
  end;
wenzelm@1218
   244
clasohm@0
   245
wenzelm@252
   246
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   247
  Generalizes over Free variables,
clasohm@0
   248
  creates the assumption, and then strips quantifiers.
clasohm@0
   249
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   250
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   251
fun assume_ax thy sP =
clasohm@0
   252
    let val sign = sign_of thy
wenzelm@252
   253
        val prop = Logic.close_form (term_of (read_cterm sign
wenzelm@252
   254
                         (sP, propT)))
lcp@229
   255
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   256
wenzelm@252
   257
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   258
fun tha RSN (i,thb) =
clasohm@0
   259
  case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   260
      ([th],_) => th
clasohm@0
   261
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   262
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   263
clasohm@0
   264
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   265
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   266
clasohm@0
   267
(*For joining lists of rules*)
wenzelm@252
   268
fun thas RLN (i,thbs) =
clasohm@0
   269
  let val resolve = biresolution false (map (pair false) thas) i
clasohm@0
   270
      fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
paulson@2672
   271
  in  List.concat (map resb thbs)  end;
clasohm@0
   272
clasohm@0
   273
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   274
lcp@11
   275
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   276
  makes proof trees*)
wenzelm@252
   277
fun rls MRS bottom_rl =
lcp@11
   278
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   279
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   280
  in  rs_aux 1 rls  end;
lcp@11
   281
lcp@11
   282
(*As above, but for rule lists*)
wenzelm@252
   283
fun rlss MRL bottom_rls =
lcp@11
   284
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   285
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   286
  in  rs_aux 1 rlss  end;
lcp@11
   287
wenzelm@252
   288
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   289
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   290
  ALWAYS deletes premise i *)
wenzelm@252
   291
fun compose(tha,i,thb) =
clasohm@0
   292
    Sequence.list_of_s (bicompose false (false,tha,0) i thb);
clasohm@0
   293
clasohm@0
   294
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   295
fun tha COMP thb =
clasohm@0
   296
    case compose(tha,1,thb) of
wenzelm@252
   297
        [th] => th
clasohm@0
   298
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   299
clasohm@0
   300
(*Instantiate theorem th, reading instantiations under signature sg*)
clasohm@0
   301
fun read_instantiate_sg sg sinsts th =
clasohm@0
   302
    let val ts = types_sorts th;
nipkow@952
   303
        val used = add_term_tvarnames(#prop(rep_thm th),[]);
nipkow@952
   304
    in  instantiate (read_insts sg ts ts used sinsts) th  end;
clasohm@0
   305
clasohm@0
   306
(*Instantiate theorem th, reading instantiations under theory of th*)
clasohm@0
   307
fun read_instantiate sinsts th =
clasohm@0
   308
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
clasohm@0
   309
clasohm@0
   310
clasohm@0
   311
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
clasohm@0
   312
  Instantiates distinct Vars by terms, inferring type instantiations. *)
clasohm@0
   313
local
nipkow@1435
   314
  fun add_types ((ct,cu), (sign,tye,maxidx)) =
paulson@2152
   315
    let val {sign=signt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
paulson@2152
   316
        and {sign=signu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
paulson@2152
   317
        val maxi = Int.max(maxidx, Int.max(maxt, maxu));
clasohm@0
   318
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
nipkow@1435
   319
        val (tye',maxi') = Type.unify (#tsig(Sign.rep_sg sign')) maxi tye (T,U)
wenzelm@252
   320
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
nipkow@1435
   321
    in  (sign', tye', maxi')  end;
clasohm@0
   322
in
wenzelm@252
   323
fun cterm_instantiate ctpairs0 th =
nipkow@1435
   324
  let val (sign,tye,_) = foldr add_types (ctpairs0, (#sign(rep_thm th),[],0))
clasohm@0
   325
      val tsig = #tsig(Sign.rep_sg sign);
clasohm@0
   326
      fun instT(ct,cu) = let val inst = subst_TVars tye
wenzelm@252
   327
                         in (cterm_fun inst ct, cterm_fun inst cu) end
lcp@229
   328
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
clasohm@0
   329
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
wenzelm@252
   330
  handle TERM _ =>
clasohm@0
   331
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
clasohm@0
   332
       | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
clasohm@0
   333
end;
clasohm@0
   334
clasohm@0
   335
clasohm@0
   336
(** theorem equality test is exported and used by BEST_FIRST **)
clasohm@0
   337
wenzelm@252
   338
(*equality of theorems uses equality of signatures and
clasohm@0
   339
  the a-convertible test for terms*)
wenzelm@252
   340
fun eq_thm (th1,th2) =
wenzelm@1218
   341
    let val {sign=sg1, shyps=shyps1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
wenzelm@1218
   342
        and {sign=sg2, shyps=shyps2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
wenzelm@252
   343
    in  Sign.eq_sg (sg1,sg2) andalso
paulson@2180
   344
        eq_set_sort (shyps1, shyps2) andalso
wenzelm@252
   345
        aconvs(hyps1,hyps2) andalso
wenzelm@252
   346
        prop1 aconv prop2
clasohm@0
   347
    end;
clasohm@0
   348
clasohm@1241
   349
(*equality of theorems using similarity of signatures,
clasohm@1241
   350
  i.e. the theorems belong to the same theory but not necessarily to the same
clasohm@1241
   351
  version of this theory*)
clasohm@1241
   352
fun same_thm (th1,th2) =
clasohm@1241
   353
    let val {sign=sg1, shyps=shyps1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
clasohm@1241
   354
        and {sign=sg2, shyps=shyps2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
clasohm@1241
   355
    in  Sign.same_sg (sg1,sg2) andalso
paulson@2180
   356
        eq_set_sort (shyps1, shyps2) andalso
clasohm@1241
   357
        aconvs(hyps1,hyps2) andalso
clasohm@1241
   358
        prop1 aconv prop2
clasohm@1241
   359
    end;
clasohm@1241
   360
clasohm@0
   361
(*Do the two theorems have the same signature?*)
wenzelm@252
   362
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
clasohm@0
   363
clasohm@0
   364
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   365
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   366
clasohm@0
   367
lcp@1194
   368
(** Mark Staples's weaker version of eq_thm: ignores variable renaming and
lcp@1194
   369
    (some) type variable renaming **)
lcp@1194
   370
lcp@1194
   371
 (* Can't use term_vars, because it sorts the resulting list of variable names.
lcp@1194
   372
    We instead need the unique list noramlised by the order of appearance
lcp@1194
   373
    in the term. *)
lcp@1194
   374
fun term_vars' (t as Var(v,T)) = [t]
lcp@1194
   375
  | term_vars' (Abs(_,_,b)) = term_vars' b
lcp@1194
   376
  | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
lcp@1194
   377
  | term_vars' _ = [];
lcp@1194
   378
lcp@1194
   379
fun forall_intr_vars th =
lcp@1194
   380
  let val {prop,sign,...} = rep_thm th;
lcp@1194
   381
      val vars = distinct (term_vars' prop);
lcp@1194
   382
  in forall_intr_list (map (cterm_of sign) vars) th end;
lcp@1194
   383
wenzelm@1237
   384
fun weak_eq_thm (tha,thb) =
lcp@1194
   385
    eq_thm(forall_intr_vars (freezeT tha), forall_intr_vars (freezeT thb));
lcp@1194
   386
lcp@1194
   387
lcp@1194
   388
clasohm@0
   389
(*** Meta-Rewriting Rules ***)
clasohm@0
   390
clasohm@0
   391
val reflexive_thm =
wenzelm@3991
   392
  let val cx = cterm_of (sign_of ProtoPure.thy) (Var(("x",0),TVar(("'a",0),logicS)))
clasohm@0
   393
  in Thm.reflexive cx end;
clasohm@0
   394
clasohm@0
   395
val symmetric_thm =
wenzelm@3991
   396
  let val xy = read_cterm (sign_of ProtoPure.thy) ("x::'a::logic == y",propT)
clasohm@0
   397
  in standard(Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
clasohm@0
   398
clasohm@0
   399
val transitive_thm =
wenzelm@3991
   400
  let val xy = read_cterm (sign_of ProtoPure.thy) ("x::'a::logic == y",propT)
wenzelm@3991
   401
      val yz = read_cterm (sign_of ProtoPure.thy) ("y::'a::logic == z",propT)
clasohm@0
   402
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
clasohm@0
   403
  in standard(Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
clasohm@0
   404
lcp@229
   405
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   406
wenzelm@3991
   407
val refl_implies = reflexive (cterm_of (sign_of ProtoPure.thy) implies);
clasohm@0
   408
clasohm@0
   409
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   410
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   411
fun goals_conv pred cv =
lcp@229
   412
  let fun gconv i ct =
paulson@2004
   413
        let val (A,B) = dest_implies ct
lcp@229
   414
            val (thA,j) = case term_of A of
lcp@229
   415
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   416
                | _ => (if pred i then cv A else reflexive A, i+1)
paulson@2004
   417
        in  combination (combination refl_implies thA) (gconv j B) end
lcp@229
   418
        handle TERM _ => reflexive ct
clasohm@0
   419
  in gconv 1 end;
clasohm@0
   420
clasohm@0
   421
(*Use a conversion to transform a theorem*)
lcp@229
   422
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   423
clasohm@0
   424
(*rewriting conversion*)
lcp@229
   425
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   426
clasohm@0
   427
(*Rewrite a theorem*)
wenzelm@3575
   428
fun rewrite_rule_aux _ []   th = th
wenzelm@3575
   429
  | rewrite_rule_aux prover thms th =
wenzelm@3575
   430
      fconv_rule (rew_conv (true,false) prover (Thm.mss_of thms)) th;
clasohm@0
   431
wenzelm@3555
   432
fun rewrite_thm mode prover mss = fconv_rule (rew_conv mode prover mss);
wenzelm@3555
   433
clasohm@0
   434
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
wenzelm@3575
   435
fun rewrite_goals_rule_aux _ []   th = th
wenzelm@3575
   436
  | rewrite_goals_rule_aux prover thms th =
wenzelm@3575
   437
      fconv_rule (goals_conv (K true) (rew_conv (true, true) prover
wenzelm@3575
   438
        (Thm.mss_of thms))) th;
clasohm@0
   439
clasohm@0
   440
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   441
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   442
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   443
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   444
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   445
clasohm@0
   446
clasohm@0
   447
(** Derived rules mainly for METAHYPS **)
clasohm@0
   448
clasohm@0
   449
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
clasohm@0
   450
fun equal_abs_elim ca eqth =
lcp@229
   451
  let val {sign=signa, t=a, ...} = rep_cterm ca
clasohm@0
   452
      and combth = combination eqth (reflexive ca)
clasohm@0
   453
      val {sign,prop,...} = rep_thm eqth
clasohm@0
   454
      val (abst,absu) = Logic.dest_equals prop
lcp@229
   455
      val cterm = cterm_of (Sign.merge (sign,signa))
clasohm@0
   456
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
clasohm@0
   457
           (transitive combth (beta_conversion (cterm (absu$a))))
clasohm@0
   458
  end
clasohm@0
   459
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
clasohm@0
   460
clasohm@0
   461
(*Calling equal_abs_elim with multiple terms*)
clasohm@0
   462
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
clasohm@0
   463
clasohm@0
   464
local
clasohm@0
   465
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
clasohm@0
   466
  fun err th = raise THM("flexpair_inst: ", 0, [th])
clasohm@0
   467
  fun flexpair_inst def th =
clasohm@0
   468
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
wenzelm@252
   469
        val cterm = cterm_of sign
wenzelm@252
   470
        fun cvar a = cterm(Var((a,0),alpha))
wenzelm@252
   471
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
wenzelm@252
   472
                   def
clasohm@0
   473
    in  equal_elim def' th
clasohm@0
   474
    end
clasohm@0
   475
    handle THM _ => err th | bind => err th
clasohm@0
   476
in
wenzelm@3991
   477
val flexpair_intr = flexpair_inst (symmetric ProtoPure.flexpair_def)
wenzelm@3991
   478
and flexpair_elim = flexpair_inst ProtoPure.flexpair_def
clasohm@0
   479
end;
clasohm@0
   480
clasohm@0
   481
(*Version for flexflex pairs -- this supports lifting.*)
wenzelm@252
   482
fun flexpair_abs_elim_list cts =
clasohm@0
   483
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
clasohm@0
   484
clasohm@0
   485
clasohm@0
   486
(*** Some useful meta-theorems ***)
clasohm@0
   487
clasohm@0
   488
(*The rule V/V, obtains assumption solving for eresolve_tac*)
wenzelm@3991
   489
val asm_rl = trivial(read_cterm (sign_of ProtoPure.thy) ("PROP ?psi",propT));
clasohm@0
   490
clasohm@0
   491
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@3991
   492
val cut_rl = trivial(read_cterm (sign_of ProtoPure.thy)
wenzelm@252
   493
        ("PROP ?psi ==> PROP ?theta", propT));
clasohm@0
   494
wenzelm@252
   495
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   496
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   497
val revcut_rl =
wenzelm@3991
   498
  let val V = read_cterm (sign_of ProtoPure.thy) ("PROP V", propT)
wenzelm@3991
   499
      and VW = read_cterm (sign_of ProtoPure.thy) ("PROP V ==> PROP W", propT);
wenzelm@252
   500
  in  standard (implies_intr V
wenzelm@252
   501
                (implies_intr VW
wenzelm@252
   502
                 (implies_elim (assume VW) (assume V))))
clasohm@0
   503
  end;
clasohm@0
   504
lcp@668
   505
(*for deleting an unwanted assumption*)
lcp@668
   506
val thin_rl =
wenzelm@3991
   507
  let val V = read_cterm (sign_of ProtoPure.thy) ("PROP V", propT)
wenzelm@3991
   508
      and W = read_cterm (sign_of ProtoPure.thy) ("PROP W", propT);
lcp@668
   509
  in  standard (implies_intr V (implies_intr W (assume W)))
lcp@668
   510
  end;
lcp@668
   511
clasohm@0
   512
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   513
val triv_forall_equality =
wenzelm@3991
   514
  let val V  = read_cterm (sign_of ProtoPure.thy) ("PROP V", propT)
wenzelm@3991
   515
      and QV = read_cterm (sign_of ProtoPure.thy) ("!!x::'a. PROP V", propT)
wenzelm@3991
   516
      and x  = read_cterm (sign_of ProtoPure.thy) ("x", TFree("'a",logicS));
clasohm@0
   517
  in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@252
   518
                           (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   519
  end;
clasohm@0
   520
nipkow@1756
   521
(* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
nipkow@1756
   522
   (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
nipkow@1756
   523
   `thm COMP swap_prems_rl' swaps the first two premises of `thm'
nipkow@1756
   524
*)
nipkow@1756
   525
val swap_prems_rl =
wenzelm@3991
   526
  let val cmajor = read_cterm (sign_of ProtoPure.thy)
nipkow@1756
   527
            ("PROP PhiA ==> PROP PhiB ==> PROP Psi", propT);
nipkow@1756
   528
      val major = assume cmajor;
wenzelm@3991
   529
      val cminor1 = read_cterm (sign_of ProtoPure.thy)  ("PROP PhiA", propT);
nipkow@1756
   530
      val minor1 = assume cminor1;
wenzelm@3991
   531
      val cminor2 = read_cterm (sign_of ProtoPure.thy)  ("PROP PhiB", propT);
nipkow@1756
   532
      val minor2 = assume cminor2;
nipkow@1756
   533
  in standard
nipkow@1756
   534
       (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
nipkow@1756
   535
         (implies_elim (implies_elim major minor1) minor2))))
nipkow@1756
   536
  end;
nipkow@1756
   537
nipkow@3653
   538
(* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
nipkow@3653
   539
   ==> PROP ?phi == PROP ?psi
nipkow@3653
   540
   Introduction rule for == using ==> not meta-hyps.
nipkow@3653
   541
*)
nipkow@3653
   542
val equal_intr_rule =
wenzelm@3991
   543
  let val PQ = read_cterm (sign_of ProtoPure.thy) ("PROP phi ==> PROP psi", propT)
wenzelm@3991
   544
      and QP = read_cterm (sign_of ProtoPure.thy) ("PROP psi ==> PROP phi", propT)
nipkow@3653
   545
  in  equal_intr (assume PQ) (assume QP)
nipkow@3653
   546
      |> implies_intr QP
nipkow@3653
   547
      |> implies_intr PQ
nipkow@3653
   548
      |> standard
nipkow@3653
   549
  end;
nipkow@3653
   550
clasohm@0
   551
end;
wenzelm@252
   552
paulson@1499
   553
open Drule;