src/Pure/global_theory.ML
author wenzelm
Mon Mar 10 15:04:01 2014 +0100 (2014-03-10)
changeset 56026 893fe12639bc
parent 56003 eccac152ffb4
child 56140 ed92ce2ac88e
permissions -rw-r--r--
tuned signature -- prefer Name_Space.get with its builtin error;
     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 hide_fact: bool -> string -> theory -> theory
    14   val get_fact: Context.generic -> theory -> Facts.ref -> thm list
    15   val get_thms: theory -> xstring -> thm list
    16   val get_thm: theory -> xstring -> thm
    17   val all_thms_of: theory -> (string * thm) list
    18   val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    19   val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
    20   val burrow_facts: ('a list -> 'b list) ->
    21     ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    22   val name_multi: string -> 'a list -> (string * 'a) list
    23   val name_thm: bool -> bool -> string -> thm -> thm
    24   val name_thms: bool -> bool -> string -> thm list -> thm list
    25   val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
    26   val store_thms: binding * thm list -> theory -> thm list * theory
    27   val store_thm: binding * thm -> theory -> thm * theory
    28   val store_thm_open: binding * thm -> theory -> thm * theory
    29   val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
    30   val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
    31   val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
    32   val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
    33   val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
    34     -> theory -> (string * thm list) list * theory
    35   val add_defs: bool -> ((binding * term) * attribute list) list ->
    36     theory -> thm list * theory
    37   val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
    38     theory -> thm list * theory
    39   val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
    40     theory -> thm list * theory
    41   val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
    42     theory -> thm list * theory
    43 end;
    44 
    45 structure Global_Theory: GLOBAL_THEORY =
    46 struct
    47 
    48 (** theory data **)
    49 
    50 structure Data = Theory_Data
    51 (
    52   type T = Facts.T;
    53   val empty = Facts.empty;
    54   val extend = I;
    55   val merge = Facts.merge;
    56 );
    57 
    58 val facts_of = Data.get;
    59 
    60 fun check_fact thy = Facts.check (Context.Theory thy) (facts_of thy);
    61 val intern_fact = Facts.intern o facts_of;
    62 val defined_fact = Facts.defined o facts_of;
    63 
    64 fun hide_fact fully name = Data.map (Facts.hide fully name);
    65 
    66 
    67 (** retrieve theorems **)
    68 
    69 fun get_fact context thy xthmref =
    70   let
    71     val facts = facts_of thy;
    72     val xname = Facts.name_of_ref xthmref;
    73     val pos = Facts.pos_of_ref xthmref;
    74 
    75     val name =
    76       (case intern_fact thy xname of
    77         "_" => "Pure.asm_rl"
    78       | name => name);
    79     val res = Facts.lookup context facts name;
    80   in
    81     (case res of
    82       NONE => error ("Unknown fact " ^ quote name ^ Position.here pos)
    83     | SOME (static, ths) =>
    84         (Context_Position.report_generic context pos (Name_Space.markup (Facts.space_of facts) name);
    85          if static then ()
    86          else Context_Position.report_generic context pos (Markup.dynamic_fact name);
    87          Facts.select xthmref (map (Thm.transfer thy) ths)))
    88   end;
    89 
    90 fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
    91 fun get_thm thy name = Facts.the_single (name, Position.none) (get_thms thy name);
    92 
    93 fun all_thms_of thy =
    94   Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
    95 
    96 
    97 
    98 (** store theorems **)
    99 
   100 (* fact specifications *)
   101 
   102 fun map_facts f = map (apsnd (map (apfst (map f))));
   103 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   104 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   105 
   106 
   107 (* naming *)
   108 
   109 fun name_multi name [x] = [(name, x)]
   110   | name_multi "" xs = map (pair "") xs
   111   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   112 
   113 fun name_thm pre official name thm = thm
   114   |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
   115       else Thm.name_derivation name)
   116   |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
   117       else Thm.put_name_hint name);
   118 
   119 fun name_thms pre official name xs =
   120   map (uncurry (name_thm pre official)) (name_multi name xs);
   121 
   122 fun name_thmss official name fact =
   123   burrow_fact (name_thms true official name) fact;
   124 
   125 
   126 (* enter_thms *)
   127 
   128 fun register_proofs thms thy = (thms, Thm.register_proofs thms thy);
   129 
   130 fun enter_thms pre_name post_name app_att (b, thms) thy =
   131   if Binding.is_empty b
   132   then app_att thms thy |-> register_proofs
   133   else
   134     let
   135       val name = Sign.full_name thy b;
   136       val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
   137       val thms'' = map (Thm.transfer thy') thms';
   138       val thy'' = thy' |> Data.map
   139         (Facts.add_static (Context.Theory thy') {strict = true, index = false} (b, thms'') #> snd);
   140     in (thms'', thy'') end;
   141 
   142 
   143 (* store_thm(s) *)
   144 
   145 fun store_thms (b, thms) =
   146   enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
   147 
   148 fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
   149 
   150 fun store_thm_open (b, th) =
   151   enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
   152 
   153 
   154 (* add_thms(s) *)
   155 
   156 fun add_thms_atts pre_name ((b, thms), atts) =
   157   enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
   158 
   159 fun gen_add_thmss pre_name =
   160   fold_map (add_thms_atts pre_name);
   161 
   162 fun gen_add_thms pre_name args =
   163   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   164 
   165 val add_thmss = gen_add_thmss (name_thms true true);
   166 val add_thms = gen_add_thms (name_thms true true);
   167 val add_thm = yield_singleton add_thms;
   168 
   169 
   170 (* add_thms_dynamic *)
   171 
   172 fun add_thms_dynamic (b, f) thy = thy
   173   |> Data.map (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
   174 
   175 
   176 (* note_thmss *)
   177 
   178 fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
   179   let
   180     val name = Sign.full_name thy b;
   181     fun app (ths, atts) =
   182       fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
   183     val (thms, thy') =
   184       enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
   185         (b, facts) thy;
   186   in ((name, thms), thy') end);
   187 
   188 
   189 (* store axioms as theorems *)
   190 
   191 local
   192 
   193 fun no_read _ (_, t) = t;
   194 
   195 fun read ctxt (b, str) =
   196   Syntax.read_prop ctxt str handle ERROR msg =>
   197     cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
   198 
   199 fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
   200   let
   201     val ctxt = Syntax.init_pretty_global thy;
   202     val prop = prep ctxt (b, raw_prop);
   203     val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
   204     val thm = def
   205       |> Thm.forall_intr_frees
   206       |> Thm.forall_elim_vars 0
   207       |> Thm.varifyT_global;
   208   in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
   209 
   210 in
   211 
   212 val add_defs = add no_read false;
   213 val add_defs_unchecked = add no_read true;
   214 val add_defs_cmd = add read false;
   215 val add_defs_unchecked_cmd = add read true;
   216 
   217 end;
   218 
   219 end;