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