src/Pure/global_theory.ML
author wenzelm
Thu Aug 15 16:02:47 2019 +0200 (9 months ago)
changeset 70533 031620901fcd
parent 70494 41108e3e9ca5
child 70543 33749040b6f8
permissions -rw-r--r--
support for (fully reconstructed) proof terms in Scala;
proper cache_typs;
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@70494
    23
  val name_multi: string * Position.T -> 'a list -> ((string * Position.T) * 'a) list
wenzelm@70430
    24
  type name_flags
wenzelm@70430
    25
  val unnamed: name_flags
wenzelm@70427
    26
  val official1: name_flags
wenzelm@70427
    27
  val official2: name_flags
wenzelm@70427
    28
  val unofficial1: name_flags
wenzelm@70427
    29
  val unofficial2: name_flags
wenzelm@70494
    30
  val name_thm: name_flags -> string * Position.T -> thm -> thm
wenzelm@70494
    31
  val name_thms: name_flags -> string * Position.T -> thm list -> thm list
wenzelm@68661
    32
  val check_thms_lazy: thm list lazy -> thm list lazy
wenzelm@67671
    33
  val add_thms_lazy: string -> (binding * thm list lazy) -> theory -> theory
haftmann@29579
    34
  val store_thm: binding * thm -> theory -> thm * theory
haftmann@29579
    35
  val store_thm_open: binding * thm -> theory -> thm * theory
haftmann@29579
    36
  val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
haftmann@29579
    37
  val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
haftmann@29579
    38
  val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
wenzelm@57929
    39
  val add_thms_dynamic': Context.generic -> binding * (Context.generic -> thm list) ->
wenzelm@57929
    40
    theory -> string * theory
haftmann@29579
    41
  val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
wenzelm@67713
    42
  val note_thms: string -> Thm.binding * (thm list * attribute list) list -> theory ->
wenzelm@67713
    43
    (string * thm list) * theory
wenzelm@67713
    44
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list -> theory ->
wenzelm@67713
    45
    (string * thm list) list * theory
haftmann@29579
    46
  val add_defs: bool -> ((binding * term) * attribute list) list ->
haftmann@18377
    47
    theory -> thm list * theory
haftmann@29579
    48
  val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
haftmann@29579
    49
    theory -> thm list * theory
wenzelm@3987
    50
end;
wenzelm@3987
    51
wenzelm@39557
    52
structure Global_Theory: GLOBAL_THEORY =
wenzelm@3987
    53
struct
wenzelm@3987
    54
wenzelm@27198
    55
(** theory data **)
wenzelm@26282
    56
wenzelm@39557
    57
structure Data = Theory_Data
wenzelm@24713
    58
(
wenzelm@49010
    59
  type T = Facts.T;
wenzelm@49010
    60
  val empty = Facts.empty;
wenzelm@49010
    61
  val extend = I;
wenzelm@49010
    62
  val merge = Facts.merge;
wenzelm@24713
    63
);
wenzelm@3987
    64
wenzelm@49010
    65
val facts_of = Data.get;
wenzelm@26666
    66
wenzelm@56003
    67
fun check_fact thy = Facts.check (Context.Theory thy) (facts_of thy);
wenzelm@26666
    68
val intern_fact = Facts.intern o facts_of;
wenzelm@26693
    69
val defined_fact = Facts.defined o facts_of;
wenzelm@16023
    70
wenzelm@57887
    71
fun alias_fact binding name thy =
wenzelm@57887
    72
  Data.map (Facts.alias (Sign.naming_of thy) binding name) thy;
wenzelm@57887
    73
wenzelm@49010
    74
fun hide_fact fully name = Data.map (Facts.hide fully name);
wenzelm@6367
    75
wenzelm@3987
    76
wenzelm@56140
    77
(* retrieve theorems *)
wenzelm@27198
    78
wenzelm@56140
    79
fun get_thms thy xname =
wenzelm@57942
    80
  #thms (Facts.retrieve (Context.Theory thy) (facts_of thy) (xname, Position.none));
wenzelm@26344
    81
wenzelm@56140
    82
fun get_thm thy xname =
wenzelm@56140
    83
  Facts.the_single (xname, Position.none) (get_thms thy xname);
wenzelm@4783
    84
wenzelm@61054
    85
fun transfer_theories thy =
wenzelm@61054
    86
  let
wenzelm@61054
    87
    val theories =
wenzelm@61054
    88
      fold (fn thy' => Symtab.update (Context.theory_name thy', thy'))
wenzelm@61054
    89
        (Theory.nodes_of thy) Symtab.empty;
wenzelm@61054
    90
    fun transfer th =
wenzelm@65458
    91
      Thm.transfer (the_default thy (Symtab.lookup theories (Thm.theory_name th))) th;
wenzelm@61054
    92
  in transfer end;
wenzelm@61054
    93
wenzelm@56161
    94
fun all_thms_of thy verbose =
wenzelm@56161
    95
  let
wenzelm@61054
    96
    val transfer = transfer_theories thy;
wenzelm@56161
    97
    val facts = facts_of thy;
wenzelm@56161
    98
    fun add (name, ths) =
wenzelm@56161
    99
      if not verbose andalso Facts.is_concealed facts name then I
wenzelm@61054
   100
      else append (map (`(Thm.get_name_hint) o transfer) ths);
wenzelm@56161
   101
  in Facts.fold_static add facts [] end;
wenzelm@16336
   102
wenzelm@4022
   103
wenzelm@4022
   104
wenzelm@26488
   105
(** store theorems **)
wenzelm@3987
   106
wenzelm@21580
   107
(* fact specifications *)
wenzelm@21580
   108
wenzelm@21580
   109
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   110
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   111
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   112
wenzelm@21580
   113
wenzelm@70430
   114
(* name theorems *)
wenzelm@4853
   115
wenzelm@70430
   116
abstype name_flags = No_Name_Flags | Name_Flags of {pre: bool, official: bool}
wenzelm@70430
   117
with
berghofe@12235
   118
wenzelm@70430
   119
val unnamed = No_Name_Flags;
wenzelm@70430
   120
val official1 = Name_Flags {pre = true, official = true};
wenzelm@70430
   121
val official2 = Name_Flags {pre = false, official = true};
wenzelm@70430
   122
val unofficial1 = Name_Flags {pre = true, official = false};
wenzelm@70430
   123
val unofficial2 = Name_Flags {pre = false, official = false};
wenzelm@70427
   124
wenzelm@70494
   125
fun name_thm name_flags (name, pos) =
wenzelm@70459
   126
  Thm.solve_constraints #> (fn thm =>
wenzelm@70459
   127
    (case name_flags of
wenzelm@70459
   128
      No_Name_Flags => thm
wenzelm@70459
   129
    | Name_Flags {pre, official} =>
wenzelm@70459
   130
        thm
wenzelm@70459
   131
        |> (official andalso (not pre orelse Thm.derivation_name thm = "")) ?
wenzelm@70494
   132
            Thm.name_derivation (name, pos)
wenzelm@70459
   133
        |> (name <> "" andalso (not pre orelse not (Thm.has_name_hint thm))) ?
wenzelm@70459
   134
            Thm.put_name_hint name));
berghofe@12872
   135
wenzelm@70430
   136
end;
wenzelm@70427
   137
wenzelm@70494
   138
fun name_multi (name, pos: Position.T) xs =
wenzelm@70494
   139
  (case xs of
wenzelm@70494
   140
    [x] => [((name, pos), x)]
wenzelm@70494
   141
  | _ =>
wenzelm@70494
   142
      if name = "" then map (pair ("", pos)) xs
wenzelm@70494
   143
      else map_index (fn (i, x) => ((name ^ "_" ^ string_of_int (i + 1), pos), x)) xs);
wenzelm@70427
   144
wenzelm@70494
   145
fun name_thms name_flags name_pos thms =
wenzelm@70494
   146
  map (uncurry (name_thm name_flags)) (name_multi name_pos thms);
berghofe@12235
   147
wenzelm@4853
   148
wenzelm@70424
   149
(* apply theorems and attributes *)
wenzelm@4853
   150
wenzelm@67671
   151
fun register_proofs thms thy = (thms, Thm.register_proofs (Lazy.value thms) thy);
wenzelm@49010
   152
wenzelm@70494
   153
fun bind_name thy b = (Sign.full_name thy b, Binding.default_pos_of b);
wenzelm@70494
   154
wenzelm@68244
   155
fun add_facts (b, fact) thy =
wenzelm@68244
   156
  let
wenzelm@70494
   157
    val (full_name, pos) = bind_name thy b;
wenzelm@68540
   158
    fun check fact =
wenzelm@68540
   159
      fact |> map_index (fn (i, thm) =>
wenzelm@68540
   160
        let
wenzelm@68540
   161
          fun err msg =
wenzelm@68540
   162
            error ("Malformed global fact " ^
wenzelm@68540
   163
              quote (full_name ^
wenzelm@68540
   164
                (if length fact = 1 then "" else "(" ^ string_of_int (i + 1) ^ ")")) ^
wenzelm@68540
   165
              Position.here pos ^ "\n" ^ msg);
wenzelm@68540
   166
          val prop = Thm.plain_prop_of thm
wenzelm@68540
   167
            handle THM _ =>
wenzelm@68540
   168
              thm
wenzelm@68540
   169
              |> Thm.check_hyps (Context.Theory thy)
wenzelm@68540
   170
              |> Thm.full_prop_of;
wenzelm@68540
   171
        in
wenzelm@68540
   172
          ignore (Logic.unvarify_global (Term_Subst.zero_var_indexes prop))
wenzelm@68540
   173
            handle TYPE (msg, _, _) => err msg
wenzelm@68540
   174
              | TERM (msg, _) => err msg
wenzelm@68540
   175
              | ERROR msg => err msg
wenzelm@68540
   176
        end);
wenzelm@68540
   177
    val arg = (b, Lazy.map_finished (tap check) fact);
wenzelm@68244
   178
  in
wenzelm@68244
   179
    thy |> Data.map (Facts.add_static (Context.Theory thy) {strict = true, index = false} arg #> #2)
wenzelm@68244
   180
  end;
wenzelm@67671
   181
wenzelm@68661
   182
fun check_thms_lazy (thms: thm list lazy) =
wenzelm@68701
   183
  if Proofterm.proofs_enabled () orelse Options.default_bool "strict_facts"
wenzelm@68701
   184
  then Lazy.force_value thms else thms;
wenzelm@68661
   185
wenzelm@67671
   186
fun add_thms_lazy kind (b, thms) thy =
wenzelm@68661
   187
  if Binding.is_empty b then Thm.register_proofs (check_thms_lazy thms) thy
wenzelm@30211
   188
  else
wenzelm@30211
   189
    let
wenzelm@70494
   190
      val name_pos = bind_name thy b;
wenzelm@68661
   191
      val thms' =
wenzelm@68661
   192
        check_thms_lazy thms
wenzelm@70494
   193
        |> Lazy.map_finished (name_thms official1 name_pos #> map (Thm.kind_rule kind));
wenzelm@67671
   194
    in thy |> Thm.register_proofs thms' |> add_facts (b, thms') end;
wenzelm@67671
   195
wenzelm@70424
   196
val app_facts =
wenzelm@70424
   197
  apfst flat oo fold_map (fn (thms, atts) => fold_map (Thm.theory_attributes atts) thms);
wenzelm@70424
   198
wenzelm@70430
   199
fun apply_facts name_flags1 name_flags2 (b, facts) thy =
wenzelm@70424
   200
  if Binding.is_empty b then app_facts facts thy |-> register_proofs
wenzelm@67671
   201
  else
wenzelm@67671
   202
    let
wenzelm@70494
   203
      val name_pos= bind_name thy b;
wenzelm@70424
   204
      val (thms', thy') = thy
wenzelm@70494
   205
        |> app_facts (map (apfst (name_thms name_flags1 name_pos)) facts)
wenzelm@70494
   206
        |>> name_thms name_flags2 name_pos |-> register_proofs;
wenzelm@67671
   207
      val thy'' = thy' |> add_facts (b, Lazy.value thms');
wenzelm@67663
   208
    in (map (Thm.transfer thy'') thms', thy'') end;
wenzelm@26488
   209
wenzelm@26488
   210
wenzelm@67715
   211
(* store_thm *)
wenzelm@26488
   212
wenzelm@67715
   213
fun store_thm (b, th) =
wenzelm@70430
   214
  apply_facts official1 official2 (b, [([th], [])]) #>> the_single;
wenzelm@26488
   215
haftmann@29579
   216
fun store_thm_open (b, th) =
wenzelm@70430
   217
  apply_facts unofficial1 unofficial2 (b, [([th], [])]) #>> the_single;
wenzelm@3987
   218
wenzelm@16023
   219
wenzelm@6091
   220
(* add_thms(s) *)
wenzelm@4853
   221
wenzelm@70428
   222
val add_thmss =
wenzelm@70430
   223
  fold_map (fn ((b, thms), atts) => apply_facts official1 official2 (b, [(thms, atts)]));
wenzelm@5907
   224
wenzelm@70428
   225
fun add_thms args =
wenzelm@70428
   226
  add_thmss (map (apfst (apsnd single)) args) #>> map the_single;
berghofe@12235
   227
haftmann@27683
   228
val add_thm = yield_singleton add_thms;
wenzelm@5907
   229
wenzelm@5907
   230
wenzelm@57929
   231
(* dynamic theorems *)
wenzelm@26488
   232
wenzelm@57929
   233
fun add_thms_dynamic' context arg thy =
wenzelm@57929
   234
  let val (name, facts') = Facts.add_dynamic context arg (Data.get thy)
wenzelm@57929
   235
  in (name, Data.put facts' thy) end;
wenzelm@57929
   236
wenzelm@57929
   237
fun add_thms_dynamic arg thy =
wenzelm@57929
   238
  add_thms_dynamic' (Context.Theory thy) arg thy |> snd;
wenzelm@26488
   239
wenzelm@26488
   240
wenzelm@27728
   241
(* note_thmss *)
wenzelm@5907
   242
wenzelm@67713
   243
fun note_thms kind ((b, more_atts), facts) thy =
wenzelm@12711
   244
  let
haftmann@28965
   245
    val name = Sign.full_name thy b;
wenzelm@70424
   246
    val facts' = facts |> map (apsnd (fn atts => surround (Thm.kind kind) (atts @ more_atts)));
wenzelm@70430
   247
    val (thms', thy') = thy |> apply_facts official1 official2 (b, facts');
wenzelm@70424
   248
  in ((name, thms'), thy') end;
wenzelm@67713
   249
wenzelm@67713
   250
val note_thmss = fold_map o note_thms;
wenzelm@12711
   251
wenzelm@5280
   252
wenzelm@62170
   253
(* old-style defs *)
wenzelm@4022
   254
wenzelm@4853
   255
local
wenzelm@35985
   256
wenzelm@62169
   257
fun add unchecked overloaded = fold_map (fn ((b, prop), atts) => fn thy =>
wenzelm@35985
   258
  let
wenzelm@62170
   259
    val context = Defs.global_context thy;
wenzelm@61262
   260
    val ((_, def), thy') = Thm.add_def context unchecked overloaded (b, prop) thy;
wenzelm@35985
   261
    val thm = def
wenzelm@35985
   262
      |> Thm.forall_intr_frees
wenzelm@35985
   263
      |> Thm.forall_elim_vars 0
wenzelm@35985
   264
      |> Thm.varifyT_global;
wenzelm@70430
   265
  in thy' |> apply_facts unnamed official2 (b, [([thm], atts)]) |>> the_single end);
wenzelm@35985
   266
wenzelm@4853
   267
in
wenzelm@35985
   268
wenzelm@62169
   269
val add_defs = add false;
wenzelm@62169
   270
val add_defs_unchecked = add true;
wenzelm@35985
   271
wenzelm@4853
   272
end;
wenzelm@4022
   273
wenzelm@3987
   274
end;