src/Pure/more_thm.ML
author wenzelm
Tue Apr 15 16:12:05 2008 +0200 (2008-04-15)
changeset 26653 60e0cf6bef89
parent 26628 63306cb94313
child 26665 2e363edf7578
permissions -rw-r--r--
Thm.forall_elim_var(s);
wenzelm@22362
     1
(*  Title:      Pure/more_thm.ML
wenzelm@22362
     2
    ID:         $Id$
wenzelm@22362
     3
    Author:     Makarius
wenzelm@22362
     4
wenzelm@22907
     5
Further operations on type ctyp/cterm/thm, outside the inference kernel.
wenzelm@22362
     6
*)
wenzelm@22362
     7
wenzelm@23169
     8
infix aconvc;
wenzelm@23169
     9
wenzelm@22362
    10
signature THM =
wenzelm@22362
    11
sig
wenzelm@22362
    12
  include THM
wenzelm@24948
    13
  val aconvc: cterm * cterm -> bool
wenzelm@23491
    14
  val add_cterm_frees: cterm -> cterm list -> cterm list
wenzelm@22907
    15
  val mk_binop: cterm -> cterm -> cterm -> cterm
wenzelm@22907
    16
  val dest_binop: cterm -> cterm * cterm
wenzelm@22907
    17
  val dest_implies: cterm -> cterm * cterm
wenzelm@22907
    18
  val dest_equals: cterm -> cterm * cterm
wenzelm@22907
    19
  val dest_equals_lhs: cterm -> cterm
wenzelm@22907
    20
  val dest_equals_rhs: cterm -> cterm
wenzelm@22907
    21
  val lhs_of: thm -> cterm
wenzelm@22907
    22
  val rhs_of: thm -> cterm
wenzelm@22362
    23
  val thm_ord: thm * thm -> order
wenzelm@23599
    24
  val is_reflexive: thm -> bool
wenzelm@22362
    25
  val eq_thm: thm * thm -> bool
wenzelm@22362
    26
  val eq_thms: thm list * thm list -> bool
wenzelm@22362
    27
  val eq_thm_thy: thm * thm -> bool
wenzelm@22362
    28
  val eq_thm_prop: thm * thm -> bool
wenzelm@22362
    29
  val equiv_thm: thm * thm -> bool
wenzelm@24048
    30
  val is_dummy: thm -> bool
wenzelm@22695
    31
  val plain_prop_of: thm -> term
wenzelm@22695
    32
  val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@24048
    33
  val add_thm: thm -> thm list -> thm list
wenzelm@24048
    34
  val del_thm: thm -> thm list -> thm list
wenzelm@24048
    35
  val merge_thms: thm list * thm list -> thm list
wenzelm@22362
    36
  val axiomK: string
wenzelm@22362
    37
  val assumptionK: string
wenzelm@22362
    38
  val definitionK: string
wenzelm@22362
    39
  val theoremK: string
wenzelm@22362
    40
  val lemmaK: string
wenzelm@22362
    41
  val corollaryK: string
wenzelm@22362
    42
  val internalK: string
wenzelm@22362
    43
  val rule_attribute: (Context.generic -> thm -> thm) -> attribute
wenzelm@22362
    44
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@22362
    45
  val theory_attributes: attribute list -> theory * thm -> theory * thm
wenzelm@22362
    46
  val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
wenzelm@22362
    47
  val no_attributes: 'a -> 'a * 'b list
wenzelm@22362
    48
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@22682
    49
  val read_def_cterms:
wenzelm@22682
    50
    theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@22682
    51
    string list -> bool -> (string * typ)list
wenzelm@22682
    52
    -> cterm list * (indexname * typ)list
wenzelm@22682
    53
  val read_cterm: theory -> string * typ -> cterm
wenzelm@24980
    54
  val elim_implies: thm -> thm -> thm
wenzelm@26653
    55
  val forall_elim_var: int -> thm -> thm
wenzelm@26653
    56
  val forall_elim_vars: int -> thm -> thm
wenzelm@24980
    57
  val unvarify: thm -> thm
wenzelm@26628
    58
  val close_derivation: thm -> thm
wenzelm@24980
    59
  val add_axiom: term list -> bstring * term -> theory -> thm * theory
haftmann@25518
    60
  val add_def: bool -> bool -> bstring * term -> theory -> thm * theory
wenzelm@22362
    61
end;
wenzelm@22362
    62
wenzelm@22362
    63
structure Thm: THM =
wenzelm@22362
    64
struct
wenzelm@22362
    65
wenzelm@22695
    66
(** basic operations **)
wenzelm@22362
    67
wenzelm@23491
    68
(* collecting cterms *)
wenzelm@23491
    69
wenzelm@23491
    70
val op aconvc = op aconv o pairself Thm.term_of;
wenzelm@23491
    71
wenzelm@23491
    72
fun add_cterm_frees ct =
wenzelm@23491
    73
  let
wenzelm@23491
    74
    val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
wenzelm@23491
    75
    val t = Thm.term_of ct;
wenzelm@23491
    76
  in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
wenzelm@23491
    77
wenzelm@23491
    78
wenzelm@22907
    79
(* cterm constructors and destructors *)
wenzelm@22907
    80
wenzelm@22907
    81
fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
wenzelm@22907
    82
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
    83
wenzelm@22907
    84
fun dest_implies ct =
wenzelm@22907
    85
  (case Thm.term_of ct of
wenzelm@22907
    86
    Const ("==>", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    87
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
    88
wenzelm@22907
    89
fun dest_equals ct =
wenzelm@22907
    90
  (case Thm.term_of ct of
wenzelm@22907
    91
    Const ("==", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    92
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
    93
wenzelm@22907
    94
fun dest_equals_lhs ct =
wenzelm@22907
    95
  (case Thm.term_of ct of
wenzelm@22907
    96
    Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
    97
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
    98
wenzelm@22907
    99
fun dest_equals_rhs ct =
wenzelm@22907
   100
  (case Thm.term_of ct of
wenzelm@22907
   101
    Const ("==", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
   102
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
   103
wenzelm@22907
   104
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
   105
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
   106
wenzelm@22907
   107
wenzelm@22907
   108
(* thm order: ignores theory context! *)
wenzelm@22682
   109
wenzelm@22362
   110
fun thm_ord (th1, th2) =
wenzelm@22362
   111
  let
wenzelm@22362
   112
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
   113
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
   114
  in
wenzelm@22362
   115
    (case Term.fast_term_ord (prop1, prop2) of
wenzelm@22362
   116
      EQUAL =>
wenzelm@22362
   117
        (case list_ord (prod_ord Term.fast_term_ord Term.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
   118
          EQUAL =>
wenzelm@22362
   119
            (case list_ord Term.fast_term_ord (hyps1, hyps2) of
wenzelm@22362
   120
              EQUAL => list_ord Term.sort_ord (shyps1, shyps2)
wenzelm@22362
   121
            | ord => ord)
wenzelm@22362
   122
        | ord => ord)
wenzelm@22362
   123
    | ord => ord)
wenzelm@22362
   124
  end;
wenzelm@22362
   125
wenzelm@22682
   126
wenzelm@22682
   127
(* equality *)
wenzelm@22682
   128
wenzelm@23599
   129
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   130
  handle TERM _ => false;
wenzelm@23599
   131
wenzelm@22362
   132
fun eq_thm ths =
wenzelm@22362
   133
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@26653
   134
  is_equal (thm_ord ths);
wenzelm@22362
   135
wenzelm@22362
   136
val eq_thms = eq_list eq_thm;
wenzelm@22362
   137
wenzelm@22362
   138
val eq_thm_thy = eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
   139
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
   140
wenzelm@22682
   141
wenzelm@22682
   142
(* pattern equivalence *)
wenzelm@22682
   143
wenzelm@22362
   144
fun equiv_thm ths =
wenzelm@22362
   145
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
   146
wenzelm@22362
   147
wenzelm@22695
   148
(* misc operations *)
wenzelm@22695
   149
wenzelm@24048
   150
fun is_dummy thm =
wenzelm@24048
   151
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   152
    NONE => false
wenzelm@24048
   153
  | SOME t => Term.is_dummy_pattern t);
wenzelm@24048
   154
wenzelm@22695
   155
fun plain_prop_of raw_thm =
wenzelm@22695
   156
  let
wenzelm@22695
   157
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   158
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   159
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
   160
  in
wenzelm@22695
   161
    if not (null hyps) then
wenzelm@22695
   162
      err "theorem may not contain hypotheses"
wenzelm@22695
   163
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   164
      err "theorem may not contain sort hypotheses"
wenzelm@22695
   165
    else if not (null tpairs) then
wenzelm@22695
   166
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
   167
    else prop
wenzelm@22695
   168
  end;
wenzelm@22695
   169
wenzelm@22695
   170
fun fold_terms f th =
wenzelm@22695
   171
  let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
wenzelm@22695
   172
  in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
wenzelm@22695
   173
wenzelm@22695
   174
wenzelm@24048
   175
(* lists of theorems in canonical order *)
wenzelm@24048
   176
wenzelm@24048
   177
val add_thm = update eq_thm_prop;
wenzelm@24048
   178
val del_thm = remove eq_thm_prop;
wenzelm@24048
   179
val merge_thms = merge eq_thm_prop;
wenzelm@24048
   180
wenzelm@24048
   181
wenzelm@22682
   182
wenzelm@22682
   183
(** theorem kinds **)
wenzelm@22362
   184
wenzelm@22362
   185
val axiomK = "axiom";
wenzelm@22362
   186
val assumptionK = "assumption";
wenzelm@22362
   187
val definitionK = "definition";
wenzelm@22362
   188
val theoremK = "theorem";
wenzelm@22362
   189
val lemmaK = "lemma";
wenzelm@22362
   190
val corollaryK = "corollary";
wenzelm@24775
   191
val internalK = Markup.internalK;
wenzelm@22362
   192
wenzelm@22362
   193
wenzelm@22682
   194
wenzelm@22682
   195
(** attributes **)
wenzelm@22362
   196
wenzelm@22362
   197
fun rule_attribute f (x, th) = (x, f x th);
wenzelm@22362
   198
fun declaration_attribute f (x, th) = (f th x, th);
wenzelm@22362
   199
wenzelm@22362
   200
fun apply_attributes mk dest =
wenzelm@22362
   201
  let
wenzelm@22362
   202
    fun app [] = I
wenzelm@22362
   203
      | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
wenzelm@22362
   204
  in app end;
wenzelm@22362
   205
wenzelm@22362
   206
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@22362
   207
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@22362
   208
wenzelm@22362
   209
fun no_attributes x = (x, []);
wenzelm@22362
   210
fun simple_fact x = [(x, [])];
wenzelm@22362
   211
wenzelm@22362
   212
wenzelm@22682
   213
(** read/certify terms (obsolete) **)    (*exception ERROR*)
wenzelm@22682
   214
wenzelm@22682
   215
fun read_def_cterms (thy, types, sorts) used freeze sTs =
wenzelm@22682
   216
  let
wenzelm@22682
   217
    val (ts', tye) = Sign.read_def_terms (thy, types, sorts) used freeze sTs;
wenzelm@22682
   218
    val cts = map (Thm.cterm_of thy) ts'
wenzelm@22682
   219
      handle TYPE (msg, _, _) => error msg
wenzelm@22682
   220
           | TERM (msg, _) => error msg;
wenzelm@22682
   221
  in (cts, tye) end;
wenzelm@22682
   222
wenzelm@22682
   223
fun read_cterm thy sT =
wenzelm@22682
   224
  let val ([ct], _) = read_def_cterms (thy, K NONE, K NONE) [] true [sT]
wenzelm@22682
   225
  in ct end;
wenzelm@22682
   226
wenzelm@22682
   227
wenzelm@24980
   228
wenzelm@24980
   229
(** basic derived rules **)
wenzelm@24980
   230
wenzelm@24980
   231
(*Elimination of implication
wenzelm@24980
   232
  A    A ==> B
wenzelm@24980
   233
  ------------
wenzelm@24980
   234
        B
wenzelm@24980
   235
*)
wenzelm@24980
   236
fun elim_implies thA thAB = Thm.implies_elim thAB thA;
wenzelm@24980
   237
wenzelm@26653
   238
wenzelm@26653
   239
(* forall_elim_var(s) *)
wenzelm@26653
   240
wenzelm@26653
   241
local
wenzelm@26653
   242
wenzelm@26653
   243
fun forall_elim_vars_aux strip_vars i th =
wenzelm@26653
   244
  let
wenzelm@26653
   245
    val thy = Thm.theory_of_thm th;
wenzelm@26653
   246
    val {tpairs, prop, ...} = Thm.rep_thm th;
wenzelm@26653
   247
    val add_used = Term.fold_aterms
wenzelm@26653
   248
      (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
wenzelm@26653
   249
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@26653
   250
    val vars = strip_vars prop;
wenzelm@26653
   251
    val cvars = (Name.variant_list used (map #1 vars), vars)
wenzelm@26653
   252
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@26653
   253
  in fold Thm.forall_elim cvars th end;
wenzelm@26653
   254
wenzelm@26653
   255
in
wenzelm@26653
   256
wenzelm@26653
   257
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@26653
   258
wenzelm@26653
   259
fun forall_elim_var i th = forall_elim_vars_aux
wenzelm@26653
   260
  (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@26653
   261
  | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@26653
   262
wenzelm@26653
   263
end;
wenzelm@26653
   264
wenzelm@26653
   265
wenzelm@26653
   266
(* unvarify: global schematic variables *)
wenzelm@26653
   267
wenzelm@24980
   268
fun unvarify th =
wenzelm@24980
   269
  let
wenzelm@24980
   270
    val thy = Thm.theory_of_thm th;
wenzelm@24980
   271
    val cert = Thm.cterm_of thy;
wenzelm@24980
   272
    val certT = Thm.ctyp_of thy;
wenzelm@24980
   273
wenzelm@24980
   274
    val prop = Thm.full_prop_of th;
wenzelm@24980
   275
    val _ = map Logic.unvarify (prop :: Thm.hyps_of th)
wenzelm@24980
   276
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@24980
   277
wenzelm@24980
   278
    val instT0 = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
wenzelm@24980
   279
    val instT = map (fn (v, T) => (certT (TVar v), certT T)) instT0;
wenzelm@24980
   280
    val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
wenzelm@24980
   281
      let val T' = TermSubst.instantiateT instT0 T
wenzelm@24980
   282
      in (cert (Var ((a, i), T')), cert (Free ((a, T')))) end);
wenzelm@24980
   283
  in Thm.instantiate (instT, inst) th end;
wenzelm@24980
   284
wenzelm@26653
   285
wenzelm@26653
   286
(* close_derivation *)
wenzelm@26653
   287
wenzelm@26628
   288
fun close_derivation thm =
wenzelm@26628
   289
  if Thm.get_name thm = "" then Thm.put_name "" thm
wenzelm@26628
   290
  else thm;
wenzelm@26628
   291
wenzelm@24980
   292
wenzelm@24980
   293
wenzelm@24980
   294
(** specification primitives **)
wenzelm@24980
   295
wenzelm@24980
   296
fun add_axiom hyps (name, prop) thy =
wenzelm@24980
   297
  let
wenzelm@24980
   298
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@24980
   299
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@24980
   300
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@24980
   301
    val axm = unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@24980
   302
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@24980
   303
    val thm = fold elim_implies prems axm;
wenzelm@24980
   304
  in (thm, thy') end;
wenzelm@24980
   305
haftmann@25518
   306
fun add_def unchecked overloaded (name, prop) thy =
wenzelm@24980
   307
  let
wenzelm@24980
   308
    val tfrees = rev (map TFree (Term.add_tfrees prop []));
wenzelm@24980
   309
    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
wenzelm@24980
   310
    val strip_sorts = tfrees ~~ tfrees';
wenzelm@24980
   311
    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
wenzelm@24980
   312
wenzelm@24980
   313
    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
haftmann@25518
   314
    val thy' = Theory.add_defs_i unchecked overloaded [(name, prop')] thy;
wenzelm@24980
   315
    val axm' = Thm.get_axiom_i thy' (Sign.full_name thy' name);
wenzelm@24980
   316
    val thm = unvarify (Thm.instantiate (recover_sorts, []) axm');
wenzelm@24980
   317
  in (thm, thy') end;
wenzelm@24980
   318
wenzelm@22362
   319
open Thm;
wenzelm@22362
   320
wenzelm@22362
   321
end;
wenzelm@22362
   322
wenzelm@23170
   323
val op aconvc = Thm.aconvc;
wenzelm@23170
   324
wenzelm@22362
   325
structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);