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