src/Pure/pure_thy.ML
author wenzelm
Thu Jun 09 12:03:23 2005 +0200 (2005-06-09)
changeset 16336 e3892698c57d
parent 16132 afd2d32c7d94
child 16441 92a8a25e53c5
permissions -rw-r--r--
thms_of no longer global;
added all_thms_of;
theorems: NameSpace.table;
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@5091
     5
Theorem database, derived theory operations, and the ProtoPure theory.
wenzelm@3987
     6
*)
wenzelm@3987
     7
wenzelm@4022
     8
signature BASIC_PURE_THY =
wenzelm@4022
     9
sig
berghofe@15456
    10
  type thmref
wenzelm@5000
    11
  val print_theorems: theory -> unit
wenzelm@5000
    12
  val print_theory: theory -> unit
berghofe@15456
    13
  val get_thm: theory -> thmref -> thm
berghofe@15456
    14
  val get_thms: theory -> thmref -> thm list
berghofe@15456
    15
  val get_thmss: theory -> thmref list -> thm list
wenzelm@5091
    16
  structure ProtoPure:
wenzelm@5091
    17
    sig
wenzelm@5091
    18
      val thy: theory
wenzelm@5091
    19
      val Goal_def: thm
wenzelm@5091
    20
    end
wenzelm@4853
    21
end;
wenzelm@4022
    22
wenzelm@3987
    23
signature PURE_THY =
wenzelm@3987
    24
sig
wenzelm@4022
    25
  include BASIC_PURE_THY
wenzelm@15703
    26
  datatype interval = FromTo of int * int | From of int | Single of int
wenzelm@16023
    27
  val string_of_thmref: thmref -> string
berghofe@15456
    28
  val get_thm_closure: theory -> thmref -> thm
berghofe@15456
    29
  val get_thms_closure: theory -> thmref -> thm list
wenzelm@9564
    30
  val single_thm: string -> thm list -> thm
berghofe@15456
    31
  val select_thm: thmref -> thm list -> thm list
wenzelm@16023
    32
  val selections: string * thm list -> (thmref * thm) list
wenzelm@16132
    33
  val extern_thm_sg: Sign.sg -> string -> xstring
wenzelm@16023
    34
  val fact_index_of: theory -> FactIndex.T
wenzelm@16023
    35
  val valid_thms: theory -> thmref * thm list -> bool
wenzelm@16023
    36
  val thms_containing: theory -> FactIndex.spec -> (string * thm list) list
nipkow@13646
    37
  val thms_containing_consts: theory -> string list -> (string * thm) list
wenzelm@16336
    38
  val thms_of: theory -> (string * thm) list
wenzelm@16336
    39
  val all_thms_of: theory -> (string * thm) list
wenzelm@12695
    40
  val hide_thms: bool -> string list -> theory -> theory
wenzelm@6091
    41
  val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm
wenzelm@7405
    42
  val smart_store_thms: (bstring * thm list) -> thm list
wenzelm@12138
    43
  val smart_store_thms_open: (bstring * thm list) -> thm list
wenzelm@7899
    44
  val forall_elim_var: int -> thm -> thm
wenzelm@7899
    45
  val forall_elim_vars: int -> thm -> thm
wenzelm@8419
    46
  val add_thms: ((bstring * thm) * theory attribute list) list -> theory -> theory * thm list
wenzelm@12711
    47
  val add_thmss: ((bstring * thm list) * theory attribute list) list -> theory
wenzelm@12711
    48
    -> theory * thm list list
wenzelm@16132
    49
  val note_thmss: theory attribute -> ((bstring * theory attribute list) *
wenzelm@16132
    50
    (thmref * theory attribute list) list) list -> theory -> theory * (bstring * thm list) list
wenzelm@16132
    51
  val note_thmss_i: theory attribute -> ((bstring * theory attribute list) *
wenzelm@16132
    52
    (thm list * theory attribute list) list) list -> theory -> theory * (bstring * thm list) list
wenzelm@8419
    53
  val add_axioms: ((bstring * string) * theory attribute list) list -> theory -> theory * thm list
wenzelm@8419
    54
  val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory * thm list
wenzelm@12711
    55
  val add_axiomss: ((bstring * string list) * theory attribute list) list -> theory
wenzelm@12711
    56
    -> theory * thm list list
wenzelm@12711
    57
  val add_axiomss_i: ((bstring * term list) * theory attribute list) list -> theory
wenzelm@12711
    58
    -> theory * thm list list
wenzelm@9318
    59
  val add_defs: bool -> ((bstring * string) * theory attribute list) list
wenzelm@9318
    60
    -> theory -> theory * thm list
wenzelm@9318
    61
  val add_defs_i: bool -> ((bstring * term) * theory attribute list) list
wenzelm@9318
    62
    -> theory -> theory * thm list
wenzelm@9318
    63
  val add_defss: bool -> ((bstring * string list) * theory attribute list) list
wenzelm@9318
    64
    -> theory -> theory * thm list list
wenzelm@9318
    65
  val add_defss_i: bool -> ((bstring * term list) * theory attribute list) list
wenzelm@9318
    66
    -> theory -> theory * thm list list
wenzelm@4963
    67
  val get_name: theory -> string
wenzelm@4963
    68
  val put_name: string -> theory -> theory
wenzelm@4963
    69
  val global_path: theory -> theory
wenzelm@4963
    70
  val local_path: theory -> theory
wenzelm@4963
    71
  val begin_theory: string -> theory list -> theory
wenzelm@4963
    72
  val end_theory: theory -> theory
wenzelm@6682
    73
  val checkpoint: theory -> theory
wenzelm@4922
    74
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
wenzelm@3987
    75
end;
wenzelm@3987
    76
wenzelm@3987
    77
structure PureThy: PURE_THY =
wenzelm@3987
    78
struct
wenzelm@3987
    79
wenzelm@3987
    80
wenzelm@4922
    81
(*** theorem database ***)
wenzelm@3987
    82
wenzelm@4922
    83
(** data kind 'Pure/theorems' **)
wenzelm@3987
    84
wenzelm@5005
    85
structure TheoremsDataArgs =
wenzelm@5005
    86
struct
wenzelm@5005
    87
  val name = "Pure/theorems";
wenzelm@3987
    88
wenzelm@5005
    89
  type T =
wenzelm@16336
    90
    {theorems: thm list NameSpace.table,
wenzelm@13274
    91
      index: FactIndex.T} ref;
wenzelm@3987
    92
wenzelm@4853
    93
  fun mk_empty _ =
wenzelm@16336
    94
    ref {theorems = NameSpace.empty_table, index = FactIndex.empty}: T;
wenzelm@3987
    95
wenzelm@5005
    96
  val empty = mk_empty ();
wenzelm@6547
    97
  fun copy (ref x) = ref x;
wenzelm@5005
    98
  val prep_ext = mk_empty;
wenzelm@5005
    99
  val merge = mk_empty;
wenzelm@5005
   100
wenzelm@16336
   101
  fun pretty sg (ref {theorems, index = _}) =
wenzelm@4853
   102
    let
wenzelm@10008
   103
      val prt_thm = Display.pretty_thm_sg sg;
wenzelm@4853
   104
      fun prt_thms (name, [th]) =
wenzelm@4853
   105
            Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
wenzelm@4853
   106
        | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
wenzelm@3987
   107
wenzelm@16336
   108
      val thmss = NameSpace.extern_table theorems;
wenzelm@9215
   109
    in Pretty.big_list "theorems:" (map prt_thms thmss) end;
wenzelm@8720
   110
wenzelm@9215
   111
  fun print sg data = Pretty.writeln (pretty sg data);
wenzelm@3987
   112
end;
wenzelm@3987
   113
wenzelm@5005
   114
structure TheoremsData = TheoryDataFun(TheoremsDataArgs);
wenzelm@5005
   115
val get_theorems_sg = TheoremsData.get_sg;
wenzelm@5005
   116
val get_theorems = TheoremsData.get;
wenzelm@5005
   117
wenzelm@16336
   118
val extern_thm_sg = NameSpace.extern o #1 o #theorems o ! o get_theorems_sg;
wenzelm@16023
   119
val fact_index_of = #index o ! o get_theorems;
wenzelm@16023
   120
wenzelm@6367
   121
wenzelm@3987
   122
wenzelm@5000
   123
(* print theory *)
wenzelm@3987
   124
wenzelm@5005
   125
val print_theorems = TheoremsData.print;
wenzelm@8720
   126
wenzelm@5000
   127
fun print_theory thy =
wenzelm@9215
   128
  Display.pretty_full_theory thy @
wenzelm@9215
   129
  [TheoremsDataArgs.pretty (Theory.sign_of thy) (get_theorems thy)]
wenzelm@8720
   130
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@4022
   131
wenzelm@3987
   132
wenzelm@3987
   133
wenzelm@4022
   134
(** retrieve theorems **)
wenzelm@3987
   135
skalberg@15531
   136
fun the_thms _ (SOME thms) = thms
skalberg@15531
   137
  | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
wenzelm@4037
   138
wenzelm@9564
   139
fun single_thm _ [thm] = thm
wenzelm@9564
   140
  | single_thm name _ = error ("Single theorem expected " ^ quote name);
wenzelm@9564
   141
wenzelm@15703
   142
wenzelm@16023
   143
(* datatype interval *)
wenzelm@15703
   144
wenzelm@15703
   145
datatype interval =
wenzelm@15703
   146
  FromTo of int * int |
wenzelm@15703
   147
  From of int |
wenzelm@15703
   148
  Single of int;
wenzelm@15703
   149
wenzelm@15703
   150
fun interval _ (FromTo (i, j)) = i upto j
wenzelm@15703
   151
  | interval n (From i) = i upto n
wenzelm@15703
   152
  | interval _ (Single i) = [i];
wenzelm@15703
   153
wenzelm@16023
   154
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@16023
   155
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@16023
   156
  | string_of_interval (Single i) = string_of_int i;
wenzelm@16023
   157
wenzelm@16023
   158
wenzelm@16023
   159
(* type thmref *)
wenzelm@15703
   160
wenzelm@16023
   161
type thmref = xstring * interval list option;
wenzelm@16023
   162
wenzelm@16023
   163
fun string_of_thmref (name, NONE) = name
wenzelm@16023
   164
  | string_of_thmref (name, SOME is) =
wenzelm@16023
   165
      name ^ enclose "(" ")" (commas (map string_of_interval is));
wenzelm@16023
   166
wenzelm@16023
   167
wenzelm@16023
   168
(* select_thm *)
wenzelm@15703
   169
wenzelm@15703
   170
fun select_thm (_, NONE) thms = thms
wenzelm@15703
   171
  | select_thm (name, SOME is) thms =
wenzelm@16023
   172
      let
wenzelm@16023
   173
        val n = length thms;
wenzelm@16023
   174
        fun select i =
wenzelm@16023
   175
          if i < 1 orelse i > n then
wenzelm@16023
   176
            error ("Bad subscript " ^ string_of_int i ^ " for " ^
wenzelm@16023
   177
              quote name ^ " (length " ^ string_of_int n ^ ")")
wenzelm@16023
   178
          else List.nth (thms, i - 1);
wenzelm@16023
   179
      in map select (List.concat (map (interval n) is)) end;
wenzelm@15703
   180
wenzelm@16023
   181
wenzelm@16023
   182
(* selections *)
wenzelm@16023
   183
wenzelm@16023
   184
fun selections (name, [thm]) = [((name, NONE), thm)]
wenzelm@16023
   185
  | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) =>
wenzelm@16023
   186
      ((name, SOME [Single i]), thm));
berghofe@15456
   187
wenzelm@9564
   188
wenzelm@9808
   189
(* get_thm(s)_closure -- statically scoped versions *)
wenzelm@9564
   190
wenzelm@9564
   191
(*beware of proper order of evaluation!*)
wenzelm@4922
   192
wenzelm@9564
   193
fun lookup_thms thy =
wenzelm@9564
   194
  let
wenzelm@9564
   195
    val sg_ref = Sign.self_ref (Theory.sign_of thy);
wenzelm@16336
   196
    val ref {theorems = (space, thms), ...} = get_theorems thy;
wenzelm@9564
   197
  in
wenzelm@9564
   198
    fn name =>
skalberg@15570
   199
      Option.map (map (Thm.transfer_sg (Sign.deref sg_ref)))        (*semi-dynamic identity*)
wenzelm@16336
   200
      (Symtab.lookup (thms, NameSpace.intern space name))           (*static content*)
wenzelm@9564
   201
  end;
wenzelm@3987
   202
wenzelm@9564
   203
fun get_thms_closure thy =
wenzelm@9564
   204
  let val closures = map lookup_thms (thy :: Theory.ancestors_of thy)
berghofe@15456
   205
  in fn namei as (name, _) => select_thm namei
berghofe@15456
   206
    (the_thms name (get_first (fn f => f name) closures))
berghofe@15456
   207
  end;
wenzelm@9564
   208
wenzelm@9808
   209
fun get_thm_closure thy =
wenzelm@9808
   210
  let val get = get_thms_closure thy
berghofe@15456
   211
  in fn namei as (name, _) => single_thm name (get namei) end;
wenzelm@9808
   212
wenzelm@9564
   213
wenzelm@9564
   214
(* get_thm etc. *)
wenzelm@9564
   215
berghofe@15456
   216
fun get_thms theory (namei as (name, _)) =
wenzelm@9564
   217
  get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory)
berghofe@15456
   218
  |> the_thms name |> select_thm namei |> map (Thm.transfer theory);
wenzelm@4022
   219
skalberg@15570
   220
fun get_thmss thy names = List.concat (map (get_thms thy) names);
berghofe@15456
   221
fun get_thm thy (namei as (name, _)) = single_thm name (get_thms thy namei);
wenzelm@4783
   222
wenzelm@4022
   223
wenzelm@16023
   224
(* thms_containing etc. *)
wenzelm@4022
   225
wenzelm@16023
   226
fun valid_thms thy (thmref, ths) =
wenzelm@16023
   227
  (case try (transform_error (get_thms thy)) thmref of
wenzelm@16023
   228
    NONE => false
wenzelm@16132
   229
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@3987
   230
wenzelm@16023
   231
fun thms_containing theory spec =
wenzelm@16023
   232
  (theory :: Theory.ancestors_of theory)
wenzelm@16023
   233
  |> map (fn thy =>
wenzelm@16023
   234
      FactIndex.find (fact_index_of thy) spec
wenzelm@16023
   235
      |> List.filter (fn (name, ths) => valid_thms theory ((name, NONE), ths))
wenzelm@16023
   236
      |> gen_distinct eq_fst)
wenzelm@16023
   237
  |> List.concat;
wenzelm@4022
   238
nipkow@13646
   239
fun thms_containing_consts thy consts =
skalberg@15570
   240
  thms_containing thy (consts, []) |> map #2 |> List.concat
wenzelm@16023
   241
  |> map (fn th => (Thm.name_of_thm th, th));
nipkow@13646
   242
kleing@15882
   243
wenzelm@16336
   244
(* thms_of etc. *)
kleing@15882
   245
wenzelm@16023
   246
fun thms_of thy =
wenzelm@16336
   247
  let val ref {theorems = (_, thms), ...} = get_theorems thy in
wenzelm@16336
   248
    map (fn th => (Thm.name_of_thm th, th)) (List.concat (map snd (Symtab.dest thms)))
kleing@15882
   249
  end;
wenzelm@15703
   250
wenzelm@16336
   251
fun all_thms_of thy = List.concat (map thms_of (thy :: Theory.ancestors_of thy));
wenzelm@16336
   252
wenzelm@4022
   253
wenzelm@4022
   254
wenzelm@4022
   255
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   256
wenzelm@12695
   257
(* hiding -- affects current theory node only! *)
wenzelm@12695
   258
wenzelm@13424
   259
fun hide_thms fully names thy =
wenzelm@12695
   260
  let
wenzelm@16336
   261
    val r as ref {theorems = (space, thms), index} = get_theorems thy;
wenzelm@16132
   262
    val space' = fold (NameSpace.hide fully) names space;
wenzelm@16336
   263
  in r := {theorems = (space', thms), index = index}; thy end;
wenzelm@12695
   264
wenzelm@12695
   265
wenzelm@4853
   266
(* naming *)
wenzelm@4853
   267
berghofe@11998
   268
fun gen_names j len name =
berghofe@11998
   269
  map (fn i => name ^ "_" ^ string_of_int i) (j+1 upto j+len);
wenzelm@4853
   270
berghofe@11998
   271
fun name_multi name xs = gen_names 0 (length xs) name ~~ xs;
berghofe@12235
   272
berghofe@12872
   273
fun name_thm pre (p as (_, thm)) =
berghofe@12872
   274
  if Thm.name_of_thm thm <> "" andalso pre then thm else Thm.name_thm p;
berghofe@12872
   275
berghofe@12872
   276
fun name_thms pre name [x] = [name_thm pre (name, x)]
berghofe@12872
   277
  | name_thms pre name xs = map (name_thm pre) (name_multi name xs);
berghofe@12235
   278
berghofe@12235
   279
fun name_thmss name xs = (case filter_out (null o fst) xs of
berghofe@12872
   280
    [([x], z)] => [([name_thm true (name, x)], z)]
berghofe@12235
   281
  | _ => snd (foldl_map (fn (i, (ys, z)) => (i + length ys,
berghofe@12872
   282
  (map (name_thm true) (gen_names i (length ys) name ~~ ys), z))) (0, xs)));
wenzelm@4853
   283
wenzelm@4853
   284
berghofe@11998
   285
(* enter_thms *)
wenzelm@4853
   286
wenzelm@7470
   287
fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@7470
   288
fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   289
wenzelm@16132
   290
fun enter_thms _ _ _ app_att thy ("", thms) = app_att (thy, thms)
wenzelm@16132
   291
  | enter_thms sg pre_name post_name app_att thy (bname, thms) =
wenzelm@7470
   292
      let
wenzelm@16132
   293
        val name = Sign.full_name sg bname;
berghofe@11998
   294
        val (thy', thms') = app_att (thy, pre_name name thms);
berghofe@11998
   295
        val named_thms = post_name name thms';
wenzelm@3987
   296
wenzelm@16336
   297
        val r as ref {theorems = (space, theorems), index} = get_theorems_sg sg;
wenzelm@16132
   298
        val space' = Sign.declare_name sg name space;
wenzelm@16023
   299
        val theorems' = Symtab.update ((name, named_thms), theorems);
wenzelm@16023
   300
        val index' = FactIndex.add (K false) (name, named_thms) index;
wenzelm@13274
   301
      in
wenzelm@16023
   302
        (case Symtab.lookup (theorems, name) of
skalberg@15531
   303
          NONE => ()
skalberg@15531
   304
        | SOME thms' =>
wenzelm@16132
   305
            if Thm.eq_thms (thms', named_thms) then warn_same name
wenzelm@13274
   306
            else warn_overwrite name);
wenzelm@16336
   307
        r := {theorems = (space', theorems'), index = index'};
berghofe@11998
   308
        (thy', named_thms)
berghofe@11998
   309
      end;
wenzelm@3987
   310
wenzelm@16023
   311
wenzelm@6091
   312
(* add_thms(s) *)
wenzelm@4853
   313
berghofe@12235
   314
fun add_thms_atts pre_name ((bname, thms), atts) thy =
berghofe@12872
   315
  enter_thms (Theory.sign_of thy) pre_name (name_thms false)
berghofe@11998
   316
    (Thm.applys_attributes o rpair atts) thy (bname, thms);
wenzelm@4853
   317
berghofe@12235
   318
fun gen_add_thmss pre_name args theory =
berghofe@12235
   319
  foldl_map (fn (thy, arg) => add_thms_atts pre_name arg thy) (theory, args);
wenzelm@5907
   320
berghofe@12235
   321
fun gen_add_thms pre_name args =
berghofe@12235
   322
  apsnd (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   323
berghofe@12872
   324
val add_thmss = gen_add_thmss (name_thms true);
berghofe@12872
   325
val add_thms = gen_add_thms (name_thms true);
wenzelm@5907
   326
wenzelm@5907
   327
wenzelm@14564
   328
(* note_thmss(_i) *)
wenzelm@5907
   329
wenzelm@9192
   330
local
wenzelm@12711
   331
wenzelm@16132
   332
fun gen_note_thss get kind_att (thy, ((bname, more_atts), ths_atts)) =
wenzelm@12711
   333
  let
wenzelm@12711
   334
    fun app (x, (ths, atts)) = Thm.applys_attributes ((x, ths), atts);
wenzelm@16132
   335
    val (thy', thms) = enter_thms (Theory.sign_of thy)
skalberg@15570
   336
      name_thmss (name_thms false) (apsnd List.concat o foldl_map app) thy
wenzelm@12711
   337
      (bname, map (fn (ths, atts) => (get thy ths, atts @ more_atts @ [kind_att])) ths_atts);
wenzelm@12711
   338
  in (thy', (bname, thms)) end;
wenzelm@12711
   339
wenzelm@16132
   340
fun gen_note_thmss get kind_att args thy =
wenzelm@16132
   341
  foldl_map (gen_note_thss get kind_att) (thy, args);
wenzelm@12711
   342
wenzelm@9192
   343
in
wenzelm@12711
   344
wenzelm@16132
   345
val note_thmss = gen_note_thmss get_thms;
wenzelm@16132
   346
val note_thmss_i = gen_note_thmss (K I);
wenzelm@12711
   347
wenzelm@9192
   348
end;
wenzelm@5280
   349
wenzelm@5280
   350
wenzelm@6091
   351
(* store_thm *)
wenzelm@5280
   352
berghofe@11998
   353
fun store_thm ((bname, thm), atts) thy =
berghofe@12872
   354
  let val (thy', [th']) = add_thms_atts (name_thms true) ((bname, [thm]), atts) thy
wenzelm@5280
   355
  in (thy', th') end;
wenzelm@3987
   356
wenzelm@3987
   357
wenzelm@7405
   358
(* smart_store_thms *)
wenzelm@3987
   359
berghofe@12235
   360
fun gen_smart_store_thms _ (name, []) =
berghofe@11516
   361
      error ("Cannot store empty list of theorems: " ^ quote name)
berghofe@12235
   362
  | gen_smart_store_thms name_thm (name, [thm]) =
berghofe@12872
   363
      snd (enter_thms (Thm.sign_of_thm thm) (name_thm true) (name_thm false)
berghofe@12872
   364
        I () (name, [thm]))
berghofe@12235
   365
  | gen_smart_store_thms name_thm (name, thms) =
wenzelm@7405
   366
      let
wenzelm@7405
   367
        val merge_sg = Sign.merge_refs o apsnd (Sign.self_ref o Thm.sign_of_thm);
skalberg@15570
   368
        val sg_ref = Library.foldl merge_sg (Sign.self_ref (Thm.sign_of_thm (hd thms)), tl thms);
berghofe@12872
   369
      in snd (enter_thms (Sign.deref sg_ref) (name_thm true) (name_thm false)
berghofe@12872
   370
        I () (name, thms))
berghofe@12872
   371
      end;
berghofe@11516
   372
berghofe@12235
   373
val smart_store_thms = gen_smart_store_thms name_thms;
berghofe@12872
   374
val smart_store_thms_open = gen_smart_store_thms (K (K I));
wenzelm@3987
   375
wenzelm@3987
   376
wenzelm@7899
   377
(* forall_elim_vars (belongs to drule.ML) *)
wenzelm@7899
   378
berghofe@13713
   379
(*Replace outermost quantified variable by Var of given index.*)
wenzelm@7899
   380
fun forall_elim_var i th =
wenzelm@7899
   381
    let val {prop,sign,...} = rep_thm th
wenzelm@7899
   382
    in case prop of
berghofe@13713
   383
        Const ("all", _) $ Abs (a, T, _) =>
berghofe@13713
   384
          let val used = map (fst o fst)
skalberg@15570
   385
            (List.filter (equal i o snd o fst) (Term.add_vars ([], prop)))
berghofe@13713
   386
          in forall_elim (cterm_of sign (Var ((variant used a, i), T))) th end
berghofe@13713
   387
      | _ => raise THM ("forall_elim_var", i, [th])
wenzelm@7899
   388
    end;
wenzelm@7899
   389
wenzelm@7899
   390
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@7899
   391
fun forall_elim_vars i th =
wenzelm@7899
   392
    forall_elim_vars i (forall_elim_var i th)
wenzelm@7899
   393
        handle THM _ => th;
wenzelm@7899
   394
wenzelm@7899
   395
wenzelm@4022
   396
(* store axioms as theorems *)
wenzelm@4022
   397
wenzelm@4853
   398
local
wenzelm@7899
   399
  fun get_axs thy named_axs =
wenzelm@7899
   400
    map (forall_elim_vars 0 o Thm.get_axiom thy o fst) named_axs;
wenzelm@7753
   401
wenzelm@8419
   402
  fun add_single add (thy, ((name, ax), atts)) =
wenzelm@4853
   403
    let
berghofe@11998
   404
      val named_ax = [(name, ax)];
wenzelm@7753
   405
      val thy' = add named_ax thy;
wenzelm@7753
   406
      val thm = hd (get_axs thy' named_ax);
berghofe@12235
   407
    in apsnd hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
wenzelm@7753
   408
wenzelm@8419
   409
  fun add_multi add (thy, ((name, axs), atts)) =
wenzelm@7753
   410
    let
wenzelm@7753
   411
      val named_axs = name_multi name axs;
wenzelm@4853
   412
      val thy' = add named_axs thy;
wenzelm@7753
   413
      val thms = get_axs thy' named_axs;
berghofe@12235
   414
    in apsnd hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
wenzelm@4022
   415
wenzelm@8419
   416
  fun add_singles add args thy = foldl_map (add_single add) (thy, args);
wenzelm@8419
   417
  fun add_multis add args thy = foldl_map (add_multi add) (thy, args);
wenzelm@4853
   418
in
wenzelm@7753
   419
  val add_axioms    = add_singles Theory.add_axioms;
wenzelm@7753
   420
  val add_axioms_i  = add_singles Theory.add_axioms_i;
wenzelm@7753
   421
  val add_axiomss   = add_multis Theory.add_axioms;
wenzelm@7753
   422
  val add_axiomss_i = add_multis Theory.add_axioms_i;
wenzelm@9318
   423
  val add_defs      = add_singles o Theory.add_defs;
wenzelm@9318
   424
  val add_defs_i    = add_singles o Theory.add_defs_i;
wenzelm@9318
   425
  val add_defss     = add_multis o Theory.add_defs;
wenzelm@9318
   426
  val add_defss_i   = add_multis o Theory.add_defs_i;
wenzelm@4853
   427
end;
wenzelm@4022
   428
wenzelm@4022
   429
wenzelm@3987
   430
wenzelm@4963
   431
(*** derived theory operations ***)
wenzelm@4963
   432
wenzelm@4963
   433
(** theory management **)
wenzelm@4963
   434
wenzelm@5005
   435
(* data kind 'Pure/theory_management' *)
wenzelm@4963
   436
wenzelm@5005
   437
structure TheoryManagementDataArgs =
wenzelm@5005
   438
struct
wenzelm@5005
   439
  val name = "Pure/theory_management";
wenzelm@6660
   440
  type T = {name: string, version: int};
wenzelm@5000
   441
wenzelm@6660
   442
  val empty = {name = "", version = 0};
wenzelm@6547
   443
  val copy = I;
wenzelm@5005
   444
  val prep_ext  = I;
wenzelm@5000
   445
  fun merge _ = empty;
wenzelm@5005
   446
  fun print _ _ = ();
wenzelm@4963
   447
end;
wenzelm@4963
   448
wenzelm@5005
   449
structure TheoryManagementData = TheoryDataFun(TheoryManagementDataArgs);
wenzelm@5005
   450
val get_info = TheoryManagementData.get;
wenzelm@5005
   451
val put_info = TheoryManagementData.put;
wenzelm@5005
   452
wenzelm@4963
   453
wenzelm@4963
   454
(* get / put name *)
wenzelm@4963
   455
wenzelm@5000
   456
val get_name = #name o get_info;
wenzelm@6660
   457
fun put_name name = put_info {name = name, version = 0};
wenzelm@4963
   458
wenzelm@4963
   459
wenzelm@4963
   460
(* control prefixing of theory name *)
wenzelm@4963
   461
wenzelm@5210
   462
val global_path = Theory.root_path;
wenzelm@4963
   463
wenzelm@4963
   464
fun local_path thy =
wenzelm@5210
   465
  thy |> Theory.root_path |> Theory.add_path (get_name thy);
wenzelm@4963
   466
wenzelm@4963
   467
wenzelm@4963
   468
(* begin / end theory *)
wenzelm@4963
   469
wenzelm@4963
   470
fun begin_theory name thys =
wenzelm@4963
   471
  Theory.prep_ext_merge thys
wenzelm@4963
   472
  |> put_name name
wenzelm@4963
   473
  |> local_path;
wenzelm@4963
   474
wenzelm@12123
   475
fun end_theory thy =
wenzelm@12123
   476
  thy
wenzelm@12123
   477
  |> Theory.add_name (get_name thy);
wenzelm@4963
   478
wenzelm@6682
   479
fun checkpoint thy =
wenzelm@6682
   480
  if is_draft thy then
wenzelm@6682
   481
    let val {name, version} = get_info thy in
wenzelm@6682
   482
      thy
wenzelm@6682
   483
      |> Theory.add_name (name ^ ":" ^ string_of_int version)
wenzelm@6682
   484
      |> put_info {name = name, version = version + 1}
wenzelm@6682
   485
    end
wenzelm@6682
   486
  else thy;
wenzelm@5000
   487
wenzelm@5000
   488
wenzelm@4963
   489
wenzelm@4963
   490
(** add logical types **)
wenzelm@4922
   491
wenzelm@4922
   492
fun add_typedecls decls thy =
wenzelm@4922
   493
  let
wenzelm@4922
   494
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@4922
   495
wenzelm@4922
   496
    fun type_of (raw_name, vs, mx) =
wenzelm@4922
   497
      if null (duplicates vs) then (raw_name, length vs, mx)
wenzelm@4922
   498
      else error ("Duplicate parameters in type declaration: " ^ quote raw_name);
wenzelm@14854
   499
  in thy |> Theory.add_types (map type_of decls) end;
wenzelm@4922
   500
wenzelm@4922
   501
wenzelm@4922
   502
wenzelm@5091
   503
(*** the ProtoPure theory ***)
wenzelm@3987
   504
wenzelm@3987
   505
val proto_pure =
wenzelm@3987
   506
  Theory.pre_pure
wenzelm@16023
   507
  |> TheoryManagementData.init |> put_name "ProtoPure"
wenzelm@16023
   508
  |> TheoremsData.init
wenzelm@16023
   509
  |> Proofterm.init
wenzelm@4963
   510
  |> global_path
wenzelm@3987
   511
  |> Theory.add_types
wenzelm@4922
   512
   [("fun", 2, NoSyn),
wenzelm@4922
   513
    ("prop", 0, NoSyn),
wenzelm@4922
   514
    ("itself", 1, NoSyn),
wenzelm@4922
   515
    ("dummy", 0, NoSyn)]
wenzelm@4922
   516
  |> Theory.add_nonterminals Syntax.pure_nonterms
wenzelm@3987
   517
  |> Theory.add_syntax Syntax.pure_syntax
wenzelm@15801
   518
  |> Theory.add_syntax Syntax.pure_appl_syntax
wenzelm@6692
   519
  |> Theory.add_modesyntax (Symbol.xsymbolsN, true) Syntax.pure_xsym_syntax
wenzelm@3987
   520
  |> Theory.add_syntax
wenzelm@7949
   521
   [("==>", "[prop, prop] => prop", Delimfix "op ==>"),
wenzelm@9534
   522
    (Term.dummy_patternN, "aprop", Delimfix "'_")]
wenzelm@3987
   523
  |> Theory.add_consts
wenzelm@14854
   524
   [("==", "['a, 'a] => prop", InfixrName ("==", 2)),
wenzelm@3987
   525
    ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@3987
   526
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@10667
   527
    ("Goal", "prop => prop", NoSyn),
wenzelm@6547
   528
    ("TYPE", "'a itself", NoSyn),
wenzelm@9534
   529
    (Term.dummy_patternN, "'a", Delimfix "'_")]
skalberg@14223
   530
  |> Theory.add_finals_i false
wenzelm@14854
   531
    [Const("==", [TFree ("'a", []), TFree ("'a", [])] ---> propT),
wenzelm@14854
   532
     Const("==>", [propT, propT] ---> propT),
wenzelm@14854
   533
     Const("all", (TFree("'a", []) --> propT) --> propT),
wenzelm@14854
   534
     Const("TYPE", a_itselfT)]
nipkow@5041
   535
  |> Theory.add_modesyntax ("", false)
wenzelm@12138
   536
    (Syntax.pure_syntax_output @ Syntax.pure_appl_syntax)
wenzelm@12250
   537
  |> Theory.add_trfuns Syntax.pure_trfuns
wenzelm@12250
   538
  |> Theory.add_trfunsT Syntax.pure_trfunsT
wenzelm@4963
   539
  |> local_path
wenzelm@10667
   540
  |> (#1 oo (add_defs_i false o map Thm.no_attributes))
wenzelm@10667
   541
   [("Goal_def", let val A = Free ("A", propT) in Logic.mk_equals (Logic.mk_goal A, A) end)]
wenzelm@9238
   542
  |> (#1 o add_thmss [(("nothing", []), [])])
berghofe@11516
   543
  |> Theory.add_axioms_i Proofterm.equality_axms
wenzelm@4963
   544
  |> end_theory;
wenzelm@3987
   545
wenzelm@5091
   546
structure ProtoPure =
wenzelm@5091
   547
struct
wenzelm@5091
   548
  val thy = proto_pure;
wenzelm@5091
   549
  val Goal_def = get_axiom thy "Goal_def";
wenzelm@5091
   550
end;
wenzelm@3987
   551
wenzelm@3987
   552
end;
wenzelm@3987
   553
wenzelm@4022
   554
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   555
open BasicPureThy;