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