src/Pure/Tools/named_theorems.ML
author wenzelm
Fri Apr 03 20:52:17 2015 +0200 (2015-04-03)
changeset 59920 86d302846b16
parent 59879 6292f1f5ffae
child 59936 b8ffc3dc9e24
permissions -rw-r--r--
check wrt. proper context, e.g. relevant for 'experiment' target;
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@57886
    53
fun content context = rev o Item_Net.content o the_entry context;
wenzelm@57886
    54
val get = content o Context.Proof;
wenzelm@57886
    55
wenzelm@57886
    56
fun add_thm name = map_entry name o Item_Net.update;
wenzelm@57886
    57
fun del_thm name = map_entry name o Item_Net.remove;
wenzelm@57886
    58
wenzelm@57886
    59
val add = Thm.declaration_attribute o add_thm;
wenzelm@57886
    60
val del = Thm.declaration_attribute o del_thm;
wenzelm@57886
    61
wenzelm@57886
    62
wenzelm@59920
    63
(* check *)
wenzelm@59920
    64
wenzelm@59920
    65
fun check ctxt (xname, pos) =
wenzelm@59920
    66
  let
wenzelm@59920
    67
    val context = Context.Proof ctxt;
wenzelm@59920
    68
    val fact_ref = Facts.Named ((xname, Position.none), NONE);
wenzelm@59920
    69
    fun err () = error (undeclared xname ^ Position.here pos);
wenzelm@59920
    70
  in
wenzelm@59920
    71
    (case try (Proof_Context.get_fact_generic context) fact_ref of
wenzelm@59920
    72
      SOME (SOME name, _) => if can (the_entry context) name then name else err ()
wenzelm@59920
    73
    | _ => err ())
wenzelm@59920
    74
  end;
wenzelm@59920
    75
wenzelm@59920
    76
wenzelm@57886
    77
(* declaration *)
wenzelm@57886
    78
wenzelm@57888
    79
fun declare binding descr lthy =
wenzelm@57886
    80
  let
wenzelm@59879
    81
    val name = Local_Theory.full_name lthy binding;
wenzelm@57886
    82
    val description =
wenzelm@57886
    83
      "declaration of " ^ (if descr = "" then Binding.name_of binding ^ " rules" else descr);
wenzelm@57888
    84
    val lthy' = lthy
wenzelm@57929
    85
      |> Local_Theory.background_theory (Context.theory_map (new_entry name))
wenzelm@57929
    86
      |> Local_Theory.map_contexts (K (Context.proof_map (new_entry name)))
wenzelm@57929
    87
      |> Local_Theory.add_thms_dynamic (binding, fn context => content context name)
wenzelm@57927
    88
      |> Attrib.local_setup binding (Attrib.add_del (add name) (del name)) description
wenzelm@57888
    89
  in (name, lthy') end;
wenzelm@57886
    90
wenzelm@57886
    91
val _ =
wenzelm@57888
    92
  Outer_Syntax.local_theory @{command_spec "named_theorems"}
wenzelm@57886
    93
    "declare named collection of theorems"
wenzelm@59028
    94
    (Parse.and_list1 (Parse.binding -- Scan.optional Parse.text "") >>
wenzelm@59028
    95
      fold (fn (b, descr) => snd o declare b descr));
wenzelm@57886
    96
wenzelm@57886
    97
wenzelm@57886
    98
(* ML antiquotation *)
wenzelm@57886
    99
wenzelm@57886
   100
val _ = Theory.setup
wenzelm@57886
   101
  (ML_Antiquotation.inline @{binding named_theorems}
wenzelm@59920
   102
    (Args.context -- Scan.lift (Parse.position Args.name) >>
wenzelm@59920
   103
      (fn (ctxt, name) => ML_Syntax.print_string (check ctxt name))));
wenzelm@57886
   104
wenzelm@57886
   105
end;