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