src/Pure/pure_thy.ML
author wenzelm
Fri Jan 27 19:03:07 2006 +0100 (2006-01-27)
changeset 18801 b913ce69660c
parent 18778 f623f7a35ced
child 18838 d32f70789342
permissions -rw-r--r--
moved theorem tags from Drule to PureThy;
replaced kind attribute by kind string;
tuned name_multi: index > 1 only;
added note_thmss_qualified (from Isar/locale.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@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@18801
    32
  val map_tags: (tag list -> tag list) -> thm -> thm
wenzelm@18801
    33
  val tag_rule: tag -> thm -> thm
wenzelm@18801
    34
  val untag_rule: string -> thm -> thm
wenzelm@18801
    35
  val tag: tag -> attribute
wenzelm@18801
    36
  val untag: string -> attribute
wenzelm@18801
    37
  val get_kind: thm -> string
wenzelm@18801
    38
  val kind_rule: string -> thm -> thm
wenzelm@18801
    39
  val kind: string -> attribute
wenzelm@18801
    40
  val theoremK: string
wenzelm@18801
    41
  val lemmaK: string
wenzelm@18801
    42
  val corollaryK: string
wenzelm@18801
    43
  val internalK: string
wenzelm@18801
    44
  val kind_internal: attribute
wenzelm@18801
    45
  val has_internal: tag list -> bool
wenzelm@18801
    46
  val is_internal: thm -> bool
wenzelm@16023
    47
  val string_of_thmref: thmref -> string
wenzelm@16493
    48
  val print_theorems_diff: theory -> theory -> unit
berghofe@15456
    49
  val get_thm_closure: theory -> thmref -> thm
berghofe@15456
    50
  val get_thms_closure: theory -> thmref -> thm list
wenzelm@9564
    51
  val single_thm: string -> thm list -> thm
wenzelm@16493
    52
  val name_of_thmref: thmref -> string
wenzelm@16493
    53
  val map_name_of_thmref: (string -> string) -> thmref -> thmref
berghofe@15456
    54
  val select_thm: thmref -> thm list -> thm list
wenzelm@16023
    55
  val selections: string * thm list -> (thmref * thm) list
wenzelm@17162
    56
  val theorems_of: theory -> thm list NameSpace.table
wenzelm@17162
    57
  val theorem_space: theory -> NameSpace.T
wenzelm@16023
    58
  val fact_index_of: theory -> FactIndex.T
wenzelm@16023
    59
  val valid_thms: theory -> thmref * thm list -> bool
wenzelm@16023
    60
  val thms_containing: theory -> FactIndex.spec -> (string * thm list) list
nipkow@13646
    61
  val thms_containing_consts: theory -> string list -> (string * thm) list
wenzelm@16336
    62
  val thms_of: theory -> (string * thm) list
wenzelm@16336
    63
  val all_thms_of: theory -> (string * thm) list
wenzelm@12695
    64
  val hide_thms: bool -> string list -> theory -> theory
wenzelm@18728
    65
  val store_thm: (bstring * thm) * attribute list -> theory -> thm * theory
wenzelm@7405
    66
  val smart_store_thms: (bstring * thm list) -> thm list
wenzelm@12138
    67
  val smart_store_thms_open: (bstring * thm list) -> thm list
wenzelm@7899
    68
  val forall_elim_var: int -> thm -> thm
wenzelm@7899
    69
  val forall_elim_vars: int -> thm -> thm
wenzelm@18778
    70
  val name_multi: string -> 'a list -> (string * 'a) list
wenzelm@18728
    71
  val add_thms: ((bstring * thm) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    72
  val add_thmss: ((bstring * thm list) * attribute list) list -> theory -> thm list list * theory 
wenzelm@18801
    73
  val note_thmss: string -> ((bstring * attribute list) *
wenzelm@18728
    74
    (thmref * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18801
    75
  val note_thmss_i: string -> ((bstring * attribute list) *
wenzelm@18801
    76
    (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18801
    77
  val note_thmss_qualified: string -> string -> ((bstring * attribute list) *
wenzelm@18728
    78
    (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18728
    79
  val add_axioms: ((bstring * string) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    80
  val add_axioms_i: ((bstring * term) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    81
  val add_axiomss: ((bstring * string list) * attribute list) list ->
wenzelm@18728
    82
    theory -> thm list list * theory
wenzelm@18728
    83
  val add_axiomss_i: ((bstring * term list) * attribute list) list ->
wenzelm@18728
    84
    theory -> thm list list * theory
wenzelm@18728
    85
  val add_defs: bool -> ((bstring * string) * attribute list) list ->
haftmann@18377
    86
    theory -> thm list * theory
wenzelm@18728
    87
  val add_defs_i: bool -> ((bstring * term) * attribute list) list ->
haftmann@18377
    88
    theory -> thm list * theory
wenzelm@18728
    89
  val add_defss: bool -> ((bstring * string list) * attribute list) list ->
haftmann@18377
    90
    theory -> thm list list * theory
wenzelm@18728
    91
  val add_defss_i: bool -> ((bstring * term list) * attribute list) list ->
haftmann@18358
    92
    theory -> thm list list * theory
wenzelm@18666
    93
  val generic_setup: string option -> theory -> theory
wenzelm@17342
    94
  val add_oracle: bstring * string * string -> theory -> theory
wenzelm@3987
    95
end;
wenzelm@3987
    96
wenzelm@3987
    97
structure PureThy: PURE_THY =
wenzelm@3987
    98
struct
wenzelm@3987
    99
wenzelm@3987
   100
wenzelm@18801
   101
(*** theorem tags ***)
wenzelm@18801
   102
wenzelm@18801
   103
(* add / delete tags *)
wenzelm@18801
   104
wenzelm@18801
   105
fun map_tags f thm =
wenzelm@18801
   106
  Thm.put_name_tags (Thm.name_of_thm thm, f (#2 (Thm.get_name_tags thm))) thm;
wenzelm@18801
   107
wenzelm@18801
   108
fun tag_rule tg = map_tags (fn tgs => if tg mem tgs then tgs else tgs @ [tg]);
wenzelm@18801
   109
fun untag_rule s = map_tags (filter_out (equal s o #1));
wenzelm@18801
   110
wenzelm@18801
   111
fun tag tg x = Thm.rule_attribute (K (tag_rule tg)) x;
wenzelm@18801
   112
fun untag s x = Thm.rule_attribute (K (untag_rule s)) x;
wenzelm@18801
   113
wenzelm@18801
   114
fun simple_tag name x = tag (name, []) x;
wenzelm@18801
   115
wenzelm@18801
   116
wenzelm@18801
   117
(* theorem kinds *)
wenzelm@18801
   118
wenzelm@18801
   119
val theoremK = "theorem";
wenzelm@18801
   120
val lemmaK = "lemma";
wenzelm@18801
   121
val corollaryK = "corollary";
wenzelm@18801
   122
val internalK = "internal";
wenzelm@18801
   123
wenzelm@18801
   124
fun get_kind thm =
wenzelm@18801
   125
  (case AList.lookup (op =) ((#2 o Thm.get_name_tags) thm) "kind" of
wenzelm@18801
   126
    SOME (k :: _) => k
wenzelm@18801
   127
  | _ => "unknown");
wenzelm@18801
   128
wenzelm@18801
   129
fun kind_rule k = tag_rule ("kind", [k]) o untag_rule "kind";
wenzelm@18801
   130
fun kind k x = if k = "" then x else Thm.rule_attribute (K (kind_rule k)) x;
wenzelm@18801
   131
fun kind_internal x = kind internalK x;
wenzelm@18801
   132
fun has_internal tags = exists (fn ("kind", [k]) => k = internalK | _ => false) tags;
wenzelm@18801
   133
val is_internal = has_internal o Thm.tags_of_thm;
wenzelm@18801
   134
wenzelm@18801
   135
wenzelm@18801
   136
wenzelm@4922
   137
(*** theorem database ***)
wenzelm@3987
   138
wenzelm@16441
   139
(** dataype theorems **)
wenzelm@3987
   140
wenzelm@16493
   141
fun pretty_theorems_diff thy prev_thms (space, thms) =
wenzelm@16441
   142
  let
wenzelm@16441
   143
    val prt_thm = Display.pretty_thm_sg thy;
wenzelm@16441
   144
    fun prt_thms (name, [th]) =
wenzelm@16441
   145
          Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
wenzelm@16441
   146
      | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
wenzelm@16493
   147
wenzelm@16493
   148
    val diff_thmss = Symtab.fold (fn fact =>
wenzelm@16493
   149
      if not (Symtab.member eq_thms prev_thms fact) then cons fact else I) thms [];
wenzelm@16493
   150
    val thmss = diff_thmss |> map (apfst (NameSpace.extern space)) |> Library.sort_wrt #1;
wenzelm@16441
   151
  in Pretty.big_list "theorems:" (map prt_thms thmss) end;
wenzelm@16441
   152
wenzelm@16493
   153
fun pretty_theorems thy = pretty_theorems_diff thy Symtab.empty;
wenzelm@16493
   154
wenzelm@16441
   155
structure TheoremsData = TheoryDataFun
wenzelm@16441
   156
(struct
wenzelm@5005
   157
  val name = "Pure/theorems";
wenzelm@5005
   158
  type T =
wenzelm@16441
   159
   {theorems: thm list NameSpace.table,
wenzelm@16441
   160
    index: FactIndex.T} ref;
wenzelm@3987
   161
wenzelm@4853
   162
  fun mk_empty _ =
wenzelm@16336
   163
    ref {theorems = NameSpace.empty_table, index = FactIndex.empty}: T;
wenzelm@3987
   164
wenzelm@5005
   165
  val empty = mk_empty ();
wenzelm@6547
   166
  fun copy (ref x) = ref x;
wenzelm@16441
   167
  val extend = mk_empty;
wenzelm@16441
   168
  fun merge _ = mk_empty;
wenzelm@16441
   169
  fun print thy (ref {theorems, index}) = Pretty.writeln (pretty_theorems thy theorems);
wenzelm@16441
   170
end);
wenzelm@3987
   171
wenzelm@16493
   172
val get_theorems_ref = TheoremsData.get;
wenzelm@16493
   173
val get_theorems = ! o get_theorems_ref;
wenzelm@17162
   174
val theorems_of = #theorems o get_theorems;
wenzelm@17162
   175
val theorem_space = #1 o theorems_of;
wenzelm@16493
   176
val fact_index_of = #index o get_theorems;
wenzelm@16023
   177
wenzelm@6367
   178
wenzelm@5000
   179
(* print theory *)
wenzelm@3987
   180
wenzelm@5005
   181
val print_theorems = TheoremsData.print;
wenzelm@8720
   182
wenzelm@16493
   183
fun print_theorems_diff prev_thy thy =
wenzelm@16493
   184
  Pretty.writeln (pretty_theorems_diff thy
wenzelm@17162
   185
    (#2 (theorems_of prev_thy)) (#theorems (get_theorems thy)));
wenzelm@16493
   186
wenzelm@5000
   187
fun print_theory thy =
wenzelm@9215
   188
  Display.pretty_full_theory thy @
wenzelm@16493
   189
    [pretty_theorems thy (#theorems (get_theorems thy))]
wenzelm@8720
   190
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@4022
   191
wenzelm@3987
   192
wenzelm@3987
   193
wenzelm@4022
   194
(** retrieve theorems **)
wenzelm@3987
   195
skalberg@15531
   196
fun the_thms _ (SOME thms) = thms
skalberg@15531
   197
  | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
wenzelm@4037
   198
wenzelm@9564
   199
fun single_thm _ [thm] = thm
wenzelm@9564
   200
  | single_thm name _ = error ("Single theorem expected " ^ quote name);
wenzelm@9564
   201
wenzelm@15703
   202
wenzelm@16023
   203
(* datatype interval *)
wenzelm@15703
   204
wenzelm@15703
   205
datatype interval =
wenzelm@15703
   206
  FromTo of int * int |
wenzelm@15703
   207
  From of int |
wenzelm@15703
   208
  Single of int;
wenzelm@15703
   209
wenzelm@15703
   210
fun interval _ (FromTo (i, j)) = i upto j
wenzelm@15703
   211
  | interval n (From i) = i upto n
wenzelm@15703
   212
  | interval _ (Single i) = [i];
wenzelm@15703
   213
wenzelm@16023
   214
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@16023
   215
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@16023
   216
  | string_of_interval (Single i) = string_of_int i;
wenzelm@16023
   217
wenzelm@16023
   218
wenzelm@16493
   219
(* datatype thmref *)
wenzelm@16493
   220
wenzelm@16493
   221
datatype thmref =
wenzelm@16493
   222
  Name of string |
wenzelm@18031
   223
  NameSelection of string * interval list |
wenzelm@18031
   224
  Fact of string;
wenzelm@15703
   225
wenzelm@16493
   226
fun name_of_thmref (Name name) = name
wenzelm@18031
   227
  | name_of_thmref (NameSelection (name, _)) = name
wenzelm@18678
   228
  | name_of_thmref (Fact _) = error "Illegal literal fact";
wenzelm@16023
   229
wenzelm@16493
   230
fun map_name_of_thmref f (Name name) = Name (f name)
wenzelm@18031
   231
  | map_name_of_thmref f (NameSelection (name, is)) = NameSelection (f name, is)
wenzelm@18031
   232
  | map_name_of_thmref _ thmref = thmref;
wenzelm@16493
   233
wenzelm@16493
   234
fun string_of_thmref (Name name) = name
wenzelm@16493
   235
  | string_of_thmref (NameSelection (name, is)) =
wenzelm@18031
   236
      name ^ enclose "(" ")" (commas (map string_of_interval is))
wenzelm@18678
   237
  | string_of_thmref (Fact _) = error "Illegal literal fact";
wenzelm@16023
   238
wenzelm@16023
   239
wenzelm@16023
   240
(* select_thm *)
wenzelm@15703
   241
wenzelm@16493
   242
fun select_thm (Name _) thms = thms
wenzelm@18031
   243
  | select_thm (Fact _) thms = thms
wenzelm@16493
   244
  | select_thm (NameSelection (name, is)) thms =
wenzelm@16023
   245
      let
wenzelm@16023
   246
        val n = length thms;
wenzelm@16023
   247
        fun select i =
wenzelm@16023
   248
          if i < 1 orelse i > n then
wenzelm@16023
   249
            error ("Bad subscript " ^ string_of_int i ^ " for " ^
wenzelm@16023
   250
              quote name ^ " (length " ^ string_of_int n ^ ")")
wenzelm@16023
   251
          else List.nth (thms, i - 1);
wenzelm@16023
   252
      in map select (List.concat (map (interval n) is)) end;
wenzelm@15703
   253
wenzelm@16023
   254
wenzelm@16023
   255
(* selections *)
wenzelm@16023
   256
wenzelm@16493
   257
fun selections (name, [thm]) = [(Name name, thm)]
wenzelm@16023
   258
  | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) =>
wenzelm@16493
   259
      (NameSelection (name, [Single i]), thm));
berghofe@15456
   260
wenzelm@9564
   261
wenzelm@9808
   262
(* get_thm(s)_closure -- statically scoped versions *)
wenzelm@9564
   263
wenzelm@9564
   264
(*beware of proper order of evaluation!*)
wenzelm@4922
   265
wenzelm@9564
   266
fun lookup_thms thy =
wenzelm@9564
   267
  let
wenzelm@16441
   268
    val thy_ref = Theory.self_ref thy;
wenzelm@16493
   269
    val (space, thms) = #theorems (get_theorems thy);
wenzelm@9564
   270
  in
wenzelm@9564
   271
    fn name =>
wenzelm@17221
   272
      Option.map (map (Thm.transfer (Theory.deref thy_ref)))     (*dynamic identity*)
wenzelm@17418
   273
      (Symtab.lookup thms (NameSpace.intern space name)) (*static content*)
wenzelm@9564
   274
  end;
wenzelm@3987
   275
wenzelm@9564
   276
fun get_thms_closure thy =
wenzelm@16441
   277
  let val closures = map lookup_thms (thy :: Theory.ancestors_of thy) in
wenzelm@16493
   278
    fn thmref =>
wenzelm@18031
   279
      let val name = name_of_thmref thmref;
wenzelm@16493
   280
      in select_thm thmref (the_thms name (get_first (fn f => f name) closures)) end
berghofe@15456
   281
  end;
wenzelm@9564
   282
wenzelm@9808
   283
fun get_thm_closure thy =
wenzelm@9808
   284
  let val get = get_thms_closure thy
wenzelm@16493
   285
  in fn thmref => single_thm (name_of_thmref thmref) (get thmref) end;
wenzelm@9808
   286
wenzelm@9564
   287
wenzelm@16441
   288
(* get_thms etc. *)
wenzelm@9564
   289
wenzelm@16493
   290
fun get_thms theory thmref =
wenzelm@16493
   291
  let val name = name_of_thmref thmref in
wenzelm@16493
   292
    get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory)
wenzelm@16493
   293
    |> the_thms name |> select_thm thmref |> map (Thm.transfer theory)
wenzelm@16493
   294
  end;
wenzelm@4022
   295
wenzelm@16493
   296
fun get_thmss thy thmrefs = List.concat (map (get_thms thy) thmrefs);
wenzelm@16493
   297
fun get_thm thy thmref = single_thm (name_of_thmref thmref) (get_thms thy thmref);
wenzelm@4783
   298
wenzelm@17930
   299
fun thm name = get_thm (the_context ()) (Name name);
wenzelm@17930
   300
fun thms name = get_thms (the_context ()) (Name name);
wenzelm@17930
   301
wenzelm@4022
   302
wenzelm@16023
   303
(* thms_containing etc. *)
wenzelm@4022
   304
wenzelm@16023
   305
fun valid_thms thy (thmref, ths) =
wenzelm@18678
   306
  (case try (get_thms thy) thmref of
wenzelm@16023
   307
    NONE => false
wenzelm@16132
   308
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@3987
   309
wenzelm@16023
   310
fun thms_containing theory spec =
wenzelm@16023
   311
  (theory :: Theory.ancestors_of theory)
wenzelm@16023
   312
  |> map (fn thy =>
wenzelm@16023
   313
      FactIndex.find (fact_index_of thy) spec
wenzelm@16493
   314
      |> List.filter (fn (name, ths) => valid_thms theory (Name name, ths))
haftmann@17496
   315
      |> gen_distinct (eq_fst (op =)))
wenzelm@16023
   316
  |> List.concat;
wenzelm@4022
   317
nipkow@13646
   318
fun thms_containing_consts thy consts =
skalberg@15570
   319
  thms_containing thy (consts, []) |> map #2 |> List.concat
wenzelm@16023
   320
  |> map (fn th => (Thm.name_of_thm th, th));
nipkow@13646
   321
kleing@15882
   322
wenzelm@16336
   323
(* thms_of etc. *)
kleing@15882
   324
wenzelm@16023
   325
fun thms_of thy =
wenzelm@17162
   326
  let val thms = #2 (theorems_of thy)
wenzelm@17162
   327
  in map (fn th => (Thm.name_of_thm th, th)) (List.concat (map snd (Symtab.dest thms))) end;
wenzelm@15703
   328
wenzelm@16336
   329
fun all_thms_of thy = List.concat (map thms_of (thy :: Theory.ancestors_of thy));
wenzelm@16336
   330
wenzelm@4022
   331
wenzelm@4022
   332
wenzelm@4022
   333
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   334
wenzelm@16441
   335
(* hiding -- affects current theory node only *)
wenzelm@12695
   336
wenzelm@13424
   337
fun hide_thms fully names thy =
wenzelm@12695
   338
  let
wenzelm@16493
   339
    val r as ref {theorems = (space, thms), index} = get_theorems_ref thy;
wenzelm@16132
   340
    val space' = fold (NameSpace.hide fully) names space;
wenzelm@16336
   341
  in r := {theorems = (space', thms), index = index}; thy end;
wenzelm@12695
   342
wenzelm@12695
   343
wenzelm@4853
   344
(* naming *)
wenzelm@4853
   345
wenzelm@18614
   346
fun gen_names _ len "" = replicate len ""
wenzelm@18614
   347
  | gen_names j len name = map (fn i => name ^ "_" ^ string_of_int i) (j + 1 upto j + len);
wenzelm@4853
   348
wenzelm@18801
   349
fun name_multi name [x] = [(name, x)]
wenzelm@18801
   350
  | name_multi name xs = gen_names 0 (length xs) name ~~ xs;
berghofe@12235
   351
wenzelm@16441
   352
fun name_thm pre (name, thm) =
wenzelm@16441
   353
  if Thm.name_of_thm thm <> "" andalso pre then thm else Thm.name_thm (name, thm);
berghofe@12872
   354
wenzelm@18801
   355
fun name_thms pre name xs = map (name_thm pre) (name_multi name xs);
berghofe@12235
   356
wenzelm@16441
   357
fun name_thmss name xs =
wenzelm@16441
   358
  (case filter_out (null o fst) xs of
berghofe@12872
   359
    [([x], z)] => [([name_thm true (name, x)], z)]
haftmann@18418
   360
  | _ => fst (fold_map (fn (ys, z) => fn i =>
haftmann@18418
   361
    ((map (name_thm true) (gen_names i (length ys) name ~~ ys), z), i + length ys)) xs 0));
wenzelm@4853
   362
wenzelm@4853
   363
berghofe@11998
   364
(* enter_thms *)
wenzelm@4853
   365
wenzelm@7470
   366
fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@7470
   367
fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   368
haftmann@18418
   369
fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
wenzelm@16441
   370
  | enter_thms pre_name post_name app_att (bname, thms) thy =
wenzelm@7470
   371
      let
wenzelm@16441
   372
        val name = Sign.full_name thy bname;
wenzelm@16441
   373
        val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
wenzelm@16513
   374
        val r as ref {theorems = (space, theorems), index} = get_theorems_ref thy';
wenzelm@16513
   375
        val space' = Sign.declare_name thy' name space;
wenzelm@17418
   376
        val theorems' = Symtab.update (name, thms') theorems;
wenzelm@18031
   377
        val index' = FactIndex.add_global (name, thms') index;
wenzelm@13274
   378
      in
wenzelm@17418
   379
        (case Symtab.lookup theorems name of
skalberg@15531
   380
          NONE => ()
wenzelm@16441
   381
        | SOME thms'' =>
wenzelm@16441
   382
            if Thm.eq_thms (thms', thms'') then warn_same name
wenzelm@13274
   383
            else warn_overwrite name);
wenzelm@16336
   384
        r := {theorems = (space', theorems'), index = index'};
haftmann@18418
   385
        (thms', thy')
berghofe@11998
   386
      end;
wenzelm@3987
   387
wenzelm@16023
   388
wenzelm@6091
   389
(* add_thms(s) *)
wenzelm@4853
   390
wenzelm@16441
   391
fun add_thms_atts pre_name ((bname, thms), atts) =
wenzelm@18728
   392
  enter_thms pre_name (name_thms false) (foldl_map (Thm.theory_attributes atts)) (bname, thms);
wenzelm@4853
   393
haftmann@18377
   394
fun gen_add_thmss pre_name =
haftmann@18377
   395
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   396
berghofe@12235
   397
fun gen_add_thms pre_name args =
haftmann@18377
   398
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   399
berghofe@12872
   400
val add_thmss = gen_add_thmss (name_thms true);
berghofe@12872
   401
val add_thms = gen_add_thms (name_thms true);
wenzelm@5907
   402
wenzelm@5907
   403
wenzelm@14564
   404
(* note_thmss(_i) *)
wenzelm@5907
   405
wenzelm@9192
   406
local
wenzelm@12711
   407
wenzelm@18801
   408
fun gen_note_thmss get k = fold_map (fn ((bname, more_atts), ths_atts) => fn thy =>
wenzelm@12711
   409
  let
wenzelm@18728
   410
    fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
haftmann@18418
   411
    val (thms, thy') = thy |> enter_thms
wenzelm@16441
   412
      name_thmss (name_thms false) (apsnd List.concat o foldl_map app)
wenzelm@18801
   413
      (bname, map (fn (ths, atts) => (get thy ths, atts @ more_atts @ [kind k])) ths_atts);
wenzelm@18801
   414
  in ((bname, thms), thy') end);
wenzelm@12711
   415
wenzelm@9192
   416
in
wenzelm@12711
   417
haftmann@18418
   418
val note_thmss = gen_note_thmss get_thms;
haftmann@18418
   419
val note_thmss_i = gen_note_thmss (K I);
wenzelm@12711
   420
wenzelm@18801
   421
fun note_thmss_qualified k path facts thy =
wenzelm@18801
   422
  thy
wenzelm@18801
   423
  |> Theory.add_path path
wenzelm@18801
   424
  |> Theory.no_base_names
wenzelm@18801
   425
  |> note_thmss_i k facts
wenzelm@18801
   426
  ||> Theory.restore_naming thy;
wenzelm@18801
   427
wenzelm@9192
   428
end;
wenzelm@5280
   429
wenzelm@5280
   430
wenzelm@6091
   431
(* store_thm *)
wenzelm@5280
   432
berghofe@11998
   433
fun store_thm ((bname, thm), atts) thy =
haftmann@18377
   434
  let val ([th'], thy') = add_thms_atts (name_thms true) ((bname, [thm]), atts) thy
haftmann@18358
   435
  in (th', thy') end;
wenzelm@3987
   436
wenzelm@3987
   437
wenzelm@16441
   438
(* smart_store_thms(_open) *)
wenzelm@3987
   439
wenzelm@16441
   440
local
wenzelm@16441
   441
wenzelm@16441
   442
fun smart_store _ (name, []) =
berghofe@11516
   443
      error ("Cannot store empty list of theorems: " ^ quote name)
wenzelm@16441
   444
  | smart_store name_thm (name, [thm]) =
haftmann@18418
   445
      fst (enter_thms (name_thm true) (name_thm false) I (name, [thm]) (Thm.theory_of_thm thm))
wenzelm@16441
   446
  | smart_store name_thm (name, thms) =
wenzelm@7405
   447
      let
wenzelm@16441
   448
        fun merge (thy, th) = Theory.merge (thy, Thm.theory_of_thm th);
wenzelm@16441
   449
        val thy = Library.foldl merge (Thm.theory_of_thm (hd thms), tl thms);
haftmann@18418
   450
      in fst (enter_thms (name_thm true) (name_thm false) I (name, thms) thy) end;
berghofe@11516
   451
wenzelm@16441
   452
in
wenzelm@16441
   453
wenzelm@16441
   454
val smart_store_thms = smart_store name_thms;
wenzelm@16441
   455
val smart_store_thms_open = smart_store (K (K I));
wenzelm@16441
   456
wenzelm@16441
   457
end;
wenzelm@3987
   458
wenzelm@3987
   459
wenzelm@16722
   460
(* forall_elim_var(s) -- belongs to drule.ML *)
wenzelm@7899
   461
wenzelm@16722
   462
fun forall_elim_vars_aux strip_vars i th =
wenzelm@16722
   463
  let
wenzelm@16722
   464
    val {thy, tpairs, prop, ...} = Thm.rep_thm th;
haftmann@16787
   465
    val add_used = Term.fold_aterms
haftmann@16787
   466
      (fn Var ((x, j), _) => if i = j then curry (op ins_string) x else I | _ => I);
wenzelm@16722
   467
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@16722
   468
    val vars = strip_vars prop;
wenzelm@16722
   469
    val cvars = (Term.variantlist (map #1 vars, used), vars)
wenzelm@16722
   470
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@16722
   471
  in fold Thm.forall_elim cvars th end;
wenzelm@7899
   472
wenzelm@16722
   473
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@16722
   474
wenzelm@16722
   475
fun forall_elim_var i th = forall_elim_vars_aux
wenzelm@16722
   476
  (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@16722
   477
  | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@7899
   478
wenzelm@7899
   479
wenzelm@4022
   480
(* store axioms as theorems *)
wenzelm@4022
   481
wenzelm@4853
   482
local
wenzelm@17418
   483
  fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
wenzelm@17418
   484
  fun get_axs thy named_axs = map (forall_elim_vars 0 o get_ax thy) named_axs;
haftmann@18377
   485
  fun add_single add ((name, ax), atts) thy =
wenzelm@4853
   486
    let
berghofe@11998
   487
      val named_ax = [(name, ax)];
wenzelm@7753
   488
      val thy' = add named_ax thy;
wenzelm@7753
   489
      val thm = hd (get_axs thy' named_ax);
haftmann@18377
   490
    in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
haftmann@18377
   491
  fun add_multi add ((name, axs), atts) thy =
wenzelm@7753
   492
    let
wenzelm@7753
   493
      val named_axs = name_multi name axs;
wenzelm@4853
   494
      val thy' = add named_axs thy;
wenzelm@7753
   495
      val thms = get_axs thy' named_axs;
haftmann@18377
   496
    in apfst hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
haftmann@18377
   497
  fun add_singles add = fold_map (add_single add);
haftmann@18377
   498
  fun add_multis add = fold_map (add_multi add);
wenzelm@4853
   499
in
wenzelm@7753
   500
  val add_axioms    = add_singles Theory.add_axioms;
wenzelm@7753
   501
  val add_axioms_i  = add_singles Theory.add_axioms_i;
wenzelm@7753
   502
  val add_axiomss   = add_multis Theory.add_axioms;
wenzelm@7753
   503
  val add_axiomss_i = add_multis Theory.add_axioms_i;
haftmann@18377
   504
  val add_defs      = add_singles o Theory.add_defs;
haftmann@18377
   505
  val add_defs_i    = add_singles o Theory.add_defs_i;
haftmann@18377
   506
  val add_defss     = add_multis o Theory.add_defs;
haftmann@18377
   507
  val add_defss_i   = add_multis o Theory.add_defs_i;
wenzelm@4853
   508
end;
wenzelm@4022
   509
wenzelm@4022
   510
wenzelm@3987
   511
wenzelm@17342
   512
(*** ML setup ***)
wenzelm@17342
   513
wenzelm@17342
   514
(* generic_setup *)
wenzelm@17342
   515
wenzelm@18713
   516
fun generic_setup NONE = (fn thy => thy |> Context.setup ())
wenzelm@18713
   517
  | generic_setup (SOME txt) = Context.use_let "val setup: theory -> theory" "setup" txt;
wenzelm@17342
   518
wenzelm@17342
   519
wenzelm@17342
   520
(* add_oracle *)
wenzelm@17342
   521
wenzelm@17342
   522
fun add_oracle (name, T, oracle) =
wenzelm@17342
   523
  let val txt =
wenzelm@17342
   524
    "local\n\
wenzelm@17342
   525
    \  type T = " ^ T ^ ";\n\
wenzelm@17342
   526
    \  val oracle: theory -> T -> term = " ^ oracle ^ ";\n\
wenzelm@17342
   527
    \  val name = " ^ quote name ^ ";\n\
wenzelm@17342
   528
    \  exception Arg of T;\n\
wenzelm@17342
   529
    \  val _ = Context.>> (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x));\n\
wenzelm@17342
   530
    \  val thy = Context.the_context ();\n\
wenzelm@17342
   531
    \  val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\
wenzelm@17342
   532
    \in\n\
wenzelm@17342
   533
    \  fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\
wenzelm@17342
   534
    \end;\n";
wenzelm@17342
   535
  in Context.use_mltext_theory txt false end;
wenzelm@17342
   536
wenzelm@17342
   537
wenzelm@17342
   538
wenzelm@5091
   539
(*** the ProtoPure theory ***)
wenzelm@3987
   540
wenzelm@16441
   541
val aT = TFree ("'a", []);
wenzelm@16441
   542
val A = Free ("A", propT);
wenzelm@16441
   543
wenzelm@3987
   544
val proto_pure =
wenzelm@16493
   545
  Context.pre_pure_thy
wenzelm@16987
   546
  |> Compress.init_data
wenzelm@16536
   547
  |> Sign.init_data
wenzelm@16536
   548
  |> Theory.init_data
wenzelm@16536
   549
  |> Proofterm.init_data
wenzelm@16023
   550
  |> TheoremsData.init
wenzelm@3987
   551
  |> Theory.add_types
wenzelm@4922
   552
   [("fun", 2, NoSyn),
wenzelm@4922
   553
    ("prop", 0, NoSyn),
wenzelm@4922
   554
    ("itself", 1, NoSyn),
wenzelm@4922
   555
    ("dummy", 0, NoSyn)]
wenzelm@18713
   556
  |> Theory.add_nonterminals Syntax.basic_nonterms
wenzelm@3987
   557
  |> Theory.add_syntax
wenzelm@18713
   558
   [("_lambda",     "[pttrns, 'a] => logic",     Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@18713
   559
    ("_abs",        "'a",                        NoSyn),
wenzelm@18713
   560
    ("",            "'a => args",                Delimfix "_"),
wenzelm@18713
   561
    ("_args",       "['a, args] => args",        Delimfix "_,/ _"),
wenzelm@18713
   562
    ("",            "id => idt",                 Delimfix "_"),
wenzelm@18713
   563
    ("_idtdummy",   "idt",                       Delimfix "'_"),
wenzelm@18713
   564
    ("_idtyp",      "[id, type] => idt",         Mixfix ("_::_", [], 0)),
wenzelm@18713
   565
    ("_idtypdummy", "type => idt",               Mixfix ("'_()::_", [], 0)),
wenzelm@18713
   566
    ("",            "idt => idt",                Delimfix "'(_')"),
wenzelm@18713
   567
    ("",            "idt => idts",               Delimfix "_"),
wenzelm@18713
   568
    ("_idts",       "[idt, idts] => idts",       Mixfix ("_/ _", [1, 0], 0)),
wenzelm@18713
   569
    ("",            "idt => pttrn",              Delimfix "_"),
wenzelm@18713
   570
    ("",            "pttrn => pttrns",           Delimfix "_"),
wenzelm@18713
   571
    ("_pttrns",     "[pttrn, pttrns] => pttrns", Mixfix ("_/ _", [1, 0], 0)),
wenzelm@18713
   572
    ("",            "id => aprop",               Delimfix "_"),
wenzelm@18713
   573
    ("",            "longid => aprop",           Delimfix "_"),
wenzelm@18713
   574
    ("",            "var => aprop",              Delimfix "_"),
wenzelm@18713
   575
    ("_DDDOT",      "aprop",                     Delimfix "..."),
wenzelm@18713
   576
    ("_aprop",      "aprop => prop",             Delimfix "PROP _"),
wenzelm@18713
   577
    ("_asm",        "prop => asms",              Delimfix "_"),
wenzelm@18713
   578
    ("_asms",       "[prop, asms] => asms",      Delimfix "_;/ _"),
wenzelm@18713
   579
    ("_bigimpl",    "[asms, prop] => prop",      Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@18713
   580
    ("_ofclass",    "[type, logic] => prop",     Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@18713
   581
    ("_mk_ofclass", "_",                         NoSyn),
wenzelm@18713
   582
    ("_TYPE",       "type => logic",             Delimfix "(1TYPE/(1'(_')))"),
wenzelm@18713
   583
    ("",            "id => logic",               Delimfix "_"),
wenzelm@18713
   584
    ("",            "longid => logic",           Delimfix "_"),
wenzelm@18713
   585
    ("",            "var => logic",              Delimfix "_"),
wenzelm@18713
   586
    ("_DDDOT",      "logic",                     Delimfix "..."),
wenzelm@18713
   587
    ("_constify",   "num => num_const",          Delimfix "_"),
wenzelm@18713
   588
    ("_indexnum",   "num_const => index",        Delimfix "\\<^sub>_"),
wenzelm@18713
   589
    ("_index",      "logic => index",            Delimfix "(00\\<^bsub>_\\<^esub>)"),
wenzelm@18713
   590
    ("_indexdefault", "index",                   Delimfix ""),
wenzelm@18713
   591
    ("_indexvar",   "index",                     Delimfix "'\\<index>"),
wenzelm@18713
   592
    ("_struct",     "index => logic",            Mixfix ("\\<struct>_", [1000], 1000)),
wenzelm@18713
   593
    ("==>",         "prop => prop => prop",      Delimfix "op ==>"),
wenzelm@18713
   594
    (Term.dummy_patternN, "aprop",               Delimfix "'_")]
wenzelm@18713
   595
  |> Theory.add_syntax Syntax.appl_syntax
wenzelm@18713
   596
  |> Theory.add_modesyntax (Symbol.xsymbolsN, true)
wenzelm@18713
   597
   [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@18713
   598
    ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@18713
   599
    ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
wenzelm@18713
   600
    ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@18713
   601
    ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@18713
   602
    ("_idtypdummy", "type => idt",        Mixfix ("'_()\\<Colon>_", [], 0)),
wenzelm@18713
   603
    ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@18713
   604
    ("==",       "['a, 'a] => prop",      InfixrName ("\\<equiv>", 2)),
wenzelm@18713
   605
    ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
wenzelm@18713
   606
    ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@18713
   607
    ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
wenzelm@18713
   608
    ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@18713
   609
    ("_DDDOT",   "logic",                 Delimfix "\\<dots>")]
wenzelm@18713
   610
  |> Theory.add_modesyntax ("", false)
wenzelm@18713
   611
   [("prop", "prop => prop", Mixfix ("_", [0], 0)),
wenzelm@18713
   612
    ("_meta_conjunction", "prop => prop => prop", InfixrName ("&&", 2))]
wenzelm@18713
   613
  |> Theory.add_modesyntax ("HTML", false)
wenzelm@18713
   614
   [("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
wenzelm@18713
   615
  |> Theory.add_trfuns Syntax.pure_trfuns
wenzelm@18713
   616
  |> Theory.add_trfunsT Syntax.pure_trfunsT
wenzelm@3987
   617
  |> Theory.add_consts
wenzelm@16441
   618
   [("==", "'a => 'a => prop", InfixrName ("==", 2)),
wenzelm@16441
   619
    ("==>", "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@3987
   620
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@18031
   621
    ("prop", "prop => prop", NoSyn),
wenzelm@6547
   622
    ("TYPE", "'a itself", NoSyn),
wenzelm@9534
   623
    (Term.dummy_patternN, "'a", Delimfix "'_")]
skalberg@14223
   624
  |> Theory.add_finals_i false
wenzelm@16441
   625
    [Const ("==", [aT, aT] ---> propT),
wenzelm@16441
   626
     Const ("==>", [propT, propT] ---> propT),
wenzelm@16441
   627
     Const ("all", (aT --> propT) --> propT),
wenzelm@17703
   628
     Const ("TYPE", a_itselfT),
wenzelm@17703
   629
     Const (Term.dummy_patternN, aT)]
wenzelm@18713
   630
  |> Theory.add_syntax
wenzelm@18713
   631
   []
wenzelm@16441
   632
  |> Sign.local_path
haftmann@18377
   633
  |> (add_defs_i false o map Thm.no_attributes)
haftmann@18377
   634
       [("prop_def", Logic.mk_equals (Logic.protect A, A))] |> snd
haftmann@18377
   635
  |> add_thmss [(("nothing", []), [])] |> snd
berghofe@11516
   636
  |> Theory.add_axioms_i Proofterm.equality_axms
wenzelm@16493
   637
  |> Theory.end_theory;
wenzelm@3987
   638
wenzelm@5091
   639
structure ProtoPure =
wenzelm@5091
   640
struct
wenzelm@5091
   641
  val thy = proto_pure;
wenzelm@18031
   642
  val prop_def = get_axiom thy "prop_def";
wenzelm@5091
   643
end;
wenzelm@3987
   644
wenzelm@3987
   645
end;
wenzelm@3987
   646
wenzelm@4022
   647
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   648
open BasicPureThy;