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