src/Pure/pure_thy.ML
author haftmann
Tue Sep 06 08:30:43 2005 +0200 (2005-09-06)
changeset 17271 2756a73f63a5
parent 17221 6cd180204582
child 17342 92504e2f6c07
permissions -rw-r--r--
introduced some new-style AList operations
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@16441
     5
Theorem storage.  The ProtoPure theory.
wenzelm@3987
     6
*)
wenzelm@3987
     7
wenzelm@4022
     8
signature BASIC_PURE_THY =
wenzelm@4022
     9
sig
wenzelm@16493
    10
  datatype interval = FromTo of int * int | From of int | Single of int
wenzelm@16493
    11
  datatype thmref = Name of string | NameSelection of string * interval list
wenzelm@5000
    12
  val print_theorems: theory -> unit
wenzelm@5000
    13
  val print_theory: theory -> unit
berghofe@15456
    14
  val get_thm: theory -> thmref -> thm
berghofe@15456
    15
  val get_thms: theory -> thmref -> thm list
berghofe@15456
    16
  val get_thmss: theory -> thmref list -> thm list
wenzelm@5091
    17
  structure ProtoPure:
wenzelm@5091
    18
    sig
wenzelm@5091
    19
      val thy: theory
wenzelm@5091
    20
      val Goal_def: thm
wenzelm@5091
    21
    end
wenzelm@4853
    22
end;
wenzelm@4022
    23
wenzelm@3987
    24
signature PURE_THY =
wenzelm@3987
    25
sig
wenzelm@4022
    26
  include BASIC_PURE_THY
wenzelm@16023
    27
  val string_of_thmref: thmref -> string
wenzelm@16493
    28
  val print_theorems_diff: theory -> theory -> unit
berghofe@15456
    29
  val get_thm_closure: theory -> thmref -> thm
berghofe@15456
    30
  val get_thms_closure: theory -> thmref -> thm list
wenzelm@9564
    31
  val single_thm: string -> thm list -> thm
wenzelm@16493
    32
  val name_of_thmref: thmref -> string
wenzelm@16493
    33
  val map_name_of_thmref: (string -> string) -> thmref -> thmref
berghofe@15456
    34
  val select_thm: thmref -> thm list -> thm list
wenzelm@16023
    35
  val selections: string * thm list -> (thmref * thm) list
wenzelm@17162
    36
  val theorems_of: theory -> thm list NameSpace.table
wenzelm@17162
    37
  val theorem_space: theory -> NameSpace.T
wenzelm@16023
    38
  val fact_index_of: theory -> FactIndex.T
wenzelm@16023
    39
  val valid_thms: theory -> thmref * thm list -> bool
wenzelm@16023
    40
  val thms_containing: theory -> FactIndex.spec -> (string * thm list) list
nipkow@13646
    41
  val thms_containing_consts: theory -> string list -> (string * thm) list
wenzelm@16336
    42
  val thms_of: theory -> (string * thm) list
wenzelm@16336
    43
  val all_thms_of: theory -> (string * thm) list
wenzelm@12695
    44
  val hide_thms: bool -> string list -> theory -> theory
wenzelm@6091
    45
  val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm
wenzelm@7405
    46
  val smart_store_thms: (bstring * thm list) -> thm list
wenzelm@12138
    47
  val smart_store_thms_open: (bstring * thm list) -> thm list
wenzelm@7899
    48
  val forall_elim_var: int -> thm -> thm
wenzelm@7899
    49
  val forall_elim_vars: int -> thm -> thm
wenzelm@8419
    50
  val add_thms: ((bstring * thm) * theory attribute list) list -> theory -> theory * thm list
wenzelm@12711
    51
  val add_thmss: ((bstring * thm list) * theory attribute list) list -> theory
wenzelm@12711
    52
    -> theory * thm list list
wenzelm@16132
    53
  val note_thmss: theory attribute -> ((bstring * theory attribute list) *
wenzelm@16441
    54
    (thmref * theory attribute list) list) list ->
wenzelm@16441
    55
    theory -> theory * (bstring * thm list) list
wenzelm@16132
    56
  val note_thmss_i: theory attribute -> ((bstring * theory attribute list) *
wenzelm@16441
    57
    (thm list * theory attribute list) list) list ->
wenzelm@16441
    58
    theory -> theory * (bstring * thm list) list
wenzelm@16441
    59
  val add_axioms: ((bstring * string) * theory attribute list) list ->
wenzelm@16441
    60
    theory -> theory * thm list
wenzelm@16441
    61
  val add_axioms_i: ((bstring * term) * theory attribute list) list ->
wenzelm@16441
    62
    theory -> theory * thm list
wenzelm@16441
    63
  val add_axiomss: ((bstring * string list) * theory attribute list) list ->
wenzelm@16441
    64
    theory -> theory * thm list list
wenzelm@16441
    65
  val add_axiomss_i: ((bstring * term list) * theory attribute list) list ->
wenzelm@16441
    66
    theory -> theory * thm list list
wenzelm@16441
    67
  val add_defs: bool -> ((bstring * string) * theory attribute list) list ->
wenzelm@16441
    68
    theory -> theory * thm list
wenzelm@16441
    69
  val add_defs_i: bool -> ((bstring * term) * theory attribute list) list ->
wenzelm@16441
    70
    theory -> theory * thm list
wenzelm@16441
    71
  val add_defss: bool -> ((bstring * string list) * theory attribute list) list ->
wenzelm@16441
    72
    theory -> theory * thm list list
wenzelm@16441
    73
  val add_defss_i: bool -> ((bstring * term list) * theory attribute list) list ->
wenzelm@16441
    74
    theory -> theory * thm list list
wenzelm@3987
    75
end;
wenzelm@3987
    76
wenzelm@3987
    77
structure PureThy: PURE_THY =
wenzelm@3987
    78
struct
wenzelm@3987
    79
wenzelm@3987
    80
wenzelm@4922
    81
(*** theorem database ***)
wenzelm@3987
    82
wenzelm@16441
    83
(** dataype theorems **)
wenzelm@3987
    84
wenzelm@16493
    85
fun pretty_theorems_diff thy prev_thms (space, thms) =
wenzelm@16441
    86
  let
wenzelm@16441
    87
    val prt_thm = Display.pretty_thm_sg thy;
wenzelm@16441
    88
    fun prt_thms (name, [th]) =
wenzelm@16441
    89
          Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
wenzelm@16441
    90
      | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
wenzelm@16493
    91
wenzelm@16493
    92
    val diff_thmss = Symtab.fold (fn fact =>
wenzelm@16493
    93
      if not (Symtab.member eq_thms prev_thms fact) then cons fact else I) thms [];
wenzelm@16493
    94
    val thmss = diff_thmss |> map (apfst (NameSpace.extern space)) |> Library.sort_wrt #1;
wenzelm@16441
    95
  in Pretty.big_list "theorems:" (map prt_thms thmss) end;
wenzelm@16441
    96
wenzelm@16493
    97
fun pretty_theorems thy = pretty_theorems_diff thy Symtab.empty;
wenzelm@16493
    98
wenzelm@16441
    99
structure TheoremsData = TheoryDataFun
wenzelm@16441
   100
(struct
wenzelm@5005
   101
  val name = "Pure/theorems";
wenzelm@5005
   102
  type T =
wenzelm@16441
   103
   {theorems: thm list NameSpace.table,
wenzelm@16441
   104
    index: FactIndex.T} ref;
wenzelm@3987
   105
wenzelm@4853
   106
  fun mk_empty _ =
wenzelm@16336
   107
    ref {theorems = NameSpace.empty_table, index = FactIndex.empty}: T;
wenzelm@3987
   108
wenzelm@5005
   109
  val empty = mk_empty ();
wenzelm@6547
   110
  fun copy (ref x) = ref x;
wenzelm@16441
   111
  val extend = mk_empty;
wenzelm@16441
   112
  fun merge _ = mk_empty;
wenzelm@16441
   113
  fun print thy (ref {theorems, index}) = Pretty.writeln (pretty_theorems thy theorems);
wenzelm@16441
   114
end);
wenzelm@3987
   115
wenzelm@16493
   116
val get_theorems_ref = TheoremsData.get;
wenzelm@16493
   117
val get_theorems = ! o get_theorems_ref;
wenzelm@17162
   118
val theorems_of = #theorems o get_theorems;
wenzelm@17162
   119
val theorem_space = #1 o theorems_of;
wenzelm@16493
   120
val fact_index_of = #index o get_theorems;
wenzelm@16023
   121
wenzelm@6367
   122
wenzelm@5000
   123
(* print theory *)
wenzelm@3987
   124
wenzelm@5005
   125
val print_theorems = TheoremsData.print;
wenzelm@8720
   126
wenzelm@16493
   127
fun print_theorems_diff prev_thy thy =
wenzelm@16493
   128
  Pretty.writeln (pretty_theorems_diff thy
wenzelm@17162
   129
    (#2 (theorems_of prev_thy)) (#theorems (get_theorems thy)));
wenzelm@16493
   130
wenzelm@5000
   131
fun print_theory thy =
wenzelm@9215
   132
  Display.pretty_full_theory thy @
wenzelm@16493
   133
    [pretty_theorems thy (#theorems (get_theorems thy))]
wenzelm@8720
   134
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@4022
   135
wenzelm@3987
   136
wenzelm@3987
   137
wenzelm@4022
   138
(** retrieve theorems **)
wenzelm@3987
   139
skalberg@15531
   140
fun the_thms _ (SOME thms) = thms
skalberg@15531
   141
  | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
wenzelm@4037
   142
wenzelm@9564
   143
fun single_thm _ [thm] = thm
wenzelm@9564
   144
  | single_thm name _ = error ("Single theorem expected " ^ quote name);
wenzelm@9564
   145
wenzelm@15703
   146
wenzelm@16023
   147
(* datatype interval *)
wenzelm@15703
   148
wenzelm@15703
   149
datatype interval =
wenzelm@15703
   150
  FromTo of int * int |
wenzelm@15703
   151
  From of int |
wenzelm@15703
   152
  Single of int;
wenzelm@15703
   153
wenzelm@15703
   154
fun interval _ (FromTo (i, j)) = i upto j
wenzelm@15703
   155
  | interval n (From i) = i upto n
wenzelm@15703
   156
  | interval _ (Single i) = [i];
wenzelm@15703
   157
wenzelm@16023
   158
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@16023
   159
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@16023
   160
  | string_of_interval (Single i) = string_of_int i;
wenzelm@16023
   161
wenzelm@16023
   162
wenzelm@16493
   163
(* datatype thmref *)
wenzelm@16493
   164
wenzelm@16493
   165
datatype thmref =
wenzelm@16493
   166
  Name of string |
wenzelm@16493
   167
  NameSelection of string * interval list;
wenzelm@15703
   168
wenzelm@16493
   169
fun name_of_thmref (Name name) = name
wenzelm@16493
   170
  | name_of_thmref (NameSelection (name, _)) = name;
wenzelm@16023
   171
wenzelm@16493
   172
fun map_name_of_thmref f (Name name) = Name (f name)
wenzelm@16493
   173
  | map_name_of_thmref f (NameSelection (name, is)) = NameSelection (f name, is);
wenzelm@16493
   174
wenzelm@16493
   175
fun string_of_thmref (Name name) = name
wenzelm@16493
   176
  | string_of_thmref (NameSelection (name, is)) =
wenzelm@16023
   177
      name ^ enclose "(" ")" (commas (map string_of_interval is));
wenzelm@16023
   178
wenzelm@16023
   179
wenzelm@16023
   180
(* select_thm *)
wenzelm@15703
   181
wenzelm@16493
   182
fun select_thm (Name _) thms = thms
wenzelm@16493
   183
  | select_thm (NameSelection (name, is)) thms =
wenzelm@16023
   184
      let
wenzelm@16023
   185
        val n = length thms;
wenzelm@16023
   186
        fun select i =
wenzelm@16023
   187
          if i < 1 orelse i > n then
wenzelm@16023
   188
            error ("Bad subscript " ^ string_of_int i ^ " for " ^
wenzelm@16023
   189
              quote name ^ " (length " ^ string_of_int n ^ ")")
wenzelm@16023
   190
          else List.nth (thms, i - 1);
wenzelm@16023
   191
      in map select (List.concat (map (interval n) is)) end;
wenzelm@15703
   192
wenzelm@16023
   193
wenzelm@16023
   194
(* selections *)
wenzelm@16023
   195
wenzelm@16493
   196
fun selections (name, [thm]) = [(Name name, thm)]
wenzelm@16023
   197
  | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) =>
wenzelm@16493
   198
      (NameSelection (name, [Single i]), thm));
berghofe@15456
   199
wenzelm@9564
   200
wenzelm@9808
   201
(* get_thm(s)_closure -- statically scoped versions *)
wenzelm@9564
   202
wenzelm@9564
   203
(*beware of proper order of evaluation!*)
wenzelm@4922
   204
wenzelm@9564
   205
fun lookup_thms thy =
wenzelm@9564
   206
  let
wenzelm@16441
   207
    val thy_ref = Theory.self_ref thy;
wenzelm@16493
   208
    val (space, thms) = #theorems (get_theorems thy);
wenzelm@9564
   209
  in
wenzelm@9564
   210
    fn name =>
wenzelm@17221
   211
      Option.map (map (Thm.transfer (Theory.deref thy_ref)))     (*dynamic identity*)
wenzelm@17221
   212
      (Symtab.curried_lookup thms (NameSpace.intern space name)) (*static content*)
wenzelm@9564
   213
  end;
wenzelm@3987
   214
wenzelm@9564
   215
fun get_thms_closure thy =
wenzelm@16441
   216
  let val closures = map lookup_thms (thy :: Theory.ancestors_of thy) in
wenzelm@16493
   217
    fn thmref =>
wenzelm@16493
   218
      let val name = name_of_thmref thmref
wenzelm@16493
   219
      in select_thm thmref (the_thms name (get_first (fn f => f name) closures)) end
berghofe@15456
   220
  end;
wenzelm@9564
   221
wenzelm@9808
   222
fun get_thm_closure thy =
wenzelm@9808
   223
  let val get = get_thms_closure thy
wenzelm@16493
   224
  in fn thmref => single_thm (name_of_thmref thmref) (get thmref) end;
wenzelm@9808
   225
wenzelm@9564
   226
wenzelm@16441
   227
(* get_thms etc. *)
wenzelm@9564
   228
wenzelm@16493
   229
fun get_thms theory thmref =
wenzelm@16493
   230
  let val name = name_of_thmref thmref in
wenzelm@16493
   231
    get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory)
wenzelm@16493
   232
    |> the_thms name |> select_thm thmref |> map (Thm.transfer theory)
wenzelm@16493
   233
  end;
wenzelm@4022
   234
wenzelm@16493
   235
fun get_thmss thy thmrefs = List.concat (map (get_thms thy) thmrefs);
wenzelm@16493
   236
fun get_thm thy thmref = single_thm (name_of_thmref thmref) (get_thms thy thmref);
wenzelm@4783
   237
wenzelm@4022
   238
wenzelm@16023
   239
(* thms_containing etc. *)
wenzelm@4022
   240
wenzelm@16023
   241
fun valid_thms thy (thmref, ths) =
wenzelm@16023
   242
  (case try (transform_error (get_thms thy)) thmref of
wenzelm@16023
   243
    NONE => false
wenzelm@16132
   244
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@3987
   245
wenzelm@16023
   246
fun thms_containing theory spec =
wenzelm@16023
   247
  (theory :: Theory.ancestors_of theory)
wenzelm@16023
   248
  |> map (fn thy =>
wenzelm@16023
   249
      FactIndex.find (fact_index_of thy) spec
wenzelm@16493
   250
      |> List.filter (fn (name, ths) => valid_thms theory (Name name, ths))
wenzelm@16023
   251
      |> gen_distinct eq_fst)
wenzelm@16023
   252
  |> List.concat;
wenzelm@4022
   253
nipkow@13646
   254
fun thms_containing_consts thy consts =
skalberg@15570
   255
  thms_containing thy (consts, []) |> map #2 |> List.concat
wenzelm@16023
   256
  |> map (fn th => (Thm.name_of_thm th, th));
nipkow@13646
   257
kleing@15882
   258
wenzelm@16336
   259
(* thms_of etc. *)
kleing@15882
   260
wenzelm@16023
   261
fun thms_of thy =
wenzelm@17162
   262
  let val thms = #2 (theorems_of thy)
wenzelm@17162
   263
  in map (fn th => (Thm.name_of_thm th, th)) (List.concat (map snd (Symtab.dest thms))) end;
wenzelm@15703
   264
wenzelm@16336
   265
fun all_thms_of thy = List.concat (map thms_of (thy :: Theory.ancestors_of thy));
wenzelm@16336
   266
wenzelm@4022
   267
wenzelm@4022
   268
wenzelm@4022
   269
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   270
wenzelm@16441
   271
(* hiding -- affects current theory node only *)
wenzelm@12695
   272
wenzelm@13424
   273
fun hide_thms fully names thy =
wenzelm@12695
   274
  let
wenzelm@16493
   275
    val r as ref {theorems = (space, thms), index} = get_theorems_ref thy;
wenzelm@16132
   276
    val space' = fold (NameSpace.hide fully) names space;
wenzelm@16336
   277
  in r := {theorems = (space', thms), index = index}; thy end;
wenzelm@12695
   278
wenzelm@12695
   279
wenzelm@4853
   280
(* naming *)
wenzelm@4853
   281
berghofe@11998
   282
fun gen_names j len name =
wenzelm@16441
   283
  map (fn i => name ^ "_" ^ string_of_int i) (j + 1 upto j + len);
wenzelm@4853
   284
berghofe@11998
   285
fun name_multi name xs = gen_names 0 (length xs) name ~~ xs;
berghofe@12235
   286
wenzelm@16441
   287
fun name_thm pre (name, thm) =
wenzelm@16441
   288
  if Thm.name_of_thm thm <> "" andalso pre then thm else Thm.name_thm (name, thm);
berghofe@12872
   289
berghofe@12872
   290
fun name_thms pre name [x] = [name_thm pre (name, x)]
berghofe@12872
   291
  | name_thms pre name xs = map (name_thm pre) (name_multi name xs);
berghofe@12235
   292
wenzelm@16441
   293
fun name_thmss name xs =
wenzelm@16441
   294
  (case filter_out (null o fst) xs of
berghofe@12872
   295
    [([x], z)] => [([name_thm true (name, x)], z)]
wenzelm@16441
   296
  | _ => snd (foldl_map (fn (i, (ys, z)) =>
wenzelm@16441
   297
    (i + length ys, (map (name_thm true) (gen_names i (length ys) name ~~ ys), z))) (0, xs)));
wenzelm@4853
   298
wenzelm@4853
   299
berghofe@11998
   300
(* enter_thms *)
wenzelm@4853
   301
wenzelm@7470
   302
fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@7470
   303
fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   304
wenzelm@16441
   305
fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms)
wenzelm@16441
   306
  | enter_thms pre_name post_name app_att (bname, thms) thy =
wenzelm@7470
   307
      let
wenzelm@16441
   308
        val name = Sign.full_name thy bname;
wenzelm@16441
   309
        val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
wenzelm@16513
   310
        val r as ref {theorems = (space, theorems), index} = get_theorems_ref thy';
wenzelm@16513
   311
        val space' = Sign.declare_name thy' name space;
wenzelm@17221
   312
        val theorems' = Symtab.curried_update (name, thms') theorems;
wenzelm@16441
   313
        val index' = FactIndex.add (K false) (name, thms') index;
wenzelm@13274
   314
      in
wenzelm@17221
   315
        (case Symtab.curried_lookup theorems name of
skalberg@15531
   316
          NONE => ()
wenzelm@16441
   317
        | SOME thms'' =>
wenzelm@16441
   318
            if Thm.eq_thms (thms', thms'') then warn_same name
wenzelm@13274
   319
            else warn_overwrite name);
wenzelm@16336
   320
        r := {theorems = (space', theorems'), index = index'};
wenzelm@16441
   321
        (thy', thms')
berghofe@11998
   322
      end;
wenzelm@3987
   323
wenzelm@16023
   324
wenzelm@6091
   325
(* add_thms(s) *)
wenzelm@4853
   326
wenzelm@16441
   327
fun add_thms_atts pre_name ((bname, thms), atts) =
wenzelm@16441
   328
  enter_thms pre_name (name_thms false)
wenzelm@16441
   329
    (Thm.applys_attributes o rpair atts) (bname, thms);
wenzelm@4853
   330
berghofe@12235
   331
fun gen_add_thmss pre_name args theory =
berghofe@12235
   332
  foldl_map (fn (thy, arg) => add_thms_atts pre_name arg thy) (theory, args);
wenzelm@5907
   333
berghofe@12235
   334
fun gen_add_thms pre_name args =
berghofe@12235
   335
  apsnd (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   336
berghofe@12872
   337
val add_thmss = gen_add_thmss (name_thms true);
berghofe@12872
   338
val add_thms = gen_add_thms (name_thms true);
wenzelm@5907
   339
wenzelm@5907
   340
wenzelm@14564
   341
(* note_thmss(_i) *)
wenzelm@5907
   342
wenzelm@9192
   343
local
wenzelm@12711
   344
wenzelm@16132
   345
fun gen_note_thss get kind_att (thy, ((bname, more_atts), ths_atts)) =
wenzelm@12711
   346
  let
wenzelm@12711
   347
    fun app (x, (ths, atts)) = Thm.applys_attributes ((x, ths), atts);
wenzelm@16441
   348
    val (thy', thms) = thy |> enter_thms
wenzelm@16441
   349
      name_thmss (name_thms false) (apsnd List.concat o foldl_map app)
wenzelm@12711
   350
      (bname, map (fn (ths, atts) => (get thy ths, atts @ more_atts @ [kind_att])) ths_atts);
wenzelm@12711
   351
  in (thy', (bname, thms)) end;
wenzelm@12711
   352
wenzelm@16132
   353
fun gen_note_thmss get kind_att args thy =
wenzelm@16132
   354
  foldl_map (gen_note_thss get kind_att) (thy, args);
wenzelm@12711
   355
wenzelm@9192
   356
in
wenzelm@12711
   357
wenzelm@16132
   358
val note_thmss = gen_note_thmss get_thms;
wenzelm@16132
   359
val note_thmss_i = gen_note_thmss (K I);
wenzelm@12711
   360
wenzelm@9192
   361
end;
wenzelm@5280
   362
wenzelm@5280
   363
wenzelm@6091
   364
(* store_thm *)
wenzelm@5280
   365
berghofe@11998
   366
fun store_thm ((bname, thm), atts) thy =
berghofe@12872
   367
  let val (thy', [th']) = add_thms_atts (name_thms true) ((bname, [thm]), atts) thy
wenzelm@5280
   368
  in (thy', th') end;
wenzelm@3987
   369
wenzelm@3987
   370
wenzelm@16441
   371
(* smart_store_thms(_open) *)
wenzelm@3987
   372
wenzelm@16441
   373
local
wenzelm@16441
   374
wenzelm@16441
   375
fun smart_store _ (name, []) =
berghofe@11516
   376
      error ("Cannot store empty list of theorems: " ^ quote name)
wenzelm@16441
   377
  | smart_store name_thm (name, [thm]) =
wenzelm@16441
   378
      #2 (enter_thms (name_thm true) (name_thm false) I (name, [thm]) (Thm.theory_of_thm thm))
wenzelm@16441
   379
  | smart_store name_thm (name, thms) =
wenzelm@7405
   380
      let
wenzelm@16441
   381
        fun merge (thy, th) = Theory.merge (thy, Thm.theory_of_thm th);
wenzelm@16441
   382
        val thy = Library.foldl merge (Thm.theory_of_thm (hd thms), tl thms);
wenzelm@16441
   383
      in #2 (enter_thms (name_thm true) (name_thm false) I (name, thms) thy) end;
berghofe@11516
   384
wenzelm@16441
   385
in
wenzelm@16441
   386
wenzelm@16441
   387
val smart_store_thms = smart_store name_thms;
wenzelm@16441
   388
val smart_store_thms_open = smart_store (K (K I));
wenzelm@16441
   389
wenzelm@16441
   390
end;
wenzelm@3987
   391
wenzelm@3987
   392
wenzelm@16722
   393
(* forall_elim_var(s) -- belongs to drule.ML *)
wenzelm@7899
   394
wenzelm@16722
   395
fun forall_elim_vars_aux strip_vars i th =
wenzelm@16722
   396
  let
wenzelm@16722
   397
    val {thy, tpairs, prop, ...} = Thm.rep_thm th;
haftmann@16787
   398
    val add_used = Term.fold_aterms
haftmann@16787
   399
      (fn Var ((x, j), _) => if i = j then curry (op ins_string) x else I | _ => I);
wenzelm@16722
   400
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@16722
   401
    val vars = strip_vars prop;
wenzelm@16722
   402
    val cvars = (Term.variantlist (map #1 vars, used), vars)
wenzelm@16722
   403
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@16722
   404
  in fold Thm.forall_elim cvars th end;
wenzelm@7899
   405
wenzelm@16722
   406
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@16722
   407
wenzelm@16722
   408
fun forall_elim_var i th = forall_elim_vars_aux
wenzelm@16722
   409
  (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@16722
   410
  | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@7899
   411
wenzelm@7899
   412
wenzelm@4022
   413
(* store axioms as theorems *)
wenzelm@4022
   414
wenzelm@4853
   415
local
wenzelm@7899
   416
  fun get_axs thy named_axs =
wenzelm@7899
   417
    map (forall_elim_vars 0 o Thm.get_axiom thy o fst) named_axs;
wenzelm@7753
   418
wenzelm@8419
   419
  fun add_single add (thy, ((name, ax), atts)) =
wenzelm@4853
   420
    let
berghofe@11998
   421
      val named_ax = [(name, ax)];
wenzelm@7753
   422
      val thy' = add named_ax thy;
wenzelm@7753
   423
      val thm = hd (get_axs thy' named_ax);
berghofe@12235
   424
    in apsnd hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
wenzelm@7753
   425
wenzelm@8419
   426
  fun add_multi add (thy, ((name, axs), atts)) =
wenzelm@7753
   427
    let
wenzelm@7753
   428
      val named_axs = name_multi name axs;
wenzelm@4853
   429
      val thy' = add named_axs thy;
wenzelm@7753
   430
      val thms = get_axs thy' named_axs;
berghofe@12235
   431
    in apsnd hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
wenzelm@4022
   432
wenzelm@8419
   433
  fun add_singles add args thy = foldl_map (add_single add) (thy, args);
wenzelm@8419
   434
  fun add_multis add args thy = foldl_map (add_multi add) (thy, args);
wenzelm@4853
   435
in
wenzelm@7753
   436
  val add_axioms    = add_singles Theory.add_axioms;
wenzelm@7753
   437
  val add_axioms_i  = add_singles Theory.add_axioms_i;
wenzelm@7753
   438
  val add_axiomss   = add_multis Theory.add_axioms;
wenzelm@7753
   439
  val add_axiomss_i = add_multis Theory.add_axioms_i;
wenzelm@9318
   440
  val add_defs      = add_singles o Theory.add_defs;
wenzelm@9318
   441
  val add_defs_i    = add_singles o Theory.add_defs_i;
wenzelm@9318
   442
  val add_defss     = add_multis o Theory.add_defs;
wenzelm@9318
   443
  val add_defss_i   = add_multis o Theory.add_defs_i;
wenzelm@4853
   444
end;
wenzelm@4022
   445
wenzelm@4022
   446
wenzelm@3987
   447
wenzelm@5091
   448
(*** the ProtoPure theory ***)
wenzelm@3987
   449
wenzelm@16441
   450
val aT = TFree ("'a", []);
wenzelm@16441
   451
val A = Free ("A", propT);
wenzelm@16441
   452
wenzelm@3987
   453
val proto_pure =
wenzelm@16493
   454
  Context.pre_pure_thy
wenzelm@16987
   455
  |> Compress.init_data
wenzelm@16536
   456
  |> Sign.init_data
wenzelm@16536
   457
  |> Theory.init_data
wenzelm@16536
   458
  |> Proofterm.init_data
wenzelm@16023
   459
  |> TheoremsData.init
wenzelm@3987
   460
  |> Theory.add_types
wenzelm@4922
   461
   [("fun", 2, NoSyn),
wenzelm@4922
   462
    ("prop", 0, NoSyn),
wenzelm@4922
   463
    ("itself", 1, NoSyn),
wenzelm@4922
   464
    ("dummy", 0, NoSyn)]
wenzelm@4922
   465
  |> Theory.add_nonterminals Syntax.pure_nonterms
wenzelm@3987
   466
  |> Theory.add_syntax Syntax.pure_syntax
wenzelm@15801
   467
  |> Theory.add_syntax Syntax.pure_appl_syntax
wenzelm@6692
   468
  |> Theory.add_modesyntax (Symbol.xsymbolsN, true) Syntax.pure_xsym_syntax
wenzelm@3987
   469
  |> Theory.add_syntax
wenzelm@16441
   470
   [("==>", "prop => prop => prop", Delimfix "op ==>"),
wenzelm@9534
   471
    (Term.dummy_patternN, "aprop", Delimfix "'_")]
wenzelm@3987
   472
  |> Theory.add_consts
wenzelm@16441
   473
   [("==", "'a => 'a => prop", InfixrName ("==", 2)),
wenzelm@16441
   474
    ("==>", "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@3987
   475
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@10667
   476
    ("Goal", "prop => prop", NoSyn),
wenzelm@6547
   477
    ("TYPE", "'a itself", NoSyn),
wenzelm@9534
   478
    (Term.dummy_patternN, "'a", Delimfix "'_")]
skalberg@14223
   479
  |> Theory.add_finals_i false
wenzelm@16441
   480
    [Const ("==", [aT, aT] ---> propT),
wenzelm@16441
   481
     Const ("==>", [propT, propT] ---> propT),
wenzelm@16441
   482
     Const ("all", (aT --> propT) --> propT),
wenzelm@16441
   483
     Const ("TYPE", a_itselfT)]
nipkow@5041
   484
  |> Theory.add_modesyntax ("", false)
wenzelm@12138
   485
    (Syntax.pure_syntax_output @ Syntax.pure_appl_syntax)
wenzelm@12250
   486
  |> Theory.add_trfuns Syntax.pure_trfuns
wenzelm@12250
   487
  |> Theory.add_trfunsT Syntax.pure_trfunsT
wenzelm@16441
   488
  |> Sign.local_path
wenzelm@10667
   489
  |> (#1 oo (add_defs_i false o map Thm.no_attributes))
wenzelm@16441
   490
   [("Goal_def", Logic.mk_equals (Logic.mk_goal A, A))]
wenzelm@9238
   491
  |> (#1 o add_thmss [(("nothing", []), [])])
berghofe@11516
   492
  |> Theory.add_axioms_i Proofterm.equality_axms
wenzelm@16493
   493
  |> Theory.end_theory;
wenzelm@3987
   494
wenzelm@5091
   495
structure ProtoPure =
wenzelm@5091
   496
struct
wenzelm@5091
   497
  val thy = proto_pure;
wenzelm@5091
   498
  val Goal_def = get_axiom thy "Goal_def";
wenzelm@5091
   499
end;
wenzelm@3987
   500
wenzelm@3987
   501
end;
wenzelm@3987
   502
wenzelm@4022
   503
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   504
open BasicPureThy;