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