src/Pure/pure_thy.ML
author kleing
Fri Apr 29 11:22:41 2005 +0200 (2005-04-29)
changeset 15882 a191d2bee3e1
parent 15801 d2f5ca3c048d
child 15884 89124b6752e5
permissions -rw-r--r--
new thms_containing that searches for patterns instead of constants
(by Rafal Kolanski, NICTA)
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
kleing@15882
    17
  val thms_with_names_of: theory -> (string * thm list) list
wenzelm@5091
    18
  structure ProtoPure:
wenzelm@5091
    19
    sig
wenzelm@5091
    20
      val thy: theory
wenzelm@5091
    21
      val Goal_def: thm
wenzelm@5091
    22
    end
wenzelm@4853
    23
end;
wenzelm@4022
    24
wenzelm@3987
    25
signature PURE_THY =
wenzelm@3987
    26
sig
wenzelm@4022
    27
  include BASIC_PURE_THY
wenzelm@15703
    28
  datatype interval = FromTo of int * int | From of int | Single of int
berghofe@15456
    29
  val get_thm_closure: theory -> thmref -> thm
berghofe@15456
    30
  val get_thms_closure: theory -> thmref -> thm list
wenzelm@9564
    31
  val single_thm: string -> thm list -> thm
berghofe@15456
    32
  val select_thm: thmref -> thm list -> thm list
wenzelm@6367
    33
  val cond_extern_thm_sg: Sign.sg -> string -> xstring
wenzelm@13274
    34
  val thms_containing: theory -> string list * string list -> (string * thm list) list
kleing@15882
    35
  val find_theorems: Sign.sg -> (string * thm list) list -> string list -> (string * thm list) list 
nipkow@13646
    36
  val thms_containing_consts: theory -> string list -> (string * thm) list
nipkow@15387
    37
  val find_matching_thms: (thm -> thm list) * (term -> term)
nipkow@15387
    38
        -> theory -> term -> (string * thm) list
nipkow@13646
    39
  val find_intros: theory -> term -> (string * thm) list
berghofe@13800
    40
  val find_intros_goal : theory -> thm -> int -> (string * thm) list
nipkow@13646
    41
  val find_elims : theory -> term -> (string * thm) list
wenzelm@12695
    42
  val hide_thms: bool -> string list -> theory -> theory
wenzelm@6091
    43
  val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm
wenzelm@7405
    44
  val smart_store_thms: (bstring * thm list) -> thm list
wenzelm@12138
    45
  val smart_store_thms_open: (bstring * thm list) -> thm list
wenzelm@7899
    46
  val forall_elim_var: int -> thm -> thm
wenzelm@7899
    47
  val forall_elim_vars: int -> thm -> thm
wenzelm@8419
    48
  val add_thms: ((bstring * thm) * theory attribute list) list -> theory -> theory * thm list
wenzelm@12711
    49
  val add_thmss: ((bstring * thm list) * theory attribute list) list -> theory
wenzelm@12711
    50
    -> theory * thm list list
ballarin@15624
    51
  val note_thmss:
ballarin@15624
    52
    theory attribute -> ((bstring * theory attribute list) *
ballarin@15624
    53
    (thmref * theory attribute list) list) list -> theory ->
ballarin@15624
    54
    theory * (bstring * thm list) list
ballarin@15624
    55
  val note_thmss_i:
ballarin@15624
    56
    theory attribute -> ((bstring * theory attribute list) *
ballarin@15624
    57
    (thm list * theory attribute list) list) list -> theory ->
ballarin@15624
    58
    theory * (bstring * thm list) list
ballarin@15696
    59
  val note_thmss_accesses:
ballarin@15624
    60
    (string -> string list) ->
ballarin@15624
    61
    theory attribute -> ((bstring * theory attribute list) *
ballarin@15624
    62
    (thmref * theory attribute list) list) list -> theory ->
ballarin@15624
    63
    theory * (bstring * thm list) list
ballarin@15696
    64
  val note_thmss_accesses_i:
ballarin@15624
    65
    (string -> string list) ->
ballarin@15624
    66
    theory attribute -> ((bstring * theory attribute list) *
ballarin@15624
    67
    (thm list * theory attribute list) list) list -> theory ->
ballarin@15624
    68
    theory * (bstring * thm list) list
wenzelm@8419
    69
  val add_axioms: ((bstring * string) * theory attribute list) list -> theory -> theory * thm list
wenzelm@8419
    70
  val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory * thm list
wenzelm@12711
    71
  val add_axiomss: ((bstring * string list) * theory attribute list) list -> theory
wenzelm@12711
    72
    -> theory * thm list list
wenzelm@12711
    73
  val add_axiomss_i: ((bstring * term list) * theory attribute list) list -> theory
wenzelm@12711
    74
    -> theory * thm list list
wenzelm@9318
    75
  val add_defs: bool -> ((bstring * string) * theory attribute list) list
wenzelm@9318
    76
    -> theory -> theory * thm list
wenzelm@9318
    77
  val add_defs_i: bool -> ((bstring * term) * theory attribute list) list
wenzelm@9318
    78
    -> theory -> theory * thm list
wenzelm@9318
    79
  val add_defss: bool -> ((bstring * string list) * theory attribute list) list
wenzelm@9318
    80
    -> theory -> theory * thm list list
wenzelm@9318
    81
  val add_defss_i: bool -> ((bstring * term list) * theory attribute list) list
wenzelm@9318
    82
    -> theory -> theory * thm list list
wenzelm@4963
    83
  val get_name: theory -> string
wenzelm@4963
    84
  val put_name: string -> theory -> theory
wenzelm@4963
    85
  val global_path: theory -> theory
wenzelm@4963
    86
  val local_path: theory -> theory
wenzelm@4963
    87
  val begin_theory: string -> theory list -> theory
wenzelm@4963
    88
  val end_theory: theory -> theory
wenzelm@6682
    89
  val checkpoint: theory -> theory
wenzelm@4922
    90
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
wenzelm@3987
    91
end;
wenzelm@3987
    92
wenzelm@3987
    93
structure PureThy: PURE_THY =
wenzelm@3987
    94
struct
wenzelm@3987
    95
wenzelm@3987
    96
wenzelm@4922
    97
(*** theorem database ***)
wenzelm@3987
    98
wenzelm@4922
    99
(** data kind 'Pure/theorems' **)
wenzelm@3987
   100
wenzelm@5005
   101
structure TheoremsDataArgs =
wenzelm@5005
   102
struct
wenzelm@5005
   103
  val name = "Pure/theorems";
wenzelm@3987
   104
wenzelm@5005
   105
  type T =
wenzelm@5005
   106
    {space: NameSpace.T,
wenzelm@6091
   107
      thms_tab: thm list Symtab.table,
wenzelm@13274
   108
      index: FactIndex.T} ref;
wenzelm@3987
   109
wenzelm@4853
   110
  fun mk_empty _ =
wenzelm@13274
   111
    ref {space = NameSpace.empty, thms_tab = Symtab.empty, index = FactIndex.empty}: T;
wenzelm@3987
   112
wenzelm@5005
   113
  val empty = mk_empty ();
wenzelm@6547
   114
  fun copy (ref x) = ref x;
wenzelm@5005
   115
  val prep_ext = mk_empty;
wenzelm@5005
   116
  val merge = mk_empty;
wenzelm@5005
   117
wenzelm@13274
   118
  fun pretty sg (ref {space, thms_tab, index = _}) =
wenzelm@4853
   119
    let
wenzelm@10008
   120
      val prt_thm = Display.pretty_thm_sg sg;
wenzelm@4853
   121
      fun prt_thms (name, [th]) =
wenzelm@4853
   122
            Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
wenzelm@4853
   123
        | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
wenzelm@3987
   124
wenzelm@6846
   125
      val thmss = NameSpace.cond_extern_table space thms_tab;
wenzelm@9215
   126
    in Pretty.big_list "theorems:" (map prt_thms thmss) end;
wenzelm@8720
   127
wenzelm@9215
   128
  fun print sg data = Pretty.writeln (pretty sg data);
wenzelm@3987
   129
end;
wenzelm@3987
   130
wenzelm@5005
   131
structure TheoremsData = TheoryDataFun(TheoremsDataArgs);
wenzelm@5005
   132
val get_theorems_sg = TheoremsData.get_sg;
wenzelm@5005
   133
val get_theorems = TheoremsData.get;
wenzelm@5005
   134
wenzelm@6367
   135
val cond_extern_thm_sg = NameSpace.cond_extern o #space o ! o get_theorems_sg;
wenzelm@6367
   136
wenzelm@3987
   137
wenzelm@5000
   138
(* print theory *)
wenzelm@3987
   139
wenzelm@5005
   140
val print_theorems = TheoremsData.print;
wenzelm@8720
   141
wenzelm@5000
   142
fun print_theory thy =
wenzelm@9215
   143
  Display.pretty_full_theory thy @
wenzelm@9215
   144
  [TheoremsDataArgs.pretty (Theory.sign_of thy) (get_theorems thy)]
wenzelm@8720
   145
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@4022
   146
wenzelm@3987
   147
wenzelm@3987
   148
wenzelm@4022
   149
(** retrieve theorems **)
wenzelm@3987
   150
skalberg@15531
   151
fun the_thms _ (SOME thms) = thms
skalberg@15531
   152
  | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
wenzelm@4037
   153
wenzelm@9564
   154
fun single_thm _ [thm] = thm
wenzelm@9564
   155
  | single_thm name _ = error ("Single theorem expected " ^ quote name);
wenzelm@9564
   156
wenzelm@15703
   157
wenzelm@15703
   158
(* selections *)
wenzelm@15703
   159
wenzelm@15703
   160
datatype interval =
wenzelm@15703
   161
  FromTo of int * int |
wenzelm@15703
   162
  From of int |
wenzelm@15703
   163
  Single of int;
wenzelm@15703
   164
wenzelm@15703
   165
type thmref = xstring * interval list option;
wenzelm@15703
   166
wenzelm@15703
   167
local
wenzelm@15703
   168
wenzelm@15703
   169
fun interval _ (FromTo (i, j)) = i upto j
wenzelm@15703
   170
  | interval n (From i) = i upto n
wenzelm@15703
   171
  | interval _ (Single i) = [i];
wenzelm@15703
   172
wenzelm@15703
   173
fun select name thms n i =
wenzelm@15703
   174
  if i < 1 orelse i > n then
wenzelm@15703
   175
    error ("Bad subscript " ^ string_of_int i ^ " for " ^
wenzelm@15703
   176
      quote name ^ " (length " ^ string_of_int n ^ ")")
wenzelm@15703
   177
  else List.nth (thms, i - 1);
wenzelm@15703
   178
wenzelm@15703
   179
in
wenzelm@15703
   180
wenzelm@15703
   181
fun select_thm (_, NONE) thms = thms
wenzelm@15703
   182
  | select_thm (name, SOME is) thms =
wenzelm@15703
   183
      let val n = length thms
wenzelm@15703
   184
      in map (select name thms n) (List.concat (map (interval n) is)) end;
wenzelm@15703
   185
wenzelm@15703
   186
end;
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@9564
   196
    val ref {space, thms_tab, ...} = 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@9564
   200
      (Symtab.lookup (thms_tab, 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@4022
   224
(* thms_of *)
wenzelm@4022
   225
wenzelm@4022
   226
fun thms_of thy =
wenzelm@4022
   227
  let val ref {thms_tab, ...} = get_theorems thy in
skalberg@15570
   228
    map (fn th => (Thm.name_of_thm th, th)) (List.concat (map snd (Symtab.dest thms_tab)))
wenzelm@4022
   229
  end;
wenzelm@4022
   230
kleing@15882
   231
(* thms_with_names_of - theorems in this theory and their names *)
kleing@15882
   232
fun thms_with_names_of thy = 
kleing@15882
   233
  TheoremsData.get thy |> ! |> #index |> FactIndex.find ([],[]);
kleing@15882
   234
  (* looking for nothing in a FactIndex finds everything, i.e. all theorems *)
wenzelm@3987
   235
wenzelm@13274
   236
(* thms_containing *)
wenzelm@3987
   237
wenzelm@13274
   238
fun thms_containing thy idx =
wenzelm@13274
   239
  let
wenzelm@13274
   240
    fun valid (name, ths) =
skalberg@15531
   241
      (case try (transform_error (get_thms thy)) (name, NONE) of
skalberg@15531
   242
        NONE => false
skalberg@15531
   243
      | SOME ths' => Library.equal_lists Thm.eq_thm (ths, ths'));
wenzelm@13274
   244
  in
wenzelm@13274
   245
    (thy :: Theory.ancestors_of thy)
skalberg@15570
   246
    |> map (gen_distinct eq_fst o List.filter valid o FactIndex.find idx o #index o ! o get_theorems)
skalberg@15570
   247
    |> List.concat
wenzelm@13274
   248
  end;
wenzelm@4022
   249
nipkow@13646
   250
fun thms_containing_consts thy consts =
skalberg@15570
   251
  thms_containing thy (consts, []) |> map #2 |> List.concat
nipkow@13646
   252
  |> map (fn th => (Thm.name_of_thm th, th))
nipkow@13646
   253
kleing@15882
   254
(* find_theorems - finding theorems by matching on a series of subterms  *)
kleing@15882
   255
kleing@15882
   256
(* Takes a string pattern, such as "(_::nat) + (_ + _)" and converts it into
kleing@15882
   257
    a term with all free variables made schematic *)
kleing@15882
   258
fun str_pattern_to_term sg str_pattern =
kleing@15882
   259
  let
kleing@15882
   260
    (* pattern as term with dummies as Consts *)
kleing@15882
   261
    val term_pattern = read_cterm sg (str_pattern, TypeInfer.logicT) 
kleing@15882
   262
                       |> Thm.term_of; 
kleing@15882
   263
    (* with dummies as Vars *)
kleing@15882
   264
    val v_pattern = #2 (Term.replace_dummy_patterns (1,term_pattern));
kleing@15882
   265
  in
kleing@15882
   266
    (* with schematic vars *)
kleing@15882
   267
    #1 (Type.varify (v_pattern, []))
kleing@15882
   268
  end;
kleing@15882
   269
kleing@15882
   270
(* find all thms such that for each returned thm, all given 
kleing@15882
   271
    propositions are subterms of it *)
kleing@15882
   272
fun thms_matching_patterns tsign (pat::pats) thms = 
kleing@15882
   273
    let 
kleing@15882
   274
        fun match_single pattern thm = 
kleing@15882
   275
            Pattern.matches_subterm tsign (pat, Thm.prop_of thm);
kleing@15882
   276
    in
kleing@15882
   277
        thms_matching_patterns tsign pats
kleing@15882
   278
            (List.filter (match_single pat) thms)
kleing@15882
   279
    end
kleing@15882
   280
  | thms_matching_patterns _ _ thms = thms; 
kleing@15882
   281
kleing@15882
   282
(* facts are pairs of theorem name and a list of its thms *)
kleing@15882
   283
fun find_theorems sg facts str_patterns =
kleing@15882
   284
  let
kleing@15882
   285
    val typesg = Sign.tsig_of sg;
kleing@15882
   286
    
kleing@15882
   287
    (* the patterns we will be looking for *)
kleing@15882
   288
    val patterns = map (str_pattern_to_term sg) str_patterns;
kleing@15882
   289
kleing@15882
   290
    (* we are interested in theorems which have one or more thms for which
kleing@15882
   291
       all patterns are subterms *)
kleing@15882
   292
    fun matches (_, thms) = 
kleing@15882
   293
        (not o null o (thms_matching_patterns typesg patterns)) thms
kleing@15882
   294
  in
kleing@15882
   295
    List.filter matches facts
kleing@15882
   296
  end;
wenzelm@15703
   297
nipkow@13646
   298
(* intro/elim theorems *)
nipkow@13646
   299
nipkow@13646
   300
(* intro: given a goal state, find a suitable intro rule for some subgoal *)
nipkow@13646
   301
(* elim: given a theorem thm,
nipkow@13646
   302
         find a theorem whose major premise eliminates the conclusion of thm *)
nipkow@13646
   303
skalberg@15531
   304
fun top_const t = (case head_of t of Const (c, _) => SOME c | _ => NONE);
nipkow@13646
   305
nipkow@15387
   306
(* This is a hack to remove the Trueprop constant that most logics use *)
nipkow@15387
   307
fun rem_top (_ $ t) = t
nipkow@15387
   308
  | rem_top _ = Bound 0 (* does not match anything *)
nipkow@13646
   309
nipkow@13646
   310
(*returns all those named_thms whose subterm extracted by extract can be
nipkow@13646
   311
  instantiated to obj; the list is sorted according to the number of premises
nipkow@13646
   312
  and the size of the required substitution.*)
nipkow@15387
   313
fun select_match(c,obj, signobj, named_thms, (extract_thms,extract_term)) =
nipkow@13646
   314
  let val tsig = Sign.tsig_of signobj
nipkow@13646
   315
      fun matches prop =
nipkow@15387
   316
        let val pat = extract_term prop
nipkow@15387
   317
        in case head_of pat of
nipkow@15387
   318
             Const(d,_) => c=d andalso Pattern.matches tsig (pat,obj)
nipkow@15387
   319
           | _ => false
nipkow@15387
   320
        end
nipkow@13646
   321
nipkow@13646
   322
      fun substsize prop =
nipkow@15387
   323
            let val pat = extract_term prop
nipkow@13646
   324
                val (_,subst) = Pattern.match tsig (pat,obj)
berghofe@15797
   325
            in Vartab.foldl (op + o apsnd (size_of_term o snd o snd)) (0, subst)
berghofe@15797
   326
            end
nipkow@13646
   327
nipkow@13646
   328
      fun thm_ord ((p0,s0,_),(p1,s1,_)) =
nipkow@13646
   329
            prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) int_ord ((p0,s0),(p1,s1));
nipkow@13646
   330
nipkow@13646
   331
      fun select((p as (_,thm))::named_thms, sels) =
nipkow@15387
   332
            let
nipkow@15387
   333
              fun sel(thm::thms,sels) =     
nipkow@15387
   334
                    let val {prop, ...} = rep_thm thm
nipkow@15387
   335
                    in if matches prop
nipkow@15387
   336
                       then (nprems_of thm,substsize prop,p)::sels
nipkow@15387
   337
                       else sel(thms,sels)
nipkow@15387
   338
                    end
nipkow@15387
   339
                | sel([],sels) = sels
nipkow@15387
   340
             val {sign, ...} = rep_thm thm
nipkow@15387
   341
           in select(named_thms,if Sign.subsig(sign, signobj)
nipkow@15387
   342
                                then sel(extract_thms thm,sels)
nipkow@15387
   343
                                else sels)
nipkow@15387
   344
           end
nipkow@13646
   345
        | select([],sels) = sels
nipkow@13646
   346
nipkow@13646
   347
  in map (fn (_,_,t) => t) (sort thm_ord (select(named_thms, []))) end;
nipkow@13646
   348
nipkow@15387
   349
fun find_matching_thms extract thy prop =
skalberg@15531
   350
  (case top_const prop of NONE => []
skalberg@15531
   351
   | SOME c => let val thms = thms_containing_consts thy [c]
nipkow@15387
   352
               in select_match(c,prop,Theory.sign_of thy,thms,extract) end)
nipkow@13646
   353
nipkow@15387
   354
val find_intros =
nipkow@15387
   355
  find_matching_thms (single, rem_top o Logic.strip_imp_concl)
nipkow@13646
   356
berghofe@13800
   357
fun find_intros_goal thy st i =
nipkow@15387
   358
  find_intros thy (rem_top(Logic.concl_of_goal (prop_of st) i));
berghofe@13800
   359
nipkow@15387
   360
val find_elims = find_matching_thms
nipkow@15387
   361
  (fn thm => if Thm.no_prems thm then [] else [thm],
nipkow@15387
   362
   rem_top o hd o Logic.strip_imp_prems)
wenzelm@4022
   363
wenzelm@4022
   364
wenzelm@4022
   365
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   366
wenzelm@12695
   367
(* hiding -- affects current theory node only! *)
wenzelm@12695
   368
wenzelm@13424
   369
fun hide_thms fully names thy =
wenzelm@12695
   370
  let
wenzelm@13274
   371
    val r as ref {space, thms_tab, index} = get_theorems thy;
wenzelm@13424
   372
    val space' = NameSpace.hide fully (space, names);
wenzelm@13274
   373
  in r := {space = space', thms_tab = thms_tab, index = index}; thy end;
wenzelm@12695
   374
wenzelm@12695
   375
wenzelm@4853
   376
(* naming *)
wenzelm@4853
   377
berghofe@11998
   378
fun gen_names j len name =
berghofe@11998
   379
  map (fn i => name ^ "_" ^ string_of_int i) (j+1 upto j+len);
wenzelm@4853
   380
berghofe@11998
   381
fun name_multi name xs = gen_names 0 (length xs) name ~~ xs;
berghofe@12235
   382
berghofe@12872
   383
fun name_thm pre (p as (_, thm)) =
berghofe@12872
   384
  if Thm.name_of_thm thm <> "" andalso pre then thm else Thm.name_thm p;
berghofe@12872
   385
berghofe@12872
   386
fun name_thms pre name [x] = [name_thm pre (name, x)]
berghofe@12872
   387
  | name_thms pre name xs = map (name_thm pre) (name_multi name xs);
berghofe@12235
   388
berghofe@12235
   389
fun name_thmss name xs = (case filter_out (null o fst) xs of
berghofe@12872
   390
    [([x], z)] => [([name_thm true (name, x)], z)]
berghofe@12235
   391
  | _ => snd (foldl_map (fn (i, (ys, z)) => (i + length ys,
berghofe@12872
   392
  (map (name_thm true) (gen_names i (length ys) name ~~ ys), z))) (0, xs)));
wenzelm@4853
   393
wenzelm@4853
   394
berghofe@11998
   395
(* enter_thms *)
wenzelm@4853
   396
wenzelm@7470
   397
fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@7470
   398
fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   399
ballarin@15624
   400
fun gen_enter_thms _ _ _ _ _ app_att thy ("", thms) = app_att (thy, thms)
ballarin@15624
   401
  | gen_enter_thms full acc sg pre_name post_name app_att thy (bname, thms) =
wenzelm@7470
   402
      let
ballarin@15624
   403
        val name = full sg bname;
berghofe@11998
   404
        val (thy', thms') = app_att (thy, pre_name name thms);
berghofe@11998
   405
        val named_thms = post_name name thms';
wenzelm@3987
   406
wenzelm@13274
   407
        val r as ref {space, thms_tab, index} = get_theorems_sg sg;
ballarin@15624
   408
        val space' = NameSpace.extend' acc (space, [name]);
wenzelm@7470
   409
        val thms_tab' = Symtab.update ((name, named_thms), thms_tab);
wenzelm@13274
   410
        val index' = FactIndex.add (K false) (index, (name, named_thms));
wenzelm@13274
   411
      in
wenzelm@13274
   412
        (case Symtab.lookup (thms_tab, name) of
skalberg@15531
   413
          NONE => ()
skalberg@15531
   414
        | SOME thms' =>
wenzelm@13274
   415
            if Library.equal_lists Thm.eq_thm (thms', named_thms) then warn_same name
wenzelm@13274
   416
            else warn_overwrite name);
wenzelm@13274
   417
        r := {space = space', thms_tab = thms_tab', index = index'};
berghofe@11998
   418
        (thy', named_thms)
berghofe@11998
   419
      end;
wenzelm@3987
   420
ballarin@15624
   421
fun enter_thms sg = gen_enter_thms Sign.full_name NameSpace.accesses sg;
wenzelm@4853
   422
wenzelm@6091
   423
(* add_thms(s) *)
wenzelm@4853
   424
berghofe@12235
   425
fun add_thms_atts pre_name ((bname, thms), atts) thy =
berghofe@12872
   426
  enter_thms (Theory.sign_of thy) pre_name (name_thms false)
berghofe@11998
   427
    (Thm.applys_attributes o rpair atts) thy (bname, thms);
wenzelm@4853
   428
berghofe@12235
   429
fun gen_add_thmss pre_name args theory =
berghofe@12235
   430
  foldl_map (fn (thy, arg) => add_thms_atts pre_name arg thy) (theory, args);
wenzelm@5907
   431
berghofe@12235
   432
fun gen_add_thms pre_name args =
berghofe@12235
   433
  apsnd (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   434
berghofe@12872
   435
val add_thmss = gen_add_thmss (name_thms true);
berghofe@12872
   436
val add_thms = gen_add_thms (name_thms true);
wenzelm@5907
   437
wenzelm@5907
   438
wenzelm@14564
   439
(* note_thmss(_i) *)
wenzelm@5907
   440
wenzelm@9192
   441
local
wenzelm@12711
   442
ballarin@15624
   443
fun gen_note_thss enter get kind_att (thy, ((bname, more_atts), ths_atts)) =
wenzelm@12711
   444
  let
wenzelm@12711
   445
    fun app (x, (ths, atts)) = Thm.applys_attributes ((x, ths), atts);
ballarin@15624
   446
    val (thy', thms) = enter (Theory.sign_of thy)
skalberg@15570
   447
      name_thmss (name_thms false) (apsnd List.concat o foldl_map app) thy
wenzelm@12711
   448
      (bname, map (fn (ths, atts) => (get thy ths, atts @ more_atts @ [kind_att])) ths_atts);
wenzelm@12711
   449
  in (thy', (bname, thms)) end;
wenzelm@12711
   450
ballarin@15624
   451
fun gen_note_thmss enter get kind_att args thy =
ballarin@15624
   452
  foldl_map (gen_note_thss enter get kind_att) (thy, args);
wenzelm@12711
   453
wenzelm@9192
   454
in
wenzelm@12711
   455
ballarin@15624
   456
(* if path is set, only permit unqualified names *)
ballarin@15624
   457
ballarin@15624
   458
val note_thmss = gen_note_thmss enter_thms get_thms;
ballarin@15624
   459
val note_thmss_i = gen_note_thmss enter_thms (K I);
ballarin@15624
   460
ballarin@15624
   461
(* always permit qualified names,
ballarin@15624
   462
   clients may specify non-standard access policy *)
ballarin@15624
   463
ballarin@15696
   464
fun note_thmss_accesses acc =
ballarin@15624
   465
  gen_note_thmss (gen_enter_thms Sign.full_name' acc) get_thms;
ballarin@15696
   466
fun note_thmss_accesses_i acc =
ballarin@15624
   467
  gen_note_thmss (gen_enter_thms Sign.full_name' acc) (K I);
wenzelm@12711
   468
wenzelm@9192
   469
end;
wenzelm@5280
   470
wenzelm@5280
   471
wenzelm@6091
   472
(* store_thm *)
wenzelm@5280
   473
berghofe@11998
   474
fun store_thm ((bname, thm), atts) thy =
berghofe@12872
   475
  let val (thy', [th']) = add_thms_atts (name_thms true) ((bname, [thm]), atts) thy
wenzelm@5280
   476
  in (thy', th') end;
wenzelm@3987
   477
wenzelm@3987
   478
wenzelm@7405
   479
(* smart_store_thms *)
wenzelm@3987
   480
berghofe@12235
   481
fun gen_smart_store_thms _ (name, []) =
berghofe@11516
   482
      error ("Cannot store empty list of theorems: " ^ quote name)
berghofe@12235
   483
  | gen_smart_store_thms name_thm (name, [thm]) =
berghofe@12872
   484
      snd (enter_thms (Thm.sign_of_thm thm) (name_thm true) (name_thm false)
berghofe@12872
   485
        I () (name, [thm]))
berghofe@12235
   486
  | gen_smart_store_thms name_thm (name, thms) =
wenzelm@7405
   487
      let
wenzelm@7405
   488
        val merge_sg = Sign.merge_refs o apsnd (Sign.self_ref o Thm.sign_of_thm);
skalberg@15570
   489
        val sg_ref = Library.foldl merge_sg (Sign.self_ref (Thm.sign_of_thm (hd thms)), tl thms);
berghofe@12872
   490
      in snd (enter_thms (Sign.deref sg_ref) (name_thm true) (name_thm false)
berghofe@12872
   491
        I () (name, thms))
berghofe@12872
   492
      end;
berghofe@11516
   493
berghofe@12235
   494
val smart_store_thms = gen_smart_store_thms name_thms;
berghofe@12872
   495
val smart_store_thms_open = gen_smart_store_thms (K (K I));
wenzelm@3987
   496
wenzelm@3987
   497
wenzelm@7899
   498
(* forall_elim_vars (belongs to drule.ML) *)
wenzelm@7899
   499
berghofe@13713
   500
(*Replace outermost quantified variable by Var of given index.*)
wenzelm@7899
   501
fun forall_elim_var i th =
wenzelm@7899
   502
    let val {prop,sign,...} = rep_thm th
wenzelm@7899
   503
    in case prop of
berghofe@13713
   504
        Const ("all", _) $ Abs (a, T, _) =>
berghofe@13713
   505
          let val used = map (fst o fst)
skalberg@15570
   506
            (List.filter (equal i o snd o fst) (Term.add_vars ([], prop)))
berghofe@13713
   507
          in forall_elim (cterm_of sign (Var ((variant used a, i), T))) th end
berghofe@13713
   508
      | _ => raise THM ("forall_elim_var", i, [th])
wenzelm@7899
   509
    end;
wenzelm@7899
   510
wenzelm@7899
   511
(*Repeat forall_elim_var until all outer quantifiers are removed*)
wenzelm@7899
   512
fun forall_elim_vars i th =
wenzelm@7899
   513
    forall_elim_vars i (forall_elim_var i th)
wenzelm@7899
   514
        handle THM _ => th;
wenzelm@7899
   515
wenzelm@7899
   516
wenzelm@4022
   517
(* store axioms as theorems *)
wenzelm@4022
   518
wenzelm@4853
   519
local
wenzelm@7899
   520
  fun get_axs thy named_axs =
wenzelm@7899
   521
    map (forall_elim_vars 0 o Thm.get_axiom thy o fst) named_axs;
wenzelm@7753
   522
wenzelm@8419
   523
  fun add_single add (thy, ((name, ax), atts)) =
wenzelm@4853
   524
    let
berghofe@11998
   525
      val named_ax = [(name, ax)];
wenzelm@7753
   526
      val thy' = add named_ax thy;
wenzelm@7753
   527
      val thm = hd (get_axs thy' named_ax);
berghofe@12235
   528
    in apsnd hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
wenzelm@7753
   529
wenzelm@8419
   530
  fun add_multi add (thy, ((name, axs), atts)) =
wenzelm@7753
   531
    let
wenzelm@7753
   532
      val named_axs = name_multi name axs;
wenzelm@4853
   533
      val thy' = add named_axs thy;
wenzelm@7753
   534
      val thms = get_axs thy' named_axs;
berghofe@12235
   535
    in apsnd hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
wenzelm@4022
   536
wenzelm@8419
   537
  fun add_singles add args thy = foldl_map (add_single add) (thy, args);
wenzelm@8419
   538
  fun add_multis add args thy = foldl_map (add_multi add) (thy, args);
wenzelm@4853
   539
in
wenzelm@7753
   540
  val add_axioms    = add_singles Theory.add_axioms;
wenzelm@7753
   541
  val add_axioms_i  = add_singles Theory.add_axioms_i;
wenzelm@7753
   542
  val add_axiomss   = add_multis Theory.add_axioms;
wenzelm@7753
   543
  val add_axiomss_i = add_multis Theory.add_axioms_i;
wenzelm@9318
   544
  val add_defs      = add_singles o Theory.add_defs;
wenzelm@9318
   545
  val add_defs_i    = add_singles o Theory.add_defs_i;
wenzelm@9318
   546
  val add_defss     = add_multis o Theory.add_defs;
wenzelm@9318
   547
  val add_defss_i   = add_multis o Theory.add_defs_i;
wenzelm@4853
   548
end;
wenzelm@4022
   549
wenzelm@4022
   550
wenzelm@3987
   551
wenzelm@4963
   552
(*** derived theory operations ***)
wenzelm@4963
   553
wenzelm@4963
   554
(** theory management **)
wenzelm@4963
   555
wenzelm@5005
   556
(* data kind 'Pure/theory_management' *)
wenzelm@4963
   557
wenzelm@5005
   558
structure TheoryManagementDataArgs =
wenzelm@5005
   559
struct
wenzelm@5005
   560
  val name = "Pure/theory_management";
wenzelm@6660
   561
  type T = {name: string, version: int};
wenzelm@5000
   562
wenzelm@6660
   563
  val empty = {name = "", version = 0};
wenzelm@6547
   564
  val copy = I;
wenzelm@5005
   565
  val prep_ext  = I;
wenzelm@5000
   566
  fun merge _ = empty;
wenzelm@5005
   567
  fun print _ _ = ();
wenzelm@4963
   568
end;
wenzelm@4963
   569
wenzelm@5005
   570
structure TheoryManagementData = TheoryDataFun(TheoryManagementDataArgs);
wenzelm@5005
   571
val get_info = TheoryManagementData.get;
wenzelm@5005
   572
val put_info = TheoryManagementData.put;
wenzelm@5005
   573
wenzelm@4963
   574
wenzelm@4963
   575
(* get / put name *)
wenzelm@4963
   576
wenzelm@5000
   577
val get_name = #name o get_info;
wenzelm@6660
   578
fun put_name name = put_info {name = name, version = 0};
wenzelm@4963
   579
wenzelm@4963
   580
wenzelm@4963
   581
(* control prefixing of theory name *)
wenzelm@4963
   582
wenzelm@5210
   583
val global_path = Theory.root_path;
wenzelm@4963
   584
wenzelm@4963
   585
fun local_path thy =
wenzelm@5210
   586
  thy |> Theory.root_path |> Theory.add_path (get_name thy);
wenzelm@4963
   587
wenzelm@4963
   588
wenzelm@4963
   589
(* begin / end theory *)
wenzelm@4963
   590
wenzelm@4963
   591
fun begin_theory name thys =
wenzelm@4963
   592
  Theory.prep_ext_merge thys
wenzelm@4963
   593
  |> put_name name
wenzelm@4963
   594
  |> local_path;
wenzelm@4963
   595
wenzelm@12123
   596
fun end_theory thy =
wenzelm@12123
   597
  thy
wenzelm@12123
   598
  |> Theory.add_name (get_name thy);
wenzelm@4963
   599
wenzelm@6682
   600
fun checkpoint thy =
wenzelm@6682
   601
  if is_draft thy then
wenzelm@6682
   602
    let val {name, version} = get_info thy in
wenzelm@6682
   603
      thy
wenzelm@6682
   604
      |> Theory.add_name (name ^ ":" ^ string_of_int version)
wenzelm@6682
   605
      |> put_info {name = name, version = version + 1}
wenzelm@6682
   606
    end
wenzelm@6682
   607
  else thy;
wenzelm@5000
   608
wenzelm@5000
   609
wenzelm@4963
   610
wenzelm@4963
   611
(** add logical types **)
wenzelm@4922
   612
wenzelm@4922
   613
fun add_typedecls decls thy =
wenzelm@4922
   614
  let
wenzelm@4922
   615
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@4922
   616
wenzelm@4922
   617
    fun type_of (raw_name, vs, mx) =
wenzelm@4922
   618
      if null (duplicates vs) then (raw_name, length vs, mx)
wenzelm@4922
   619
      else error ("Duplicate parameters in type declaration: " ^ quote raw_name);
wenzelm@14854
   620
  in thy |> Theory.add_types (map type_of decls) end;
wenzelm@4922
   621
wenzelm@4922
   622
wenzelm@4922
   623
wenzelm@5091
   624
(*** the ProtoPure theory ***)
wenzelm@3987
   625
paulson@14669
   626
paulson@14669
   627
(*It might make sense to restrict the polymorphism of the constant "==" to
paulson@14669
   628
  sort logic, instead of the universal sort, {}.  Unfortunately, this change
paulson@14669
   629
  causes HOL/Import/shuffler.ML to fail.*)
paulson@14669
   630
wenzelm@3987
   631
val proto_pure =
wenzelm@3987
   632
  Theory.pre_pure
berghofe@11516
   633
  |> Library.apply [TheoremsData.init, TheoryManagementData.init, Proofterm.init]
wenzelm@4963
   634
  |> put_name "ProtoPure"
wenzelm@4963
   635
  |> global_path
wenzelm@3987
   636
  |> Theory.add_types
wenzelm@4922
   637
   [("fun", 2, NoSyn),
wenzelm@4922
   638
    ("prop", 0, NoSyn),
wenzelm@4922
   639
    ("itself", 1, NoSyn),
wenzelm@4922
   640
    ("dummy", 0, NoSyn)]
wenzelm@4922
   641
  |> Theory.add_nonterminals Syntax.pure_nonterms
wenzelm@3987
   642
  |> Theory.add_syntax Syntax.pure_syntax
wenzelm@15801
   643
  |> Theory.add_syntax Syntax.pure_appl_syntax
wenzelm@6692
   644
  |> Theory.add_modesyntax (Symbol.xsymbolsN, true) Syntax.pure_xsym_syntax
wenzelm@3987
   645
  |> Theory.add_syntax
wenzelm@7949
   646
   [("==>", "[prop, prop] => prop", Delimfix "op ==>"),
wenzelm@9534
   647
    (Term.dummy_patternN, "aprop", Delimfix "'_")]
wenzelm@3987
   648
  |> Theory.add_consts
wenzelm@14854
   649
   [("==", "['a, 'a] => prop", InfixrName ("==", 2)),
wenzelm@3987
   650
    ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@3987
   651
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@10667
   652
    ("Goal", "prop => prop", NoSyn),
wenzelm@6547
   653
    ("TYPE", "'a itself", NoSyn),
wenzelm@9534
   654
    (Term.dummy_patternN, "'a", Delimfix "'_")]
skalberg@14223
   655
  |> Theory.add_finals_i false
wenzelm@14854
   656
    [Const("==", [TFree ("'a", []), TFree ("'a", [])] ---> propT),
wenzelm@14854
   657
     Const("==>", [propT, propT] ---> propT),
wenzelm@14854
   658
     Const("all", (TFree("'a", []) --> propT) --> propT),
wenzelm@14854
   659
     Const("TYPE", a_itselfT)]
nipkow@5041
   660
  |> Theory.add_modesyntax ("", false)
wenzelm@12138
   661
    (Syntax.pure_syntax_output @ Syntax.pure_appl_syntax)
wenzelm@12250
   662
  |> Theory.add_trfuns Syntax.pure_trfuns
wenzelm@12250
   663
  |> Theory.add_trfunsT Syntax.pure_trfunsT
wenzelm@4963
   664
  |> local_path
wenzelm@10667
   665
  |> (#1 oo (add_defs_i false o map Thm.no_attributes))
wenzelm@10667
   666
   [("Goal_def", let val A = Free ("A", propT) in Logic.mk_equals (Logic.mk_goal A, A) end)]
wenzelm@9238
   667
  |> (#1 o add_thmss [(("nothing", []), [])])
berghofe@11516
   668
  |> Theory.add_axioms_i Proofterm.equality_axms
wenzelm@4963
   669
  |> end_theory;
wenzelm@3987
   670
wenzelm@5091
   671
structure ProtoPure =
wenzelm@5091
   672
struct
wenzelm@5091
   673
  val thy = proto_pure;
wenzelm@5091
   674
  val Goal_def = get_axiom thy "Goal_def";
wenzelm@5091
   675
end;
wenzelm@3987
   676
wenzelm@3987
   677
wenzelm@3987
   678
end;
wenzelm@3987
   679
wenzelm@3987
   680
wenzelm@4022
   681
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   682
open BasicPureThy;
wenzelm@15715
   683