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