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