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