src/Pure/thm.ML
author lcp
Tue Jan 18 13:46:08 1994 +0100 (1994-01-18)
changeset 229 4002c4cd450c
parent 225 76f60e6400e8
child 242 8fe3e66abf0c
permissions -rw-r--r--
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
functions from sign.ML to thm.ML or drule.ML. This allows the "prop" field
of a theorem to be regarded as a cterm -- avoids expensive calls to
cterm_of.
clasohm@0
     1
(*  Title: 	thm
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
lcp@229
     4
    Copyright   1994  University of Cambridge
lcp@229
     5
lcp@229
     6
NO REP_CTERM!!
clasohm@0
     7
clasohm@0
     8
The abstract types "theory" and "thm"
lcp@229
     9
Also "cterm" / "ctyp" (certified terms / typs under a signature).
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
signature THM = 
clasohm@0
    13
  sig
clasohm@0
    14
  structure Envir : ENVIR
clasohm@0
    15
  structure Sequence : SEQUENCE
clasohm@0
    16
  structure Sign : SIGN
lcp@229
    17
  type cterm
lcp@229
    18
  type ctyp
clasohm@0
    19
  type meta_simpset
clasohm@0
    20
  type theory
clasohm@0
    21
  type thm
clasohm@0
    22
  exception THM of string * int * thm list
clasohm@0
    23
  exception THEORY of string * theory list
clasohm@0
    24
  exception SIMPLIFIER of string * thm
lcp@229
    25
  (*Certified terms/types; previously in sign.ML*)
lcp@229
    26
  val cterm_of: Sign.sg -> term -> cterm
lcp@229
    27
  val ctyp_of: Sign.sg -> typ -> ctyp
lcp@229
    28
  val read_cterm: Sign.sg -> string * typ -> cterm
lcp@229
    29
  val rep_cterm: cterm -> {T: typ, t: term, sign: Sign.sg, maxidx: int}
lcp@229
    30
  val rep_ctyp: ctyp -> {T: typ, sign: Sign.sg}
lcp@229
    31
  val term_of: cterm -> term
lcp@229
    32
  val typ_of: ctyp -> typ
lcp@229
    33
  (*End of cterm/ctyp functions*)  
lcp@229
    34
  val abstract_rule: string -> cterm -> thm -> thm
clasohm@0
    35
  val add_congs: meta_simpset * thm list -> meta_simpset
clasohm@0
    36
  val add_prems: meta_simpset * thm list -> meta_simpset
clasohm@0
    37
  val add_simps: meta_simpset * thm list -> meta_simpset
lcp@229
    38
  val assume: cterm -> thm
clasohm@0
    39
  val assumption: int -> thm -> thm Sequence.seq   
clasohm@0
    40
  val axioms_of: theory -> (string * thm) list
lcp@229
    41
  val beta_conversion: cterm -> thm   
clasohm@0
    42
  val bicompose: bool -> bool * thm * int -> int -> thm -> thm Sequence.seq   
clasohm@0
    43
  val biresolution: bool -> (bool*thm)list -> int -> thm -> thm Sequence.seq   
clasohm@0
    44
  val combination: thm -> thm -> thm   
clasohm@0
    45
  val concl_of: thm -> term   
lcp@229
    46
  val cprop_of: thm -> cterm
nipkow@87
    47
  val del_simps: meta_simpset * thm list -> meta_simpset
lcp@229
    48
  val dest_cimplies: cterm -> cterm*cterm
clasohm@0
    49
  val dest_state: thm * int -> (term*term)list * term list * term * term
clasohm@0
    50
  val empty_mss: meta_simpset
clasohm@0
    51
  val eq_assumption: int -> thm -> thm   
clasohm@0
    52
  val equal_intr: thm -> thm -> thm
clasohm@0
    53
  val equal_elim: thm -> thm -> thm
clasohm@0
    54
  val extend_theory: theory -> string
clasohm@0
    55
	-> (class * class list) list * sort
clasohm@0
    56
	   * (string list * int)list
nipkow@200
    57
           * (string * indexname list * string) list
clasohm@0
    58
	   * (string list * (sort list * class))list
clasohm@0
    59
	   * (string list * string)list * Sign.Syntax.sext option
clasohm@0
    60
	-> (string*string)list -> theory
clasohm@0
    61
  val extensional: thm -> thm   
clasohm@0
    62
  val flexflex_rule: thm -> thm Sequence.seq  
clasohm@0
    63
  val flexpair_def: thm 
lcp@229
    64
  val forall_elim: cterm -> thm -> thm
lcp@229
    65
  val forall_intr: cterm -> thm -> thm
clasohm@0
    66
  val freezeT: thm -> thm
clasohm@0
    67
  val get_axiom: theory -> string -> thm
clasohm@0
    68
  val implies_elim: thm -> thm -> thm
lcp@229
    69
  val implies_intr: cterm -> thm -> thm
clasohm@0
    70
  val implies_intr_hyps: thm -> thm
lcp@229
    71
  val instantiate: (indexname*ctyp)list * (cterm*cterm)list 
clasohm@0
    72
                   -> thm -> thm
clasohm@0
    73
  val lift_rule: (thm * int) -> thm -> thm
clasohm@0
    74
  val merge_theories: theory * theory -> theory
clasohm@0
    75
  val mk_rews_of_mss: meta_simpset -> thm -> thm list
clasohm@0
    76
  val mss_of: thm list -> meta_simpset
clasohm@0
    77
  val nprems_of: thm -> int
clasohm@0
    78
  val parents_of: theory -> theory list
clasohm@0
    79
  val prems_of: thm -> term list
clasohm@0
    80
  val prems_of_mss: meta_simpset -> thm list
clasohm@0
    81
  val pure_thy: theory
lcp@229
    82
  val read_def_cterm :
lcp@229
    83
         Sign.sg * (indexname -> typ option) * (indexname -> sort option) ->
lcp@229
    84
         string * typ -> cterm * (indexname * typ) list
lcp@229
    85
   val reflexive: cterm -> thm 
clasohm@0
    86
  val rename_params_rule: string list * int -> thm -> thm
clasohm@0
    87
  val rep_thm: thm -> {prop: term, hyps: term list, maxidx: int, sign: Sign.sg}
nipkow@214
    88
  val rewrite_cterm:
nipkow@214
    89
         bool*bool -> meta_simpset -> (meta_simpset -> thm -> thm option)
lcp@229
    90
           -> cterm -> thm
clasohm@0
    91
  val set_mk_rews: meta_simpset * (thm -> thm list) -> meta_simpset
clasohm@0
    92
  val sign_of: theory -> Sign.sg   
clasohm@0
    93
  val syn_of: theory -> Sign.Syntax.syntax
clasohm@0
    94
  val stamps_of_thm: thm -> string ref list
clasohm@0
    95
  val stamps_of_thy: theory -> string ref list
clasohm@0
    96
  val symmetric: thm -> thm   
clasohm@0
    97
  val tpairs_of: thm -> (term*term)list
clasohm@0
    98
  val trace_simp: bool ref
clasohm@0
    99
  val transitive: thm -> thm -> thm
lcp@229
   100
  val trivial: cterm -> thm
clasohm@0
   101
  val varifyT: thm -> thm
clasohm@0
   102
  end;
clasohm@0
   103
clasohm@0
   104
clasohm@0
   105
clasohm@0
   106
functor ThmFun (structure Logic: LOGIC and Unify: UNIFY and Pattern:PATTERN
clasohm@0
   107
                      and Net:NET
clasohm@0
   108
                sharing type Pattern.type_sig = Unify.Sign.Type.type_sig)
lcp@229
   109
        : THM =
clasohm@0
   110
struct
clasohm@0
   111
structure Sequence = Unify.Sequence;
clasohm@0
   112
structure Envir = Unify.Envir;
clasohm@0
   113
structure Sign = Unify.Sign;
clasohm@0
   114
structure Type = Sign.Type;
clasohm@0
   115
structure Syntax = Sign.Syntax;
clasohm@0
   116
structure Symtab = Sign.Symtab;
clasohm@0
   117
clasohm@0
   118
lcp@229
   119
(** Certified Types **)
lcp@229
   120
lcp@229
   121
lcp@229
   122
(*Certified typs under a signature*)
lcp@229
   123
datatype ctyp = Ctyp of {sign: Sign.sg,  T: typ};
lcp@229
   124
lcp@229
   125
fun rep_ctyp(Ctyp ctyp) = ctyp;
lcp@229
   126
fun typ_of (Ctyp{sign,T}) = T;
lcp@229
   127
lcp@229
   128
fun ctyp_of sign T =
lcp@229
   129
    case Type.type_errors (#tsig(Sign.rep_sg sign)) (T,[]) of
lcp@229
   130
      []   => Ctyp{sign= sign,T= T}
lcp@229
   131
    | errs =>  error (cat_lines ("Error in type:" :: errs));
lcp@229
   132
lcp@229
   133
(** Certified Terms **)
lcp@229
   134
lcp@229
   135
(*Certified terms under a signature, with checked typ and maxidx of Vars*)
lcp@229
   136
datatype cterm = Cterm of {sign: Sign.sg,  t: term,  T: typ,  maxidx: int};
lcp@229
   137
lcp@229
   138
fun rep_cterm (Cterm args) = args;
lcp@229
   139
lcp@229
   140
(*Return the underlying term*)
lcp@229
   141
fun term_of (Cterm{t,...}) = t;
lcp@229
   142
lcp@229
   143
(*Create a cterm by checking a "raw" term with respect to a signature*)
lcp@229
   144
fun cterm_of sign t =
lcp@229
   145
  case  Sign.term_errors sign t  of
lcp@229
   146
      [] => Cterm{sign=sign, t=t, T= type_of t, maxidx= maxidx_of_term t}
lcp@229
   147
    | errs => raise TERM(cat_lines("Term not in signature"::errs), [t]);
lcp@229
   148
lcp@229
   149
(*dest_implies for cterms.  Note T=prop below*)
lcp@229
   150
fun dest_cimplies (Cterm{sign, T, maxidx, t=Const("==>",_) $ A $ B}) = 
lcp@229
   151
       (Cterm{sign=sign, T=T, maxidx=maxidx, t=A},
lcp@229
   152
	Cterm{sign=sign, T=T, maxidx=maxidx, t=B})
lcp@229
   153
  | dest_cimplies ct = raise TERM("dest_cimplies", [term_of ct]);
lcp@229
   154
lcp@229
   155
(** Reading of cterms -- needed twice below! **)
lcp@229
   156
lcp@229
   157
(*Lexing, parsing, polymorphic typechecking of a term.*)
lcp@229
   158
fun read_def_cterm (sign, types, sorts) (a,T) =
lcp@229
   159
  let val {tsig, const_tab, syn,...} = Sign.rep_sg sign
lcp@229
   160
      val showtyp = Sign.string_of_typ sign
lcp@229
   161
      and showterm = Sign.string_of_term sign
lcp@229
   162
      fun termerr [] = ""
lcp@229
   163
        | termerr [t] = "\nInvolving this term:\n" ^ showterm t ^ "\n"
lcp@229
   164
        | termerr ts = "\nInvolving these terms:\n" ^
lcp@229
   165
                       cat_lines (map showterm ts)
lcp@229
   166
      val t = Syntax.read syn T a;
lcp@229
   167
      val (t',tye) = Type.infer_types (tsig, const_tab, types,
lcp@229
   168
                                       sorts, showtyp, T, t)
lcp@229
   169
                  handle TYPE (msg, Ts, ts) =>
lcp@229
   170
          error ("Type checking error: " ^ msg ^ "\n" ^
lcp@229
   171
                  cat_lines (map showtyp Ts) ^ termerr ts)
lcp@229
   172
  in (cterm_of sign t', tye)
lcp@229
   173
  end
lcp@229
   174
  handle TERM (msg, _) => error ("Error: " ^  msg);
lcp@229
   175
lcp@229
   176
fun read_cterm sign = #1 o (read_def_cterm (sign, K None, K None));
lcp@229
   177
lcp@229
   178
(**** META-THEOREMS ****)
lcp@229
   179
clasohm@0
   180
datatype thm = Thm of
clasohm@0
   181
    {sign: Sign.sg,  maxidx: int,  hyps: term list,  prop: term};
clasohm@0
   182
clasohm@0
   183
fun rep_thm (Thm x) = x;
clasohm@0
   184
clasohm@0
   185
(*Errors involving theorems*)
clasohm@0
   186
exception THM of string * int * thm list;
clasohm@0
   187
clasohm@0
   188
(*maps object-rule to tpairs *)
clasohm@0
   189
fun tpairs_of (Thm{prop,...}) = #1 (Logic.strip_flexpairs prop);
clasohm@0
   190
clasohm@0
   191
(*maps object-rule to premises *)
clasohm@0
   192
fun prems_of (Thm{prop,...}) =
clasohm@0
   193
    Logic.strip_imp_prems (Logic.skip_flexpairs prop);
clasohm@0
   194
clasohm@0
   195
(*counts premises in a rule*)
clasohm@0
   196
fun nprems_of (Thm{prop,...}) =
clasohm@0
   197
    Logic.count_prems (Logic.skip_flexpairs prop, 0);
clasohm@0
   198
clasohm@0
   199
(*maps object-rule to conclusion *)
clasohm@0
   200
fun concl_of (Thm{prop,...}) = Logic.strip_imp_concl prop;
clasohm@0
   201
lcp@229
   202
(*The statement of any Thm is a Cterm*)
lcp@229
   203
fun cprop_of (Thm{sign,maxidx,hyps,prop}) = 
lcp@229
   204
	Cterm{sign=sign, maxidx=maxidx, T=propT, t=prop};
lcp@229
   205
clasohm@0
   206
(*Stamps associated with a signature*)
clasohm@0
   207
val stamps_of_thm = #stamps o Sign.rep_sg o #sign o rep_thm;
clasohm@0
   208
clasohm@0
   209
(*Theories.  There is one pure theory.
clasohm@0
   210
  A theory can be extended.  Two theories can be merged.*)
clasohm@0
   211
datatype theory =
clasohm@0
   212
    Pure of {sign: Sign.sg}
clasohm@0
   213
  | Extend of {sign: Sign.sg,  axioms: thm Symtab.table,  thy: theory}
clasohm@0
   214
  | Merge of {sign: Sign.sg,  thy1: theory,  thy2: theory};
clasohm@0
   215
clasohm@0
   216
(*Errors involving theories*)
clasohm@0
   217
exception THEORY of string * theory list;
clasohm@0
   218
clasohm@0
   219
fun sign_of (Pure {sign}) = sign
clasohm@0
   220
  | sign_of (Extend {sign,...}) = sign
clasohm@0
   221
  | sign_of (Merge {sign,...}) = sign;
clasohm@0
   222
clasohm@0
   223
val syn_of = #syn o Sign.rep_sg o sign_of;
clasohm@0
   224
clasohm@0
   225
(*return the axioms of a theory and its ancestors*)
clasohm@0
   226
fun axioms_of (Pure _) = []
clasohm@0
   227
  | axioms_of (Extend{axioms,thy,...}) = Symtab.alist_of axioms
clasohm@0
   228
  | axioms_of (Merge{thy1,thy2,...}) = axioms_of thy1  @  axioms_of thy2;
clasohm@0
   229
clasohm@0
   230
(*return the immediate ancestors -- also distinguishes the kinds of theories*)
clasohm@0
   231
fun parents_of (Pure _) = []
clasohm@0
   232
  | parents_of (Extend{thy,...}) = [thy]
clasohm@0
   233
  | parents_of (Merge{thy1,thy2,...}) = [thy1,thy2];
clasohm@0
   234
clasohm@0
   235
clasohm@0
   236
(*Merge theories of two theorems.  Raise exception if incompatible.
clasohm@0
   237
  Prefers (via Sign.merge) the signature of th1.  *)
clasohm@0
   238
fun merge_theories(th1,th2) =
clasohm@0
   239
  let val Thm{sign=sign1,...} = th1 and Thm{sign=sign2,...} = th2
clasohm@0
   240
  in  Sign.merge (sign1,sign2)  end
clasohm@0
   241
  handle TERM _ => raise THM("incompatible signatures", 0, [th1,th2]);
clasohm@0
   242
clasohm@0
   243
(*Stamps associated with a theory*)
clasohm@0
   244
val stamps_of_thy = #stamps o Sign.rep_sg o sign_of;
clasohm@0
   245
clasohm@0
   246
clasohm@0
   247
(**** Primitive rules ****)
clasohm@0
   248
clasohm@0
   249
(* discharge all assumptions t from ts *)
clasohm@0
   250
val disch = gen_rem (op aconv);
clasohm@0
   251
clasohm@0
   252
(*The assumption rule A|-A in a theory  *)
clasohm@0
   253
fun assume ct : thm = 
lcp@229
   254
  let val {sign, t=prop, T, maxidx} = rep_cterm ct
clasohm@0
   255
  in  if T<>propT then  
clasohm@0
   256
	raise THM("assume: assumptions must have type prop", 0, [])
clasohm@0
   257
      else if maxidx <> ~1 then
clasohm@0
   258
	raise THM("assume: assumptions may not contain scheme variables", 
clasohm@0
   259
		  maxidx, [])
clasohm@0
   260
      else Thm{sign = sign, maxidx = ~1, hyps = [prop], prop = prop}
clasohm@0
   261
  end;
clasohm@0
   262
clasohm@0
   263
(* Implication introduction  
clasohm@0
   264
	      A |- B
clasohm@0
   265
	      -------
clasohm@0
   266
	      A ==> B    *)
clasohm@0
   267
fun implies_intr cA (thB as Thm{sign,maxidx,hyps,prop}) : thm =
lcp@229
   268
  let val {sign=signA, t=A, T, maxidx=maxidxA} = rep_cterm cA
clasohm@0
   269
  in  if T<>propT then
clasohm@0
   270
	raise THM("implies_intr: assumptions must have type prop", 0, [thB])
clasohm@0
   271
      else Thm{sign= Sign.merge (sign,signA),  maxidx= max[maxidxA, maxidx], 
clasohm@0
   272
	     hyps= disch(hyps,A),  prop= implies$A$prop}
clasohm@0
   273
      handle TERM _ =>
clasohm@0
   274
        raise THM("implies_intr: incompatible signatures", 0, [thB])
clasohm@0
   275
  end;
clasohm@0
   276
clasohm@0
   277
(* Implication elimination
clasohm@0
   278
	A ==> B       A
clasohm@0
   279
	---------------
clasohm@0
   280
		B      *)
clasohm@0
   281
fun implies_elim thAB thA : thm =
clasohm@0
   282
    let val Thm{maxidx=maxA, hyps=hypsA, prop=propA,...} = thA
clasohm@0
   283
	and Thm{sign, maxidx, hyps, prop,...} = thAB;
clasohm@0
   284
	fun err(a) = raise THM("implies_elim: "^a, 0, [thAB,thA])
clasohm@0
   285
    in  case prop of
clasohm@0
   286
	    imp$A$B => 
clasohm@0
   287
		if imp=implies andalso  A aconv propA
clasohm@0
   288
		then  Thm{sign= merge_theories(thAB,thA),
clasohm@0
   289
			  maxidx= max[maxA,maxidx], 
clasohm@0
   290
			  hyps= hypsA union hyps,  (*dups suppressed*)
clasohm@0
   291
			  prop= B}
clasohm@0
   292
		else err("major premise")
clasohm@0
   293
	  | _ => err("major premise")
clasohm@0
   294
    end;
clasohm@0
   295
      
clasohm@0
   296
(* Forall introduction.  The Free or Var x must not be free in the hypotheses.
clasohm@0
   297
     A
clasohm@0
   298
   ------
clasohm@0
   299
   !!x.A       *)
clasohm@0
   300
fun forall_intr cx (th as Thm{sign,maxidx,hyps,prop}) =
lcp@229
   301
  let val x = term_of cx;
clasohm@0
   302
      fun result(a,T) = Thm{sign= sign, maxidx= maxidx, hyps= hyps,
clasohm@0
   303
	                    prop= all(T) $ Abs(a, T, abstract_over (x,prop))}
clasohm@0
   304
  in  case x of
clasohm@0
   305
	Free(a,T) => 
clasohm@0
   306
	  if exists (apl(x, Logic.occs)) hyps 
clasohm@0
   307
	  then  raise THM("forall_intr: variable free in assumptions", 0, [th])
clasohm@0
   308
	  else  result(a,T)
clasohm@0
   309
      | Var((a,_),T) => result(a,T)
clasohm@0
   310
      | _ => raise THM("forall_intr: not a variable", 0, [th])
clasohm@0
   311
  end;
clasohm@0
   312
clasohm@0
   313
(* Forall elimination
clasohm@0
   314
	      !!x.A
clasohm@0
   315
	     --------
clasohm@0
   316
	      A[t/x]     *)
clasohm@0
   317
fun forall_elim ct (th as Thm{sign,maxidx,hyps,prop}) : thm =
lcp@229
   318
  let val {sign=signt, t, T, maxidx=maxt} = rep_cterm ct
clasohm@0
   319
  in  case prop of
clasohm@0
   320
	  Const("all",Type("fun",[Type("fun",[qary,_]),_])) $ A =>
clasohm@0
   321
	    if T<>qary then
clasohm@0
   322
		raise THM("forall_elim: type mismatch", 0, [th])
clasohm@0
   323
	    else Thm{sign= Sign.merge(sign,signt), 
clasohm@0
   324
		     maxidx= max[maxidx, maxt],
clasohm@0
   325
		     hyps= hyps,  prop= betapply(A,t)}
clasohm@0
   326
	| _ => raise THM("forall_elim: not quantified", 0, [th])
clasohm@0
   327
  end
clasohm@0
   328
  handle TERM _ =>
clasohm@0
   329
	 raise THM("forall_elim: incompatible signatures", 0, [th]);
clasohm@0
   330
clasohm@0
   331
clasohm@0
   332
(*** Equality ***)
clasohm@0
   333
clasohm@0
   334
(*Definition of the relation =?= *)
clasohm@0
   335
val flexpair_def =
clasohm@0
   336
  Thm{sign= Sign.pure, hyps= [], maxidx= 0, 
lcp@229
   337
      prop= term_of 
lcp@229
   338
	      (read_cterm Sign.pure 
clasohm@0
   339
	         ("(?t =?= ?u) == (?t == ?u::?'a::{})", propT))};
clasohm@0
   340
clasohm@0
   341
(*The reflexivity rule: maps  t   to the theorem   t==t   *)
clasohm@0
   342
fun reflexive ct = 
lcp@229
   343
  let val {sign, t, T, maxidx} = rep_cterm ct
clasohm@0
   344
  in  Thm{sign= sign, hyps= [], maxidx= maxidx, prop= Logic.mk_equals(t,t)}
clasohm@0
   345
  end;
clasohm@0
   346
clasohm@0
   347
(*The symmetry rule
clasohm@0
   348
    t==u
clasohm@0
   349
    ----
clasohm@0
   350
    u==t         *)
clasohm@0
   351
fun symmetric (th as Thm{sign,hyps,prop,maxidx}) =
clasohm@0
   352
  case prop of
clasohm@0
   353
      (eq as Const("==",_)) $ t $ u =>
clasohm@0
   354
	  Thm{sign=sign, hyps=hyps, maxidx=maxidx, prop= eq$u$t} 
clasohm@0
   355
    | _ => raise THM("symmetric", 0, [th]);
clasohm@0
   356
clasohm@0
   357
(*The transitive rule
clasohm@0
   358
    t1==u    u==t2
clasohm@0
   359
    ------------
clasohm@0
   360
        t1==t2      *)
clasohm@0
   361
fun transitive th1 th2 =
clasohm@0
   362
  let val Thm{maxidx=max1, hyps=hyps1, prop=prop1,...} = th1
clasohm@0
   363
      and Thm{maxidx=max2, hyps=hyps2, prop=prop2,...} = th2;
clasohm@0
   364
      fun err(msg) = raise THM("transitive: "^msg, 0, [th1,th2])
clasohm@0
   365
  in case (prop1,prop2) of
clasohm@0
   366
       ((eq as Const("==",_)) $ t1 $ u, Const("==",_) $ u' $ t2) =>
clasohm@0
   367
	  if not (u aconv u') then err"middle term"  else
clasohm@0
   368
	      Thm{sign= merge_theories(th1,th2), hyps= hyps1 union hyps2, 
clasohm@0
   369
		  maxidx= max[max1,max2], prop= eq$t1$t2}
clasohm@0
   370
     | _ =>  err"premises"
clasohm@0
   371
  end;
clasohm@0
   372
clasohm@0
   373
(*Beta-conversion: maps (%(x)t)(u) to the theorem  (%(x)t)(u) == t[u/x]   *)
clasohm@0
   374
fun beta_conversion ct = 
lcp@229
   375
  let val {sign, t, T, maxidx} = rep_cterm ct
clasohm@0
   376
  in  case t of
clasohm@0
   377
	  Abs(_,_,bodt) $ u => 
clasohm@0
   378
	    Thm{sign= sign,  hyps= [],  
clasohm@0
   379
		maxidx= maxidx_of_term t, 
clasohm@0
   380
		prop= Logic.mk_equals(t, subst_bounds([u],bodt))}
clasohm@0
   381
	| _ =>  raise THM("beta_conversion: not a redex", 0, [])
clasohm@0
   382
  end;
clasohm@0
   383
clasohm@0
   384
(*The extensionality rule   (proviso: x not free in f, g, or hypotheses)
clasohm@0
   385
    f(x) == g(x)
clasohm@0
   386
    ------------
clasohm@0
   387
       f == g    *)
clasohm@0
   388
fun extensional (th as Thm{sign,maxidx,hyps,prop}) =
clasohm@0
   389
  case prop of
clasohm@0
   390
    (Const("==",_)) $ (f$x) $ (g$y) =>
clasohm@0
   391
      let fun err(msg) = raise THM("extensional: "^msg, 0, [th]) 
clasohm@0
   392
      in (if x<>y then err"different variables" else
clasohm@0
   393
          case y of
clasohm@0
   394
		Free _ => 
clasohm@0
   395
		  if exists (apl(y, Logic.occs)) (f::g::hyps) 
clasohm@0
   396
		  then err"variable free in hyps or functions"    else  ()
clasohm@0
   397
	      | Var _ => 
clasohm@0
   398
		  if Logic.occs(y,f)  orelse  Logic.occs(y,g) 
clasohm@0
   399
		  then err"variable free in functions"   else  ()
clasohm@0
   400
	      | _ => err"not a variable");
clasohm@0
   401
	  Thm{sign=sign, hyps=hyps, maxidx=maxidx, 
clasohm@0
   402
	      prop= Logic.mk_equals(f,g)} 
clasohm@0
   403
      end
clasohm@0
   404
 | _ =>  raise THM("extensional: premise", 0, [th]);
clasohm@0
   405
clasohm@0
   406
(*The abstraction rule.  The Free or Var x must not be free in the hypotheses.
clasohm@0
   407
  The bound variable will be named "a" (since x will be something like x320)
clasohm@0
   408
          t == u
clasohm@0
   409
    ----------------
clasohm@0
   410
      %(x)t == %(x)u     *)
clasohm@0
   411
fun abstract_rule a cx (th as Thm{sign,maxidx,hyps,prop}) =
lcp@229
   412
  let val x = term_of cx;
clasohm@0
   413
      val (t,u) = Logic.dest_equals prop  
clasohm@0
   414
	    handle TERM _ =>
clasohm@0
   415
		raise THM("abstract_rule: premise not an equality", 0, [th])
clasohm@0
   416
      fun result T =
clasohm@0
   417
            Thm{sign= sign, maxidx= maxidx, hyps= hyps,
clasohm@0
   418
	        prop= Logic.mk_equals(Abs(a, T, abstract_over (x,t)),
clasohm@0
   419
		  	              Abs(a, T, abstract_over (x,u)))}
clasohm@0
   420
  in  case x of
clasohm@0
   421
	Free(_,T) => 
clasohm@0
   422
	 if exists (apl(x, Logic.occs)) hyps 
clasohm@0
   423
	 then raise THM("abstract_rule: variable free in assumptions", 0, [th])
clasohm@0
   424
	 else result T
clasohm@0
   425
      | Var(_,T) => result T
clasohm@0
   426
      | _ => raise THM("abstract_rule: not a variable", 0, [th])
clasohm@0
   427
  end;
clasohm@0
   428
clasohm@0
   429
(*The combination rule
clasohm@0
   430
    f==g    t==u
clasohm@0
   431
    ------------
clasohm@0
   432
     f(t)==g(u)      *)
clasohm@0
   433
fun combination th1 th2 =
clasohm@0
   434
  let val Thm{maxidx=max1, hyps=hyps1, prop=prop1,...} = th1
clasohm@0
   435
      and Thm{maxidx=max2, hyps=hyps2, prop=prop2,...} = th2
clasohm@0
   436
  in  case (prop1,prop2)  of
clasohm@0
   437
       (Const("==",_) $ f $ g, Const("==",_) $ t $ u) =>
clasohm@0
   438
	      Thm{sign= merge_theories(th1,th2), hyps= hyps1 union hyps2, 
clasohm@0
   439
		  maxidx= max[max1,max2], prop= Logic.mk_equals(f$t, g$u)}
clasohm@0
   440
     | _ =>  raise THM("combination: premises", 0, [th1,th2])
clasohm@0
   441
  end;
clasohm@0
   442
clasohm@0
   443
clasohm@0
   444
(*The equal propositions rule
clasohm@0
   445
    A==B    A
clasohm@0
   446
    ---------
clasohm@0
   447
        B          *)
clasohm@0
   448
fun equal_elim th1 th2 =
clasohm@0
   449
  let val Thm{maxidx=max1, hyps=hyps1, prop=prop1,...} = th1
clasohm@0
   450
      and Thm{maxidx=max2, hyps=hyps2, prop=prop2,...} = th2;
clasohm@0
   451
      fun err(msg) = raise THM("equal_elim: "^msg, 0, [th1,th2])
clasohm@0
   452
  in  case prop1  of
clasohm@0
   453
       Const("==",_) $ A $ B =>
clasohm@0
   454
	  if not (prop2 aconv A) then err"not equal"  else
clasohm@0
   455
	      Thm{sign= merge_theories(th1,th2), hyps= hyps1 union hyps2, 
clasohm@0
   456
		  maxidx= max[max1,max2], prop= B}
clasohm@0
   457
     | _ =>  err"major premise"
clasohm@0
   458
  end;
clasohm@0
   459
clasohm@0
   460
clasohm@0
   461
(* Equality introduction
clasohm@0
   462
    A==>B    B==>A
clasohm@0
   463
    -------------
clasohm@0
   464
         A==B            *)
clasohm@0
   465
fun equal_intr th1 th2 =
clasohm@0
   466
let val Thm{maxidx=max1, hyps=hyps1, prop=prop1,...} = th1
clasohm@0
   467
    and Thm{maxidx=max2, hyps=hyps2, prop=prop2,...} = th2;
clasohm@0
   468
    fun err(msg) = raise THM("equal_intr: "^msg, 0, [th1,th2])
clasohm@0
   469
in case (prop1,prop2) of
clasohm@0
   470
     (Const("==>",_) $ A $ B, Const("==>",_) $ B' $ A')  =>
clasohm@0
   471
	if A aconv A' andalso B aconv B'
clasohm@0
   472
	then Thm{sign= merge_theories(th1,th2), hyps= hyps1 union hyps2, 
clasohm@0
   473
		 maxidx= max[max1,max2], prop= Logic.mk_equals(A,B)}
clasohm@0
   474
	else err"not equal"
clasohm@0
   475
   | _ =>  err"premises"
clasohm@0
   476
end;
clasohm@0
   477
clasohm@0
   478
(**** Derived rules ****)
clasohm@0
   479
clasohm@0
   480
(*Discharge all hypotheses (need not verify cterms)
clasohm@0
   481
  Repeated hypotheses are discharged only once;  fold cannot do this*)
clasohm@0
   482
fun implies_intr_hyps (Thm{sign, maxidx, hyps=A::As, prop}) =
clasohm@0
   483
      implies_intr_hyps
clasohm@0
   484
	    (Thm{sign=sign,  maxidx=maxidx, 
clasohm@0
   485
	         hyps= disch(As,A),  prop= implies$A$prop})
clasohm@0
   486
  | implies_intr_hyps th = th;
clasohm@0
   487
clasohm@0
   488
(*Smash" unifies the list of term pairs leaving no flex-flex pairs.
clasohm@0
   489
  Instantiates the theorem and deletes trivial tpairs. 
clasohm@0
   490
  Resulting sequence may contain multiple elements if the tpairs are
clasohm@0
   491
    not all flex-flex. *)
clasohm@0
   492
fun flexflex_rule (Thm{sign,maxidx,hyps,prop}) =
clasohm@0
   493
  let fun newthm env = 
clasohm@0
   494
	  let val (tpairs,horn) = 
clasohm@0
   495
			Logic.strip_flexpairs (Envir.norm_term env prop)
clasohm@0
   496
	        (*Remove trivial tpairs, of the form t=t*)
clasohm@0
   497
	      val distpairs = filter (not o op aconv) tpairs
clasohm@0
   498
	      val newprop = Logic.list_flexpairs(distpairs, horn)
clasohm@0
   499
	  in  Thm{sign= sign, hyps= hyps, 
clasohm@0
   500
		  maxidx= maxidx_of_term newprop, prop= newprop}
clasohm@0
   501
	  end;
clasohm@0
   502
      val (tpairs,_) = Logic.strip_flexpairs prop
clasohm@0
   503
  in Sequence.maps newthm
clasohm@0
   504
	    (Unify.smash_unifiers(sign, Envir.empty maxidx, tpairs))
clasohm@0
   505
  end;
clasohm@0
   506
clasohm@0
   507
(*Instantiation of Vars
clasohm@0
   508
		      A
clasohm@0
   509
	     --------------------
clasohm@0
   510
	      A[t1/v1,....,tn/vn]     *)
clasohm@0
   511
clasohm@0
   512
(*Check that all the terms are Vars and are distinct*)
clasohm@0
   513
fun instl_ok ts = forall is_Var ts andalso null(findrep ts);
clasohm@0
   514
clasohm@0
   515
(*For instantiate: process pair of cterms, merge theories*)
clasohm@0
   516
fun add_ctpair ((ct,cu), (sign,tpairs)) =
lcp@229
   517
  let val {sign=signt, t=t, T= T, ...} = rep_cterm ct
lcp@229
   518
      and {sign=signu, t=u, T= U, ...} = rep_cterm cu
clasohm@0
   519
  in  if T=U  then (Sign.merge(sign, Sign.merge(signt, signu)), (t,u)::tpairs)
clasohm@0
   520
      else raise TYPE("add_ctpair", [T,U], [t,u])
clasohm@0
   521
  end;
clasohm@0
   522
clasohm@0
   523
fun add_ctyp ((v,ctyp), (sign',vTs)) =
lcp@229
   524
  let val {T,sign} = rep_ctyp ctyp
clasohm@0
   525
  in (Sign.merge(sign,sign'), (v,T)::vTs) end;
clasohm@0
   526
clasohm@0
   527
(*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   528
  Instantiates distinct Vars by terms of same type.
clasohm@0
   529
  Normalizes the new theorem! *)
clasohm@0
   530
fun instantiate (vcTs,ctpairs)  (th as Thm{sign,maxidx,hyps,prop}) = 
clasohm@0
   531
  let val (newsign,tpairs) = foldr add_ctpair (ctpairs, (sign,[]));
clasohm@0
   532
      val (newsign,vTs) = foldr add_ctyp (vcTs, (newsign,[]));
nipkow@193
   533
      val newprop = 
nipkow@193
   534
	    Envir.norm_term (Envir.empty 0) 
nipkow@193
   535
	      (subst_atomic tpairs 
nipkow@193
   536
	       (Type.inst_term_tvars(#tsig(Sign.rep_sg newsign),vTs) prop))
clasohm@0
   537
      val newth = Thm{sign= newsign, hyps= hyps,
clasohm@0
   538
		      maxidx= maxidx_of_term newprop, prop= newprop}
nipkow@193
   539
  in  if not(instl_ok(map #1 tpairs)) 
nipkow@193
   540
      then raise THM("instantiate: variables not distinct", 0, [th])
nipkow@193
   541
      else if not(null(findrep(map #1 vTs)))
nipkow@193
   542
      then raise THM("instantiate: type variables not distinct", 0, [th])
nipkow@193
   543
      else (*Check types of Vars for agreement*)
nipkow@193
   544
      case findrep (map (#1 o dest_Var) (term_vars newprop)) of
nipkow@193
   545
	  ix::_ => raise THM("instantiate: conflicting types for variable " ^
nipkow@193
   546
			     Syntax.string_of_vname ix ^ "\n", 0, [newth])
nipkow@193
   547
	| [] => 
nipkow@193
   548
	     case findrep (map #1 (term_tvars newprop)) of
nipkow@193
   549
	     ix::_ => raise THM
nipkow@193
   550
		    ("instantiate: conflicting sorts for type variable " ^
nipkow@193
   551
		     Syntax.string_of_vname ix ^ "\n", 0, [newth])
nipkow@193
   552
        | [] => newth
clasohm@0
   553
  end
clasohm@0
   554
  handle TERM _ => 
clasohm@0
   555
           raise THM("instantiate: incompatible signatures",0,[th])
nipkow@193
   556
       | TYPE _ => raise THM("instantiate: type conflict", 0, [th]);
clasohm@0
   557
clasohm@0
   558
(*The trivial implication A==>A, justified by assume and forall rules.
clasohm@0
   559
  A can contain Vars, not so for assume!   *)
clasohm@0
   560
fun trivial ct : thm = 
lcp@229
   561
  let val {sign, t=A, T, maxidx} = rep_cterm ct
clasohm@0
   562
  in  if T<>propT then  
clasohm@0
   563
	    raise THM("trivial: the term must have type prop", 0, [])
clasohm@0
   564
      else Thm{sign= sign, maxidx= maxidx, hyps= [], prop= implies$A$A}
clasohm@0
   565
  end;
clasohm@0
   566
clasohm@0
   567
(* Replace all TFrees not in the hyps by new TVars *)
clasohm@0
   568
fun varifyT(Thm{sign,maxidx,hyps,prop}) =
clasohm@0
   569
  let val tfrees = foldr add_term_tfree_names (hyps,[])
clasohm@0
   570
  in Thm{sign=sign, maxidx=max[0,maxidx], hyps=hyps,
clasohm@0
   571
	 prop= Type.varify(prop,tfrees)}
clasohm@0
   572
  end;
clasohm@0
   573
clasohm@0
   574
(* Replace all TVars by new TFrees *)
clasohm@0
   575
fun freezeT(Thm{sign,maxidx,hyps,prop}) =
clasohm@0
   576
  let val prop' = Type.freeze (K true) prop
clasohm@0
   577
  in Thm{sign=sign, maxidx=maxidx_of_term prop', hyps=hyps, prop=prop'} end;
clasohm@0
   578
clasohm@0
   579
clasohm@0
   580
(*** Inference rules for tactics ***)
clasohm@0
   581
clasohm@0
   582
(*Destruct proof state into constraints, other goals, goal(i), rest *)
clasohm@0
   583
fun dest_state (state as Thm{prop,...}, i) =
clasohm@0
   584
  let val (tpairs,horn) = Logic.strip_flexpairs prop
clasohm@0
   585
  in  case  Logic.strip_prems(i, [], horn) of
clasohm@0
   586
          (B::rBs, C) => (tpairs, rev rBs, B, C)
clasohm@0
   587
        | _ => raise THM("dest_state", i, [state])
clasohm@0
   588
  end
clasohm@0
   589
  handle TERM _ => raise THM("dest_state", i, [state]);
clasohm@0
   590
clasohm@0
   591
(*Increment variables and parameters of rule as required for
clasohm@0
   592
  resolution with goal i of state. *)
clasohm@0
   593
fun lift_rule (state, i) orule =
clasohm@0
   594
  let val Thm{prop=sprop,maxidx=smax,...} = state;
clasohm@0
   595
      val (Bi::_, _) = Logic.strip_prems(i, [], Logic.skip_flexpairs sprop)
clasohm@0
   596
	handle TERM _ => raise THM("lift_rule", i, [orule,state]);
clasohm@0
   597
      val (lift_abs,lift_all) = Logic.lift_fns(Bi,smax+1);
clasohm@0
   598
      val (Thm{sign,maxidx,hyps,prop}) = orule
clasohm@0
   599
      val (tpairs,As,B) = Logic.strip_horn prop
clasohm@0
   600
  in  Thm{hyps=hyps, sign= merge_theories(state,orule),
clasohm@0
   601
	  maxidx= maxidx+smax+1,
clasohm@0
   602
	  prop= Logic.rule_of(map (pairself lift_abs) tpairs,
clasohm@0
   603
			      map lift_all As,    lift_all B)}
clasohm@0
   604
  end;
clasohm@0
   605
clasohm@0
   606
(*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
clasohm@0
   607
fun assumption i state =
clasohm@0
   608
  let val Thm{sign,maxidx,hyps,prop} = state;
clasohm@0
   609
      val (tpairs, Bs, Bi, C) = dest_state(state,i)
clasohm@0
   610
      fun newth (env as Envir.Envir{maxidx,asol,iTs}, tpairs) =
clasohm@0
   611
	  Thm{sign=sign, hyps=hyps, maxidx=maxidx, prop=
clasohm@0
   612
	    case (Envir.alist_of_olist asol, iTs) of
clasohm@0
   613
		(*avoid wasted normalizations*)
clasohm@0
   614
	        ([],[]) => Logic.rule_of(tpairs, Bs, C)
clasohm@0
   615
	      | _ => (*normalize the new rule fully*)
clasohm@0
   616
		      Envir.norm_term env (Logic.rule_of(tpairs, Bs, C))};
clasohm@0
   617
      fun addprfs [] = Sequence.null
clasohm@0
   618
        | addprfs ((t,u)::apairs) = Sequence.seqof (fn()=> Sequence.pull
clasohm@0
   619
             (Sequence.mapp newth
clasohm@0
   620
	        (Unify.unifiers(sign,Envir.empty maxidx, (t,u)::tpairs)) 
clasohm@0
   621
	        (addprfs apairs)))
clasohm@0
   622
  in  addprfs (Logic.assum_pairs Bi)  end;
clasohm@0
   623
clasohm@0
   624
(*Solve subgoal Bi of proof state B1...Bn/C by assumption. 
clasohm@0
   625
  Checks if Bi's conclusion is alpha-convertible to one of its assumptions*)
clasohm@0
   626
fun eq_assumption i state =
clasohm@0
   627
  let val Thm{sign,maxidx,hyps,prop} = state;
clasohm@0
   628
      val (tpairs, Bs, Bi, C) = dest_state(state,i)
clasohm@0
   629
  in  if exists (op aconv) (Logic.assum_pairs Bi)
clasohm@0
   630
      then Thm{sign=sign, hyps=hyps, maxidx=maxidx, 
clasohm@0
   631
	       prop=Logic.rule_of(tpairs, Bs, C)}
clasohm@0
   632
      else  raise THM("eq_assumption", 0, [state])
clasohm@0
   633
  end;
clasohm@0
   634
clasohm@0
   635
clasohm@0
   636
(** User renaming of parameters in a subgoal **)
clasohm@0
   637
clasohm@0
   638
(*Calls error rather than raising an exception because it is intended
clasohm@0
   639
  for top-level use -- exception handling would not make sense here.
clasohm@0
   640
  The names in cs, if distinct, are used for the innermost parameters;
clasohm@0
   641
   preceding parameters may be renamed to make all params distinct.*)
clasohm@0
   642
fun rename_params_rule (cs, i) state =
clasohm@0
   643
  let val Thm{sign,maxidx,hyps,prop} = state
clasohm@0
   644
      val (tpairs, Bs, Bi, C) = dest_state(state,i)
clasohm@0
   645
      val iparams = map #1 (Logic.strip_params Bi)
clasohm@0
   646
      val short = length iparams - length cs
clasohm@0
   647
      val newnames = 
clasohm@0
   648
	    if short<0 then error"More names than abstractions!"
clasohm@0
   649
	    else variantlist(take (short,iparams), cs) @ cs
clasohm@0
   650
      val freenames = map (#1 o dest_Free) (term_frees prop)
clasohm@0
   651
      val newBi = Logic.list_rename_params (newnames, Bi)
clasohm@0
   652
  in  
clasohm@0
   653
  case findrep cs of
clasohm@0
   654
     c::_ => error ("Bound variables not distinct: " ^ c)
clasohm@0
   655
   | [] => (case cs inter freenames of
clasohm@0
   656
       a::_ => error ("Bound/Free variable clash: " ^ a)
clasohm@0
   657
     | [] => Thm{sign=sign, hyps=hyps, maxidx=maxidx, prop=
clasohm@0
   658
		    Logic.rule_of(tpairs, Bs@[newBi], C)})
clasohm@0
   659
  end;
clasohm@0
   660
clasohm@0
   661
(*** Preservation of bound variable names ***)
clasohm@0
   662
clasohm@0
   663
(*Scan a pair of terms; while they are similar, 
clasohm@0
   664
  accumulate corresponding bound vars in "al"*)
clasohm@0
   665
fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = match_bvs(s,t,(x,y)::al)
clasohm@0
   666
  | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
clasohm@0
   667
  | match_bvs(_,_,al) = al;
clasohm@0
   668
clasohm@0
   669
(* strip abstractions created by parameters *)
clasohm@0
   670
fun match_bvars((s,t),al) = match_bvs(strip_abs_body s, strip_abs_body t, al);
clasohm@0
   671
clasohm@0
   672
clasohm@0
   673
(* strip_apply f A(,B) strips off all assumptions/parameters from A 
clasohm@0
   674
   introduced by lifting over B, and applies f to remaining part of A*)
clasohm@0
   675
fun strip_apply f =
clasohm@0
   676
  let fun strip(Const("==>",_)$ A1 $ B1,
clasohm@0
   677
		Const("==>",_)$ _  $ B2) = implies $ A1 $ strip(B1,B2)
clasohm@0
   678
	| strip((c as Const("all",_)) $ Abs(a,T,t1),
clasohm@0
   679
		      Const("all",_)  $ Abs(_,_,t2)) = c$Abs(a,T,strip(t1,t2))
clasohm@0
   680
	| strip(A,_) = f A
clasohm@0
   681
  in strip end;
clasohm@0
   682
clasohm@0
   683
(*Use the alist to rename all bound variables and some unknowns in a term
clasohm@0
   684
  dpairs = current disagreement pairs;  tpairs = permanent ones (flexflex);
clasohm@0
   685
  Preserves unknowns in tpairs and on lhs of dpairs. *)
clasohm@0
   686
fun rename_bvs([],_,_,_) = I
clasohm@0
   687
  | rename_bvs(al,dpairs,tpairs,B) =
clasohm@0
   688
    let val vars = foldr add_term_vars 
clasohm@0
   689
			(map fst dpairs @ map fst tpairs @ map snd tpairs, [])
clasohm@0
   690
	(*unknowns appearing elsewhere be preserved!*)
clasohm@0
   691
	val vids = map (#1 o #1 o dest_Var) vars;
clasohm@0
   692
	fun rename(t as Var((x,i),T)) =
clasohm@0
   693
		(case assoc(al,x) of
clasohm@0
   694
		   Some(y) => if x mem vids orelse y mem vids then t
clasohm@0
   695
			      else Var((y,i),T)
clasohm@0
   696
		 | None=> t)
clasohm@0
   697
          | rename(Abs(x,T,t)) =
clasohm@0
   698
	      Abs(case assoc(al,x) of Some(y) => y | None => x,
clasohm@0
   699
		  T, rename t)
clasohm@0
   700
          | rename(f$t) = rename f $ rename t
clasohm@0
   701
          | rename(t) = t;
clasohm@0
   702
	fun strip_ren Ai = strip_apply rename (Ai,B)
clasohm@0
   703
    in strip_ren end;
clasohm@0
   704
clasohm@0
   705
(*Function to rename bounds/unknowns in the argument, lifted over B*)
clasohm@0
   706
fun rename_bvars(dpairs, tpairs, B) =
clasohm@0
   707
	rename_bvs(foldr match_bvars (dpairs,[]), dpairs, tpairs, B);
clasohm@0
   708
clasohm@0
   709
clasohm@0
   710
(*** RESOLUTION ***)
clasohm@0
   711
clasohm@0
   712
(*strip off pairs of assumptions/parameters in parallel -- they are
clasohm@0
   713
  identical because of lifting*)
clasohm@0
   714
fun strip_assums2 (Const("==>", _) $ _ $ B1, 
clasohm@0
   715
		   Const("==>", _) $ _ $ B2) = strip_assums2 (B1,B2)
clasohm@0
   716
  | strip_assums2 (Const("all",_)$Abs(a,T,t1),
clasohm@0
   717
		   Const("all",_)$Abs(_,_,t2)) = 
clasohm@0
   718
      let val (B1,B2) = strip_assums2 (t1,t2)
clasohm@0
   719
      in  (Abs(a,T,B1), Abs(a,T,B2))  end
clasohm@0
   720
  | strip_assums2 BB = BB;
clasohm@0
   721
clasohm@0
   722
clasohm@0
   723
(*Composition of object rule r=(A1...Am/B) with proof state s=(B1...Bn/C)
clasohm@0
   724
  Unifies B with Bi, replacing subgoal i    (1 <= i <= n)  
clasohm@0
   725
  If match then forbid instantiations in proof state
clasohm@0
   726
  If lifted then shorten the dpair using strip_assums2.
clasohm@0
   727
  If eres_flg then simultaneously proves A1 by assumption.
clasohm@0
   728
  nsubgoal is the number of new subgoals (written m above). 
clasohm@0
   729
  Curried so that resolution calls dest_state only once.
clasohm@0
   730
*)
clasohm@0
   731
local open Sequence; exception Bicompose
clasohm@0
   732
in
clasohm@0
   733
fun bicompose_aux match (state, (stpairs, Bs, Bi, C), lifted) 
clasohm@0
   734
                        (eres_flg, orule, nsubgoal) =
clasohm@0
   735
 let val Thm{maxidx=smax, hyps=shyps, ...} = state
clasohm@0
   736
     and Thm{maxidx=rmax, hyps=rhyps, prop=rprop,...} = orule;
clasohm@0
   737
     val sign = merge_theories(state,orule);
clasohm@0
   738
     (** Add new theorem with prop = '[| Bs; As |] ==> C' to thq **)
clasohm@0
   739
     fun addth As ((env as Envir.Envir{maxidx,asol,iTs}, tpairs), thq) =
clasohm@0
   740
       let val minenv = case Envir.alist_of_olist asol of
clasohm@0
   741
			  [] => ~1  |  ((_,i),_) :: _ => i;
clasohm@0
   742
	   val minx = min (minenv :: map (fn ((_,i),_) => i) iTs);
clasohm@0
   743
	   val normt = Envir.norm_term env;
clasohm@0
   744
	   (*Perform minimal copying here by examining env*)
clasohm@0
   745
	   val normp = if minx = ~1 then (tpairs, Bs@As, C) 
clasohm@0
   746
		       else 
clasohm@0
   747
		       let val ntps = map (pairself normt) tpairs
clasohm@0
   748
		       in if minx>smax then (*no assignments in state*)
clasohm@0
   749
			    (ntps, Bs @ map normt As, C)
clasohm@0
   750
			  else if match then raise Bicompose
clasohm@0
   751
			  else (*normalize the new rule fully*)
clasohm@0
   752
			    (ntps, map normt (Bs @ As), normt C)
clasohm@0
   753
		       end
clasohm@0
   754
	   val th = Thm{sign=sign, hyps=rhyps union shyps, maxidx=maxidx,
clasohm@0
   755
			prop= Logic.rule_of normp}
clasohm@0
   756
        in  cons(th, thq)  end  handle Bicompose => thq
clasohm@0
   757
     val (rtpairs,rhorn) = Logic.strip_flexpairs(rprop);
clasohm@0
   758
     val (rAs,B) = Logic.strip_prems(nsubgoal, [], rhorn)
clasohm@0
   759
       handle TERM _ => raise THM("bicompose: rule", 0, [orule,state]);
clasohm@0
   760
     (*Modify assumptions, deleting n-th if n>0 for e-resolution*)
clasohm@0
   761
     fun newAs(As0, n, dpairs, tpairs) =
clasohm@0
   762
       let val As1 = if !Logic.auto_rename orelse not lifted then As0
clasohm@0
   763
		     else map (rename_bvars(dpairs,tpairs,B)) As0
clasohm@0
   764
       in (map (Logic.flatten_params n) As1)
clasohm@0
   765
	  handle TERM _ =>
clasohm@0
   766
	  raise THM("bicompose: 1st premise", 0, [orule])
clasohm@0
   767
       end;
clasohm@0
   768
     val env = Envir.empty(max[rmax,smax]);
clasohm@0
   769
     val BBi = if lifted then strip_assums2(B,Bi) else (B,Bi);
clasohm@0
   770
     val dpairs = BBi :: (rtpairs@stpairs);
clasohm@0
   771
     (*elim-resolution: try each assumption in turn.  Initially n=1*)
clasohm@0
   772
     fun tryasms (_, _, []) = null
clasohm@0
   773
       | tryasms (As, n, (t,u)::apairs) =
clasohm@0
   774
	  (case pull(Unify.unifiers(sign, env, (t,u)::dpairs))  of
clasohm@0
   775
	       None                   => tryasms (As, n+1, apairs)
clasohm@0
   776
	     | cell as Some((_,tpairs),_) => 
clasohm@0
   777
		   its_right (addth (newAs(As, n, [BBi,(u,t)], tpairs)))
clasohm@0
   778
		       (seqof (fn()=> cell),
clasohm@0
   779
		        seqof (fn()=> pull (tryasms (As, n+1, apairs)))));
clasohm@0
   780
     fun eres [] = raise THM("bicompose: no premises", 0, [orule,state])
clasohm@0
   781
       | eres (A1::As) = tryasms (As, 1, Logic.assum_pairs A1);
clasohm@0
   782
     (*ordinary resolution*)
clasohm@0
   783
     fun res(None) = null
clasohm@0
   784
       | res(cell as Some((_,tpairs),_)) = 
clasohm@0
   785
	     its_right (addth(newAs(rev rAs, 0, [BBi], tpairs)))
clasohm@0
   786
	 	       (seqof (fn()=> cell), null)
clasohm@0
   787
 in  if eres_flg then eres(rev rAs)
clasohm@0
   788
     else res(pull(Unify.unifiers(sign, env, dpairs)))
clasohm@0
   789
 end;
clasohm@0
   790
end;  (*open Sequence*)
clasohm@0
   791
clasohm@0
   792
clasohm@0
   793
fun bicompose match arg i state =
clasohm@0
   794
    bicompose_aux match (state, dest_state(state,i), false) arg;
clasohm@0
   795
clasohm@0
   796
(*Quick test whether rule is resolvable with the subgoal with hyps Hs
clasohm@0
   797
  and conclusion B.  If eres_flg then checks 1st premise of rule also*)
clasohm@0
   798
fun could_bires (Hs, B, eres_flg, rule) =
clasohm@0
   799
    let fun could_reshyp (A1::_) = exists (apl(A1,could_unify)) Hs
clasohm@0
   800
	  | could_reshyp [] = false;  (*no premise -- illegal*)
clasohm@0
   801
    in  could_unify(concl_of rule, B) andalso 
clasohm@0
   802
	(not eres_flg  orelse  could_reshyp (prems_of rule))
clasohm@0
   803
    end;
clasohm@0
   804
clasohm@0
   805
(*Bi-resolution of a state with a list of (flag,rule) pairs.
clasohm@0
   806
  Puts the rule above:  rule/state.  Renames vars in the rules. *)
clasohm@0
   807
fun biresolution match brules i state = 
clasohm@0
   808
    let val lift = lift_rule(state, i);
clasohm@0
   809
	val (stpairs, Bs, Bi, C) = dest_state(state,i)
clasohm@0
   810
	val B = Logic.strip_assums_concl Bi;
clasohm@0
   811
	val Hs = Logic.strip_assums_hyp Bi;
clasohm@0
   812
	val comp = bicompose_aux match (state, (stpairs, Bs, Bi, C), true);
clasohm@0
   813
	fun res [] = Sequence.null
clasohm@0
   814
	  | res ((eres_flg, rule)::brules) = 
clasohm@0
   815
	      if could_bires (Hs, B, eres_flg, rule)
clasohm@0
   816
	      then Sequence.seqof (*delay processing remainder til needed*)
clasohm@0
   817
	          (fn()=> Some(comp (eres_flg, lift rule, nprems_of rule),
clasohm@0
   818
			       res brules))
clasohm@0
   819
	      else res brules
clasohm@0
   820
    in  Sequence.flats (res brules)  end;
clasohm@0
   821
clasohm@0
   822
clasohm@0
   823
(**** THEORIES ****)
clasohm@0
   824
clasohm@0
   825
val pure_thy = Pure{sign = Sign.pure};
clasohm@0
   826
clasohm@0
   827
(*Look up the named axiom in the theory*)
clasohm@0
   828
fun get_axiom thy axname =
clasohm@0
   829
    let fun get (Pure _) = raise Match
clasohm@0
   830
	  | get (Extend{axioms,thy,...}) =
clasohm@0
   831
	     (case Symtab.lookup(axioms,axname) of
clasohm@0
   832
		  Some th => th
clasohm@0
   833
		| None => get thy)
clasohm@0
   834
 	 | get (Merge{thy1,thy2,...}) = 
clasohm@0
   835
		get thy1  handle Match => get thy2
clasohm@0
   836
    in  get thy
clasohm@0
   837
	handle Match => raise THEORY("get_axiom: No axiom "^axname, [thy])
clasohm@0
   838
    end;
clasohm@0
   839
clasohm@0
   840
(*Converts Frees to Vars: axioms can be written without question marks*)
clasohm@0
   841
fun prepare_axiom sign sP =
lcp@229
   842
    Logic.varify (term_of (read_cterm sign (sP,propT)));
clasohm@0
   843
clasohm@0
   844
(*Read an axiom for a new theory*)
clasohm@0
   845
fun read_ax sign (a, sP) : string*thm =
clasohm@0
   846
  let val prop = prepare_axiom sign sP
clasohm@0
   847
  in  (a, Thm{sign=sign, hyps=[], maxidx= maxidx_of_term prop, prop= prop}) 
clasohm@0
   848
  end
clasohm@0
   849
  handle ERROR =>
clasohm@0
   850
	error("extend_theory: The error above occurred in axiom " ^ a);
clasohm@0
   851
clasohm@0
   852
fun mk_axioms sign axpairs =
clasohm@0
   853
	Symtab.st_of_alist(map (read_ax sign) axpairs, Symtab.null)
clasohm@0
   854
	handle Symtab.DUPLICATE(a) => error("Two axioms named " ^ a);
clasohm@0
   855
clasohm@0
   856
(*Extension of a theory with given classes, types, constants and syntax.
clasohm@0
   857
  Reads the axioms from strings: axpairs have the form (axname, axiom). *)
clasohm@0
   858
fun extend_theory thy thyname ext axpairs =
clasohm@0
   859
  let val sign = Sign.extend (sign_of thy) thyname ext;
clasohm@0
   860
      val axioms= mk_axioms sign axpairs
clasohm@0
   861
  in  Extend{sign=sign, axioms= axioms, thy = thy}  end;
clasohm@0
   862
clasohm@0
   863
(*The union of two theories*)
clasohm@0
   864
fun merge_theories (thy1,thy2) =
clasohm@0
   865
    Merge{sign = Sign.merge(sign_of thy1, sign_of thy2),
clasohm@0
   866
	  thy1 = thy1, thy2 = thy2};
clasohm@0
   867
clasohm@0
   868
clasohm@0
   869
(*** Meta simp sets ***)
clasohm@0
   870
clasohm@0
   871
type rrule = {thm:thm, lhs:term};
clasohm@0
   872
datatype meta_simpset =
clasohm@0
   873
  Mss of {net:rrule Net.net, congs:(string * rrule)list, primes:string,
clasohm@0
   874
          prems: thm list, mk_rews: thm -> thm list};
clasohm@0
   875
clasohm@0
   876
(*A "mss" contains data needed during conversion:
clasohm@0
   877
  net: discrimination net of rewrite rules
clasohm@0
   878
  congs: association list of congruence rules
clasohm@0
   879
  primes: offset for generating unique new names
clasohm@0
   880
          for rewriting under lambda abstractions
clasohm@0
   881
  mk_rews: used when local assumptions are added
clasohm@0
   882
*)
clasohm@0
   883
clasohm@0
   884
val empty_mss = Mss{net= Net.empty, congs= [], primes="", prems= [],
clasohm@0
   885
                    mk_rews = K[]};
clasohm@0
   886
clasohm@0
   887
exception SIMPLIFIER of string * thm;
clasohm@0
   888
lcp@229
   889
fun prtm a sign t = (writeln a; writeln(Sign.string_of_term sign t));
clasohm@0
   890
nipkow@209
   891
val trace_simp = ref false;
nipkow@209
   892
lcp@229
   893
fun trace_term a sign t = if !trace_simp then prtm a sign t else ();
nipkow@209
   894
nipkow@209
   895
fun trace_thm a (Thm{sign,prop,...}) = trace_term a sign prop;
nipkow@209
   896
clasohm@0
   897
(*simple test for looping rewrite*)
clasohm@0
   898
fun loops sign prems (lhs,rhs) =
clasohm@0
   899
  null(prems) andalso
clasohm@0
   900
  Pattern.eta_matches (#tsig(Sign.rep_sg sign)) (lhs,rhs);
clasohm@0
   901
clasohm@0
   902
fun mk_rrule (thm as Thm{hyps,sign,prop,maxidx,...}) =
clasohm@0
   903
  let val prems = Logic.strip_imp_prems prop
clasohm@0
   904
      val concl = Pattern.eta_contract (Logic.strip_imp_concl prop)
clasohm@0
   905
      val (lhs,rhs) = Logic.dest_equals concl handle TERM _ =>
clasohm@0
   906
                      raise SIMPLIFIER("Rewrite rule not a meta-equality",thm)
clasohm@0
   907
  in if loops sign prems (lhs,rhs)
clasohm@0
   908
     then (prtm "Warning: ignoring looping rewrite rule" sign prop; None)
clasohm@0
   909
     else Some{thm=thm,lhs=lhs}
clasohm@0
   910
  end;
clasohm@0
   911
nipkow@87
   912
local
nipkow@87
   913
 fun eq({thm=Thm{prop=p1,...},...}:rrule,
nipkow@87
   914
        {thm=Thm{prop=p2,...},...}:rrule) = p1 aconv p2
nipkow@87
   915
in
nipkow@87
   916
clasohm@0
   917
fun add_simp(mss as Mss{net,congs,primes,prems,mk_rews},
clasohm@0
   918
             thm as Thm{sign,prop,...}) =
nipkow@87
   919
  case mk_rrule thm of
nipkow@87
   920
    None => mss
nipkow@87
   921
  | Some(rrule as {lhs,...}) =>
nipkow@209
   922
      (trace_thm "Adding rewrite rule:" thm;
nipkow@209
   923
       Mss{net= (Net.insert_term((lhs,rrule),net,eq)
nipkow@209
   924
                 handle Net.INSERT =>
nipkow@87
   925
                  (prtm "Warning: ignoring duplicate rewrite rule" sign prop;
nipkow@87
   926
                   net)),
nipkow@209
   927
           congs=congs, primes=primes, prems=prems,mk_rews=mk_rews});
nipkow@87
   928
nipkow@87
   929
fun del_simp(mss as Mss{net,congs,primes,prems,mk_rews},
nipkow@87
   930
             thm as Thm{sign,prop,...}) =
nipkow@87
   931
  case mk_rrule thm of
nipkow@87
   932
    None => mss
nipkow@87
   933
  | Some(rrule as {lhs,...}) =>
nipkow@87
   934
      Mss{net= (Net.delete_term((lhs,rrule),net,eq)
nipkow@87
   935
                handle Net.INSERT =>
nipkow@87
   936
                 (prtm "Warning: rewrite rule not in simpset" sign prop;
nipkow@87
   937
                  net)),
clasohm@0
   938
             congs=congs, primes=primes, prems=prems,mk_rews=mk_rews}
nipkow@87
   939
nipkow@87
   940
end;
clasohm@0
   941
clasohm@0
   942
val add_simps = foldl add_simp;
nipkow@87
   943
val del_simps = foldl del_simp;
clasohm@0
   944
clasohm@0
   945
fun mss_of thms = add_simps(empty_mss,thms);
clasohm@0
   946
clasohm@0
   947
fun add_cong(Mss{net,congs,primes,prems,mk_rews},thm) =
clasohm@0
   948
  let val (lhs,_) = Logic.dest_equals(concl_of thm) handle TERM _ =>
clasohm@0
   949
                    raise SIMPLIFIER("Congruence not a meta-equality",thm)
clasohm@0
   950
      val lhs = Pattern.eta_contract lhs
clasohm@0
   951
      val (a,_) = dest_Const (head_of lhs) handle TERM _ =>
clasohm@0
   952
                  raise SIMPLIFIER("Congruence must start with a constant",thm)
clasohm@0
   953
  in Mss{net=net, congs=(a,{lhs=lhs,thm=thm})::congs, primes=primes,
clasohm@0
   954
         prems=prems, mk_rews=mk_rews}
clasohm@0
   955
  end;
clasohm@0
   956
clasohm@0
   957
val (op add_congs) = foldl add_cong;
clasohm@0
   958
clasohm@0
   959
fun add_prems(Mss{net,congs,primes,prems,mk_rews},thms) =
clasohm@0
   960
  Mss{net=net, congs=congs, primes=primes, prems=thms@prems, mk_rews=mk_rews};
clasohm@0
   961
clasohm@0
   962
fun prems_of_mss(Mss{prems,...}) = prems;
clasohm@0
   963
clasohm@0
   964
fun set_mk_rews(Mss{net,congs,primes,prems,...},mk_rews) =
clasohm@0
   965
  Mss{net=net, congs=congs, primes=primes, prems=prems, mk_rews=mk_rews};
clasohm@0
   966
fun mk_rews_of_mss(Mss{mk_rews,...}) = mk_rews;
clasohm@0
   967
clasohm@0
   968
clasohm@0
   969
(*** Meta-level rewriting 
clasohm@0
   970
     uses conversions, omitting proofs for efficiency.  See
clasohm@0
   971
	L C Paulson, A higher-order implementation of rewriting,
clasohm@0
   972
	Science of Computer Programming 3 (1983), pages 119-149. ***)
clasohm@0
   973
clasohm@0
   974
type prover = meta_simpset -> thm -> thm option;
clasohm@0
   975
type termrec = (Sign.sg * term list) * term;
clasohm@0
   976
type conv = meta_simpset -> termrec -> termrec;
clasohm@0
   977
nipkow@208
   978
fun check_conv(thm as Thm{hyps,prop,...}, prop0) =
nipkow@112
   979
  let fun err() = (trace_thm "Proved wrong thm (Check subgoaler?)" thm; None)
clasohm@0
   980
      val (lhs0,_) = Logic.dest_equals(Logic.strip_imp_concl prop0)
clasohm@0
   981
  in case prop of
clasohm@0
   982
       Const("==",_) $ lhs $ rhs =>
clasohm@0
   983
         if (lhs = lhs0) orelse
clasohm@0
   984
            (lhs aconv (Envir.norm_term (Envir.empty 0) lhs0))
nipkow@208
   985
         then (trace_thm "SUCCEEDED" thm; Some(hyps,rhs))
clasohm@0
   986
         else err()
clasohm@0
   987
     | _ => err()
clasohm@0
   988
  end;
clasohm@0
   989
clasohm@0
   990
(*Conversion to apply the meta simpset to a term*)
nipkow@208
   991
fun rewritec (prover,signt) (mss as Mss{net,...}) (hypst,t) =
nipkow@225
   992
  let val t = Pattern.eta_contract t;
nipkow@225
   993
      fun rew {thm as Thm{sign,hyps,maxidx,prop,...}, lhs} =
nipkow@208
   994
	let val unit = if Sign.subsig(sign,signt) then ()
nipkow@208
   995
                  else (writeln"Warning: rewrite rule from different theory";
nipkow@208
   996
                        raise Pattern.MATCH)
nipkow@208
   997
            val insts = Pattern.match (#tsig(Sign.rep_sg signt)) (lhs,t)
clasohm@0
   998
            val prop' = subst_vars insts prop;
clasohm@0
   999
            val hyps' = hyps union hypst;
nipkow@208
  1000
            val thm' = Thm{sign=signt, hyps=hyps', prop=prop', maxidx=maxidx}
clasohm@0
  1001
        in if nprems_of thm' = 0
clasohm@0
  1002
           then let val (_,rhs) = Logic.dest_equals prop'
nipkow@208
  1003
                in trace_thm "Rewriting:" thm'; Some(hyps',rhs) end
clasohm@0
  1004
           else (trace_thm "Trying to rewrite:" thm';
clasohm@0
  1005
                 case prover mss thm' of
clasohm@0
  1006
                   None       => (trace_thm "FAILED" thm'; None)
nipkow@112
  1007
                 | Some(thm2) => check_conv(thm2,prop'))
clasohm@0
  1008
        end
clasohm@0
  1009
nipkow@225
  1010
      fun rews [] = None
nipkow@225
  1011
        | rews (rrule::rrules) =
nipkow@225
  1012
            let val opt = rew rrule handle Pattern.MATCH => None
nipkow@225
  1013
            in case opt of None => rews rrules | some => some end;
clasohm@0
  1014
clasohm@0
  1015
  in case t of
nipkow@208
  1016
       Abs(_,_,body) $ u => Some(hypst,subst_bounds([u], body))
nipkow@225
  1017
     | _                 => rews(Net.match_term net t)
clasohm@0
  1018
  end;
clasohm@0
  1019
clasohm@0
  1020
(*Conversion to apply a congruence rule to a term*)
nipkow@208
  1021
fun congc (prover,signt) {thm=cong,lhs=lhs} (hypst,t) =
clasohm@0
  1022
  let val Thm{sign,hyps,maxidx,prop,...} = cong
nipkow@208
  1023
      val unit = if Sign.subsig(sign,signt) then ()
nipkow@208
  1024
                 else error("Congruence rule from different theory")
nipkow@208
  1025
      val tsig = #tsig(Sign.rep_sg signt)
clasohm@0
  1026
      val insts = Pattern.match tsig (lhs,t) handle Pattern.MATCH =>
clasohm@0
  1027
                  error("Congruence rule did not match")
clasohm@0
  1028
      val prop' = subst_vars insts prop;
nipkow@208
  1029
      val thm' = Thm{sign=signt, hyps=hyps union hypst,
clasohm@0
  1030
                     prop=prop', maxidx=maxidx}
clasohm@0
  1031
      val unit = trace_thm "Applying congruence rule" thm';
nipkow@112
  1032
      fun err() = error("Failed congruence proof!")
clasohm@0
  1033
clasohm@0
  1034
  in case prover thm' of
nipkow@112
  1035
       None => err()
nipkow@112
  1036
     | Some(thm2) => (case check_conv(thm2,prop') of
nipkow@112
  1037
                        None => err() | Some(x) => x)
clasohm@0
  1038
  end;
clasohm@0
  1039
clasohm@0
  1040
nipkow@214
  1041
fun bottomc ((simprem,useprem),prover,sign) =
clasohm@0
  1042
  let fun botc mss trec = let val trec1 = subc mss trec
nipkow@208
  1043
                          in case rewritec (prover,sign) mss trec1 of
clasohm@0
  1044
                               None => trec1
clasohm@0
  1045
                             | Some(trec2) => botc mss trec2
clasohm@0
  1046
                          end
clasohm@0
  1047
clasohm@0
  1048
      and subc (mss as Mss{net,congs,primes,prems,mk_rews})
nipkow@208
  1049
               (trec as (hyps,t)) =
clasohm@0
  1050
        (case t of
clasohm@0
  1051
            Abs(a,T,t) =>
clasohm@0
  1052
              let val v = Free(".subc." ^ primes,T)
clasohm@0
  1053
                  val mss' = Mss{net=net, congs=congs, primes=primes^"'",
clasohm@0
  1054
                                 prems=prems,mk_rews=mk_rews}
nipkow@208
  1055
                  val (hyps',t') = botc mss' (hyps,subst_bounds([v],t))
nipkow@208
  1056
              in  (hyps', Abs(a, T, abstract_over(v,t')))  end
clasohm@0
  1057
          | t$u => (case t of
nipkow@208
  1058
              Const("==>",_)$s  => impc(hyps,s,u,mss)
nipkow@208
  1059
            | Abs(_,_,body)     => subc mss (hyps,subst_bounds([u], body))
clasohm@0
  1060
            | _                 =>
nipkow@208
  1061
                let fun appc() = let val (hyps1,t1) = botc mss (hyps,t)
nipkow@208
  1062
                                     val (hyps2,u1) = botc mss (hyps1,u)
nipkow@208
  1063
                                 in (hyps2,t1$u1) end
clasohm@0
  1064
                    val (h,ts) = strip_comb t
clasohm@0
  1065
                in case h of
clasohm@0
  1066
                     Const(a,_) =>
clasohm@0
  1067
                       (case assoc(congs,a) of
clasohm@0
  1068
                          None => appc()
nipkow@208
  1069
                        | Some(cong) => congc (prover mss,sign) cong trec)
clasohm@0
  1070
                   | _ => appc()
clasohm@0
  1071
                end)
clasohm@0
  1072
          | _ => trec)
clasohm@0
  1073
nipkow@208
  1074
      and impc(hyps,s,u,mss as Mss{mk_rews,...}) =
nipkow@214
  1075
        let val (hyps1,s') = if simprem then botc mss (hyps,s) else (hyps,s)
nipkow@214
  1076
            val mss' =
nipkow@214
  1077
              if not useprem orelse maxidx_of_term s' <> ~1 then mss
nipkow@208
  1078
              else let val thm = Thm{sign=sign,hyps=[s'],prop=s',maxidx= ~1}
nipkow@214
  1079
                   in add_simps(add_prems(mss,[thm]), mk_rews thm) end
nipkow@208
  1080
            val (hyps2,u') = botc mss' (hyps1,u)
nipkow@134
  1081
            val hyps2' = if s' mem hyps1 then hyps2 else hyps2\s'
nipkow@208
  1082
        in (hyps2', Logic.mk_implies(s',u')) end
clasohm@0
  1083
clasohm@0
  1084
  in botc end;
clasohm@0
  1085
clasohm@0
  1086
clasohm@0
  1087
(*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***)
clasohm@0
  1088
(* Parameters:
nipkow@214
  1089
   mode = (simplify A, use A in simplifying B) when simplifying A ==> B 
clasohm@0
  1090
   mss: contains equality theorems of the form [|p1,...|] ==> t==u
clasohm@0
  1091
   prover: how to solve premises in conditional rewrites and congruences
clasohm@0
  1092
*)
clasohm@0
  1093
clasohm@0
  1094
(*** FIXME: check that #primes(mss) does not "occur" in ct alread ***)
nipkow@214
  1095
fun rewrite_cterm mode mss prover ct =
lcp@229
  1096
  let val {sign, t, T, maxidx} = rep_cterm ct;
nipkow@214
  1097
      val (hyps,u) = bottomc (mode,prover,sign) mss ([],t);
clasohm@0
  1098
      val prop = Logic.mk_equals(t,u)
nipkow@208
  1099
  in  Thm{sign= sign, hyps= hyps, maxidx= maxidx_of_term prop, prop= prop}
clasohm@0
  1100
  end
clasohm@0
  1101
clasohm@0
  1102
end;