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