author | nipkow |
Fri, 09 Oct 1998 15:28:04 +0200 | |
changeset 5635 | b7d6b7f66131 |
parent 5633 | fb7fa1b154c4 |
child 5642 | 1b3e48bdbb93 |
permissions | -rw-r--r-- |
19 | 1 |
(* Title: Pure/sign.ML |
0 | 2 |
ID: $Id$ |
251 | 3 |
Author: Lawrence C Paulson and Markus Wenzel |
0 | 4 |
|
251 | 5 |
The abstract type "sg" of signatures. |
0 | 6 |
*) |
7 |
||
3956 | 8 |
(*base names*) |
9 |
type bstring = string; |
|
10 |
type bclass = class; |
|
11 |
(*external forms -- partially qualified names*) |
|
3805 | 12 |
type xstring = string; |
13 |
type xclass = class; |
|
14 |
type xsort = sort; |
|
15 |
type xtyp = typ; |
|
16 |
type xterm = term; |
|
17 |
||
19 | 18 |
signature SIGN = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
19 |
sig |
1501 | 20 |
type sg |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
21 |
type sg_ref |
4256 | 22 |
type data |
2197 | 23 |
val rep_sg: sg -> |
3975 | 24 |
{self: sg_ref, |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
25 |
tsig: Type.type_sig, |
2197 | 26 |
const_tab: typ Symtab.table, |
27 |
syn: Syntax.syntax, |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
28 |
path: string list, |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
29 |
spaces: (string * NameSpace.T) list, |
4256 | 30 |
data: data} |
3975 | 31 |
val name_of: sg -> string |
32 |
val stamp_names_of: sg -> string list |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
33 |
val tsig_of: sg -> Type.type_sig |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
34 |
val deref: sg_ref -> sg |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
35 |
val self_ref: sg -> sg_ref |
1501 | 36 |
val subsig: sg * sg -> bool |
37 |
val eq_sg: sg * sg -> bool |
|
38 |
val same_sg: sg * sg -> bool |
|
39 |
val is_draft: sg -> bool |
|
4951 | 40 |
val is_stale: sg -> bool |
1501 | 41 |
val const_type: sg -> string -> typ option |
42 |
val classes: sg -> class list |
|
4844 | 43 |
val defaultS: sg -> sort |
1501 | 44 |
val subsort: sg -> sort * sort -> bool |
45 |
val nodup_Vars: term -> unit |
|
46 |
val norm_sort: sg -> sort -> sort |
|
47 |
val nonempty_sort: sg -> sort list -> sort -> bool |
|
4568 | 48 |
val of_sort: sg -> typ * sort -> bool |
3810 | 49 |
val classK: string |
50 |
val typeK: string |
|
51 |
val constK: string |
|
3956 | 52 |
val full_name: sg -> bstring -> string |
4844 | 53 |
val full_name_path: sg -> string -> bstring -> string |
3956 | 54 |
val base_name: string -> bstring |
3810 | 55 |
val intern: sg -> string -> xstring -> string |
56 |
val extern: sg -> string -> string -> xstring |
|
3937 | 57 |
val cond_extern: sg -> string -> string -> xstring |
3805 | 58 |
val intern_class: sg -> xclass -> class |
59 |
val intern_tycon: sg -> xstring -> string |
|
60 |
val intern_const: sg -> xstring -> string |
|
3937 | 61 |
val intern_sort: sg -> xsort -> sort |
62 |
val intern_typ: sg -> xtyp -> typ |
|
63 |
val intern_term: sg -> xterm -> term |
|
64 |
val intern_tycons: sg -> xtyp -> typ |
|
1501 | 65 |
val pretty_sg: sg -> Pretty.T |
4051 | 66 |
val str_of_sg: sg -> string |
1501 | 67 |
val pprint_sg: sg -> pprint_args -> unit |
68 |
val pretty_term: sg -> term -> Pretty.T |
|
69 |
val pretty_typ: sg -> typ -> Pretty.T |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
70 |
val pretty_sort: sg -> sort -> Pretty.T |
4249 | 71 |
val pretty_classrel: sg -> class * class -> Pretty.T |
72 |
val pretty_arity: sg -> string * sort list * sort -> Pretty.T |
|
1501 | 73 |
val string_of_term: sg -> term -> string |
74 |
val string_of_typ: sg -> typ -> string |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
75 |
val string_of_sort: sg -> sort -> string |
3855 | 76 |
val str_of_sort: sg -> sort -> string |
77 |
val str_of_classrel: sg -> class * class -> string |
|
78 |
val str_of_arity: sg -> string * sort list * sort -> string |
|
1501 | 79 |
val pprint_term: sg -> term -> pprint_args -> unit |
80 |
val pprint_typ: sg -> typ -> pprint_args -> unit |
|
81 |
val certify_typ: sg -> typ -> typ |
|
82 |
val certify_term: sg -> term -> term * typ * int |
|
4227 | 83 |
val read_raw_typ: sg * (indexname -> sort option) -> string -> typ |
1501 | 84 |
val read_typ: sg * (indexname -> sort option) -> string -> typ |
85 |
val infer_types: sg -> (indexname -> typ option) -> |
|
86 |
(indexname -> sort option) -> string list -> bool |
|
4249 | 87 |
-> xterm list * typ -> term * (indexname * typ) list |
88 |
val infer_types_simult: sg -> (indexname -> typ option) -> |
|
89 |
(indexname -> sort option) -> string list -> bool |
|
90 |
-> (xterm list * typ) list -> term list * (indexname * typ) list |
|
3956 | 91 |
val add_classes: (bclass * xclass list) list -> sg -> sg |
92 |
val add_classes_i: (bclass * class list) list -> sg -> sg |
|
3805 | 93 |
val add_classrel: (xclass * xclass) list -> sg -> sg |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
94 |
val add_classrel_i: (class * class) list -> sg -> sg |
3805 | 95 |
val add_defsort: xsort -> sg -> sg |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
96 |
val add_defsort_i: sort -> sg -> sg |
3956 | 97 |
val add_types: (bstring * int * mixfix) list -> sg -> sg |
4844 | 98 |
val add_nonterminals: bstring list -> sg -> sg |
3956 | 99 |
val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg |
100 |
val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg |
|
3805 | 101 |
val add_arities: (xstring * xsort list * xsort) list -> sg -> sg |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
102 |
val add_arities_i: (string * sort list * sort) list -> sg -> sg |
3956 | 103 |
val add_consts: (bstring * string * mixfix) list -> sg -> sg |
104 |
val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg |
|
105 |
val add_syntax: (bstring * string * mixfix) list -> sg -> sg |
|
106 |
val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg |
|
107 |
val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg |
|
108 |
val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg |
|
1501 | 109 |
val add_trfuns: |
4344 | 110 |
(string * (ast list -> ast)) list * |
111 |
(string * (term list -> term)) list * |
|
112 |
(string * (term list -> term)) list * |
|
113 |
(string * (ast list -> ast)) list -> sg -> sg |
|
2385 | 114 |
val add_trfunsT: |
4344 | 115 |
(string * (bool -> typ -> term list -> term)) list -> sg -> sg |
2693 | 116 |
val add_tokentrfuns: |
117 |
(string * string * (string -> string * int)) list -> sg -> sg |
|
4619 | 118 |
val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg |
1810
0eef167ebe1b
Translation infixes <->, etc., no longer available at top-level
paulson
parents:
1580
diff
changeset
|
119 |
val add_trrules_i: ast Syntax.trrule list -> sg -> sg |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
120 |
val add_path: string -> sg -> sg |
3810 | 121 |
val add_space: string * string list -> sg -> sg |
1501 | 122 |
val add_name: string -> sg -> sg |
4256 | 123 |
val data_kinds: data -> string list |
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
124 |
val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
125 |
(Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
126 |
val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
127 |
val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
128 |
val print_data: Object.kind -> sg -> unit |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
129 |
val merge_refs: sg_ref * sg_ref -> sg_ref |
4627 | 130 |
val merge: sg * sg -> sg |
3975 | 131 |
val prep_ext: sg -> sg |
4627 | 132 |
val nontriv_merge: sg * sg -> sg |
3995 | 133 |
val pre_pure: sg |
1501 | 134 |
val const_of_class: class -> string |
135 |
val class_of_const: string -> class |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
136 |
end; |
0 | 137 |
|
4249 | 138 |
structure Sign: SIGN = |
143 | 139 |
struct |
0 | 140 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
141 |
|
251 | 142 |
(** datatype sg **) |
143 |
||
4256 | 144 |
(* types sg, data, sg_ref *) |
145 |
||
19 | 146 |
datatype sg = |
3975 | 147 |
Sg of |
148 |
{id: string ref, (*id*) |
|
149 |
stamps: string ref list} * (*unique theory indentifier*) |
|
150 |
{self: sg_ref, (*mutable self reference*) |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
151 |
tsig: Type.type_sig, (*order-sorted signature of types*) |
3805 | 152 |
const_tab: typ Symtab.table, (*type schemes of constants*) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
153 |
syn: Syntax.syntax, (*syntax for parsing and printing*) |
3975 | 154 |
path: string list, (*current name space entry prefix*) |
155 |
spaces: (string * NameSpace.T) list, (*name spaces for consts, types etc.*) |
|
4256 | 156 |
data: data} (*anytype data*) |
157 |
and data = |
|
158 |
Data of |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
159 |
(Object.kind * (*kind (for authorization)*) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
160 |
(Object.T * (*value*) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
161 |
((Object.T -> Object.T) * (*prepare extend method*) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
162 |
(Object.T * Object.T -> Object.T) * (*merge and prepare extend method*) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
163 |
(sg -> Object.T -> unit)))) (*print method*) |
4256 | 164 |
Symtab.table |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
165 |
and sg_ref = |
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
166 |
SgRef of sg ref option; |
0 | 167 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
168 |
(*make signature*) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
169 |
fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) = |
3975 | 170 |
Sg ({id = id, stamps = stamps}, {self = self, tsig = tsig, const_tab = const_tab, |
171 |
syn = syn, path = path, spaces = spaces, data = data}); |
|
172 |
||
173 |
||
4256 | 174 |
(* basic operations *) |
3975 | 175 |
|
176 |
fun rep_sg (Sg (_, args)) = args; |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
177 |
|
3975 | 178 |
(*show stamps*) |
179 |
fun stamp_names_of (Sg ({stamps, ...}, _)) = rev (map ! stamps); |
|
180 |
fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg); |
|
181 |
val str_of_sg = Pretty.str_of o pretty_sg; |
|
182 |
val pprint_sg = Pretty.pprint o pretty_sg; |
|
183 |
||
402 | 184 |
val tsig_of = #tsig o rep_sg; |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
185 |
|
3975 | 186 |
fun const_type (Sg (_, {const_tab, ...})) c = Symtab.lookup (const_tab, c); |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
187 |
|
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
188 |
|
3995 | 189 |
(* id and self *) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
190 |
|
3975 | 191 |
fun check_stale (sg as Sg ({id, ...}, |
192 |
{self = SgRef (Some (ref (Sg ({id = id', ...}, _)))), ...})) = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
193 |
if id = id' then sg |
3975 | 194 |
else raise TERM ("Stale signature: " ^ str_of_sg sg, []) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
195 |
| check_stale _ = sys_error "Sign.check_stale"; |
0 | 196 |
|
4951 | 197 |
fun is_stale sg = (check_stale sg; false) handle TERM _ => true; |
198 |
||
3995 | 199 |
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale sg; self); |
200 |
||
201 |
fun deref (SgRef (Some (ref sg))) = sg |
|
202 |
| deref (SgRef None) = sys_error "Sign.deref"; |
|
203 |
||
3975 | 204 |
fun name_of (sg as Sg ({id = ref name, ...}, _)) = |
4844 | 205 |
if name = "" orelse ord name = ord "#" then |
3975 | 206 |
raise TERM ("Nameless signature " ^ str_of_sg sg, []) |
207 |
else name; |
|
208 |
||
206
0d624d1ba9cc
added subsig: sg * sg -> bool to test if one signature is contained in another.
nipkow
parents:
200
diff
changeset
|
209 |
|
2979 | 210 |
(* inclusion and equality *) |
2180
934572a94139
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2144
diff
changeset
|
211 |
|
2185 | 212 |
local |
213 |
(*avoiding polymorphic equality: factor 10 speedup*) |
|
214 |
fun mem_stamp (_:string ref, []) = false |
|
215 |
| mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys); |
|
216 |
||
217 |
fun subset_stamp ([], ys) = true |
|
218 |
| subset_stamp (x :: xs, ys) = |
|
219 |
mem_stamp (x, ys) andalso subset_stamp (xs, ys); |
|
2180
934572a94139
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2144
diff
changeset
|
220 |
|
2185 | 221 |
(*fast partial test*) |
222 |
fun fast_sub ([]: string ref list, _) = true |
|
223 |
| fast_sub (_, []) = false |
|
224 |
| fast_sub (x :: xs, y :: ys) = |
|
225 |
if x = y then fast_sub (xs, ys) |
|
226 |
else fast_sub (x :: xs, ys); |
|
227 |
in |
|
3975 | 228 |
fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
229 |
(check_stale sg1; check_stale sg2; id1 = id2); |
2185 | 230 |
|
3975 | 231 |
fun subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
232 |
eq_sg (sg1, sg2) orelse subset_stamp (s1, s2); |
2180
934572a94139
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2144
diff
changeset
|
233 |
|
3975 | 234 |
fun fast_subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
235 |
eq_sg (sg1, sg2) orelse fast_sub (s1, s2); |
2185 | 236 |
end; |
237 |
||
402 | 238 |
|
2185 | 239 |
(*test if same theory names are contained in signatures' stamps, |
240 |
i.e. if signatures belong to same theory but not necessarily to the |
|
241 |
same version of it*) |
|
3975 | 242 |
fun same_sg (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
243 |
eq_sg (sg1, sg2) orelse eq_set_string (pairself (map (op !)) (s1, s2)); |
2185 | 244 |
|
245 |
(*test for drafts*) |
|
4844 | 246 |
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) = name = "" orelse ord name = ord "#"; |
386 | 247 |
|
0 | 248 |
|
4568 | 249 |
(* classes and sorts *) |
250 |
||
251 |
val classes = #classes o Type.rep_tsig o tsig_of; |
|
252 |
||
4844 | 253 |
val defaultS = Type.defaultS o tsig_of; |
4568 | 254 |
val subsort = Type.subsort o tsig_of; |
255 |
val norm_sort = Type.norm_sort o tsig_of; |
|
256 |
val nonempty_sort = Type.nonempty_sort o tsig_of; |
|
257 |
||
258 |
fun of_sort (Sg (_, {tsig, ...})) = |
|
259 |
Sorts.of_sort (#classrel (Type.rep_tsig tsig)) (#arities (Type.rep_tsig tsig)); |
|
260 |
||
261 |
||
4256 | 262 |
|
263 |
(** signature data **) |
|
264 |
||
265 |
(* errors *) |
|
266 |
||
4261 | 267 |
fun of_theory sg = "\nof theory " ^ str_of_sg sg; |
4256 | 268 |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
269 |
fun err_inconsistent kinds = |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
270 |
error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data"); |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
271 |
|
4256 | 272 |
fun err_method name kind = |
4892 | 273 |
error ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); |
4256 | 274 |
|
275 |
fun err_dup_init sg kind = |
|
276 |
error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg); |
|
277 |
||
278 |
fun err_uninit sg kind = |
|
279 |
error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg); |
|
280 |
||
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
281 |
fun err_access sg kind = |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
282 |
error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg); |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
283 |
|
4256 | 284 |
|
285 |
(* prepare data *) |
|
286 |
||
4489 | 287 |
val empty_data = Data Symtab.empty; |
4256 | 288 |
|
289 |
fun merge_data (Data tab1, Data tab2) = |
|
290 |
let |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
291 |
val data1 = map snd (Symtab.dest tab1); |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
292 |
val data2 = map snd (Symtab.dest tab2); |
4256 | 293 |
val all_data = data1 @ data2; |
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
294 |
val kinds = gen_distinct Object.eq_kind (map fst all_data); |
4256 | 295 |
|
296 |
fun entry data kind = |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
297 |
(case gen_assoc Object.eq_kind (data, kind) of |
4256 | 298 |
None => [] |
299 |
| Some x => [(kind, x)]); |
|
300 |
||
301 |
fun merge_entries [(kind, (e, mths as (ext, _, _)))] = |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
302 |
(kind, (ext e handle _ => err_method "prep_ext" (Object.name_of_kind kind), mths)) |
4256 | 303 |
| merge_entries [(kind, (e1, mths as (_, mrg, _))), (_, (e2, _))] = |
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
304 |
(kind, (mrg (e1, e2) handle _ => err_method "merge" (Object.name_of_kind kind), mths)) |
4256 | 305 |
| merge_entries _ = sys_error "merge_entries"; |
306 |
||
307 |
val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds; |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
308 |
val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
309 |
in |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
310 |
Data (Symtab.make data_idx) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
311 |
handle Symtab.DUPS dups => err_inconsistent dups |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
312 |
end; |
4256 | 313 |
|
314 |
fun prep_ext_data data = merge_data (data, empty_data); |
|
315 |
||
316 |
fun init_data_sg sg (Data tab) kind e ext mrg prt = |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
317 |
let val name = Object.name_of_kind kind in |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
318 |
Data (Symtab.update_new ((name, (kind, (e, (ext, mrg, prt)))), tab)) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
319 |
handle Symtab.DUP _ => err_dup_init sg name |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
320 |
end; |
4256 | 321 |
|
322 |
||
323 |
(* access data *) |
|
324 |
||
325 |
fun data_kinds (Data tab) = map fst (Symtab.dest tab); |
|
326 |
||
327 |
fun lookup_data sg tab kind = |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
328 |
let val name = Object.name_of_kind kind in |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
329 |
(case Symtab.lookup (tab, name) of |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
330 |
Some (k, x) => |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
331 |
if Object.eq_kind (kind, k) then x |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
332 |
else err_access sg name |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
333 |
| None => err_uninit sg name) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
334 |
end; |
4256 | 335 |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
336 |
fun get_data kind f (sg as Sg (_, {data = Data tab, ...})) = |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
337 |
let val x = fst (lookup_data sg tab kind) |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
338 |
in f x handle Match => Object.kind_error kind end; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
339 |
|
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
340 |
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) = |
4256 | 341 |
let val (e, (_, _, prt)) = lookup_data sg tab kind |
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
342 |
in prt sg e handle _ => err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) end; |
4256 | 343 |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
344 |
fun put_data_sg sg (Data tab) kind f x = |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
345 |
Data (Symtab.update ((Object.name_of_kind kind, |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
346 |
(kind, (f x, snd (lookup_data sg tab kind)))), tab)); |
4256 | 347 |
|
348 |
||
349 |
||
350 |
(** build signatures **) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
351 |
|
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
352 |
fun ext_stamps stamps (id as ref name) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
353 |
let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
354 |
if exists (equal name o !) stmps then |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
355 |
error ("Theory already contains a " ^ quote name ^ " component") |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
356 |
else id :: stmps |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
357 |
end; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
358 |
|
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
359 |
fun create_sign self stamps name (syn, tsig, ctab, (path, spaces), data) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
360 |
let |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
361 |
val id = ref name; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
362 |
val sign = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
363 |
make_sign (id, self, tsig, ctab, syn, path, spaces, data, ext_stamps stamps id); |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
364 |
in |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
365 |
(case self of |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
366 |
SgRef (Some r) => r := sign |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
367 |
| _ => sys_error "Sign.create_sign"); |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
368 |
sign |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
369 |
end; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
370 |
|
3975 | 371 |
fun extend_sign keep extfun name decls |
372 |
(sg as Sg ({id = _, stamps}, {self, tsig, const_tab, syn, path, spaces, data})) = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
373 |
let |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
374 |
val _ = check_stale sg; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
375 |
val (self', data') = |
3975 | 376 |
if is_draft sg andalso keep then (self, data) |
4256 | 377 |
else (SgRef (Some (ref sg)), prep_ext_data data); |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
378 |
in |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
379 |
create_sign self' stamps name |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
380 |
(extfun (syn, tsig, const_tab, (path, spaces), data') decls) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
381 |
end; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
382 |
|
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
383 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
384 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
385 |
(** name spaces **) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
386 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
387 |
(* kinds *) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
388 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
389 |
val classK = "class"; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
390 |
val typeK = "type"; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
391 |
val constK = "const"; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
392 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
393 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
394 |
(* add and retrieve names *) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
395 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
396 |
fun space_of spaces kind = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
397 |
if_none (assoc (spaces, kind)) NameSpace.empty; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
398 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
399 |
(*input and output of qualified names*) |
3995 | 400 |
fun intrn spaces kind = NameSpace.intern (space_of spaces kind); |
401 |
fun extrn spaces kind = NameSpace.extern (space_of spaces kind); |
|
5175 | 402 |
fun cond_extrn spaces kind = NameSpace.cond_extern (space_of spaces kind); |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
403 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
404 |
(*add names*) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
405 |
fun add_names spaces kind names = |
4489 | 406 |
let val space' = NameSpace.extend (space_of spaces kind, names) in |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
407 |
overwrite (spaces, (kind, space')) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
408 |
end; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
409 |
|
3810 | 410 |
(*make full names*) |
3937 | 411 |
fun full path name = |
4489 | 412 |
if name = "" then error "Attempt to declare empty name \"\"" |
413 |
else if NameSpace.qualified name then |
|
3937 | 414 |
error ("Attempt to declare qualified name " ^ quote name) |
415 |
else NameSpace.pack (path @ [name]); |
|
416 |
||
417 |
(*base name*) |
|
418 |
val base_name = NameSpace.base; |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
419 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
420 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
421 |
(* intern / extern names *) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
422 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
423 |
local |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
424 |
(*prepare mapping of names*) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
425 |
fun mapping f add_xs t = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
426 |
let |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
427 |
fun f' x = let val y = f x in if x = y then None else Some (x, y) end; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
428 |
val table = mapfilter f' (add_xs (t, [])); |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
429 |
fun lookup x = if_none (assoc (table, x)) x; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
430 |
in lookup end; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
431 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
432 |
(*intern / extern typ*) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
433 |
fun trn_typ trn T = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
434 |
T |> map_typ |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
435 |
(mapping (trn classK) add_typ_classes T) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
436 |
(mapping (trn typeK) add_typ_tycons T); |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
437 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
438 |
(*intern / extern term*) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
439 |
fun trn_term trn t = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
440 |
t |> map_term |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
441 |
(mapping (trn classK) add_term_classes t) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
442 |
(mapping (trn typeK) add_term_tycons t) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
443 |
(mapping (trn constK) add_term_consts t); |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
444 |
|
3975 | 445 |
val spaces_of = #spaces o rep_sg; |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
446 |
in |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
447 |
fun intrn_class spaces = intrn spaces classK; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
448 |
fun extrn_class spaces = extrn spaces classK; |
3937 | 449 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
450 |
val intrn_sort = map o intrn_class; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
451 |
val intrn_typ = trn_typ o intrn; |
3937 | 452 |
val intrn_term = trn_term o intrn; |
453 |
||
454 |
val extrn_sort = map o extrn_class; |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
455 |
val extrn_typ = trn_typ o extrn; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
456 |
val extrn_term = trn_term o extrn; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
457 |
|
3805 | 458 |
fun intrn_tycons spaces T = |
459 |
map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T; |
|
460 |
||
3810 | 461 |
val intern = intrn o spaces_of; |
3855 | 462 |
val extern = extrn o spaces_of; |
5175 | 463 |
val cond_extern = cond_extrn o spaces_of; |
3937 | 464 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
465 |
val intern_class = intrn_class o spaces_of; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
466 |
val intern_sort = intrn_sort o spaces_of; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
467 |
val intern_typ = intrn_typ o spaces_of; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
468 |
val intern_term = intrn_term o spaces_of; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
469 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
470 |
fun intern_tycon sg = intrn (spaces_of sg) typeK; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
471 |
fun intern_const sg = intrn (spaces_of sg) constK; |
3937 | 472 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
473 |
val intern_tycons = intrn_tycons o spaces_of; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
474 |
|
3975 | 475 |
val full_name = full o #path o rep_sg; |
4908 | 476 |
fun full_name_path sg elems name = |
4844 | 477 |
full (#path (rep_sg sg) @ NameSpace.unpack elems) name; |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
478 |
end; |
620 | 479 |
|
402 | 480 |
|
0 | 481 |
|
4249 | 482 |
(** pretty printing of terms, types etc. **) |
3937 | 483 |
|
4249 | 484 |
fun pretty_term (sg as Sg ({stamps, ...}, {syn, spaces, ...})) t = |
3937 | 485 |
Syntax.pretty_term syn |
4249 | 486 |
(exists (equal "CPure" o !) stamps) |
5175 | 487 |
(if ! NameSpace.long_names then t else extrn_term spaces t); |
3937 | 488 |
|
3975 | 489 |
fun pretty_typ (Sg (_, {syn, spaces, ...})) T = |
3937 | 490 |
Syntax.pretty_typ syn |
5175 | 491 |
(if ! NameSpace.long_names then T else extrn_typ spaces T); |
3937 | 492 |
|
3975 | 493 |
fun pretty_sort (Sg (_, {syn, spaces, ...})) S = |
3937 | 494 |
Syntax.pretty_sort syn |
5175 | 495 |
(if ! NameSpace.long_names then S else extrn_sort spaces S); |
3937 | 496 |
|
497 |
fun pretty_classrel sg (c1, c2) = Pretty.block |
|
498 |
[pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]]; |
|
499 |
||
500 |
fun pretty_arity sg (t, Ss, S) = |
|
501 |
let |
|
502 |
val t' = cond_extern sg typeK t; |
|
503 |
val dom = |
|
504 |
if null Ss then [] |
|
505 |
else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1]; |
|
506 |
in |
|
507 |
Pretty.block |
|
508 |
([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S]) |
|
509 |
end; |
|
510 |
||
511 |
fun string_of_term sg t = Pretty.string_of (pretty_term sg t); |
|
512 |
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T); |
|
513 |
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S); |
|
514 |
||
515 |
fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S); |
|
516 |
fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2); |
|
517 |
fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar); |
|
518 |
||
519 |
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg); |
|
520 |
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg); |
|
521 |
||
522 |
||
523 |
||
562 | 524 |
(** read types **) (*exception ERROR*) |
525 |
||
526 |
fun err_in_type s = |
|
527 |
error ("The error(s) above occurred in type " ^ quote s); |
|
528 |
||
4227 | 529 |
fun rd_raw_typ syn tsig spaces def_sort str = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
530 |
intrn_tycons spaces |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
531 |
(Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) str |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
532 |
handle ERROR => err_in_type str); |
4227 | 533 |
|
534 |
fun read_raw_typ (sg as Sg (_, {tsig, syn, spaces, ...}), def_sort) str = |
|
535 |
(check_stale sg; rd_raw_typ syn tsig spaces def_sort str); |
|
536 |
||
562 | 537 |
(*read and certify typ wrt a signature*) |
4227 | 538 |
fun read_typ (sg, def_sort) str = |
539 |
(Type.cert_typ (tsig_of sg) (read_raw_typ (sg, def_sort) str) |
|
3969 | 540 |
handle TYPE (msg, _, _) => (error_msg msg; err_in_type str)); |
562 | 541 |
|
542 |
||
543 |
||
386 | 544 |
(** certify types and terms **) (*exception TYPE*) |
251 | 545 |
|
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
546 |
(* certify_typ *) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
547 |
|
3975 | 548 |
val certify_typ = Type.cert_typ o tsig_of; |
251 | 549 |
|
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
550 |
|
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
551 |
(* certify_term *) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
552 |
|
2979 | 553 |
(*check for duplicate TVars with distinct sorts*) |
554 |
fun nodup_TVars (tvars, T) = |
|
555 |
(case T of |
|
556 |
Type (_, Ts) => nodup_TVars_list (tvars, Ts) |
|
557 |
| TFree _ => tvars |
|
558 |
| TVar (v as (a, S)) => |
|
559 |
(case assoc_string_int (tvars, a) of |
|
560 |
Some S' => |
|
561 |
if S = S' then tvars |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
562 |
else raise TYPE ("Type variable " ^ Syntax.string_of_vname a ^ |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
563 |
" has two distinct sorts", [TVar (a, S'), T], []) |
2979 | 564 |
| None => v :: tvars)) |
565 |
(*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*) |
|
566 |
and nodup_TVars_list (tvars, []) = tvars |
|
567 |
| nodup_TVars_list (tvars, T :: Ts) = |
|
568 |
nodup_TVars_list (nodup_TVars (tvars, T), Ts); |
|
1494
22f67e796445
added nodup_Vars check in cterm_of. Prevents same var with distinct types.
nipkow
parents:
1460
diff
changeset
|
569 |
|
2979 | 570 |
(*check for duplicate Vars with distinct types*) |
1494
22f67e796445
added nodup_Vars check in cterm_of. Prevents same var with distinct types.
nipkow
parents:
1460
diff
changeset
|
571 |
fun nodup_Vars tm = |
2979 | 572 |
let |
573 |
fun nodups vars tvars tm = |
|
574 |
(case tm of |
|
575 |
Const (c, T) => (vars, nodup_TVars (tvars, T)) |
|
576 |
| Free (a, T) => (vars, nodup_TVars (tvars, T)) |
|
577 |
| Var (v as (ixn, T)) => |
|
578 |
(case assoc_string_int (vars, ixn) of |
|
579 |
Some T' => |
|
580 |
if T = T' then (vars, nodup_TVars (tvars, T)) |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
581 |
else raise TYPE ("Variable " ^ Syntax.string_of_vname ixn ^ |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
582 |
" has two distinct types", [T', T], []) |
2979 | 583 |
| None => (v :: vars, tvars)) |
584 |
| Bound _ => (vars, tvars) |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
585 |
| Abs (_, T, t) => nodups vars (nodup_TVars (tvars, T)) t |
2979 | 586 |
| s $ t => |
587 |
let val (vars',tvars') = nodups vars tvars s in |
|
588 |
nodups vars' tvars' t |
|
589 |
end); |
|
590 |
in nodups [] [] tm; () end; |
|
591 |
||
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
592 |
(*compute and check type of the term*) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
593 |
fun type_check sg tm = |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
594 |
let |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
595 |
val prt = |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
596 |
setmp Syntax.show_brackets true |
5175 | 597 |
(setmp NameSpace.long_names true (pretty_term sg)); |
598 |
val prT = setmp NameSpace.long_names true (pretty_typ sg); |
|
251 | 599 |
|
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
600 |
fun err_appl why bs t T u U = |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
601 |
let |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
602 |
val xs = map Free bs; (*we do not rename here*) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
603 |
val t' = subst_bounds (xs, t); |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
604 |
val u' = subst_bounds (xs, u); |
5635 | 605 |
val text = cat_lines(TypeInfer.appl_error prt prT why t' T u' U); |
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
606 |
in raise TYPE (text, [T, U], [t', u']) end; |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
607 |
|
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
608 |
fun typ_of (_, Const (_, T)) = T |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
609 |
| typ_of (_, Free (_, T)) = T |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
610 |
| typ_of (_, Var (_, T)) = T |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
611 |
| typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ => |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
612 |
raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i])) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
613 |
| typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
614 |
| typ_of (bs, t $ u) = |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
615 |
let val T = typ_of (bs, t) and U = typ_of (bs, u) in |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
616 |
(case T of |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
617 |
Type ("fun", [T1, T2]) => |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
618 |
if T1 = U then T2 else err_appl "Incompatible operand type." bs t T u U |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
619 |
| _ => err_appl "Operator not of function type." bs t T u U) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
620 |
end; |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
621 |
|
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
622 |
in typ_of ([], tm) end; |
0 | 623 |
|
2979 | 624 |
|
3975 | 625 |
fun certify_term sg tm = |
251 | 626 |
let |
3969 | 627 |
val _ = check_stale sg; |
3975 | 628 |
val tsig = tsig_of sg; |
3969 | 629 |
|
3975 | 630 |
fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T); |
169 | 631 |
|
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
632 |
fun atom_err (errs, Const (a, T)) = |
3975 | 633 |
(case const_type sg a of |
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
634 |
None => ("Undeclared constant " ^ show_const a T) :: errs |
3975 | 635 |
| Some U => |
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
636 |
if Type.typ_instance (tsig, T, U) then errs |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
637 |
else ("Illegal type for constant " ^ show_const a T) :: errs) |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
638 |
| atom_err (errs, Var ((x, i), _)) = |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
639 |
if i < 0 then ("Negative index for Var " ^ quote x) :: errs else errs |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
640 |
| atom_err (errs, _) = errs; |
251 | 641 |
|
642 |
val norm_tm = |
|
2185 | 643 |
(case it_term_types (Type.typ_errors tsig) (tm, []) of |
644 |
[] => map_term_types (Type.norm_typ tsig) tm |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
645 |
| errs => raise TYPE (cat_lines errs, [], [tm])); |
1494
22f67e796445
added nodup_Vars check in cterm_of. Prevents same var with distinct types.
nipkow
parents:
1460
diff
changeset
|
646 |
val _ = nodup_Vars norm_tm; |
251 | 647 |
in |
4961
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
648 |
(case foldl_aterms atom_err ([], norm_tm) of |
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
wenzelm
parents:
4951
diff
changeset
|
649 |
[] => (norm_tm, type_check sg norm_tm, maxidx_of_term norm_tm) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
650 |
| errs => raise TYPE (cat_lines errs, [], [norm_tm])) |
251 | 651 |
end; |
652 |
||
653 |
||
654 |
||
583 | 655 |
(** infer_types **) (*exception ERROR*) |
251 | 656 |
|
2979 | 657 |
(* |
658 |
def_type: partial map from indexnames to types (constrains Frees, Vars) |
|
659 |
def_sort: partial map from indexnames to sorts (constrains TFrees, TVars) |
|
660 |
used: list of already used type variables |
|
661 |
freeze: if true then generated parameters are turned into TFrees, else TVars |
|
4249 | 662 |
|
663 |
termss: lists of alternative parses (only one combination should be type-correct) |
|
664 |
typs: expected types |
|
2979 | 665 |
*) |
666 |
||
4249 | 667 |
fun infer_types_simult sg def_type def_sort used freeze args = |
251 | 668 |
let |
3975 | 669 |
val tsig = tsig_of sg; |
3810 | 670 |
val prt = |
671 |
setmp Syntax.show_brackets true |
|
5175 | 672 |
(setmp NameSpace.long_names true (pretty_term sg)); |
673 |
val prT = setmp NameSpace.long_names true (pretty_typ sg); |
|
4249 | 674 |
|
675 |
val termss = foldr multiply (map fst args, [[]]); |
|
676 |
val typs = |
|
677 |
map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args; |
|
169 | 678 |
|
4249 | 679 |
fun infer ts = OK |
680 |
(Type.infer_types prt prT tsig (const_type sg) def_type def_sort |
|
681 |
(intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts) |
|
682 |
handle TYPE (msg, _, _) => Error msg; |
|
623 | 683 |
|
4249 | 684 |
val err_results = map infer termss; |
685 |
val errs = mapfilter get_error err_results; |
|
686 |
val results = mapfilter get_ok err_results; |
|
687 |
||
688 |
val ambiguity = length termss; (* FIXME !? *) |
|
689 |
(* FIXME to syntax.ML!? *) |
|
690 |
fun ambig_msg () = |
|
691 |
if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level |
|
692 |
then |
|
693 |
error_msg "Got more than one parse tree.\n\ |
|
3805 | 694 |
\Retry with smaller Syntax.ambiguity_level for more information." |
952
9e10962866b0
Removed an old bug which made some simultaneous instantiations fail if they
nipkow
parents:
949
diff
changeset
|
695 |
else (); |
4249 | 696 |
in |
697 |
if null results then (ambig_msg (); error (cat_lines errs)) |
|
698 |
else if length results = 1 then |
|
699 |
(if ambiguity > ! Syntax.ambiguity_level then |
|
700 |
warning "Fortunately, only one parse tree is type correct.\n\ |
|
701 |
\You may still want to disambiguate your grammar or your input." |
|
702 |
else (); hd results) |
|
703 |
else (ambig_msg (); error ("More than one term is type correct:\n" ^ |
|
704 |
(cat_lines (map (Pretty.string_of o prt) (flat (map fst results)))))) |
|
705 |
end; |
|
623 | 706 |
|
4249 | 707 |
|
708 |
fun infer_types sg def_type def_sort used freeze tsT = |
|
709 |
apfst hd (infer_types_simult sg def_type def_sort used freeze [tsT]); |
|
251 | 710 |
|
711 |
||
2979 | 712 |
|
623 | 713 |
(** extend signature **) (*exception ERROR*) |
714 |
||
620 | 715 |
(** signature extension functions **) (*exception ERROR*) |
386 | 716 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
717 |
fun decls_of path name_of mfixs = |
3810 | 718 |
map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs; |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
719 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
720 |
fun no_read _ _ _ decl = decl; |
386 | 721 |
|
722 |
||
723 |
(* add default sort *) |
|
724 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
725 |
fun ext_defsort int (syn, tsig, ctab, (path, spaces), data) S = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
726 |
(syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S), |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
727 |
ctab, (path, spaces), data); |
386 | 728 |
|
729 |
||
730 |
(* add type constructors *) |
|
731 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
732 |
fun ext_types (syn, tsig, ctab, (path, spaces), data) types = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
733 |
let val decls = decls_of path Syntax.type_name types in |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
734 |
(Syntax.extend_type_gram syn types, |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
735 |
Type.ext_tsig_types tsig decls, ctab, |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
736 |
(path, add_names spaces typeK (map fst decls)), data) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
737 |
end; |
386 | 738 |
|
4844 | 739 |
fun ext_nonterminals sg nonterms = |
740 |
ext_types sg (map (fn n => (n, 0, Syntax.NoSyn)) nonterms); |
|
741 |
||
386 | 742 |
|
743 |
(* add type abbreviations *) |
|
744 |
||
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
745 |
fun read_abbr syn tsig spaces (t, vs, rhs_src) = |
4227 | 746 |
(t, vs, rd_raw_typ syn tsig spaces (K None) rhs_src) |
386 | 747 |
handle ERROR => error ("in type abbreviation " ^ t); |
748 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
749 |
fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces), data) abbrs = |
386 | 750 |
let |
751 |
fun mfix_of (t, vs, _, mx) = (t, length vs, mx); |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
752 |
val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs); |
386 | 753 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
754 |
val abbrs' = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
755 |
map (fn (t, vs, rhs, mx) => |
3810 | 756 |
(full path (Syntax.type_name t mx), vs, rhs)) abbrs; |
3805 | 757 |
val spaces' = add_names spaces typeK (map #1 abbrs'); |
758 |
val decls = map (rd_abbr syn' tsig spaces') abbrs'; |
|
386 | 759 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
760 |
(syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data) |
386 | 761 |
end; |
762 |
||
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
763 |
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
764 |
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs; |
386 | 765 |
|
766 |
||
767 |
(* add type arities *) |
|
768 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
769 |
fun ext_arities int (syn, tsig, ctab, (path, spaces), data) arities = |
386 | 770 |
let |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
771 |
fun intrn_arity (c, Ss, S) = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
772 |
(intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S); |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
773 |
val intrn = if int then map intrn_arity else I; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
774 |
val tsig' = Type.ext_tsig_arities tsig (intrn arities); |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
775 |
val log_types = Type.logical_types tsig'; |
386 | 776 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
777 |
(Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces), data) |
386 | 778 |
end; |
779 |
||
780 |
||
781 |
(* add term constants and syntax *) |
|
782 |
||
3805 | 783 |
fun const_name path c mx = |
3810 | 784 |
full path (Syntax.const_name c mx); |
3805 | 785 |
|
386 | 786 |
fun err_in_const c = |
787 |
error ("in declaration of constant " ^ quote c); |
|
788 |
||
789 |
fun err_dup_consts cs = |
|
790 |
error ("Duplicate declaration of constant(s) " ^ commas_quote cs); |
|
791 |
||
251 | 792 |
|
3805 | 793 |
fun read_const syn tsig (path, spaces) (c, ty_src, mx) = |
4227 | 794 |
(c, rd_raw_typ syn tsig spaces (K None) ty_src, mx) |
3805 | 795 |
handle ERROR => err_in_const (const_name path c mx); |
386 | 796 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
797 |
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces), data) raw_consts = |
386 | 798 |
let |
2979 | 799 |
fun prep_const (c, ty, mx) = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
800 |
(c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
801 |
handle TYPE (msg, _, _) => |
3805 | 802 |
(error_msg msg; err_in_const (const_name path c mx)); |
386 | 803 |
|
3805 | 804 |
val consts = map (prep_const o rd_const syn tsig (path, spaces)) raw_consts; |
386 | 805 |
val decls = |
806 |
if syn_only then [] |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
807 |
else decls_of path Syntax.const_name consts; |
386 | 808 |
in |
2197 | 809 |
(Syntax.extend_const_gram syn prmode consts, tsig, |
4489 | 810 |
Symtab.extend (ctab, decls) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
811 |
handle Symtab.DUPS cs => err_dup_consts cs, |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
812 |
(path, add_names spaces constK (map fst decls)), data) |
386 | 813 |
end; |
814 |
||
4007 | 815 |
fun ext_consts_i sg = ext_cnsts no_read false ("", true) sg; |
816 |
fun ext_consts sg = ext_cnsts read_const false ("", true) sg; |
|
817 |
fun ext_syntax_i sg = ext_cnsts no_read true ("", true) sg; |
|
818 |
fun ext_syntax sg = ext_cnsts read_const true ("", true) sg; |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
819 |
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts; |
2197 | 820 |
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts; |
386 | 821 |
|
822 |
||
823 |
(* add type classes *) |
|
824 |
||
825 |
fun const_of_class c = c ^ "_class"; |
|
826 |
||
827 |
fun class_of_const c_class = |
|
828 |
let |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
829 |
val c = implode (take (size c_class - size "_class", explode c_class)); |
386 | 830 |
in |
831 |
if const_of_class c = c_class then c |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
832 |
else raise TERM ("class_of_const: bad name " ^ quote c_class, []) |
386 | 833 |
end; |
834 |
||
835 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
836 |
fun ext_classes int (syn, tsig, ctab, (path, spaces), data) classes = |
386 | 837 |
let |
562 | 838 |
val names = map fst classes; |
386 | 839 |
val consts = |
840 |
map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names; |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
841 |
|
3810 | 842 |
val full_names = map (full path) names; |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
843 |
val spaces' = add_names spaces classK full_names; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
844 |
val intrn = if int then map (intrn_class spaces') else I; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
845 |
val classes' = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
846 |
ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes); |
386 | 847 |
in |
848 |
ext_consts_i |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
849 |
(Syntax.extend_consts syn names, |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
850 |
Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
851 |
consts |
386 | 852 |
end; |
853 |
||
854 |
||
2963 | 855 |
(* add to classrel *) |
421 | 856 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
857 |
fun ext_classrel int (syn, tsig, ctab, (path, spaces), data) pairs = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
858 |
let val intrn = if int then map (pairself (intrn_class spaces)) else I in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
859 |
(syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
860 |
end; |
421 | 861 |
|
862 |
||
4619 | 863 |
(* add translation rules *) |
864 |
||
865 |
fun ext_trrules (syn, tsig, ctab, (path, spaces), data) args = |
|
866 |
(Syntax.extend_trrules syn |
|
867 |
(map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args), |
|
868 |
tsig, ctab, (path, spaces), data); |
|
869 |
||
870 |
||
1159 | 871 |
(* add to syntax *) |
386 | 872 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
873 |
fun ext_syn extfun (syn, tsig, ctab, names, data) args = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
874 |
(extfun syn args, tsig, ctab, names, data); |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
875 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
876 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
877 |
(* add to path *) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
878 |
|
4844 | 879 |
fun ext_path (syn, tsig, ctab, (path, spaces), data) elems = |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
880 |
let |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
881 |
val path' = |
4844 | 882 |
if elems = ".." andalso not (null path) then fst (split_last path) |
883 |
else if elems = "/" then [] |
|
884 |
else path @ NameSpace.unpack elems; |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
885 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
886 |
(syn, tsig, ctab, (path', spaces), data) |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
887 |
end; |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
888 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
889 |
|
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
890 |
(* add to name space *) |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
891 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
892 |
fun ext_space (syn, tsig, ctab, (path, spaces), data) (kind, names) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
893 |
(syn, tsig, ctab, (path, add_names spaces kind names), data); |
386 | 894 |
|
895 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
896 |
(* signature data *) |
386 | 897 |
|
4256 | 898 |
fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, ext, mrg, prt)) = |
899 |
(syn, tsig, ctab, names, init_data_sg sg data kind e ext mrg prt); |
|
386 | 900 |
|
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
901 |
fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) = |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
902 |
(syn, tsig, ctab, names, put_data_sg sg data kind f x); |
386 | 903 |
|
904 |
||
905 |
(* the external interfaces *) |
|
906 |
||
4998
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
907 |
val add_classes = extend_sign true (ext_classes true) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
908 |
val add_classes_i = extend_sign true (ext_classes false) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
909 |
val add_classrel = extend_sign true (ext_classrel true) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
910 |
val add_classrel_i = extend_sign true (ext_classrel false) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
911 |
val add_defsort = extend_sign true (ext_defsort true) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
912 |
val add_defsort_i = extend_sign true (ext_defsort false) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
913 |
val add_types = extend_sign true ext_types "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
914 |
val add_nonterminals = extend_sign true ext_nonterminals "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
915 |
val add_tyabbrs = extend_sign true ext_tyabbrs "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
916 |
val add_tyabbrs_i = extend_sign true ext_tyabbrs_i "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
917 |
val add_arities = extend_sign true (ext_arities true) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
918 |
val add_arities_i = extend_sign true (ext_arities false) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
919 |
val add_consts = extend_sign true ext_consts "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
920 |
val add_consts_i = extend_sign true ext_consts_i "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
921 |
val add_syntax = extend_sign true ext_syntax "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
922 |
val add_syntax_i = extend_sign true ext_syntax_i "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
923 |
val add_modesyntax = extend_sign true ext_modesyntax "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
924 |
val add_modesyntax_i = extend_sign true ext_modesyntax_i "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
925 |
val add_trfuns = extend_sign true (ext_syn Syntax.extend_trfuns) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
926 |
val add_trfunsT = extend_sign true (ext_syn Syntax.extend_trfunsT) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
927 |
val add_tokentrfuns = extend_sign true (ext_syn Syntax.extend_tokentrfuns) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
928 |
val add_trrules = extend_sign true ext_trrules "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
929 |
val add_trrules_i = extend_sign true (ext_syn Syntax.extend_trrules_i) "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
930 |
val add_path = extend_sign true ext_path "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
931 |
val add_space = extend_sign true ext_space "#"; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
932 |
fun init_data arg sg = extend_sign true (ext_init_data sg) "#" arg sg; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
933 |
fun put_data k f x sg = extend_sign true (ext_put_data sg) "#" (k, f, x) sg; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
934 |
fun add_name name sg = extend_sign true K name () sg; |
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
wenzelm
parents:
4961
diff
changeset
|
935 |
fun prep_ext sg = extend_sign false K "#" () sg; |
386 | 936 |
|
937 |
||
3867 | 938 |
|
4017
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
4007
diff
changeset
|
939 |
(** merge signatures **) (*exception TERM*) |
3867 | 940 |
|
4228
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
941 |
(* merge_stamps *) |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
942 |
|
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
943 |
fun merge_stamps stamps1 stamps2 = |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
944 |
let val stamps = merge_rev_lists stamps1 stamps2 in |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
945 |
(case duplicates (map ! stamps) of |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
946 |
[] => stamps |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
947 |
| dups => raise TERM ("Attempt to merge different versions of theories " |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
948 |
^ commas_quote dups, [])) |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
949 |
end; |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
950 |
|
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
951 |
|
4627 | 952 |
(* implicit merge -- trivial only *) |
3877 | 953 |
|
4228
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
954 |
fun merge_refs (sgr1 as SgRef (Some (ref (sg1 as Sg ({stamps = s1, ...}, _)))), |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
955 |
sgr2 as SgRef (Some (ref (sg2 as Sg ({stamps = s2, ...}, _))))) = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
956 |
if fast_subsig (sg2, sg1) then sgr1 |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
957 |
else if fast_subsig (sg1, sg2) then sgr2 |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
958 |
else if subsig (sg2, sg1) then sgr1 |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
959 |
else if subsig (sg1, sg2) then sgr2 |
4228
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
960 |
else (merge_stamps s1 s2; (*check for different versions*) |
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
961 |
raise TERM ("Attempt to do non-trivial merge of signatures", [])) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
962 |
| merge_refs _ = sys_error "Sign.merge_refs"; |
3867 | 963 |
|
4627 | 964 |
val merge = deref o merge_refs o pairself self_ref; |
965 |
||
3867 | 966 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
967 |
(* proper merge *) |
143 | 968 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
969 |
fun merge_aux (sg1, sg2) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
970 |
if subsig (sg2, sg1) then sg1 |
251 | 971 |
else if subsig (sg1, sg2) then sg2 |
386 | 972 |
else if is_draft sg1 orelse is_draft sg2 then |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
973 |
raise TERM ("Attempt to merge draft signatures", []) |
251 | 974 |
else |
975 |
(*neither is union already; must form union*) |
|
976 |
let |
|
3975 | 977 |
val Sg ({id = _, stamps = stamps1}, {self = _, tsig = tsig1, const_tab = const_tab1, |
978 |
syn = syn1, path = _, spaces = spaces1, data = data1}) = sg1; |
|
979 |
val Sg ({id = _, stamps = stamps2}, {self = _, tsig = tsig2, const_tab = const_tab2, |
|
980 |
syn = syn2, path = _, spaces = spaces2, data = data2}) = sg2; |
|
386 | 981 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
982 |
val id = ref ""; |
3975 | 983 |
val self_ref = ref sg1; (*dummy value*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
984 |
val self = SgRef (Some self_ref); |
402 | 985 |
|
4228
22e3f0368c85
merge_refs: check for different versions of theories;
wenzelm
parents:
4227
diff
changeset
|
986 |
val stamps = merge_stamps stamps1 stamps2; |
2185 | 987 |
val tsig = Type.merge_tsigs (tsig1, tsig2); |
251 | 988 |
val const_tab = Symtab.merge (op =) (const_tab1, const_tab2) |
386 | 989 |
handle Symtab.DUPS cs => |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
990 |
raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []); |
386 | 991 |
val syn = Syntax.merge_syntaxes syn1 syn2; |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
992 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
993 |
val path = []; |
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
994 |
val kinds = distinct (map fst (spaces1 @ spaces2)); |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
995 |
val spaces = |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
996 |
kinds ~~ |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
997 |
ListPair.map NameSpace.merge |
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
998 |
(map (space_of spaces1) kinds, map (space_of spaces2) kinds); |
3867 | 999 |
|
4256 | 1000 |
val data = merge_data (data1, data2); |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1001 |
|
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1002 |
val sign = make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps); |
251 | 1003 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1004 |
self_ref := sign; sign |
251 | 1005 |
end; |
143 | 1006 |
|
4627 | 1007 |
fun nontriv_merge sg1_sg2 = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1008 |
(case handle_error merge_aux sg1_sg2 of |
3805 | 1009 |
OK sg => sg |
1010 |
| Error msg => raise TERM (msg, [])); |
|
1011 |
||
0 | 1012 |
|
1013 |
||
3995 | 1014 |
(** partial Pure signature **) |
0 | 1015 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1016 |
val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0, |
4489 | 1017 |
Symtab.empty, Syntax.pure_syn, [], [], empty_data, []); |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1018 |
|
3995 | 1019 |
val pre_pure = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3956
diff
changeset
|
1020 |
create_sign (SgRef (Some (ref dummy_sg))) [] "#" |
4489 | 1021 |
(Syntax.pure_syn, Type.tsig0, Symtab.empty, ([], []), empty_data); |
0 | 1022 |
|
3791
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
wenzelm
parents:
3552
diff
changeset
|
1023 |
|
0 | 1024 |
end; |