src/Pure/drule.ML
author wenzelm
Tue Aug 23 19:34:01 1994 +0200 (1994-08-23 ago)
changeset 575 74f0e5fce609
parent 561 95225e63ef02
child 641 49fc43cd6a35
permissions -rw-r--r--
added print_syntax: theory -> unit;
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
wenzelm@561
    15
  val add_defs: (string * string) list -> theory -> theory
wenzelm@561
    16
  val add_defs_i: (string * term) list -> theory -> theory
clasohm@0
    17
  val asm_rl: thm
clasohm@0
    18
  val assume_ax: theory -> string -> thm
clasohm@0
    19
  val COMP: thm * thm -> thm
clasohm@0
    20
  val compose: thm * int * thm -> thm list
lcp@229
    21
  val cterm_instantiate: (cterm*cterm)list -> thm -> thm
clasohm@0
    22
  val cut_rl: thm
lcp@229
    23
  val equal_abs_elim: cterm  -> thm -> thm
lcp@229
    24
  val equal_abs_elim_list: cterm list -> thm -> thm
clasohm@0
    25
  val eq_thm: thm * thm -> bool
clasohm@0
    26
  val eq_thm_sg: thm * thm -> bool
lcp@229
    27
  val flexpair_abs_elim_list: cterm list -> thm -> thm
lcp@229
    28
  val forall_intr_list: cterm list -> thm -> thm
clasohm@0
    29
  val forall_intr_frees: thm -> thm
lcp@229
    30
  val forall_elim_list: cterm list -> thm -> thm
clasohm@0
    31
  val forall_elim_var: int -> thm -> thm
clasohm@0
    32
  val forall_elim_vars: int -> thm -> thm
clasohm@0
    33
  val implies_elim_list: thm -> thm list -> thm
lcp@229
    34
  val implies_intr_list: cterm list -> thm -> thm
lcp@11
    35
  val MRL: thm list list * thm list -> thm list
lcp@11
    36
  val MRS: thm list * thm -> thm
lcp@229
    37
  val pprint_cterm: cterm -> pprint_args -> unit
lcp@229
    38
  val pprint_ctyp: ctyp -> pprint_args -> unit
lcp@229
    39
  val pprint_theory: theory -> pprint_args -> unit
lcp@229
    40
  val pprint_thm: thm -> pprint_args -> unit
lcp@229
    41
  val pretty_thm: thm -> Sign.Syntax.Pretty.T
lcp@229
    42
  val print_cterm: cterm -> unit
lcp@229
    43
  val print_ctyp: ctyp -> unit
clasohm@0
    44
  val print_goals: int -> thm -> unit
lcp@67
    45
  val print_goals_ref: (int -> thm -> unit) ref
wenzelm@575
    46
  val print_syntax: theory -> unit
wenzelm@385
    47
  val print_sign: theory -> unit
wenzelm@385
    48
  val print_axioms: theory -> unit
clasohm@0
    49
  val print_theory: theory -> unit
clasohm@0
    50
  val print_thm: thm -> unit
clasohm@0
    51
  val prth: thm -> thm
clasohm@0
    52
  val prthq: thm Sequence.seq -> thm Sequence.seq
clasohm@0
    53
  val prths: thm list -> thm list
clasohm@0
    54
  val read_instantiate: (string*string)list -> thm -> thm
clasohm@0
    55
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
wenzelm@252
    56
  val read_insts:
lcp@229
    57
          Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
lcp@229
    58
                  -> (indexname -> typ option) * (indexname -> sort option)
lcp@229
    59
                  -> (string*string)list
lcp@229
    60
                  -> (indexname*ctyp)list * (cterm*cterm)list
clasohm@0
    61
  val reflexive_thm: thm
clasohm@0
    62
  val revcut_rl: thm
nipkow@214
    63
  val rewrite_goal_rule: bool*bool -> (meta_simpset -> thm -> thm option)
nipkow@214
    64
        -> meta_simpset -> int -> thm -> thm
clasohm@0
    65
  val rewrite_goals_rule: thm list -> thm -> thm
clasohm@0
    66
  val rewrite_rule: thm list -> thm -> thm
clasohm@0
    67
  val RS: thm * thm -> thm
clasohm@0
    68
  val RSN: thm * (int * thm) -> thm
clasohm@0
    69
  val RL: thm list * thm list -> thm list
clasohm@0
    70
  val RLN: thm list * (int * thm list) -> thm list
clasohm@0
    71
  val show_hyps: bool ref
clasohm@0
    72
  val size_of_thm: thm -> int
clasohm@0
    73
  val standard: thm -> thm
lcp@229
    74
  val string_of_cterm: cterm -> string
lcp@229
    75
  val string_of_ctyp: ctyp -> string
clasohm@0
    76
  val string_of_thm: thm -> string
clasohm@0
    77
  val symmetric_thm: thm
clasohm@0
    78
  val transitive_thm: thm
clasohm@0
    79
  val triv_forall_equality: thm
clasohm@0
    80
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
clasohm@0
    81
  val zero_var_indexes: thm -> thm
clasohm@0
    82
  end
clasohm@0
    83
  end;
clasohm@0
    84
wenzelm@252
    85
functor DruleFun (structure Logic: LOGIC and Thm: THM): DRULE =
clasohm@0
    86
struct
clasohm@0
    87
structure Thm = Thm;
clasohm@0
    88
structure Sign = Thm.Sign;
clasohm@0
    89
structure Type = Sign.Type;
wenzelm@575
    90
structure Syntax = Sign.Syntax;
wenzelm@575
    91
structure Pretty = Syntax.Pretty
wenzelm@400
    92
structure Symtab = Sign.Symtab;
wenzelm@400
    93
clasohm@0
    94
local open Thm
clasohm@0
    95
in
clasohm@0
    96
wenzelm@561
    97
(**** Extend Theories ****)
wenzelm@561
    98
wenzelm@561
    99
(** add constant definitions **)
wenzelm@561
   100
wenzelm@561
   101
(* all_axioms_of *)
wenzelm@561
   102
wenzelm@561
   103
(*results may contain duplicates!*)
wenzelm@561
   104
wenzelm@561
   105
fun ancestry_of thy =
wenzelm@561
   106
  thy :: flat (map ancestry_of (parents_of thy));
wenzelm@561
   107
wenzelm@561
   108
val all_axioms_of = flat o map axioms_of o ancestry_of;
wenzelm@561
   109
wenzelm@561
   110
wenzelm@561
   111
(* clash_types, clash_consts *)
wenzelm@561
   112
wenzelm@561
   113
(*check if types have common instance (ignoring sorts)*)
wenzelm@561
   114
wenzelm@561
   115
fun clash_types ty1 ty2 =
wenzelm@561
   116
  let
wenzelm@561
   117
    val ty1' = Type.varifyT ty1;
wenzelm@561
   118
    val ty2' = incr_tvar (maxidx_of_typ ty1' + 1) (Type.varifyT ty2);
wenzelm@561
   119
  in
wenzelm@561
   120
    Type.raw_unify (ty1', ty2')
wenzelm@561
   121
  end;
wenzelm@561
   122
wenzelm@561
   123
fun clash_consts (c1, ty1) (c2, ty2) =
wenzelm@561
   124
  c1 = c2 andalso clash_types ty1 ty2;
wenzelm@561
   125
wenzelm@561
   126
wenzelm@561
   127
(* clash_defns *)
wenzelm@561
   128
wenzelm@561
   129
fun clash_defn c_ty (name, tm) =
wenzelm@561
   130
  let val (c, ty') = dest_Const (head_of (fst (Logic.dest_equals tm))) in
wenzelm@561
   131
    if clash_consts c_ty (c, ty') then Some (name, ty') else None
wenzelm@561
   132
  end handle TERM _ => None;
wenzelm@561
   133
wenzelm@561
   134
fun clash_defns c_ty axms =
wenzelm@561
   135
  distinct (mapfilter (clash_defn c_ty) axms);
wenzelm@561
   136
wenzelm@561
   137
wenzelm@561
   138
(* dest_defn *)
wenzelm@561
   139
wenzelm@561
   140
fun dest_defn tm =
wenzelm@561
   141
  let
wenzelm@561
   142
    fun err msg = raise_term msg [tm];
wenzelm@561
   143
wenzelm@561
   144
    val (lhs, rhs) = Logic.dest_equals tm
wenzelm@561
   145
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@561
   146
    val (head, args) = strip_comb lhs;
wenzelm@561
   147
    val (c, ty) = dest_Const head
wenzelm@561
   148
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@561
   149
wenzelm@561
   150
    fun occs_const (Const c_ty') = clash_consts (c, ty) c_ty'
wenzelm@561
   151
      | occs_const (Abs (_, _, t)) = occs_const t
wenzelm@561
   152
      | occs_const (t $ u) = occs_const t orelse occs_const u
wenzelm@561
   153
      | occs_const _ = false;
wenzelm@561
   154
  in
wenzelm@561
   155
    if not (forall is_Free args) then
wenzelm@561
   156
      err "Arguments of lhs have to be variables"
wenzelm@561
   157
    else if not (null (duplicates args)) then
wenzelm@561
   158
      err "Duplicate variables on lhs"
wenzelm@561
   159
    else if not (term_frees rhs subset args) then
wenzelm@561
   160
      err "Extra variables on rhs"
wenzelm@561
   161
    else if not (term_tfrees rhs subset typ_tfrees ty) then
wenzelm@561
   162
      err "Extra type variables on rhs"
wenzelm@561
   163
    else if occs_const rhs then
wenzelm@561
   164
      err "Constant to be defined clashes with occurrence(s) on rhs"
wenzelm@561
   165
    else (c, ty)
wenzelm@561
   166
  end;
wenzelm@561
   167
wenzelm@561
   168
wenzelm@561
   169
(* check_defn *)
wenzelm@561
   170
wenzelm@561
   171
fun err_in_axm name msg =
wenzelm@561
   172
  (writeln msg; error ("The error(s) above occurred in axiom " ^ quote name));
wenzelm@561
   173
wenzelm@561
   174
fun check_defn sign (axms, (name, tm)) =
wenzelm@561
   175
  let
wenzelm@561
   176
    fun show_const (c, ty) = quote (Pretty.string_of (Pretty.block
wenzelm@561
   177
      [Pretty.str (c ^ " ::"), Pretty.brk 1, Sign.pretty_typ sign ty]));
wenzelm@561
   178
wenzelm@561
   179
    fun show_defn c (dfn, ty') = show_const (c, ty') ^ " in " ^ dfn;
wenzelm@561
   180
    fun show_defns c = commas o map (show_defn c);
wenzelm@561
   181
wenzelm@561
   182
    val (c, ty) = dest_defn tm
wenzelm@561
   183
      handle TERM (msg, _) => err_in_axm name msg;
wenzelm@561
   184
    val defns = clash_defns (c, ty) axms;
wenzelm@561
   185
  in
wenzelm@561
   186
    if not (null defns) then
wenzelm@561
   187
      err_in_axm name ("Definition of " ^ show_const (c, ty) ^
wenzelm@561
   188
        " clashes with " ^ show_defns c defns)
wenzelm@561
   189
    else (name, tm) :: axms
wenzelm@561
   190
  end;
wenzelm@561
   191
wenzelm@561
   192
wenzelm@561
   193
(* add_defs *)
wenzelm@561
   194
wenzelm@561
   195
fun ext_defns prep_axm raw_axms thy =
wenzelm@561
   196
  let
wenzelm@561
   197
    val axms = map (prep_axm (sign_of thy)) raw_axms;
wenzelm@561
   198
    val all_axms = all_axioms_of thy;
wenzelm@561
   199
  in
wenzelm@561
   200
    foldl (check_defn (sign_of thy)) (all_axms, axms);
wenzelm@561
   201
    add_axioms_i axms thy
wenzelm@561
   202
  end;
wenzelm@561
   203
wenzelm@561
   204
val add_defs_i = ext_defns cert_axm;
wenzelm@561
   205
val add_defs = ext_defns read_axm;
wenzelm@561
   206
wenzelm@561
   207
wenzelm@561
   208
clasohm@0
   209
(**** More derived rules and operations on theorems ****)
clasohm@0
   210
lcp@229
   211
(** reading of instantiations **)
lcp@229
   212
lcp@229
   213
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
lcp@229
   214
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
lcp@229
   215
lcp@229
   216
fun absent ixn =
lcp@229
   217
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
lcp@229
   218
lcp@229
   219
fun inst_failure ixn =
lcp@229
   220
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
lcp@229
   221
lcp@229
   222
fun read_insts sign (rtypes,rsorts) (types,sorts) insts =
lcp@229
   223
let val {tsig,...} = Sign.rep_sg sign
lcp@229
   224
    fun split([],tvs,vs) = (tvs,vs)
lcp@229
   225
      | split((sv,st)::l,tvs,vs) = (case explode sv of
lcp@229
   226
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
lcp@229
   227
                | cs => split(l,tvs,(indexname cs,st)::vs));
lcp@229
   228
    val (tvs,vs) = split(insts,[],[]);
lcp@229
   229
    fun readT((a,i),st) =
lcp@229
   230
        let val ixn = ("'" ^ a,i);
lcp@229
   231
            val S = case rsorts ixn of Some S => S | None => absent ixn;
lcp@229
   232
            val T = Sign.read_typ (sign,sorts) st;
lcp@229
   233
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
lcp@229
   234
           else inst_failure ixn
lcp@229
   235
        end
lcp@229
   236
    val tye = map readT tvs;
lcp@229
   237
    fun add_cterm ((cts,tye), (ixn,st)) =
lcp@229
   238
        let val T = case rtypes ixn of
lcp@229
   239
                      Some T => typ_subst_TVars tye T
lcp@229
   240
                    | None => absent ixn;
lcp@229
   241
            val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T);
lcp@229
   242
            val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T))
lcp@229
   243
        in ((cv,ct)::cts,tye2 @ tye) end
lcp@229
   244
    val (cterms,tye') = foldl add_cterm (([],tye), vs);
lcp@229
   245
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
lcp@229
   246
lcp@229
   247
wenzelm@252
   248
wenzelm@252
   249
(*** Printing of theories, theorems, etc. ***)
lcp@229
   250
lcp@229
   251
(*If false, hypotheses are printed as dots*)
lcp@229
   252
val show_hyps = ref true;
lcp@229
   253
lcp@229
   254
fun pretty_thm th =
lcp@229
   255
let val {sign, hyps, prop,...} = rep_thm th
lcp@229
   256
    val hsymbs = if null hyps then []
wenzelm@252
   257
                 else if !show_hyps then
wenzelm@252
   258
                      [Pretty.brk 2,
wenzelm@252
   259
                       Pretty.lst("[","]") (map (Sign.pretty_term sign) hyps)]
wenzelm@252
   260
                 else Pretty.str" [" :: map (fn _ => Pretty.str".") hyps @
wenzelm@252
   261
                      [Pretty.str"]"];
lcp@229
   262
in Pretty.blk(0, Sign.pretty_term sign prop :: hsymbs) end;
lcp@229
   263
lcp@229
   264
val string_of_thm = Pretty.string_of o pretty_thm;
lcp@229
   265
lcp@229
   266
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
lcp@229
   267
lcp@229
   268
lcp@229
   269
(** Top-level commands for printing theorems **)
lcp@229
   270
val print_thm = writeln o string_of_thm;
lcp@229
   271
lcp@229
   272
fun prth th = (print_thm th; th);
lcp@229
   273
lcp@229
   274
(*Print and return a sequence of theorems, separated by blank lines. *)
lcp@229
   275
fun prthq thseq =
wenzelm@252
   276
  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
lcp@229
   277
lcp@229
   278
(*Print and return a list of theorems, separated by blank lines. *)
lcp@229
   279
fun prths ths = (print_list_ln print_thm ths; ths);
lcp@229
   280
wenzelm@252
   281
wenzelm@252
   282
(* other printing commands *)
lcp@229
   283
wenzelm@252
   284
fun pprint_ctyp cT =
wenzelm@252
   285
  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
wenzelm@252
   286
wenzelm@252
   287
fun string_of_ctyp cT =
wenzelm@252
   288
  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
lcp@229
   289
lcp@229
   290
val print_ctyp = writeln o string_of_ctyp;
lcp@229
   291
wenzelm@252
   292
fun pprint_cterm ct =
wenzelm@252
   293
  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
lcp@229
   294
wenzelm@252
   295
fun string_of_cterm ct =
wenzelm@252
   296
  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
lcp@229
   297
lcp@229
   298
val print_cterm = writeln o string_of_cterm;
lcp@229
   299
wenzelm@252
   300
wenzelm@252
   301
(* print theory *)
wenzelm@252
   302
wenzelm@252
   303
val pprint_theory = Sign.pprint_sg o sign_of;
lcp@229
   304
wenzelm@575
   305
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@575
   306
wenzelm@385
   307
val print_sign = Sign.print_sg o sign_of;
wenzelm@385
   308
wenzelm@385
   309
fun print_axioms thy =
wenzelm@252
   310
  let
wenzelm@400
   311
    val {sign, new_axioms, ...} = rep_theory thy;
wenzelm@400
   312
    val axioms = Symtab.dest new_axioms;
lcp@229
   313
wenzelm@385
   314
    fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
wenzelm@385
   315
      Pretty.quote (Sign.pretty_term sign t)];
wenzelm@252
   316
  in
wenzelm@385
   317
    Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
wenzelm@252
   318
  end;
lcp@229
   319
wenzelm@385
   320
fun print_theory thy = (print_sign thy; print_axioms thy);
wenzelm@385
   321
lcp@229
   322
lcp@229
   323
lcp@229
   324
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
lcp@229
   325
lcp@229
   326
fun prettyprints es = writeln(Pretty.string_of(Pretty.blk(0,es)));
lcp@229
   327
lcp@229
   328
fun print_goals maxgoals th : unit =
lcp@229
   329
let val {sign, hyps, prop,...} = rep_thm th;
lcp@229
   330
    fun printgoals (_, []) = ()
lcp@229
   331
      | printgoals (n, A::As) =
wenzelm@252
   332
        let val prettyn = Pretty.str(" " ^ string_of_int n ^ ". ");
wenzelm@252
   333
            val prettyA = Sign.pretty_term sign A
wenzelm@252
   334
        in prettyprints[prettyn,prettyA];
wenzelm@252
   335
           printgoals (n+1,As)
lcp@229
   336
        end;
lcp@229
   337
    fun prettypair(t,u) =
lcp@229
   338
        Pretty.blk(0, [Sign.pretty_term sign t, Pretty.str" =?=", Pretty.brk 1,
wenzelm@252
   339
                       Sign.pretty_term sign u]);
lcp@229
   340
    fun printff [] = ()
lcp@229
   341
      | printff tpairs =
wenzelm@252
   342
         writeln("\nFlex-flex pairs:\n" ^
wenzelm@252
   343
                 Pretty.string_of(Pretty.lst("","") (map prettypair tpairs)))
lcp@229
   344
    val (tpairs,As,B) = Logic.strip_horn(prop);
lcp@229
   345
    val ngoals = length As
wenzelm@252
   346
in
lcp@229
   347
   writeln (Sign.string_of_term sign B);
lcp@229
   348
   if ngoals=0 then writeln"No subgoals!"
wenzelm@252
   349
   else if ngoals>maxgoals
lcp@229
   350
        then (printgoals (1, take(maxgoals,As));
wenzelm@252
   351
              writeln("A total of " ^ string_of_int ngoals ^ " subgoals..."))
lcp@229
   352
        else printgoals (1, As);
lcp@229
   353
   printff tpairs
lcp@229
   354
end;
lcp@229
   355
lcp@229
   356
(*"hook" for user interfaces: allows print_goals to be replaced*)
lcp@229
   357
val print_goals_ref = ref print_goals;
lcp@229
   358
wenzelm@252
   359
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   360
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   361
     type variables) when reading another term.
clasohm@0
   362
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   363
***)
clasohm@0
   364
clasohm@0
   365
fun types_sorts thm =
clasohm@0
   366
    let val {prop,hyps,...} = rep_thm thm;
wenzelm@252
   367
        val big = list_comb(prop,hyps); (* bogus term! *)
wenzelm@252
   368
        val vars = map dest_Var (term_vars big);
wenzelm@252
   369
        val frees = map dest_Free (term_frees big);
wenzelm@252
   370
        val tvars = term_tvars big;
wenzelm@252
   371
        val tfrees = term_tfrees big;
wenzelm@252
   372
        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
wenzelm@252
   373
        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
clasohm@0
   374
    in (typ,sort) end;
clasohm@0
   375
clasohm@0
   376
(** Standardization of rules **)
clasohm@0
   377
clasohm@0
   378
(*Generalization over a list of variables, IGNORING bad ones*)
clasohm@0
   379
fun forall_intr_list [] th = th
clasohm@0
   380
  | forall_intr_list (y::ys) th =
wenzelm@252
   381
        let val gth = forall_intr_list ys th
wenzelm@252
   382
        in  forall_intr y gth   handle THM _ =>  gth  end;
clasohm@0
   383
clasohm@0
   384
(*Generalization over all suitable Free variables*)
clasohm@0
   385
fun forall_intr_frees th =
clasohm@0
   386
    let val {prop,sign,...} = rep_thm th
clasohm@0
   387
    in  forall_intr_list
wenzelm@252
   388
         (map (cterm_of sign) (sort atless (term_frees prop)))
clasohm@0
   389
         th
clasohm@0
   390
    end;
clasohm@0
   391
clasohm@0
   392
(*Replace outermost quantified variable by Var of given index.
clasohm@0
   393
    Could clash with Vars already present.*)
wenzelm@252
   394
fun forall_elim_var i th =
clasohm@0
   395
    let val {prop,sign,...} = rep_thm th
clasohm@0
   396
    in case prop of
wenzelm@252
   397
          Const("all",_) $ Abs(a,T,_) =>
wenzelm@252
   398
              forall_elim (cterm_of sign (Var((a,i), T)))  th
wenzelm@252
   399
        | _ => raise THM("forall_elim_var", i, [th])
clasohm@0
   400
    end;
clasohm@0
   401
clasohm@0
   402
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@252
   403
fun forall_elim_vars i th =
clasohm@0
   404
    forall_elim_vars i (forall_elim_var i th)
wenzelm@252
   405
        handle THM _ => th;
clasohm@0
   406
clasohm@0
   407
(*Specialization over a list of cterms*)
clasohm@0
   408
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
clasohm@0
   409
clasohm@0
   410
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
clasohm@0
   411
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
clasohm@0
   412
clasohm@0
   413
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
clasohm@0
   414
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
clasohm@0
   415
clasohm@0
   416
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@252
   417
fun zero_var_indexes th =
clasohm@0
   418
    let val {prop,sign,...} = rep_thm th;
clasohm@0
   419
        val vars = term_vars prop
clasohm@0
   420
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
wenzelm@252
   421
        val inrs = add_term_tvars(prop,[]);
wenzelm@252
   422
        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
wenzelm@252
   423
        val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
wenzelm@252
   424
        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
wenzelm@252
   425
        fun varpairs([],[]) = []
wenzelm@252
   426
          | varpairs((var as Var(v,T)) :: vars, b::bs) =
wenzelm@252
   427
                let val T' = typ_subst_TVars tye T
wenzelm@252
   428
                in (cterm_of sign (Var(v,T')),
wenzelm@252
   429
                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
wenzelm@252
   430
                end
wenzelm@252
   431
          | varpairs _ = raise TERM("varpairs", []);
clasohm@0
   432
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
clasohm@0
   433
clasohm@0
   434
clasohm@0
   435
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
clasohm@0
   436
    all generality expressed by Vars having index 0.*)
clasohm@0
   437
fun standard th =
clasohm@0
   438
    let val {maxidx,...} = rep_thm th
wenzelm@252
   439
    in  varifyT (zero_var_indexes (forall_elim_vars(maxidx+1)
clasohm@0
   440
                         (forall_intr_frees(implies_intr_hyps th))))
clasohm@0
   441
    end;
clasohm@0
   442
wenzelm@252
   443
(*Assume a new formula, read following the same conventions as axioms.
clasohm@0
   444
  Generalizes over Free variables,
clasohm@0
   445
  creates the assumption, and then strips quantifiers.
clasohm@0
   446
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
wenzelm@252
   447
             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
clasohm@0
   448
fun assume_ax thy sP =
clasohm@0
   449
    let val sign = sign_of thy
wenzelm@252
   450
        val prop = Logic.close_form (term_of (read_cterm sign
wenzelm@252
   451
                         (sP, propT)))
lcp@229
   452
    in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
clasohm@0
   453
wenzelm@252
   454
(*Resolution: exactly one resolvent must be produced.*)
clasohm@0
   455
fun tha RSN (i,thb) =
clasohm@0
   456
  case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
clasohm@0
   457
      ([th],_) => th
clasohm@0
   458
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
clasohm@0
   459
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
clasohm@0
   460
clasohm@0
   461
(*resolution: P==>Q, Q==>R gives P==>R. *)
clasohm@0
   462
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   463
clasohm@0
   464
(*For joining lists of rules*)
wenzelm@252
   465
fun thas RLN (i,thbs) =
clasohm@0
   466
  let val resolve = biresolution false (map (pair false) thas) i
clasohm@0
   467
      fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
clasohm@0
   468
  in  flat (map resb thbs)  end;
clasohm@0
   469
clasohm@0
   470
fun thas RL thbs = thas RLN (1,thbs);
clasohm@0
   471
lcp@11
   472
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   473
  makes proof trees*)
wenzelm@252
   474
fun rls MRS bottom_rl =
lcp@11
   475
  let fun rs_aux i [] = bottom_rl
wenzelm@252
   476
        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
lcp@11
   477
  in  rs_aux 1 rls  end;
lcp@11
   478
lcp@11
   479
(*As above, but for rule lists*)
wenzelm@252
   480
fun rlss MRL bottom_rls =
lcp@11
   481
  let fun rs_aux i [] = bottom_rls
wenzelm@252
   482
        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
lcp@11
   483
  in  rs_aux 1 rlss  end;
lcp@11
   484
wenzelm@252
   485
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   486
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   487
  ALWAYS deletes premise i *)
wenzelm@252
   488
fun compose(tha,i,thb) =
clasohm@0
   489
    Sequence.list_of_s (bicompose false (false,tha,0) i thb);
clasohm@0
   490
clasohm@0
   491
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
clasohm@0
   492
fun tha COMP thb =
clasohm@0
   493
    case compose(tha,1,thb) of
wenzelm@252
   494
        [th] => th
clasohm@0
   495
      | _ =>   raise THM("COMP", 1, [tha,thb]);
clasohm@0
   496
clasohm@0
   497
(*Instantiate theorem th, reading instantiations under signature sg*)
clasohm@0
   498
fun read_instantiate_sg sg sinsts th =
clasohm@0
   499
    let val ts = types_sorts th;
lcp@229
   500
    in  instantiate (read_insts sg ts ts sinsts) th  end;
clasohm@0
   501
clasohm@0
   502
(*Instantiate theorem th, reading instantiations under theory of th*)
clasohm@0
   503
fun read_instantiate sinsts th =
clasohm@0
   504
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
clasohm@0
   505
clasohm@0
   506
clasohm@0
   507
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
clasohm@0
   508
  Instantiates distinct Vars by terms, inferring type instantiations. *)
clasohm@0
   509
local
clasohm@0
   510
  fun add_types ((ct,cu), (sign,tye)) =
lcp@229
   511
    let val {sign=signt, t=t, T= T, ...} = rep_cterm ct
lcp@229
   512
        and {sign=signu, t=u, T= U, ...} = rep_cterm cu
clasohm@0
   513
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
wenzelm@252
   514
        val tye' = Type.unify (#tsig(Sign.rep_sg sign')) ((T,U), tye)
wenzelm@252
   515
          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
clasohm@0
   516
    in  (sign', tye')  end;
clasohm@0
   517
in
wenzelm@252
   518
fun cterm_instantiate ctpairs0 th =
clasohm@0
   519
  let val (sign,tye) = foldr add_types (ctpairs0, (#sign(rep_thm th),[]))
clasohm@0
   520
      val tsig = #tsig(Sign.rep_sg sign);
clasohm@0
   521
      fun instT(ct,cu) = let val inst = subst_TVars tye
wenzelm@252
   522
                         in (cterm_fun inst ct, cterm_fun inst cu) end
lcp@229
   523
      fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
clasohm@0
   524
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
wenzelm@252
   525
  handle TERM _ =>
clasohm@0
   526
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
clasohm@0
   527
       | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
clasohm@0
   528
end;
clasohm@0
   529
clasohm@0
   530
clasohm@0
   531
(** theorem equality test is exported and used by BEST_FIRST **)
clasohm@0
   532
wenzelm@252
   533
(*equality of theorems uses equality of signatures and
clasohm@0
   534
  the a-convertible test for terms*)
wenzelm@252
   535
fun eq_thm (th1,th2) =
clasohm@0
   536
    let val {sign=sg1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
wenzelm@252
   537
        and {sign=sg2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
wenzelm@252
   538
    in  Sign.eq_sg (sg1,sg2) andalso
wenzelm@252
   539
        aconvs(hyps1,hyps2) andalso
wenzelm@252
   540
        prop1 aconv prop2
clasohm@0
   541
    end;
clasohm@0
   542
clasohm@0
   543
(*Do the two theorems have the same signature?*)
wenzelm@252
   544
fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
clasohm@0
   545
clasohm@0
   546
(*Useful "distance" function for BEST_FIRST*)
clasohm@0
   547
val size_of_thm = size_of_term o #prop o rep_thm;
clasohm@0
   548
clasohm@0
   549
clasohm@0
   550
(*** Meta-Rewriting Rules ***)
clasohm@0
   551
clasohm@0
   552
clasohm@0
   553
val reflexive_thm =
wenzelm@385
   554
  let val cx = cterm_of Sign.pure (Var(("x",0),TVar(("'a",0),logicS)))
clasohm@0
   555
  in Thm.reflexive cx end;
clasohm@0
   556
clasohm@0
   557
val symmetric_thm =
lcp@229
   558
  let val xy = read_cterm Sign.pure ("x::'a::logic == y",propT)
clasohm@0
   559
  in standard(Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
clasohm@0
   560
clasohm@0
   561
val transitive_thm =
lcp@229
   562
  let val xy = read_cterm Sign.pure ("x::'a::logic == y",propT)
lcp@229
   563
      val yz = read_cterm Sign.pure ("y::'a::logic == z",propT)
clasohm@0
   564
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
clasohm@0
   565
  in standard(Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
clasohm@0
   566
lcp@229
   567
(** Below, a "conversion" has type cterm -> thm **)
lcp@229
   568
lcp@229
   569
val refl_cimplies = reflexive (cterm_of Sign.pure implies);
clasohm@0
   570
clasohm@0
   571
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
nipkow@214
   572
(*Do not rewrite flex-flex pairs*)
wenzelm@252
   573
fun goals_conv pred cv =
lcp@229
   574
  let fun gconv i ct =
lcp@229
   575
        let val (A,B) = Thm.dest_cimplies ct
lcp@229
   576
            val (thA,j) = case term_of A of
lcp@229
   577
                  Const("=?=",_)$_$_ => (reflexive A, i)
lcp@229
   578
                | _ => (if pred i then cv A else reflexive A, i+1)
wenzelm@252
   579
        in  combination (combination refl_cimplies thA) (gconv j B) end
lcp@229
   580
        handle TERM _ => reflexive ct
clasohm@0
   581
  in gconv 1 end;
clasohm@0
   582
clasohm@0
   583
(*Use a conversion to transform a theorem*)
lcp@229
   584
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
clasohm@0
   585
clasohm@0
   586
(*rewriting conversion*)
lcp@229
   587
fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
clasohm@0
   588
clasohm@0
   589
(*Rewrite a theorem*)
nipkow@214
   590
fun rewrite_rule thms =
nipkow@214
   591
  fconv_rule (rew_conv (true,false) (K(K None)) (Thm.mss_of thms));
clasohm@0
   592
clasohm@0
   593
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
clasohm@0
   594
fun rewrite_goals_rule thms =
nipkow@214
   595
  fconv_rule (goals_conv (K true) (rew_conv (true,false) (K(K None))
nipkow@214
   596
             (Thm.mss_of thms)));
clasohm@0
   597
clasohm@0
   598
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
nipkow@214
   599
fun rewrite_goal_rule mode prover mss i thm =
nipkow@214
   600
  if 0 < i  andalso  i <= nprems_of thm
nipkow@214
   601
  then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
nipkow@214
   602
  else raise THM("rewrite_goal_rule",i,[thm]);
clasohm@0
   603
clasohm@0
   604
clasohm@0
   605
(** Derived rules mainly for METAHYPS **)
clasohm@0
   606
clasohm@0
   607
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
clasohm@0
   608
fun equal_abs_elim ca eqth =
lcp@229
   609
  let val {sign=signa, t=a, ...} = rep_cterm ca
clasohm@0
   610
      and combth = combination eqth (reflexive ca)
clasohm@0
   611
      val {sign,prop,...} = rep_thm eqth
clasohm@0
   612
      val (abst,absu) = Logic.dest_equals prop
lcp@229
   613
      val cterm = cterm_of (Sign.merge (sign,signa))
clasohm@0
   614
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
clasohm@0
   615
           (transitive combth (beta_conversion (cterm (absu$a))))
clasohm@0
   616
  end
clasohm@0
   617
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
clasohm@0
   618
clasohm@0
   619
(*Calling equal_abs_elim with multiple terms*)
clasohm@0
   620
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
clasohm@0
   621
clasohm@0
   622
local
clasohm@0
   623
  open Logic
clasohm@0
   624
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
clasohm@0
   625
  fun err th = raise THM("flexpair_inst: ", 0, [th])
clasohm@0
   626
  fun flexpair_inst def th =
clasohm@0
   627
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
wenzelm@252
   628
        val cterm = cterm_of sign
wenzelm@252
   629
        fun cvar a = cterm(Var((a,0),alpha))
wenzelm@252
   630
        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
wenzelm@252
   631
                   def
clasohm@0
   632
    in  equal_elim def' th
clasohm@0
   633
    end
clasohm@0
   634
    handle THM _ => err th | bind => err th
clasohm@0
   635
in
clasohm@0
   636
val flexpair_intr = flexpair_inst (symmetric flexpair_def)
clasohm@0
   637
and flexpair_elim = flexpair_inst flexpair_def
clasohm@0
   638
end;
clasohm@0
   639
clasohm@0
   640
(*Version for flexflex pairs -- this supports lifting.*)
wenzelm@252
   641
fun flexpair_abs_elim_list cts =
clasohm@0
   642
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
clasohm@0
   643
clasohm@0
   644
clasohm@0
   645
(*** Some useful meta-theorems ***)
clasohm@0
   646
clasohm@0
   647
(*The rule V/V, obtains assumption solving for eresolve_tac*)
lcp@229
   648
val asm_rl = trivial(read_cterm Sign.pure ("PROP ?psi",propT));
clasohm@0
   649
clasohm@0
   650
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@252
   651
val cut_rl = trivial(read_cterm Sign.pure
wenzelm@252
   652
        ("PROP ?psi ==> PROP ?theta", propT));
clasohm@0
   653
wenzelm@252
   654
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   655
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   656
val revcut_rl =
lcp@229
   657
  let val V = read_cterm Sign.pure ("PROP V", propT)
lcp@229
   658
      and VW = read_cterm Sign.pure ("PROP V ==> PROP W", propT);
wenzelm@252
   659
  in  standard (implies_intr V
wenzelm@252
   660
                (implies_intr VW
wenzelm@252
   661
                 (implies_elim (assume VW) (assume V))))
clasohm@0
   662
  end;
clasohm@0
   663
clasohm@0
   664
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   665
val triv_forall_equality =
lcp@229
   666
  let val V  = read_cterm Sign.pure ("PROP V", propT)
lcp@229
   667
      and QV = read_cterm Sign.pure ("!!x::'a. PROP V", propT)
wenzelm@385
   668
      and x  = read_cterm Sign.pure ("x", TFree("'a",logicS));
clasohm@0
   669
  in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
wenzelm@252
   670
                           (implies_intr V  (forall_intr x (assume V))))
clasohm@0
   671
  end;
clasohm@0
   672
clasohm@0
   673
end
clasohm@0
   674
end;
wenzelm@252
   675