src/Pure/global_theory.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 50201 c26369c9eda6
child 52788 da1fdbfebd39
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
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@26683
    13
  val get_fact: Context.generic -> theory -> Facts.ref -> thm list
wenzelm@26344
    14
  val get_thms: theory -> xstring -> thm list
wenzelm@26344
    15
  val get_thm: theory -> xstring -> thm
wenzelm@16336
    16
  val all_thms_of: theory -> (string * thm) list
wenzelm@21580
    17
  val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21567
    18
  val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
wenzelm@21580
    19
  val burrow_facts: ('a list -> 'b list) ->
wenzelm@21580
    20
    ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21580
    21
  val name_multi: string -> 'a list -> (string * 'a) list
wenzelm@33700
    22
  val name_thm: bool -> bool -> string -> thm -> thm
wenzelm@33700
    23
  val name_thms: bool -> bool -> string -> thm list -> thm list
wenzelm@33700
    24
  val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
haftmann@29579
    25
  val store_thms: binding * thm list -> theory -> thm list * theory
haftmann@29579
    26
  val store_thm: binding * thm -> theory -> thm * theory
haftmann@29579
    27
  val store_thm_open: binding * thm -> theory -> thm * theory
haftmann@29579
    28
  val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
haftmann@29579
    29
  val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
haftmann@29579
    30
  val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
haftmann@29579
    31
  val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
wenzelm@30853
    32
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
wenzelm@30853
    33
    -> theory -> (string * thm list) list * theory
haftmann@29579
    34
  val add_defs: bool -> ((binding * term) * attribute list) list ->
haftmann@18377
    35
    theory -> thm list * theory
haftmann@29579
    36
  val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
haftmann@29579
    37
    theory -> thm list * theory
wenzelm@30337
    38
  val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
haftmann@18377
    39
    theory -> thm list * theory
wenzelm@30337
    40
  val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
wenzelm@19629
    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@26666
    59
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    60
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    61
wenzelm@49010
    62
fun hide_fact fully name = Data.map (Facts.hide fully name);
wenzelm@6367
    63
wenzelm@3987
    64
wenzelm@4022
    65
(** retrieve theorems **)
wenzelm@3987
    66
wenzelm@27198
    67
fun get_fact context thy xthmref =
wenzelm@26683
    68
  let
wenzelm@42378
    69
    val facts = facts_of thy;
wenzelm@26683
    70
    val xname = Facts.name_of_ref xthmref;
wenzelm@26683
    71
    val pos = Facts.pos_of_ref xthmref;
wenzelm@27198
    72
wenzelm@42471
    73
    val name =
wenzelm@42471
    74
      (case intern_fact thy xname of
wenzelm@42471
    75
        "_" => "Pure.asm_rl"
wenzelm@42471
    76
      | name => name);
wenzelm@42378
    77
    val res = Facts.lookup context facts name;
wenzelm@27198
    78
    val _ = Theory.check_thy thy;
wenzelm@26683
    79
  in
wenzelm@27198
    80
    (case res of
wenzelm@48992
    81
      NONE => error ("Unknown fact " ^ quote name ^ Position.here pos)
wenzelm@27739
    82
    | SOME (static, ths) =>
wenzelm@47007
    83
        (Context_Position.report_generic context pos (Name_Space.markup (Facts.space_of facts) name);
wenzelm@45666
    84
         if static then ()
wenzelm@50201
    85
         else Context_Position.report_generic context pos (Markup.dynamic_fact name);
wenzelm@27739
    86
         Facts.select xthmref (map (Thm.transfer thy) ths)))
wenzelm@26683
    87
  end;
wenzelm@26344
    88
wenzelm@26683
    89
fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
wenzelm@26344
    90
fun get_thm thy name = Facts.the_single name (get_thms thy name);
wenzelm@4783
    91
wenzelm@27198
    92
fun all_thms_of thy =
wenzelm@27865
    93
  Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
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@26488
   169
(* add_thms_dynamic *)
wenzelm@26488
   170
haftmann@29579
   171
fun add_thms_dynamic (b, f) thy = thy
wenzelm@49010
   172
  |> Data.map (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
wenzelm@26488
   173
wenzelm@26488
   174
wenzelm@27728
   175
(* note_thmss *)
wenzelm@5907
   176
wenzelm@46775
   177
fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
wenzelm@12711
   178
  let
haftmann@28965
   179
    val name = Sign.full_name thy b;
wenzelm@46775
   180
    fun app (ths, atts) =
wenzelm@46775
   181
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@46775
   182
    val (thms, thy') =
wenzelm@46775
   183
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@46775
   184
        (b, facts) thy;
wenzelm@28076
   185
  in ((name, thms), thy') end);
wenzelm@12711
   186
wenzelm@5280
   187
wenzelm@4022
   188
(* store axioms as theorems *)
wenzelm@4022
   189
wenzelm@4853
   190
local
wenzelm@35985
   191
wenzelm@35985
   192
fun no_read _ (_, t) = t;
wenzelm@35985
   193
wenzelm@42375
   194
fun read ctxt (b, str) =
wenzelm@42375
   195
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@42381
   196
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35985
   197
wenzelm@35985
   198
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   199
  let
wenzelm@42375
   200
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@42375
   201
    val prop = prep ctxt (b, raw_prop);
wenzelm@42375
   202
    val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
wenzelm@35985
   203
    val thm = def
wenzelm@35985
   204
      |> Thm.forall_intr_frees
wenzelm@35985
   205
      |> Thm.forall_elim_vars 0
wenzelm@35985
   206
      |> Thm.varifyT_global;
wenzelm@35985
   207
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   208
wenzelm@4853
   209
in
wenzelm@35985
   210
wenzelm@35985
   211
val add_defs = add no_read false;
wenzelm@35985
   212
val add_defs_unchecked = add no_read true;
wenzelm@35985
   213
val add_defs_cmd = add read false;
wenzelm@35985
   214
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   215
wenzelm@4853
   216
end;
wenzelm@4022
   217
wenzelm@3987
   218
end;