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