src/Pure/global_theory.ML
author wenzelm
Sun, 22 Jul 2012 23:31:57 +0200
changeset 49440 0d95980e9aae
parent 48241 bd24e466bef9
child 50007 0518bf89c777
permissions -rw-r--r--
parallel scheduling of jobs;
misc tuning;
wenzelm@39814
     1
(*  Title:      Pure/global_theory.ML
wenzelm@39814
     2
    Author:     Makarius
wenzelm@3987
     3
wenzelm@39814
     4
Global theory content: stored facts.
wenzelm@3987
     5
*)
wenzelm@3987
     6
wenzelm@39814
     7
signature GLOBAL_THEORY =
wenzelm@3987
     8
sig
wenzelm@27198
     9
  val facts_of: theory -> Facts.T
wenzelm@26666
    10
  val intern_fact: theory -> xstring -> string
wenzelm@26693
    11
  val defined_fact: theory -> string -> bool
wenzelm@27198
    12
  val hide_fact: bool -> string -> theory -> theory
wenzelm@45179
    13
  val begin_recent_proofs: theory -> theory
wenzelm@45179
    14
  val join_recent_proofs: theory -> unit
wenzelm@32125
    15
  val join_proofs: theory -> unit
wenzelm@26683
    16
  val get_fact: Context.generic -> theory -> Facts.ref -> thm list
wenzelm@26344
    17
  val get_thms: theory -> xstring -> thm list
wenzelm@26344
    18
  val get_thm: theory -> xstring -> thm
wenzelm@16336
    19
  val all_thms_of: theory -> (string * thm) list
wenzelm@21580
    20
  val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21567
    21
  val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
wenzelm@21580
    22
  val burrow_facts: ('a list -> 'b list) ->
wenzelm@21580
    23
    ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21580
    24
  val name_multi: string -> 'a list -> (string * 'a) list
wenzelm@33700
    25
  val name_thm: bool -> bool -> string -> thm -> thm
wenzelm@33700
    26
  val name_thms: bool -> bool -> string -> thm list -> thm list
wenzelm@33700
    27
  val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
haftmann@29579
    28
  val store_thms: binding * thm list -> theory -> thm list * theory
haftmann@29579
    29
  val store_thm: binding * thm -> theory -> thm * theory
haftmann@29579
    30
  val store_thm_open: binding * thm -> theory -> thm * theory
haftmann@29579
    31
  val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
haftmann@29579
    32
  val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
haftmann@29579
    33
  val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
haftmann@29579
    34
  val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
wenzelm@30853
    35
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
wenzelm@30853
    36
    -> theory -> (string * thm list) list * theory
haftmann@29579
    37
  val add_defs: bool -> ((binding * term) * attribute list) list ->
haftmann@18377
    38
    theory -> thm list * theory
haftmann@29579
    39
  val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
haftmann@29579
    40
    theory -> thm list * theory
wenzelm@30343
    41
  val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
haftmann@18377
    42
    theory -> thm list * theory
wenzelm@30343
    43
  val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
wenzelm@19629
    44
    theory -> thm list * theory
wenzelm@3987
    45
end;
wenzelm@3987
    46
wenzelm@39814
    47
structure Global_Theory: GLOBAL_THEORY =
wenzelm@3987
    48
struct
wenzelm@3987
    49
wenzelm@27198
    50
(** theory data **)
wenzelm@3987
    51
wenzelm@48241
    52
type proofs = thm list * unit lazy;  (*accumulated thms, persistent join*)
wenzelm@46514
    53
wenzelm@46514
    54
val empty_proofs: proofs = ([], Lazy.value ());
wenzelm@46514
    55
wenzelm@46514
    56
fun add_proofs more_thms ((thms, _): proofs) =
wenzelm@46514
    57
  let val thms' = fold cons more_thms thms
wenzelm@46514
    58
  in (thms', Lazy.lazy (fn () => Thm.join_proofs (rev thms'))) end;
wenzelm@46514
    59
wenzelm@46514
    60
fun force_proofs ((_, prfs): proofs) = Lazy.force prfs;
wenzelm@46514
    61
wenzelm@39814
    62
structure Data = Theory_Data
wenzelm@24713
    63
(
wenzelm@48241
    64
  type T = Facts.T * (proofs * proofs);  (*facts, recent proofs, all proofs of theory node*)
wenzelm@46514
    65
  val empty = (Facts.empty, (empty_proofs, empty_proofs));
wenzelm@46514
    66
  fun extend (facts, _) = (facts, snd empty);
wenzelm@46514
    67
  fun merge ((facts1, _), (facts2, _)) = (Facts.merge (facts1, facts2), snd empty);
wenzelm@24713
    68
);
wenzelm@5005
    69
wenzelm@28841
    70
wenzelm@28841
    71
(* facts *)
wenzelm@28841
    72
wenzelm@39814
    73
val facts_of = #1 o Data.get;
wenzelm@26666
    74
wenzelm@26666
    75
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    76
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    77
wenzelm@39814
    78
fun hide_fact fully name = Data.map (apfst (Facts.hide fully name));
wenzelm@28841
    79
wenzelm@28841
    80
wenzelm@46514
    81
(* forked proofs *)
wenzelm@28841
    82
wenzelm@47659
    83
fun register_proofs thms thy = (thms, Data.map (apsnd (pairself (add_proofs thms))) thy);
wenzelm@28977
    84
wenzelm@46514
    85
val begin_recent_proofs = Data.map (apsnd (apfst (K empty_proofs)));
wenzelm@46514
    86
val join_recent_proofs = force_proofs o #1 o #2 o Data.get;
wenzelm@46514
    87
val join_proofs = force_proofs o #2 o #2 o Data.get;
wenzelm@6367
    88
wenzelm@4022
    89
wenzelm@4022
    90
(** retrieve theorems **)
wenzelm@4022
    91
wenzelm@27198
    92
fun get_fact context thy xthmref =
wenzelm@26683
    93
  let
wenzelm@43249
    94
    val facts = facts_of thy;
wenzelm@26683
    95
    val xname = Facts.name_of_ref xthmref;
wenzelm@26683
    96
    val pos = Facts.pos_of_ref xthmref;
wenzelm@27198
    97
wenzelm@43342
    98
    val name =
wenzelm@43342
    99
      (case intern_fact thy xname of
wenzelm@43342
   100
        "_" => "Pure.asm_rl"
wenzelm@43342
   101
      | name => name);
wenzelm@43249
   102
    val res = Facts.lookup context facts name;
wenzelm@27198
   103
    val _ = Theory.check_thy thy;
wenzelm@26683
   104
  in
wenzelm@27198
   105
    (case res of
wenzelm@27198
   106
      NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
wenzelm@27739
   107
    | SOME (static, ths) =>
wenzelm@47878
   108
        (Context_Position.report_generic context pos (Name_Space.markup (Facts.space_of facts) name);
wenzelm@46537
   109
         if static then ()
wenzelm@47878
   110
         else Context_Position.report_generic context pos (Isabelle_Markup.dynamic_fact name);
wenzelm@27739
   111
         Facts.select xthmref (map (Thm.transfer thy) ths)))
wenzelm@26683
   112
  end;
wenzelm@26344
   113
wenzelm@26683
   114
fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
wenzelm@26344
   115
fun get_thm thy name = Facts.the_single name (get_thms thy name);
wenzelm@4783
   116
wenzelm@27198
   117
fun all_thms_of thy =
wenzelm@27865
   118
  Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
wenzelm@16336
   119
wenzelm@4022
   120
wenzelm@4022
   121
wenzelm@26488
   122
(** store theorems **)
wenzelm@3987
   123
wenzelm@21580
   124
(* fact specifications *)
wenzelm@21580
   125
wenzelm@21580
   126
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   127
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   128
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   129
wenzelm@21580
   130
wenzelm@4853
   131
(* naming *)
wenzelm@4853
   132
wenzelm@18801
   133
fun name_multi name [x] = [(name, x)]
wenzelm@26457
   134
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
   135
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
   136
wenzelm@33700
   137
fun name_thm pre official name thm = thm
wenzelm@42567
   138
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@42567
   139
      else Thm.name_derivation name)
wenzelm@42567
   140
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@42567
   141
      else Thm.put_name_hint name);
berghofe@12872
   142
wenzelm@33700
   143
fun name_thms pre official name xs =
wenzelm@33700
   144
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   145
wenzelm@33700
   146
fun name_thmss official name fact =
wenzelm@33700
   147
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   148
wenzelm@4853
   149
berghofe@11998
   150
(* enter_thms *)
wenzelm@4853
   151
haftmann@28861
   152
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   153
  if Binding.is_empty b
wenzelm@47659
   154
  then app_att thms thy |-> register_proofs
wenzelm@30215
   155
  else
wenzelm@30215
   156
    let
wenzelm@47876
   157
      val name = Sign.full_name thy b;
wenzelm@47659
   158
      val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
wenzelm@30215
   159
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@43246
   160
      val thy'' = thy'
wenzelm@47876
   161
        |> (Data.map o apfst) (Facts.add_global (Context.Theory thy') (b, thms'') #> snd);
wenzelm@30215
   162
    in (thms'', thy'') end;
wenzelm@5933
   163
wenzelm@26488
   164
wenzelm@26488
   165
(* store_thm(s) *)
wenzelm@26488
   166
wenzelm@33700
   167
fun store_thms (b, thms) =
wenzelm@47659
   168
  enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
wenzelm@28076
   169
haftmann@29579
   170
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   171
haftmann@29579
   172
fun store_thm_open (b, th) =
wenzelm@47659
   173
  enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
wenzelm@3987
   174
wenzelm@16023
   175
wenzelm@6091
   176
(* add_thms(s) *)
wenzelm@3987
   177
haftmann@29579
   178
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@47659
   179
  enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@3987
   180
haftmann@18377
   181
fun gen_add_thmss pre_name =
haftmann@18377
   182
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   183
berghofe@12235
   184
fun gen_add_thms pre_name args =
haftmann@18377
   185
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   186
wenzelm@33700
   187
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@33700
   188
val add_thms = gen_add_thms (name_thms true true);
haftmann@27683
   189
val add_thm = yield_singleton add_thms;
wenzelm@5907
   190
wenzelm@5907
   191
wenzelm@26488
   192
(* add_thms_dynamic *)
wenzelm@26488
   193
haftmann@29579
   194
fun add_thms_dynamic (b, f) thy = thy
wenzelm@47876
   195
  |> (Data.map o apfst) (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
wenzelm@26488
   196
wenzelm@26488
   197
wenzelm@27728
   198
(* note_thmss *)
wenzelm@5907
   199
wenzelm@47659
   200
fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
wenzelm@12711
   201
  let
haftmann@28965
   202
    val name = Sign.full_name thy b;
wenzelm@47659
   203
    fun app (ths, atts) =
wenzelm@47659
   204
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@47659
   205
    val (thms, thy') =
wenzelm@47659
   206
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@47659
   207
        (b, facts) thy;
wenzelm@28076
   208
  in ((name, thms), thy') end);
wenzelm@12711
   209
wenzelm@5280
   210
wenzelm@4022
   211
(* store axioms as theorems *)
wenzelm@4022
   212
wenzelm@4853
   213
local
wenzelm@35986
   214
wenzelm@35986
   215
fun no_read _ (_, t) = t;
wenzelm@35986
   216
wenzelm@43246
   217
fun read ctxt (b, str) =
wenzelm@43246
   218
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@43252
   219
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35986
   220
wenzelm@35986
   221
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35986
   222
  let
wenzelm@43246
   223
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@43246
   224
    val prop = prep ctxt (b, raw_prop);
wenzelm@43246
   225
    val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
wenzelm@35986
   226
    val thm = def
wenzelm@35986
   227
      |> Thm.forall_intr_frees
wenzelm@35986
   228
      |> Thm.forall_elim_vars 0
wenzelm@35986
   229
      |> Thm.varifyT_global;
wenzelm@35986
   230
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35986
   231
wenzelm@4853
   232
in
wenzelm@35986
   233
wenzelm@35986
   234
val add_defs = add no_read false;
wenzelm@35986
   235
val add_defs_unchecked = add no_read true;
wenzelm@35986
   236
val add_defs_cmd = add read false;
wenzelm@35986
   237
val add_defs_unchecked_cmd = add read true;
wenzelm@35986
   238
wenzelm@4853
   239
end;
wenzelm@4022
   240
wenzelm@5091
   241
end;