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