src/Pure/more_thm.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66168 fcd09fc36d7f
child 67559 833d154ab189
permissions -rw-r--r--
more robust sorted_entries;
     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   val show_consts: bool Config.T
    13   val show_hyps: bool Config.T
    14   val show_tags: bool Config.T
    15   structure Ctermtab: TABLE
    16   structure Thmtab: TABLE
    17   val aconvc: cterm * cterm -> bool
    18   type attribute = Context.generic * thm -> Context.generic option * thm option
    19 end;
    20 
    21 signature THM =
    22 sig
    23   include THM
    24   structure Ctermtab: TABLE
    25   structure Thmtab: TABLE
    26   val eq_ctyp: ctyp * ctyp -> bool
    27   val aconvc: cterm * cterm -> bool
    28   val add_tvars: thm -> ctyp list -> ctyp list
    29   val add_frees: thm -> cterm list -> cterm list
    30   val add_vars: thm -> cterm list -> cterm list
    31   val all_name: Proof.context -> string * cterm -> cterm -> cterm
    32   val all: Proof.context -> cterm -> cterm -> cterm
    33   val mk_binop: cterm -> cterm -> cterm -> cterm
    34   val dest_binop: cterm -> cterm * cterm
    35   val dest_implies: cterm -> cterm * cterm
    36   val dest_equals: cterm -> cterm * cterm
    37   val dest_equals_lhs: cterm -> cterm
    38   val dest_equals_rhs: cterm -> cterm
    39   val lhs_of: thm -> cterm
    40   val rhs_of: thm -> cterm
    41   val thm_ord: thm * thm -> order
    42   val cterm_cache: (cterm -> 'a) -> cterm -> 'a
    43   val thm_cache: (thm -> 'a) -> thm -> 'a
    44   val is_reflexive: thm -> bool
    45   val eq_thm: thm * thm -> bool
    46   val eq_thm_prop: thm * thm -> bool
    47   val eq_thm_strict: thm * thm -> bool
    48   val equiv_thm: theory -> thm * thm -> bool
    49   val class_triv: theory -> class -> thm
    50   val of_sort: ctyp * sort -> thm list
    51   val is_dummy: thm -> bool
    52   val plain_prop_of: thm -> term
    53   val add_thm: thm -> thm list -> thm list
    54   val del_thm: thm -> thm list -> thm list
    55   val merge_thms: thm list * thm list -> thm list
    56   val full_rules: thm Item_Net.T
    57   val intro_rules: thm Item_Net.T
    58   val elim_rules: thm Item_Net.T
    59   val declare_hyps: cterm -> Proof.context -> Proof.context
    60   val assume_hyps: cterm -> Proof.context -> thm * Proof.context
    61   val unchecked_hyps: Proof.context -> Proof.context
    62   val restore_hyps: Proof.context -> Proof.context -> Proof.context
    63   val undeclared_hyps: Context.generic -> thm -> term list
    64   val check_hyps: Context.generic -> thm -> thm
    65   val declare_term_sorts: term -> Proof.context -> Proof.context
    66   val extra_shyps': Proof.context -> thm -> sort list
    67   val check_shyps: Proof.context -> thm -> thm
    68   val weaken_sorts': Proof.context -> cterm -> cterm
    69   val elim_implies: thm -> thm -> thm
    70   val forall_intr_name: string * cterm -> thm -> thm
    71   val forall_elim_var: int -> thm -> thm
    72   val forall_elim_vars: int -> thm -> thm
    73   val instantiate': ctyp option list -> cterm option list -> thm -> thm
    74   val forall_intr_frees: thm -> thm
    75   val unvarify_global: theory -> thm -> thm
    76   val unvarify_axiom: theory -> string -> thm
    77   val close_derivation: thm -> thm
    78   val rename_params_rule: string list * int -> thm -> thm
    79   val rename_boundvars: term -> term -> thm -> thm
    80   val add_axiom: Proof.context -> binding * term -> theory -> (string * thm) * theory
    81   val add_axiom_global: binding * term -> theory -> (string * thm) * theory
    82   val add_def: Defs.context -> bool -> bool -> binding * term -> theory -> (string * thm) * theory
    83   val add_def_global: bool -> bool -> binding * term -> theory -> (string * thm) * theory
    84   type attribute = Context.generic * thm -> Context.generic option * thm option
    85   type binding = binding * attribute list
    86   val tag_rule: string * string -> thm -> thm
    87   val untag_rule: string -> thm -> thm
    88   val is_free_dummy: thm -> bool
    89   val tag_free_dummy: thm -> thm
    90   val def_name: string -> string
    91   val def_name_optional: string -> string -> string
    92   val def_binding: Binding.binding -> Binding.binding
    93   val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
    94   val make_def_binding: bool -> Binding.binding -> Binding.binding
    95   val has_name_hint: thm -> bool
    96   val get_name_hint: thm -> string
    97   val put_name_hint: string -> thm -> thm
    98   val theoremK: string
    99   val legacy_get_kind: thm -> string
   100   val kind_rule: string -> thm -> thm
   101   val rule_attribute: thm list -> (Context.generic -> thm -> thm) -> attribute
   102   val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
   103   val mixed_attribute: (Context.generic * thm -> Context.generic * thm) -> attribute
   104   val apply_attribute: attribute -> thm -> Context.generic -> thm * Context.generic
   105   val attribute_declaration: attribute -> thm -> Context.generic -> Context.generic
   106   val theory_attributes: attribute list -> thm -> theory -> thm * theory
   107   val proof_attributes: attribute list -> thm -> Proof.context -> thm * Proof.context
   108   val no_attributes: 'a -> 'a * 'b list
   109   val simple_fact: 'a -> ('a * 'b list) list
   110   val tag: string * string -> attribute
   111   val untag: string -> attribute
   112   val kind: string -> attribute
   113   val register_proofs: thm list -> theory -> theory
   114   val consolidate_theory: theory -> unit
   115   val show_consts_raw: Config.raw
   116   val show_consts: bool Config.T
   117   val show_hyps_raw: Config.raw
   118   val show_hyps: bool Config.T
   119   val show_tags_raw: Config.raw
   120   val show_tags: bool Config.T
   121   val pretty_flexpair: Proof.context -> term * term -> Pretty.T
   122   val pretty_thm_raw: Proof.context -> {quote: bool, show_hyps: bool} -> thm -> Pretty.T
   123   val pretty_thm: Proof.context -> thm -> Pretty.T
   124   val pretty_thm_item: Proof.context -> thm -> Pretty.T
   125   val pretty_thm_global: theory -> thm -> Pretty.T
   126   val string_of_thm: Proof.context -> thm -> string
   127   val string_of_thm_global: theory -> thm -> string
   128 end;
   129 
   130 structure Thm: THM =
   131 struct
   132 
   133 (** basic operations **)
   134 
   135 (* collecting ctyps and cterms *)
   136 
   137 val eq_ctyp = op = o apply2 Thm.typ_of;
   138 val op aconvc = op aconv o apply2 Thm.term_of;
   139 
   140 val add_tvars = Thm.fold_atomic_ctyps (fn a => is_TVar (Thm.typ_of a) ? insert eq_ctyp a);
   141 val add_frees = Thm.fold_atomic_cterms (fn a => is_Free (Thm.term_of a) ? insert (op aconvc) a);
   142 val add_vars = Thm.fold_atomic_cterms (fn a => is_Var (Thm.term_of a) ? insert (op aconvc) a);
   143 
   144 
   145 (* cterm constructors and destructors *)
   146 
   147 fun all_name ctxt (x, t) A =
   148   let
   149     val T = Thm.typ_of_cterm t;
   150     val all_const = Thm.cterm_of ctxt (Const ("Pure.all", (T --> propT) --> propT));
   151   in Thm.apply all_const (Thm.lambda_name (x, t) A) end;
   152 
   153 fun all ctxt t A = all_name ctxt ("", t) A;
   154 
   155 fun mk_binop c a b = Thm.apply (Thm.apply c a) b;
   156 fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
   157 
   158 fun dest_implies ct =
   159   (case Thm.term_of ct of
   160     Const ("Pure.imp", _) $ _ $ _ => dest_binop ct
   161   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   162 
   163 fun dest_equals ct =
   164   (case Thm.term_of ct of
   165     Const ("Pure.eq", _) $ _ $ _ => dest_binop ct
   166   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   167 
   168 fun dest_equals_lhs ct =
   169   (case Thm.term_of ct of
   170     Const ("Pure.eq", _) $ _ $ _ => Thm.dest_arg1 ct
   171   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   172 
   173 fun dest_equals_rhs ct =
   174   (case Thm.term_of ct of
   175     Const ("Pure.eq", _) $ _ $ _ => Thm.dest_arg ct
   176   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   177 
   178 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   179 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   180 
   181 
   182 (* thm order: ignores theory context! *)
   183 
   184 fun thm_ord ths =
   185   (case Term_Ord.fast_term_ord (apply2 Thm.prop_of ths) of
   186     EQUAL =>
   187       (case
   188         list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord)
   189           (apply2 Thm.tpairs_of ths)
   190        of
   191         EQUAL =>
   192           (case list_ord Term_Ord.fast_term_ord (apply2 Thm.hyps_of ths) of
   193             EQUAL => list_ord Term_Ord.sort_ord (apply2 Thm.shyps_of ths)
   194           | ord => ord)
   195       | ord => ord)
   196   | ord => ord);
   197 
   198 
   199 (* tables and caches *)
   200 
   201 structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o apply2 Thm.term_of);
   202 structure Thmtab = Table(type key = thm val ord = thm_ord);
   203 
   204 fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
   205 fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
   206 
   207 
   208 (* equality *)
   209 
   210 fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
   211   handle TERM _ => false;
   212 
   213 val eq_thm = is_equal o thm_ord;
   214 
   215 val eq_thm_prop = op aconv o apply2 Thm.full_prop_of;
   216 
   217 fun eq_thm_strict ths =
   218   eq_thm ths andalso
   219   Context.eq_thy_id (apply2 Thm.theory_id ths) andalso
   220   op = (apply2 Thm.maxidx_of ths) andalso
   221   op = (apply2 Thm.get_tags ths);
   222 
   223 
   224 (* pattern equivalence *)
   225 
   226 fun equiv_thm thy ths =
   227   Pattern.equiv thy (apply2 (Thm.full_prop_of o Thm.transfer thy) ths);
   228 
   229 
   230 (* type classes and sorts *)
   231 
   232 fun class_triv thy c =
   233   Thm.of_class (Thm.global_ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
   234 
   235 fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
   236 
   237 
   238 (* misc operations *)
   239 
   240 fun is_dummy thm =
   241   (case try Logic.dest_term (Thm.concl_of thm) of
   242     NONE => false
   243   | SOME t => Term.is_dummy_pattern (Term.head_of t));
   244 
   245 fun plain_prop_of raw_thm =
   246   let
   247     val thm = Thm.strip_shyps raw_thm;
   248     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   249   in
   250     if not (null (Thm.hyps_of thm)) then
   251       err "theorem may not contain hypotheses"
   252     else if not (null (Thm.extra_shyps thm)) then
   253       err "theorem may not contain sort hypotheses"
   254     else if not (null (Thm.tpairs_of thm)) then
   255       err "theorem may not contain flex-flex pairs"
   256     else Thm.prop_of thm
   257   end;
   258 
   259 
   260 (* collections of theorems in canonical order *)
   261 
   262 val add_thm = update eq_thm_prop;
   263 val del_thm = remove eq_thm_prop;
   264 val merge_thms = merge eq_thm_prop;
   265 
   266 val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
   267 val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
   268 val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
   269 
   270 
   271 
   272 (** declared hyps and sort hyps **)
   273 
   274 structure Hyps = Proof_Data
   275 (
   276   type T = {checked_hyps: bool, hyps: Termtab.set, shyps: sort Ord_List.T};
   277   fun init _ : T = {checked_hyps = true, hyps = Termtab.empty, shyps = []};
   278 );
   279 
   280 fun map_hyps f = Hyps.map (fn {checked_hyps, hyps, shyps} =>
   281   let val (checked_hyps', hyps', shyps') = f (checked_hyps, hyps, shyps)
   282   in {checked_hyps = checked_hyps', hyps = hyps', shyps = shyps'} end);
   283 
   284 
   285 (* hyps *)
   286 
   287 fun declare_hyps raw_ct ctxt = ctxt |> map_hyps (fn (checked_hyps, hyps, shyps) =>
   288   let
   289     val ct = Thm.transfer_cterm (Proof_Context.theory_of ctxt) raw_ct;
   290     val hyps' = Termtab.update (Thm.term_of ct, ()) hyps;
   291   in (checked_hyps, hyps', shyps) end);
   292 
   293 fun assume_hyps ct ctxt = (Thm.assume ct, declare_hyps ct ctxt);
   294 
   295 val unchecked_hyps = map_hyps (fn (_, hyps, shyps) => (false, hyps, shyps));
   296 
   297 fun restore_hyps ctxt =
   298   map_hyps (fn (_, hyps, shyps) => (#checked_hyps (Hyps.get ctxt), hyps, shyps));
   299 
   300 fun undeclared_hyps context th =
   301   Thm.hyps_of th
   302   |> filter_out
   303     (case context of
   304       Context.Theory _ => K false
   305     | Context.Proof ctxt =>
   306         (case Hyps.get ctxt of
   307           {checked_hyps = false, ...} => K true
   308         | {hyps, ...} => Termtab.defined hyps));
   309 
   310 fun check_hyps context th =
   311   (case undeclared_hyps context th of
   312     [] => th
   313   | undeclared =>
   314       error (Pretty.string_of (Pretty.big_list "Undeclared hyps:"
   315         (map (Pretty.item o single o Syntax.pretty_term (Syntax.init_pretty context)) undeclared))));
   316 
   317 
   318 (* shyps *)
   319 
   320 fun declare_term_sorts t =
   321   map_hyps (fn (checked_hyps, hyps, shyps) =>
   322     (checked_hyps, hyps, Sorts.insert_term t shyps));
   323 
   324 fun extra_shyps' ctxt th =
   325   Sorts.subtract (#shyps (Hyps.get ctxt)) (Thm.extra_shyps th);
   326 
   327 fun check_shyps ctxt raw_th =
   328   let
   329     val th = Thm.strip_shyps raw_th;
   330     val extra_shyps = extra_shyps' ctxt th;
   331   in
   332     if null extra_shyps then th
   333     else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
   334       Pretty.brk 1 :: Pretty.commas (map (Syntax.pretty_sort ctxt) extra_shyps))))
   335   end;
   336 
   337 val weaken_sorts' = Thm.weaken_sorts o #shyps o Hyps.get;
   338 
   339 
   340 
   341 (** basic derived rules **)
   342 
   343 (*Elimination of implication
   344   A    A ==> B
   345   ------------
   346         B
   347 *)
   348 fun elim_implies thA thAB = Thm.implies_elim thAB thA;
   349 
   350 
   351 (* forall_intr_name *)
   352 
   353 fun forall_intr_name (a, x) th =
   354   let
   355     val th' = Thm.forall_intr x th;
   356     val prop' = (case Thm.prop_of th' of all $ Abs (_, T, b) => all $ Abs (a, T, b));
   357   in Thm.renamed_prop prop' th' end;
   358 
   359 
   360 (* forall_elim_var(s) *)
   361 
   362 local
   363 
   364 fun dest_all ct =
   365   (case Thm.term_of ct of
   366     Const ("Pure.all", _) $ Abs (a, _, _) =>
   367       let val (x, ct') = Thm.dest_abs NONE (Thm.dest_arg ct)
   368       in SOME ((a, Thm.ctyp_of_cterm x), ct') end
   369   | _ => NONE);
   370 
   371 fun dest_all_list ct =
   372   (case dest_all ct of
   373     NONE => []
   374   | SOME (v, ct') => v :: dest_all_list ct');
   375 
   376 fun forall_elim_vars_list vars i th =
   377   let
   378     val used =
   379       (Thm.fold_terms o Term.fold_aterms)
   380         (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I) th [];
   381     val vars' = (Name.variant_list used (map #1 vars), vars)
   382       |> ListPair.map (fn (x, (_, T)) => Thm.var ((x, i), T));
   383   in fold Thm.forall_elim vars' th end;
   384 
   385 in
   386 
   387 fun forall_elim_vars i th =
   388   forall_elim_vars_list (dest_all_list (Thm.cprop_of th)) i th;
   389 
   390 fun forall_elim_var i th =
   391   let
   392     val vars =
   393       (case dest_all (Thm.cprop_of th) of
   394         SOME (v, _) => [v]
   395       | NONE => raise THM ("forall_elim_var", i, [th]));
   396   in forall_elim_vars_list vars i th end;
   397 
   398 end;
   399 
   400 
   401 (* instantiate by left-to-right occurrence of variables *)
   402 
   403 fun instantiate' cTs cts thm =
   404   let
   405     fun err msg =
   406       raise TYPE ("instantiate': " ^ msg,
   407         map_filter (Option.map Thm.typ_of) cTs,
   408         map_filter (Option.map Thm.term_of) cts);
   409 
   410     fun zip_vars xs ys =
   411       zip_options xs ys handle ListPair.UnequalLengths =>
   412         err "more instantiations than variables in thm";
   413 
   414     val thm' =
   415       Thm.instantiate ((zip_vars (rev (Thm.fold_terms Term.add_tvars thm [])) cTs), []) thm;
   416     val thm'' =
   417       Thm.instantiate ([], zip_vars (rev (Thm.fold_terms Term.add_vars thm' [])) cts) thm';
   418   in thm'' end;
   419 
   420 
   421 (* forall_intr_frees: generalization over all suitable Free variables *)
   422 
   423 fun forall_intr_frees th =
   424   let
   425     val fixed =
   426       fold Term.add_frees (Thm.terms_of_tpairs (Thm.tpairs_of th) @ Thm.hyps_of th) [];
   427     val frees =
   428       Thm.fold_atomic_cterms (fn a =>
   429         (case Thm.term_of a of
   430           Free v => not (member (op =) fixed v) ? insert (op aconvc) a
   431         | _ => I)) th [];
   432   in fold Thm.forall_intr frees th end;
   433 
   434 
   435 (* unvarify_global: global schematic variables *)
   436 
   437 fun unvarify_global thy th =
   438   let
   439     val prop = Thm.full_prop_of th;
   440     val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
   441       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   442 
   443     val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   444     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   445       let val T' = Term_Subst.instantiateT instT T
   446       in (((a, i), T'), Thm.global_cterm_of thy (Free ((a, T')))) end);
   447   in Thm.instantiate (map (apsnd (Thm.global_ctyp_of thy)) instT, inst) th end;
   448 
   449 fun unvarify_axiom thy = unvarify_global thy o Thm.axiom thy;
   450 
   451 
   452 (* close_derivation *)
   453 
   454 fun close_derivation thm =
   455   if Thm.derivation_closed thm then thm else Thm.name_derivation "" thm;
   456 
   457 
   458 (* user renaming of parameters in a subgoal *)
   459 
   460 (*The names, if distinct, are used for the innermost parameters of subgoal i;
   461   preceding parameters may be renamed to make all parameters distinct.*)
   462 fun rename_params_rule (names, i) st =
   463   let
   464     val (_, Bs, Bi, C) = Thm.dest_state (st, i);
   465     val params = map #1 (Logic.strip_params Bi);
   466     val short = length params - length names;
   467     val names' =
   468       if short < 0 then error "More names than parameters in subgoal!"
   469       else Name.variant_list names (take short params) @ names;
   470     val free_names = Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) Bi [];
   471     val Bi' = Logic.list_rename_params names' Bi;
   472   in
   473     (case duplicates (op =) names of
   474       a :: _ => (warning ("Can't rename.  Bound variables not distinct: " ^ a); st)
   475     | [] =>
   476       (case inter (op =) names free_names of
   477         a :: _ => (warning ("Can't rename.  Bound/Free variable clash: " ^ a); st)
   478       | [] => Thm.renamed_prop (Logic.list_implies (Bs @ [Bi'], C)) st))
   479   end;
   480 
   481 
   482 (* preservation of bound variable names *)
   483 
   484 fun rename_boundvars pat obj th =
   485   (case Term.rename_abs pat obj (Thm.prop_of th) of
   486     NONE => th
   487   | SOME prop' => Thm.renamed_prop prop' th);
   488 
   489 
   490 
   491 (** specification primitives **)
   492 
   493 (* rules *)
   494 
   495 fun stripped_sorts thy t =
   496   let
   497     val tfrees = rev (Term.add_tfrees t []);
   498     val tfrees' = map (fn a => (a, [])) (Name.invent Name.context Name.aT (length tfrees));
   499     val recover =
   500       map2 (fn (a', S') => fn (a, S) => (((a', 0), S'), Thm.global_ctyp_of thy (TVar ((a, 0), S))))
   501         tfrees' tfrees;
   502     val strip = map (apply2 TFree) (tfrees ~~ tfrees');
   503     val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
   504   in (strip, recover, t') end;
   505 
   506 fun add_axiom ctxt (b, prop) thy =
   507   let
   508     val _ = Sign.no_vars ctxt prop;
   509     val (strip, recover, prop') = stripped_sorts thy prop;
   510     val constraints = map (fn (TFree (_, S), T) => (T, S)) strip;
   511     val of_sorts = maps (fn (T as TFree (_, S), _) => of_sort (Thm.ctyp_of ctxt T, S)) strip;
   512 
   513     val thy' = thy
   514       |> Theory.add_axiom ctxt (b, Logic.list_implies (maps Logic.mk_of_sort constraints, prop'));
   515     val axm_name = Sign.full_name thy' b;
   516     val axm' = Thm.axiom thy' axm_name;
   517     val thm =
   518       Thm.instantiate (recover, []) axm'
   519       |> unvarify_global thy'
   520       |> fold elim_implies of_sorts;
   521   in ((axm_name, thm), thy') end;
   522 
   523 fun add_axiom_global arg thy = add_axiom (Syntax.init_pretty_global thy) arg thy;
   524 
   525 fun add_def (context as (ctxt, _)) unchecked overloaded (b, prop) thy =
   526   let
   527     val _ = Sign.no_vars ctxt prop;
   528     val prems = map (Thm.cterm_of ctxt) (Logic.strip_imp_prems prop);
   529     val (_, recover, concl') = stripped_sorts thy (Logic.strip_imp_concl prop);
   530 
   531     val thy' = Theory.add_def context unchecked overloaded (b, concl') thy;
   532     val axm_name = Sign.full_name thy' b;
   533     val axm' = Thm.axiom thy' axm_name;
   534     val thm =
   535       Thm.instantiate (recover, []) axm'
   536       |> unvarify_global thy'
   537       |> fold_rev Thm.implies_intr prems;
   538   in ((axm_name, thm), thy') end;
   539 
   540 fun add_def_global unchecked overloaded arg thy =
   541   add_def (Defs.global_context thy) unchecked overloaded arg thy;
   542 
   543 
   544 
   545 (*** theorem tags ***)
   546 
   547 (* add / delete tags *)
   548 
   549 fun tag_rule tg = Thm.map_tags (insert (op =) tg);
   550 fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
   551 
   552 
   553 (* free dummy thm -- for abstract closure *)
   554 
   555 val free_dummyN = "free_dummy";
   556 fun is_free_dummy thm = Properties.defined (Thm.get_tags thm) free_dummyN;
   557 val tag_free_dummy = tag_rule (free_dummyN, "");
   558 
   559 
   560 (* def_name *)
   561 
   562 fun def_name c = c ^ "_def";
   563 
   564 fun def_name_optional c "" = def_name c
   565   | def_name_optional _ name = name;
   566 
   567 val def_binding = Binding.map_name def_name #> Binding.reset_pos;
   568 fun def_binding_optional b name = if Binding.is_empty name then def_binding b else name;
   569 fun make_def_binding cond b = if cond then def_binding b else Binding.empty;
   570 
   571 
   572 (* unofficial theorem names *)
   573 
   574 fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
   575 
   576 val has_name_hint = can the_name_hint;
   577 val get_name_hint = the_default "??.unknown" o try the_name_hint;
   578 
   579 fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
   580 
   581 
   582 (* theorem kinds *)
   583 
   584 val theoremK = "theorem";
   585 
   586 fun legacy_get_kind thm = the_default "" (Properties.get (Thm.get_tags thm) Markup.kindN);
   587 
   588 fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
   589 
   590 
   591 
   592 (** attributes **)
   593 
   594 (*attributes subsume any kind of rules or context modifiers*)
   595 type attribute = Context.generic * thm -> Context.generic option * thm option;
   596 
   597 type binding = binding * attribute list;
   598 
   599 fun rule_attribute ths f (x, th) =
   600   (NONE,
   601     (case find_first is_free_dummy (th :: ths) of
   602       SOME th' => SOME th'
   603     | NONE => SOME (f x th)));
   604 
   605 fun declaration_attribute f (x, th) =
   606   (if is_free_dummy th then NONE else SOME (f th x), NONE);
   607 
   608 fun mixed_attribute f (x, th) =
   609   let val (x', th') = f (x, th) in (SOME x', SOME th') end;
   610 
   611 fun apply_attribute (att: attribute) th x =
   612   let val (x', th') = att (x, check_hyps x (Thm.transfer (Context.theory_of x) th))
   613   in (the_default th th', the_default x x') end;
   614 
   615 fun attribute_declaration att th x = #2 (apply_attribute att th x);
   616 
   617 fun apply_attributes mk dest =
   618   let
   619     fun app [] th x = (th, x)
   620       | app (att :: atts) th x = apply_attribute att th (mk x) ||> dest |-> app atts;
   621   in app end;
   622 
   623 val theory_attributes = apply_attributes Context.Theory Context.the_theory;
   624 val proof_attributes = apply_attributes Context.Proof Context.the_proof;
   625 
   626 fun no_attributes x = (x, []);
   627 fun simple_fact x = [(x, [])];
   628 
   629 fun tag tg = rule_attribute [] (K (tag_rule tg));
   630 fun untag s = rule_attribute [] (K (untag_rule s));
   631 fun kind k = rule_attribute [] (K (k <> "" ? kind_rule k));
   632 
   633 
   634 (* forked proofs *)
   635 
   636 structure Proofs = Theory_Data
   637 (
   638   type T = thm list;
   639   val empty = [];
   640   fun extend _ = empty;
   641   fun merge _ = empty;
   642 );
   643 
   644 fun register_proofs more_thms =
   645   Proofs.map (fold (cons o Thm.trim_context) more_thms);
   646 
   647 fun consolidate_theory thy =
   648   Thm.consolidate (map (Thm.transfer thy) (rev (Proofs.get thy)));
   649 
   650 
   651 
   652 (** print theorems **)
   653 
   654 (* options *)
   655 
   656 val show_consts_raw = Config.declare_option ("show_consts", \<^here>);
   657 val show_consts = Config.bool show_consts_raw;
   658 
   659 val show_hyps_raw = Config.declare ("show_hyps", \<^here>) (fn _ => Config.Bool false);
   660 val show_hyps = Config.bool show_hyps_raw;
   661 
   662 val show_tags_raw = Config.declare ("show_tags", \<^here>) (fn _ => Config.Bool false);
   663 val show_tags = Config.bool show_tags_raw;
   664 
   665 
   666 (* pretty_thm etc. *)
   667 
   668 fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
   669 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
   670 
   671 fun pretty_flexpair ctxt (t, u) = Pretty.block
   672   [Syntax.pretty_term ctxt t, Pretty.str " =?=", Pretty.brk 1, Syntax.pretty_term ctxt u];
   673 
   674 fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps'} raw_th =
   675   let
   676     val show_tags = Config.get ctxt show_tags;
   677     val show_hyps = Config.get ctxt show_hyps;
   678 
   679     val th = raw_th
   680       |> perhaps (try (Thm.transfer (Proof_Context.theory_of ctxt)))
   681       |> perhaps (try Thm.strip_shyps);
   682 
   683     val hyps = if show_hyps then Thm.hyps_of th else undeclared_hyps (Context.Proof ctxt) th;
   684     val extra_shyps = extra_shyps' ctxt th;
   685     val tags = Thm.get_tags th;
   686     val tpairs = Thm.tpairs_of th;
   687 
   688     val q = if quote then Pretty.quote else I;
   689     val prt_term = q o Syntax.pretty_term ctxt;
   690 
   691 
   692     val hlen = length extra_shyps + length hyps + length tpairs;
   693     val hsymbs =
   694       if hlen = 0 then []
   695       else if show_hyps orelse show_hyps' then
   696         [Pretty.brk 2, Pretty.list "[" "]"
   697           (map (q o pretty_flexpair ctxt) tpairs @ map prt_term hyps @
   698            map (Syntax.pretty_sort ctxt) extra_shyps)]
   699       else [Pretty.brk 2, Pretty.str ("[" ^ replicate_string hlen "." ^ "]")];
   700     val tsymbs =
   701       if null tags orelse not show_tags then []
   702       else [Pretty.brk 1, pretty_tags tags];
   703   in Pretty.block (prt_term (Thm.prop_of th) :: (hsymbs @ tsymbs)) end;
   704 
   705 fun pretty_thm ctxt = pretty_thm_raw ctxt {quote = false, show_hyps = true};
   706 fun pretty_thm_item ctxt th = Pretty.item [pretty_thm ctxt th];
   707 
   708 fun pretty_thm_global thy =
   709   pretty_thm_raw (Syntax.init_pretty_global thy) {quote = false, show_hyps = false};
   710 
   711 val string_of_thm = Pretty.string_of oo pretty_thm;
   712 val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
   713 
   714 
   715 open Thm;
   716 
   717 end;
   718 
   719 structure Basic_Thm: BASIC_THM = Thm;
   720 open Basic_Thm;