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