src/Pure/drule.ML
author nipkow
Thu Jan 11 10:29:31 1996 +0100 (1996-01-11)
changeset 1435 aefcd255ed4a
parent 1412 2ab32768c996
child 1439 1f5949a43e82
permissions -rw-r--r--
Removed bug in type unification. Negative indexes are not used any longer.
Had to change interface to Type.unify to pass maxidx. Thus changes in the
clients.
wenzelm@252
     1
(*  Title:      Pure/drule.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@252
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Derived rules and other operations on theorems and theories
clasohm@0
     7
*)
clasohm@0
     8
lcp@11
     9
infix 0 RS RSN RL RLN MRS MRL COMP;
clasohm@0
    10
clasohm@0
    11
signature DRULE =
clasohm@0
    12
  sig
clasohm@0
    13
  structure Thm : THM
clasohm@0
    14
  local open Thm  in
lcp@668
    15
  val add_defs		: (string * string) list -> theory -> theory
lcp@668
    16
  val add_defs_i	: (string * term) list -> theory -> theory
lcp@668
    17
  val asm_rl		: thm
lcp@668
    18
  val assume_ax		: theory -> string -> thm
lcp@668
    19
  val COMP		: thm * thm -> thm
lcp@668
    20
  val compose		: thm * int * thm -> thm list
lcp@708
    21
  val cprems_of		: thm -> cterm list
lcp@708
    22
  val cskip_flexpairs	: cterm -> cterm
lcp@708
    23
  val cstrip_imp_prems	: cterm -> cterm list
lcp@668
    24
  val cterm_instantiate	: (cterm*cterm)list -> thm -> thm
lcp@668
    25
  val cut_rl		: thm
lcp@668
    26
  val equal_abs_elim	: cterm  -> thm -> thm
lcp@229
    27
  val equal_abs_elim_list: cterm list -> thm -> thm
lcp@668
    28
  val eq_thm		: thm * thm -> bool
clasohm@1241
    29
  val same_thm		: thm * thm -> bool
lcp@668
    30
  val eq_thm_sg		: thm * thm -> bool
lcp@229
    31
  val flexpair_abs_elim_list: cterm list -> thm -> thm
lcp@668
    32
  val forall_intr_list	: cterm list -> thm -> thm
lcp@668
    33
  val forall_intr_frees	: thm -> thm
lcp@1194
    34
  val forall_intr_vars	: thm -> thm
lcp@668
    35
  val forall_elim_list	: cterm list -> thm -> thm
lcp@668
    36
  val forall_elim_var	: int -> thm -> thm
lcp@668
    37
  val forall_elim_vars	: int -> thm -> thm
lcp@668
    38
  val implies_elim_list	: thm -> thm list -> thm
lcp@668
    39
  val implies_intr_list	: cterm list -> thm -> thm
lcp@668
    40
  val MRL		: thm list list * thm list -> thm list
lcp@668
    41
  val MRS		: thm list * thm -> thm
lcp@668
    42
  val pprint_cterm	: cterm -> pprint_args -> unit
lcp@668
    43
  val pprint_ctyp	: ctyp -> pprint_args -> unit
lcp@668
    44
  val pprint_theory	: theory -> pprint_args -> unit
lcp@668
    45
  val pprint_thm	: thm -> pprint_args -> unit
lcp@668
    46
  val pretty_thm	: thm -> Sign.Syntax.Pretty.T
lcp@668
    47
  val print_cterm	: cterm -> unit
lcp@668
    48
  val print_ctyp	: ctyp -> unit
lcp@668
    49
  val print_goals	: int -> thm -> unit
lcp@668
    50
  val print_goals_ref	: (int -> thm -> unit) ref
lcp@668
    51
  val print_syntax	: theory -> unit
lcp@668
    52
  val print_theory	: theory -> unit
lcp@668
    53
  val print_thm		: thm -> unit
lcp@668
    54
  val prth		: thm -> thm
lcp@668
    55
  val prthq		: thm Sequence.seq -> thm Sequence.seq
lcp@668
    56
  val prths		: thm list -> thm list
lcp@668
    57
  val read_instantiate	: (string*string)list -> thm -> thm
clasohm@0
    58
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
lcp@668
    59
  val read_insts	:
lcp@229
    60
          Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
lcp@229
    61
                  -> (indexname -> typ option) * (indexname -> sort option)
nipkow@949
    62
                  -> string list -> (string*string)list
lcp@229
    63
                  -> (indexname*ctyp)list * (cterm*cterm)list
lcp@668
    64
  val reflexive_thm	: thm
lcp@668
    65
  val revcut_rl		: thm
lcp@668
    66
  val rewrite_goal_rule	: bool*bool -> (meta_simpset -> thm -> thm option)
nipkow@214
    67
        -> meta_simpset -> int -> thm -> thm
clasohm@0
    68
  val rewrite_goals_rule: thm list -> thm -> thm
lcp@668
    69
  val rewrite_rule	: thm list -> thm -> thm
lcp@668
    70
  val RS		: thm * thm -> thm
lcp@668
    71
  val RSN		: thm * (int * thm) -> thm
lcp@668
    72
  val RL		: thm list * thm list -> thm list
lcp@668
    73
  val RLN		: thm list * (int * thm list) -> thm list
lcp@668
    74
  val show_hyps		: bool ref
lcp@668
    75
  val size_of_thm	: thm -> int
lcp@668
    76
  val standard		: thm -> thm
lcp@668
    77
  val string_of_cterm	: cterm -> string
lcp@668
    78
  val string_of_ctyp	: ctyp -> string
lcp@668
    79
  val string_of_thm	: thm -> string
lcp@668
    80
  val symmetric_thm	: thm
lcp@668
    81
  val thin_rl		: thm
lcp@668
    82
  val transitive_thm	: thm
clasohm@0
    83
  val triv_forall_equality: thm
clasohm@0
    84
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
lcp@668
    85
  val zero_var_indexes	: thm -> thm
clasohm@0
    86
  end
clasohm@0
    87
  end;
clasohm@0
    88
lcp@668
    89
wenzelm@252
    90
functor DruleFun (structure Logic: LOGIC and Thm: THM): DRULE =
clasohm@0
    91
struct
clasohm@0
    92
structure Thm = Thm;
clasohm@0
    93
structure Sign = Thm.Sign;
clasohm@0
    94
structure Type = Sign.Type;
wenzelm@575
    95
structure Syntax = Sign.Syntax;
wenzelm@575
    96
structure Pretty = Syntax.Pretty
wenzelm@400
    97
structure Symtab = Sign.Symtab;
wenzelm@400
    98
clasohm@0
    99
local open Thm
clasohm@0
   100
in
clasohm@0
   101
wenzelm@561
   102
(**** Extend Theories ****)
wenzelm@561
   103
wenzelm@561
   104
(** add constant definitions **)
wenzelm@561
   105
wenzelm@561
   106
(* all_axioms_of *)
wenzelm@561
   107
wenzelm@561
   108
(*results may contain duplicates!*)
wenzelm@561
   109
wenzelm@561
   110
fun ancestry_of thy =
wenzelm@561
   111
  thy :: flat (map ancestry_of (parents_of thy));
wenzelm@561
   112
wenzelm@1237
   113
val all_axioms_of =
wenzelm@776
   114
  flat o map (Symtab.dest o #new_axioms o rep_theory) o ancestry_of;
wenzelm@561
   115
wenzelm@561
   116
wenzelm@561
   117
(* clash_types, clash_consts *)
wenzelm@561
   118
wenzelm@561
   119
(*check if types have common instance (ignoring sorts)*)
wenzelm@561
   120
wenzelm@561
   121
fun clash_types ty1 ty2 =
wenzelm@561
   122
  let
wenzelm@561
   123
    val ty1' = Type.varifyT ty1;
wenzelm@561
   124
    val ty2' = incr_tvar (maxidx_of_typ ty1' + 1) (Type.varifyT ty2);
wenzelm@561
   125
  in
wenzelm@561
   126
    Type.raw_unify (ty1', ty2')
wenzelm@561
   127
  end;
wenzelm@561
   128
wenzelm@561
   129
fun clash_consts (c1, ty1) (c2, ty2) =
wenzelm@561
   130
  c1 = c2 andalso clash_types ty1 ty2;
wenzelm@561
   131
wenzelm@561
   132
wenzelm@561
   133
(* clash_defns *)
wenzelm@561
   134
wenzelm@561
   135
fun clash_defn c_ty (name, tm) =
wenzelm@561
   136
  let val (c, ty') = dest_Const (head_of (fst (Logic.dest_equals tm))) in
wenzelm@561
   137
    if clash_consts c_ty (c, ty') then Some (name, ty') else None
wenzelm@561
   138
  end handle TERM _ => None;
wenzelm@561
   139
wenzelm@561
   140
fun clash_defns c_ty axms =
wenzelm@561
   141
  distinct (mapfilter (clash_defn c_ty) axms);
wenzelm@561
   142
wenzelm@561
   143
wenzelm@561
   144
(* dest_defn *)
wenzelm@561
   145
wenzelm@561
   146
fun dest_defn tm =
wenzelm@561
   147
  let
wenzelm@561
   148
    fun err msg = raise_term msg [tm];
wenzelm@561
   149
wenzelm@561
   150
    val (lhs, rhs) = Logic.dest_equals tm
wenzelm@561
   151
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@561
   152
    val (head, args) = strip_comb lhs;
wenzelm@561
   153
    val (c, ty) = dest_Const head
wenzelm@561
   154
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@561
   155
wenzelm@655
   156
    fun occs_const (Const c_ty') = (c_ty' = (c, ty))
wenzelm@561
   157
      | occs_const (Abs (_, _, t)) = occs_const t
wenzelm@561
   158
      | occs_const (t $ u) = occs_const t orelse occs_const u
wenzelm@561
   159
      | occs_const _ = false;
wenzelm@641
   160
wenzelm@641
   161
    val show_frees = commas_quote o map (fst o dest_Free);
wenzelm@641
   162
    val show_tfrees = commas_quote o map fst;
wenzelm@641
   163
wenzelm@641
   164
    val lhs_dups = duplicates args;
wenzelm@641
   165
    val rhs_extras = gen_rems (op =) (term_frees rhs, args);
wenzelm@641
   166
    val rhs_extrasT = gen_rems (op =) (term_tfrees rhs, typ_tfrees ty);
wenzelm@561
   167
  in
wenzelm@561
   168
    if not (forall is_Free args) then
wenzelm@561
   169
      err "Arguments of lhs have to be variables"
wenzelm@641
   170
    else if not (null lhs_dups) then
wenzelm@641
   171
      err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
wenzelm@641
   172
    else if not (null rhs_extras) then
wenzelm@641
   173
      err ("Extra variables on rhs: " ^ show_frees rhs_extras)
wenzelm@641
   174
    else if not (null rhs_extrasT) then
wenzelm@641
   175
      err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
wenzelm@561
   176
    else if occs_const rhs then
wenzelm@655
   177
      err ("Constant to be defined occurs on rhs")
wenzelm@561
   178
    else (c, ty)
wenzelm@561
   179
  end;
wenzelm@561
   180
wenzelm@561
   181
wenzelm@561
   182
(* check_defn *)
wenzelm@561
   183
wenzelm@641
   184
fun err_in_defn name msg =
wenzelm@641
   185
  (writeln msg; error ("The error(s) above occurred in definition " ^ quote name));
wenzelm@561
   186
wenzelm@561
   187
fun check_defn sign (axms, (name, tm)) =
wenzelm@561
   188
  let
wenzelm@561
   189
    fun show_const (c, ty) = quote (Pretty.string_of (Pretty.block
wenzelm@561
   190
      [Pretty.str (c ^ " ::"), Pretty.brk 1, Sign.pretty_typ sign ty]));
wenzelm@561
   191
wenzelm@561
   192
    fun show_defn c (dfn, ty') = show_const (c, ty') ^ " in " ^ dfn;
wenzelm@561
   193
    fun show_defns c = commas o map (show_defn c);
wenzelm@561
   194
wenzelm@561
   195
    val (c, ty) = dest_defn tm
wenzelm@641
   196
      handle TERM (msg, _) => err_in_defn name msg;
wenzelm@561
   197
    val defns = clash_defns (c, ty) axms;
wenzelm@561
   198
  in
wenzelm@561
   199
    if not (null defns) then
wenzelm@641
   200
      err_in_defn name ("Definition of " ^ show_const (c, ty) ^
wenzelm@561
   201
        " clashes with " ^ show_defns c defns)
wenzelm@561
   202
    else (name, tm) :: axms
wenzelm@561
   203
  end;
wenzelm@561
   204
wenzelm@561
   205
wenzelm@561
   206
(* add_defs *)
wenzelm@561
   207
wenzelm@561
   208
fun ext_defns prep_axm raw_axms thy =
wenzelm@561
   209
  let
wenzelm@561
   210
    val axms = map (prep_axm (sign_of thy)) raw_axms;
wenzelm@561
   211
    val all_axms = all_axioms_of thy;
wenzelm@561
   212
  in
wenzelm@561
   213
    foldl (check_defn (sign_of thy)) (all_axms, axms);
wenzelm@561
   214
    add_axioms_i axms thy
wenzelm@561
   215
  end;
wenzelm@561
   216
wenzelm@561
   217
val add_defs_i = ext_defns cert_axm;
wenzelm@561
   218
val add_defs = ext_defns read_axm;
wenzelm@561
   219
wenzelm@561
   220
wenzelm@561
   221
clasohm@0
   222
(**** More derived rules and operations on theorems ****)
clasohm@0
   223
lcp@708
   224
(** some cterm->cterm operations: much faster than calling cterm_of! **)
lcp@708
   225
lcp@708
   226
(*Discard flexflex pairs; return a cterm*)
lcp@708
   227
fun cskip_flexpairs ct =
lcp@708
   228
    case term_of ct of
lcp@708
   229
	(Const("==>", _) $ (Const("=?=",_)$_$_) $ _) =>
lcp@708
   230
	    cskip_flexpairs (#2 (dest_cimplies ct))
lcp@708
   231
      | _ => ct;
lcp@708
   232
lcp@708
   233
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
lcp@708
   234
fun cstrip_imp_prems ct =
lcp@708
   235
    let val (cA,cB) = dest_cimplies ct
lcp@708
   236
    in  cA :: cstrip_imp_prems cB  end
lcp@708
   237
    handle TERM _ => [];
lcp@708
   238
lcp@708
   239
(*The premises of a theorem, as a cterm list*)
lcp@708
   240
val cprems_of = cstrip_imp_prems o cskip_flexpairs o cprop_of;
lcp@708
   241
lcp@708
   242
lcp@229
   243
(** reading of instantiations **)
lcp@229
   244
lcp@229
   245
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
lcp@229
   246
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
lcp@229
   247
lcp@229
   248
fun absent ixn =
lcp@229
   249
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
lcp@229
   250
lcp@229
   251
fun inst_failure ixn =
lcp@229
   252
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
lcp@229
   253
nipkow@952
   254
(* this code is a bit of a mess. add_cterm could be simplified greatly if
nipkow@952
   255
   simultaneous instantiations were read or at least type checked
nipkow@952
   256
   simultaneously rather than one after the other. This would make the tricky
nipkow@952
   257
   composition of implicit type instantiations (parameter tye) superfluous.
nipkow@952
   258
*)
nipkow@949
   259
fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
lcp@229
   260
let val {tsig,...} = Sign.rep_sg sign
lcp@229
   261
    fun split([],tvs,vs) = (tvs,vs)
lcp@229
   262
      | split((sv,st)::l,tvs,vs) = (case explode sv of
lcp@229
   263
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
lcp@229
   264
                | cs => split(l,tvs,(indexname cs,st)::vs));
lcp@229
   265
    val (tvs,vs) = split(insts,[],[]);
lcp@229
   266
    fun readT((a,i),st) =
lcp@229
   267
        let val ixn = ("'" ^ a,i);
lcp@229
   268
            val S = case rsorts ixn of Some S => S | None => absent ixn;
lcp@229
   269
            val T = Sign.read_typ (sign,sorts) st;
lcp@229
   270
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
lcp@229
   271
           else inst_failure ixn
lcp@229
   272
        end
lcp@229
   273
    val tye = map readT tvs;
nipkow@949
   274
    fun add_cterm ((cts,tye,used), (ixn,st)) =
lcp@229
   275
        let val T = case rtypes ixn of
lcp@229
   276
                      Some T => typ_subst_TVars tye T
lcp@229
   277
                    | None => absent ixn;
nipkow@949
   278
            val (ct,tye2) = read_def_cterm(sign,types,sorts) used false (st,T);
nipkow@952
   279
            val cts' = (ixn,T,ct)::cts
nipkow@952
   280
            fun inst(ixn,T,ct) = (ixn,typ_subst_TVars tye2 T,ct)
nipkow@949
   281
            val used' = add_term_tvarnames(term_of ct,used);
nipkow@952
   282
        in (map inst cts',tye2 @ tye,used') end
nipkow@949
   283
    val (cterms,tye',_) = foldl add_cterm (([],tye,used), vs);
nipkow@952
   284
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye',
nipkow@952
   285
    map (fn (ixn,T,ct) => (cterm_of sign (Var(ixn,T)), ct)) cterms)
nipkow@952
   286
end;
lcp@229
   287
lcp@229
   288
wenzelm@252
   289
wenzelm@252
   290
(*** Printing of theories, theorems, etc. ***)
lcp@229
   291
lcp@229
   292
(*If false, hypotheses are printed as dots*)
lcp@229
   293
val show_hyps = ref true;
lcp@229
   294
lcp@229
   295
fun pretty_thm th =
wenzelm@1218
   296
  let
wenzelm@1237
   297
    val {sign, hyps, prop, ...} = rep_thm th;
wenzelm@1237
   298
    val xshyps = extra_shyps th;
wenzelm@1237
   299
    val hlen = length xshyps + length hyps;
wenzelm@1218
   300
    val hsymbs =
wenzelm@1218
   301
      if hlen = 0 then []
wenzelm@1218
   302
      else if ! show_hyps then
wenzelm@1218
   303
        [Pretty.brk 2, Pretty.list "[" "]"
wenzelm@1237
   304
          (map (Sign.pretty_term sign) hyps @ map Sign.pretty_sort xshyps)]
wenzelm@1218
   305
      else
wenzelm@1218
   306
        [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
wenzelm@1218
   307
  in
wenzelm@1218
   308
    Pretty.block (Sign.pretty_term sign prop :: hsymbs)
wenzelm@1218
   309
  end;
lcp@229
   310
lcp@229
   311
val string_of_thm = Pretty.string_of o pretty_thm;
lcp@229
   312
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
lcp@229
   313
lcp@229
   314
lcp@229
   315
(** Top-level commands for printing theorems **)
lcp@229
   316
val print_thm = writeln o string_of_thm;
lcp@229
   317
lcp@229
   318
fun prth th = (print_thm th; th);
lcp@229
   319
lcp@229
   320
(*Print and return a sequence of theorems, separated by blank lines. *)
lcp@229
   321
fun prthq thseq =
wenzelm@252
   322
  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
lcp@229
   323
lcp@229
   324
(*Print and return a list of theorems, separated by blank lines. *)
lcp@229
   325
fun prths ths = (print_list_ln print_thm ths; ths);
lcp@229
   326
wenzelm@252
   327
wenzelm@252
   328
(* other printing commands *)
lcp@229
   329
wenzelm@252
   330
fun pprint_ctyp cT =
wenzelm@252
   331
  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
wenzelm@252
   332
wenzelm@252
   333
fun string_of_ctyp cT =
wenzelm@252
   334
  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
lcp@229
   335
lcp@229
   336
val print_ctyp = writeln o string_of_ctyp;
lcp@229
   337
wenzelm@252
   338
fun pprint_cterm ct =
wenzelm@252
   339
  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
lcp@229
   340
wenzelm@252
   341
fun string_of_cterm ct =
wenzelm@252
   342
  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
lcp@229
   343
lcp@229
   344
val print_cterm = writeln o string_of_cterm;
lcp@229
   345
wenzelm@252
   346
wenzelm@252
   347
(* print theory *)
wenzelm@252
   348
wenzelm@252
   349
val pprint_theory = Sign.pprint_sg o sign_of;
lcp@229
   350
wenzelm@575
   351
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@575
   352
wenzelm@385
   353
fun print_axioms thy =
wenzelm@252
   354
  let
wenzelm@400
   355
    val {sign, new_axioms, ...} = rep_theory thy;
wenzelm@400
   356
    val axioms = Symtab.dest new_axioms;
lcp@229
   357
wenzelm@385
   358
    fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
wenzelm@385
   359
      Pretty.quote (Sign.pretty_term sign t)];
wenzelm@252
   360
  in
wenzelm@385
   361
    Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
wenzelm@252
   362
  end;
lcp@229
   363
wenzelm@843
   364
fun print_theory thy = (Sign.print_sg (sign_of thy); print_axioms thy);
wenzelm@385
   365
lcp@229
   366
lcp@229
   367
lcp@229
   368
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
lcp@229
   369
wenzelm@641
   370
(* get type_env, sort_env of term *)
wenzelm@641
   371
wenzelm@641
   372
local
wenzelm@641
   373
  open Syntax;
wenzelm@641
   374
wenzelm@641
   375
  fun ins_entry (x, y) [] = [(x, [y])]
wenzelm@641
   376
    | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
wenzelm@641
   377
        if x = x' then (x', y ins ys') :: pairs
wenzelm@641
   378
        else pair :: ins_entry (x, y) pairs;
wenzelm@641
   379
wenzelm@641
   380
  fun add_type_env (Free (x, T), env) = ins_entry (T, x) env
wenzelm@641
   381
    | add_type_env (Var (xi, T), env) = ins_entry (T, string_of_vname xi) env
wenzelm@641
   382
    | add_type_env (Abs (_, _, t), env) = add_type_env (t, env)
wenzelm@641
   383
    | add_type_env (t $ u, env) = add_type_env (u, add_type_env (t, env))
wenzelm@641
   384
    | add_type_env (_, env) = env;
wenzelm@641
   385
wenzelm@641
   386
  fun add_sort_env (Type (_, Ts), env) = foldr add_sort_env (Ts, env)
wenzelm@641
   387
    | add_sort_env (TFree (x, S), env) = ins_entry (S, x) env
wenzelm@641
   388
    | add_sort_env (TVar (xi, S), env) = ins_entry (S, string_of_vname xi) env;
wenzelm@641
   389
wenzelm@641
   390
  val sort = map (apsnd sort_strings);
wenzelm@641
   391
in
wenzelm@641
   392
  fun type_env t = sort (add_type_env (t, []));
wenzelm@641
   393
  fun sort_env t = rev (sort (it_term_types add_sort_env (t, [])));
wenzelm@641
   394
end;
wenzelm@641
   395
wenzelm@641
   396
wenzelm@641
   397
(* print_goals *)
wenzelm@641
   398
wenzelm@641
   399
fun print_goals maxgoals state =
wenzelm@641
   400
  let
wenzelm@641
   401
    open Syntax;
wenzelm@641
   402
wenzelm@641
   403
    val {sign, prop, ...} = rep_thm state;
wenzelm@641
   404
wenzelm@641
   405
    val pretty_term = Sign.pretty_term sign;
wenzelm@641
   406
    val pretty_typ = Sign.pretty_typ sign;
wenzelm@641
   407
    val pretty_sort = Sign.pretty_sort;
wenzelm@641
   408
wenzelm@641
   409
    fun pretty_vars prtf (X, vs) = Pretty.block
wenzelm@641
   410
      [Pretty.block (Pretty.commas (map Pretty.str vs)),
wenzelm@641
   411
        Pretty.str " ::", Pretty.brk 1, prtf X];
lcp@229
   412
wenzelm@641
   413
    fun print_list _ _ [] = ()
wenzelm@641
   414
      | print_list name prtf lst =
wenzelm@641
   415
          (writeln ""; Pretty.writeln (Pretty.big_list name (map prtf lst)));
wenzelm@641
   416
wenzelm@641
   417
wenzelm@641
   418
    fun print_goals (_, []) = ()
wenzelm@641
   419
      | print_goals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
wenzelm@641
   420
          [Pretty.str (" " ^ string_of_int n ^ ". "), pretty_term A]));
wenzelm@641
   421
            print_goals (n + 1, As));
wenzelm@641
   422
wenzelm@641
   423
    val print_ffpairs =
wenzelm@641
   424
      print_list "Flex-flex pairs:" (pretty_term o Logic.mk_flexpair);
wenzelm@641
   425
wenzelm@641
   426
    val print_types = print_list "Types:" (pretty_vars pretty_typ) o type_env;
wenzelm@641
   427
    val print_sorts = print_list "Sorts:" (pretty_vars pretty_sort) o sort_env;
wenzelm@641
   428
wenzelm@641
   429
wenzelm@641
   430
    val (tpairs, As, B) = Logic.strip_horn prop;
wenzelm@641
   431
    val ngoals = length As;
wenzelm@641
   432
wenzelm@641
   433
    val orig_no_freeTs = ! show_no_free_types;
wenzelm@641
   434
    val orig_sorts = ! show_sorts;
wenzelm@641
   435
wenzelm@641
   436
    fun restore () =
wenzelm@641
   437
      (show_no_free_types := orig_no_freeTs; show_sorts := orig_sorts);
wenzelm@641
   438
  in
wenzelm@641
   439
    (show_no_free_types := true; show_sorts := false;
wenzelm@641
   440
      Pretty.writeln (pretty_term B);
wenzelm@641
   441
      if ngoals = 0 then writeln "No subgoals!"
wenzelm@641
   442
      else if ngoals > maxgoals then
wenzelm@641
   443
        (print_goals (1, take (maxgoals, As));
wenzelm@641
   444
          writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
wenzelm@641
   445
      else print_goals (1, As);
wenzelm@641
   446
wenzelm@641
   447
      print_ffpairs tpairs;
wenzelm@641
   448
wenzelm@641
   449
      if orig_sorts then
wenzelm@641
   450
        (print_types prop; print_sorts prop)
wenzelm@641
   451
      else if ! show_types then
wenzelm@641
   452
        print_types prop
wenzelm@641
   453
      else ())
wenzelm@641
   454
    handle exn => (restore (); raise exn);
wenzelm@641
   455
    restore ()
wenzelm@641
   456
  end;
wenzelm@641
   457
lcp@229
   458
lcp@229
   459
(*"hook" for user interfaces: allows print_goals to be replaced*)
lcp@229
   460
val print_goals_ref = ref print_goals;
lcp@229
   461
wenzelm@252
   462
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   463
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   464
     type variables) when reading another term.
clasohm@0
   465
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   466
***)
clasohm@0
   467
clasohm@0
   468
fun types_sorts thm =
clasohm@0
   469
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   470
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   471
        val vars = map dest_Var (term_vars big);
wenzelm@252
   472
        val frees = map dest_Free (term_frees big);
wenzelm@252
   473
        val tvars = term_tvars big;
wenzelm@252
   474
        val tfrees = term_tfrees big;
wenzelm@252
   475
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   476
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   477
    in (typ,sort) end;
clasohm@0
   478
clasohm@0
   479
(** Standardization of rules **)
clasohm@0
   480
clasohm@0
   481
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   482
fun forall_intr_list [] th = th
clasohm@0
   483
  | forall_intr_list (y::ys) th =
wenzelm@252
   484
        let val gth = forall_intr_list ys th
wenzelm@252
   485
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   486
clasohm@0
   487
(*Generalization over all suitable Free variables*)
clasohm@0
   488
fun forall_intr_frees th =
clasohm@0
   489
    let val {prop,sign,...} = rep_thm th
clasohm@0
   490
    in  forall_intr_list
wenzelm@252
   491
         (map (cterm_of sign) (sort atless (term_frees prop)))
clasohm@0
   492
         th
clasohm@0
   493
    end;
clasohm@0
   494
clasohm@0
   495
(*Replace outermost quantified variable by Var of given index.
clasohm@0
   496
    Could clash with Vars already present.*)
wenzelm@252
   497
fun forall_elim_var i th =
clasohm@0
   498
    let val {prop,sign,...} = rep_thm th
clasohm@0
   499
    in case prop of
wenzelm@252
   500
          Const("all",_) $ Abs(a,T,_) =>
wenzelm@252
   501
              forall_elim (cterm_of sign (Var((a,i), T)))  th
wenzelm@252
   502
        | _ => raise THM("forall_elim_var", i, [th])
clasohm@0
   503
    end;
clasohm@0
   504
clasohm@0
   505
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@252
   506
fun forall_elim_vars i th =
clasohm@0
   507
    forall_elim_vars i (forall_elim_var i th)
wenzelm@252
   508
        handle THM _ => th;
clasohm@0
   509
clasohm@0
   510
(*Specialization over a list of cterms*)
clasohm@0
   511
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   512
clasohm@0
   513
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   514
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   515
clasohm@0
   516
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   517
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   518
clasohm@0
   519
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   520
fun zero_var_indexes th =
clasohm@0
   521
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   522
        val vars = term_vars prop
clasohm@0
   523
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   524
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   525
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
wenzelm@252
   526
        val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
wenzelm@252
   527
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   528
        fun varpairs([],[]) = []
wenzelm@252
   529
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   530
                let val T' = typ_subst_TVars tye T
wenzelm@252
   531
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   532
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   533
                end
wenzelm@252
   534
          | varpairs _ = raise TERM("varpairs", []);
clasohm@0
   535
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   536
clasohm@0
   537
clasohm@0
   538
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   539
    all generality expressed by Vars having index 0.*)
clasohm@0
   540
fun standard th =
wenzelm@1218
   541
  let val {maxidx,...} = rep_thm th
wenzelm@1237
   542
  in
wenzelm@1218
   543
    th |> implies_intr_hyps
paulson@1412
   544
       |> Thm.strip_shyps |> implies_intr_shyps
paulson@1412
   545
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
paulson@1412
   546
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
wenzelm@1218
   547
  end;
wenzelm@1218
   548
clasohm@0
   549
wenzelm@252
   550
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   551
  Generalizes over Free variables,
clasohm@0
   552
  creates the assumption, and then strips quantifiers.
clasohm@0
   553
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   554
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   555
fun assume_ax thy sP =
clasohm@0
   556
    let val sign = sign_of thy
wenzelm@252
   557
        val prop = Logic.close_form (term_of (read_cterm sign
wenzelm@252
   558
                         (sP, propT)))
lcp@229
   559
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   560
wenzelm@252
   561
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   562
fun tha RSN (i,thb) =
clasohm@0
   563
  case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   564
      ([th],_) => th
clasohm@0
   565
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   566
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   567
clasohm@0
   568
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   569
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   570
clasohm@0
   571
(*For joining lists of rules*)
wenzelm@252
   572
fun thas RLN (i,thbs) =
clasohm@0
   573
  let val resolve = biresolution false (map (pair false) thas) i
clasohm@0
   574
      fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
clasohm@0
   575
  in  flat (map resb thbs)  end;
clasohm@0
   576
clasohm@0
   577
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   578
lcp@11
   579
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   580
  makes proof trees*)
wenzelm@252
   581
fun rls MRS bottom_rl =
lcp@11
   582
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   583
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   584
  in  rs_aux 1 rls  end;
lcp@11
   585
lcp@11
   586
(*As above, but for rule lists*)
wenzelm@252
   587
fun rlss MRL bottom_rls =
lcp@11
   588
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   589
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   590
  in  rs_aux 1 rlss  end;
lcp@11
   591
wenzelm@252
   592
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   593
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   594
  ALWAYS deletes premise i *)
wenzelm@252
   595
fun compose(tha,i,thb) =
clasohm@0
   596
    Sequence.list_of_s (bicompose false (false,tha,0) i thb);
clasohm@0
   597
clasohm@0
   598
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   599
fun tha COMP thb =
clasohm@0
   600
    case compose(tha,1,thb) of
wenzelm@252
   601
        [th] => th
clasohm@0
   602
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   603
clasohm@0
   604
(*Instantiate theorem th, reading instantiations under signature sg*)
clasohm@0
   605
fun read_instantiate_sg sg sinsts th =
clasohm@0
   606
    let val ts = types_sorts th;
nipkow@952
   607
        val used = add_term_tvarnames(#prop(rep_thm th),[]);
nipkow@952
   608
    in  instantiate (read_insts sg ts ts used sinsts) th  end;
clasohm@0
   609
clasohm@0
   610
(*Instantiate theorem th, reading instantiations under theory of th*)
clasohm@0
   611
fun read_instantiate sinsts th =
clasohm@0
   612
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
clasohm@0
   613
clasohm@0
   614
clasohm@0
   615
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
clasohm@0
   616
  Instantiates distinct Vars by terms, inferring type instantiations. *)
clasohm@0
   617
local
nipkow@1435
   618
  fun add_types ((ct,cu), (sign,tye,maxidx)) =
nipkow@1435
   619
    let val {sign=signt, t=t, T= T, maxidx=maxidxt,...} = rep_cterm ct
nipkow@1435
   620
        and {sign=signu, t=u, T= U, maxidx=maxidxu,...} = rep_cterm cu;
nipkow@1435
   621
        val maxi = max[maxidx,maxidxt,maxidxu];
clasohm@0
   622
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
nipkow@1435
   623
        val (tye',maxi') = Type.unify (#tsig(Sign.rep_sg sign')) maxi tye (T,U)
wenzelm@252
   624
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
nipkow@1435
   625
    in  (sign', tye', maxi')  end;
clasohm@0
   626
in
wenzelm@252
   627
fun cterm_instantiate ctpairs0 th =
nipkow@1435
   628
  let val (sign,tye,_) = foldr add_types (ctpairs0, (#sign(rep_thm th),[],0))
clasohm@0
   629
      val tsig = #tsig(Sign.rep_sg sign);
clasohm@0
   630
      fun instT(ct,cu) = let val inst = subst_TVars tye
wenzelm@252
   631
                         in (cterm_fun inst ct, cterm_fun inst cu) end
lcp@229
   632
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
clasohm@0
   633
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
wenzelm@252
   634
  handle TERM _ =>
clasohm@0
   635
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
clasohm@0
   636
       | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
clasohm@0
   637
end;
clasohm@0
   638
clasohm@0
   639
clasohm@0
   640
(** theorem equality test is exported and used by BEST_FIRST **)
clasohm@0
   641
wenzelm@252
   642
(*equality of theorems uses equality of signatures and
clasohm@0
   643
  the a-convertible test for terms*)
wenzelm@252
   644
fun eq_thm (th1,th2) =
wenzelm@1218
   645
    let val {sign=sg1, shyps=shyps1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
wenzelm@1218
   646
        and {sign=sg2, shyps=shyps2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
wenzelm@252
   647
    in  Sign.eq_sg (sg1,sg2) andalso
wenzelm@1218
   648
        eq_set (shyps1, shyps2) andalso
wenzelm@252
   649
        aconvs(hyps1,hyps2) andalso
wenzelm@252
   650
        prop1 aconv prop2
clasohm@0
   651
    end;
clasohm@0
   652
clasohm@1241
   653
(*equality of theorems using similarity of signatures,
clasohm@1241
   654
  i.e. the theorems belong to the same theory but not necessarily to the same
clasohm@1241
   655
  version of this theory*)
clasohm@1241
   656
fun same_thm (th1,th2) =
clasohm@1241
   657
    let val {sign=sg1, shyps=shyps1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
clasohm@1241
   658
        and {sign=sg2, shyps=shyps2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
clasohm@1241
   659
    in  Sign.same_sg (sg1,sg2) andalso
clasohm@1241
   660
        eq_set (shyps1, shyps2) andalso
clasohm@1241
   661
        aconvs(hyps1,hyps2) andalso
clasohm@1241
   662
        prop1 aconv prop2
clasohm@1241
   663
    end;
clasohm@1241
   664
clasohm@0
   665
(*Do the two theorems have the same signature?*)
wenzelm@252
   666
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
clasohm@0
   667
clasohm@0
   668
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   669
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   670
clasohm@0
   671
lcp@1194
   672
(** Mark Staples's weaker version of eq_thm: ignores variable renaming and
lcp@1194
   673
    (some) type variable renaming **)
lcp@1194
   674
lcp@1194
   675
 (* Can't use term_vars, because it sorts the resulting list of variable names.
lcp@1194
   676
    We instead need the unique list noramlised by the order of appearance
lcp@1194
   677
    in the term. *)
lcp@1194
   678
fun term_vars' (t as Var(v,T)) = [t]
lcp@1194
   679
  | term_vars' (Abs(_,_,b)) = term_vars' b
lcp@1194
   680
  | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
lcp@1194
   681
  | term_vars' _ = [];
lcp@1194
   682
lcp@1194
   683
fun forall_intr_vars th =
lcp@1194
   684
  let val {prop,sign,...} = rep_thm th;
lcp@1194
   685
      val vars = distinct (term_vars' prop);
lcp@1194
   686
  in forall_intr_list (map (cterm_of sign) vars) th end;
lcp@1194
   687
wenzelm@1237
   688
fun weak_eq_thm (tha,thb) =
lcp@1194
   689
    eq_thm(forall_intr_vars (freezeT tha), forall_intr_vars (freezeT thb));
lcp@1194
   690
lcp@1194
   691
lcp@1194
   692
clasohm@0
   693
(*** Meta-Rewriting Rules ***)
clasohm@0
   694
clasohm@0
   695
clasohm@0
   696
val reflexive_thm =
clasohm@922
   697
  let val cx = cterm_of Sign.proto_pure (Var(("x",0),TVar(("'a",0),logicS)))
clasohm@0
   698
  in Thm.reflexive cx end;
clasohm@0
   699
clasohm@0
   700
val symmetric_thm =
clasohm@922
   701
  let val xy = read_cterm Sign.proto_pure ("x::'a::logic == y",propT)
clasohm@0
   702
  in standard(Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
clasohm@0
   703
clasohm@0
   704
val transitive_thm =
clasohm@922
   705
  let val xy = read_cterm Sign.proto_pure ("x::'a::logic == y",propT)
clasohm@922
   706
      val yz = read_cterm Sign.proto_pure ("y::'a::logic == z",propT)
clasohm@0
   707
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
clasohm@0
   708
  in standard(Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
clasohm@0
   709
lcp@229
   710
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   711
clasohm@922
   712
val refl_cimplies = reflexive (cterm_of Sign.proto_pure implies);
clasohm@0
   713
clasohm@0
   714
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   715
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   716
fun goals_conv pred cv =
lcp@229
   717
  let fun gconv i ct =
lcp@229
   718
        let val (A,B) = Thm.dest_cimplies ct
lcp@229
   719
            val (thA,j) = case term_of A of
lcp@229
   720
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   721
                | _ => (if pred i then cv A else reflexive A, i+1)
wenzelm@252
   722
        in  combination (combination refl_cimplies thA) (gconv j B) end
lcp@229
   723
        handle TERM _ => reflexive ct
clasohm@0
   724
  in gconv 1 end;
clasohm@0
   725
clasohm@0
   726
(*Use a conversion to transform a theorem*)
lcp@229
   727
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   728
clasohm@0
   729
(*rewriting conversion*)
lcp@229
   730
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   731
clasohm@0
   732
(*Rewrite a theorem*)
paulson@1412
   733
fun rewrite_rule []   th = th
paulson@1412
   734
  | rewrite_rule thms th =
paulson@1412
   735
	fconv_rule (rew_conv (true,false) (K(K None)) (Thm.mss_of thms)) th;
clasohm@0
   736
clasohm@0
   737
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
paulson@1412
   738
fun rewrite_goals_rule []   th = th
paulson@1412
   739
  | rewrite_goals_rule thms th =
paulson@1412
   740
	fconv_rule (goals_conv (K true) 
paulson@1412
   741
		    (rew_conv (true,false) (K(K None))
paulson@1412
   742
		     (Thm.mss_of thms))) 
paulson@1412
   743
	           th;
clasohm@0
   744
clasohm@0
   745
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   746
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   747
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   748
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   749
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   750
clasohm@0
   751
clasohm@0
   752
(** Derived rules mainly for METAHYPS **)
clasohm@0
   753
clasohm@0
   754
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
clasohm@0
   755
fun equal_abs_elim ca eqth =
lcp@229
   756
  let val {sign=signa, t=a, ...} = rep_cterm ca
clasohm@0
   757
      and combth = combination eqth (reflexive ca)
clasohm@0
   758
      val {sign,prop,...} = rep_thm eqth
clasohm@0
   759
      val (abst,absu) = Logic.dest_equals prop
lcp@229
   760
      val cterm = cterm_of (Sign.merge (sign,signa))
clasohm@0
   761
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
clasohm@0
   762
           (transitive combth (beta_conversion (cterm (absu$a))))
clasohm@0
   763
  end
clasohm@0
   764
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
clasohm@0
   765
clasohm@0
   766
(*Calling equal_abs_elim with multiple terms*)
clasohm@0
   767
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
clasohm@0
   768
clasohm@0
   769
local
clasohm@0
   770
  open Logic
clasohm@0
   771
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
clasohm@0
   772
  fun err th = raise THM("flexpair_inst: ", 0, [th])
clasohm@0
   773
  fun flexpair_inst def th =
clasohm@0
   774
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
wenzelm@252
   775
        val cterm = cterm_of sign
wenzelm@252
   776
        fun cvar a = cterm(Var((a,0),alpha))
wenzelm@252
   777
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
wenzelm@252
   778
                   def
clasohm@0
   779
    in  equal_elim def' th
clasohm@0
   780
    end
clasohm@0
   781
    handle THM _ => err th | bind => err th
clasohm@0
   782
in
clasohm@0
   783
val flexpair_intr = flexpair_inst (symmetric flexpair_def)
clasohm@0
   784
and flexpair_elim = flexpair_inst flexpair_def
clasohm@0
   785
end;
clasohm@0
   786
clasohm@0
   787
(*Version for flexflex pairs -- this supports lifting.*)
wenzelm@252
   788
fun flexpair_abs_elim_list cts =
clasohm@0
   789
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
clasohm@0
   790
clasohm@0
   791
clasohm@0
   792
(*** Some useful meta-theorems ***)
clasohm@0
   793
clasohm@0
   794
(*The rule V/V, obtains assumption solving for eresolve_tac*)
clasohm@922
   795
val asm_rl = trivial(read_cterm Sign.proto_pure ("PROP ?psi",propT));
clasohm@0
   796
clasohm@0
   797
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
clasohm@922
   798
val cut_rl = trivial(read_cterm Sign.proto_pure
wenzelm@252
   799
        ("PROP ?psi ==> PROP ?theta", propT));
clasohm@0
   800
wenzelm@252
   801
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   802
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   803
val revcut_rl =
clasohm@922
   804
  let val V = read_cterm Sign.proto_pure ("PROP V", propT)
clasohm@922
   805
      and VW = read_cterm Sign.proto_pure ("PROP V ==> PROP W", propT);
wenzelm@252
   806
  in  standard (implies_intr V
wenzelm@252
   807
                (implies_intr VW
wenzelm@252
   808
                 (implies_elim (assume VW) (assume V))))
clasohm@0
   809
  end;
clasohm@0
   810
lcp@668
   811
(*for deleting an unwanted assumption*)
lcp@668
   812
val thin_rl =
clasohm@922
   813
  let val V = read_cterm Sign.proto_pure ("PROP V", propT)
clasohm@922
   814
      and W = read_cterm Sign.proto_pure ("PROP W", propT);
lcp@668
   815
  in  standard (implies_intr V (implies_intr W (assume W)))
lcp@668
   816
  end;
lcp@668
   817
clasohm@0
   818
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   819
val triv_forall_equality =
clasohm@922
   820
  let val V  = read_cterm Sign.proto_pure ("PROP V", propT)
clasohm@922
   821
      and QV = read_cterm Sign.proto_pure ("!!x::'a. PROP V", propT)
clasohm@922
   822
      and x  = read_cterm Sign.proto_pure ("x", TFree("'a",logicS));
clasohm@0
   823
  in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@252
   824
                           (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   825
  end;
clasohm@0
   826
clasohm@0
   827
end
clasohm@0
   828
end;
wenzelm@252
   829