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