src/Pure/pure_thy.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21)
changeset 18728 6790126ab5f6
parent 18713 cf777b9788b5
child 18778 f623f7a35ced
permissions -rw-r--r--
simplified type attribute;
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@18728
    50
  val store_thm: (bstring * thm) * attribute list -> theory -> thm * theory
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@18728
    55
  val add_thms: ((bstring * thm) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    56
  val add_thmss: ((bstring * thm list) * attribute list) list -> theory -> thm list list * theory 
wenzelm@18728
    57
  val note_thmss: attribute -> ((bstring * attribute list) *
wenzelm@18728
    58
    (thmref * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18728
    59
  val note_thmss_i: attribute -> ((bstring * attribute list) *
wenzelm@18728
    60
    (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18728
    61
  val add_axioms: ((bstring * string) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    62
  val add_axioms_i: ((bstring * term) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    63
  val add_axiomss: ((bstring * string list) * attribute list) list ->
wenzelm@18728
    64
    theory -> thm list list * theory
wenzelm@18728
    65
  val add_axiomss_i: ((bstring * term list) * attribute list) list ->
wenzelm@18728
    66
    theory -> thm list list * theory
wenzelm@18728
    67
  val add_defs: bool -> ((bstring * string) * attribute list) list ->
haftmann@18377
    68
    theory -> thm list * theory
wenzelm@18728
    69
  val add_defs_i: bool -> ((bstring * term) * attribute list) list ->
haftmann@18377
    70
    theory -> thm list * theory
wenzelm@18728
    71
  val add_defss: bool -> ((bstring * string list) * attribute list) list ->
haftmann@18377
    72
    theory -> thm list list * theory
wenzelm@18728
    73
  val add_defss_i: bool -> ((bstring * term list) * attribute list) list ->
haftmann@18358
    74
    theory -> thm list list * theory
wenzelm@18666
    75
  val generic_setup: string option -> 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@18031
   169
  NameSelection of string * interval list |
wenzelm@18031
   170
  Fact of string;
wenzelm@15703
   171
wenzelm@16493
   172
fun name_of_thmref (Name name) = name
wenzelm@18031
   173
  | name_of_thmref (NameSelection (name, _)) = name
wenzelm@18678
   174
  | name_of_thmref (Fact _) = error "Illegal literal fact";
wenzelm@16023
   175
wenzelm@16493
   176
fun map_name_of_thmref f (Name name) = Name (f name)
wenzelm@18031
   177
  | map_name_of_thmref f (NameSelection (name, is)) = NameSelection (f name, is)
wenzelm@18031
   178
  | map_name_of_thmref _ thmref = thmref;
wenzelm@16493
   179
wenzelm@16493
   180
fun string_of_thmref (Name name) = name
wenzelm@16493
   181
  | string_of_thmref (NameSelection (name, is)) =
wenzelm@18031
   182
      name ^ enclose "(" ")" (commas (map string_of_interval is))
wenzelm@18678
   183
  | string_of_thmref (Fact _) = error "Illegal literal fact";
wenzelm@16023
   184
wenzelm@16023
   185
wenzelm@16023
   186
(* select_thm *)
wenzelm@15703
   187
wenzelm@16493
   188
fun select_thm (Name _) thms = thms
wenzelm@18031
   189
  | select_thm (Fact _) thms = thms
wenzelm@16493
   190
  | select_thm (NameSelection (name, is)) thms =
wenzelm@16023
   191
      let
wenzelm@16023
   192
        val n = length thms;
wenzelm@16023
   193
        fun select i =
wenzelm@16023
   194
          if i < 1 orelse i > n then
wenzelm@16023
   195
            error ("Bad subscript " ^ string_of_int i ^ " for " ^
wenzelm@16023
   196
              quote name ^ " (length " ^ string_of_int n ^ ")")
wenzelm@16023
   197
          else List.nth (thms, i - 1);
wenzelm@16023
   198
      in map select (List.concat (map (interval n) is)) end;
wenzelm@15703
   199
wenzelm@16023
   200
wenzelm@16023
   201
(* selections *)
wenzelm@16023
   202
wenzelm@16493
   203
fun selections (name, [thm]) = [(Name name, thm)]
wenzelm@16023
   204
  | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) =>
wenzelm@16493
   205
      (NameSelection (name, [Single i]), thm));
berghofe@15456
   206
wenzelm@9564
   207
wenzelm@9808
   208
(* get_thm(s)_closure -- statically scoped versions *)
wenzelm@9564
   209
wenzelm@9564
   210
(*beware of proper order of evaluation!*)
wenzelm@4922
   211
wenzelm@9564
   212
fun lookup_thms thy =
wenzelm@9564
   213
  let
wenzelm@16441
   214
    val thy_ref = Theory.self_ref thy;
wenzelm@16493
   215
    val (space, thms) = #theorems (get_theorems thy);
wenzelm@9564
   216
  in
wenzelm@9564
   217
    fn name =>
wenzelm@17221
   218
      Option.map (map (Thm.transfer (Theory.deref thy_ref)))     (*dynamic identity*)
wenzelm@17418
   219
      (Symtab.lookup thms (NameSpace.intern space name)) (*static content*)
wenzelm@9564
   220
  end;
wenzelm@3987
   221
wenzelm@9564
   222
fun get_thms_closure thy =
wenzelm@16441
   223
  let val closures = map lookup_thms (thy :: Theory.ancestors_of thy) in
wenzelm@16493
   224
    fn thmref =>
wenzelm@18031
   225
      let val name = name_of_thmref thmref;
wenzelm@16493
   226
      in select_thm thmref (the_thms name (get_first (fn f => f name) closures)) end
berghofe@15456
   227
  end;
wenzelm@9564
   228
wenzelm@9808
   229
fun get_thm_closure thy =
wenzelm@9808
   230
  let val get = get_thms_closure thy
wenzelm@16493
   231
  in fn thmref => single_thm (name_of_thmref thmref) (get thmref) end;
wenzelm@9808
   232
wenzelm@9564
   233
wenzelm@16441
   234
(* get_thms etc. *)
wenzelm@9564
   235
wenzelm@16493
   236
fun get_thms theory thmref =
wenzelm@16493
   237
  let val name = name_of_thmref thmref in
wenzelm@16493
   238
    get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory)
wenzelm@16493
   239
    |> the_thms name |> select_thm thmref |> map (Thm.transfer theory)
wenzelm@16493
   240
  end;
wenzelm@4022
   241
wenzelm@16493
   242
fun get_thmss thy thmrefs = List.concat (map (get_thms thy) thmrefs);
wenzelm@16493
   243
fun get_thm thy thmref = single_thm (name_of_thmref thmref) (get_thms thy thmref);
wenzelm@4783
   244
wenzelm@17930
   245
fun thm name = get_thm (the_context ()) (Name name);
wenzelm@17930
   246
fun thms name = get_thms (the_context ()) (Name name);
wenzelm@17930
   247
wenzelm@4022
   248
wenzelm@16023
   249
(* thms_containing etc. *)
wenzelm@4022
   250
wenzelm@16023
   251
fun valid_thms thy (thmref, ths) =
wenzelm@18678
   252
  (case try (get_thms thy) thmref of
wenzelm@16023
   253
    NONE => false
wenzelm@16132
   254
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@3987
   255
wenzelm@16023
   256
fun thms_containing theory spec =
wenzelm@16023
   257
  (theory :: Theory.ancestors_of theory)
wenzelm@16023
   258
  |> map (fn thy =>
wenzelm@16023
   259
      FactIndex.find (fact_index_of thy) spec
wenzelm@16493
   260
      |> List.filter (fn (name, ths) => valid_thms theory (Name name, ths))
haftmann@17496
   261
      |> gen_distinct (eq_fst (op =)))
wenzelm@16023
   262
  |> List.concat;
wenzelm@4022
   263
nipkow@13646
   264
fun thms_containing_consts thy consts =
skalberg@15570
   265
  thms_containing thy (consts, []) |> map #2 |> List.concat
wenzelm@16023
   266
  |> map (fn th => (Thm.name_of_thm th, th));
nipkow@13646
   267
kleing@15882
   268
wenzelm@16336
   269
(* thms_of etc. *)
kleing@15882
   270
wenzelm@16023
   271
fun thms_of thy =
wenzelm@17162
   272
  let val thms = #2 (theorems_of thy)
wenzelm@17162
   273
  in map (fn th => (Thm.name_of_thm th, th)) (List.concat (map snd (Symtab.dest thms))) end;
wenzelm@15703
   274
wenzelm@16336
   275
fun all_thms_of thy = List.concat (map thms_of (thy :: Theory.ancestors_of thy));
wenzelm@16336
   276
wenzelm@4022
   277
wenzelm@4022
   278
wenzelm@4022
   279
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   280
wenzelm@16441
   281
(* hiding -- affects current theory node only *)
wenzelm@12695
   282
wenzelm@13424
   283
fun hide_thms fully names thy =
wenzelm@12695
   284
  let
wenzelm@16493
   285
    val r as ref {theorems = (space, thms), index} = get_theorems_ref thy;
wenzelm@16132
   286
    val space' = fold (NameSpace.hide fully) names space;
wenzelm@16336
   287
  in r := {theorems = (space', thms), index = index}; thy end;
wenzelm@12695
   288
wenzelm@12695
   289
wenzelm@4853
   290
(* naming *)
wenzelm@4853
   291
wenzelm@18614
   292
fun gen_names _ len "" = replicate len ""
wenzelm@18614
   293
  | gen_names j len name = map (fn i => name ^ "_" ^ string_of_int i) (j + 1 upto j + len);
wenzelm@4853
   294
berghofe@11998
   295
fun name_multi name xs = gen_names 0 (length xs) name ~~ xs;
berghofe@12235
   296
wenzelm@16441
   297
fun name_thm pre (name, thm) =
wenzelm@16441
   298
  if Thm.name_of_thm thm <> "" andalso pre then thm else Thm.name_thm (name, thm);
berghofe@12872
   299
berghofe@12872
   300
fun name_thms pre name [x] = [name_thm pre (name, x)]
berghofe@12872
   301
  | name_thms pre name xs = map (name_thm pre) (name_multi name xs);
berghofe@12235
   302
wenzelm@16441
   303
fun name_thmss name xs =
wenzelm@16441
   304
  (case filter_out (null o fst) xs of
berghofe@12872
   305
    [([x], z)] => [([name_thm true (name, x)], z)]
haftmann@18418
   306
  | _ => fst (fold_map (fn (ys, z) => fn i =>
haftmann@18418
   307
    ((map (name_thm true) (gen_names i (length ys) name ~~ ys), z), i + length ys)) xs 0));
wenzelm@4853
   308
wenzelm@4853
   309
berghofe@11998
   310
(* enter_thms *)
wenzelm@4853
   311
wenzelm@7470
   312
fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@7470
   313
fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   314
haftmann@18418
   315
fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
wenzelm@16441
   316
  | enter_thms pre_name post_name app_att (bname, thms) thy =
wenzelm@7470
   317
      let
wenzelm@16441
   318
        val name = Sign.full_name thy bname;
wenzelm@16441
   319
        val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
wenzelm@16513
   320
        val r as ref {theorems = (space, theorems), index} = get_theorems_ref thy';
wenzelm@16513
   321
        val space' = Sign.declare_name thy' name space;
wenzelm@17418
   322
        val theorems' = Symtab.update (name, thms') theorems;
wenzelm@18031
   323
        val index' = FactIndex.add_global (name, thms') index;
wenzelm@13274
   324
      in
wenzelm@17418
   325
        (case Symtab.lookup theorems name of
skalberg@15531
   326
          NONE => ()
wenzelm@16441
   327
        | SOME thms'' =>
wenzelm@16441
   328
            if Thm.eq_thms (thms', thms'') then warn_same name
wenzelm@13274
   329
            else warn_overwrite name);
wenzelm@16336
   330
        r := {theorems = (space', theorems'), index = index'};
haftmann@18418
   331
        (thms', thy')
berghofe@11998
   332
      end;
wenzelm@3987
   333
wenzelm@16023
   334
wenzelm@6091
   335
(* add_thms(s) *)
wenzelm@4853
   336
wenzelm@16441
   337
fun add_thms_atts pre_name ((bname, thms), atts) =
wenzelm@18728
   338
  enter_thms pre_name (name_thms false) (foldl_map (Thm.theory_attributes atts)) (bname, thms);
wenzelm@4853
   339
haftmann@18377
   340
fun gen_add_thmss pre_name =
haftmann@18377
   341
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   342
berghofe@12235
   343
fun gen_add_thms pre_name args =
haftmann@18377
   344
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   345
berghofe@12872
   346
val add_thmss = gen_add_thmss (name_thms true);
berghofe@12872
   347
val add_thms = gen_add_thms (name_thms true);
wenzelm@5907
   348
wenzelm@5907
   349
wenzelm@14564
   350
(* note_thmss(_i) *)
wenzelm@5907
   351
wenzelm@9192
   352
local
wenzelm@12711
   353
haftmann@18418
   354
fun gen_note_thss get kind_att ((bname, more_atts), ths_atts) thy =
wenzelm@12711
   355
  let
wenzelm@18728
   356
    fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
haftmann@18418
   357
    val (thms, thy') = thy |> enter_thms
wenzelm@16441
   358
      name_thmss (name_thms false) (apsnd List.concat o foldl_map app)
wenzelm@12711
   359
      (bname, map (fn (ths, atts) => (get thy ths, atts @ more_atts @ [kind_att])) ths_atts);
haftmann@18418
   360
  in ((bname, thms), thy') end;
wenzelm@12711
   361
haftmann@18418
   362
fun gen_note_thmss get kind_att =
haftmann@18418
   363
  fold_map (gen_note_thss get kind_att);
wenzelm@12711
   364
wenzelm@9192
   365
in
wenzelm@12711
   366
haftmann@18418
   367
val note_thmss = gen_note_thmss get_thms;
haftmann@18418
   368
val note_thmss_i = gen_note_thmss (K I);
wenzelm@12711
   369
wenzelm@9192
   370
end;
wenzelm@5280
   371
wenzelm@5280
   372
wenzelm@6091
   373
(* store_thm *)
wenzelm@5280
   374
berghofe@11998
   375
fun store_thm ((bname, thm), atts) thy =
haftmann@18377
   376
  let val ([th'], thy') = add_thms_atts (name_thms true) ((bname, [thm]), atts) thy
haftmann@18358
   377
  in (th', thy') end;
wenzelm@3987
   378
wenzelm@3987
   379
wenzelm@16441
   380
(* smart_store_thms(_open) *)
wenzelm@3987
   381
wenzelm@16441
   382
local
wenzelm@16441
   383
wenzelm@16441
   384
fun smart_store _ (name, []) =
berghofe@11516
   385
      error ("Cannot store empty list of theorems: " ^ quote name)
wenzelm@16441
   386
  | smart_store name_thm (name, [thm]) =
haftmann@18418
   387
      fst (enter_thms (name_thm true) (name_thm false) I (name, [thm]) (Thm.theory_of_thm thm))
wenzelm@16441
   388
  | smart_store name_thm (name, thms) =
wenzelm@7405
   389
      let
wenzelm@16441
   390
        fun merge (thy, th) = Theory.merge (thy, Thm.theory_of_thm th);
wenzelm@16441
   391
        val thy = Library.foldl merge (Thm.theory_of_thm (hd thms), tl thms);
haftmann@18418
   392
      in fst (enter_thms (name_thm true) (name_thm false) I (name, thms) thy) end;
berghofe@11516
   393
wenzelm@16441
   394
in
wenzelm@16441
   395
wenzelm@16441
   396
val smart_store_thms = smart_store name_thms;
wenzelm@16441
   397
val smart_store_thms_open = smart_store (K (K I));
wenzelm@16441
   398
wenzelm@16441
   399
end;
wenzelm@3987
   400
wenzelm@3987
   401
wenzelm@16722
   402
(* forall_elim_var(s) -- belongs to drule.ML *)
wenzelm@7899
   403
wenzelm@16722
   404
fun forall_elim_vars_aux strip_vars i th =
wenzelm@16722
   405
  let
wenzelm@16722
   406
    val {thy, tpairs, prop, ...} = Thm.rep_thm th;
haftmann@16787
   407
    val add_used = Term.fold_aterms
haftmann@16787
   408
      (fn Var ((x, j), _) => if i = j then curry (op ins_string) x else I | _ => I);
wenzelm@16722
   409
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@16722
   410
    val vars = strip_vars prop;
wenzelm@16722
   411
    val cvars = (Term.variantlist (map #1 vars, used), vars)
wenzelm@16722
   412
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@16722
   413
  in fold Thm.forall_elim cvars th end;
wenzelm@7899
   414
wenzelm@16722
   415
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@16722
   416
wenzelm@16722
   417
fun forall_elim_var i th = forall_elim_vars_aux
wenzelm@16722
   418
  (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@16722
   419
  | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@7899
   420
wenzelm@7899
   421
wenzelm@4022
   422
(* store axioms as theorems *)
wenzelm@4022
   423
wenzelm@4853
   424
local
wenzelm@17418
   425
  fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
wenzelm@17418
   426
  fun get_axs thy named_axs = map (forall_elim_vars 0 o get_ax thy) named_axs;
haftmann@18377
   427
  fun add_single add ((name, ax), atts) thy =
wenzelm@4853
   428
    let
berghofe@11998
   429
      val named_ax = [(name, ax)];
wenzelm@7753
   430
      val thy' = add named_ax thy;
wenzelm@7753
   431
      val thm = hd (get_axs thy' named_ax);
haftmann@18377
   432
    in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
haftmann@18377
   433
  fun add_multi add ((name, axs), atts) thy =
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;
haftmann@18377
   438
    in apfst hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
haftmann@18377
   439
  fun add_singles add = fold_map (add_single add);
haftmann@18377
   440
  fun add_multis add = fold_map (add_multi add);
wenzelm@4853
   441
in
wenzelm@7753
   442
  val add_axioms    = add_singles Theory.add_axioms;
wenzelm@7753
   443
  val add_axioms_i  = add_singles Theory.add_axioms_i;
wenzelm@7753
   444
  val add_axiomss   = add_multis Theory.add_axioms;
wenzelm@7753
   445
  val add_axiomss_i = add_multis Theory.add_axioms_i;
haftmann@18377
   446
  val add_defs      = add_singles o Theory.add_defs;
haftmann@18377
   447
  val add_defs_i    = add_singles o Theory.add_defs_i;
haftmann@18377
   448
  val add_defss     = add_multis o Theory.add_defs;
haftmann@18377
   449
  val add_defss_i   = add_multis o Theory.add_defs_i;
wenzelm@4853
   450
end;
wenzelm@4022
   451
wenzelm@4022
   452
wenzelm@3987
   453
wenzelm@17342
   454
(*** ML setup ***)
wenzelm@17342
   455
wenzelm@17342
   456
(* generic_setup *)
wenzelm@17342
   457
wenzelm@18713
   458
fun generic_setup NONE = (fn thy => thy |> Context.setup ())
wenzelm@18713
   459
  | generic_setup (SOME txt) = Context.use_let "val setup: theory -> theory" "setup" txt;
wenzelm@17342
   460
wenzelm@17342
   461
wenzelm@17342
   462
(* add_oracle *)
wenzelm@17342
   463
wenzelm@17342
   464
fun add_oracle (name, T, oracle) =
wenzelm@17342
   465
  let val txt =
wenzelm@17342
   466
    "local\n\
wenzelm@17342
   467
    \  type T = " ^ T ^ ";\n\
wenzelm@17342
   468
    \  val oracle: theory -> T -> term = " ^ oracle ^ ";\n\
wenzelm@17342
   469
    \  val name = " ^ quote name ^ ";\n\
wenzelm@17342
   470
    \  exception Arg of T;\n\
wenzelm@17342
   471
    \  val _ = Context.>> (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x));\n\
wenzelm@17342
   472
    \  val thy = Context.the_context ();\n\
wenzelm@17342
   473
    \  val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\
wenzelm@17342
   474
    \in\n\
wenzelm@17342
   475
    \  fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\
wenzelm@17342
   476
    \end;\n";
wenzelm@17342
   477
  in Context.use_mltext_theory txt false end;
wenzelm@17342
   478
wenzelm@17342
   479
wenzelm@17342
   480
wenzelm@5091
   481
(*** the ProtoPure theory ***)
wenzelm@3987
   482
wenzelm@16441
   483
val aT = TFree ("'a", []);
wenzelm@16441
   484
val A = Free ("A", propT);
wenzelm@16441
   485
wenzelm@3987
   486
val proto_pure =
wenzelm@16493
   487
  Context.pre_pure_thy
wenzelm@16987
   488
  |> Compress.init_data
wenzelm@16536
   489
  |> Sign.init_data
wenzelm@16536
   490
  |> Theory.init_data
wenzelm@16536
   491
  |> Proofterm.init_data
wenzelm@16023
   492
  |> TheoremsData.init
wenzelm@3987
   493
  |> Theory.add_types
wenzelm@4922
   494
   [("fun", 2, NoSyn),
wenzelm@4922
   495
    ("prop", 0, NoSyn),
wenzelm@4922
   496
    ("itself", 1, NoSyn),
wenzelm@4922
   497
    ("dummy", 0, NoSyn)]
wenzelm@18713
   498
  |> Theory.add_nonterminals Syntax.basic_nonterms
wenzelm@3987
   499
  |> Theory.add_syntax
wenzelm@18713
   500
   [("_lambda",     "[pttrns, 'a] => logic",     Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@18713
   501
    ("_abs",        "'a",                        NoSyn),
wenzelm@18713
   502
    ("",            "'a => args",                Delimfix "_"),
wenzelm@18713
   503
    ("_args",       "['a, args] => args",        Delimfix "_,/ _"),
wenzelm@18713
   504
    ("",            "id => idt",                 Delimfix "_"),
wenzelm@18713
   505
    ("_idtdummy",   "idt",                       Delimfix "'_"),
wenzelm@18713
   506
    ("_idtyp",      "[id, type] => idt",         Mixfix ("_::_", [], 0)),
wenzelm@18713
   507
    ("_idtypdummy", "type => idt",               Mixfix ("'_()::_", [], 0)),
wenzelm@18713
   508
    ("",            "idt => idt",                Delimfix "'(_')"),
wenzelm@18713
   509
    ("",            "idt => idts",               Delimfix "_"),
wenzelm@18713
   510
    ("_idts",       "[idt, idts] => idts",       Mixfix ("_/ _", [1, 0], 0)),
wenzelm@18713
   511
    ("",            "idt => pttrn",              Delimfix "_"),
wenzelm@18713
   512
    ("",            "pttrn => pttrns",           Delimfix "_"),
wenzelm@18713
   513
    ("_pttrns",     "[pttrn, pttrns] => pttrns", Mixfix ("_/ _", [1, 0], 0)),
wenzelm@18713
   514
    ("",            "id => aprop",               Delimfix "_"),
wenzelm@18713
   515
    ("",            "longid => aprop",           Delimfix "_"),
wenzelm@18713
   516
    ("",            "var => aprop",              Delimfix "_"),
wenzelm@18713
   517
    ("_DDDOT",      "aprop",                     Delimfix "..."),
wenzelm@18713
   518
    ("_aprop",      "aprop => prop",             Delimfix "PROP _"),
wenzelm@18713
   519
    ("_asm",        "prop => asms",              Delimfix "_"),
wenzelm@18713
   520
    ("_asms",       "[prop, asms] => asms",      Delimfix "_;/ _"),
wenzelm@18713
   521
    ("_bigimpl",    "[asms, prop] => prop",      Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@18713
   522
    ("_ofclass",    "[type, logic] => prop",     Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@18713
   523
    ("_mk_ofclass", "_",                         NoSyn),
wenzelm@18713
   524
    ("_TYPE",       "type => logic",             Delimfix "(1TYPE/(1'(_')))"),
wenzelm@18713
   525
    ("",            "id => logic",               Delimfix "_"),
wenzelm@18713
   526
    ("",            "longid => logic",           Delimfix "_"),
wenzelm@18713
   527
    ("",            "var => logic",              Delimfix "_"),
wenzelm@18713
   528
    ("_DDDOT",      "logic",                     Delimfix "..."),
wenzelm@18713
   529
    ("_constify",   "num => num_const",          Delimfix "_"),
wenzelm@18713
   530
    ("_indexnum",   "num_const => index",        Delimfix "\\<^sub>_"),
wenzelm@18713
   531
    ("_index",      "logic => index",            Delimfix "(00\\<^bsub>_\\<^esub>)"),
wenzelm@18713
   532
    ("_indexdefault", "index",                   Delimfix ""),
wenzelm@18713
   533
    ("_indexvar",   "index",                     Delimfix "'\\<index>"),
wenzelm@18713
   534
    ("_struct",     "index => logic",            Mixfix ("\\<struct>_", [1000], 1000)),
wenzelm@18713
   535
    ("==>",         "prop => prop => prop",      Delimfix "op ==>"),
wenzelm@18713
   536
    (Term.dummy_patternN, "aprop",               Delimfix "'_")]
wenzelm@18713
   537
  |> Theory.add_syntax Syntax.appl_syntax
wenzelm@18713
   538
  |> Theory.add_modesyntax (Symbol.xsymbolsN, true)
wenzelm@18713
   539
   [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@18713
   540
    ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@18713
   541
    ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
wenzelm@18713
   542
    ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@18713
   543
    ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@18713
   544
    ("_idtypdummy", "type => idt",        Mixfix ("'_()\\<Colon>_", [], 0)),
wenzelm@18713
   545
    ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@18713
   546
    ("==",       "['a, 'a] => prop",      InfixrName ("\\<equiv>", 2)),
wenzelm@18713
   547
    ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
wenzelm@18713
   548
    ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@18713
   549
    ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
wenzelm@18713
   550
    ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@18713
   551
    ("_DDDOT",   "logic",                 Delimfix "\\<dots>")]
wenzelm@18713
   552
  |> Theory.add_modesyntax ("", false)
wenzelm@18713
   553
   [("prop", "prop => prop", Mixfix ("_", [0], 0)),
wenzelm@18713
   554
    ("_meta_conjunction", "prop => prop => prop", InfixrName ("&&", 2))]
wenzelm@18713
   555
  |> Theory.add_modesyntax ("HTML", false)
wenzelm@18713
   556
   [("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
wenzelm@18713
   557
  |> Theory.add_trfuns Syntax.pure_trfuns
wenzelm@18713
   558
  |> Theory.add_trfunsT Syntax.pure_trfunsT
wenzelm@3987
   559
  |> Theory.add_consts
wenzelm@16441
   560
   [("==", "'a => 'a => prop", InfixrName ("==", 2)),
wenzelm@16441
   561
    ("==>", "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@3987
   562
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@18031
   563
    ("prop", "prop => prop", NoSyn),
wenzelm@6547
   564
    ("TYPE", "'a itself", NoSyn),
wenzelm@9534
   565
    (Term.dummy_patternN, "'a", Delimfix "'_")]
skalberg@14223
   566
  |> Theory.add_finals_i false
wenzelm@16441
   567
    [Const ("==", [aT, aT] ---> propT),
wenzelm@16441
   568
     Const ("==>", [propT, propT] ---> propT),
wenzelm@16441
   569
     Const ("all", (aT --> propT) --> propT),
wenzelm@17703
   570
     Const ("TYPE", a_itselfT),
wenzelm@17703
   571
     Const (Term.dummy_patternN, aT)]
wenzelm@18713
   572
  |> Theory.add_syntax
wenzelm@18713
   573
   []
wenzelm@16441
   574
  |> Sign.local_path
haftmann@18377
   575
  |> (add_defs_i false o map Thm.no_attributes)
haftmann@18377
   576
       [("prop_def", Logic.mk_equals (Logic.protect A, A))] |> snd
haftmann@18377
   577
  |> add_thmss [(("nothing", []), [])] |> snd
berghofe@11516
   578
  |> Theory.add_axioms_i Proofterm.equality_axms
wenzelm@16493
   579
  |> Theory.end_theory;
wenzelm@3987
   580
wenzelm@5091
   581
structure ProtoPure =
wenzelm@5091
   582
struct
wenzelm@5091
   583
  val thy = proto_pure;
wenzelm@18031
   584
  val prop_def = get_axiom thy "prop_def";
wenzelm@5091
   585
end;
wenzelm@3987
   586
wenzelm@3987
   587
end;
wenzelm@3987
   588
wenzelm@4022
   589
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   590
open BasicPureThy;