src/Pure/global_theory.ML
author blanchet
Mon May 19 23:43:53 2014 +0200 (2014-05-19)
changeset 57008 10f68b83b474
parent 56161 300f613060b0
child 57887 44354c99d754
permissions -rw-r--r--
use E 1.8's auto scheduler option
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@26344
    14
  val get_thms: theory -> xstring -> thm list
wenzelm@26344
    15
  val get_thm: theory -> xstring -> thm
wenzelm@56161
    16
  val all_thms_of: theory -> bool -> (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@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@49010
    63
fun hide_fact fully name = Data.map (Facts.hide fully name);
wenzelm@6367
    64
wenzelm@3987
    65
wenzelm@56140
    66
(* retrieve theorems *)
wenzelm@27198
    67
wenzelm@56140
    68
fun get_thms thy xname =
wenzelm@56140
    69
  #2 (Facts.retrieve (Context.Theory thy) (facts_of thy) (xname, Position.none));
wenzelm@26344
    70
wenzelm@56140
    71
fun get_thm thy xname =
wenzelm@56140
    72
  Facts.the_single (xname, Position.none) (get_thms thy xname);
wenzelm@4783
    73
wenzelm@56161
    74
fun all_thms_of thy verbose =
wenzelm@56161
    75
  let
wenzelm@56161
    76
    val facts = facts_of thy;
wenzelm@56161
    77
    fun add (name, ths) =
wenzelm@56161
    78
      if not verbose andalso Facts.is_concealed facts name then I
wenzelm@56161
    79
      else append (map (`(Thm.get_name_hint)) ths);
wenzelm@56161
    80
  in Facts.fold_static add facts [] end;
wenzelm@16336
    81
wenzelm@4022
    82
wenzelm@4022
    83
wenzelm@26488
    84
(** store theorems **)
wenzelm@3987
    85
wenzelm@21580
    86
(* fact specifications *)
wenzelm@21580
    87
wenzelm@21580
    88
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
    89
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
    90
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
    91
wenzelm@21580
    92
wenzelm@4853
    93
(* naming *)
wenzelm@4853
    94
wenzelm@18801
    95
fun name_multi name [x] = [(name, x)]
wenzelm@26457
    96
  | name_multi "" xs = map (pair "") xs
wenzelm@26457
    97
  | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
berghofe@12235
    98
wenzelm@33700
    99
fun name_thm pre official name thm = thm
wenzelm@41696
   100
  |> (if not official orelse pre andalso Thm.derivation_name thm <> "" then I
wenzelm@41696
   101
      else Thm.name_derivation name)
wenzelm@41696
   102
  |> (if name = "" orelse pre andalso Thm.has_name_hint thm then I
wenzelm@41696
   103
      else Thm.put_name_hint name);
berghofe@12872
   104
wenzelm@33700
   105
fun name_thms pre official name xs =
wenzelm@33700
   106
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   107
wenzelm@33700
   108
fun name_thmss official name fact =
wenzelm@33700
   109
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   110
wenzelm@4853
   111
berghofe@11998
   112
(* enter_thms *)
wenzelm@4853
   113
wenzelm@49062
   114
fun register_proofs thms thy = (thms, Thm.register_proofs thms thy);
wenzelm@49010
   115
haftmann@28861
   116
fun enter_thms pre_name post_name app_att (b, thms) thy =
haftmann@28965
   117
  if Binding.is_empty b
wenzelm@46775
   118
  then app_att thms thy |-> register_proofs
wenzelm@30211
   119
  else
wenzelm@30211
   120
    let
wenzelm@47005
   121
      val name = Sign.full_name thy b;
wenzelm@46775
   122
      val (thms', thy') = app_att (pre_name name thms) thy |>> post_name name |-> register_proofs;
wenzelm@30211
   123
      val thms'' = map (Thm.transfer thy') thms';
wenzelm@49747
   124
      val thy'' = thy' |> Data.map
wenzelm@49747
   125
        (Facts.add_static (Context.Theory thy') {strict = true, index = false} (b, thms'') #> snd);
wenzelm@30211
   126
    in (thms'', thy'') end;
wenzelm@26488
   127
wenzelm@26488
   128
wenzelm@26488
   129
(* store_thm(s) *)
wenzelm@26488
   130
wenzelm@33700
   131
fun store_thms (b, thms) =
wenzelm@46775
   132
  enter_thms (name_thms true true) (name_thms false true) pair (b, thms);
wenzelm@28076
   133
haftmann@29579
   134
fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
wenzelm@26488
   135
haftmann@29579
   136
fun store_thm_open (b, th) =
wenzelm@46775
   137
  enter_thms (name_thms true false) (name_thms false false) pair (b, [th]) #>> the_single;
wenzelm@3987
   138
wenzelm@16023
   139
wenzelm@6091
   140
(* add_thms(s) *)
wenzelm@4853
   141
haftmann@29579
   142
fun add_thms_atts pre_name ((b, thms), atts) =
wenzelm@46775
   143
  enter_thms pre_name (name_thms false true) (fold_map (Thm.theory_attributes atts)) (b, thms);
wenzelm@4853
   144
haftmann@18377
   145
fun gen_add_thmss pre_name =
haftmann@18377
   146
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   147
berghofe@12235
   148
fun gen_add_thms pre_name args =
haftmann@18377
   149
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   150
wenzelm@33700
   151
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@33700
   152
val add_thms = gen_add_thms (name_thms true true);
haftmann@27683
   153
val add_thm = yield_singleton add_thms;
wenzelm@5907
   154
wenzelm@5907
   155
wenzelm@26488
   156
(* add_thms_dynamic *)
wenzelm@26488
   157
haftmann@29579
   158
fun add_thms_dynamic (b, f) thy = thy
wenzelm@49010
   159
  |> Data.map (Facts.add_dynamic (Context.Theory thy) (b, f) #> snd);
wenzelm@26488
   160
wenzelm@26488
   161
wenzelm@27728
   162
(* note_thmss *)
wenzelm@5907
   163
wenzelm@46775
   164
fun note_thmss kind = fold_map (fn ((b, more_atts), facts) => fn thy =>
wenzelm@12711
   165
  let
haftmann@28965
   166
    val name = Sign.full_name thy b;
wenzelm@46775
   167
    fun app (ths, atts) =
wenzelm@46775
   168
      fold_map (Thm.theory_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
wenzelm@46775
   169
    val (thms, thy') =
wenzelm@46775
   170
      enter_thms (name_thmss true) (name_thms false true) (apfst flat oo fold_map app)
wenzelm@46775
   171
        (b, facts) thy;
wenzelm@28076
   172
  in ((name, thms), thy') end);
wenzelm@12711
   173
wenzelm@5280
   174
wenzelm@4022
   175
(* store axioms as theorems *)
wenzelm@4022
   176
wenzelm@4853
   177
local
wenzelm@35985
   178
wenzelm@35985
   179
fun no_read _ (_, t) = t;
wenzelm@35985
   180
wenzelm@42375
   181
fun read ctxt (b, str) =
wenzelm@42375
   182
  Syntax.read_prop ctxt str handle ERROR msg =>
wenzelm@42381
   183
    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
wenzelm@35985
   184
wenzelm@35985
   185
fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
wenzelm@35985
   186
  let
wenzelm@42375
   187
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@42375
   188
    val prop = prep ctxt (b, raw_prop);
wenzelm@42375
   189
    val ((_, def), thy') = Thm.add_def ctxt unchecked overloaded (b, prop) thy;
wenzelm@35985
   190
    val thm = def
wenzelm@35985
   191
      |> Thm.forall_intr_frees
wenzelm@35985
   192
      |> Thm.forall_elim_vars 0
wenzelm@35985
   193
      |> Thm.varifyT_global;
wenzelm@35985
   194
  in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
wenzelm@35985
   195
wenzelm@4853
   196
in
wenzelm@35985
   197
wenzelm@35985
   198
val add_defs = add no_read false;
wenzelm@35985
   199
val add_defs_unchecked = add no_read true;
wenzelm@35985
   200
val add_defs_cmd = add read false;
wenzelm@35985
   201
val add_defs_unchecked_cmd = add read true;
wenzelm@35985
   202
wenzelm@4853
   203
end;
wenzelm@4022
   204
wenzelm@3987
   205
end;