src/Pure/more_thm.ML
author wenzelm
Sun Apr 15 14:31:53 2007 +0200 (2007-04-15)
changeset 22695 17073e9b94f2
parent 22682 92448396c9d9
child 22907 dccd0763ae37
permissions -rw-r--r--
moved Drule.plain_prop_of, Drule.fold_terms to more_thm.ML;
wenzelm@22362
     1
(*  Title:      Pure/more_thm.ML
wenzelm@22362
     2
    ID:         $Id$
wenzelm@22362
     3
    Author:     Makarius
wenzelm@22362
     4
wenzelm@22362
     5
Further operations on type thm, outside the inference kernel.
wenzelm@22362
     6
*)
wenzelm@22362
     7
wenzelm@22362
     8
signature THM =
wenzelm@22362
     9
sig
wenzelm@22362
    10
  include THM
wenzelm@22362
    11
  val thm_ord: thm * thm -> order
wenzelm@22362
    12
  val eq_thm: thm * thm -> bool
wenzelm@22362
    13
  val eq_thms: thm list * thm list -> bool
wenzelm@22362
    14
  val eq_thm_thy: thm * thm -> bool
wenzelm@22362
    15
  val eq_thm_prop: thm * thm -> bool
wenzelm@22362
    16
  val equiv_thm: thm * thm -> bool
wenzelm@22695
    17
  val plain_prop_of: thm -> term
wenzelm@22695
    18
  val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@22362
    19
  val axiomK: string
wenzelm@22362
    20
  val assumptionK: string
wenzelm@22362
    21
  val definitionK: string
wenzelm@22362
    22
  val theoremK: string
wenzelm@22362
    23
  val lemmaK: string
wenzelm@22362
    24
  val corollaryK: string
wenzelm@22362
    25
  val internalK: string
wenzelm@22362
    26
  val rule_attribute: (Context.generic -> thm -> thm) -> attribute
wenzelm@22362
    27
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@22362
    28
  val theory_attributes: attribute list -> theory * thm -> theory * thm
wenzelm@22362
    29
  val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
wenzelm@22362
    30
  val no_attributes: 'a -> 'a * 'b list
wenzelm@22362
    31
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@22682
    32
  val read_def_cterms:
wenzelm@22682
    33
    theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@22682
    34
    string list -> bool -> (string * typ)list
wenzelm@22682
    35
    -> cterm list * (indexname * typ)list
wenzelm@22682
    36
  val read_cterm: theory -> string * typ -> cterm
wenzelm@22362
    37
end;
wenzelm@22362
    38
wenzelm@22362
    39
structure Thm: THM =
wenzelm@22362
    40
struct
wenzelm@22362
    41
wenzelm@22695
    42
(** basic operations **)
wenzelm@22362
    43
wenzelm@22682
    44
(* order: ignores theory context! *)
wenzelm@22682
    45
wenzelm@22362
    46
fun thm_ord (th1, th2) =
wenzelm@22362
    47
  let
wenzelm@22362
    48
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
    49
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
    50
  in
wenzelm@22362
    51
    (case Term.fast_term_ord (prop1, prop2) of
wenzelm@22362
    52
      EQUAL =>
wenzelm@22362
    53
        (case list_ord (prod_ord Term.fast_term_ord Term.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
    54
          EQUAL =>
wenzelm@22362
    55
            (case list_ord Term.fast_term_ord (hyps1, hyps2) of
wenzelm@22362
    56
              EQUAL => list_ord Term.sort_ord (shyps1, shyps2)
wenzelm@22362
    57
            | ord => ord)
wenzelm@22362
    58
        | ord => ord)
wenzelm@22362
    59
    | ord => ord)
wenzelm@22362
    60
  end;
wenzelm@22362
    61
wenzelm@22682
    62
wenzelm@22682
    63
(* equality *)
wenzelm@22682
    64
wenzelm@22362
    65
fun eq_thm ths =
wenzelm@22362
    66
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@22362
    67
  thm_ord ths = EQUAL;
wenzelm@22362
    68
wenzelm@22362
    69
val eq_thms = eq_list eq_thm;
wenzelm@22362
    70
wenzelm@22362
    71
val eq_thm_thy = eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
    72
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
    73
wenzelm@22682
    74
wenzelm@22682
    75
(* pattern equivalence *)
wenzelm@22682
    76
wenzelm@22362
    77
fun equiv_thm ths =
wenzelm@22362
    78
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
    79
wenzelm@22362
    80
wenzelm@22695
    81
(* misc operations *)
wenzelm@22695
    82
wenzelm@22695
    83
fun plain_prop_of raw_thm =
wenzelm@22695
    84
  let
wenzelm@22695
    85
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
    86
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
    87
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
    88
  in
wenzelm@22695
    89
    if not (null hyps) then
wenzelm@22695
    90
      err "theorem may not contain hypotheses"
wenzelm@22695
    91
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
    92
      err "theorem may not contain sort hypotheses"
wenzelm@22695
    93
    else if not (null tpairs) then
wenzelm@22695
    94
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
    95
    else prop
wenzelm@22695
    96
  end;
wenzelm@22695
    97
wenzelm@22695
    98
fun fold_terms f th =
wenzelm@22695
    99
  let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
wenzelm@22695
   100
  in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
wenzelm@22695
   101
wenzelm@22695
   102
wenzelm@22682
   103
wenzelm@22682
   104
(** theorem kinds **)
wenzelm@22362
   105
wenzelm@22362
   106
val axiomK = "axiom";
wenzelm@22362
   107
val assumptionK = "assumption";
wenzelm@22362
   108
val definitionK = "definition";
wenzelm@22362
   109
val theoremK = "theorem";
wenzelm@22362
   110
val lemmaK = "lemma";
wenzelm@22362
   111
val corollaryK = "corollary";
wenzelm@22362
   112
val internalK = "internal";
wenzelm@22362
   113
wenzelm@22362
   114
wenzelm@22682
   115
wenzelm@22682
   116
(** attributes **)
wenzelm@22362
   117
wenzelm@22362
   118
fun rule_attribute f (x, th) = (x, f x th);
wenzelm@22362
   119
fun declaration_attribute f (x, th) = (f th x, th);
wenzelm@22362
   120
wenzelm@22362
   121
fun apply_attributes mk dest =
wenzelm@22362
   122
  let
wenzelm@22362
   123
    fun app [] = I
wenzelm@22362
   124
      | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
wenzelm@22362
   125
  in app end;
wenzelm@22362
   126
wenzelm@22362
   127
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@22362
   128
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@22362
   129
wenzelm@22362
   130
fun no_attributes x = (x, []);
wenzelm@22362
   131
fun simple_fact x = [(x, [])];
wenzelm@22362
   132
wenzelm@22362
   133
wenzelm@22682
   134
(** read/certify terms (obsolete) **)    (*exception ERROR*)
wenzelm@22682
   135
wenzelm@22682
   136
fun read_def_cterms (thy, types, sorts) used freeze sTs =
wenzelm@22682
   137
  let
wenzelm@22682
   138
    val (ts', tye) = Sign.read_def_terms (thy, types, sorts) used freeze sTs;
wenzelm@22682
   139
    val cts = map (Thm.cterm_of thy) ts'
wenzelm@22682
   140
      handle TYPE (msg, _, _) => error msg
wenzelm@22682
   141
           | TERM (msg, _) => error msg;
wenzelm@22682
   142
  in (cts, tye) end;
wenzelm@22682
   143
wenzelm@22682
   144
fun read_cterm thy sT =
wenzelm@22682
   145
  let val ([ct], _) = read_def_cterms (thy, K NONE, K NONE) [] true [sT]
wenzelm@22682
   146
  in ct end;
wenzelm@22682
   147
wenzelm@22682
   148
wenzelm@22362
   149
open Thm;
wenzelm@22362
   150
wenzelm@22362
   151
end;
wenzelm@22362
   152
wenzelm@22362
   153
structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);