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