src/Pure/Thy/thm_database.ML
author wenzelm
Fri Dec 15 00:08:06 2006 +0100 (2006-12-15)
changeset 21858 05f57309170c
parent 21482 7bb5de80917f
child 22086 cf6019fece63
permissions -rw-r--r--
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
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@20926
    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@13279
    54
    else (qed_thms := [thm'];
wenzelm@13279
    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@7854
    61
    else (qed_thms := thms'; use_text_verbose ("val " ^ name ^ " = ! ThmDatabase.qed_thms;"))
clasohm@1262
    62
  end;
clasohm@1262
    63
wenzelm@4023
    64
wenzelm@17170
    65
(* legacy bindings *)
wenzelm@17170
    66
wenzelm@17170
    67
fun legacy_bindings thy =
wenzelm@17170
    68
  let
wenzelm@17170
    69
    val thy_name = Context.theory_name thy;
wenzelm@17170
    70
    val (space, thms) = PureThy.theorems_of thy;
wenzelm@17170
    71
wenzelm@17170
    72
    fun prune name =
wenzelm@17170
    73
      let
wenzelm@17170
    74
        val xname = NameSpace.extern space name;
wenzelm@17170
    75
        fun result prfx bname =
wenzelm@21482
    76
          if (prfx = "" orelse ML_Syntax.is_identifier prfx) andalso
wenzelm@21482
    77
              ML_Syntax.is_identifier bname andalso
wenzelm@17170
    78
              NameSpace.intern space xname = name then
wenzelm@17412
    79
            SOME (prfx, (bname, xname, length (the (Symtab.lookup thms name)) = 1))
wenzelm@17170
    80
          else NONE;
wenzelm@21858
    81
        val names = NameSpace.explode name;
wenzelm@17170
    82
      in
wenzelm@19012
    83
        (case #2 (chop (length names - 2) names) of
wenzelm@17170
    84
          [bname] => result "" bname
wenzelm@17170
    85
        | [prfx, bname] => result (if prfx = thy_name then "" else prfx) bname
wenzelm@17170
    86
        | _ => NONE)
wenzelm@17170
    87
      end;
wenzelm@17170
    88
wenzelm@17170
    89
    fun mk_struct "" = I
wenzelm@17170
    90
      | mk_struct prfx = enclose ("structure " ^ prfx ^ " =\nstruct\n") "\nend\n";
wenzelm@17170
    91
wenzelm@17170
    92
    fun mk_thm (bname, xname, singleton) =
wenzelm@17170
    93
      "val " ^ bname ^ " = thm" ^ (if singleton then "" else "s") ^ " " ^ quote xname;
wenzelm@17170
    94
  in
wenzelm@19482
    95
    Symtab.keys thms |> map_filter prune
wenzelm@18931
    96
    |> Symtab.make_list |> Symtab.dest |> sort_wrt #1
wenzelm@17170
    97
    |> map (fn (prfx, entries) =>
wenzelm@17170
    98
      entries |> sort_wrt #1 |> map mk_thm |> cat_lines |> mk_struct prfx)
wenzelm@17170
    99
    |> cat_lines
wenzelm@17170
   100
  end;
wenzelm@17170
   101
wenzelm@17170
   102
fun use_legacy_bindings thy = Context.use_mltext (legacy_bindings thy) true (SOME thy);
wenzelm@17170
   103
clasohm@1132
   104
end;
wenzelm@6204
   105
wenzelm@6204
   106
structure BasicThmDatabase: BASIC_THM_DATABASE = ThmDatabase;
wenzelm@6204
   107
open BasicThmDatabase;