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