src/Pure/drule.ML
author nipkow
Mon Nov 24 16:43:43 1997 +0100 (1997-11-24)
changeset 4281 6c6073b13600
parent 4270 957c887b89b5
child 4285 05af145a61d4
permissions -rw-r--r--
Added read_def_cterms for simultaneous reading/typing of terms under
defaults.
Redefined read_def_cterm in in terms of read_def_cterms.
Deleted obsolete read_cterms.

Cleaned up def of read_insts, which is not much shorter but much clearere are
correcter now.
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@4281
   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;
nipkow@4281
   155
*)
nipkow@4281
   156
nipkow@4281
   157
fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
nipkow@4281
   158
let val {tsig,...} = Sign.rep_sg sign
nipkow@4281
   159
    fun split([],tvs,vs) = (tvs,vs)
nipkow@4281
   160
      | split((sv,st)::l,tvs,vs) = (case explode sv of
nipkow@4281
   161
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
nipkow@4281
   162
                | cs => split(l,tvs,(indexname cs,st)::vs));
nipkow@4281
   163
    val (tvs,vs) = split(insts,[],[]);
nipkow@4281
   164
    fun readT((a,i),st) =
nipkow@4281
   165
        let val ixn = ("'" ^ a,i);
nipkow@4281
   166
            val S = case rsorts ixn of Some S => S | None => absent ixn;
nipkow@4281
   167
            val T = Sign.read_typ (sign,sorts) st;
nipkow@4281
   168
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
nipkow@4281
   169
           else inst_failure ixn
nipkow@4281
   170
        end
nipkow@4281
   171
    val tye = map readT tvs;
nipkow@4281
   172
    fun mkty(ixn,st) = (case rtypes ixn of
nipkow@4281
   173
                          Some T => (ixn,(st,typ_subst_TVars tye T))
nipkow@4281
   174
                        | None => absent ixn);
nipkow@4281
   175
    val ixnsTs = map mkty vs;
nipkow@4281
   176
    val ixns = map fst ixnsTs
nipkow@4281
   177
    and sTs  = map snd ixnsTs
nipkow@4281
   178
    val (cts,tye2) = read_def_cterms(sign,types,sorts) used false sTs;
nipkow@4281
   179
    fun mkcVar(ixn,T) =
nipkow@4281
   180
        let val U = typ_subst_TVars tye2 T
nipkow@4281
   181
        in cterm_of sign (Var(ixn,U)) end
nipkow@4281
   182
    val ixnTs = ListPair.zip(ixns, map snd sTs)
nipkow@4281
   183
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) (tye2 @ tye),
nipkow@4281
   184
    ListPair.zip(map mkcVar ixnTs,cts))
nipkow@4281
   185
end;
lcp@229
   186
lcp@229
   187
wenzelm@252
   188
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   189
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   190
     type variables) when reading another term.
clasohm@0
   191
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   192
***)
clasohm@0
   193
clasohm@0
   194
fun types_sorts thm =
clasohm@0
   195
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   196
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   197
        val vars = map dest_Var (term_vars big);
wenzelm@252
   198
        val frees = map dest_Free (term_frees big);
wenzelm@252
   199
        val tvars = term_tvars big;
wenzelm@252
   200
        val tfrees = term_tfrees big;
wenzelm@252
   201
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   202
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   203
    in (typ,sort) end;
clasohm@0
   204
clasohm@0
   205
(** Standardization of rules **)
clasohm@0
   206
clasohm@0
   207
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   208
fun forall_intr_list [] th = th
clasohm@0
   209
  | forall_intr_list (y::ys) th =
wenzelm@252
   210
        let val gth = forall_intr_list ys th
wenzelm@252
   211
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   212
clasohm@0
   213
(*Generalization over all suitable Free variables*)
clasohm@0
   214
fun forall_intr_frees th =
clasohm@0
   215
    let val {prop,sign,...} = rep_thm th
clasohm@0
   216
    in  forall_intr_list
wenzelm@252
   217
         (map (cterm_of sign) (sort atless (term_frees prop)))
clasohm@0
   218
         th
clasohm@0
   219
    end;
clasohm@0
   220
clasohm@0
   221
(*Replace outermost quantified variable by Var of given index.
clasohm@0
   222
    Could clash with Vars already present.*)
wenzelm@252
   223
fun forall_elim_var i th =
clasohm@0
   224
    let val {prop,sign,...} = rep_thm th
clasohm@0
   225
    in case prop of
wenzelm@252
   226
          Const("all",_) $ Abs(a,T,_) =>
wenzelm@252
   227
              forall_elim (cterm_of sign (Var((a,i), T)))  th
wenzelm@252
   228
        | _ => raise THM("forall_elim_var", i, [th])
clasohm@0
   229
    end;
clasohm@0
   230
clasohm@0
   231
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@252
   232
fun forall_elim_vars i th =
clasohm@0
   233
    forall_elim_vars i (forall_elim_var i th)
wenzelm@252
   234
        handle THM _ => th;
clasohm@0
   235
clasohm@0
   236
(*Specialization over a list of cterms*)
clasohm@0
   237
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   238
clasohm@0
   239
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   240
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   241
clasohm@0
   242
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   243
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   244
clasohm@0
   245
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   246
fun zero_var_indexes th =
clasohm@0
   247
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   248
        val vars = term_vars prop
clasohm@0
   249
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   250
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   251
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
paulson@2266
   252
        val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
paulson@2266
   253
	             (inrs, nms')
wenzelm@252
   254
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   255
        fun varpairs([],[]) = []
wenzelm@252
   256
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   257
                let val T' = typ_subst_TVars tye T
wenzelm@252
   258
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   259
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   260
                end
wenzelm@252
   261
          | varpairs _ = raise TERM("varpairs", []);
clasohm@0
   262
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   263
clasohm@0
   264
clasohm@0
   265
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   266
    all generality expressed by Vars having index 0.*)
clasohm@0
   267
fun standard th =
wenzelm@1218
   268
  let val {maxidx,...} = rep_thm th
wenzelm@1237
   269
  in
wenzelm@1218
   270
    th |> implies_intr_hyps
paulson@1412
   271
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
wenzelm@1439
   272
       |> Thm.strip_shyps |> Thm.implies_intr_shyps
paulson@1412
   273
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
wenzelm@1218
   274
  end;
wenzelm@1218
   275
clasohm@0
   276
wenzelm@252
   277
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   278
  Generalizes over Free variables,
clasohm@0
   279
  creates the assumption, and then strips quantifiers.
clasohm@0
   280
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   281
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   282
fun assume_ax thy sP =
clasohm@0
   283
    let val sign = sign_of thy
wenzelm@252
   284
        val prop = Logic.close_form (term_of (read_cterm sign
wenzelm@252
   285
                         (sP, propT)))
lcp@229
   286
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   287
wenzelm@252
   288
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   289
fun tha RSN (i,thb) =
wenzelm@4270
   290
  case Seq.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   291
      ([th],_) => th
clasohm@0
   292
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   293
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   294
clasohm@0
   295
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   296
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   297
clasohm@0
   298
(*For joining lists of rules*)
wenzelm@252
   299
fun thas RLN (i,thbs) =
clasohm@0
   300
  let val resolve = biresolution false (map (pair false) thas) i
wenzelm@4270
   301
      fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
paulson@2672
   302
  in  List.concat (map resb thbs)  end;
clasohm@0
   303
clasohm@0
   304
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   305
lcp@11
   306
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   307
  makes proof trees*)
wenzelm@252
   308
fun rls MRS bottom_rl =
lcp@11
   309
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   310
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   311
  in  rs_aux 1 rls  end;
lcp@11
   312
lcp@11
   313
(*As above, but for rule lists*)
wenzelm@252
   314
fun rlss MRL bottom_rls =
lcp@11
   315
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   316
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   317
  in  rs_aux 1 rlss  end;
lcp@11
   318
wenzelm@252
   319
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   320
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   321
  ALWAYS deletes premise i *)
wenzelm@252
   322
fun compose(tha,i,thb) =
wenzelm@4270
   323
    Seq.list_of (bicompose false (false,tha,0) i thb);
clasohm@0
   324
clasohm@0
   325
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   326
fun tha COMP thb =
clasohm@0
   327
    case compose(tha,1,thb) of
wenzelm@252
   328
        [th] => th
clasohm@0
   329
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   330
clasohm@0
   331
(*Instantiate theorem th, reading instantiations under signature sg*)
clasohm@0
   332
fun read_instantiate_sg sg sinsts th =
clasohm@0
   333
    let val ts = types_sorts th;
nipkow@952
   334
        val used = add_term_tvarnames(#prop(rep_thm th),[]);
nipkow@952
   335
    in  instantiate (read_insts sg ts ts used sinsts) th  end;
clasohm@0
   336
clasohm@0
   337
(*Instantiate theorem th, reading instantiations under theory of th*)
clasohm@0
   338
fun read_instantiate sinsts th =
clasohm@0
   339
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
clasohm@0
   340
clasohm@0
   341
clasohm@0
   342
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
clasohm@0
   343
  Instantiates distinct Vars by terms, inferring type instantiations. *)
clasohm@0
   344
local
nipkow@1435
   345
  fun add_types ((ct,cu), (sign,tye,maxidx)) =
paulson@2152
   346
    let val {sign=signt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
paulson@2152
   347
        and {sign=signu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
paulson@2152
   348
        val maxi = Int.max(maxidx, Int.max(maxt, maxu));
clasohm@0
   349
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
nipkow@1435
   350
        val (tye',maxi') = Type.unify (#tsig(Sign.rep_sg sign')) maxi tye (T,U)
wenzelm@252
   351
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
nipkow@1435
   352
    in  (sign', tye', maxi')  end;
clasohm@0
   353
in
wenzelm@252
   354
fun cterm_instantiate ctpairs0 th =
nipkow@1435
   355
  let val (sign,tye,_) = foldr add_types (ctpairs0, (#sign(rep_thm th),[],0))
clasohm@0
   356
      val tsig = #tsig(Sign.rep_sg sign);
clasohm@0
   357
      fun instT(ct,cu) = let val inst = subst_TVars tye
wenzelm@252
   358
                         in (cterm_fun inst ct, cterm_fun inst cu) end
lcp@229
   359
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
clasohm@0
   360
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
wenzelm@252
   361
  handle TERM _ =>
clasohm@0
   362
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
wenzelm@4057
   363
       | TYPE (msg, _, _) => raise THM("cterm_instantiate: " ^ msg, 0, [th])
clasohm@0
   364
end;
clasohm@0
   365
clasohm@0
   366
wenzelm@4016
   367
(** theorem equality **)
clasohm@0
   368
wenzelm@4016
   369
val eq_thm = Thm.eq_thm;
clasohm@0
   370
clasohm@1241
   371
(*equality of theorems using similarity of signatures,
clasohm@1241
   372
  i.e. the theorems belong to the same theory but not necessarily to the same
clasohm@1241
   373
  version of this theory*)
clasohm@1241
   374
fun same_thm (th1,th2) =
clasohm@1241
   375
    let val {sign=sg1, shyps=shyps1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
clasohm@1241
   376
        and {sign=sg2, shyps=shyps2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
clasohm@1241
   377
    in  Sign.same_sg (sg1,sg2) andalso
paulson@2180
   378
        eq_set_sort (shyps1, shyps2) andalso
clasohm@1241
   379
        aconvs(hyps1,hyps2) andalso
clasohm@1241
   380
        prop1 aconv prop2
clasohm@1241
   381
    end;
clasohm@1241
   382
clasohm@0
   383
(*Do the two theorems have the same signature?*)
wenzelm@252
   384
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
clasohm@0
   385
clasohm@0
   386
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   387
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   388
clasohm@0
   389
lcp@1194
   390
(** Mark Staples's weaker version of eq_thm: ignores variable renaming and
lcp@1194
   391
    (some) type variable renaming **)
lcp@1194
   392
lcp@1194
   393
 (* Can't use term_vars, because it sorts the resulting list of variable names.
lcp@1194
   394
    We instead need the unique list noramlised by the order of appearance
lcp@1194
   395
    in the term. *)
lcp@1194
   396
fun term_vars' (t as Var(v,T)) = [t]
lcp@1194
   397
  | term_vars' (Abs(_,_,b)) = term_vars' b
lcp@1194
   398
  | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
lcp@1194
   399
  | term_vars' _ = [];
lcp@1194
   400
lcp@1194
   401
fun forall_intr_vars th =
lcp@1194
   402
  let val {prop,sign,...} = rep_thm th;
lcp@1194
   403
      val vars = distinct (term_vars' prop);
lcp@1194
   404
  in forall_intr_list (map (cterm_of sign) vars) th end;
lcp@1194
   405
wenzelm@1237
   406
fun weak_eq_thm (tha,thb) =
lcp@1194
   407
    eq_thm(forall_intr_vars (freezeT tha), forall_intr_vars (freezeT thb));
lcp@1194
   408
lcp@1194
   409
lcp@1194
   410
clasohm@0
   411
(*** Meta-Rewriting Rules ***)
clasohm@0
   412
wenzelm@4016
   413
fun store_thm name thm = PureThy.smart_store_thm (name, standard thm);
wenzelm@4016
   414
clasohm@0
   415
val reflexive_thm =
wenzelm@3991
   416
  let val cx = cterm_of (sign_of ProtoPure.thy) (Var(("x",0),TVar(("'a",0),logicS)))
wenzelm@4016
   417
  in store_thm "reflexive" (Thm.reflexive cx) end;
clasohm@0
   418
clasohm@0
   419
val symmetric_thm =
wenzelm@3991
   420
  let val xy = read_cterm (sign_of ProtoPure.thy) ("x::'a::logic == y",propT)
wenzelm@4016
   421
  in store_thm "symmetric" (Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
clasohm@0
   422
clasohm@0
   423
val transitive_thm =
wenzelm@3991
   424
  let val xy = read_cterm (sign_of ProtoPure.thy) ("x::'a::logic == y",propT)
wenzelm@3991
   425
      val yz = read_cterm (sign_of ProtoPure.thy) ("y::'a::logic == z",propT)
clasohm@0
   426
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
wenzelm@4016
   427
  in store_thm "transitive" (Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
clasohm@0
   428
lcp@229
   429
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   430
wenzelm@3991
   431
val refl_implies = reflexive (cterm_of (sign_of ProtoPure.thy) implies);
clasohm@0
   432
clasohm@0
   433
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   434
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   435
fun goals_conv pred cv =
lcp@229
   436
  let fun gconv i ct =
paulson@2004
   437
        let val (A,B) = dest_implies ct
lcp@229
   438
            val (thA,j) = case term_of A of
lcp@229
   439
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   440
                | _ => (if pred i then cv A else reflexive A, i+1)
paulson@2004
   441
        in  combination (combination refl_implies thA) (gconv j B) end
lcp@229
   442
        handle TERM _ => reflexive ct
clasohm@0
   443
  in gconv 1 end;
clasohm@0
   444
clasohm@0
   445
(*Use a conversion to transform a theorem*)
lcp@229
   446
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   447
clasohm@0
   448
(*rewriting conversion*)
lcp@229
   449
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   450
clasohm@0
   451
(*Rewrite a theorem*)
wenzelm@3575
   452
fun rewrite_rule_aux _ []   th = th
wenzelm@3575
   453
  | rewrite_rule_aux prover thms th =
wenzelm@3575
   454
      fconv_rule (rew_conv (true,false) prover (Thm.mss_of thms)) th;
clasohm@0
   455
wenzelm@3555
   456
fun rewrite_thm mode prover mss = fconv_rule (rew_conv mode prover mss);
wenzelm@3555
   457
clasohm@0
   458
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
wenzelm@3575
   459
fun rewrite_goals_rule_aux _ []   th = th
wenzelm@3575
   460
  | rewrite_goals_rule_aux prover thms th =
wenzelm@3575
   461
      fconv_rule (goals_conv (K true) (rew_conv (true, true) prover
wenzelm@3575
   462
        (Thm.mss_of thms))) th;
clasohm@0
   463
clasohm@0
   464
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   465
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   466
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   467
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   468
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   469
clasohm@0
   470
clasohm@0
   471
(** Derived rules mainly for METAHYPS **)
clasohm@0
   472
clasohm@0
   473
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
clasohm@0
   474
fun equal_abs_elim ca eqth =
lcp@229
   475
  let val {sign=signa, t=a, ...} = rep_cterm ca
clasohm@0
   476
      and combth = combination eqth (reflexive ca)
clasohm@0
   477
      val {sign,prop,...} = rep_thm eqth
clasohm@0
   478
      val (abst,absu) = Logic.dest_equals prop
lcp@229
   479
      val cterm = cterm_of (Sign.merge (sign,signa))
clasohm@0
   480
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
clasohm@0
   481
           (transitive combth (beta_conversion (cterm (absu$a))))
clasohm@0
   482
  end
clasohm@0
   483
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
clasohm@0
   484
clasohm@0
   485
(*Calling equal_abs_elim with multiple terms*)
clasohm@0
   486
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
clasohm@0
   487
clasohm@0
   488
local
clasohm@0
   489
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
clasohm@0
   490
  fun err th = raise THM("flexpair_inst: ", 0, [th])
clasohm@0
   491
  fun flexpair_inst def th =
clasohm@0
   492
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
wenzelm@252
   493
        val cterm = cterm_of sign
wenzelm@252
   494
        fun cvar a = cterm(Var((a,0),alpha))
wenzelm@252
   495
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
wenzelm@252
   496
                   def
clasohm@0
   497
    in  equal_elim def' th
clasohm@0
   498
    end
clasohm@0
   499
    handle THM _ => err th | bind => err th
clasohm@0
   500
in
wenzelm@3991
   501
val flexpair_intr = flexpair_inst (symmetric ProtoPure.flexpair_def)
wenzelm@3991
   502
and flexpair_elim = flexpair_inst ProtoPure.flexpair_def
clasohm@0
   503
end;
clasohm@0
   504
clasohm@0
   505
(*Version for flexflex pairs -- this supports lifting.*)
wenzelm@252
   506
fun flexpair_abs_elim_list cts =
clasohm@0
   507
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
clasohm@0
   508
clasohm@0
   509
clasohm@0
   510
(*** Some useful meta-theorems ***)
clasohm@0
   511
clasohm@0
   512
(*The rule V/V, obtains assumption solving for eresolve_tac*)
wenzelm@4016
   513
val asm_rl =
wenzelm@4016
   514
  store_thm "asm_rl" (trivial(read_cterm (sign_of ProtoPure.thy) ("PROP ?psi",propT)));
clasohm@0
   515
clasohm@0
   516
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@4016
   517
val cut_rl =
wenzelm@4016
   518
  store_thm "cut_rl"
wenzelm@4016
   519
    (trivial(read_cterm (sign_of ProtoPure.thy) ("PROP ?psi ==> PROP ?theta", propT)));
clasohm@0
   520
wenzelm@252
   521
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   522
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   523
val revcut_rl =
wenzelm@3991
   524
  let val V = read_cterm (sign_of ProtoPure.thy) ("PROP V", propT)
wenzelm@3991
   525
      and VW = read_cterm (sign_of ProtoPure.thy) ("PROP V ==> PROP W", propT);
wenzelm@4016
   526
  in
wenzelm@4016
   527
    store_thm "revcut_rl"
wenzelm@4016
   528
      (implies_intr V (implies_intr VW (implies_elim (assume VW) (assume V))))
clasohm@0
   529
  end;
clasohm@0
   530
lcp@668
   531
(*for deleting an unwanted assumption*)
lcp@668
   532
val thin_rl =
wenzelm@3991
   533
  let val V = read_cterm (sign_of ProtoPure.thy) ("PROP V", propT)
wenzelm@3991
   534
      and W = read_cterm (sign_of ProtoPure.thy) ("PROP W", propT);
wenzelm@4016
   535
  in  store_thm "thin_rl" (implies_intr V (implies_intr W (assume W)))
lcp@668
   536
  end;
lcp@668
   537
clasohm@0
   538
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   539
val triv_forall_equality =
wenzelm@3991
   540
  let val V  = read_cterm (sign_of ProtoPure.thy) ("PROP V", propT)
wenzelm@3991
   541
      and QV = read_cterm (sign_of ProtoPure.thy) ("!!x::'a. PROP V", propT)
wenzelm@3991
   542
      and x  = read_cterm (sign_of ProtoPure.thy) ("x", TFree("'a",logicS));
wenzelm@4016
   543
  in
wenzelm@4016
   544
    store_thm "triv_forall_equality"
wenzelm@4016
   545
      (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@4016
   546
        (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   547
  end;
clasohm@0
   548
nipkow@1756
   549
(* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
nipkow@1756
   550
   (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
nipkow@1756
   551
   `thm COMP swap_prems_rl' swaps the first two premises of `thm'
nipkow@1756
   552
*)
nipkow@1756
   553
val swap_prems_rl =
wenzelm@3991
   554
  let val cmajor = read_cterm (sign_of ProtoPure.thy)
nipkow@1756
   555
            ("PROP PhiA ==> PROP PhiB ==> PROP Psi", propT);
nipkow@1756
   556
      val major = assume cmajor;
wenzelm@3991
   557
      val cminor1 = read_cterm (sign_of ProtoPure.thy)  ("PROP PhiA", propT);
nipkow@1756
   558
      val minor1 = assume cminor1;
wenzelm@3991
   559
      val cminor2 = read_cterm (sign_of ProtoPure.thy)  ("PROP PhiB", propT);
nipkow@1756
   560
      val minor2 = assume cminor2;
wenzelm@4016
   561
  in store_thm "swap_prems_rl"
nipkow@1756
   562
       (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
nipkow@1756
   563
         (implies_elim (implies_elim major minor1) minor2))))
nipkow@1756
   564
  end;
nipkow@1756
   565
nipkow@3653
   566
(* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
nipkow@3653
   567
   ==> PROP ?phi == PROP ?psi
nipkow@3653
   568
   Introduction rule for == using ==> not meta-hyps.
nipkow@3653
   569
*)
nipkow@3653
   570
val equal_intr_rule =
wenzelm@3991
   571
  let val PQ = read_cterm (sign_of ProtoPure.thy) ("PROP phi ==> PROP psi", propT)
wenzelm@3991
   572
      and QP = read_cterm (sign_of ProtoPure.thy) ("PROP psi ==> PROP phi", propT)
wenzelm@4016
   573
  in
wenzelm@4016
   574
    store_thm "equal_intr_rule"
wenzelm@4016
   575
      (implies_intr PQ (implies_intr QP (equal_intr (assume PQ) (assume QP))))
nipkow@3653
   576
  end;
nipkow@3653
   577
clasohm@0
   578
end;
wenzelm@252
   579
paulson@1499
   580
open Drule;