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