src/Pure/global_theory.ML
author wenzelm
Wed Apr 04 17:14:19 2012 +0200 (2012-04-04)
changeset 47337 bd24e466bef9
parent 47007 0dacedb4a948
child 48992 0518bf89c777
permissions -rw-r--r--
tuned comments;
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@47337
    52
type proofs = thm list * unit lazy;  (*accumulated thms, persistent join*)
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@47337
    64
  type T = Facts.T * (proofs * proofs);  (*facts, recent proofs, all proofs of theory node*)
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@46775
    83
fun register_proofs thms thy = (thms, Data.map (apsnd (pairself (add_proofs thms))) thy);
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 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@42471
    98
    val name =
wenzelm@42471
    99
      (case intern_fact thy xname of
wenzelm@42471
   100
        "_" => "Pure.asm_rl"
wenzelm@42471
   101
      | name => name);
wenzelm@42378
   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@47007
   108
        (Context_Position.report_generic context pos (Name_Space.markup (Facts.space_of facts) name);
wenzelm@45666
   109
         if static then ()
wenzelm@47007
   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@41696
   138
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@41696
   139
      else Thm.name_derivation name)
wenzelm@41696
   140
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@41696
   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@46775
   154
  then app_att thms thy |-> register_proofs
wenzelm@30211
   155
  else
wenzelm@30211
   156
    let
wenzelm@47005
   157
      val name = Sign.full_name thy b;
wenzelm@46775
   158
      val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
wenzelm@30211
   159
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@42375
   160
      val thy'' = thy'
wenzelm@47005
   161
        |> (Data.map o apfst) (Facts.add_global (Context.Theory thy') (b, thms'') #> snd);
wenzelm@30211
   162
    in (thms'', thy'') end;
wenzelm@26488
   163
wenzelm@26488
   164
wenzelm@26488
   165
(* store_thm(s) *)
wenzelm@26488
   166
wenzelm@33700
   167
fun store_thms (b, thms) =
wenzelm@46775
   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@46775
   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@4853
   177
haftmann@29579
   178
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@46775
   179
  enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@4853
   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@47005
   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@46775
   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@46775
   203
    fun app (ths, atts) =
wenzelm@46775
   204
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@46775
   205
    val (thms, thy') =
wenzelm@46775
   206
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@46775
   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@35985
   214
wenzelm@35985
   215
fun no_read _ (_, t) = t;
wenzelm@35985
   216
wenzelm@42375
   217
fun read ctxt (b, str) =
wenzelm@42375
   218
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@42381
   219
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35985
   220
wenzelm@35985
   221
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   222
  let
wenzelm@42375
   223
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@42375
   224
    val prop = prep ctxt (b, raw_prop);
wenzelm@42375
   225
    val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
wenzelm@35985
   226
    val thm = def
wenzelm@35985
   227
      |> Thm.forall_intr_frees
wenzelm@35985
   228
      |> Thm.forall_elim_vars 0
wenzelm@35985
   229
      |> Thm.varifyT_global;
wenzelm@35985
   230
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   231
wenzelm@4853
   232
in
wenzelm@35985
   233
wenzelm@35985
   234
val add_defs = add no_read false;
wenzelm@35985
   235
val add_defs_unchecked = add no_read true;
wenzelm@35985
   236
val add_defs_cmd = add read false;
wenzelm@35985
   237
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   238
wenzelm@4853
   239
end;
wenzelm@4022
   240
wenzelm@3987
   241
end;