src/Pure/global_theory.ML
author wenzelm
Mon Mar 10 15:04:01 2014 +0100 (2014-03-10)
changeset 56026 893fe12639bc
parent 56003 eccac152ffb4
child 56140 ed92ce2ac88e
permissions -rw-r--r--
tuned signature -- prefer Name_Space.get with its builtin error;
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@27198
    13
  val hide_fact: bool -> string -> theory -> theory
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@49010
    52
  type T = Facts.T;
wenzelm@49010
    53
  val empty = Facts.empty;
wenzelm@49010
    54
  val extend = I;
wenzelm@49010
    55
  val merge = Facts.merge;
wenzelm@24713
    56
);
wenzelm@3987
    57
wenzelm@49010
    58
val facts_of = Data.get;
wenzelm@26666
    59
wenzelm@56003
    60
fun check_fact thy = Facts.check (Context.Theory thy) (facts_of thy);
wenzelm@26666
    61
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    62
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    63
wenzelm@49010
    64
fun hide_fact fully name = Data.map (Facts.hide fully name);
wenzelm@6367
    65
wenzelm@3987
    66
wenzelm@4022
    67
(** retrieve theorems **)
wenzelm@3987
    68
wenzelm@27198
    69
fun get_fact context thy xthmref =
wenzelm@26683
    70
  let
wenzelm@42378
    71
    val facts = facts_of thy;
wenzelm@26683
    72
    val xname = Facts.name_of_ref xthmref;
wenzelm@26683
    73
    val pos = Facts.pos_of_ref xthmref;
wenzelm@27198
    74
wenzelm@42471
    75
    val name =
wenzelm@42471
    76
      (case intern_fact thy xname of
wenzelm@42471
    77
        "_" => "Pure.asm_rl"
wenzelm@42471
    78
      | name => name);
wenzelm@42378
    79
    val res = Facts.lookup context facts name;
wenzelm@26683
    80
  in
wenzelm@27198
    81
    (case res of
wenzelm@48992
    82
      NONE => error ("Unknown fact " ^ quote name ^ Position.here pos)
wenzelm@27739
    83
    | SOME (static, ths) =>
wenzelm@47007
    84
        (Context_Position.report_generic context pos (Name_Space.markup (Facts.space_of facts) name);
wenzelm@45666
    85
         if static then ()
wenzelm@50201
    86
         else Context_Position.report_generic context pos (Markup.dynamic_fact name);
wenzelm@27739
    87
         Facts.select xthmref (map (Thm.transfer thy) ths)))
wenzelm@26683
    88
  end;
wenzelm@26344
    89
wenzelm@26683
    90
fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
wenzelm@55740
    91
fun get_thm thy name = Facts.the_single (name, Position.none) (get_thms thy name);
wenzelm@4783
    92
wenzelm@27198
    93
fun all_thms_of thy =
wenzelm@27865
    94
  Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
wenzelm@16336
    95
wenzelm@4022
    96
wenzelm@4022
    97
wenzelm@26488
    98
(** store theorems **)
wenzelm@3987
    99
wenzelm@21580
   100
(* fact specifications *)
wenzelm@21580
   101
wenzelm@21580
   102
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   103
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   104
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   105
wenzelm@21580
   106
wenzelm@4853
   107
(* naming *)
wenzelm@4853
   108
wenzelm@18801
   109
fun name_multi name [x] = [(name, x)]
wenzelm@26457
   110
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
   111
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
   112
wenzelm@33700
   113
fun name_thm pre official name thm = thm
wenzelm@41696
   114
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@41696
   115
      else Thm.name_derivation name)
wenzelm@41696
   116
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@41696
   117
      else Thm.put_name_hint name);
berghofe@12872
   118
wenzelm@33700
   119
fun name_thms pre official name xs =
wenzelm@33700
   120
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   121
wenzelm@33700
   122
fun name_thmss official name fact =
wenzelm@33700
   123
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   124
wenzelm@4853
   125
berghofe@11998
   126
(* enter_thms *)
wenzelm@4853
   127
wenzelm@49062
   128
fun register_proofs thms thy = (thms, Thm.register_proofs thms thy);
wenzelm@49010
   129
haftmann@28861
   130
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   131
  if Binding.is_empty b
wenzelm@46775
   132
  then app_att thms thy |-> register_proofs
wenzelm@30211
   133
  else
wenzelm@30211
   134
    let
wenzelm@47005
   135
      val name = Sign.full_name thy b;
wenzelm@46775
   136
      val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
wenzelm@30211
   137
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@49747
   138
      val thy'' = thy' |> Data.map
wenzelm@49747
   139
        (Facts.add_static (Context.Theory thy') {strict = true, index = false} (b, thms'') #> snd);
wenzelm@30211
   140
    in (thms'', thy'') end;
wenzelm@26488
   141
wenzelm@26488
   142
wenzelm@26488
   143
(* store_thm(s) *)
wenzelm@26488
   144
wenzelm@33700
   145
fun store_thms (b, thms) =
wenzelm@46775
   146
  enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
wenzelm@28076
   147
haftmann@29579
   148
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   149
haftmann@29579
   150
fun store_thm_open (b, th) =
wenzelm@46775
   151
  enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
wenzelm@3987
   152
wenzelm@16023
   153
wenzelm@6091
   154
(* add_thms(s) *)
wenzelm@4853
   155
haftmann@29579
   156
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@46775
   157
  enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@4853
   158
haftmann@18377
   159
fun gen_add_thmss pre_name =
haftmann@18377
   160
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   161
berghofe@12235
   162
fun gen_add_thms pre_name args =
haftmann@18377
   163
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   164
wenzelm@33700
   165
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@33700
   166
val add_thms = gen_add_thms (name_thms true true);
haftmann@27683
   167
val add_thm = yield_singleton add_thms;
wenzelm@5907
   168
wenzelm@5907
   169
wenzelm@26488
   170
(* add_thms_dynamic *)
wenzelm@26488
   171
haftmann@29579
   172
fun add_thms_dynamic (b, f) thy = thy
wenzelm@49010
   173
  |> Data.map (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
wenzelm@26488
   174
wenzelm@26488
   175
wenzelm@27728
   176
(* note_thmss *)
wenzelm@5907
   177
wenzelm@46775
   178
fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
wenzelm@12711
   179
  let
haftmann@28965
   180
    val name = Sign.full_name thy b;
wenzelm@46775
   181
    fun app (ths, atts) =
wenzelm@46775
   182
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@46775
   183
    val (thms, thy') =
wenzelm@46775
   184
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@46775
   185
        (b, facts) thy;
wenzelm@28076
   186
  in ((name, thms), thy') end);
wenzelm@12711
   187
wenzelm@5280
   188
wenzelm@4022
   189
(* store axioms as theorems *)
wenzelm@4022
   190
wenzelm@4853
   191
local
wenzelm@35985
   192
wenzelm@35985
   193
fun no_read _ (_, t) = t;
wenzelm@35985
   194
wenzelm@42375
   195
fun read ctxt (b, str) =
wenzelm@42375
   196
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@42381
   197
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35985
   198
wenzelm@35985
   199
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   200
  let
wenzelm@42375
   201
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@42375
   202
    val prop = prep ctxt (b, raw_prop);
wenzelm@42375
   203
    val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
wenzelm@35985
   204
    val thm = def
wenzelm@35985
   205
      |> Thm.forall_intr_frees
wenzelm@35985
   206
      |> Thm.forall_elim_vars 0
wenzelm@35985
   207
      |> Thm.varifyT_global;
wenzelm@35985
   208
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   209
wenzelm@4853
   210
in
wenzelm@35985
   211
wenzelm@35985
   212
val add_defs = add no_read false;
wenzelm@35985
   213
val add_defs_unchecked = add no_read true;
wenzelm@35985
   214
val add_defs_cmd = add read false;
wenzelm@35985
   215
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   216
wenzelm@4853
   217
end;
wenzelm@4022
   218
wenzelm@3987
   219
end;