src/Pure/pure_thy.ML
author wenzelm
Sat Apr 04 11:42:26 1998 +0200 (1998-04-04)
changeset 4788 b54c337f2c7f
parent 4783 ca29125de4af
child 4792 8e3c2dddb9c8
permissions -rw-r--r--
added Goal_def;
wenzelm@3987
     1
(*  Title:      Pure/pure_thy.ML
wenzelm@3987
     2
    ID:         $Id$
wenzelm@3987
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@3987
     4
wenzelm@4783
     5
The Pure theories.
wenzelm@4783
     6
wenzelm@4783
     7
TODO:
wenzelm@4783
     8
  - tagged axioms / defs;
wenzelm@3987
     9
*)
wenzelm@3987
    10
wenzelm@4022
    11
signature BASIC_PURE_THY =
wenzelm@4022
    12
sig
wenzelm@4022
    13
  val get_thm: theory -> xstring -> thm
wenzelm@4022
    14
  val get_thms: theory -> xstring -> thm list
wenzelm@4022
    15
  val thms_of: theory -> (string * thm) list
wenzelm@4022
    16
end
wenzelm@4022
    17
wenzelm@3987
    18
signature PURE_THY =
wenzelm@3987
    19
sig
wenzelm@4022
    20
  include BASIC_PURE_THY
wenzelm@4783
    21
  val get_tthm: theory -> xstring -> tthm
wenzelm@4783
    22
  val get_tthms: theory -> xstring -> tthm list
wenzelm@4022
    23
  val thms_containing: theory -> string list -> (string * thm) list
wenzelm@4022
    24
  val store_thms: (bstring * thm) list -> theory -> theory
wenzelm@4022
    25
  val store_thmss: (bstring * thm list) list -> theory -> theory
wenzelm@4783
    26
  val store_tthms: (bstring * tthm) list -> theory -> theory
wenzelm@4783
    27
  val store_tthmss: (bstring * tthm list) list -> theory -> theory
wenzelm@4022
    28
  val smart_store_thm: (bstring * thm) -> thm
wenzelm@4022
    29
  val add_store_axioms: (bstring * string) list -> theory -> theory
wenzelm@4022
    30
  val add_store_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@4022
    31
  val add_store_defs: (bstring * string) list -> theory -> theory
wenzelm@4022
    32
  val add_store_defs_i: (bstring * term) list -> theory -> theory
wenzelm@3987
    33
  val proto_pure: theory
wenzelm@3987
    34
  val pure: theory
wenzelm@3987
    35
  val cpure: theory
wenzelm@3987
    36
end;
wenzelm@3987
    37
wenzelm@3987
    38
structure PureThy: PURE_THY =
wenzelm@3987
    39
struct
wenzelm@3987
    40
wenzelm@3987
    41
wenzelm@4022
    42
(*** init theorems data ***)
wenzelm@3987
    43
wenzelm@4783
    44
(** data kind 'theorems' **)
wenzelm@3987
    45
wenzelm@4783
    46
val theoremsK = "Pure/theorems";
wenzelm@3987
    47
wenzelm@3987
    48
exception Theorems of
wenzelm@4022
    49
 {space: NameSpace.T,
wenzelm@4783
    50
  thms_tab: tthm list Symtab.table,
wenzelm@4783
    51
  const_idx: int * (int * tthm) list Symtab.table} ref;
wenzelm@3987
    52
wenzelm@3987
    53
wenzelm@3987
    54
(* methods *)
wenzelm@3987
    55
wenzelm@3987
    56
local
wenzelm@3987
    57
wenzelm@4022
    58
fun mk_empty _ =
wenzelm@4022
    59
  Theorems (ref {space = NameSpace.empty,
wenzelm@4487
    60
    thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)});
wenzelm@3987
    61
wenzelm@4258
    62
fun print sg (Theorems (ref {space, thms_tab, const_idx = _})) =
wenzelm@3987
    63
  let
wenzelm@4783
    64
    val prt_thm = Attribute.pretty_tthm o apfst (Thm.transfer_sg sg);
wenzelm@3987
    65
    fun prt_thms (name, [th]) =
wenzelm@4037
    66
          Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
wenzelm@3987
    67
      | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
wenzelm@3987
    68
wenzelm@3987
    69
    fun extrn name =
wenzelm@3987
    70
      if ! long_names then name else NameSpace.extern space name;
wenzelm@3987
    71
    val thmss = sort_wrt fst (map (apfst extrn) (Symtab.dest thms_tab));
wenzelm@3987
    72
  in
wenzelm@4498
    73
    Pretty.writeln (Display.pretty_name_space ("theorem name space", space));
wenzelm@3987
    74
    Pretty.writeln (Pretty.big_list "theorems:" (map prt_thms thmss))
wenzelm@3987
    75
  end;
wenzelm@3987
    76
wenzelm@3987
    77
in
wenzelm@3987
    78
wenzelm@4783
    79
val theorems_setup = Theory.init_data [(theoremsK, (mk_empty (), mk_empty, mk_empty, print))];
wenzelm@3987
    80
wenzelm@3987
    81
end;
wenzelm@3987
    82
wenzelm@3987
    83
wenzelm@3987
    84
(* get data record *)
wenzelm@3987
    85
wenzelm@4022
    86
fun get_theorems_sg sg =
wenzelm@4049
    87
  (case Sign.get_data sg theoremsK of
wenzelm@3987
    88
    Theorems r => r
wenzelm@4022
    89
  | _ => sys_error "get_theorems_sg");
wenzelm@4022
    90
wenzelm@4783
    91
val get_theorems = get_theorems_sg o Theory.sign_of;
wenzelm@4022
    92
wenzelm@3987
    93
wenzelm@3987
    94
wenzelm@4022
    95
(** retrieve theorems **)
wenzelm@3987
    96
wenzelm@4022
    97
fun local_thms thy name =
wenzelm@3987
    98
  let
wenzelm@4022
    99
    val ref {space, thms_tab, ...} = get_theorems thy;
wenzelm@4022
   100
    val full_name = NameSpace.intern space name;
wenzelm@4022
   101
  in Symtab.lookup (thms_tab, full_name) end;
wenzelm@4022
   102
wenzelm@4022
   103
fun all_local_thms [] _ = None
wenzelm@4022
   104
  | all_local_thms (thy :: thys) name =
wenzelm@4022
   105
      (case local_thms thy name of
wenzelm@4022
   106
        None => all_local_thms thys name
wenzelm@4022
   107
      | some => some);
wenzelm@4022
   108
wenzelm@3987
   109
wenzelm@4783
   110
(* get_thms etc. *)
wenzelm@4037
   111
wenzelm@4783
   112
fun get_tthms thy name =
wenzelm@4022
   113
  (case all_local_thms (thy :: Theory.ancestors_of thy) name of
wenzelm@4590
   114
    None => raise THEORY ("Unknown theorem(s) " ^ quote name, [thy])
wenzelm@4022
   115
  | Some thms => thms);
wenzelm@3987
   116
wenzelm@4783
   117
fun get_tthm thy name =
wenzelm@4783
   118
  (case get_tthms thy name of
wenzelm@3987
   119
    [thm] => thm
wenzelm@4590
   120
  | _ => raise THEORY ("Single theorem expected " ^ quote name, [thy]));
wenzelm@4022
   121
wenzelm@4783
   122
fun get_thms thy = map Attribute.thm_of o get_tthms thy;
wenzelm@4783
   123
fun get_thm thy = Attribute.thm_of o get_tthm thy;
wenzelm@4783
   124
wenzelm@4022
   125
wenzelm@4022
   126
(* thms_of *)
wenzelm@4022
   127
wenzelm@4783
   128
fun attach_name (thm, _) = (Thm.name_of_thm thm, thm);
wenzelm@4022
   129
wenzelm@4022
   130
fun thms_of thy =
wenzelm@4022
   131
  let val ref {thms_tab, ...} = get_theorems thy in
wenzelm@4022
   132
    map attach_name (flat (map snd (Symtab.dest thms_tab)))
wenzelm@4022
   133
  end;
wenzelm@4022
   134
wenzelm@3987
   135
wenzelm@3987
   136
wenzelm@4022
   137
(** theorems indexed by constants **)
wenzelm@4022
   138
wenzelm@4022
   139
(* make index *)
wenzelm@4022
   140
wenzelm@4037
   141
val ignore = ["Trueprop", "all", "==>", "=="];
wenzelm@4022
   142
wenzelm@4783
   143
fun add_const_idx ((next, table), tthm as (thm, _)) =
wenzelm@4022
   144
  let
wenzelm@4022
   145
    val {hyps, prop, ...} = Thm.rep_thm thm;
wenzelm@4022
   146
    val consts =
wenzelm@4037
   147
      foldr add_term_consts (hyps, add_term_consts (prop, [])) \\ ignore;
wenzelm@4022
   148
wenzelm@4022
   149
    fun add (tab, c) =
wenzelm@4783
   150
      Symtab.update ((c, (next, tthm) :: Symtab.lookup_multi (tab, c)), tab);
wenzelm@4022
   151
  in (next + 1, foldl add (table, consts)) end;
wenzelm@4022
   152
wenzelm@4022
   153
fun make_const_idx thm_tab =
wenzelm@4487
   154
  foldl (foldl add_const_idx) ((0, Symtab.empty), map snd (Symtab.dest thm_tab));
wenzelm@4022
   155
wenzelm@4022
   156
wenzelm@4022
   157
(* lookup index *)
wenzelm@3987
   158
wenzelm@4022
   159
(*search locally*)
wenzelm@4022
   160
fun containing [] thy = thms_of thy
wenzelm@4022
   161
  | containing consts thy =
wenzelm@4022
   162
      let
wenzelm@4037
   163
        fun int ([], _) = []
wenzelm@4037
   164
          | int (_, []) = []
wenzelm@4037
   165
          | int (xxs as ((x as (i:int, _)) :: xs), yys as ((y as (j, _)) :: ys)) =
wenzelm@4037
   166
              if i = j then x :: int (xs, ys)
wenzelm@4037
   167
              else if i > j then int (xs, yys)
wenzelm@4037
   168
              else int (xxs, ys);
wenzelm@4022
   169
wenzelm@4037
   170
        fun ints [xs] = xs
wenzelm@4037
   171
          | ints xss = if exists null xss then [] else foldl int (hd xss, tl xss);
wenzelm@4022
   172
wenzelm@4022
   173
        val ref {const_idx = (_, ctab), ...} = get_theorems thy;
wenzelm@4022
   174
        val ithmss = map (fn c => Symtab.lookup_multi (ctab, c)) consts;
wenzelm@4022
   175
      in
wenzelm@4037
   176
        map (attach_name o snd) (ints ithmss)
wenzelm@4022
   177
      end;
wenzelm@4022
   178
wenzelm@4022
   179
(*search globally*)
wenzelm@4022
   180
fun thms_containing thy consts =
wenzelm@4037
   181
  flat (map (containing (consts \\ ignore)) (thy :: Theory.ancestors_of thy));
wenzelm@4022
   182
wenzelm@4022
   183
wenzelm@4022
   184
wenzelm@4022
   185
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   186
wenzelm@3987
   187
fun warn_overwrite name =
wenzelm@3987
   188
  warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@3987
   189
wenzelm@3987
   190
fun warn_same name =
wenzelm@3987
   191
  warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   192
wenzelm@4783
   193
fun enter_tthms sg single (raw_name, tthms) =
wenzelm@3987
   194
  let
wenzelm@4783
   195
    val len = length tthms;
wenzelm@3987
   196
    val name = Sign.full_name sg raw_name;
wenzelm@3987
   197
    val names =
wenzelm@3987
   198
      if single then replicate len name
wenzelm@3987
   199
      else map (fn i => name ^ "_" ^ string_of_int i) (0 upto (len - 1));
wenzelm@4783
   200
    val named_tthms = map2 (fn (x, (th, tg)) => (Thm.name_thm (x, th), tg)) (names, tthms);
wenzelm@3987
   201
wenzelm@4783
   202
    fun eq_tthm ((th1, _), (th2, _)) = Thm.eq_thm (th1, th2);
wenzelm@4022
   203
wenzelm@4022
   204
    val r as ref {space, thms_tab, const_idx} = get_theorems_sg sg;
wenzelm@4022
   205
wenzelm@4022
   206
    val overwrite =
wenzelm@4022
   207
      (case Symtab.lookup (thms_tab, name) of
wenzelm@4022
   208
        None => false
wenzelm@4783
   209
      | Some tthms' =>
wenzelm@4783
   210
          if length tthms' = len andalso forall2 eq_tthm (tthms', named_tthms) then
wenzelm@4022
   211
            (warn_same name; false)
wenzelm@4022
   212
          else (warn_overwrite name; true));
wenzelm@4022
   213
wenzelm@4487
   214
    val space' = NameSpace.extend (space, [name]);
wenzelm@4783
   215
    val thms_tab' = Symtab.update ((name, named_tthms), thms_tab);
wenzelm@4022
   216
    val const_idx' =
wenzelm@4022
   217
      if overwrite then make_const_idx thms_tab'
wenzelm@4783
   218
      else foldl add_const_idx (const_idx, named_tthms);
wenzelm@3987
   219
  in
wenzelm@4022
   220
    r := {space = space', thms_tab = thms_tab', const_idx = const_idx'};
wenzelm@4783
   221
    named_tthms
wenzelm@3987
   222
  end;
wenzelm@3987
   223
wenzelm@4783
   224
fun do_enter_tthms sg single tthms = (enter_tthms sg single tthms; ());
wenzelm@3987
   225
wenzelm@3987
   226
wenzelm@4783
   227
fun store_tthmss tthmss thy =
wenzelm@4783
   228
  (seq (do_enter_tthms (Theory.sign_of thy) false) tthmss; thy);
wenzelm@3987
   229
wenzelm@4783
   230
fun store_tthms tthms thy =
wenzelm@4783
   231
  (seq (do_enter_tthms (Theory.sign_of thy) true) (map (apsnd (fn th => [th])) tthms); thy);
wenzelm@4783
   232
wenzelm@4783
   233
fun store_thmss thmss = store_tthmss (map (apsnd (map Attribute.tthm_of)) thmss);
wenzelm@4783
   234
fun store_thms thms = store_tthms (map (apsnd Attribute.tthm_of) thms);
wenzelm@3987
   235
wenzelm@4012
   236
fun smart_store_thm (name, thm) =
wenzelm@4783
   237
  let val [(thm', _)] = enter_tthms (Thm.sign_of_thm thm) true (name, [Attribute.tthm_of thm])
wenzelm@4783
   238
  in thm' end;
wenzelm@3987
   239
wenzelm@3987
   240
wenzelm@4022
   241
(* store axioms as theorems *)
wenzelm@4022
   242
wenzelm@4022
   243
fun add_store add named_axs thy =
wenzelm@4022
   244
  let
wenzelm@4022
   245
    val thy' = add named_axs thy;
wenzelm@4037
   246
    val named_thms = map (fn (name, _) => (name, get_axiom thy' name)) named_axs;
wenzelm@4022
   247
  in store_thms named_thms thy' end;
wenzelm@4022
   248
wenzelm@4022
   249
val add_store_axioms = add_store Theory.add_axioms;
wenzelm@4022
   250
val add_store_axioms_i = add_store Theory.add_axioms_i;
wenzelm@4022
   251
val add_store_defs = add_store Theory.add_defs;
wenzelm@4022
   252
val add_store_defs_i = add_store Theory.add_defs_i;
wenzelm@4022
   253
wenzelm@4022
   254
wenzelm@3987
   255
wenzelm@3987
   256
(** the Pure theories **)
wenzelm@3987
   257
wenzelm@3987
   258
val proto_pure =
wenzelm@3987
   259
  Theory.pre_pure
wenzelm@4788
   260
  |> Theory.setup [Attribute.setup, theorems_setup]
wenzelm@3987
   261
  |> Theory.add_types
wenzelm@3987
   262
   (("fun", 2, NoSyn) ::
wenzelm@3987
   263
    ("prop", 0, NoSyn) ::
wenzelm@3987
   264
    ("itself", 1, NoSyn) ::
wenzelm@3987
   265
    Syntax.pure_types)
wenzelm@3987
   266
  |> Theory.add_classes_i [(logicC, [])]
wenzelm@3987
   267
  |> Theory.add_defsort_i logicS
wenzelm@3987
   268
  |> Theory.add_arities_i
wenzelm@3987
   269
   [("fun", [logicS, logicS], logicS),
wenzelm@3987
   270
    ("prop", [], logicS),
wenzelm@3987
   271
    ("itself", [logicS], logicS)]
wenzelm@3987
   272
  |> Theory.add_syntax Syntax.pure_syntax
wenzelm@3987
   273
  |> Theory.add_modesyntax ("symbols", true) Syntax.pure_sym_syntax
wenzelm@3987
   274
  |> Theory.add_trfuns Syntax.pure_trfuns
wenzelm@3987
   275
  |> Theory.add_trfunsT Syntax.pure_trfunsT
wenzelm@3987
   276
  |> Theory.add_syntax
wenzelm@3987
   277
   [("==>", "[prop, prop] => prop", Delimfix "op ==>")]
wenzelm@3987
   278
  |> Theory.add_consts
wenzelm@3987
   279
   [("==", "['a::{}, 'a] => prop", InfixrName ("==", 2)),
wenzelm@3987
   280
    ("=?=", "['a::{}, 'a] => prop", InfixrName ("=?=", 2)),
wenzelm@3987
   281
    ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@3987
   282
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@4788
   283
    ("Goal", "prop => prop", Mixfix ("GOAL _", [999], 1000)),
wenzelm@3987
   284
    ("TYPE", "'a itself", NoSyn)]
wenzelm@4022
   285
  |> Theory.add_path "ProtoPure"
wenzelm@4788
   286
  |> add_store_defs
wenzelm@4788
   287
   [("flexpair_def", "(t =?= u) == (t == u::'a::{})"),
wenzelm@4788
   288
    ("Goal_def", "GOAL (PROP A) == PROP A")]
wenzelm@3987
   289
  |> Theory.add_name "ProtoPure";
wenzelm@3987
   290
wenzelm@4022
   291
val pure =
wenzelm@4022
   292
  Theory.prep_ext_merge [proto_pure]
wenzelm@3987
   293
  |> Theory.add_syntax Syntax.pure_appl_syntax
wenzelm@4022
   294
  |> Theory.add_path "Pure"
wenzelm@3987
   295
  |> Theory.add_name "Pure";
wenzelm@3987
   296
wenzelm@4022
   297
val cpure =
wenzelm@4022
   298
  Theory.prep_ext_merge [proto_pure]
wenzelm@3987
   299
  |> Theory.add_syntax Syntax.pure_applC_syntax
wenzelm@4022
   300
  |> Theory.add_path "CPure"
wenzelm@3987
   301
  |> Theory.add_name "CPure";
wenzelm@3987
   302
wenzelm@3987
   303
wenzelm@3987
   304
end;
wenzelm@3987
   305
wenzelm@3987
   306
wenzelm@4022
   307
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   308
open BasicPureThy;
wenzelm@4022
   309
wenzelm@4022
   310
wenzelm@3987
   311
wenzelm@3987
   312
(** theory structures **)
wenzelm@3987
   313
wenzelm@3987
   314
structure ProtoPure =
wenzelm@3987
   315
struct
wenzelm@3987
   316
  val thy = PureThy.proto_pure;
wenzelm@3987
   317
  val flexpair_def = get_axiom thy "flexpair_def";
wenzelm@4788
   318
  val Goal_def = get_axiom thy "Goal_def";
wenzelm@3987
   319
end;
wenzelm@3987
   320
wenzelm@3987
   321
structure Pure = struct val thy = PureThy.pure end;
wenzelm@3987
   322
structure CPure = struct val thy = PureThy.cpure end;