src/Pure/more_thm.ML
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 24775 4f86d3384111
child 24948 c12c16a680a0
permissions -rw-r--r--
Name.uu, Name.aT;
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@23491
    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@22362
    54
end;
wenzelm@22362
    55
wenzelm@22362
    56
structure Thm: THM =
wenzelm@22362
    57
struct
wenzelm@22362
    58
wenzelm@22695
    59
(** basic operations **)
wenzelm@22362
    60
wenzelm@23491
    61
(* collecting cterms *)
wenzelm@23491
    62
wenzelm@23491
    63
val op aconvc = op aconv o pairself Thm.term_of;
wenzelm@23491
    64
wenzelm@23491
    65
fun add_cterm_frees ct =
wenzelm@23491
    66
  let
wenzelm@23491
    67
    val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
wenzelm@23491
    68
    val t = Thm.term_of ct;
wenzelm@23491
    69
  in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
wenzelm@23491
    70
wenzelm@23491
    71
wenzelm@22907
    72
(* cterm constructors and destructors *)
wenzelm@22907
    73
wenzelm@22907
    74
fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
wenzelm@22907
    75
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
    76
wenzelm@22907
    77
fun dest_implies ct =
wenzelm@22907
    78
  (case Thm.term_of ct of
wenzelm@22907
    79
    Const ("==>", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    80
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
    81
wenzelm@22907
    82
fun dest_equals ct =
wenzelm@22907
    83
  (case Thm.term_of ct of
wenzelm@22907
    84
    Const ("==", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    85
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
    86
wenzelm@22907
    87
fun dest_equals_lhs ct =
wenzelm@22907
    88
  (case Thm.term_of ct of
wenzelm@22907
    89
    Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
    90
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
    91
wenzelm@22907
    92
fun dest_equals_rhs ct =
wenzelm@22907
    93
  (case Thm.term_of ct of
wenzelm@22907
    94
    Const ("==", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
    95
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
    96
wenzelm@22907
    97
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
    98
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
    99
wenzelm@22907
   100
wenzelm@22907
   101
(* thm order: ignores theory context! *)
wenzelm@22682
   102
wenzelm@22362
   103
fun thm_ord (th1, th2) =
wenzelm@22362
   104
  let
wenzelm@22362
   105
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
   106
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
   107
  in
wenzelm@22362
   108
    (case Term.fast_term_ord (prop1, prop2) of
wenzelm@22362
   109
      EQUAL =>
wenzelm@22362
   110
        (case list_ord (prod_ord Term.fast_term_ord Term.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
   111
          EQUAL =>
wenzelm@22362
   112
            (case list_ord Term.fast_term_ord (hyps1, hyps2) of
wenzelm@22362
   113
              EQUAL => list_ord Term.sort_ord (shyps1, shyps2)
wenzelm@22362
   114
            | ord => ord)
wenzelm@22362
   115
        | ord => ord)
wenzelm@22362
   116
    | ord => ord)
wenzelm@22362
   117
  end;
wenzelm@22362
   118
wenzelm@22682
   119
wenzelm@22682
   120
(* equality *)
wenzelm@22682
   121
wenzelm@23599
   122
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   123
  handle TERM _ => false;
wenzelm@23599
   124
wenzelm@22362
   125
fun eq_thm ths =
wenzelm@22362
   126
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@22362
   127
  thm_ord ths = EQUAL;
wenzelm@22362
   128
wenzelm@22362
   129
val eq_thms = eq_list eq_thm;
wenzelm@22362
   130
wenzelm@22362
   131
val eq_thm_thy = eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
   132
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
   133
wenzelm@22682
   134
wenzelm@22682
   135
(* pattern equivalence *)
wenzelm@22682
   136
wenzelm@22362
   137
fun equiv_thm ths =
wenzelm@22362
   138
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
   139
wenzelm@22362
   140
wenzelm@22695
   141
(* misc operations *)
wenzelm@22695
   142
wenzelm@24048
   143
fun is_dummy thm =
wenzelm@24048
   144
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   145
    NONE => false
wenzelm@24048
   146
  | SOME t => Term.is_dummy_pattern t);
wenzelm@24048
   147
wenzelm@22695
   148
fun plain_prop_of raw_thm =
wenzelm@22695
   149
  let
wenzelm@22695
   150
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   151
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   152
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
   153
  in
wenzelm@22695
   154
    if not (null hyps) then
wenzelm@22695
   155
      err "theorem may not contain hypotheses"
wenzelm@22695
   156
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   157
      err "theorem may not contain sort hypotheses"
wenzelm@22695
   158
    else if not (null tpairs) then
wenzelm@22695
   159
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
   160
    else prop
wenzelm@22695
   161
  end;
wenzelm@22695
   162
wenzelm@22695
   163
fun fold_terms f th =
wenzelm@22695
   164
  let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
wenzelm@22695
   165
  in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
wenzelm@22695
   166
wenzelm@22695
   167
wenzelm@24048
   168
(* lists of theorems in canonical order *)
wenzelm@24048
   169
wenzelm@24048
   170
val add_thm = update eq_thm_prop;
wenzelm@24048
   171
val del_thm = remove eq_thm_prop;
wenzelm@24048
   172
val merge_thms = merge eq_thm_prop;
wenzelm@24048
   173
wenzelm@24048
   174
wenzelm@22682
   175
wenzelm@22682
   176
(** theorem kinds **)
wenzelm@22362
   177
wenzelm@22362
   178
val axiomK = "axiom";
wenzelm@22362
   179
val assumptionK = "assumption";
wenzelm@22362
   180
val definitionK = "definition";
wenzelm@22362
   181
val theoremK = "theorem";
wenzelm@22362
   182
val lemmaK = "lemma";
wenzelm@22362
   183
val corollaryK = "corollary";
wenzelm@24775
   184
val internalK = Markup.internalK;
wenzelm@22362
   185
wenzelm@22362
   186
wenzelm@22682
   187
wenzelm@22682
   188
(** attributes **)
wenzelm@22362
   189
wenzelm@22362
   190
fun rule_attribute f (x, th) = (x, f x th);
wenzelm@22362
   191
fun declaration_attribute f (x, th) = (f th x, th);
wenzelm@22362
   192
wenzelm@22362
   193
fun apply_attributes mk dest =
wenzelm@22362
   194
  let
wenzelm@22362
   195
    fun app [] = I
wenzelm@22362
   196
      | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
wenzelm@22362
   197
  in app end;
wenzelm@22362
   198
wenzelm@22362
   199
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@22362
   200
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@22362
   201
wenzelm@22362
   202
fun no_attributes x = (x, []);
wenzelm@22362
   203
fun simple_fact x = [(x, [])];
wenzelm@22362
   204
wenzelm@22362
   205
wenzelm@22682
   206
(** read/certify terms (obsolete) **)    (*exception ERROR*)
wenzelm@22682
   207
wenzelm@22682
   208
fun read_def_cterms (thy, types, sorts) used freeze sTs =
wenzelm@22682
   209
  let
wenzelm@22682
   210
    val (ts', tye) = Sign.read_def_terms (thy, types, sorts) used freeze sTs;
wenzelm@22682
   211
    val cts = map (Thm.cterm_of thy) ts'
wenzelm@22682
   212
      handle TYPE (msg, _, _) => error msg
wenzelm@22682
   213
           | TERM (msg, _) => error msg;
wenzelm@22682
   214
  in (cts, tye) end;
wenzelm@22682
   215
wenzelm@22682
   216
fun read_cterm thy sT =
wenzelm@22682
   217
  let val ([ct], _) = read_def_cterms (thy, K NONE, K NONE) [] true [sT]
wenzelm@22682
   218
  in ct end;
wenzelm@22682
   219
wenzelm@22682
   220
wenzelm@22362
   221
open Thm;
wenzelm@22362
   222
wenzelm@22362
   223
end;
wenzelm@22362
   224
wenzelm@23170
   225
val op aconvc = Thm.aconvc;
wenzelm@23170
   226
wenzelm@22362
   227
structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);