src/Pure/global_theory.ML
author wenzelm
Fri Oct 07 21:16:48 2016 +0200 (2016-10-07)
changeset 64092 95469c544b82
parent 62170 b61c55e4b4b9
child 65458 cf504b7a7aa7
permissions -rw-r--r--
accept obscure timezone used in 2011;
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@56003
    10
  val check_fact: theory -> xstring * Position.T -> string
wenzelm@26666
    11
  val intern_fact: theory -> xstring -> string
wenzelm@26693
    12
  val defined_fact: theory -> string -> bool
wenzelm@57887
    13
  val alias_fact: binding -> string -> theory -> theory
wenzelm@27198
    14
  val hide_fact: bool -> string -> theory -> theory
wenzelm@26344
    15
  val get_thms: theory -> xstring -> thm list
wenzelm@26344
    16
  val get_thm: theory -> xstring -> thm
wenzelm@61054
    17
  val transfer_theories: theory -> thm -> thm
wenzelm@56161
    18
  val all_thms_of: theory -> bool -> (string * thm) list
wenzelm@21580
    19
  val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21567
    20
  val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
wenzelm@21580
    21
  val burrow_facts: ('a list -> 'b list) ->
wenzelm@21580
    22
    ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21580
    23
  val name_multi: string -> 'a list -> (string * 'a) list
wenzelm@33700
    24
  val name_thm: bool -> bool -> string -> thm -> thm
wenzelm@33700
    25
  val name_thms: bool -> bool -> string -> thm list -> thm list
wenzelm@33700
    26
  val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
haftmann@29579
    27
  val store_thms: binding * thm list -> theory -> thm list * theory
haftmann@29579
    28
  val store_thm: binding * thm -> theory -> thm * theory
haftmann@29579
    29
  val store_thm_open: binding * thm -> theory -> thm * theory
haftmann@29579
    30
  val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
haftmann@29579
    31
  val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
haftmann@29579
    32
  val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
wenzelm@57929
    33
  val add_thms_dynamic': Context.generic -> binding * (Context.generic -> thm list) ->
wenzelm@57929
    34
    theory -> string * theory
haftmann@29579
    35
  val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
wenzelm@30853
    36
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
wenzelm@30853
    37
    -> theory -> (string * thm list) list * theory
haftmann@29579
    38
  val add_defs: bool -> ((binding * term) * attribute list) list ->
haftmann@18377
    39
    theory -> thm list * theory
haftmann@29579
    40
  val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
haftmann@29579
    41
    theory -> thm list * theory
wenzelm@3987
    42
end;
wenzelm@3987
    43
wenzelm@39557
    44
structure Global_Theory: GLOBAL_THEORY =
wenzelm@3987
    45
struct
wenzelm@3987
    46
wenzelm@27198
    47
(** theory data **)
wenzelm@26282
    48
wenzelm@39557
    49
structure Data = Theory_Data
wenzelm@24713
    50
(
wenzelm@49010
    51
  type T = Facts.T;
wenzelm@49010
    52
  val empty = Facts.empty;
wenzelm@49010
    53
  val extend = I;
wenzelm@49010
    54
  val merge = Facts.merge;
wenzelm@24713
    55
);
wenzelm@3987
    56
wenzelm@49010
    57
val facts_of = Data.get;
wenzelm@26666
    58
wenzelm@56003
    59
fun check_fact thy = Facts.check (Context.Theory thy) (facts_of thy);
wenzelm@26666
    60
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    61
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    62
wenzelm@57887
    63
fun alias_fact binding name thy =
wenzelm@57887
    64
  Data.map (Facts.alias (Sign.naming_of thy) binding name) thy;
wenzelm@57887
    65
wenzelm@49010
    66
fun hide_fact fully name = Data.map (Facts.hide fully name);
wenzelm@6367
    67
wenzelm@3987
    68
wenzelm@56140
    69
(* retrieve theorems *)
wenzelm@27198
    70
wenzelm@56140
    71
fun get_thms thy xname =
wenzelm@57942
    72
  #thms (Facts.retrieve (Context.Theory thy) (facts_of thy) (xname, Position.none));
wenzelm@26344
    73
wenzelm@56140
    74
fun get_thm thy xname =
wenzelm@56140
    75
  Facts.the_single (xname, Position.none) (get_thms thy xname);
wenzelm@4783
    76
wenzelm@61054
    77
fun transfer_theories thy =
wenzelm@61054
    78
  let
wenzelm@61054
    79
    val theories =
wenzelm@61054
    80
      fold (fn thy' => Symtab.update (Context.theory_name thy', thy'))
wenzelm@61054
    81
        (Theory.nodes_of thy) Symtab.empty;
wenzelm@61054
    82
    fun transfer th =
wenzelm@61054
    83
      Thm.transfer (the_default thy (Symtab.lookup theories (Thm.theory_name_of_thm th))) th;
wenzelm@61054
    84
  in transfer end;
wenzelm@61054
    85
wenzelm@56161
    86
fun all_thms_of thy verbose =
wenzelm@56161
    87
  let
wenzelm@61054
    88
    val transfer = transfer_theories thy;
wenzelm@56161
    89
    val facts = facts_of thy;
wenzelm@56161
    90
    fun add (name, ths) =
wenzelm@56161
    91
      if not verbose andalso Facts.is_concealed facts name then I
wenzelm@61054
    92
      else append (map (`(Thm.get_name_hint) o transfer) ths);
wenzelm@56161
    93
  in Facts.fold_static add facts [] end;
wenzelm@16336
    94
wenzelm@4022
    95
wenzelm@4022
    96
wenzelm@26488
    97
(** store theorems **)
wenzelm@3987
    98
wenzelm@21580
    99
(* fact specifications *)
wenzelm@21580
   100
wenzelm@21580
   101
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   102
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   103
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   104
wenzelm@21580
   105
wenzelm@4853
   106
(* naming *)
wenzelm@4853
   107
wenzelm@18801
   108
fun name_multi name [x] = [(name, x)]
wenzelm@26457
   109
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
   110
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
   111
wenzelm@33700
   112
fun name_thm pre official name thm = thm
wenzelm@41696
   113
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@41696
   114
      else Thm.name_derivation name)
wenzelm@41696
   115
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@41696
   116
      else Thm.put_name_hint name);
berghofe@12872
   117
wenzelm@33700
   118
fun name_thms pre official name xs =
wenzelm@33700
   119
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   120
wenzelm@33700
   121
fun name_thmss official name fact =
wenzelm@33700
   122
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   123
wenzelm@4853
   124
berghofe@11998
   125
(* enter_thms *)
wenzelm@4853
   126
wenzelm@49062
   127
fun register_proofs thms thy = (thms, Thm.register_proofs thms thy);
wenzelm@49010
   128
haftmann@28861
   129
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   130
  if Binding.is_empty b
wenzelm@46775
   131
  then app_att thms thy |-> register_proofs
wenzelm@30211
   132
  else
wenzelm@30211
   133
    let
wenzelm@47005
   134
      val name = Sign.full_name thy b;
wenzelm@46775
   135
      val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
wenzelm@30211
   136
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@49747
   137
      val thy'' = thy' |> Data.map
wenzelm@49747
   138
        (Facts.add_static (Context.Theory thy') {strict = true, index = false} (b, thms'') #> snd);
wenzelm@30211
   139
    in (thms'', thy'') end;
wenzelm@26488
   140
wenzelm@26488
   141
wenzelm@26488
   142
(* store_thm(s) *)
wenzelm@26488
   143
wenzelm@33700
   144
fun store_thms (b, thms) =
wenzelm@46775
   145
  enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
wenzelm@28076
   146
haftmann@29579
   147
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   148
haftmann@29579
   149
fun store_thm_open (b, th) =
wenzelm@46775
   150
  enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
wenzelm@3987
   151
wenzelm@16023
   152
wenzelm@6091
   153
(* add_thms(s) *)
wenzelm@4853
   154
haftmann@29579
   155
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@46775
   156
  enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@4853
   157
haftmann@18377
   158
fun gen_add_thmss pre_name =
haftmann@18377
   159
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   160
berghofe@12235
   161
fun gen_add_thms pre_name args =
haftmann@18377
   162
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   163
wenzelm@33700
   164
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@33700
   165
val add_thms = gen_add_thms (name_thms true true);
haftmann@27683
   166
val add_thm = yield_singleton add_thms;
wenzelm@5907
   167
wenzelm@5907
   168
wenzelm@57929
   169
(* dynamic theorems *)
wenzelm@26488
   170
wenzelm@57929
   171
fun add_thms_dynamic' context arg thy =
wenzelm@57929
   172
  let val (name, facts') = Facts.add_dynamic context arg (Data.get thy)
wenzelm@57929
   173
  in (name, Data.put facts' thy) end;
wenzelm@57929
   174
wenzelm@57929
   175
fun add_thms_dynamic arg thy =
wenzelm@57929
   176
  add_thms_dynamic' (Context.Theory thy) arg thy |> snd;
wenzelm@26488
   177
wenzelm@26488
   178
wenzelm@27728
   179
(* note_thmss *)
wenzelm@5907
   180
wenzelm@46775
   181
fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
wenzelm@12711
   182
  let
haftmann@28965
   183
    val name = Sign.full_name thy b;
wenzelm@46775
   184
    fun app (ths, atts) =
wenzelm@46775
   185
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@46775
   186
    val (thms, thy') =
wenzelm@46775
   187
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@46775
   188
        (b, facts) thy;
wenzelm@28076
   189
  in ((name, thms), thy') end);
wenzelm@12711
   190
wenzelm@5280
   191
wenzelm@62170
   192
(* old-style defs *)
wenzelm@4022
   193
wenzelm@4853
   194
local
wenzelm@35985
   195
wenzelm@62169
   196
fun add unchecked overloaded = fold_map (fn ((b, prop), atts) => fn thy =>
wenzelm@35985
   197
  let
wenzelm@62170
   198
    val context = Defs.global_context thy;
wenzelm@61262
   199
    val ((_, def), thy') = Thm.add_def context unchecked overloaded (b, prop) thy;
wenzelm@35985
   200
    val thm = def
wenzelm@35985
   201
      |> Thm.forall_intr_frees
wenzelm@35985
   202
      |> Thm.forall_elim_vars 0
wenzelm@35985
   203
      |> Thm.varifyT_global;
wenzelm@35985
   204
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   205
wenzelm@4853
   206
in
wenzelm@35985
   207
wenzelm@62169
   208
val add_defs = add false;
wenzelm@62169
   209
val add_defs_unchecked = add true;
wenzelm@35985
   210
wenzelm@4853
   211
end;
wenzelm@4022
   212
wenzelm@3987
   213
end;