src/Pure/more_thm.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 51316 dfe469293eb4
child 52683 fb028440473e
permissions -rw-r--r--
tuned signature;
tuned comments;
     1 (*  Title:      Pure/more_thm.ML
     2     Author:     Makarius
     3 
     4 Further operations on type ctyp/cterm/thm, outside the inference kernel.
     5 *)
     6 
     7 infix aconvc;
     8 
     9 signature BASIC_THM =
    10 sig
    11   include BASIC_THM
    12   structure Ctermtab: TABLE
    13   structure Thmtab: TABLE
    14   val aconvc: cterm * cterm -> bool
    15   type attribute = Context.generic * thm -> Context.generic option * thm option
    16 end;
    17 
    18 signature THM =
    19 sig
    20   include THM
    21   structure Ctermtab: TABLE
    22   structure Thmtab: TABLE
    23   val aconvc: cterm * cterm -> bool
    24   val add_cterm_frees: cterm -> cterm list -> cterm list
    25   val all_name: string * cterm -> cterm -> cterm
    26   val all: cterm -> cterm -> cterm
    27   val mk_binop: cterm -> cterm -> cterm -> cterm
    28   val dest_binop: cterm -> cterm * cterm
    29   val dest_implies: cterm -> cterm * cterm
    30   val dest_equals: cterm -> cterm * cterm
    31   val dest_equals_lhs: cterm -> cterm
    32   val dest_equals_rhs: cterm -> cterm
    33   val lhs_of: thm -> cterm
    34   val rhs_of: thm -> cterm
    35   val thm_ord: thm * thm -> order
    36   val cterm_cache: (cterm -> 'a) -> cterm -> 'a
    37   val thm_cache: (thm -> 'a) -> thm -> 'a
    38   val is_reflexive: thm -> bool
    39   val eq_thm: thm * thm -> bool
    40   val eq_thm_thy: thm * thm -> bool
    41   val eq_thm_prop: thm * thm -> bool
    42   val equiv_thm: thm * thm -> bool
    43   val class_triv: theory -> class -> thm
    44   val of_sort: ctyp * sort -> thm list
    45   val check_shyps: sort list -> thm -> thm
    46   val is_dummy: thm -> bool
    47   val plain_prop_of: thm -> term
    48   val add_thm: thm -> thm list -> thm list
    49   val del_thm: thm -> thm list -> thm list
    50   val merge_thms: thm list * thm list -> thm list
    51   val full_rules: thm Item_Net.T
    52   val intro_rules: thm Item_Net.T
    53   val elim_rules: thm Item_Net.T
    54   val elim_implies: thm -> thm -> thm
    55   val forall_elim_var: int -> thm -> thm
    56   val forall_elim_vars: int -> thm -> thm
    57   val certify_inst: theory ->
    58     ((indexname * sort) * typ) list * ((indexname * typ) * term) list ->
    59     (ctyp * ctyp) list * (cterm * cterm) list
    60   val certify_instantiate:
    61     ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> thm -> thm
    62   val forall_intr_frees: thm -> thm
    63   val unvarify_global: thm -> thm
    64   val close_derivation: thm -> thm
    65   val add_axiom: Proof.context -> binding * term -> theory -> (string * thm) * theory
    66   val add_axiom_global: binding * term -> theory -> (string * thm) * theory
    67   val add_def: Proof.context -> bool -> bool -> binding * term -> theory -> (string * thm) * theory
    68   val add_def_global: bool -> bool -> binding * term -> theory -> (string * thm) * theory
    69   type attribute = Context.generic * thm -> Context.generic option * thm option
    70   type binding = binding * attribute list
    71   val empty_binding: binding
    72   val rule_attribute: (Context.generic -> thm -> thm) -> attribute
    73   val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
    74   val mixed_attribute: (Context.generic * thm -> Context.generic * thm) -> attribute
    75   val apply_attribute: attribute -> thm -> Context.generic -> thm * Context.generic
    76   val attribute_declaration: attribute -> thm -> Context.generic -> Context.generic
    77   val theory_attributes: attribute list -> thm -> theory -> thm * theory
    78   val proof_attributes: attribute list -> thm -> Proof.context -> thm * Proof.context
    79   val no_attributes: 'a -> 'a * 'b list
    80   val simple_fact: 'a -> ('a * 'b list) list
    81   val tag_rule: string * string -> thm -> thm
    82   val untag_rule: string -> thm -> thm
    83   val tag: string * string -> attribute
    84   val untag: string -> attribute
    85   val def_name: string -> string
    86   val def_name_optional: string -> string -> string
    87   val def_binding: Binding.binding -> Binding.binding
    88   val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
    89   val has_name_hint: thm -> bool
    90   val get_name_hint: thm -> string
    91   val put_name_hint: string -> thm -> thm
    92   val theoremK: string
    93   val lemmaK: string
    94   val corollaryK: string
    95   val legacy_get_kind: thm -> string
    96   val kind_rule: string -> thm -> thm
    97   val kind: string -> attribute
    98   val register_proofs: thm list -> theory -> theory
    99   val join_theory_proofs: theory -> unit
   100 end;
   101 
   102 structure Thm: THM =
   103 struct
   104 
   105 (** basic operations **)
   106 
   107 (* collecting cterms *)
   108 
   109 val op aconvc = op aconv o pairself Thm.term_of;
   110 
   111 fun add_cterm_frees ct =
   112   let
   113     val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
   114     val t = Thm.term_of ct;
   115   in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
   116 
   117 
   118 (* cterm constructors and destructors *)
   119 
   120 fun all_name (x, t) A =
   121   let
   122     val cert = Thm.cterm_of (Thm.theory_of_cterm t);
   123     val T = #T (Thm.rep_cterm t);
   124   in Thm.apply (cert (Const ("all", (T --> propT) --> propT))) (Thm.lambda_name (x, t) A) end;
   125 
   126 fun all t A = all_name ("", t) A;
   127 
   128 fun mk_binop c a b = Thm.apply (Thm.apply c a) b;
   129 fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
   130 
   131 fun dest_implies ct =
   132   (case Thm.term_of ct of
   133     Const ("==>", _) $ _ $ _ => dest_binop ct
   134   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   135 
   136 fun dest_equals ct =
   137   (case Thm.term_of ct of
   138     Const ("==", _) $ _ $ _ => dest_binop ct
   139   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   140 
   141 fun dest_equals_lhs ct =
   142   (case Thm.term_of ct of
   143     Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
   144   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   145 
   146 fun dest_equals_rhs ct =
   147   (case Thm.term_of ct of
   148     Const ("==", _) $ _ $ _ => Thm.dest_arg ct
   149   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   150 
   151 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   152 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   153 
   154 
   155 (* thm order: ignores theory context! *)
   156 
   157 fun thm_ord (th1, th2) =
   158   let
   159     val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
   160     val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
   161   in
   162     (case Term_Ord.fast_term_ord (prop1, prop2) of
   163       EQUAL =>
   164         (case list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord) (tpairs1, tpairs2) of
   165           EQUAL =>
   166             (case list_ord Term_Ord.fast_term_ord (hyps1, hyps2) of
   167               EQUAL => list_ord Term_Ord.sort_ord (shyps1, shyps2)
   168             | ord => ord)
   169         | ord => ord)
   170     | ord => ord)
   171   end;
   172 
   173 
   174 (* tables and caches *)
   175 
   176 structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o pairself Thm.term_of);
   177 structure Thmtab = Table(type key = thm val ord = thm_ord);
   178 
   179 fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
   180 fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
   181 
   182 
   183 (* equality *)
   184 
   185 fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
   186   handle TERM _ => false;
   187 
   188 fun eq_thm ths =
   189   Context.joinable (pairself Thm.theory_of_thm ths) andalso
   190   is_equal (thm_ord ths);
   191 
   192 val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
   193 val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
   194 
   195 
   196 (* pattern equivalence *)
   197 
   198 fun equiv_thm ths =
   199   Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
   200 
   201 
   202 (* type classes and sorts *)
   203 
   204 fun class_triv thy c =
   205   Thm.of_class (Thm.ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
   206 
   207 fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
   208 
   209 fun check_shyps sorts raw_th =
   210   let
   211     val th = Thm.strip_shyps raw_th;
   212     val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
   213     val pending = Sorts.subtract sorts (Thm.extra_shyps th);
   214   in
   215     if null pending then th
   216     else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
   217       Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
   218   end;
   219 
   220 
   221 (* misc operations *)
   222 
   223 fun is_dummy thm =
   224   (case try Logic.dest_term (Thm.concl_of thm) of
   225     NONE => false
   226   | SOME t => Term.is_dummy_pattern t);
   227 
   228 fun plain_prop_of raw_thm =
   229   let
   230     val thm = Thm.strip_shyps raw_thm;
   231     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   232     val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
   233   in
   234     if not (null hyps) then
   235       err "theorem may not contain hypotheses"
   236     else if not (null (Thm.extra_shyps thm)) then
   237       err "theorem may not contain sort hypotheses"
   238     else if not (null tpairs) then
   239       err "theorem may not contain flex-flex pairs"
   240     else prop
   241   end;
   242 
   243 
   244 (* collections of theorems in canonical order *)
   245 
   246 val add_thm = update eq_thm_prop;
   247 val del_thm = remove eq_thm_prop;
   248 val merge_thms = merge eq_thm_prop;
   249 
   250 val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
   251 val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
   252 val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
   253 
   254 
   255 
   256 (** basic derived rules **)
   257 
   258 (*Elimination of implication
   259   A    A ==> B
   260   ------------
   261         B
   262 *)
   263 fun elim_implies thA thAB = Thm.implies_elim thAB thA;
   264 
   265 
   266 (* forall_elim_var(s) *)
   267 
   268 local
   269 
   270 fun forall_elim_vars_aux strip_vars i th =
   271   let
   272     val thy = Thm.theory_of_thm th;
   273     val {tpairs, prop, ...} = Thm.rep_thm th;
   274     val add_used = Term.fold_aterms
   275       (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
   276     val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
   277     val vars = strip_vars prop;
   278     val cvars = (Name.variant_list used (map #1 vars), vars)
   279       |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
   280   in fold Thm.forall_elim cvars th end;
   281 
   282 in
   283 
   284 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
   285 
   286 fun forall_elim_var i th =
   287   forall_elim_vars_aux
   288     (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
   289       | _ => raise THM ("forall_elim_vars", i, [th])) i th;
   290 
   291 end;
   292 
   293 
   294 (* certify_instantiate *)
   295 
   296 fun certify_inst thy (instT, inst) =
   297   (map (fn (v, T) => (Thm.ctyp_of thy (TVar v), Thm.ctyp_of thy T)) instT,
   298     map (fn (v, t) => (Thm.cterm_of thy (Var v), Thm.cterm_of thy t)) inst);
   299 
   300 fun certify_instantiate insts th =
   301   Thm.instantiate (certify_inst (Thm.theory_of_thm th) insts) th;
   302 
   303 
   304 (* forall_intr_frees: generalization over all suitable Free variables *)
   305 
   306 fun forall_intr_frees th =
   307   let
   308     val thy = Thm.theory_of_thm th;
   309     val {prop, hyps, tpairs, ...} = Thm.rep_thm th;
   310     val fixed = fold Term.add_frees (Thm.terms_of_tpairs tpairs @ hyps) [];
   311     val frees = Term.fold_aterms (fn Free v =>
   312       if member (op =) fixed v then I else insert (op =) v | _ => I) prop [];
   313   in fold (Thm.forall_intr o Thm.cterm_of thy o Free) frees th end;
   314 
   315 
   316 (* unvarify_global: global schematic variables *)
   317 
   318 fun unvarify_global th =
   319   let
   320     val prop = Thm.full_prop_of th;
   321     val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
   322       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   323 
   324     val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   325     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   326       let val T' = Term_Subst.instantiateT instT T
   327       in (((a, i), T'), Free ((a, T'))) end);
   328   in certify_instantiate (instT, inst) th end;
   329 
   330 
   331 (* close_derivation *)
   332 
   333 fun close_derivation thm =
   334   if Thm.derivation_name thm = "" then Thm.name_derivation "" thm
   335   else thm;
   336 
   337 
   338 
   339 (** specification primitives **)
   340 
   341 (* rules *)
   342 
   343 fun stripped_sorts thy t =
   344   let
   345     val tfrees = rev (map TFree (Term.add_tfrees t []));
   346     val tfrees' = map (fn a => TFree (a, [])) (Name.invent Name.context Name.aT (length tfrees));
   347     val strip = tfrees ~~ tfrees';
   348     val recover = map (pairself (Thm.ctyp_of thy o Logic.varifyT_global) o swap) strip;
   349     val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
   350   in (strip, recover, t') end;
   351 
   352 fun add_axiom ctxt (b, prop) thy =
   353   let
   354     val _ = Sign.no_vars ctxt prop;
   355     val (strip, recover, prop') = stripped_sorts thy prop;
   356     val constraints = map (fn (TFree (_, S), T) => (T, S)) strip;
   357     val of_sorts = maps (fn (T as TFree (_, S), _) => of_sort (Thm.ctyp_of thy T, S)) strip;
   358 
   359     val thy' = thy
   360       |> Theory.add_axiom ctxt (b, Logic.list_implies (maps Logic.mk_of_sort constraints, prop'));
   361     val axm_name = Sign.full_name thy' b;
   362     val axm' = Thm.axiom thy' axm_name;
   363     val thm =
   364       Thm.instantiate (recover, []) axm'
   365       |> unvarify_global
   366       |> fold elim_implies of_sorts;
   367   in ((axm_name, thm), thy') end;
   368 
   369 fun add_axiom_global arg thy = add_axiom (Syntax.init_pretty_global thy) arg thy;
   370 
   371 fun add_def ctxt unchecked overloaded (b, prop) thy =
   372   let
   373     val _ = Sign.no_vars ctxt prop;
   374     val prems = map (Thm.cterm_of thy) (Logic.strip_imp_prems prop);
   375     val (_, recover, concl') = stripped_sorts thy (Logic.strip_imp_concl prop);
   376 
   377     val thy' = Theory.add_def ctxt unchecked overloaded (b, concl') thy;
   378     val axm_name = Sign.full_name thy' b;
   379     val axm' = Thm.axiom thy' axm_name;
   380     val thm =
   381       Thm.instantiate (recover, []) axm'
   382       |> unvarify_global
   383       |> fold_rev Thm.implies_intr prems;
   384   in ((axm_name, thm), thy') end;
   385 
   386 fun add_def_global unchecked overloaded arg thy =
   387   add_def (Syntax.init_pretty_global thy) unchecked overloaded arg thy;
   388 
   389 
   390 
   391 (** attributes **)
   392 
   393 (*attributes subsume any kind of rules or context modifiers*)
   394 type attribute = Context.generic * thm -> Context.generic option * thm option;
   395 
   396 type binding = binding * attribute list;
   397 val empty_binding: binding = (Binding.empty, []);
   398 
   399 fun rule_attribute f (x, th) = (NONE, SOME (f x th));
   400 fun declaration_attribute f (x, th) = (SOME (f th x), NONE);
   401 fun mixed_attribute f (x, th) = let val (x', th') = f (x, th) in (SOME x', SOME th') end;
   402 
   403 fun apply_attribute (att: attribute) th x =
   404   let val (x', th') = att (x, th)
   405   in (the_default th th', the_default x x') end;
   406 
   407 fun attribute_declaration att th x = #2 (apply_attribute att th x);
   408 
   409 fun apply_attributes mk dest =
   410   let
   411     fun app [] th x = (th, x)
   412       | app (att :: atts) th x = apply_attribute att th (mk x) ||> dest |-> app atts;
   413   in app end;
   414 
   415 val theory_attributes = apply_attributes Context.Theory Context.the_theory;
   416 val proof_attributes = apply_attributes Context.Proof Context.the_proof;
   417 
   418 fun no_attributes x = (x, []);
   419 fun simple_fact x = [(x, [])];
   420 
   421 
   422 
   423 (*** theorem tags ***)
   424 
   425 (* add / delete tags *)
   426 
   427 fun tag_rule tg = Thm.map_tags (insert (op =) tg);
   428 fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
   429 
   430 fun tag tg = rule_attribute (K (tag_rule tg));
   431 fun untag s = rule_attribute (K (untag_rule s));
   432 
   433 
   434 (* def_name *)
   435 
   436 fun def_name c = c ^ "_def";
   437 
   438 fun def_name_optional c "" = def_name c
   439   | def_name_optional _ name = name;
   440 
   441 val def_binding = Binding.map_name def_name;
   442 
   443 fun def_binding_optional b name =
   444   if Binding.is_empty name then def_binding b else name;
   445 
   446 
   447 (* unofficial theorem names *)
   448 
   449 fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
   450 
   451 val has_name_hint = can the_name_hint;
   452 val get_name_hint = the_default "??.unknown" o try the_name_hint;
   453 
   454 fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
   455 
   456 
   457 (* theorem kinds *)
   458 
   459 val theoremK = "theorem";
   460 val lemmaK = "lemma";
   461 val corollaryK = "corollary";
   462 
   463 fun legacy_get_kind thm = the_default "" (Properties.get (Thm.get_tags thm) Markup.kindN);
   464 
   465 fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
   466 fun kind k = rule_attribute (K (k <> "" ? kind_rule k));
   467 
   468 
   469 (* forked proofs *)
   470 
   471 structure Proofs = Theory_Data
   472 (
   473   type T = thm list;
   474   val empty = [];
   475   fun extend _ = empty;
   476   fun merge _ = empty;
   477 );
   478 
   479 fun register_proofs more_thms = Proofs.map (fn thms => fold cons more_thms thms);
   480 val join_theory_proofs = Thm.join_proofs o rev o Proofs.get;
   481 
   482 
   483 open Thm;
   484 
   485 end;
   486 
   487 structure Basic_Thm: BASIC_THM = Thm;
   488 open Basic_Thm;
   489