src/Pure/global_theory.ML
author wenzelm
Mon Sep 20 16:05:25 2010 +0200 (2010-09-20)
changeset 39557 fe5722fce758
parent 39507 src/Pure/pure_thy.ML@839873937ddd
child 41696 f69bb9077b02
permissions -rw-r--r--
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
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@32105
    13
  val join_proofs: theory -> unit
wenzelm@26683
    14
  val get_fact: Context.generic -> theory -> Facts.ref -> thm list
wenzelm@26344
    15
  val get_thms: theory -> xstring -> thm list
wenzelm@26344
    16
  val get_thm: theory -> xstring -> thm
wenzelm@16336
    17
  val all_thms_of: theory -> (string * thm) list
wenzelm@21580
    18
  val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21567
    19
  val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
wenzelm@21580
    20
  val burrow_facts: ('a list -> 'b list) ->
wenzelm@21580
    21
    ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21580
    22
  val name_multi: string -> 'a list -> (string * 'a) list
wenzelm@33700
    23
  val name_thm: bool -> bool -> string -> thm -> thm
wenzelm@33700
    24
  val name_thms: bool -> bool -> string -> thm list -> thm list
wenzelm@33700
    25
  val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
haftmann@29579
    26
  val store_thms: binding * thm list -> theory -> thm list * theory
haftmann@29579
    27
  val store_thm: binding * thm -> theory -> thm * theory
haftmann@29579
    28
  val store_thm_open: binding * thm -> theory -> thm * theory
haftmann@29579
    29
  val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
haftmann@29579
    30
  val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
haftmann@29579
    31
  val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
haftmann@29579
    32
  val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
wenzelm@30853
    33
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
wenzelm@30853
    34
    -> theory -> (string * thm list) list * theory
haftmann@29579
    35
  val add_defs: bool -> ((binding * term) * attribute list) list ->
haftmann@18377
    36
    theory -> thm list * theory
haftmann@29579
    37
  val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
haftmann@29579
    38
    theory -> thm list * theory
wenzelm@30337
    39
  val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
haftmann@18377
    40
    theory -> thm list * theory
wenzelm@30337
    41
  val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
wenzelm@19629
    42
    theory -> thm list * theory
wenzelm@3987
    43
end;
wenzelm@3987
    44
wenzelm@39557
    45
structure Global_Theory: GLOBAL_THEORY =
wenzelm@3987
    46
struct
wenzelm@3987
    47
wenzelm@27198
    48
(** theory data **)
wenzelm@26282
    49
wenzelm@39557
    50
structure Data = Theory_Data
wenzelm@24713
    51
(
wenzelm@32105
    52
  type T = Facts.T * thm list;
wenzelm@32105
    53
  val empty = (Facts.empty, []);
wenzelm@32105
    54
  fun extend (facts, _) = (facts, []);
wenzelm@33522
    55
  fun merge ((facts1, _), (facts2, _)) = (Facts.merge (facts1, facts2), []);
wenzelm@24713
    56
);
wenzelm@3987
    57
wenzelm@28841
    58
wenzelm@28841
    59
(* facts *)
wenzelm@28841
    60
wenzelm@39557
    61
val facts_of = #1 o Data.get;
wenzelm@26666
    62
wenzelm@26666
    63
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    64
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    65
wenzelm@39557
    66
fun hide_fact fully name = Data.map (apfst (Facts.hide fully name));
wenzelm@28841
    67
wenzelm@28841
    68
wenzelm@28841
    69
(* proofs *)
wenzelm@28841
    70
wenzelm@39557
    71
fun register_proofs (thy, thms) = (Data.map (apsnd (append thms)) thy, thms);
wenzelm@28977
    72
wenzelm@39557
    73
fun join_proofs thy = Thm.join_proofs (rev (#2 (Data.get thy)));
wenzelm@27198
    74
wenzelm@6367
    75
wenzelm@3987
    76
wenzelm@4022
    77
(** retrieve theorems **)
wenzelm@3987
    78
wenzelm@27198
    79
fun get_fact context thy xthmref =
wenzelm@26683
    80
  let
wenzelm@26683
    81
    val xname = Facts.name_of_ref xthmref;
wenzelm@26683
    82
    val pos = Facts.pos_of_ref xthmref;
wenzelm@27198
    83
wenzelm@27198
    84
    val name = intern_fact thy xname;
wenzelm@27198
    85
    val res = Facts.lookup context (facts_of thy) name;
wenzelm@27198
    86
    val _ = Theory.check_thy thy;
wenzelm@26683
    87
  in
wenzelm@27198
    88
    (case res of
wenzelm@27198
    89
      NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
wenzelm@27739
    90
    | SOME (static, ths) =>
wenzelm@39507
    91
        (Position.report pos ((if static then Markup.fact else Markup.dynamic_fact) name);
wenzelm@27739
    92
         Facts.select xthmref (map (Thm.transfer thy) ths)))
wenzelm@26683
    93
  end;
wenzelm@26344
    94
wenzelm@26683
    95
fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
wenzelm@26344
    96
fun get_thm thy name = Facts.the_single name (get_thms thy name);
wenzelm@4783
    97
wenzelm@27198
    98
fun all_thms_of thy =
wenzelm@27865
    99
  Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
wenzelm@16336
   100
wenzelm@4022
   101
wenzelm@4022
   102
wenzelm@26488
   103
(** store theorems **)
wenzelm@3987
   104
wenzelm@21580
   105
(* fact specifications *)
wenzelm@21580
   106
wenzelm@21580
   107
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   108
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   109
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   110
wenzelm@21580
   111
wenzelm@4853
   112
(* naming *)
wenzelm@4853
   113
wenzelm@18801
   114
fun name_multi name [x] = [(name, x)]
wenzelm@26457
   115
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
   116
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
   117
wenzelm@33700
   118
fun name_thm pre official name thm = thm
wenzelm@36744
   119
  |> not (Thm.derivation_name thm <> "" andalso pre orelse not official) ? Thm.name_derivation name
wenzelm@33700
   120
  |> (if Thm.has_name_hint thm andalso pre orelse name = "" then I else Thm.put_name_hint name);
berghofe@12872
   121
wenzelm@33700
   122
fun name_thms pre official name xs =
wenzelm@33700
   123
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   124
wenzelm@33700
   125
fun name_thmss official name fact =
wenzelm@33700
   126
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   127
wenzelm@4853
   128
berghofe@11998
   129
(* enter_thms *)
wenzelm@4853
   130
haftmann@28861
   131
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   132
  if Binding.is_empty b
wenzelm@32105
   133
  then swap (register_proofs (app_att (thy, thms)))
wenzelm@30211
   134
  else
wenzelm@30211
   135
    let
wenzelm@30211
   136
      val naming = Sign.naming_of thy;
wenzelm@33095
   137
      val name = Name_Space.full_name naming b;
wenzelm@30211
   138
      val (thy', thms') =
wenzelm@32105
   139
        register_proofs (apsnd (post_name name) (app_att (thy, pre_name name thms)));
wenzelm@30211
   140
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@39557
   141
      val thy'' = thy' |> (Data.map o apfst) (Facts.add_global naming (b, thms'') #> snd);
wenzelm@30211
   142
    in (thms'', thy'') end;
wenzelm@26488
   143
wenzelm@26488
   144
wenzelm@26488
   145
(* store_thm(s) *)
wenzelm@26488
   146
wenzelm@33700
   147
fun store_thms (b, thms) =
wenzelm@33700
   148
  enter_thms (name_thms true true) (name_thms false true) I (b, thms);
wenzelm@28076
   149
haftmann@29579
   150
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   151
haftmann@29579
   152
fun store_thm_open (b, th) =
wenzelm@33700
   153
  enter_thms (name_thms true false) (name_thms false false) I (b, [th]) #>> the_single;
wenzelm@3987
   154
wenzelm@16023
   155
wenzelm@6091
   156
(* add_thms(s) *)
wenzelm@4853
   157
haftmann@29579
   158
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@33700
   159
  enter_thms pre_name (name_thms false true)
wenzelm@30190
   160
    (Library.foldl_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@4853
   161
haftmann@18377
   162
fun gen_add_thmss pre_name =
haftmann@18377
   163
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   164
berghofe@12235
   165
fun gen_add_thms pre_name args =
haftmann@18377
   166
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   167
wenzelm@33700
   168
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@33700
   169
val add_thms = gen_add_thms (name_thms true true);
haftmann@27683
   170
val add_thm = yield_singleton add_thms;
wenzelm@5907
   171
wenzelm@5907
   172
wenzelm@26488
   173
(* add_thms_dynamic *)
wenzelm@26488
   174
haftmann@29579
   175
fun add_thms_dynamic (b, f) thy = thy
wenzelm@39557
   176
  |> (Data.map o apfst)
haftmann@29579
   177
      (Facts.add_dynamic (Sign.naming_of thy) (b, f) #> snd);
wenzelm@26488
   178
wenzelm@26488
   179
wenzelm@27728
   180
(* note_thmss *)
wenzelm@5907
   181
wenzelm@33167
   182
fun note_thmss kind = fold_map (fn ((b, more_atts), ths_atts) => fn thy =>
wenzelm@12711
   183
  let
haftmann@28941
   184
    val pos = Binding.pos_of b;
haftmann@28965
   185
    val name = Sign.full_name thy b;
wenzelm@39507
   186
    val _ = Position.report pos (Markup.fact_decl name);
wenzelm@28076
   187
wenzelm@30190
   188
    fun app (x, (ths, atts)) = Library.foldl_map (Thm.theory_attributes atts) (x, ths);
haftmann@18418
   189
    val (thms, thy') = thy |> enter_thms
wenzelm@33700
   190
      (name_thmss true) (name_thms false true) (apsnd flat o Library.foldl_map app)
wenzelm@33167
   191
      (b, map (fn (ths, atts) => (ths, surround (Thm.kind kind) (atts @ more_atts))) ths_atts);
wenzelm@28076
   192
  in ((name, thms), thy') end);
wenzelm@12711
   193
wenzelm@5280
   194
wenzelm@4022
   195
(* store axioms as theorems *)
wenzelm@4022
   196
wenzelm@4853
   197
local
wenzelm@35985
   198
wenzelm@35985
   199
fun no_read _ (_, t) = t;
wenzelm@35985
   200
wenzelm@35985
   201
fun read thy (b, str) =
wenzelm@35985
   202
  Syntax.read_prop_global thy str handle ERROR msg =>
wenzelm@35985
   203
    cat_error msg ("The error(s) above occurred in definition " ^ quote (Binding.str_of b));
wenzelm@35985
   204
wenzelm@35985
   205
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   206
  let
wenzelm@35985
   207
    val prop = prep thy (b, raw_prop);
wenzelm@36106
   208
    val ((_, def), thy') = Thm.add_def unchecked overloaded (b, prop) thy;
wenzelm@35985
   209
    val thm = def
wenzelm@35985
   210
      |> Thm.forall_intr_frees
wenzelm@35985
   211
      |> Thm.forall_elim_vars 0
wenzelm@35985
   212
      |> Thm.varifyT_global;
wenzelm@35985
   213
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   214
wenzelm@4853
   215
in
wenzelm@35985
   216
wenzelm@35985
   217
val add_defs = add no_read false;
wenzelm@35985
   218
val add_defs_unchecked = add no_read true;
wenzelm@35985
   219
val add_defs_cmd = add read false;
wenzelm@35985
   220
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   221
wenzelm@4853
   222
end;
wenzelm@4022
   223
wenzelm@3987
   224
end;