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