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