src/Pure/more_thm.ML
author wenzelm
Thu May 31 19:11:19 2007 +0200 (2007-05-31)
changeset 23170 94e9413bd7fc
parent 23169 37091da05d8e
child 23491 c13ca04303de
permissions -rw-r--r--
made aconvc pervasive;
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@22907
    13
  val mk_binop: cterm -> cterm -> cterm -> cterm
wenzelm@22907
    14
  val dest_binop: cterm -> cterm * cterm
wenzelm@22907
    15
  val dest_implies: cterm -> cterm * cterm
wenzelm@22907
    16
  val dest_equals: cterm -> cterm * cterm
wenzelm@22907
    17
  val dest_equals_lhs: cterm -> cterm
wenzelm@22907
    18
  val dest_equals_rhs: cterm -> cterm
wenzelm@22907
    19
  val lhs_of: thm -> cterm
wenzelm@22907
    20
  val rhs_of: thm -> cterm
wenzelm@22362
    21
  val thm_ord: thm * thm -> order
wenzelm@23169
    22
  val aconvc : cterm * cterm -> bool
wenzelm@22362
    23
  val eq_thm: thm * thm -> bool
wenzelm@22362
    24
  val eq_thms: thm list * thm list -> bool
wenzelm@22362
    25
  val eq_thm_thy: thm * thm -> bool
wenzelm@22362
    26
  val eq_thm_prop: thm * thm -> bool
wenzelm@22362
    27
  val equiv_thm: thm * thm -> bool
wenzelm@22695
    28
  val plain_prop_of: thm -> term
wenzelm@22695
    29
  val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@22362
    30
  val axiomK: string
wenzelm@22362
    31
  val assumptionK: string
wenzelm@22362
    32
  val definitionK: string
wenzelm@22362
    33
  val theoremK: string
wenzelm@22362
    34
  val lemmaK: string
wenzelm@22362
    35
  val corollaryK: string
wenzelm@22362
    36
  val internalK: string
wenzelm@22362
    37
  val rule_attribute: (Context.generic -> thm -> thm) -> attribute
wenzelm@22362
    38
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@22362
    39
  val theory_attributes: attribute list -> theory * thm -> theory * thm
wenzelm@22362
    40
  val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
wenzelm@22362
    41
  val no_attributes: 'a -> 'a * 'b list
wenzelm@22362
    42
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@22682
    43
  val read_def_cterms:
wenzelm@22682
    44
    theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@22682
    45
    string list -> bool -> (string * typ)list
wenzelm@22682
    46
    -> cterm list * (indexname * typ)list
wenzelm@22682
    47
  val read_cterm: theory -> string * typ -> cterm
wenzelm@22362
    48
end;
wenzelm@22362
    49
wenzelm@22362
    50
structure Thm: THM =
wenzelm@22362
    51
struct
wenzelm@22362
    52
wenzelm@22695
    53
(** basic operations **)
wenzelm@22362
    54
wenzelm@22907
    55
(* cterm constructors and destructors *)
wenzelm@22907
    56
wenzelm@22907
    57
fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
wenzelm@22907
    58
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
    59
wenzelm@22907
    60
fun dest_implies ct =
wenzelm@22907
    61
  (case Thm.term_of ct of
wenzelm@22907
    62
    Const ("==>", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    63
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
    64
wenzelm@22907
    65
fun dest_equals ct =
wenzelm@22907
    66
  (case Thm.term_of ct of
wenzelm@22907
    67
    Const ("==", _) $ _ $ _ => dest_binop ct
wenzelm@22907
    68
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
    69
wenzelm@22907
    70
fun dest_equals_lhs ct =
wenzelm@22907
    71
  (case Thm.term_of ct of
wenzelm@22907
    72
    Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
    73
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
    74
wenzelm@22907
    75
fun dest_equals_rhs ct =
wenzelm@22907
    76
  (case Thm.term_of ct of
wenzelm@22907
    77
    Const ("==", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
    78
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
    79
wenzelm@22907
    80
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
    81
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
    82
wenzelm@22907
    83
wenzelm@22907
    84
(* thm order: ignores theory context! *)
wenzelm@22682
    85
wenzelm@22362
    86
fun thm_ord (th1, th2) =
wenzelm@22362
    87
  let
wenzelm@22362
    88
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
    89
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
    90
  in
wenzelm@22362
    91
    (case Term.fast_term_ord (prop1, prop2) of
wenzelm@22362
    92
      EQUAL =>
wenzelm@22362
    93
        (case list_ord (prod_ord Term.fast_term_ord Term.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
    94
          EQUAL =>
wenzelm@22362
    95
            (case list_ord Term.fast_term_ord (hyps1, hyps2) of
wenzelm@22362
    96
              EQUAL => list_ord Term.sort_ord (shyps1, shyps2)
wenzelm@22362
    97
            | ord => ord)
wenzelm@22362
    98
        | ord => ord)
wenzelm@22362
    99
    | ord => ord)
wenzelm@22362
   100
  end;
wenzelm@22362
   101
wenzelm@22682
   102
wenzelm@22682
   103
(* equality *)
wenzelm@22682
   104
wenzelm@23169
   105
val op aconvc = op aconv o pairself Thm.term_of;
wenzelm@23169
   106
wenzelm@22362
   107
fun eq_thm ths =
wenzelm@22362
   108
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@22362
   109
  thm_ord ths = EQUAL;
wenzelm@22362
   110
wenzelm@22362
   111
val eq_thms = eq_list eq_thm;
wenzelm@22362
   112
wenzelm@22362
   113
val eq_thm_thy = eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
   114
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
   115
wenzelm@22682
   116
wenzelm@22682
   117
(* pattern equivalence *)
wenzelm@22682
   118
wenzelm@22362
   119
fun equiv_thm ths =
wenzelm@22362
   120
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
   121
wenzelm@22362
   122
wenzelm@22695
   123
(* misc operations *)
wenzelm@22695
   124
wenzelm@22695
   125
fun plain_prop_of raw_thm =
wenzelm@22695
   126
  let
wenzelm@22695
   127
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   128
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   129
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
   130
  in
wenzelm@22695
   131
    if not (null hyps) then
wenzelm@22695
   132
      err "theorem may not contain hypotheses"
wenzelm@22695
   133
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   134
      err "theorem may not contain sort hypotheses"
wenzelm@22695
   135
    else if not (null tpairs) then
wenzelm@22695
   136
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
   137
    else prop
wenzelm@22695
   138
  end;
wenzelm@22695
   139
wenzelm@22695
   140
fun fold_terms f th =
wenzelm@22695
   141
  let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
wenzelm@22695
   142
  in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
wenzelm@22695
   143
wenzelm@22695
   144
wenzelm@22682
   145
wenzelm@22682
   146
(** theorem kinds **)
wenzelm@22362
   147
wenzelm@22362
   148
val axiomK = "axiom";
wenzelm@22362
   149
val assumptionK = "assumption";
wenzelm@22362
   150
val definitionK = "definition";
wenzelm@22362
   151
val theoremK = "theorem";
wenzelm@22362
   152
val lemmaK = "lemma";
wenzelm@22362
   153
val corollaryK = "corollary";
wenzelm@22362
   154
val internalK = "internal";
wenzelm@22362
   155
wenzelm@22362
   156
wenzelm@22682
   157
wenzelm@22682
   158
(** attributes **)
wenzelm@22362
   159
wenzelm@22362
   160
fun rule_attribute f (x, th) = (x, f x th);
wenzelm@22362
   161
fun declaration_attribute f (x, th) = (f th x, th);
wenzelm@22362
   162
wenzelm@22362
   163
fun apply_attributes mk dest =
wenzelm@22362
   164
  let
wenzelm@22362
   165
    fun app [] = I
wenzelm@22362
   166
      | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
wenzelm@22362
   167
  in app end;
wenzelm@22362
   168
wenzelm@22362
   169
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@22362
   170
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@22362
   171
wenzelm@22362
   172
fun no_attributes x = (x, []);
wenzelm@22362
   173
fun simple_fact x = [(x, [])];
wenzelm@22362
   174
wenzelm@22362
   175
wenzelm@22682
   176
(** read/certify terms (obsolete) **)    (*exception ERROR*)
wenzelm@22682
   177
wenzelm@22682
   178
fun read_def_cterms (thy, types, sorts) used freeze sTs =
wenzelm@22682
   179
  let
wenzelm@22682
   180
    val (ts', tye) = Sign.read_def_terms (thy, types, sorts) used freeze sTs;
wenzelm@22682
   181
    val cts = map (Thm.cterm_of thy) ts'
wenzelm@22682
   182
      handle TYPE (msg, _, _) => error msg
wenzelm@22682
   183
           | TERM (msg, _) => error msg;
wenzelm@22682
   184
  in (cts, tye) end;
wenzelm@22682
   185
wenzelm@22682
   186
fun read_cterm thy sT =
wenzelm@22682
   187
  let val ([ct], _) = read_def_cterms (thy, K NONE, K NONE) [] true [sT]
wenzelm@22682
   188
  in ct end;
wenzelm@22682
   189
wenzelm@22682
   190
wenzelm@22362
   191
open Thm;
wenzelm@22362
   192
wenzelm@22362
   193
end;
wenzelm@22362
   194
wenzelm@23170
   195
val op aconvc = Thm.aconvc;
wenzelm@23170
   196
wenzelm@22362
   197
structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);