berghofe@3604
|
1 |
(* Title: Pure/Thy/thy_info.ML
|
berghofe@3604
|
2 |
ID: $Id$
|
berghofe@3604
|
3 |
Author: Carsten Clasohm and Markus Wenzel and Sonia Mahjoub and
|
berghofe@3604
|
4 |
Tobias Nipkow and L C Paulson
|
berghofe@3604
|
5 |
Copyright 1994 TU Muenchen
|
berghofe@3604
|
6 |
|
berghofe@3604
|
7 |
Functions for storing and retrieving arbitrary theory information.
|
berghofe@3604
|
8 |
*)
|
berghofe@3604
|
9 |
|
berghofe@3604
|
10 |
(*Types for theory storage*)
|
berghofe@3604
|
11 |
|
berghofe@3604
|
12 |
(*Functions to handle arbitrary data added by the user; type "exn" is used
|
berghofe@3604
|
13 |
to store data*)
|
berghofe@3604
|
14 |
datatype thy_methods =
|
berghofe@3604
|
15 |
ThyMethods of {merge: exn list -> exn, put: exn -> unit, get: unit -> exn};
|
berghofe@3604
|
16 |
|
berghofe@3604
|
17 |
datatype thy_info =
|
berghofe@3604
|
18 |
ThyInfo of {path: string,
|
berghofe@3604
|
19 |
children: string list, parents: string list,
|
berghofe@3604
|
20 |
thy_time: string option, ml_time: string option,
|
berghofe@3604
|
21 |
theory: theory option, thms: thm Symtab.table,
|
berghofe@3604
|
22 |
methods: thy_methods Symtab.table,
|
berghofe@3604
|
23 |
data: exn Symtab.table * exn Symtab.table};
|
berghofe@3604
|
24 |
(*thy_time, ml_time = None theory file has not been read yet
|
berghofe@3604
|
25 |
= Some "" theory was read but has either been marked
|
berghofe@3604
|
26 |
as outdated or there is no such file for
|
berghofe@3604
|
27 |
this theory (see e.g. 'virtual' theories
|
berghofe@3604
|
28 |
like Pure or theories without a ML file)
|
berghofe@3604
|
29 |
theory = None theory has not been read yet
|
berghofe@3604
|
30 |
|
berghofe@3604
|
31 |
parents: While 'children' contains all theories the theory depends
|
berghofe@3604
|
32 |
on (i.e. also ones quoted in the .thy file),
|
berghofe@3604
|
33 |
'parents' only contains the theories which were used to form
|
berghofe@3604
|
34 |
the base of this theory.
|
berghofe@3604
|
35 |
|
berghofe@3604
|
36 |
methods: three methods for each user defined data;
|
berghofe@3604
|
37 |
merge: merges data of ancestor theories
|
berghofe@3604
|
38 |
put: retrieves data from loaded_thys and stores it somewhere
|
berghofe@3604
|
39 |
get: retrieves data from somewhere and stores it
|
berghofe@3604
|
40 |
in loaded_thys
|
berghofe@3604
|
41 |
data: user defined data; exn is used to allow arbitrary types;
|
berghofe@3604
|
42 |
first element of pairs contains result that get returned after
|
berghofe@3604
|
43 |
thy file was read, second element after ML file was read;
|
berghofe@3604
|
44 |
if ML files has not been read, second element is identical to
|
berghofe@3604
|
45 |
first one because get_thydata, which is meant to return the
|
berghofe@3604
|
46 |
latest data, always accesses the 2nd element
|
berghofe@3604
|
47 |
*)
|
berghofe@3604
|
48 |
|
berghofe@3604
|
49 |
|
berghofe@3604
|
50 |
signature THY_INFO =
|
berghofe@3604
|
51 |
sig
|
berghofe@3604
|
52 |
val loaded_thys : thy_info Symtab.table ref
|
berghofe@3604
|
53 |
val store_theory : theory * string -> unit
|
berghofe@3604
|
54 |
|
berghofe@3604
|
55 |
val theory_of : string -> theory
|
berghofe@3604
|
56 |
val theory_of_sign : Sign.sg -> theory
|
berghofe@3604
|
57 |
val theory_of_thm : thm -> theory
|
berghofe@3604
|
58 |
val children_of : string -> string list
|
berghofe@3604
|
59 |
val parents_of_name: string -> string list
|
berghofe@3604
|
60 |
val thyname_of_sign: Sign.sg -> string
|
berghofe@3604
|
61 |
val thyinfo_of_sign: Sign.sg -> string * thy_info
|
berghofe@3604
|
62 |
|
berghofe@3604
|
63 |
val add_thydata : string -> string * thy_methods -> unit
|
berghofe@3604
|
64 |
val get_thydata : string -> string -> exn option
|
berghofe@3604
|
65 |
val put_thydata : bool -> string -> unit
|
berghofe@3604
|
66 |
val set_current_thy: string -> unit
|
berghofe@3604
|
67 |
val get_thyinfo : string -> thy_info option
|
berghofe@3604
|
68 |
|
berghofe@3604
|
69 |
val path_of : string -> string
|
berghofe@3604
|
70 |
end;
|
berghofe@3604
|
71 |
|
berghofe@3604
|
72 |
|
berghofe@3604
|
73 |
|
berghofe@3604
|
74 |
structure ThyInfo: THY_INFO =
|
berghofe@3604
|
75 |
struct
|
berghofe@3604
|
76 |
|
berghofe@3604
|
77 |
|
berghofe@3604
|
78 |
val loaded_thys =
|
berghofe@3604
|
79 |
ref (Symtab.make [("ProtoPure",
|
berghofe@3604
|
80 |
ThyInfo {path = "",
|
berghofe@3604
|
81 |
children = ["Pure", "CPure"], parents = [],
|
berghofe@3604
|
82 |
thy_time = Some "", ml_time = Some "",
|
berghofe@3604
|
83 |
theory = Some proto_pure_thy,
|
berghofe@3604
|
84 |
thms = Symtab.null, methods = Symtab.null,
|
berghofe@3604
|
85 |
data = (Symtab.null, Symtab.null)}),
|
berghofe@3604
|
86 |
("Pure",
|
berghofe@3604
|
87 |
ThyInfo {path = "", children = [],
|
berghofe@3604
|
88 |
parents = ["ProtoPure"],
|
berghofe@3604
|
89 |
thy_time = Some "", ml_time = Some "",
|
berghofe@3604
|
90 |
theory = Some pure_thy, thms = Symtab.null,
|
berghofe@3604
|
91 |
methods = Symtab.null,
|
berghofe@3604
|
92 |
data = (Symtab.null, Symtab.null)}),
|
berghofe@3604
|
93 |
("CPure",
|
berghofe@3604
|
94 |
ThyInfo {path = "",
|
berghofe@3604
|
95 |
children = [], parents = ["ProtoPure"],
|
berghofe@3604
|
96 |
thy_time = Some "", ml_time = Some "",
|
berghofe@3604
|
97 |
theory = Some cpure_thy,
|
berghofe@3604
|
98 |
thms = Symtab.null,
|
berghofe@3604
|
99 |
methods = Symtab.null,
|
berghofe@3604
|
100 |
data = (Symtab.null, Symtab.null)})
|
berghofe@3604
|
101 |
]);
|
berghofe@3604
|
102 |
|
berghofe@3604
|
103 |
|
berghofe@3604
|
104 |
(*Get thy_info for a loaded theory *)
|
berghofe@3604
|
105 |
fun get_thyinfo tname = Symtab.lookup (!loaded_thys, tname);
|
berghofe@3604
|
106 |
|
berghofe@3604
|
107 |
(*Get path where theory's files are located*)
|
berghofe@3604
|
108 |
fun path_of tname =
|
berghofe@3604
|
109 |
let val ThyInfo {path, ...} = the (get_thyinfo tname)
|
berghofe@3604
|
110 |
in path end;
|
berghofe@3604
|
111 |
|
berghofe@3604
|
112 |
|
berghofe@3604
|
113 |
(*Guess the name of a theory object
|
berghofe@3604
|
114 |
(First the quick way by looking at the stamps; if that doesn't work,
|
berghofe@3604
|
115 |
we search loaded_thys for the first theory which fits.)
|
berghofe@3604
|
116 |
*)
|
berghofe@3604
|
117 |
fun thyname_of_sign sg =
|
berghofe@3604
|
118 |
let val ref xname = hd (#stamps (Sign.rep_sg sg));
|
berghofe@3604
|
119 |
val opt_info = get_thyinfo xname;
|
berghofe@3604
|
120 |
|
berghofe@3604
|
121 |
fun eq_sg (ThyInfo {theory = None, ...}) = false
|
berghofe@3604
|
122 |
| eq_sg (ThyInfo {theory = Some thy, ...}) = Sign.eq_sg (sg,sign_of thy);
|
berghofe@3604
|
123 |
|
berghofe@3604
|
124 |
val show_sg = Pretty.str_of o Sign.pretty_sg;
|
berghofe@3604
|
125 |
in
|
berghofe@3604
|
126 |
if is_some opt_info andalso eq_sg (the opt_info) then xname
|
berghofe@3604
|
127 |
else
|
berghofe@3604
|
128 |
(case Symtab.find_first (eq_sg o snd) (!loaded_thys) of
|
berghofe@3604
|
129 |
Some (name, _) => name
|
berghofe@3604
|
130 |
| None => error ("Theory " ^ show_sg sg ^ " not stored by loader"))
|
berghofe@3604
|
131 |
end;
|
berghofe@3604
|
132 |
|
berghofe@3604
|
133 |
|
berghofe@3604
|
134 |
(*Guess to which theory a signature belongs and return it's thy_info*)
|
berghofe@3604
|
135 |
fun thyinfo_of_sign sg =
|
berghofe@3604
|
136 |
let val name = thyname_of_sign sg;
|
berghofe@3604
|
137 |
in (name, the (get_thyinfo name)) end;
|
berghofe@3604
|
138 |
|
berghofe@3604
|
139 |
|
berghofe@3604
|
140 |
(*Try to get the theory object corresponding to a given signature*)
|
berghofe@3604
|
141 |
fun theory_of_sign sg =
|
berghofe@3604
|
142 |
(case thyinfo_of_sign sg of
|
berghofe@3604
|
143 |
(_, ThyInfo {theory = Some thy, ...}) => thy
|
berghofe@3604
|
144 |
| _ => sys_error "theory_of_sign");
|
berghofe@3604
|
145 |
|
berghofe@3604
|
146 |
|
berghofe@3604
|
147 |
(*Try to get the theory object corresponding to a given theorem*)
|
berghofe@3604
|
148 |
val theory_of_thm = theory_of_sign o #sign o rep_thm;
|
berghofe@3604
|
149 |
|
berghofe@3604
|
150 |
|
berghofe@3604
|
151 |
(*Get all direct descendants of a theory*)
|
berghofe@3604
|
152 |
fun children_of t =
|
berghofe@3604
|
153 |
case get_thyinfo t of Some (ThyInfo {children, ...}) => children
|
berghofe@3604
|
154 |
| None => [];
|
berghofe@3604
|
155 |
|
berghofe@3604
|
156 |
|
berghofe@3604
|
157 |
(*Get all direct ancestors of a theory*)
|
berghofe@3604
|
158 |
fun parents_of_name t =
|
berghofe@3604
|
159 |
case get_thyinfo t of Some (ThyInfo {parents, ...}) => parents
|
berghofe@3604
|
160 |
| None => [];
|
berghofe@3604
|
161 |
|
berghofe@3604
|
162 |
|
berghofe@3604
|
163 |
(*Get theory object for a loaded theory *)
|
berghofe@3604
|
164 |
fun theory_of name =
|
berghofe@3604
|
165 |
case get_thyinfo name of Some (ThyInfo {theory = Some t, ...}) => t
|
berghofe@3604
|
166 |
| _ => error ("Theory " ^ name ^
|
berghofe@3604
|
167 |
" not stored by loader");
|
berghofe@3604
|
168 |
|
berghofe@3604
|
169 |
|
berghofe@3604
|
170 |
(*Invoke every put method stored in a theory's methods table to initialize
|
berghofe@3604
|
171 |
the state of user defined variables*)
|
berghofe@3604
|
172 |
fun put_thydata first tname =
|
berghofe@3604
|
173 |
let val (methods, data) =
|
berghofe@3604
|
174 |
case get_thyinfo tname of
|
berghofe@3604
|
175 |
Some (ThyInfo {methods, data, ...}) =>
|
berghofe@3604
|
176 |
(methods, Symtab.dest ((if first then fst else snd) data))
|
berghofe@3604
|
177 |
| None => error ("Theory " ^ tname ^ " not stored by loader");
|
berghofe@3604
|
178 |
|
berghofe@3604
|
179 |
fun put_data (id, date) =
|
berghofe@3604
|
180 |
case Symtab.lookup (methods, id) of
|
berghofe@3604
|
181 |
Some (ThyMethods {put, ...}) => put date
|
berghofe@3604
|
182 |
| None => error ("No method defined for theory data \"" ^
|
berghofe@3604
|
183 |
id ^ "\"");
|
berghofe@3604
|
184 |
in seq put_data data end;
|
berghofe@3604
|
185 |
|
berghofe@3604
|
186 |
|
berghofe@3604
|
187 |
val set_current_thy = put_thydata false;
|
berghofe@3604
|
188 |
|
berghofe@3604
|
189 |
|
berghofe@3604
|
190 |
(*Change theory object for an existent item of loaded_thys*)
|
berghofe@3604
|
191 |
fun store_theory (thy, tname) =
|
berghofe@3604
|
192 |
let val tinfo = case Symtab.lookup (!loaded_thys, tname) of
|
berghofe@3604
|
193 |
Some (ThyInfo {path, children, parents, thy_time, ml_time, thms,
|
berghofe@3604
|
194 |
methods, data, ...}) =>
|
berghofe@3604
|
195 |
ThyInfo {path = path, children = children, parents = parents,
|
berghofe@3604
|
196 |
thy_time = thy_time, ml_time = ml_time,
|
berghofe@3604
|
197 |
theory = Some thy, thms = thms,
|
berghofe@3604
|
198 |
methods = methods, data = data}
|
berghofe@3604
|
199 |
| None => error ("store_theory: theory " ^ tname ^ " not found");
|
berghofe@3604
|
200 |
in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end;
|
berghofe@3604
|
201 |
|
berghofe@3604
|
202 |
|
berghofe@3604
|
203 |
(*** Misc functions ***)
|
berghofe@3604
|
204 |
|
berghofe@3604
|
205 |
(*Add data handling methods to theory*)
|
berghofe@3604
|
206 |
fun add_thydata tname (new_methods as (id, ThyMethods {get, ...})) =
|
berghofe@3604
|
207 |
let val ThyInfo {path, children, parents, thy_time, ml_time, theory,
|
berghofe@3604
|
208 |
thms, methods, data} =
|
berghofe@3604
|
209 |
case get_thyinfo tname of
|
berghofe@3604
|
210 |
Some ti => ti
|
berghofe@3604
|
211 |
| None => error ("Theory " ^ tname ^ " not stored by loader");
|
berghofe@3604
|
212 |
in loaded_thys := Symtab.update ((tname, ThyInfo {path = path,
|
berghofe@3604
|
213 |
children = children, parents = parents, thy_time = thy_time,
|
berghofe@3604
|
214 |
ml_time = ml_time, theory = theory, thms = thms,
|
berghofe@3604
|
215 |
methods = Symtab.update (new_methods, methods), data = data}),
|
berghofe@3604
|
216 |
!loaded_thys)
|
berghofe@3604
|
217 |
end;
|
berghofe@3604
|
218 |
|
berghofe@3604
|
219 |
|
berghofe@3604
|
220 |
(*Retrieve data associated with theory*)
|
berghofe@3604
|
221 |
fun get_thydata tname id =
|
berghofe@3604
|
222 |
let val d2 = case get_thyinfo tname of
|
berghofe@3604
|
223 |
Some (ThyInfo {data, ...}) => snd data
|
berghofe@3604
|
224 |
| None => error ("Theory " ^ tname ^ " not stored by loader");
|
berghofe@3604
|
225 |
in Symtab.lookup (d2, id) end;
|
berghofe@3604
|
226 |
|
berghofe@3604
|
227 |
end;
|