src/Pure/more_thm.ML
author wenzelm
Sat Mar 15 18:08:00 2008 +0100 (2008-03-15)
changeset 26279 e8440c90c474
parent 25518 00d5cc16e891
child 26628 63306cb94313
permissions -rw-r--r--
removed obsolete fact_index.ML;
added facts.ML;
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@24980
    55
  val unvarify: thm -> thm
wenzelm@24980
    56
  val add_axiom: term list -> bstring * term -> theory -> thm * theory
haftmann@25518
    57
  val add_def: bool -> bool -> bstring * term -> theory -> thm * theory
wenzelm@22362
    58
end;
wenzelm@22362
    59
wenzelm@22362
    60
structure Thm: THM =
wenzelm@22362
    61
struct
wenzelm@22362
    62
wenzelm@22695
    63
(** basic operations **)
wenzelm@22362
    64
wenzelm@23491
    65
(* collecting cterms *)
wenzelm@23491
    66
wenzelm@23491
    67
val op aconvc = op aconv o pairself Thm.term_of;
wenzelm@23491
    68
wenzelm@23491
    69
fun add_cterm_frees ct =
wenzelm@23491
    70
  let
wenzelm@23491
    71
    val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
wenzelm@23491
    72
    val t = Thm.term_of ct;
wenzelm@23491
    73
  in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
wenzelm@23491
    74
wenzelm@23491
    75
wenzelm@22907
    76
(* cterm constructors and destructors *)
wenzelm@22907
    77
wenzelm@22907
    78
fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
wenzelm@22907
    79
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
    80
wenzelm@22907
    81
fun dest_implies ct =
wenzelm@22907
    82
  (case Thm.term_of ct of
wenzelm@22907
    83
    Const ("==>", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    84
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
    85
wenzelm@22907
    86
fun dest_equals ct =
wenzelm@22907
    87
  (case Thm.term_of ct of
wenzelm@22907
    88
    Const ("==", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    89
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
    90
wenzelm@22907
    91
fun dest_equals_lhs ct =
wenzelm@22907
    92
  (case Thm.term_of ct of
wenzelm@22907
    93
    Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
    94
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
    95
wenzelm@22907
    96
fun dest_equals_rhs ct =
wenzelm@22907
    97
  (case Thm.term_of ct of
wenzelm@22907
    98
    Const ("==", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
    99
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
   100
wenzelm@22907
   101
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
   102
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
   103
wenzelm@22907
   104
wenzelm@22907
   105
(* thm order: ignores theory context! *)
wenzelm@22682
   106
wenzelm@22362
   107
fun thm_ord (th1, th2) =
wenzelm@22362
   108
  let
wenzelm@22362
   109
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
   110
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
   111
  in
wenzelm@22362
   112
    (case Term.fast_term_ord (prop1, prop2) of
wenzelm@22362
   113
      EQUAL =>
wenzelm@22362
   114
        (case list_ord (prod_ord Term.fast_term_ord Term.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
   115
          EQUAL =>
wenzelm@22362
   116
            (case list_ord Term.fast_term_ord (hyps1, hyps2) of
wenzelm@22362
   117
              EQUAL => list_ord Term.sort_ord (shyps1, shyps2)
wenzelm@22362
   118
            | ord => ord)
wenzelm@22362
   119
        | ord => ord)
wenzelm@22362
   120
    | ord => ord)
wenzelm@22362
   121
  end;
wenzelm@22362
   122
wenzelm@22682
   123
wenzelm@22682
   124
(* equality *)
wenzelm@22682
   125
wenzelm@23599
   126
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   127
  handle TERM _ => false;
wenzelm@23599
   128
wenzelm@22362
   129
fun eq_thm ths =
wenzelm@22362
   130
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@22362
   131
  thm_ord ths = EQUAL;
wenzelm@22362
   132
wenzelm@22362
   133
val eq_thms = eq_list eq_thm;
wenzelm@22362
   134
wenzelm@22362
   135
val eq_thm_thy = eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
   136
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
   137
wenzelm@22682
   138
wenzelm@22682
   139
(* pattern equivalence *)
wenzelm@22682
   140
wenzelm@22362
   141
fun equiv_thm ths =
wenzelm@22362
   142
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
   143
wenzelm@22362
   144
wenzelm@22695
   145
(* misc operations *)
wenzelm@22695
   146
wenzelm@24048
   147
fun is_dummy thm =
wenzelm@24048
   148
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   149
    NONE => false
wenzelm@24048
   150
  | SOME t => Term.is_dummy_pattern t);
wenzelm@24048
   151
wenzelm@22695
   152
fun plain_prop_of raw_thm =
wenzelm@22695
   153
  let
wenzelm@22695
   154
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   155
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   156
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
   157
  in
wenzelm@22695
   158
    if not (null hyps) then
wenzelm@22695
   159
      err "theorem may not contain hypotheses"
wenzelm@22695
   160
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   161
      err "theorem may not contain sort hypotheses"
wenzelm@22695
   162
    else if not (null tpairs) then
wenzelm@22695
   163
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
   164
    else prop
wenzelm@22695
   165
  end;
wenzelm@22695
   166
wenzelm@22695
   167
fun fold_terms f th =
wenzelm@22695
   168
  let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
wenzelm@22695
   169
  in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
wenzelm@22695
   170
wenzelm@22695
   171
wenzelm@24048
   172
(* lists of theorems in canonical order *)
wenzelm@24048
   173
wenzelm@24048
   174
val add_thm = update eq_thm_prop;
wenzelm@24048
   175
val del_thm = remove eq_thm_prop;
wenzelm@24048
   176
val merge_thms = merge eq_thm_prop;
wenzelm@24048
   177
wenzelm@24048
   178
wenzelm@22682
   179
wenzelm@22682
   180
(** theorem kinds **)
wenzelm@22362
   181
wenzelm@22362
   182
val axiomK = "axiom";
wenzelm@22362
   183
val assumptionK = "assumption";
wenzelm@22362
   184
val definitionK = "definition";
wenzelm@22362
   185
val theoremK = "theorem";
wenzelm@22362
   186
val lemmaK = "lemma";
wenzelm@22362
   187
val corollaryK = "corollary";
wenzelm@24775
   188
val internalK = Markup.internalK;
wenzelm@22362
   189
wenzelm@22362
   190
wenzelm@22682
   191
wenzelm@22682
   192
(** attributes **)
wenzelm@22362
   193
wenzelm@22362
   194
fun rule_attribute f (x, th) = (x, f x th);
wenzelm@22362
   195
fun declaration_attribute f (x, th) = (f th x, th);
wenzelm@22362
   196
wenzelm@22362
   197
fun apply_attributes mk dest =
wenzelm@22362
   198
  let
wenzelm@22362
   199
    fun app [] = I
wenzelm@22362
   200
      | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
wenzelm@22362
   201
  in app end;
wenzelm@22362
   202
wenzelm@22362
   203
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@22362
   204
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@22362
   205
wenzelm@22362
   206
fun no_attributes x = (x, []);
wenzelm@22362
   207
fun simple_fact x = [(x, [])];
wenzelm@22362
   208
wenzelm@22362
   209
wenzelm@22682
   210
(** read/certify terms (obsolete) **)    (*exception ERROR*)
wenzelm@22682
   211
wenzelm@22682
   212
fun read_def_cterms (thy, types, sorts) used freeze sTs =
wenzelm@22682
   213
  let
wenzelm@22682
   214
    val (ts', tye) = Sign.read_def_terms (thy, types, sorts) used freeze sTs;
wenzelm@22682
   215
    val cts = map (Thm.cterm_of thy) ts'
wenzelm@22682
   216
      handle TYPE (msg, _, _) => error msg
wenzelm@22682
   217
           | TERM (msg, _) => error msg;
wenzelm@22682
   218
  in (cts, tye) end;
wenzelm@22682
   219
wenzelm@22682
   220
fun read_cterm thy sT =
wenzelm@22682
   221
  let val ([ct], _) = read_def_cterms (thy, K NONE, K NONE) [] true [sT]
wenzelm@22682
   222
  in ct end;
wenzelm@22682
   223
wenzelm@22682
   224
wenzelm@24980
   225
wenzelm@24980
   226
(** basic derived rules **)
wenzelm@24980
   227
wenzelm@24980
   228
(*Elimination of implication
wenzelm@24980
   229
  A    A ==> B
wenzelm@24980
   230
  ------------
wenzelm@24980
   231
        B
wenzelm@24980
   232
*)
wenzelm@24980
   233
fun elim_implies thA thAB = Thm.implies_elim thAB thA;
wenzelm@24980
   234
wenzelm@24980
   235
(*global schematic variables*)
wenzelm@24980
   236
fun unvarify th =
wenzelm@24980
   237
  let
wenzelm@24980
   238
    val thy = Thm.theory_of_thm th;
wenzelm@24980
   239
    val cert = Thm.cterm_of thy;
wenzelm@24980
   240
    val certT = Thm.ctyp_of thy;
wenzelm@24980
   241
wenzelm@24980
   242
    val prop = Thm.full_prop_of th;
wenzelm@24980
   243
    val _ = map Logic.unvarify (prop :: Thm.hyps_of th)
wenzelm@24980
   244
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@24980
   245
wenzelm@24980
   246
    val instT0 = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
wenzelm@24980
   247
    val instT = map (fn (v, T) => (certT (TVar v), certT T)) instT0;
wenzelm@24980
   248
    val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
wenzelm@24980
   249
      let val T' = TermSubst.instantiateT instT0 T
wenzelm@24980
   250
      in (cert (Var ((a, i), T')), cert (Free ((a, T')))) end);
wenzelm@24980
   251
  in Thm.instantiate (instT, inst) th end;
wenzelm@24980
   252
wenzelm@24980
   253
wenzelm@24980
   254
wenzelm@24980
   255
(** specification primitives **)
wenzelm@24980
   256
wenzelm@24980
   257
fun add_axiom hyps (name, prop) thy =
wenzelm@24980
   258
  let
wenzelm@24980
   259
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@24980
   260
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@24980
   261
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@24980
   262
    val axm = unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@24980
   263
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@24980
   264
    val thm = fold elim_implies prems axm;
wenzelm@24980
   265
  in (thm, thy') end;
wenzelm@24980
   266
haftmann@25518
   267
fun add_def unchecked overloaded (name, prop) thy =
wenzelm@24980
   268
  let
wenzelm@24980
   269
    val tfrees = rev (map TFree (Term.add_tfrees prop []));
wenzelm@24980
   270
    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
wenzelm@24980
   271
    val strip_sorts = tfrees ~~ tfrees';
wenzelm@24980
   272
    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
wenzelm@24980
   273
wenzelm@24980
   274
    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
haftmann@25518
   275
    val thy' = Theory.add_defs_i unchecked overloaded [(name, prop')] thy;
wenzelm@24980
   276
    val axm' = Thm.get_axiom_i thy' (Sign.full_name thy' name);
wenzelm@24980
   277
    val thm = unvarify (Thm.instantiate (recover_sorts, []) axm');
wenzelm@24980
   278
  in (thm, thy') end;
wenzelm@24980
   279
wenzelm@22362
   280
open Thm;
wenzelm@22362
   281
wenzelm@22362
   282
end;
wenzelm@22362
   283
wenzelm@23170
   284
val op aconvc = Thm.aconvc;
wenzelm@23170
   285
wenzelm@22362
   286
structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);