src/Pure/Thy/thy_info.ML
author wenzelm
Thu, 23 Oct 1997 12:43:07 +0200
changeset 3976 1030dd79720b
parent 3604 6bf9f09f3d61
child 3997 42062f636bdf
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_info.ML
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     2
    ID:         $Id$
3976
wenzelm
parents: 3604
diff changeset
     3
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     4
3976
wenzelm
parents: 3604
diff changeset
     5
Theory loader info database.
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     6
*)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     7
3976
wenzelm
parents: 3604
diff changeset
     8
(* FIXME wipe out! *)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     9
(*Functions to handle arbitrary data added by the user; type "exn" is used
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    10
  to store data*)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    11
datatype thy_methods =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    12
  ThyMethods of {merge: exn list -> exn, put: exn -> unit, get: unit -> exn};
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    13
3976
wenzelm
parents: 3604
diff changeset
    14
wenzelm
parents: 3604
diff changeset
    15
type thy_info =
wenzelm
parents: 3604
diff changeset
    16
  {path: string,
wenzelm
parents: 3604
diff changeset
    17
    children: string list, parents: string list,
wenzelm
parents: 3604
diff changeset
    18
    thy_time: string option, ml_time: string option,
wenzelm
parents: 3604
diff changeset
    19
    theory: theory option, thms: thm Symtab.table,
wenzelm
parents: 3604
diff changeset
    20
    methods: thy_methods Symtab.table,
wenzelm
parents: 3604
diff changeset
    21
    data: exn Symtab.table * exn Symtab.table};
wenzelm
parents: 3604
diff changeset
    22
wenzelm
parents: 3604
diff changeset
    23
(*
wenzelm
parents: 3604
diff changeset
    24
        path: directory where theory's files are located
wenzelm
parents: 3604
diff changeset
    25
wenzelm
parents: 3604
diff changeset
    26
        thy_time, ml_time = None     theory file has not been read yet
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    27
                          = Some ""  theory was read but has either been marked
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    28
                                     as outdated or there is no such file for
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    29
                                     this theory (see e.g. 'virtual' theories
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    30
                                     like Pure or theories without a ML file)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    31
        theory = None  theory has not been read yet
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    32
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    33
        parents: While 'children' contains all theories the theory depends
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    34
                 on (i.e. also ones quoted in the .thy file),
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    35
                 'parents' only contains the theories which were used to form
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    36
                 the base of this theory.
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    37
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    38
        methods: three methods for each user defined data;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    39
                 merge: merges data of ancestor theories
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    40
                 put: retrieves data from loaded_thys and stores it somewhere
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    41
                 get: retrieves data from somewhere and stores it
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    42
                      in loaded_thys
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    43
        data: user defined data; exn is used to allow arbitrary types;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    44
              first element of pairs contains result that get returned after
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    45
              thy file was read, second element after ML file was read;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    46
              if ML files has not been read, second element is identical to
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    47
              first one because get_thydata, which is meant to return the
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    48
              latest data, always accesses the 2nd element
3976
wenzelm
parents: 3604
diff changeset
    49
*)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    50
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    51
signature THY_INFO =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    52
sig
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    53
  val loaded_thys    : thy_info Symtab.table ref
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    54
  val store_theory   : theory * string -> unit
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    55
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    56
  val theory_of      : string -> theory
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    57
  val theory_of_sign : Sign.sg -> theory
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    58
  val theory_of_thm  : thm -> theory
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    59
  val children_of    : string -> string list
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    60
  val parents_of_name: string -> string list
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    61
  val thyinfo_of_sign: Sign.sg -> string * thy_info
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    62
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    63
  val add_thydata    : string -> string * thy_methods -> unit
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    64
  val get_thydata    : string -> string -> exn option
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    65
  val put_thydata    : bool -> string -> unit
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    66
  val set_current_thy: string -> unit
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    67
  val get_thyinfo    : string -> thy_info option
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    68
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    69
  val path_of        : string -> string
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    70
end;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    71
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    72
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    73
structure ThyInfo: THY_INFO =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    74
struct
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    75
3976
wenzelm
parents: 3604
diff changeset
    76
(* loaded theories *)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    77
3976
wenzelm
parents: 3604
diff changeset
    78
fun mk_info (name, children, parents, theory) =
wenzelm
parents: 3604
diff changeset
    79
  (name,
wenzelm
parents: 3604
diff changeset
    80
    {path = "", children = children, parents = parents, thy_time = Some "",
wenzelm
parents: 3604
diff changeset
    81
      ml_time = Some "", theory = Some theory, thms = Symtab.null,
wenzelm
parents: 3604
diff changeset
    82
      methods = Symtab.null, data = (Symtab.null, Symtab.null)}: thy_info);
wenzelm
parents: 3604
diff changeset
    83
wenzelm
parents: 3604
diff changeset
    84
(*preloaded theories*)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    85
val loaded_thys =
3976
wenzelm
parents: 3604
diff changeset
    86
  ref (Symtab.make (map mk_info
wenzelm
parents: 3604
diff changeset
    87
    [("ProtoPure", ["Pure", "CPure"], [], Theory.proto_pure),
wenzelm
parents: 3604
diff changeset
    88
     ("Pure", [], ["ProtoPure"], Theory.pure),
wenzelm
parents: 3604
diff changeset
    89
     ("CPure", [], ["ProtoPure"], Theory.cpure)]));
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    90
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    91
3976
wenzelm
parents: 3604
diff changeset
    92
(* retrieve info *)
wenzelm
parents: 3604
diff changeset
    93
wenzelm
parents: 3604
diff changeset
    94
fun err_not_stored name =
wenzelm
parents: 3604
diff changeset
    95
  error ("Theory " ^ name ^ " not stored by loader");
wenzelm
parents: 3604
diff changeset
    96
wenzelm
parents: 3604
diff changeset
    97
fun get_thyinfo name = Symtab.lookup (! loaded_thys, name);
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    98
3976
wenzelm
parents: 3604
diff changeset
    99
fun the_thyinfo name =
wenzelm
parents: 3604
diff changeset
   100
  (case get_thyinfo name of
wenzelm
parents: 3604
diff changeset
   101
    Some info => info
wenzelm
parents: 3604
diff changeset
   102
  | None => err_not_stored name);
wenzelm
parents: 3604
diff changeset
   103
wenzelm
parents: 3604
diff changeset
   104
fun thyinfo_of_sign sg =
wenzelm
parents: 3604
diff changeset
   105
  let val name = Sign.name_of sg
wenzelm
parents: 3604
diff changeset
   106
  in (name, the_thyinfo name) end;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   107
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   108
3976
wenzelm
parents: 3604
diff changeset
   109
(*path where theory's files are located*)
wenzelm
parents: 3604
diff changeset
   110
val path_of = #path o the_thyinfo;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   111
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   112
3976
wenzelm
parents: 3604
diff changeset
   113
(*try to get the theory object corresponding to a given signature*)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   114
fun theory_of_sign sg =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   115
  (case thyinfo_of_sign sg of
3976
wenzelm
parents: 3604
diff changeset
   116
    (_, {theory = Some thy, ...}) => thy
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   117
  | _ => sys_error "theory_of_sign");
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   118
3976
wenzelm
parents: 3604
diff changeset
   119
(*try to get the theory object corresponding to a given theorem*)
wenzelm
parents: 3604
diff changeset
   120
val theory_of_thm = theory_of_sign o #sign o rep_thm;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   121
3976
wenzelm
parents: 3604
diff changeset
   122
(*get all direct descendants of a theory*)
wenzelm
parents: 3604
diff changeset
   123
fun children_of t =
wenzelm
parents: 3604
diff changeset
   124
  (case get_thyinfo t of
wenzelm
parents: 3604
diff changeset
   125
    Some ({children, ...}) => children
wenzelm
parents: 3604
diff changeset
   126
  | None => []);
wenzelm
parents: 3604
diff changeset
   127
wenzelm
parents: 3604
diff changeset
   128
(*get all direct ancestors of a theory*)
wenzelm
parents: 3604
diff changeset
   129
fun parents_of_name t =
wenzelm
parents: 3604
diff changeset
   130
  (case get_thyinfo t of
wenzelm
parents: 3604
diff changeset
   131
    Some ({parents, ...}) => parents
wenzelm
parents: 3604
diff changeset
   132
  | None => []);
wenzelm
parents: 3604
diff changeset
   133
wenzelm
parents: 3604
diff changeset
   134
(*get theory object for a loaded theory*)
wenzelm
parents: 3604
diff changeset
   135
fun theory_of name =
wenzelm
parents: 3604
diff changeset
   136
  (case get_thyinfo name of
wenzelm
parents: 3604
diff changeset
   137
    Some ({theory = Some t, ...}) => t
wenzelm
parents: 3604
diff changeset
   138
  | _ => err_not_stored name);
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   139
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   140
3976
wenzelm
parents: 3604
diff changeset
   141
(*invoke every put method stored in a theory's methods table to initialize
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   142
  the state of user defined variables*)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   143
fun put_thydata first tname =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   144
  let val (methods, data) = 
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   145
        case get_thyinfo tname of
3976
wenzelm
parents: 3604
diff changeset
   146
            Some ({methods, data, ...}) =>
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   147
              (methods, Symtab.dest ((if first then fst else snd) data))
3976
wenzelm
parents: 3604
diff changeset
   148
          | None => err_not_stored tname;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   149
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   150
      fun put_data (id, date) =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   151
            case Symtab.lookup (methods, id) of
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   152
                Some (ThyMethods {put, ...}) => put date
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   153
              | None => error ("No method defined for theory data \"" ^
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   154
                               id ^ "\"");
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   155
  in seq put_data data end;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   156
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   157
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   158
val set_current_thy = put_thydata false;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   159
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   160
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   161
(*Change theory object for an existent item of loaded_thys*)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   162
fun store_theory (thy, tname) =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   163
  let val tinfo = case Symtab.lookup (!loaded_thys, tname) of
3976
wenzelm
parents: 3604
diff changeset
   164
               Some ({path, children, parents, thy_time, ml_time, thms,
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   165
                              methods, data, ...}) =>
3976
wenzelm
parents: 3604
diff changeset
   166
                 {path = path, children = children, parents = parents,
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   167
                          thy_time = thy_time, ml_time = ml_time,
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   168
                          theory = Some thy, thms = thms,
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   169
                          methods = methods, data = data}
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   170
             | None => error ("store_theory: theory " ^ tname ^ " not found");
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   171
  in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   172
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   173
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   174
(*** Misc functions ***)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   175
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   176
(*Add data handling methods to theory*)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   177
fun add_thydata tname (new_methods as (id, ThyMethods {get, ...})) =
3976
wenzelm
parents: 3604
diff changeset
   178
  let val {path, children, parents, thy_time, ml_time, theory,
wenzelm
parents: 3604
diff changeset
   179
                   thms, methods, data} = the_thyinfo tname;
wenzelm
parents: 3604
diff changeset
   180
  in loaded_thys := Symtab.update ((tname, {path = path,
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   181
       children = children, parents = parents, thy_time = thy_time,
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   182
       ml_time = ml_time, theory = theory, thms = thms,
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   183
       methods = Symtab.update (new_methods, methods), data = data}),
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   184
       !loaded_thys)
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   185
  end;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   186
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   187
3976
wenzelm
parents: 3604
diff changeset
   188
(*retrieve data associated with theory*)
wenzelm
parents: 3604
diff changeset
   189
fun get_thydata name id =
wenzelm
parents: 3604
diff changeset
   190
  Symtab.lookup (snd (#data (the_thyinfo name)), id);
wenzelm
parents: 3604
diff changeset
   191
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   192
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   193
end;