src/Pure/more_thm.ML
author wenzelm
Thu Aug 02 12:36:54 2012 +0200 (2012-08-02)
changeset 48646 91281e9472d8
parent 46861 152e8ca3264e
child 49010 72808e956879
permissions -rw-r--r--
more official command specifications, including source position;
     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 end;
    99 
   100 structure Thm: THM =
   101 struct
   102 
   103 (** basic operations **)
   104 
   105 (* collecting cterms *)
   106 
   107 val op aconvc = op aconv o pairself Thm.term_of;
   108 
   109 fun add_cterm_frees ct =
   110   let
   111     val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
   112     val t = Thm.term_of ct;
   113   in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
   114 
   115 
   116 (* cterm constructors and destructors *)
   117 
   118 fun all_name (x, t) A =
   119   let
   120     val cert = Thm.cterm_of (Thm.theory_of_cterm t);
   121     val T = #T (Thm.rep_cterm t);
   122   in Thm.apply (cert (Const ("all", (T --> propT) --> propT))) (Thm.lambda_name (x, t) A) end;
   123 
   124 fun all t A = all_name ("", t) A;
   125 
   126 fun mk_binop c a b = Thm.apply (Thm.apply c a) b;
   127 fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
   128 
   129 fun dest_implies ct =
   130   (case Thm.term_of ct of
   131     Const ("==>", _) $ _ $ _ => dest_binop ct
   132   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   133 
   134 fun dest_equals ct =
   135   (case Thm.term_of ct of
   136     Const ("==", _) $ _ $ _ => dest_binop ct
   137   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   138 
   139 fun dest_equals_lhs ct =
   140   (case Thm.term_of ct of
   141     Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
   142   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   143 
   144 fun dest_equals_rhs ct =
   145   (case Thm.term_of ct of
   146     Const ("==", _) $ _ $ _ => Thm.dest_arg ct
   147   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   148 
   149 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   150 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   151 
   152 
   153 (* thm order: ignores theory context! *)
   154 
   155 fun thm_ord (th1, th2) =
   156   let
   157     val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
   158     val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
   159   in
   160     (case Term_Ord.fast_term_ord (prop1, prop2) of
   161       EQUAL =>
   162         (case list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord) (tpairs1, tpairs2) of
   163           EQUAL =>
   164             (case list_ord Term_Ord.fast_term_ord (hyps1, hyps2) of
   165               EQUAL => list_ord Term_Ord.sort_ord (shyps1, shyps2)
   166             | ord => ord)
   167         | ord => ord)
   168     | ord => ord)
   169   end;
   170 
   171 
   172 (* tables and caches *)
   173 
   174 structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o pairself Thm.term_of);
   175 structure Thmtab = Table(type key = thm val ord = thm_ord);
   176 
   177 fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
   178 fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
   179 
   180 
   181 (* equality *)
   182 
   183 fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
   184   handle TERM _ => false;
   185 
   186 fun eq_thm ths =
   187   Context.joinable (pairself Thm.theory_of_thm ths) andalso
   188   is_equal (thm_ord ths);
   189 
   190 val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
   191 val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
   192 
   193 
   194 (* pattern equivalence *)
   195 
   196 fun equiv_thm ths =
   197   Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
   198 
   199 
   200 (* type classes and sorts *)
   201 
   202 fun class_triv thy c =
   203   Thm.of_class (Thm.ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
   204 
   205 fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
   206 
   207 fun check_shyps sorts raw_th =
   208   let
   209     val th = Thm.strip_shyps raw_th;
   210     val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
   211     val pending = Sorts.subtract sorts (Thm.extra_shyps th);
   212   in
   213     if null pending then th
   214     else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
   215       Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
   216   end;
   217 
   218 
   219 (* misc operations *)
   220 
   221 fun is_dummy thm =
   222   (case try Logic.dest_term (Thm.concl_of thm) of
   223     NONE => false
   224   | SOME t => Term.is_dummy_pattern t);
   225 
   226 fun plain_prop_of raw_thm =
   227   let
   228     val thm = Thm.strip_shyps raw_thm;
   229     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   230     val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
   231   in
   232     if not (null hyps) then
   233       err "theorem may not contain hypotheses"
   234     else if not (null (Thm.extra_shyps thm)) then
   235       err "theorem may not contain sort hypotheses"
   236     else if not (null tpairs) then
   237       err "theorem may not contain flex-flex pairs"
   238     else prop
   239   end;
   240 
   241 
   242 (* collections of theorems in canonical order *)
   243 
   244 val add_thm = update eq_thm_prop;
   245 val del_thm = remove eq_thm_prop;
   246 val merge_thms = merge eq_thm_prop;
   247 
   248 val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
   249 val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
   250 val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
   251 
   252 
   253 
   254 (** basic derived rules **)
   255 
   256 (*Elimination of implication
   257   A    A ==> B
   258   ------------
   259         B
   260 *)
   261 fun elim_implies thA thAB = Thm.implies_elim thAB thA;
   262 
   263 
   264 (* forall_elim_var(s) *)
   265 
   266 local
   267 
   268 fun forall_elim_vars_aux strip_vars i th =
   269   let
   270     val thy = Thm.theory_of_thm th;
   271     val {tpairs, prop, ...} = Thm.rep_thm th;
   272     val add_used = Term.fold_aterms
   273       (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
   274     val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
   275     val vars = strip_vars prop;
   276     val cvars = (Name.variant_list used (map #1 vars), vars)
   277       |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
   278   in fold Thm.forall_elim cvars th end;
   279 
   280 in
   281 
   282 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
   283 
   284 fun forall_elim_var i th =
   285   forall_elim_vars_aux
   286     (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
   287       | _ => raise THM ("forall_elim_vars", i, [th])) i th;
   288 
   289 end;
   290 
   291 
   292 (* certify_instantiate *)
   293 
   294 fun certify_inst thy (instT, inst) =
   295   (map (fn (v, T) => (Thm.ctyp_of thy (TVar v), Thm.ctyp_of thy T)) instT,
   296     map (fn (v, t) => (Thm.cterm_of thy (Var v), Thm.cterm_of thy t)) inst);
   297 
   298 fun certify_instantiate insts th =
   299   Thm.instantiate (certify_inst (Thm.theory_of_thm th) insts) th;
   300 
   301 
   302 (* forall_intr_frees: generalization over all suitable Free variables *)
   303 
   304 fun forall_intr_frees th =
   305   let
   306     val thy = Thm.theory_of_thm th;
   307     val {prop, hyps, tpairs, ...} = Thm.rep_thm th;
   308     val fixed = fold Term.add_frees (Thm.terms_of_tpairs tpairs @ hyps) [];
   309     val frees = Term.fold_aterms (fn Free v =>
   310       if member (op =) fixed v then I else insert (op =) v | _ => I) prop [];
   311   in fold (Thm.forall_intr o Thm.cterm_of thy o Free) frees th end;
   312 
   313 
   314 (* unvarify_global: global schematic variables *)
   315 
   316 fun unvarify_global th =
   317   let
   318     val prop = Thm.full_prop_of th;
   319     val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
   320       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   321 
   322     val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   323     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   324       let val T' = Term_Subst.instantiateT instT T
   325       in (((a, i), T'), Free ((a, T'))) end);
   326   in certify_instantiate (instT, inst) th end;
   327 
   328 
   329 (* close_derivation *)
   330 
   331 fun close_derivation thm =
   332   if Thm.derivation_name thm = "" then Thm.name_derivation "" thm
   333   else thm;
   334 
   335 
   336 
   337 (** specification primitives **)
   338 
   339 (* rules *)
   340 
   341 fun stripped_sorts thy t =
   342   let
   343     val tfrees = rev (map TFree (Term.add_tfrees t []));
   344     val tfrees' = map (fn a => TFree (a, [])) (Name.invent Name.context Name.aT (length tfrees));
   345     val strip = tfrees ~~ tfrees';
   346     val recover = map (pairself (Thm.ctyp_of thy o Logic.varifyT_global) o swap) strip;
   347     val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
   348   in (strip, recover, t') end;
   349 
   350 fun add_axiom ctxt (b, prop) thy =
   351   let
   352     val b' = if Binding.is_empty b then Binding.name ("unnamed_axiom_" ^ serial_string ()) else b;
   353 
   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 open Thm;
   470 
   471 end;
   472 
   473 structure Basic_Thm: BASIC_THM = Thm;
   474 open Basic_Thm;
   475