src/Pure/Thy/thm_database.ML
author wenzelm
Tue Dec 18 19:54:33 2007 +0100 (2007-12-18)
changeset 25699 891fe6b71d3b
parent 23921 947152add153
child 26386 9c806de22a6a
permissions -rw-r--r--
named some critical sections;
clasohm@1132
     1
(*  Title:      Pure/Thy/thm_database.ML
clasohm@1132
     2
    ID:         $Id$
wenzelm@11895
     3
    Author:     Markus Wenzel, TU Muenchen
clasohm@1132
     4
wenzelm@17170
     5
ML toplevel interface to the theorem database.
wenzelm@4023
     6
*)
clasohm@1262
     7
wenzelm@6204
     8
signature BASIC_THM_DATABASE =
wenzelm@4023
     9
sig
wenzelm@4023
    10
  val store_thm: string * thm -> thm
wenzelm@7410
    11
  val store_thms: string * thm list -> thm list
wenzelm@17170
    12
  val legacy_bindings: theory -> string
wenzelm@17170
    13
  val use_legacy_bindings: theory -> unit
wenzelm@4023
    14
end;
clasohm@1132
    15
wenzelm@6204
    16
signature THM_DATABASE =
wenzelm@6204
    17
sig
wenzelm@6204
    18
  include BASIC_THM_DATABASE
wenzelm@7410
    19
  val qed_thms: thm list ref
wenzelm@6204
    20
  val ml_store_thm: string * thm -> unit
wenzelm@7410
    21
  val ml_store_thms: string * thm list -> unit
wenzelm@6204
    22
end;
wenzelm@6204
    23
wenzelm@3627
    24
structure ThmDatabase: THM_DATABASE =
nipkow@1221
    25
struct
clasohm@1132
    26
wenzelm@4023
    27
(** store theorems **)
clasohm@1132
    28
wenzelm@13279
    29
(* store in theory and perform presentation *)
clasohm@1136
    30
wenzelm@4023
    31
fun store_thm (name, thm) =
wenzelm@7410
    32
  let val thm' = hd (PureThy.smart_store_thms (name, [thm]))
wenzelm@6327
    33
  in Present.theorem name thm'; thm' end;
clasohm@1132
    34
wenzelm@7410
    35
fun store_thms (name, thms) =
wenzelm@7410
    36
  let val thms' = PureThy.smart_store_thms (name, thms)
wenzelm@7410
    37
  in Present.theorems name thms'; thms' end;
wenzelm@7410
    38
clasohm@1262
    39
wenzelm@4023
    40
(* store on ML toplevel *)
wenzelm@4023
    41
wenzelm@7410
    42
val qed_thms: thm list ref = ref [];
clasohm@1262
    43
wenzelm@7410
    44
fun warn_ml name =
wenzelm@21482
    45
  if ML_Syntax.is_identifier name then false
wenzelm@7573
    46
  else if name = "" then true
wenzelm@7410
    47
  else (warning ("Cannot bind theorem(s) " ^ quote name ^ " as ML value"); true);
wenzelm@7410
    48
wenzelm@22144
    49
val use_text_verbose = use_text "" Output.ml_output true;
wenzelm@7854
    50
wenzelm@4023
    51
fun ml_store_thm (name, thm) =
wenzelm@4023
    52
  let val thm' = store_thm (name, thm) in
wenzelm@7410
    53
    if warn_ml name then ()
wenzelm@25699
    54
    else NAMED_CRITICAL "ML" (fn () => (qed_thms := [thm'];
wenzelm@23921
    55
      use_text_verbose ("val " ^ name ^ " = hd (! ThmDatabase.qed_thms);")))
wenzelm@7410
    56
  end;
wenzelm@7410
    57
wenzelm@7410
    58
fun ml_store_thms (name, thms) =
wenzelm@7410
    59
  let val thms' = store_thms (name, thms) in
wenzelm@7410
    60
    if warn_ml name then ()
wenzelm@25699
    61
    else NAMED_CRITICAL "ML" (fn () => (qed_thms := thms';
wenzelm@23921
    62
      use_text_verbose ("val " ^ name ^ " = ! ThmDatabase.qed_thms;")))
clasohm@1262
    63
  end;
clasohm@1262
    64
wenzelm@4023
    65
wenzelm@17170
    66
(* legacy bindings *)
wenzelm@17170
    67
wenzelm@17170
    68
fun legacy_bindings thy =
wenzelm@17170
    69
  let
wenzelm@17170
    70
    val thy_name = Context.theory_name thy;
wenzelm@17170
    71
    val (space, thms) = PureThy.theorems_of thy;
wenzelm@17170
    72
wenzelm@17170
    73
    fun prune name =
wenzelm@17170
    74
      let
wenzelm@17170
    75
        val xname = NameSpace.extern space name;
wenzelm@17170
    76
        fun result prfx bname =
wenzelm@21482
    77
          if (prfx = "" orelse ML_Syntax.is_identifier prfx) andalso
wenzelm@21482
    78
              ML_Syntax.is_identifier bname andalso
wenzelm@17170
    79
              NameSpace.intern space xname = name then
wenzelm@17412
    80
            SOME (prfx, (bname, xname, length (the (Symtab.lookup thms name)) = 1))
wenzelm@17170
    81
          else NONE;
wenzelm@21858
    82
        val names = NameSpace.explode name;
wenzelm@17170
    83
      in
wenzelm@19012
    84
        (case #2 (chop (length names - 2) names) of
wenzelm@17170
    85
          [bname] => result "" bname
wenzelm@17170
    86
        | [prfx, bname] => result (if prfx = thy_name then "" else prfx) bname
wenzelm@17170
    87
        | _ => NONE)
wenzelm@17170
    88
      end;
wenzelm@17170
    89
wenzelm@17170
    90
    fun mk_struct "" = I
wenzelm@17170
    91
      | mk_struct prfx = enclose ("structure " ^ prfx ^ " =\nstruct\n") "\nend\n";
wenzelm@17170
    92
wenzelm@17170
    93
    fun mk_thm (bname, xname, singleton) =
wenzelm@17170
    94
      "val " ^ bname ^ " = thm" ^ (if singleton then "" else "s") ^ " " ^ quote xname;
wenzelm@17170
    95
  in
wenzelm@19482
    96
    Symtab.keys thms |> map_filter prune
wenzelm@18931
    97
    |> Symtab.make_list |> Symtab.dest |> sort_wrt #1
wenzelm@17170
    98
    |> map (fn (prfx, entries) =>
wenzelm@17170
    99
      entries |> sort_wrt #1 |> map mk_thm |> cat_lines |> mk_struct prfx)
wenzelm@17170
   100
    |> cat_lines
wenzelm@17170
   101
  end;
wenzelm@17170
   102
wenzelm@22086
   103
fun use_legacy_bindings thy =
wenzelm@22095
   104
  ML_Context.use_mltext (legacy_bindings thy) true (SOME (Context.Theory thy));
wenzelm@17170
   105
clasohm@1132
   106
end;
wenzelm@6204
   107
wenzelm@6204
   108
structure BasicThmDatabase: BASIC_THM_DATABASE = ThmDatabase;
wenzelm@6204
   109
open BasicThmDatabase;