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