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