src/Pure/drule.ML
author wenzelm
Thu Feb 03 13:55:03 1994 +0100 (1994-02-03)
changeset 252 7532f95d7f44
parent 229 4002c4cd450c
child 385 921f87897a76
permissions -rw-r--r--
removed eq_sg, pprint_sg, print_sg (now in sign.ML);
removed cterm_fun, read_ctyp (now in thm.ML);
print_theory: now shows all contents;
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@0
    13
  structure Thm : THM
clasohm@0
    14
  local open Thm  in
clasohm@0
    15
  val asm_rl: thm
clasohm@0
    16
  val assume_ax: theory -> string -> thm
clasohm@0
    17
  val COMP: thm * thm -> thm
clasohm@0
    18
  val compose: thm * int * thm -> thm list
lcp@229
    19
  val cterm_instantiate: (cterm*cterm)list -> thm -> thm
clasohm@0
    20
  val cut_rl: thm
lcp@229
    21
  val equal_abs_elim: cterm  -> thm -> thm
lcp@229
    22
  val equal_abs_elim_list: cterm list -> thm -> thm
clasohm@0
    23
  val eq_thm: thm * thm -> bool
clasohm@0
    24
  val eq_thm_sg: thm * thm -> bool
lcp@229
    25
  val flexpair_abs_elim_list: cterm list -> thm -> thm
lcp@229
    26
  val forall_intr_list: cterm list -> thm -> thm
clasohm@0
    27
  val forall_intr_frees: thm -> thm
lcp@229
    28
  val forall_elim_list: cterm list -> thm -> thm
clasohm@0
    29
  val forall_elim_var: int -> thm -> thm
clasohm@0
    30
  val forall_elim_vars: int -> thm -> thm
clasohm@0
    31
  val implies_elim_list: thm -> thm list -> thm
lcp@229
    32
  val implies_intr_list: cterm list -> thm -> thm
lcp@11
    33
  val MRL: thm list list * thm list -> thm list
lcp@11
    34
  val MRS: thm list * thm -> thm
lcp@229
    35
  val pprint_cterm: cterm -> pprint_args -> unit
lcp@229
    36
  val pprint_ctyp: ctyp -> pprint_args -> unit
lcp@229
    37
  val pprint_theory: theory -> pprint_args -> unit
lcp@229
    38
  val pprint_thm: thm -> pprint_args -> unit
lcp@229
    39
  val pretty_thm: thm -> Sign.Syntax.Pretty.T
lcp@229
    40
  val print_cterm: cterm -> unit
lcp@229
    41
  val print_ctyp: ctyp -> unit
clasohm@0
    42
  val print_goals: int -> thm -> unit
lcp@67
    43
  val print_goals_ref: (int -> thm -> unit) ref
clasohm@0
    44
  val print_theory: theory -> unit
clasohm@0
    45
  val print_thm: thm -> unit
clasohm@0
    46
  val prth: thm -> thm
clasohm@0
    47
  val prthq: thm Sequence.seq -> thm Sequence.seq
clasohm@0
    48
  val prths: thm list -> thm list
clasohm@0
    49
  val read_instantiate: (string*string)list -> thm -> thm
clasohm@0
    50
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
wenzelm@252
    51
  val read_insts:
lcp@229
    52
          Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
lcp@229
    53
                  -> (indexname -> typ option) * (indexname -> sort option)
lcp@229
    54
                  -> (string*string)list
lcp@229
    55
                  -> (indexname*ctyp)list * (cterm*cterm)list
clasohm@0
    56
  val reflexive_thm: thm
clasohm@0
    57
  val revcut_rl: thm
nipkow@214
    58
  val rewrite_goal_rule: bool*bool -> (meta_simpset -> thm -> thm option)
nipkow@214
    59
        -> meta_simpset -> int -> thm -> thm
clasohm@0
    60
  val rewrite_goals_rule: thm list -> thm -> thm
clasohm@0
    61
  val rewrite_rule: thm list -> thm -> thm
clasohm@0
    62
  val RS: thm * thm -> thm
clasohm@0
    63
  val RSN: thm * (int * thm) -> thm
clasohm@0
    64
  val RL: thm list * thm list -> thm list
clasohm@0
    65
  val RLN: thm list * (int * thm list) -> thm list
clasohm@0
    66
  val show_hyps: bool ref
clasohm@0
    67
  val size_of_thm: thm -> int
clasohm@0
    68
  val standard: thm -> thm
lcp@229
    69
  val string_of_cterm: cterm -> string
lcp@229
    70
  val string_of_ctyp: ctyp -> string
clasohm@0
    71
  val string_of_thm: thm -> string
clasohm@0
    72
  val symmetric_thm: thm
clasohm@0
    73
  val transitive_thm: thm
clasohm@0
    74
  val triv_forall_equality: thm
clasohm@0
    75
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
clasohm@0
    76
  val zero_var_indexes: thm -> thm
clasohm@0
    77
  end
clasohm@0
    78
  end;
clasohm@0
    79
wenzelm@252
    80
functor DruleFun (structure Logic: LOGIC and Thm: THM): DRULE =
clasohm@0
    81
struct
clasohm@0
    82
structure Thm = Thm;
clasohm@0
    83
structure Sign = Thm.Sign;
clasohm@0
    84
structure Type = Sign.Type;
clasohm@0
    85
structure Pretty = Sign.Syntax.Pretty
clasohm@0
    86
local open Thm
clasohm@0
    87
in
clasohm@0
    88
clasohm@0
    89
(**** More derived rules and operations on theorems ****)
clasohm@0
    90
lcp@229
    91
(** reading of instantiations **)
lcp@229
    92
lcp@229
    93
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
lcp@229
    94
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
lcp@229
    95
lcp@229
    96
fun absent ixn =
lcp@229
    97
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
lcp@229
    98
lcp@229
    99
fun inst_failure ixn =
lcp@229
   100
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
lcp@229
   101
lcp@229
   102
fun read_insts sign (rtypes,rsorts) (types,sorts) insts =
lcp@229
   103
let val {tsig,...} = Sign.rep_sg sign
lcp@229
   104
    fun split([],tvs,vs) = (tvs,vs)
lcp@229
   105
      | split((sv,st)::l,tvs,vs) = (case explode sv of
lcp@229
   106
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
lcp@229
   107
                | cs => split(l,tvs,(indexname cs,st)::vs));
lcp@229
   108
    val (tvs,vs) = split(insts,[],[]);
lcp@229
   109
    fun readT((a,i),st) =
lcp@229
   110
        let val ixn = ("'" ^ a,i);
lcp@229
   111
            val S = case rsorts ixn of Some S => S | None => absent ixn;
lcp@229
   112
            val T = Sign.read_typ (sign,sorts) st;
lcp@229
   113
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
lcp@229
   114
           else inst_failure ixn
lcp@229
   115
        end
lcp@229
   116
    val tye = map readT tvs;
lcp@229
   117
    fun add_cterm ((cts,tye), (ixn,st)) =
lcp@229
   118
        let val T = case rtypes ixn of
lcp@229
   119
                      Some T => typ_subst_TVars tye T
lcp@229
   120
                    | None => absent ixn;
lcp@229
   121
            val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T);
lcp@229
   122
            val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T))
lcp@229
   123
        in ((cv,ct)::cts,tye2 @ tye) end
lcp@229
   124
    val (cterms,tye') = foldl add_cterm (([],tye), vs);
lcp@229
   125
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
lcp@229
   126
lcp@229
   127
wenzelm@252
   128
wenzelm@252
   129
(*** Printing of theories, theorems, etc. ***)
lcp@229
   130
lcp@229
   131
(*If false, hypotheses are printed as dots*)
lcp@229
   132
val show_hyps = ref true;
lcp@229
   133
lcp@229
   134
fun pretty_thm th =
lcp@229
   135
let val {sign, hyps, prop,...} = rep_thm th
lcp@229
   136
    val hsymbs = if null hyps then []
wenzelm@252
   137
                 else if !show_hyps then
wenzelm@252
   138
                      [Pretty.brk 2,
wenzelm@252
   139
                       Pretty.lst("[","]") (map (Sign.pretty_term sign) hyps)]
wenzelm@252
   140
                 else Pretty.str" [" :: map (fn _ => Pretty.str".") hyps @
wenzelm@252
   141
                      [Pretty.str"]"];
lcp@229
   142
in Pretty.blk(0, Sign.pretty_term sign prop :: hsymbs) end;
lcp@229
   143
lcp@229
   144
val string_of_thm = Pretty.string_of o pretty_thm;
lcp@229
   145
lcp@229
   146
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
lcp@229
   147
lcp@229
   148
lcp@229
   149
(** Top-level commands for printing theorems **)
lcp@229
   150
val print_thm = writeln o string_of_thm;
lcp@229
   151
lcp@229
   152
fun prth th = (print_thm th; th);
lcp@229
   153
lcp@229
   154
(*Print and return a sequence of theorems, separated by blank lines. *)
lcp@229
   155
fun prthq thseq =
wenzelm@252
   156
  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
lcp@229
   157
lcp@229
   158
(*Print and return a list of theorems, separated by blank lines. *)
lcp@229
   159
fun prths ths = (print_list_ln print_thm ths; ths);
lcp@229
   160
wenzelm@252
   161
wenzelm@252
   162
(* other printing commands *)
lcp@229
   163
wenzelm@252
   164
fun pprint_ctyp cT =
wenzelm@252
   165
  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
wenzelm@252
   166
wenzelm@252
   167
fun string_of_ctyp cT =
wenzelm@252
   168
  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
lcp@229
   169
lcp@229
   170
val print_ctyp = writeln o string_of_ctyp;
lcp@229
   171
wenzelm@252
   172
fun pprint_cterm ct =
wenzelm@252
   173
  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
lcp@229
   174
wenzelm@252
   175
fun string_of_cterm ct =
wenzelm@252
   176
  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
lcp@229
   177
lcp@229
   178
val print_cterm = writeln o string_of_cterm;
lcp@229
   179
wenzelm@252
   180
wenzelm@252
   181
(* print theory *)
wenzelm@252
   182
wenzelm@252
   183
val pprint_theory = Sign.pprint_sg o sign_of;
lcp@229
   184
wenzelm@252
   185
fun print_theory thy =
wenzelm@252
   186
  let
wenzelm@252
   187
    fun prt_thm (name, thm) = Pretty.block
wenzelm@252
   188
      [Pretty.str (name ^ ":"), Pretty.brk 1, Pretty.quote (pretty_thm thm)];
lcp@229
   189
wenzelm@252
   190
    val sg = sign_of thy;
wenzelm@252
   191
    val axioms =        (* FIXME should rather fix axioms_of *)
wenzelm@252
   192
      sort (fn ((x, _), (y, _)) => x <= y)
wenzelm@252
   193
        (gen_distinct eq_fst (axioms_of thy));
wenzelm@252
   194
  in
wenzelm@252
   195
    Sign.print_sg sg;
wenzelm@252
   196
    Pretty.writeln (Pretty.big_list "axioms:" (map prt_thm axioms))
wenzelm@252
   197
  end;
lcp@229
   198
lcp@229
   199
lcp@229
   200
lcp@229
   201
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
lcp@229
   202
lcp@229
   203
fun prettyprints es = writeln(Pretty.string_of(Pretty.blk(0,es)));
lcp@229
   204
lcp@229
   205
fun print_goals maxgoals th : unit =
lcp@229
   206
let val {sign, hyps, prop,...} = rep_thm th;
lcp@229
   207
    fun printgoals (_, []) = ()
lcp@229
   208
      | printgoals (n, A::As) =
wenzelm@252
   209
        let val prettyn = Pretty.str(" " ^ string_of_int n ^ ". ");
wenzelm@252
   210
            val prettyA = Sign.pretty_term sign A
wenzelm@252
   211
        in prettyprints[prettyn,prettyA];
wenzelm@252
   212
           printgoals (n+1,As)
lcp@229
   213
        end;
lcp@229
   214
    fun prettypair(t,u) =
lcp@229
   215
        Pretty.blk(0, [Sign.pretty_term sign t, Pretty.str" =?=", Pretty.brk 1,
wenzelm@252
   216
                       Sign.pretty_term sign u]);
lcp@229
   217
    fun printff [] = ()
lcp@229
   218
      | printff tpairs =
wenzelm@252
   219
         writeln("\nFlex-flex pairs:\n" ^
wenzelm@252
   220
                 Pretty.string_of(Pretty.lst("","") (map prettypair tpairs)))
lcp@229
   221
    val (tpairs,As,B) = Logic.strip_horn(prop);
lcp@229
   222
    val ngoals = length As
wenzelm@252
   223
in
lcp@229
   224
   writeln (Sign.string_of_term sign B);
lcp@229
   225
   if ngoals=0 then writeln"No subgoals!"
wenzelm@252
   226
   else if ngoals>maxgoals
lcp@229
   227
        then (printgoals (1, take(maxgoals,As));
wenzelm@252
   228
              writeln("A total of " ^ string_of_int ngoals ^ " subgoals..."))
lcp@229
   229
        else printgoals (1, As);
lcp@229
   230
   printff tpairs
lcp@229
   231
end;
lcp@229
   232
lcp@229
   233
(*"hook" for user interfaces: allows print_goals to be replaced*)
lcp@229
   234
val print_goals_ref = ref print_goals;
lcp@229
   235
wenzelm@252
   236
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   237
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   238
     type variables) when reading another term.
clasohm@0
   239
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   240
***)
clasohm@0
   241
clasohm@0
   242
fun types_sorts thm =
clasohm@0
   243
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   244
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   245
        val vars = map dest_Var (term_vars big);
wenzelm@252
   246
        val frees = map dest_Free (term_frees big);
wenzelm@252
   247
        val tvars = term_tvars big;
wenzelm@252
   248
        val tfrees = term_tfrees big;
wenzelm@252
   249
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   250
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   251
    in (typ,sort) end;
clasohm@0
   252
clasohm@0
   253
(** Standardization of rules **)
clasohm@0
   254
clasohm@0
   255
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   256
fun forall_intr_list [] th = th
clasohm@0
   257
  | forall_intr_list (y::ys) th =
wenzelm@252
   258
        let val gth = forall_intr_list ys th
wenzelm@252
   259
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   260
clasohm@0
   261
(*Generalization over all suitable Free variables*)
clasohm@0
   262
fun forall_intr_frees th =
clasohm@0
   263
    let val {prop,sign,...} = rep_thm th
clasohm@0
   264
    in  forall_intr_list
wenzelm@252
   265
         (map (cterm_of sign) (sort atless (term_frees prop)))
clasohm@0
   266
         th
clasohm@0
   267
    end;
clasohm@0
   268
clasohm@0
   269
(*Replace outermost quantified variable by Var of given index.
clasohm@0
   270
    Could clash with Vars already present.*)
wenzelm@252
   271
fun forall_elim_var i th =
clasohm@0
   272
    let val {prop,sign,...} = rep_thm th
clasohm@0
   273
    in case prop of
wenzelm@252
   274
          Const("all",_) $ Abs(a,T,_) =>
wenzelm@252
   275
              forall_elim (cterm_of sign (Var((a,i), T)))  th
wenzelm@252
   276
        | _ => raise THM("forall_elim_var", i, [th])
clasohm@0
   277
    end;
clasohm@0
   278
clasohm@0
   279
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@252
   280
fun forall_elim_vars i th =
clasohm@0
   281
    forall_elim_vars i (forall_elim_var i th)
wenzelm@252
   282
        handle THM _ => th;
clasohm@0
   283
clasohm@0
   284
(*Specialization over a list of cterms*)
clasohm@0
   285
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   286
clasohm@0
   287
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   288
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   289
clasohm@0
   290
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   291
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   292
clasohm@0
   293
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   294
fun zero_var_indexes th =
clasohm@0
   295
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   296
        val vars = term_vars prop
clasohm@0
   297
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   298
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   299
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
wenzelm@252
   300
        val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
wenzelm@252
   301
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   302
        fun varpairs([],[]) = []
wenzelm@252
   303
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   304
                let val T' = typ_subst_TVars tye T
wenzelm@252
   305
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   306
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   307
                end
wenzelm@252
   308
          | varpairs _ = raise TERM("varpairs", []);
clasohm@0
   309
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   310
clasohm@0
   311
clasohm@0
   312
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   313
    all generality expressed by Vars having index 0.*)
clasohm@0
   314
fun standard th =
clasohm@0
   315
    let val {maxidx,...} = rep_thm th
wenzelm@252
   316
    in  varifyT (zero_var_indexes (forall_elim_vars(maxidx+1)
clasohm@0
   317
                         (forall_intr_frees(implies_intr_hyps th))))
clasohm@0
   318
    end;
clasohm@0
   319
wenzelm@252
   320
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   321
  Generalizes over Free variables,
clasohm@0
   322
  creates the assumption, and then strips quantifiers.
clasohm@0
   323
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   324
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   325
fun assume_ax thy sP =
clasohm@0
   326
    let val sign = sign_of thy
wenzelm@252
   327
        val prop = Logic.close_form (term_of (read_cterm sign
wenzelm@252
   328
                         (sP, propT)))
lcp@229
   329
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   330
wenzelm@252
   331
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   332
fun tha RSN (i,thb) =
clasohm@0
   333
  case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   334
      ([th],_) => th
clasohm@0
   335
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   336
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   337
clasohm@0
   338
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   339
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   340
clasohm@0
   341
(*For joining lists of rules*)
wenzelm@252
   342
fun thas RLN (i,thbs) =
clasohm@0
   343
  let val resolve = biresolution false (map (pair false) thas) i
clasohm@0
   344
      fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
clasohm@0
   345
  in  flat (map resb thbs)  end;
clasohm@0
   346
clasohm@0
   347
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   348
lcp@11
   349
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   350
  makes proof trees*)
wenzelm@252
   351
fun rls MRS bottom_rl =
lcp@11
   352
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   353
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   354
  in  rs_aux 1 rls  end;
lcp@11
   355
lcp@11
   356
(*As above, but for rule lists*)
wenzelm@252
   357
fun rlss MRL bottom_rls =
lcp@11
   358
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   359
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   360
  in  rs_aux 1 rlss  end;
lcp@11
   361
wenzelm@252
   362
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   363
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   364
  ALWAYS deletes premise i *)
wenzelm@252
   365
fun compose(tha,i,thb) =
clasohm@0
   366
    Sequence.list_of_s (bicompose false (false,tha,0) i thb);
clasohm@0
   367
clasohm@0
   368
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   369
fun tha COMP thb =
clasohm@0
   370
    case compose(tha,1,thb) of
wenzelm@252
   371
        [th] => th
clasohm@0
   372
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   373
clasohm@0
   374
(*Instantiate theorem th, reading instantiations under signature sg*)
clasohm@0
   375
fun read_instantiate_sg sg sinsts th =
clasohm@0
   376
    let val ts = types_sorts th;
lcp@229
   377
    in  instantiate (read_insts sg ts ts sinsts) th  end;
clasohm@0
   378
clasohm@0
   379
(*Instantiate theorem th, reading instantiations under theory of th*)
clasohm@0
   380
fun read_instantiate sinsts th =
clasohm@0
   381
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
clasohm@0
   382
clasohm@0
   383
clasohm@0
   384
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
clasohm@0
   385
  Instantiates distinct Vars by terms, inferring type instantiations. *)
clasohm@0
   386
local
clasohm@0
   387
  fun add_types ((ct,cu), (sign,tye)) =
lcp@229
   388
    let val {sign=signt, t=t, T= T, ...} = rep_cterm ct
lcp@229
   389
        and {sign=signu, t=u, T= U, ...} = rep_cterm cu
clasohm@0
   390
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
wenzelm@252
   391
        val tye' = Type.unify (#tsig(Sign.rep_sg sign')) ((T,U), tye)
wenzelm@252
   392
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
clasohm@0
   393
    in  (sign', tye')  end;
clasohm@0
   394
in
wenzelm@252
   395
fun cterm_instantiate ctpairs0 th =
clasohm@0
   396
  let val (sign,tye) = foldr add_types (ctpairs0, (#sign(rep_thm th),[]))
clasohm@0
   397
      val tsig = #tsig(Sign.rep_sg sign);
clasohm@0
   398
      fun instT(ct,cu) = let val inst = subst_TVars tye
wenzelm@252
   399
                         in (cterm_fun inst ct, cterm_fun inst cu) end
lcp@229
   400
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
clasohm@0
   401
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
wenzelm@252
   402
  handle TERM _ =>
clasohm@0
   403
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
clasohm@0
   404
       | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
clasohm@0
   405
end;
clasohm@0
   406
clasohm@0
   407
clasohm@0
   408
(** theorem equality test is exported and used by BEST_FIRST **)
clasohm@0
   409
wenzelm@252
   410
(*equality of theorems uses equality of signatures and
clasohm@0
   411
  the a-convertible test for terms*)
wenzelm@252
   412
fun eq_thm (th1,th2) =
clasohm@0
   413
    let val {sign=sg1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
wenzelm@252
   414
        and {sign=sg2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
wenzelm@252
   415
    in  Sign.eq_sg (sg1,sg2) andalso
wenzelm@252
   416
        aconvs(hyps1,hyps2) andalso
wenzelm@252
   417
        prop1 aconv prop2
clasohm@0
   418
    end;
clasohm@0
   419
clasohm@0
   420
(*Do the two theorems have the same signature?*)
wenzelm@252
   421
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
clasohm@0
   422
clasohm@0
   423
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   424
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   425
clasohm@0
   426
clasohm@0
   427
(*** Meta-Rewriting Rules ***)
clasohm@0
   428
clasohm@0
   429
clasohm@0
   430
val reflexive_thm =
lcp@229
   431
  let val cx = cterm_of Sign.pure (Var(("x",0),TVar(("'a",0),["logic"])))
clasohm@0
   432
  in Thm.reflexive cx end;
clasohm@0
   433
clasohm@0
   434
val symmetric_thm =
lcp@229
   435
  let val xy = read_cterm Sign.pure ("x::'a::logic == y",propT)
clasohm@0
   436
  in standard(Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
clasohm@0
   437
clasohm@0
   438
val transitive_thm =
lcp@229
   439
  let val xy = read_cterm Sign.pure ("x::'a::logic == y",propT)
lcp@229
   440
      val yz = read_cterm Sign.pure ("y::'a::logic == z",propT)
clasohm@0
   441
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
clasohm@0
   442
  in standard(Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
clasohm@0
   443
lcp@229
   444
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   445
lcp@229
   446
val refl_cimplies = reflexive (cterm_of Sign.pure implies);
clasohm@0
   447
clasohm@0
   448
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   449
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   450
fun goals_conv pred cv =
lcp@229
   451
  let fun gconv i ct =
lcp@229
   452
        let val (A,B) = Thm.dest_cimplies ct
lcp@229
   453
            val (thA,j) = case term_of A of
lcp@229
   454
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   455
                | _ => (if pred i then cv A else reflexive A, i+1)
wenzelm@252
   456
        in  combination (combination refl_cimplies thA) (gconv j B) end
lcp@229
   457
        handle TERM _ => reflexive ct
clasohm@0
   458
  in gconv 1 end;
clasohm@0
   459
clasohm@0
   460
(*Use a conversion to transform a theorem*)
lcp@229
   461
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   462
clasohm@0
   463
(*rewriting conversion*)
lcp@229
   464
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   465
clasohm@0
   466
(*Rewrite a theorem*)
nipkow@214
   467
fun rewrite_rule thms =
nipkow@214
   468
  fconv_rule (rew_conv (true,false) (K(K None)) (Thm.mss_of thms));
clasohm@0
   469
clasohm@0
   470
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
clasohm@0
   471
fun rewrite_goals_rule thms =
nipkow@214
   472
  fconv_rule (goals_conv (K true) (rew_conv (true,false) (K(K None))
nipkow@214
   473
             (Thm.mss_of thms)));
clasohm@0
   474
clasohm@0
   475
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   476
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   477
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   478
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   479
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   480
clasohm@0
   481
clasohm@0
   482
(** Derived rules mainly for METAHYPS **)
clasohm@0
   483
clasohm@0
   484
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
clasohm@0
   485
fun equal_abs_elim ca eqth =
lcp@229
   486
  let val {sign=signa, t=a, ...} = rep_cterm ca
clasohm@0
   487
      and combth = combination eqth (reflexive ca)
clasohm@0
   488
      val {sign,prop,...} = rep_thm eqth
clasohm@0
   489
      val (abst,absu) = Logic.dest_equals prop
lcp@229
   490
      val cterm = cterm_of (Sign.merge (sign,signa))
clasohm@0
   491
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
clasohm@0
   492
           (transitive combth (beta_conversion (cterm (absu$a))))
clasohm@0
   493
  end
clasohm@0
   494
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
clasohm@0
   495
clasohm@0
   496
(*Calling equal_abs_elim with multiple terms*)
clasohm@0
   497
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
clasohm@0
   498
clasohm@0
   499
local
clasohm@0
   500
  open Logic
clasohm@0
   501
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
clasohm@0
   502
  fun err th = raise THM("flexpair_inst: ", 0, [th])
clasohm@0
   503
  fun flexpair_inst def th =
clasohm@0
   504
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
wenzelm@252
   505
        val cterm = cterm_of sign
wenzelm@252
   506
        fun cvar a = cterm(Var((a,0),alpha))
wenzelm@252
   507
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
wenzelm@252
   508
                   def
clasohm@0
   509
    in  equal_elim def' th
clasohm@0
   510
    end
clasohm@0
   511
    handle THM _ => err th | bind => err th
clasohm@0
   512
in
clasohm@0
   513
val flexpair_intr = flexpair_inst (symmetric flexpair_def)
clasohm@0
   514
and flexpair_elim = flexpair_inst flexpair_def
clasohm@0
   515
end;
clasohm@0
   516
clasohm@0
   517
(*Version for flexflex pairs -- this supports lifting.*)
wenzelm@252
   518
fun flexpair_abs_elim_list cts =
clasohm@0
   519
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
clasohm@0
   520
clasohm@0
   521
clasohm@0
   522
(*** Some useful meta-theorems ***)
clasohm@0
   523
clasohm@0
   524
(*The rule V/V, obtains assumption solving for eresolve_tac*)
lcp@229
   525
val asm_rl = trivial(read_cterm Sign.pure ("PROP ?psi",propT));
clasohm@0
   526
clasohm@0
   527
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@252
   528
val cut_rl = trivial(read_cterm Sign.pure
wenzelm@252
   529
        ("PROP ?psi ==> PROP ?theta", propT));
clasohm@0
   530
wenzelm@252
   531
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   532
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   533
val revcut_rl =
lcp@229
   534
  let val V = read_cterm Sign.pure ("PROP V", propT)
lcp@229
   535
      and VW = read_cterm Sign.pure ("PROP V ==> PROP W", propT);
wenzelm@252
   536
  in  standard (implies_intr V
wenzelm@252
   537
                (implies_intr VW
wenzelm@252
   538
                 (implies_elim (assume VW) (assume V))))
clasohm@0
   539
  end;
clasohm@0
   540
clasohm@0
   541
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   542
val triv_forall_equality =
lcp@229
   543
  let val V  = read_cterm Sign.pure ("PROP V", propT)
lcp@229
   544
      and QV = read_cterm Sign.pure ("!!x::'a. PROP V", propT)
lcp@229
   545
      and x  = read_cterm Sign.pure ("x", TFree("'a",["logic"]));
clasohm@0
   546
  in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@252
   547
                           (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   548
  end;
clasohm@0
   549
clasohm@0
   550
end
clasohm@0
   551
end;
wenzelm@252
   552