src/Pure/global_theory.ML
author wenzelm
Thu Sep 24 23:33:29 2015 +0200 (2015-09-24)
changeset 61261 ddb2da7cb2e4
parent 61054 add998b3c597
child 61262 7bd1eb4b056e
permissions -rw-r--r--
more explicit Defs.context: use proper name spaces as far as possible;
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@30337
    42
  val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
haftmann@18377
    43
    theory -> thm list * theory
wenzelm@30337
    44
  val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
wenzelm@19629
    45
    theory -> thm list * theory
wenzelm@3987
    46
end;
wenzelm@3987
    47
wenzelm@39557
    48
structure Global_Theory: GLOBAL_THEORY =
wenzelm@3987
    49
struct
wenzelm@3987
    50
wenzelm@27198
    51
(** theory data **)
wenzelm@26282
    52
wenzelm@39557
    53
structure Data = Theory_Data
wenzelm@24713
    54
(
wenzelm@49010
    55
  type T = Facts.T;
wenzelm@49010
    56
  val empty = Facts.empty;
wenzelm@49010
    57
  val extend = I;
wenzelm@49010
    58
  val merge = Facts.merge;
wenzelm@24713
    59
);
wenzelm@3987
    60
wenzelm@49010
    61
val facts_of = Data.get;
wenzelm@26666
    62
wenzelm@56003
    63
fun check_fact thy = Facts.check (Context.Theory thy) (facts_of thy);
wenzelm@26666
    64
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    65
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    66
wenzelm@57887
    67
fun alias_fact binding name thy =
wenzelm@57887
    68
  Data.map (Facts.alias (Sign.naming_of thy) binding name) thy;
wenzelm@57887
    69
wenzelm@49010
    70
fun hide_fact fully name = Data.map (Facts.hide fully name);
wenzelm@6367
    71
wenzelm@3987
    72
wenzelm@56140
    73
(* retrieve theorems *)
wenzelm@27198
    74
wenzelm@56140
    75
fun get_thms thy xname =
wenzelm@57942
    76
  #thms (Facts.retrieve (Context.Theory thy) (facts_of thy) (xname, Position.none));
wenzelm@26344
    77
wenzelm@56140
    78
fun get_thm thy xname =
wenzelm@56140
    79
  Facts.the_single (xname, Position.none) (get_thms thy xname);
wenzelm@4783
    80
wenzelm@61054
    81
fun transfer_theories thy =
wenzelm@61054
    82
  let
wenzelm@61054
    83
    val theories =
wenzelm@61054
    84
      fold (fn thy' => Symtab.update (Context.theory_name thy', thy'))
wenzelm@61054
    85
        (Theory.nodes_of thy) Symtab.empty;
wenzelm@61054
    86
    fun transfer th =
wenzelm@61054
    87
      Thm.transfer (the_default thy (Symtab.lookup theories (Thm.theory_name_of_thm th))) th;
wenzelm@61054
    88
  in transfer end;
wenzelm@61054
    89
wenzelm@56161
    90
fun all_thms_of thy verbose =
wenzelm@56161
    91
  let
wenzelm@61054
    92
    val transfer = transfer_theories thy;
wenzelm@56161
    93
    val facts = facts_of thy;
wenzelm@56161
    94
    fun add (name, ths) =
wenzelm@56161
    95
      if not verbose andalso Facts.is_concealed facts name then I
wenzelm@61054
    96
      else append (map (`(Thm.get_name_hint) o transfer) ths);
wenzelm@56161
    97
  in Facts.fold_static add facts [] end;
wenzelm@16336
    98
wenzelm@4022
    99
wenzelm@4022
   100
wenzelm@26488
   101
(** store theorems **)
wenzelm@3987
   102
wenzelm@21580
   103
(* fact specifications *)
wenzelm@21580
   104
wenzelm@21580
   105
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   106
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   107
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   108
wenzelm@21580
   109
wenzelm@4853
   110
(* naming *)
wenzelm@4853
   111
wenzelm@18801
   112
fun name_multi name [x] = [(name, x)]
wenzelm@26457
   113
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
   114
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
   115
wenzelm@33700
   116
fun name_thm pre official name thm = thm
wenzelm@41696
   117
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@41696
   118
      else Thm.name_derivation name)
wenzelm@41696
   119
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@41696
   120
      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
wenzelm@49062
   131
fun register_proofs thms thy = (thms, Thm.register_proofs thms thy);
wenzelm@49010
   132
haftmann@28861
   133
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   134
  if Binding.is_empty b
wenzelm@46775
   135
  then app_att thms thy |-> register_proofs
wenzelm@30211
   136
  else
wenzelm@30211
   137
    let
wenzelm@47005
   138
      val name = Sign.full_name thy b;
wenzelm@46775
   139
      val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
wenzelm@30211
   140
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@49747
   141
      val thy'' = thy' |> Data.map
wenzelm@49747
   142
        (Facts.add_static (Context.Theory thy') {strict = true, index = false} (b, thms'') #> snd);
wenzelm@30211
   143
    in (thms'', thy'') end;
wenzelm@26488
   144
wenzelm@26488
   145
wenzelm@26488
   146
(* store_thm(s) *)
wenzelm@26488
   147
wenzelm@33700
   148
fun store_thms (b, thms) =
wenzelm@46775
   149
  enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
wenzelm@28076
   150
haftmann@29579
   151
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   152
haftmann@29579
   153
fun store_thm_open (b, th) =
wenzelm@46775
   154
  enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
wenzelm@3987
   155
wenzelm@16023
   156
wenzelm@6091
   157
(* add_thms(s) *)
wenzelm@4853
   158
haftmann@29579
   159
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@46775
   160
  enter_thms pre_name (name_thms false true) (fold_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@57929
   173
(* dynamic theorems *)
wenzelm@26488
   174
wenzelm@57929
   175
fun add_thms_dynamic' context arg thy =
wenzelm@57929
   176
  let val (name, facts') = Facts.add_dynamic context arg (Data.get thy)
wenzelm@57929
   177
  in (name, Data.put facts' thy) end;
wenzelm@57929
   178
wenzelm@57929
   179
fun add_thms_dynamic arg thy =
wenzelm@57929
   180
  add_thms_dynamic' (Context.Theory thy) arg thy |> snd;
wenzelm@26488
   181
wenzelm@26488
   182
wenzelm@27728
   183
(* note_thmss *)
wenzelm@5907
   184
wenzelm@46775
   185
fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
wenzelm@12711
   186
  let
haftmann@28965
   187
    val name = Sign.full_name thy b;
wenzelm@46775
   188
    fun app (ths, atts) =
wenzelm@46775
   189
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@46775
   190
    val (thms, thy') =
wenzelm@46775
   191
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@46775
   192
        (b, facts) thy;
wenzelm@28076
   193
  in ((name, thms), thy') end);
wenzelm@12711
   194
wenzelm@5280
   195
wenzelm@4022
   196
(* store axioms as theorems *)
wenzelm@4022
   197
wenzelm@4853
   198
local
wenzelm@35985
   199
wenzelm@35985
   200
fun no_read _ (_, t) = t;
wenzelm@35985
   201
wenzelm@42375
   202
fun read ctxt (b, str) =
wenzelm@42375
   203
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@42381
   204
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35985
   205
wenzelm@35985
   206
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   207
  let
wenzelm@42375
   208
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@42375
   209
    val prop = prep ctxt (b, raw_prop);
wenzelm@61261
   210
    val ((_, def), thy') = Thm.add_def (ctxt, NONE) unchecked overloaded (b, prop) thy;
wenzelm@35985
   211
    val thm = def
wenzelm@35985
   212
      |> Thm.forall_intr_frees
wenzelm@35985
   213
      |> Thm.forall_elim_vars 0
wenzelm@35985
   214
      |> Thm.varifyT_global;
wenzelm@35985
   215
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   216
wenzelm@4853
   217
in
wenzelm@35985
   218
wenzelm@35985
   219
val add_defs = add no_read false;
wenzelm@35985
   220
val add_defs_unchecked = add no_read true;
wenzelm@35985
   221
val add_defs_cmd = add read false;
wenzelm@35985
   222
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   223
wenzelm@4853
   224
end;
wenzelm@4022
   225
wenzelm@3987
   226
end;