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