src/Pure/Tools/named_theorems.ML
author wenzelm
Sun Aug 30 13:08:00 2015 +0200 (2015-08-30)
changeset 61049 0d401f874942
parent 59936 b8ffc3dc9e24
child 61900 3f5e2e0a6d29
permissions -rw-r--r--
trim context for persistent storage;
wenzelm@57886
     1
(*  Title:      Pure/Tools/named_theorems.ML
wenzelm@57886
     2
    Author:     Makarius
wenzelm@57886
     3
wenzelm@57886
     4
Named collections of theorems in canonical order.
wenzelm@57886
     5
*)
wenzelm@57886
     6
wenzelm@57886
     7
signature NAMED_THEOREMS =
wenzelm@57886
     8
sig
wenzelm@57886
     9
  val member: Proof.context -> string -> thm -> bool
wenzelm@57886
    10
  val get: Proof.context -> string -> thm list
wenzelm@57886
    11
  val add_thm: string -> thm -> Context.generic -> Context.generic
wenzelm@57886
    12
  val del_thm: string -> thm -> Context.generic -> Context.generic
wenzelm@57886
    13
  val add: string -> attribute
wenzelm@57886
    14
  val del: string -> attribute
wenzelm@59920
    15
  val check: Proof.context -> string * Position.T -> string
wenzelm@57888
    16
  val declare: binding -> string -> local_theory -> string * local_theory
wenzelm@57886
    17
end;
wenzelm@57886
    18
wenzelm@57886
    19
structure Named_Theorems: NAMED_THEOREMS =
wenzelm@57886
    20
struct
wenzelm@57886
    21
wenzelm@57886
    22
(* context data *)
wenzelm@57886
    23
wenzelm@57886
    24
structure Data = Generic_Data
wenzelm@57886
    25
(
wenzelm@57886
    26
  type T = thm Item_Net.T Symtab.table;
wenzelm@57886
    27
  val empty: T = Symtab.empty;
wenzelm@57886
    28
  val extend = I;
wenzelm@57965
    29
  val merge : T * T -> T = Symtab.join (K Item_Net.merge);
wenzelm@57886
    30
);
wenzelm@57886
    31
wenzelm@57886
    32
fun new_entry name =
wenzelm@57886
    33
  Data.map (fn data =>
wenzelm@57886
    34
    if Symtab.defined data name
wenzelm@57886
    35
    then error ("Duplicate declaration of named theorems: " ^ quote name)
wenzelm@57886
    36
    else Symtab.update (name, Thm.full_rules) data);
wenzelm@57886
    37
wenzelm@59920
    38
fun undeclared name = "Undeclared named theorems " ^ quote name;
wenzelm@59920
    39
wenzelm@57886
    40
fun the_entry context name =
wenzelm@57886
    41
  (case Symtab.lookup (Data.get context) name of
wenzelm@59920
    42
    NONE => error (undeclared name)
wenzelm@57886
    43
  | SOME entry => entry);
wenzelm@57886
    44
wenzelm@57886
    45
fun map_entry name f context =
wenzelm@57886
    46
  (the_entry context name; Data.map (Symtab.map_entry name f) context);
wenzelm@57886
    47
wenzelm@57886
    48
wenzelm@57886
    49
(* maintain content *)
wenzelm@57886
    50
wenzelm@57886
    51
fun member ctxt = Item_Net.member o the_entry (Context.Proof ctxt);
wenzelm@57886
    52
wenzelm@61049
    53
fun content context =
wenzelm@61049
    54
  rev o map (Thm.transfer (Context.theory_of context)) o Item_Net.content o the_entry context;
wenzelm@61049
    55
wenzelm@57886
    56
val get = content o Context.Proof;
wenzelm@57886
    57
wenzelm@61049
    58
fun add_thm name th = map_entry name (Item_Net.update (Thm.trim_context th));
wenzelm@57886
    59
fun del_thm name = map_entry name o Item_Net.remove;
wenzelm@57886
    60
wenzelm@57886
    61
val add = Thm.declaration_attribute o add_thm;
wenzelm@57886
    62
val del = Thm.declaration_attribute o del_thm;
wenzelm@57886
    63
wenzelm@57886
    64
wenzelm@59920
    65
(* check *)
wenzelm@59920
    66
wenzelm@59920
    67
fun check ctxt (xname, pos) =
wenzelm@59920
    68
  let
wenzelm@59920
    69
    val context = Context.Proof ctxt;
wenzelm@59920
    70
    val fact_ref = Facts.Named ((xname, Position.none), NONE);
wenzelm@59920
    71
    fun err () = error (undeclared xname ^ Position.here pos);
wenzelm@59920
    72
  in
wenzelm@59920
    73
    (case try (Proof_Context.get_fact_generic context) fact_ref of
wenzelm@59920
    74
      SOME (SOME name, _) => if can (the_entry context) name then name else err ()
wenzelm@59920
    75
    | _ => err ())
wenzelm@59920
    76
  end;
wenzelm@59920
    77
wenzelm@59920
    78
wenzelm@57886
    79
(* declaration *)
wenzelm@57886
    80
wenzelm@57888
    81
fun declare binding descr lthy =
wenzelm@57886
    82
  let
wenzelm@59879
    83
    val name = Local_Theory.full_name lthy binding;
wenzelm@57886
    84
    val description =
wenzelm@57886
    85
      "declaration of " ^ (if descr = "" then Binding.name_of binding ^ " rules" else descr);
wenzelm@57888
    86
    val lthy' = lthy
wenzelm@57929
    87
      |> Local_Theory.background_theory (Context.theory_map (new_entry name))
wenzelm@57929
    88
      |> Local_Theory.map_contexts (K (Context.proof_map (new_entry name)))
wenzelm@57929
    89
      |> Local_Theory.add_thms_dynamic (binding, fn context => content context name)
wenzelm@57927
    90
      |> Attrib.local_setup binding (Attrib.add_del (add name) (del name)) description
wenzelm@57888
    91
  in (name, lthy') end;
wenzelm@57886
    92
wenzelm@57886
    93
val _ =
wenzelm@59936
    94
  Outer_Syntax.local_theory @{command_keyword named_theorems}
wenzelm@57886
    95
    "declare named collection of theorems"
wenzelm@59028
    96
    (Parse.and_list1 (Parse.binding -- Scan.optional Parse.text "") >>
wenzelm@59028
    97
      fold (fn (b, descr) => snd o declare b descr));
wenzelm@57886
    98
wenzelm@57886
    99
wenzelm@57886
   100
(* ML antiquotation *)
wenzelm@57886
   101
wenzelm@57886
   102
val _ = Theory.setup
wenzelm@57886
   103
  (ML_Antiquotation.inline @{binding named_theorems}
wenzelm@59920
   104
    (Args.context -- Scan.lift (Parse.position Args.name) >>
wenzelm@59920
   105
      (fn (ctxt, name) => ML_Syntax.print_string (check ctxt name))));
wenzelm@57886
   106
wenzelm@57886
   107
end;