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