src/Pure/global_theory.ML
author wenzelm
Fri Aug 19 23:25:47 2011 +0200 (2011-08-19)
changeset 44304 7ee000ce5390
parent 42471 593289343c7d
child 45643 9e49cfe7015d
permissions -rw-r--r--
maintain recent future proofs at transaction boundaries;
wenzelm@39557
     1
(*  Title:      Pure/global_theory.ML
wenzelm@39557
     2
    Author:     Makarius
wenzelm@3987
     3
wenzelm@39557
     4
Global theory content: stored facts.
wenzelm@3987
     5
*)
wenzelm@3987
     6
wenzelm@39557
     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@44304
    13
  val begin_recent_proofs: theory -> theory
wenzelm@44304
    14
  val join_recent_proofs: theory -> unit
wenzelm@32105
    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@30337
    41
  val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
haftmann@18377
    42
    theory -> thm list * theory
wenzelm@30337
    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@39557
    47
structure Global_Theory: GLOBAL_THEORY =
wenzelm@3987
    48
struct
wenzelm@3987
    49
wenzelm@27198
    50
(** theory data **)
wenzelm@26282
    51
wenzelm@39557
    52
structure Data = Theory_Data
wenzelm@24713
    53
(
wenzelm@44304
    54
  type T = Facts.T * (thm list * thm list);
wenzelm@44304
    55
  val empty = (Facts.empty, ([], []));
wenzelm@44304
    56
  fun extend (facts, _) = (facts, ([], []));
wenzelm@44304
    57
  fun merge ((facts1, _), (facts2, _)) = (Facts.merge (facts1, facts2), ([], []));
wenzelm@24713
    58
);
wenzelm@3987
    59
wenzelm@28841
    60
wenzelm@28841
    61
(* facts *)
wenzelm@28841
    62
wenzelm@39557
    63
val facts_of = #1 o Data.get;
wenzelm@26666
    64
wenzelm@26666
    65
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    66
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    67
wenzelm@39557
    68
fun hide_fact fully name = Data.map (apfst (Facts.hide fully name));
wenzelm@28841
    69
wenzelm@28841
    70
wenzelm@28841
    71
(* proofs *)
wenzelm@28841
    72
wenzelm@44304
    73
fun register_proofs (thy, thms) = (Data.map (apsnd (pairself (append thms))) thy, thms);
wenzelm@28977
    74
wenzelm@44304
    75
val begin_recent_proofs = Data.map (apsnd (fn (_, thms) => ([], thms)));
wenzelm@44304
    76
val join_recent_proofs = Thm.join_proofs o rev o #1 o #2 o Data.get;
wenzelm@44304
    77
val join_proofs = Thm.join_proofs o rev o #2 o #2 o Data.get;
wenzelm@6367
    78
wenzelm@3987
    79
wenzelm@4022
    80
(** retrieve theorems **)
wenzelm@3987
    81
wenzelm@27198
    82
fun get_fact context thy xthmref =
wenzelm@26683
    83
  let
wenzelm@42378
    84
    val ctxt = Context.proof_of context;
wenzelm@42378
    85
wenzelm@42378
    86
    val facts = facts_of thy;
wenzelm@26683
    87
    val xname = Facts.name_of_ref xthmref;
wenzelm@26683
    88
    val pos = Facts.pos_of_ref xthmref;
wenzelm@27198
    89
wenzelm@42471
    90
    val name =
wenzelm@42471
    91
      (case intern_fact thy xname of
wenzelm@42471
    92
        "_" => "Pure.asm_rl"
wenzelm@42471
    93
      | name => name);
wenzelm@42378
    94
    val res = Facts.lookup context facts name;
wenzelm@27198
    95
    val _ = Theory.check_thy thy;
wenzelm@26683
    96
  in
wenzelm@27198
    97
    (case res of
wenzelm@27198
    98
      NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
wenzelm@27739
    99
    | SOME (static, ths) =>
wenzelm@42379
   100
        (Context_Position.report ctxt pos (Name_Space.markup (Facts.space_of facts) name);
wenzelm@42378
   101
         if static then () else Context_Position.report ctxt pos (Markup.dynamic_fact name);
wenzelm@27739
   102
         Facts.select xthmref (map (Thm.transfer thy) ths)))
wenzelm@26683
   103
  end;
wenzelm@26344
   104
wenzelm@26683
   105
fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
wenzelm@26344
   106
fun get_thm thy name = Facts.the_single name (get_thms thy name);
wenzelm@4783
   107
wenzelm@27198
   108
fun all_thms_of thy =
wenzelm@27865
   109
  Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
wenzelm@16336
   110
wenzelm@4022
   111
wenzelm@4022
   112
wenzelm@26488
   113
(** store theorems **)
wenzelm@3987
   114
wenzelm@21580
   115
(* fact specifications *)
wenzelm@21580
   116
wenzelm@21580
   117
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   118
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   119
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   120
wenzelm@21580
   121
wenzelm@4853
   122
(* naming *)
wenzelm@4853
   123
wenzelm@18801
   124
fun name_multi name [x] = [(name, x)]
wenzelm@26457
   125
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
   126
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
   127
wenzelm@33700
   128
fun name_thm pre official name thm = thm
wenzelm@41696
   129
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@41696
   130
      else Thm.name_derivation name)
wenzelm@41696
   131
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@41696
   132
      else Thm.put_name_hint name);
berghofe@12872
   133
wenzelm@33700
   134
fun name_thms pre official name xs =
wenzelm@33700
   135
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   136
wenzelm@33700
   137
fun name_thmss official name fact =
wenzelm@33700
   138
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   139
wenzelm@4853
   140
berghofe@11998
   141
(* enter_thms *)
wenzelm@4853
   142
haftmann@28861
   143
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   144
  if Binding.is_empty b
wenzelm@32105
   145
  then swap (register_proofs (app_att (thy, thms)))
wenzelm@30211
   146
  else
wenzelm@30211
   147
    let
wenzelm@30211
   148
      val naming = Sign.naming_of thy;
wenzelm@33095
   149
      val name = Name_Space.full_name naming b;
wenzelm@30211
   150
      val (thy', thms') =
wenzelm@32105
   151
        register_proofs (apsnd (post_name name) (app_att (thy, pre_name name thms)));
wenzelm@30211
   152
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@42375
   153
      val thy'' = thy'
wenzelm@42375
   154
        |> (Data.map o apfst)
wenzelm@42375
   155
            (Facts.add_global (Proof_Context.init_global thy') naming (b, thms'') #> snd);
wenzelm@30211
   156
    in (thms'', thy'') end;
wenzelm@26488
   157
wenzelm@26488
   158
wenzelm@26488
   159
(* store_thm(s) *)
wenzelm@26488
   160
wenzelm@33700
   161
fun store_thms (b, thms) =
wenzelm@33700
   162
  enter_thms (name_thms true true) (name_thms false true) I (b, thms);
wenzelm@28076
   163
haftmann@29579
   164
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   165
haftmann@29579
   166
fun store_thm_open (b, th) =
wenzelm@33700
   167
  enter_thms (name_thms true false) (name_thms false false) I (b, [th]) #>> the_single;
wenzelm@3987
   168
wenzelm@16023
   169
wenzelm@6091
   170
(* add_thms(s) *)
wenzelm@4853
   171
haftmann@29579
   172
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@33700
   173
  enter_thms pre_name (name_thms false true)
wenzelm@30190
   174
    (Library.foldl_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@4853
   175
haftmann@18377
   176
fun gen_add_thmss pre_name =
haftmann@18377
   177
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   178
berghofe@12235
   179
fun gen_add_thms pre_name args =
haftmann@18377
   180
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   181
wenzelm@33700
   182
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@33700
   183
val add_thms = gen_add_thms (name_thms true true);
haftmann@27683
   184
val add_thm = yield_singleton add_thms;
wenzelm@5907
   185
wenzelm@5907
   186
wenzelm@26488
   187
(* add_thms_dynamic *)
wenzelm@26488
   188
haftmann@29579
   189
fun add_thms_dynamic (b, f) thy = thy
wenzelm@39557
   190
  |> (Data.map o apfst)
wenzelm@42375
   191
      (Facts.add_dynamic (Proof_Context.init_global thy) (Sign.naming_of thy) (b, f) #> snd);
wenzelm@26488
   192
wenzelm@26488
   193
wenzelm@27728
   194
(* note_thmss *)
wenzelm@5907
   195
wenzelm@33167
   196
fun note_thmss kind = fold_map (fn ((b, more_atts), ths_atts) => fn thy =>
wenzelm@12711
   197
  let
haftmann@28965
   198
    val name = Sign.full_name thy b;
wenzelm@30190
   199
    fun app (x, (ths, atts)) = Library.foldl_map (Thm.theory_attributes atts) (x, ths);
haftmann@18418
   200
    val (thms, thy') = thy |> enter_thms
wenzelm@33700
   201
      (name_thmss true) (name_thms false true) (apsnd flat o Library.foldl_map app)
wenzelm@33167
   202
      (b, map (fn (ths, atts) => (ths, surround (Thm.kind kind) (atts @ more_atts))) ths_atts);
wenzelm@28076
   203
  in ((name, thms), thy') end);
wenzelm@12711
   204
wenzelm@5280
   205
wenzelm@4022
   206
(* store axioms as theorems *)
wenzelm@4022
   207
wenzelm@4853
   208
local
wenzelm@35985
   209
wenzelm@35985
   210
fun no_read _ (_, t) = t;
wenzelm@35985
   211
wenzelm@42375
   212
fun read ctxt (b, str) =
wenzelm@42375
   213
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@42381
   214
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35985
   215
wenzelm@35985
   216
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   217
  let
wenzelm@42375
   218
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@42375
   219
    val prop = prep ctxt (b, raw_prop);
wenzelm@42375
   220
    val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
wenzelm@35985
   221
    val thm = def
wenzelm@35985
   222
      |> Thm.forall_intr_frees
wenzelm@35985
   223
      |> Thm.forall_elim_vars 0
wenzelm@35985
   224
      |> Thm.varifyT_global;
wenzelm@35985
   225
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   226
wenzelm@4853
   227
in
wenzelm@35985
   228
wenzelm@35985
   229
val add_defs = add no_read false;
wenzelm@35985
   230
val add_defs_unchecked = add no_read true;
wenzelm@35985
   231
val add_defs_cmd = add read false;
wenzelm@35985
   232
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   233
wenzelm@4853
   234
end;
wenzelm@4022
   235
wenzelm@3987
   236
end;