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