1 (* Title: Pure/sign.ML |
1 (* Title: Pure/sign.ML |
2 ID: $Id$ |
2 ID: $Id$ |
3 Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
3 Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
4 Copyright 1992 University of Cambridge |
4 Copyright 1994 University of Cambridge |
5 |
5 |
6 The abstract types "sg" (signatures) and "cterm" / "ctyp" (certified terms / |
6 The abstract types "sg" of signatures |
7 typs under a signature). |
|
8 *) |
7 *) |
9 |
8 |
10 signature SIGN = |
9 signature SIGN = |
11 sig |
10 sig |
12 structure Type: TYPE |
11 structure Type: TYPE |
13 structure Symtab: SYMTAB |
12 structure Symtab: SYMTAB |
14 structure Syntax: SYNTAX |
13 structure Syntax: SYNTAX |
15 sharing Symtab = Type.Symtab |
14 sharing Symtab = Type.Symtab |
16 type sg |
15 type sg |
17 type cterm |
|
18 type ctyp |
|
19 val cfun: (term -> term) -> (cterm -> cterm) |
|
20 val cterm_of: sg -> term -> cterm |
|
21 val ctyp_of: sg -> typ -> ctyp |
|
22 val extend: sg -> string -> |
16 val extend: sg -> string -> |
23 (class * class list) list * class list * |
17 (class * class list) list * class list * |
24 (string list * int) list * |
18 (string list * int) list * |
25 (string * indexname list * string) list * |
19 (string * indexname list * string) list * |
26 (string list * (sort list * class)) list * |
20 (string list * (sort list * class)) list * |
27 (string list * string)list * Syntax.sext option -> sg |
21 (string list * string)list * Syntax.sext option -> sg |
28 val merge: sg * sg -> sg |
22 val merge: sg * sg -> sg |
29 val pure: sg |
23 val pure: sg |
30 val read_cterm: sg -> string * typ -> cterm |
|
31 val read_ctyp: sg -> string -> ctyp |
|
32 val read_insts: sg -> (indexname -> typ option) * (indexname -> sort option) |
|
33 -> (indexname -> typ option) * (indexname -> sort option) |
|
34 -> (string*string)list |
|
35 -> (indexname*ctyp)list * (cterm*cterm)list |
|
36 val read_typ: sg * (indexname -> sort option) -> string -> typ |
24 val read_typ: sg * (indexname -> sort option) -> string -> typ |
37 val rep_cterm: cterm -> {T: typ, t: term, sign: sg, maxidx: int} |
|
38 val rep_ctyp: ctyp -> {T: typ, sign: sg} |
|
39 val rep_sg: sg -> {tsig: Type.type_sig, |
25 val rep_sg: sg -> {tsig: Type.type_sig, |
40 const_tab: typ Symtab.table, |
26 const_tab: typ Symtab.table, |
41 syn: Syntax.syntax, |
27 syn: Syntax.syntax, |
42 stamps: string ref list} |
28 stamps: string ref list} |
43 val string_of_cterm: cterm -> string |
|
44 val string_of_ctyp: ctyp -> string |
|
45 val pprint_cterm: cterm -> pprint_args -> unit |
|
46 val pprint_ctyp: ctyp -> pprint_args -> unit |
|
47 val string_of_term: sg -> term -> string |
29 val string_of_term: sg -> term -> string |
48 val string_of_typ: sg -> typ -> string |
30 val string_of_typ: sg -> typ -> string |
49 val subsig: sg * sg -> bool |
31 val subsig: sg * sg -> bool |
50 val pprint_term: sg -> term -> pprint_args -> unit |
32 val pprint_term: sg -> term -> pprint_args -> unit |
51 val pprint_typ: sg -> typ -> pprint_args -> unit |
33 val pprint_typ: sg -> typ -> pprint_args -> unit |
52 val term_of: cterm -> term |
|
53 val typ_of: ctyp -> typ |
|
54 val pretty_term: sg -> term -> Syntax.Pretty.T |
34 val pretty_term: sg -> term -> Syntax.Pretty.T |
55 val fake_cterm_of: sg -> term -> cterm |
35 val term_errors: sg -> term -> string list |
56 end; |
36 end; |
57 |
37 |
58 functor SignFun(structure Type: TYPE and Syntax: SYNTAX) : SIGN = |
38 functor SignFun(structure Type: TYPE and Syntax: SYNTAX) : SIGN = |
59 struct |
39 struct |
60 |
40 |
88 |
68 |
89 |
69 |
90 (*Check a term for errors. Are all constants and types valid in signature? |
70 (*Check a term for errors. Are all constants and types valid in signature? |
91 Does not check that term is well-typed!*) |
71 Does not check that term is well-typed!*) |
92 fun term_errors (sign as Sg{tsig,const_tab,...}) = |
72 fun term_errors (sign as Sg{tsig,const_tab,...}) = |
93 let val showtyp = string_of_typ sign |
73 let val showtyp = string_of_typ sign |
94 fun terrs (Const (a,T), errs) = |
74 fun terrs (Const (a,T), errs) = |
95 if valid_const tsig const_tab (a,T) |
75 if valid_const tsig const_tab (a,T) |
96 then Type.type_errors tsig (T,errs) |
76 then Type.type_errors tsig (T,errs) |
97 else ("Illegal type for constant: " ^ a ^ ": " ^ showtyp T) :: errs |
77 else ("Illegal type for constant: " ^ a ^ ": " ^ showtyp T) :: errs |
98 | terrs (Free (_,T), errs) = Type.type_errors tsig (T,errs) |
78 | terrs (Free (_,T), errs) = Type.type_errors tsig (T,errs) |
99 | terrs (Var ((a,i),T), errs) = |
79 | terrs (Var ((a,i),T), errs) = |
100 if i>=0 then Type.type_errors tsig (T,errs) |
80 if i>=0 then Type.type_errors tsig (T,errs) |
101 else ("Negative index for Var: " ^ a) :: errs |
81 else ("Negative index for Var: " ^ a) :: errs |
102 | terrs (Bound _, errs) = errs (*loose bvars detected by type_of*) |
82 | terrs (Bound _, errs) = errs (*loose bvars detected by type_of*) |
103 | terrs (Abs (_,T,t), errs) = |
83 | terrs (Abs (_,T,t), errs) = |
104 Type.type_errors tsig (T,terrs(t,errs)) |
84 Type.type_errors tsig (T,terrs(t,errs)) |
105 | terrs (f$t, errs) = terrs(f, terrs (t,errs)) |
85 | terrs (f$t, errs) = terrs(f, terrs (t,errs)) |
106 in terrs end; |
86 in fn t => terrs(t,[]) end; |
107 |
87 |
108 |
88 |
109 |
89 |
110 (** The Extend operation **) |
90 (** The Extend operation **) |
111 |
91 |
236 syn = Syntax.merge (Type.logical_types tsig) syn1 syn2} |
216 syn = Syntax.merge (Type.logical_types tsig) syn1 syn2} |
237 end; |
217 end; |
238 |
218 |
239 |
219 |
240 |
220 |
241 (**** CERTIFIED TYPES ****) |
|
242 |
|
243 |
|
244 (*Certified typs under a signature*) |
|
245 datatype ctyp = Ctyp of {sign: sg, T: typ}; |
|
246 |
|
247 fun rep_ctyp(Ctyp ctyp) = ctyp; |
|
248 |
|
249 fun typ_of (Ctyp{sign,T}) = T; |
|
250 |
|
251 fun ctyp_of (sign as Sg{tsig,...}) T = |
|
252 case Type.type_errors tsig (T,[]) of |
|
253 [] => Ctyp{sign= sign,T= T} |
|
254 | errs => error (cat_lines ("Error in type:" :: errs)); |
|
255 |
|
256 fun read_typ(Sg{tsig,syn,...}, defS) s = |
221 fun read_typ(Sg{tsig,syn,...}, defS) s = |
257 let val term = Syntax.read syn Syntax.typeT s; |
222 let val term = Syntax.read syn Syntax.typeT s; |
258 val S0 = Type.defaultS tsig; |
223 val S0 = Type.defaultS tsig; |
259 fun defS0 s = case defS s of Some S => S | None => S0; |
224 fun defS0 s = case defS s of Some S => S | None => S0; |
260 in Syntax.typ_of_term defS0 term end; |
225 in Syntax.typ_of_term defS0 term end; |
261 |
226 |
262 fun read_ctyp sign = ctyp_of sign o read_typ(sign, K None); |
|
263 |
|
264 fun string_of_ctyp (Ctyp{sign,T}) = string_of_typ sign T; |
|
265 |
|
266 fun pprint_ctyp (Ctyp{sign,T}) = pprint_typ sign T; |
|
267 |
|
268 |
|
269 (**** CERTIFIED TERMS ****) |
|
270 |
|
271 (*Certified terms under a signature, with checked typ and maxidx of Vars*) |
|
272 datatype cterm = Cterm of {sign: sg, t: term, T: typ, maxidx: int}; |
|
273 |
|
274 fun rep_cterm (Cterm args) = args; |
|
275 |
|
276 (*Return the underlying term*) |
|
277 fun term_of (Cterm{sign,t,T,maxidx}) = t; |
|
278 |
|
279 (** pretty printing of terms **) |
227 (** pretty printing of terms **) |
280 |
228 |
281 fun pretty_term (Sg{tsig,syn,...}) = Syntax.pretty_term syn; |
229 fun pretty_term (Sg{tsig,syn,...}) = Syntax.pretty_term syn; |
282 |
230 |
283 fun string_of_term sign t = Pretty.string_of (pretty_term sign t); |
231 fun string_of_term sign t = Pretty.string_of (pretty_term sign t); |
284 |
232 |
285 fun pprint_term sign = Pretty.pprint o Pretty.quote o (pretty_term sign); |
233 fun pprint_term sign = Pretty.pprint o Pretty.quote o (pretty_term sign); |
286 |
234 |
287 fun string_of_cterm (Cterm{sign,t,...}) = string_of_term sign t; |
|
288 |
|
289 fun pprint_cterm (Cterm{sign,t,...}) = pprint_term sign t; |
|
290 |
|
291 (*Create a cterm by checking a "raw" term with respect to a signature*) |
|
292 fun cterm_of sign t = |
|
293 case term_errors sign (t,[]) of |
|
294 [] => Cterm{sign=sign, t=t, T= type_of t, maxidx= maxidx_of_term t} |
|
295 | errs => raise TERM(cat_lines("Term not in signature"::errs), [t]); |
|
296 |
|
297 (*The only use is a horrible hack in the simplifier!*) |
|
298 fun fake_cterm_of sign t = |
|
299 Cterm{sign=sign, t=t, T= fastype_of t, maxidx= maxidx_of_term t} |
|
300 |
|
301 fun cfun f = fn Cterm{sign,t,...} => cterm_of sign (f t); |
|
302 |
|
303 (*Lexing, parsing, polymorphic typechecking of a term.*) |
|
304 fun read_def_cterm (sign as Sg{tsig, const_tab, syn,...}, types, sorts) |
|
305 (a,T) = |
|
306 let val showtyp = string_of_typ sign |
|
307 and showterm = string_of_term sign |
|
308 fun termerr [] = "" |
|
309 | termerr [t] = "\nInvolving this term:\n" ^ showterm t ^ "\n" |
|
310 | termerr ts = "\nInvolving these terms:\n" ^ |
|
311 cat_lines (map showterm ts) |
|
312 val t = Syntax.read syn T a; |
|
313 val (t',tye) = Type.infer_types (tsig, const_tab, types, |
|
314 sorts, showtyp, T, t) |
|
315 handle TYPE (msg, Ts, ts) => |
|
316 error ("Type checking error: " ^ msg ^ "\n" ^ |
|
317 cat_lines (map showtyp Ts) ^ termerr ts) |
|
318 in (cterm_of sign t', tye) |
|
319 end |
|
320 handle TERM (msg, _) => error ("Error: " ^ msg); |
|
321 |
|
322 |
|
323 fun read_cterm sign = #1 o (read_def_cterm (sign, K None, K None)); |
|
324 |
|
325 (** reading of instantiations **) |
|
326 |
|
327 fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v |
|
328 | _ => error("Lexical error in variable name " ^ quote (implode cs)); |
|
329 |
|
330 fun absent ixn = |
|
331 error("No such variable in term: " ^ Syntax.string_of_vname ixn); |
|
332 |
|
333 fun inst_failure ixn = |
|
334 error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails"); |
|
335 |
|
336 fun read_insts (sign as Sg{tsig,...}) (rtypes,rsorts) (types,sorts) insts = |
|
337 let fun split([],tvs,vs) = (tvs,vs) |
|
338 | split((sv,st)::l,tvs,vs) = (case explode sv of |
|
339 "'"::cs => split(l,(indexname cs,st)::tvs,vs) |
|
340 | cs => split(l,tvs,(indexname cs,st)::vs)); |
|
341 val (tvs,vs) = split(insts,[],[]); |
|
342 fun readT((a,i),st) = |
|
343 let val ixn = ("'" ^ a,i); |
|
344 val S = case rsorts ixn of Some S => S | None => absent ixn; |
|
345 val T = read_typ (sign,sorts) st; |
|
346 in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T) |
|
347 else inst_failure ixn |
|
348 end |
|
349 val tye = map readT tvs; |
|
350 fun add_cterm ((cts,tye), (ixn,st)) = |
|
351 let val T = case rtypes ixn of |
|
352 Some T => typ_subst_TVars tye T |
|
353 | None => absent ixn; |
|
354 val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T); |
|
355 val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T)) |
|
356 in ((cv,ct)::cts,tye2 @ tye) end |
|
357 val (cterms,tye') = foldl add_cterm (([],tye), vs); |
|
358 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end; |
|
359 |
|
360 end; |
235 end; |
361 |
236 |