20 val cterm_of: sg -> term -> cterm |
20 val cterm_of: sg -> term -> cterm |
21 val ctyp_of: sg -> typ -> ctyp |
21 val ctyp_of: sg -> typ -> ctyp |
22 val extend: sg -> string -> |
22 val extend: sg -> string -> |
23 (class * class list) list * class list * |
23 (class * class list) list * class list * |
24 (string list * int) list * |
24 (string list * int) list * |
|
25 (string * indexname list * string) list * |
25 (string list * (sort list * class)) list * |
26 (string list * (sort list * class)) list * |
26 (string list * string)list * Syntax.sext option -> sg |
27 (string list * string)list * Syntax.sext option -> sg |
27 val merge: sg * sg -> sg |
28 val merge: sg * sg -> sg |
28 val pure: sg |
29 val pure: sg |
29 val read_cterm: sg -> string * typ -> cterm |
30 val read_cterm: sg -> string * typ -> cterm |
50 val term_of: cterm -> term |
51 val term_of: cterm -> term |
51 val typ_of: ctyp -> typ |
52 val typ_of: ctyp -> typ |
52 val pretty_term: sg -> term -> Syntax.Pretty.T |
53 val pretty_term: sg -> term -> Syntax.Pretty.T |
53 end; |
54 end; |
54 |
55 |
55 functor SignFun(structure Type: TYPE and Syntax: SYNTAX): SIGN = |
56 functor SignFun(structure Type: TYPE and Syntax: SYNTAX) : SIGN = |
56 struct |
57 struct |
57 |
58 |
58 structure Type = Type; |
59 structure Type = Type; |
59 structure Symtab = Type.Symtab; |
60 structure Symtab = Type.Symtab; |
60 structure Syntax = Syntax; |
61 structure Syntax = Syntax; |
83 |
84 |
84 |
85 |
85 (*Check a term for errors. Are all constants and types valid in signature? |
86 (*Check a term for errors. Are all constants and types valid in signature? |
86 Does not check that term is well-typed!*) |
87 Does not check that term is well-typed!*) |
87 fun term_errors (sign as Sg{tsig,const_tab,...}) = |
88 fun term_errors (sign as Sg{tsig,const_tab,...}) = |
88 let val showtyp = string_of_typ sign; |
89 let val showtyp = string_of_typ sign |
89 fun terrs (Const (a,T), errs) = |
90 fun terrs (Const (a,T), errs) = |
90 if valid_const tsig const_tab (a,T) |
91 if valid_const tsig const_tab (a,T) |
91 then Type.type_errors (tsig,showtyp) (T,errs) |
92 then Type.type_errors tsig (T,errs) |
92 else ("Illegal type for constant: " ^ a ^ ": " ^ showtyp T) :: errs |
93 else ("Illegal type for constant: " ^ a ^ ": " ^ showtyp T) :: errs |
93 | terrs (Free (_,T), errs) = Type.type_errors (tsig,showtyp) (T,errs) |
94 | terrs (Free (_,T), errs) = Type.type_errors tsig (T,errs) |
94 | terrs (Var ((a,i),T), errs) = |
95 | terrs (Var ((a,i),T), errs) = |
95 if i>=0 then Type.type_errors (tsig,showtyp) (T,errs) |
96 if i>=0 then Type.type_errors tsig (T,errs) |
96 else ("Negative index for Var: " ^ a) :: errs |
97 else ("Negative index for Var: " ^ a) :: errs |
97 | terrs (Bound _, errs) = errs (*loose bvars detected by type_of*) |
98 | terrs (Bound _, errs) = errs (*loose bvars detected by type_of*) |
98 | terrs (Abs (_,T,t), errs) = |
99 | terrs (Abs (_,T,t), errs) = |
99 Type.type_errors(tsig,showtyp)(T,terrs(t,errs)) |
100 Type.type_errors tsig (T,terrs(t,errs)) |
100 | terrs (f$t, errs) = terrs(f, terrs (t,errs)) |
101 | terrs (f$t, errs) = terrs(f, terrs (t,errs)) |
101 in terrs end; |
102 in terrs end; |
102 |
103 |
103 |
104 |
104 |
105 |
107 (* Extend a signature: may add classes, types and constants. The "ref" in |
108 (* Extend a signature: may add classes, types and constants. The "ref" in |
108 stamps ensures that no two signatures are identical -- it is impossible to |
109 stamps ensures that no two signatures are identical -- it is impossible to |
109 forge a signature. *) |
110 forge a signature. *) |
110 |
111 |
111 fun extend (Sg {tsig, const_tab, syn, stamps}) name |
112 fun extend (Sg {tsig, const_tab, syn, stamps}) name |
112 (classes, default, types, arities, const_decs, opt_sext) = |
113 (classes, default, types, abbr, arities, const_decs, opt_sext) = |
113 let |
114 let |
114 fun err_in_typ s = error ("The error(s) above occurred in type " ^ quote s); |
115 fun err_in_typ s = error ("The error(s) above occurred in type " ^ quote s); |
115 |
116 |
116 fun read_typ tsg sy s = |
117 fun read_typ tsg sy s = |
117 Syntax.read_typ sy (K (Type.defaultS tsg)) s handle ERROR => err_in_typ s; |
118 Syntax.read_typ sy (K (Type.defaultS tsg)) s handle ERROR => err_in_typ s; |
118 |
119 |
119 fun check_typ tsg sy ty = |
120 fun check_typ tsg sy ty = |
120 (case Type.type_errors (tsg, Syntax.string_of_typ sy) (ty, []) of |
121 (case Type.type_errors tsg (ty, []) of |
121 [] => ty |
122 [] => ty |
122 | errs => (prs (cat_lines errs); err_in_typ (Syntax.string_of_typ sy ty))); |
123 | errs => (prs (cat_lines errs); err_in_typ (Syntax.string_of_typ sy ty))); |
123 |
124 |
124 (*reset TVar indices to zero, renaming to preserve distinctness*) |
125 (*reset TVar indices to zero, renaming to preserve distinctness*) |
125 fun zero_tvar_indices T = |
126 fun zero_tvar_indices T = |
133 indices because type inference requires it*) |
134 indices because type inference requires it*) |
134 |
135 |
135 fun read_consts tsg sy (cs, s) = |
136 fun read_consts tsg sy (cs, s) = |
136 let val ty = zero_tvar_indices (Type.varifyT (read_typ tsg sy s)); |
137 let val ty = zero_tvar_indices (Type.varifyT (read_typ tsg sy s)); |
137 in |
138 in |
138 (case Type.type_errors (tsg, Syntax.string_of_typ sy) (ty, []) of |
139 (case Type.type_errors tsg (ty, []) of |
139 [] => (cs, ty) |
140 [] => (cs, ty) |
140 | errs => error (cat_lines (("Error in type of constants " ^ |
141 | errs => error (cat_lines (("Error in type of constants " ^ |
141 space_implode " " (map quote cs)) :: errs))) |
142 space_implode " " (map quote cs)) :: errs))) |
142 end; |
143 end; |
143 |
144 |
144 |
|
145 (* FIXME abbr *) |
|
146 val tsig' = Type.extend (tsig, classes, default, types, arities); |
145 val tsig' = Type.extend (tsig, classes, default, types, arities); |
147 |
146 |
148 (* FIXME *) |
147 fun read_typ_abbr(a,v,s)= |
149 fun expand_typ _ ty = ty; |
148 let val T = Type.varifyT(read_typ tsig' syn s) |
|
149 in (a,(v,T)) end handle ERROR => error("This error occured in abbreviation "^ quote a); |
|
150 |
|
151 val abbr' = map read_typ_abbr abbr; |
|
152 val tsig'' = Type.add_abbrs(tsig',abbr'); |
150 |
153 |
151 val read_ty = |
154 val read_ty = |
152 (expand_typ tsig') o (check_typ tsig' syn) o (read_typ tsig' syn); |
155 (Type.expand_typ tsig'') o (check_typ tsig'' syn) o (read_typ tsig'' syn); |
153 val log_types = Type.logical_types tsig'; |
156 val log_types = Type.logical_types tsig''; |
154 val xconsts = map #1 classes @ flat (map #1 types) @ flat (map #1 const_decs); |
157 val xconsts = map #1 classes @ flat (map #1 types) @ flat (map #1 const_decs); |
155 val sext = case opt_sext of Some sx => sx | None => Syntax.empty_sext; |
158 val sext = case opt_sext of Some sx => sx | None => Syntax.empty_sext; |
156 |
159 |
157 val syn' = Syntax.extend syn read_ty (log_types, xconsts, sext); |
160 val syn' = Syntax.extend syn read_ty (log_types, xconsts, sext); |
158 |
161 |
159 val const_decs' = |
162 val const_decs' = |
160 map (read_consts tsig' syn') (Syntax.constants sext @ const_decs); |
163 map (read_consts tsig'' syn') (Syntax.constants sext @ const_decs); |
161 in |
164 in |
162 Sg { |
165 Sg { |
163 tsig = tsig', |
166 tsig = tsig'', |
164 const_tab = Symtab.st_of_declist (const_decs', const_tab) |
167 const_tab = Symtab.st_of_declist (const_decs', const_tab) |
165 handle Symtab.DUPLICATE a => error ("Constant " ^ quote a ^ " declared twice"), |
168 handle Symtab.DUPLICATE a => error ("Constant " ^ quote a ^ " declared twice"), |
166 syn = syn', |
169 syn = syn', |
167 stamps = ref name :: stamps} |
170 stamps = ref name :: stamps} |
168 end; |
171 end; |
180 ([("logic", [])], |
183 ([("logic", [])], |
181 ["logic"], |
184 ["logic"], |
182 [(["fun"], 2), |
185 [(["fun"], 2), |
183 (["prop"], 0), |
186 (["prop"], 0), |
184 (Syntax.syntax_types, 0)], |
187 (Syntax.syntax_types, 0)], |
|
188 [], |
185 [(["fun"], ([["logic"], ["logic"]], "logic")), |
189 [(["fun"], ([["logic"], ["logic"]], "logic")), |
186 (["prop"], ([], "logic"))], |
190 (["prop"], ([], "logic"))], |
187 [([Syntax.constrainC], "'a::logic => 'a")], |
191 [([Syntax.constrainC], "'a::logic => 'a")], |
188 Some Syntax.pure_sext); |
192 Some Syntax.pure_sext); |
189 |
193 |
239 fun rep_ctyp(Ctyp ctyp) = ctyp; |
243 fun rep_ctyp(Ctyp ctyp) = ctyp; |
240 |
244 |
241 fun typ_of (Ctyp{sign,T}) = T; |
245 fun typ_of (Ctyp{sign,T}) = T; |
242 |
246 |
243 fun ctyp_of (sign as Sg{tsig,...}) T = |
247 fun ctyp_of (sign as Sg{tsig,...}) T = |
244 case Type.type_errors (tsig,string_of_typ sign) (T,[]) of |
248 case Type.type_errors tsig (T,[]) of |
245 [] => Ctyp{sign= sign,T= T} |
249 [] => Ctyp{sign= sign,T= T} |
246 | errs => error (cat_lines ("Error in type:" :: errs)); |
250 | errs => error (cat_lines ("Error in type:" :: errs)); |
247 |
251 |
248 (*The only use is a horrible hack in the simplifier!*) |
252 (*The only use is a horrible hack in the simplifier!*) |
249 fun read_typ(Sg{tsig,syn,...}, defS) s = |
253 fun read_typ(Sg{tsig,syn,...}, defS) s = |
339 fun add_cterm ((cts,tye), (ixn,st)) = |
343 fun add_cterm ((cts,tye), (ixn,st)) = |
340 let val T = case rtypes ixn of |
344 let val T = case rtypes ixn of |
341 Some T => typ_subst_TVars tye T |
345 Some T => typ_subst_TVars tye T |
342 | None => absent ixn; |
346 | None => absent ixn; |
343 val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T); |
347 val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T); |
344 in ((ixn,T,ct)::cts,tye2 @ tye) end |
348 val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T)) |
|
349 in ((cv,ct)::cts,tye2 @ tye) end |
345 val (cterms,tye') = foldl add_cterm (([],tye), vs); |
350 val (cterms,tye') = foldl add_cterm (([],tye), vs); |
346 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', |
351 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end; |
347 map (fn (ixn,T,ct)=> (cterm_of sign (Var(ixn,typ_subst_TVars tye' T)), ct)) |
352 |
348 cterms) |
|
349 end; |
353 end; |
350 |
354 |
351 end; |
|
352 |
|