src/Pure/global_theory.ML
author wenzelm
Sun, 18 Mar 2012 13:04:22 +0100
changeset 47876 421760a1efe7
parent 47659 6287653e63ec
child 47878 0dacedb4a948
permissions -rw-r--r--
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
more explicit Context.generic for Name_Space.declare/define and derivatives (NB: naming changed after Proof_Context.init_global);
prefer Context.pretty in low-level operations of structure Sorts/Type (defer full Syntax.init_pretty until error output);
simplified signatures;
     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 intern_fact: theory -> xstring -> string
    11   val defined_fact: theory -> string -> bool
    12   val hide_fact: bool -> string -> theory -> theory
    13   val begin_recent_proofs: theory -> theory
    14   val join_recent_proofs: theory -> unit
    15   val join_proofs: theory -> unit
    16   val get_fact: Context.generic -> theory -> Facts.ref -> thm list
    17   val get_thms: theory -> xstring -> thm list
    18   val get_thm: theory -> xstring -> thm
    19   val all_thms_of: theory -> (string * thm) list
    20   val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    21   val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
    22   val burrow_facts: ('a list -> 'b list) ->
    23     ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    24   val name_multi: string -> 'a list -> (string * 'a) list
    25   val name_thm: bool -> bool -> string -> thm -> thm
    26   val name_thms: bool -> bool -> string -> thm list -> thm list
    27   val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
    28   val store_thms: binding * thm list -> theory -> thm list * theory
    29   val store_thm: binding * thm -> theory -> thm * theory
    30   val store_thm_open: binding * thm -> theory -> thm * theory
    31   val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
    32   val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
    33   val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
    34   val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
    35   val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
    36     -> theory -> (string * thm list) list * theory
    37   val add_defs: bool -> ((binding * term) * attribute list) list ->
    38     theory -> thm list * theory
    39   val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
    40     theory -> thm list * theory
    41   val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
    42     theory -> thm list * theory
    43   val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
    44     theory -> thm list * theory
    45 end;
    46 
    47 structure Global_Theory: GLOBAL_THEORY =
    48 struct
    49 
    50 (** theory data **)
    51 
    52 type proofs = thm list * unit lazy;
    53 
    54 val empty_proofs: proofs = ([], Lazy.value ());
    55 
    56 fun add_proofs more_thms ((thms, _): proofs) =
    57   let val thms' = fold cons more_thms thms
    58   in (thms', Lazy.lazy (fn () => Thm.join_proofs (rev thms'))) end;
    59 
    60 fun force_proofs ((_, prfs): proofs) = Lazy.force prfs;
    61 
    62 structure Data = Theory_Data
    63 (
    64   type T = Facts.T * (proofs * proofs);
    65   val empty = (Facts.empty, (empty_proofs, empty_proofs));
    66   fun extend (facts, _) = (facts, snd empty);
    67   fun merge ((facts1, _), (facts2, _)) = (Facts.merge (facts1, facts2), snd empty);
    68 );
    69 
    70 
    71 (* facts *)
    72 
    73 val facts_of = #1 o Data.get;
    74 
    75 val intern_fact = Facts.intern o facts_of;
    76 val defined_fact = Facts.defined o facts_of;
    77 
    78 fun hide_fact fully name = Data.map (apfst (Facts.hide fully name));
    79 
    80 
    81 (* forked proofs *)
    82 
    83 fun register_proofs thms thy = (thms, Data.map (apsnd (pairself (add_proofs thms))) thy);
    84 
    85 val begin_recent_proofs = Data.map (apsnd (apfst (K empty_proofs)));
    86 val join_recent_proofs = force_proofs o #1 o #2 o Data.get;
    87 val join_proofs = force_proofs o #2 o #2 o Data.get;
    88 
    89 
    90 (** retrieve theorems **)
    91 
    92 fun get_fact context thy xthmref =
    93   let
    94     val ctxt = Context.proof_of context;
    95 
    96     val facts = facts_of thy;
    97     val xname = Facts.name_of_ref xthmref;
    98     val pos = Facts.pos_of_ref xthmref;
    99 
   100     val name =
   101       (case intern_fact thy xname of
   102         "_" => "Pure.asm_rl"
   103       | name => name);
   104     val res = Facts.lookup context facts name;
   105     val _ = Theory.check_thy thy;
   106   in
   107     (case res of
   108       NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
   109     | SOME (static, ths) =>
   110         (Context_Position.report ctxt pos (Name_Space.markup (Facts.space_of facts) name);
   111          if static then ()
   112          else Context_Position.report ctxt pos (Isabelle_Markup.dynamic_fact name);
   113          Facts.select xthmref (map (Thm.transfer thy) ths)))
   114   end;
   115 
   116 fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
   117 fun get_thm thy name = Facts.the_single name (get_thms thy name);
   118 
   119 fun all_thms_of thy =
   120   Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
   121 
   122 
   123 
   124 (** store theorems **)
   125 
   126 (* fact specifications *)
   127 
   128 fun map_facts f = map (apsnd (map (apfst (map f))));
   129 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   130 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   131 
   132 
   133 (* naming *)
   134 
   135 fun name_multi name [x] = [(name, x)]
   136   | name_multi "" xs = map (pair "") xs
   137   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   138 
   139 fun name_thm pre official name thm = thm
   140   |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
   141       else Thm.name_derivation name)
   142   |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
   143       else Thm.put_name_hint name);
   144 
   145 fun name_thms pre official name xs =
   146   map (uncurry (name_thm pre official)) (name_multi name xs);
   147 
   148 fun name_thmss official name fact =
   149   burrow_fact (name_thms true official name) fact;
   150 
   151 
   152 (* enter_thms *)
   153 
   154 fun enter_thms pre_name post_name app_att (b, thms) thy =
   155   if Binding.is_empty b
   156   then app_att thms thy |-> register_proofs
   157   else
   158     let
   159       val name = Sign.full_name thy b;
   160       val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
   161       val thms'' = map (Thm.transfer thy') thms';
   162       val thy'' = thy'
   163         |> (Data.map o apfst) (Facts.add_global (Context.Theory thy') (b, thms'') #> snd);
   164     in (thms'', thy'') end;
   165 
   166 
   167 (* store_thm(s) *)
   168 
   169 fun store_thms (b, thms) =
   170   enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
   171 
   172 fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
   173 
   174 fun store_thm_open (b, th) =
   175   enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
   176 
   177 
   178 (* add_thms(s) *)
   179 
   180 fun add_thms_atts pre_name ((b, thms), atts) =
   181   enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
   182 
   183 fun gen_add_thmss pre_name =
   184   fold_map (add_thms_atts pre_name);
   185 
   186 fun gen_add_thms pre_name args =
   187   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   188 
   189 val add_thmss = gen_add_thmss (name_thms true true);
   190 val add_thms = gen_add_thms (name_thms true true);
   191 val add_thm = yield_singleton add_thms;
   192 
   193 
   194 (* add_thms_dynamic *)
   195 
   196 fun add_thms_dynamic (b, f) thy = thy
   197   |> (Data.map o apfst) (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
   198 
   199 
   200 (* note_thmss *)
   201 
   202 fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
   203   let
   204     val name = Sign.full_name thy b;
   205     fun app (ths, atts) =
   206       fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
   207     val (thms, thy') =
   208       enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
   209         (b, facts) thy;
   210   in ((name, thms), thy') end);
   211 
   212 
   213 (* store axioms as theorems *)
   214 
   215 local
   216 
   217 fun no_read _ (_, t) = t;
   218 
   219 fun read ctxt (b, str) =
   220   Syntax.read_prop ctxt str handle ERROR msg =>
   221     cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
   222 
   223 fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
   224   let
   225     val ctxt = Syntax.init_pretty_global thy;
   226     val prop = prep ctxt (b, raw_prop);
   227     val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
   228     val thm = def
   229       |> Thm.forall_intr_frees
   230       |> Thm.forall_elim_vars 0
   231       |> Thm.varifyT_global;
   232   in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
   233 
   234 in
   235 
   236 val add_defs = add no_read false;
   237 val add_defs_unchecked = add no_read true;
   238 val add_defs_cmd = add read false;
   239 val add_defs_unchecked_cmd = add read true;
   240 
   241 end;
   242 
   243 end;