src/Pure/global_theory.ML
author wenzelm
Fri Jun 29 15:54:41 2018 +0200 (17 months ago ago)
changeset 68540 000a0e062529
parent 68245 e0cd57aeb60c
child 68559 7aae213d9e69
permissions -rw-r--r--
disallow pending hyps;
disallow pending shyps, with option to override the check;
tuned message;
     1 (*  Title:      Pure/global_theory.ML
     2     Author:     Makarius
     3 
     4 Global theory content: stored facts.
     5 *)
     6 
     7 signature GLOBAL_THEORY =
     8 sig
     9   val facts_of: theory -> Facts.T
    10   val check_fact: theory -> xstring * Position.T -> string
    11   val intern_fact: theory -> xstring -> string
    12   val defined_fact: theory -> string -> bool
    13   val alias_fact: binding -> string -> theory -> theory
    14   val hide_fact: bool -> string -> theory -> theory
    15   val get_thms: theory -> xstring -> thm list
    16   val get_thm: theory -> xstring -> thm
    17   val transfer_theories: theory -> thm -> thm
    18   val all_thms_of: theory -> bool -> (string * thm) list
    19   val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    20   val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
    21   val burrow_facts: ('a list -> 'b list) ->
    22     ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    23   val name_multi: string -> 'a list -> (string * 'a) list
    24   val name_thm: bool -> bool -> string -> thm -> thm
    25   val name_thms: bool -> bool -> string -> thm list -> thm list
    26   val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
    27   val pending_shyps_raw: Config.raw
    28   val pending_shyps: bool Config.T
    29   val add_thms_lazy: string -> (binding * thm list lazy) -> theory -> theory
    30   val store_thm: binding * thm -> theory -> thm * theory
    31   val store_thm_open: binding * thm -> theory -> thm * theory
    32   val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
    33   val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
    34   val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
    35   val add_thms_dynamic': Context.generic -> binding * (Context.generic -> thm list) ->
    36     theory -> string * theory
    37   val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
    38   val note_thms: string -> Thm.binding * (thm list * attribute list) list -> theory ->
    39     (string * thm list) * theory
    40   val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list -> theory ->
    41     (string * thm list) list * theory
    42   val add_defs: bool -> ((binding * term) * attribute list) list ->
    43     theory -> thm list * theory
    44   val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
    45     theory -> thm list * theory
    46 end;
    47 
    48 structure Global_Theory: GLOBAL_THEORY =
    49 struct
    50 
    51 (** theory data **)
    52 
    53 structure Data = Theory_Data
    54 (
    55   type T = Facts.T;
    56   val empty = Facts.empty;
    57   val extend = I;
    58   val merge = Facts.merge;
    59 );
    60 
    61 val facts_of = Data.get;
    62 
    63 fun check_fact thy = Facts.check (Context.Theory thy) (facts_of thy);
    64 val intern_fact = Facts.intern o facts_of;
    65 val defined_fact = Facts.defined o facts_of;
    66 
    67 fun alias_fact binding name thy =
    68   Data.map (Facts.alias (Sign.naming_of thy) binding name) thy;
    69 
    70 fun hide_fact fully name = Data.map (Facts.hide fully name);
    71 
    72 
    73 (* retrieve theorems *)
    74 
    75 fun get_thms thy xname =
    76   #thms (Facts.retrieve (Context.Theory thy) (facts_of thy) (xname, Position.none));
    77 
    78 fun get_thm thy xname =
    79   Facts.the_single (xname, Position.none) (get_thms thy xname);
    80 
    81 fun transfer_theories thy =
    82   let
    83     val theories =
    84       fold (fn thy' => Symtab.update (Context.theory_name thy', thy'))
    85         (Theory.nodes_of thy) Symtab.empty;
    86     fun transfer th =
    87       Thm.transfer (the_default thy (Symtab.lookup theories (Thm.theory_name th))) th;
    88   in transfer end;
    89 
    90 fun all_thms_of thy verbose =
    91   let
    92     val transfer = transfer_theories thy;
    93     val facts = facts_of thy;
    94     fun add (name, ths) =
    95       if not verbose andalso Facts.is_concealed facts name then I
    96       else append (map (`(Thm.get_name_hint) o transfer) ths);
    97   in Facts.fold_static add facts [] end;
    98 
    99 
   100 
   101 (** store theorems **)
   102 
   103 (* fact specifications *)
   104 
   105 fun map_facts f = map (apsnd (map (apfst (map f))));
   106 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   107 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   108 
   109 
   110 (* naming *)
   111 
   112 fun name_multi name [x] = [(name, x)]
   113   | name_multi "" xs = map (pair "") xs
   114   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   115 
   116 fun name_thm pre official name thm = thm
   117   |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
   118       else Thm.name_derivation name)
   119   |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
   120       else Thm.put_name_hint name);
   121 
   122 fun name_thms pre official name thms =
   123   map (uncurry (name_thm pre official)) (name_multi name thms);
   124 
   125 fun name_thmss official name fact =
   126   burrow_fact (name_thms true official name) fact;
   127 
   128 
   129 (* enter_thms *)
   130 
   131 fun register_proofs thms thy = (thms, Thm.register_proofs (Lazy.value thms) thy);
   132 
   133 val pending_shyps_raw = Config.declare ("pending_shyps", \<^here>) (K (Config.Bool false));
   134 val pending_shyps = Config.bool pending_shyps_raw;
   135 
   136 fun add_facts (b, fact) thy =
   137   let
   138     val full_name = Sign.full_name thy b;
   139     val pos = Binding.pos_of b;
   140     fun check fact =
   141       fact |> map_index (fn (i, thm) =>
   142         let
   143           fun err msg =
   144             error ("Malformed global fact " ^
   145               quote (full_name ^
   146                 (if length fact = 1 then "" else "(" ^ string_of_int (i + 1) ^ ")")) ^
   147               Position.here pos ^ "\n" ^ msg);
   148           val prop = Thm.plain_prop_of thm
   149             handle THM _ =>
   150               thm
   151               |> not (Config.get_global thy pending_shyps) ?
   152                   Thm.check_shyps (Proof_Context.init_global thy)
   153               |> Thm.check_hyps (Context.Theory thy)
   154               |> Thm.full_prop_of;
   155         in
   156           ignore (Logic.unvarify_global (Term_Subst.zero_var_indexes prop))
   157             handle TYPE (msg, _, _) => err msg
   158               | TERM (msg, _) => err msg
   159               | ERROR msg => err msg
   160         end);
   161     val arg = (b, Lazy.map_finished (tap check) fact);
   162   in
   163     thy |> Data.map (Facts.add_static (Context.Theory thy) {strict = true, index = false} arg #> #2)
   164   end;
   165 
   166 fun add_thms_lazy kind (b, thms) thy =
   167   if Binding.is_empty b then Thm.register_proofs thms thy
   168   else
   169     let
   170       val name = Sign.full_name thy b;
   171       val thms' = thms
   172         |> Lazy.map_finished (name_thms true true name #> map (Thm.kind_rule kind));
   173     in thy |> Thm.register_proofs thms' |> add_facts (b, thms') end;
   174 
   175 fun enter_thms pre_name post_name app_att (b, thms) thy =
   176   if Binding.is_empty b then app_att thms thy |-> register_proofs
   177   else
   178     let
   179       val name = Sign.full_name thy b;
   180       val (thms', thy') =
   181         app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
   182       val thy'' = thy' |> add_facts (b, Lazy.value thms');
   183     in (map (Thm.transfer thy'') thms', thy'') end;
   184 
   185 
   186 (* store_thm *)
   187 
   188 fun store_thm (b, th) =
   189   enter_thms (name_thms true true) (name_thms false true) pair (b, [th]) #>> the_single;
   190 
   191 fun store_thm_open (b, th) =
   192   enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
   193 
   194 
   195 (* add_thms(s) *)
   196 
   197 fun add_thms_atts pre_name ((b, thms), atts) =
   198   enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
   199 
   200 fun gen_add_thmss pre_name =
   201   fold_map (add_thms_atts pre_name);
   202 
   203 fun gen_add_thms pre_name args =
   204   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   205 
   206 val add_thmss = gen_add_thmss (name_thms true true);
   207 val add_thms = gen_add_thms (name_thms true true);
   208 val add_thm = yield_singleton add_thms;
   209 
   210 
   211 (* dynamic theorems *)
   212 
   213 fun add_thms_dynamic' context arg thy =
   214   let val (name, facts') = Facts.add_dynamic context arg (Data.get thy)
   215   in (name, Data.put facts' thy) end;
   216 
   217 fun add_thms_dynamic arg thy =
   218   add_thms_dynamic' (Context.Theory thy) arg thy |> snd;
   219 
   220 
   221 (* note_thmss *)
   222 
   223 fun note_thms kind ((b, more_atts), facts) thy =
   224   let
   225     val name = Sign.full_name thy b;
   226     fun app (ths, atts) =
   227       fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
   228     val (thms, thy') =
   229       enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
   230         (b, facts) thy;
   231   in ((name, thms), thy') end;
   232 
   233 val note_thmss = fold_map o note_thms;
   234 
   235 
   236 (* old-style defs *)
   237 
   238 local
   239 
   240 fun add unchecked overloaded = fold_map (fn ((b, prop), atts) => fn thy =>
   241   let
   242     val context = Defs.global_context thy;
   243     val ((_, def), thy') = Thm.add_def context unchecked overloaded (b, prop) thy;
   244     val thm = def
   245       |> Thm.forall_intr_frees
   246       |> Thm.forall_elim_vars 0
   247       |> Thm.varifyT_global;
   248   in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
   249 
   250 in
   251 
   252 val add_defs = add false;
   253 val add_defs_unchecked = add true;
   254 
   255 end;
   256 
   257 end;