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