src/Pure/more_thm.ML
author wenzelm
Sat Dec 13 15:00:39 2008 +0100 (2008-12-13)
changeset 29091 b81fe045e799
parent 28965 1de908189869
child 29269 5c25a2012975
permissions -rw-r--r--
Context.display_names;
     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 check_shyps: sort list -> thm -> thm
    31   val is_dummy: thm -> bool
    32   val plain_prop_of: thm -> term
    33   val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
    34   val add_thm: thm -> thm list -> thm list
    35   val del_thm: thm -> thm list -> thm list
    36   val merge_thms: thm list * thm list -> thm list
    37   val elim_implies: thm -> thm -> thm
    38   val forall_elim_var: int -> thm -> thm
    39   val forall_elim_vars: int -> thm -> thm
    40   val unvarify: thm -> thm
    41   val close_derivation: thm -> thm
    42   val add_axiom: bstring * term -> theory -> thm * theory
    43   val add_def: bool -> bool -> bstring * term -> theory -> thm * theory
    44   val rule_attribute: (Context.generic -> thm -> thm) -> attribute
    45   val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
    46   val theory_attributes: attribute list -> theory * thm -> theory * thm
    47   val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
    48   val no_attributes: 'a -> 'a * 'b list
    49   val simple_fact: 'a -> ('a * 'b list) list
    50   val tag_rule: Properties.property -> thm -> thm
    51   val untag_rule: string -> thm -> thm
    52   val tag: Properties.property -> attribute
    53   val untag: string -> attribute
    54   val position_of: thm -> Position.T
    55   val default_position: Position.T -> thm -> thm
    56   val default_position_of: thm -> thm -> thm
    57   val has_name_hint: thm -> bool
    58   val get_name_hint: thm -> string
    59   val put_name_hint: string -> thm -> thm
    60   val get_group: thm -> string option
    61   val put_group: string -> thm -> thm
    62   val group: string -> attribute
    63   val axiomK: string
    64   val assumptionK: string
    65   val definitionK: string
    66   val theoremK: string
    67   val lemmaK: string
    68   val corollaryK: string
    69   val internalK: string
    70   val has_kind: thm -> bool
    71   val get_kind: thm -> string
    72   val kind_rule: string -> thm -> thm
    73   val kind: string -> attribute
    74   val kind_internal: attribute
    75   val has_internal: Properties.property list -> bool
    76   val is_internal: thm -> bool
    77 end;
    78 
    79 structure Thm: THM =
    80 struct
    81 
    82 (** basic operations **)
    83 
    84 (* collecting cterms *)
    85 
    86 val op aconvc = op aconv o pairself Thm.term_of;
    87 
    88 fun add_cterm_frees ct =
    89   let
    90     val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
    91     val t = Thm.term_of ct;
    92   in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
    93 
    94 
    95 (* cterm constructors and destructors *)
    96 
    97 fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
    98 fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
    99 
   100 fun dest_implies ct =
   101   (case Thm.term_of ct of
   102     Const ("==>", _) $ _ $ _ => dest_binop ct
   103   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   104 
   105 fun dest_equals ct =
   106   (case Thm.term_of ct of
   107     Const ("==", _) $ _ $ _ => dest_binop ct
   108   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   109 
   110 fun dest_equals_lhs ct =
   111   (case Thm.term_of ct of
   112     Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
   113   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   114 
   115 fun dest_equals_rhs ct =
   116   (case Thm.term_of ct of
   117     Const ("==", _) $ _ $ _ => Thm.dest_arg ct
   118   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   119 
   120 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   121 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   122 
   123 
   124 (* thm order: ignores theory context! *)
   125 
   126 fun thm_ord (th1, th2) =
   127   let
   128     val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
   129     val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
   130   in
   131     (case Term.fast_term_ord (prop1, prop2) of
   132       EQUAL =>
   133         (case list_ord (prod_ord Term.fast_term_ord Term.fast_term_ord) (tpairs1, tpairs2) of
   134           EQUAL =>
   135             (case list_ord Term.fast_term_ord (hyps1, hyps2) of
   136               EQUAL => list_ord Term.sort_ord (shyps1, shyps2)
   137             | ord => ord)
   138         | ord => ord)
   139     | ord => ord)
   140   end;
   141 
   142 
   143 (* equality *)
   144 
   145 fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
   146   handle TERM _ => false;
   147 
   148 fun eq_thm ths =
   149   Context.joinable (pairself Thm.theory_of_thm ths) andalso
   150   is_equal (thm_ord ths);
   151 
   152 val eq_thms = eq_list eq_thm;
   153 
   154 val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
   155 val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
   156 
   157 
   158 (* pattern equivalence *)
   159 
   160 fun equiv_thm ths =
   161   Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
   162 
   163 
   164 (* sort hypotheses *)
   165 
   166 fun check_shyps sorts raw_th =
   167   let
   168     val th = Thm.strip_shyps raw_th;
   169     val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
   170     val pending = Sorts.subtract sorts (Thm.extra_shyps th);
   171   in
   172     if null pending then th
   173     else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
   174       Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
   175   end;
   176 
   177 
   178 (* misc operations *)
   179 
   180 fun is_dummy thm =
   181   (case try Logic.dest_term (Thm.concl_of thm) of
   182     NONE => false
   183   | SOME t => Term.is_dummy_pattern t);
   184 
   185 fun plain_prop_of raw_thm =
   186   let
   187     val thm = Thm.strip_shyps raw_thm;
   188     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   189     val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
   190   in
   191     if not (null hyps) then
   192       err "theorem may not contain hypotheses"
   193     else if not (null (Thm.extra_shyps thm)) then
   194       err "theorem may not contain sort hypotheses"
   195     else if not (null tpairs) then
   196       err "theorem may not contain flex-flex pairs"
   197     else prop
   198   end;
   199 
   200 fun fold_terms f th =
   201   let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
   202   in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
   203 
   204 
   205 (* lists of theorems in canonical order *)
   206 
   207 val add_thm = update eq_thm_prop;
   208 val del_thm = remove eq_thm_prop;
   209 val merge_thms = merge eq_thm_prop;
   210 
   211 
   212 
   213 (** basic derived rules **)
   214 
   215 (*Elimination of implication
   216   A    A ==> B
   217   ------------
   218         B
   219 *)
   220 fun elim_implies thA thAB = Thm.implies_elim thAB thA;
   221 
   222 
   223 (* forall_elim_var(s) *)
   224 
   225 local
   226 
   227 fun forall_elim_vars_aux strip_vars i th =
   228   let
   229     val thy = Thm.theory_of_thm th;
   230     val {tpairs, prop, ...} = Thm.rep_thm th;
   231     val add_used = Term.fold_aterms
   232       (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
   233     val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
   234     val vars = strip_vars prop;
   235     val cvars = (Name.variant_list used (map #1 vars), vars)
   236       |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
   237   in fold Thm.forall_elim cvars th end;
   238 
   239 in
   240 
   241 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
   242 
   243 fun forall_elim_var i th = forall_elim_vars_aux
   244   (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
   245   | _ => raise THM ("forall_elim_vars", i, [th])) i th;
   246 
   247 end;
   248 
   249 
   250 (* unvarify: global schematic variables *)
   251 
   252 fun unvarify th =
   253   let
   254     val thy = Thm.theory_of_thm th;
   255     val cert = Thm.cterm_of thy;
   256     val certT = Thm.ctyp_of thy;
   257 
   258     val prop = Thm.full_prop_of th;
   259     val _ = map Logic.unvarify (prop :: Thm.hyps_of th)
   260       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   261 
   262     val instT0 = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   263     val instT = map (fn (v, T) => (certT (TVar v), certT T)) instT0;
   264     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   265       let val T' = TermSubst.instantiateT instT0 T
   266       in (cert (Var ((a, i), T')), cert (Free ((a, T')))) end);
   267   in Thm.instantiate (instT, inst) th end;
   268 
   269 
   270 (* close_derivation *)
   271 
   272 fun close_derivation thm =
   273   if Thm.get_name thm = "" then Thm.put_name "" thm
   274   else thm;
   275 
   276 
   277 
   278 (** specification primitives **)
   279 
   280 fun add_axiom (name, prop) thy =
   281   let
   282     val name' = if name = "" then "axiom_" ^ serial_string () else name;
   283     val thy' = thy |> Theory.add_axioms_i [(name', prop)];
   284     val axm = unvarify (Thm.axiom thy' (Sign.full_bname thy' name'));
   285   in (axm, thy') end;
   286 
   287 fun add_def unchecked overloaded (name, prop) thy =
   288   let
   289     val tfrees = rev (map TFree (Term.add_tfrees prop []));
   290     val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
   291     val strip_sorts = tfrees ~~ tfrees';
   292     val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
   293 
   294     val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
   295     val thy' = Theory.add_defs_i unchecked overloaded [(name, prop')] thy;
   296     val axm' = Thm.axiom thy' (Sign.full_bname thy' name);
   297     val thm = unvarify (Thm.instantiate (recover_sorts, []) axm');
   298   in (thm, thy') end;
   299 
   300 
   301 
   302 (** attributes **)
   303 
   304 fun rule_attribute f (x, th) = (x, f x th);
   305 fun declaration_attribute f (x, th) = (f th x, th);
   306 
   307 fun apply_attributes mk dest =
   308   let
   309     fun app [] = I
   310       | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
   311   in app end;
   312 
   313 val theory_attributes = apply_attributes Context.Theory Context.the_theory;
   314 val proof_attributes = apply_attributes Context.Proof Context.the_proof;
   315 
   316 fun no_attributes x = (x, []);
   317 fun simple_fact x = [(x, [])];
   318 
   319 
   320 
   321 (*** theorem tags ***)
   322 
   323 (* add / delete tags *)
   324 
   325 fun tag_rule tg = Thm.map_tags (insert (op =) tg);
   326 fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
   327 
   328 fun tag tg x = rule_attribute (K (tag_rule tg)) x;
   329 fun untag s x = rule_attribute (K (untag_rule s)) x;
   330 
   331 
   332 (* position *)
   333 
   334 val position_of = Position.of_properties o Thm.get_tags;
   335 
   336 fun default_position pos = Thm.map_tags (Position.default_properties pos);
   337 val default_position_of = default_position o position_of;
   338 
   339 
   340 (* unofficial theorem names *)
   341 
   342 fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
   343 
   344 val has_name_hint = can the_name_hint;
   345 val get_name_hint = the_default "??.unknown" o try the_name_hint;
   346 
   347 fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
   348 
   349 
   350 (* theorem groups *)
   351 
   352 fun get_group thm = Properties.get (Thm.get_tags thm) Markup.groupN;
   353 
   354 fun put_group name = if name = "" then I else Thm.map_tags (Properties.put (Markup.groupN, name));
   355 
   356 fun group name = rule_attribute (K (put_group name));
   357 
   358 
   359 (* theorem kinds *)
   360 
   361 val axiomK = "axiom";
   362 val assumptionK = "assumption";
   363 val definitionK = "definition";
   364 val theoremK = "theorem";
   365 val lemmaK = "lemma";
   366 val corollaryK = "corollary";
   367 val internalK = Markup.internalK;
   368 
   369 fun the_kind thm = the (Properties.get (Thm.get_tags thm) Markup.kindN);
   370 
   371 val has_kind = can the_kind;
   372 val get_kind = the_default "" o try the_kind;
   373 
   374 fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
   375 fun kind k x = if k = "" then x else rule_attribute (K (kind_rule k)) x;
   376 fun kind_internal x = kind internalK x;
   377 fun has_internal tags = exists (fn tg => tg = (Markup.kindN, internalK)) tags;
   378 val is_internal = has_internal o Thm.get_tags;
   379 
   380 
   381 open Thm;
   382 
   383 end;
   384 
   385 val op aconvc = Thm.aconvc;
   386 
   387 structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);