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