src/Pure/global_theory.ML
author wenzelm
Sun, 18 Mar 2012 13:51:51 +0100
changeset 47878 0dacedb4a948
parent 47876 421760a1efe7
child 48241 bd24e466bef9
permissions -rw-r--r--
tuned;
     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 facts = facts_of thy;
    95     val xname = Facts.name_of_ref xthmref;
    96     val pos = Facts.pos_of_ref xthmref;
    97 
    98     val name =
    99       (case intern_fact thy xname of
   100         "_" => "Pure.asm_rl"
   101       | name => name);
   102     val res = Facts.lookup context facts name;
   103     val _ = Theory.check_thy thy;
   104   in
   105     (case res of
   106       NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
   107     | SOME (static, ths) =>
   108         (Context_Position.report_generic context pos (Name_Space.markup (Facts.space_of facts) name);
   109          if static then ()
   110          else Context_Position.report_generic context pos (Isabelle_Markup.dynamic_fact name);
   111          Facts.select xthmref (map (Thm.transfer thy) ths)))
   112   end;
   113 
   114 fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
   115 fun get_thm thy name = Facts.the_single name (get_thms thy name);
   116 
   117 fun all_thms_of thy =
   118   Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
   119 
   120 
   121 
   122 (** store theorems **)
   123 
   124 (* fact specifications *)
   125 
   126 fun map_facts f = map (apsnd (map (apfst (map f))));
   127 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   128 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   129 
   130 
   131 (* naming *)
   132 
   133 fun name_multi name [x] = [(name, x)]
   134   | name_multi "" xs = map (pair "") xs
   135   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   136 
   137 fun name_thm pre official name thm = thm
   138   |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
   139       else Thm.name_derivation name)
   140   |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
   141       else Thm.put_name_hint name);
   142 
   143 fun name_thms pre official name xs =
   144   map (uncurry (name_thm pre official)) (name_multi name xs);
   145 
   146 fun name_thmss official name fact =
   147   burrow_fact (name_thms true official name) fact;
   148 
   149 
   150 (* enter_thms *)
   151 
   152 fun enter_thms pre_name post_name app_att (b, thms) thy =
   153   if Binding.is_empty b
   154   then app_att thms thy |-> register_proofs
   155   else
   156     let
   157       val name = Sign.full_name thy b;
   158       val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
   159       val thms'' = map (Thm.transfer thy') thms';
   160       val thy'' = thy'
   161         |> (Data.map o apfst) (Facts.add_global (Context.Theory thy') (b, thms'') #> snd);
   162     in (thms'', thy'') end;
   163 
   164 
   165 (* store_thm(s) *)
   166 
   167 fun store_thms (b, thms) =
   168   enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
   169 
   170 fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
   171 
   172 fun store_thm_open (b, th) =
   173   enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
   174 
   175 
   176 (* add_thms(s) *)
   177 
   178 fun add_thms_atts pre_name ((b, thms), atts) =
   179   enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
   180 
   181 fun gen_add_thmss pre_name =
   182   fold_map (add_thms_atts pre_name);
   183 
   184 fun gen_add_thms pre_name args =
   185   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   186 
   187 val add_thmss = gen_add_thmss (name_thms true true);
   188 val add_thms = gen_add_thms (name_thms true true);
   189 val add_thm = yield_singleton add_thms;
   190 
   191 
   192 (* add_thms_dynamic *)
   193 
   194 fun add_thms_dynamic (b, f) thy = thy
   195   |> (Data.map o apfst) (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
   196 
   197 
   198 (* note_thmss *)
   199 
   200 fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
   201   let
   202     val name = Sign.full_name thy b;
   203     fun app (ths, atts) =
   204       fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
   205     val (thms, thy') =
   206       enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
   207         (b, facts) thy;
   208   in ((name, thms), thy') end);
   209 
   210 
   211 (* store axioms as theorems *)
   212 
   213 local
   214 
   215 fun no_read _ (_, t) = t;
   216 
   217 fun read ctxt (b, str) =
   218   Syntax.read_prop ctxt str handle ERROR msg =>
   219     cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
   220 
   221 fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
   222   let
   223     val ctxt = Syntax.init_pretty_global thy;
   224     val prop = prep ctxt (b, raw_prop);
   225     val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
   226     val thm = def
   227       |> Thm.forall_intr_frees
   228       |> Thm.forall_elim_vars 0
   229       |> Thm.varifyT_global;
   230   in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
   231 
   232 in
   233 
   234 val add_defs = add no_read false;
   235 val add_defs_unchecked = add no_read true;
   236 val add_defs_cmd = add read false;
   237 val add_defs_unchecked_cmd = add read true;
   238 
   239 end;
   240 
   241 end;