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