author | wenzelm |
Tue, 21 Nov 2006 18:07:30 +0100 | |
changeset 21434 | 944f80576be0 |
parent 21205 | dfe338ec9f9c |
child 21680 | 5d2230ad1261 |
permissions | -rw-r--r-- |
18060 | 1 |
(* Title: Pure/consts.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
18935 | 5 |
Polymorphic constants: declarations, abbreviations, additional type |
6 |
constraints. |
|
18060 | 7 |
*) |
8 |
||
9 |
signature CONSTS = |
|
10 |
sig |
|
11 |
type T |
|
19027 | 12 |
val eq_consts: T * T -> bool |
19364 | 13 |
val abbrevs_of: T -> string list -> (term * term) list |
18935 | 14 |
val dest: T -> |
15 |
{constants: (typ * term option) NameSpace.table, |
|
16 |
constraints: typ NameSpace.table} |
|
19364 | 17 |
val declaration: T -> string -> typ (*exception TYPE*) |
18 |
val monomorphic: T -> string -> bool (*exception TYPE*) |
|
19 |
val constraint: T -> string -> typ (*exception TYPE*) |
|
18965 | 20 |
val space_of: T -> NameSpace.T |
19364 | 21 |
val intern: T -> xstring -> string |
22 |
val extern: T -> string -> xstring |
|
23 |
val extern_early: T -> string -> xstring |
|
19657 | 24 |
val syntax: T -> string * mixfix -> string * typ * mixfix |
21181 | 25 |
val syntax_name: T -> string -> string |
18965 | 26 |
val read_const: T -> string -> term |
19364 | 27 |
val certify: Pretty.pp -> Type.tsig -> T -> term -> term (*exception TYPE*) |
18146 | 28 |
val typargs: T -> string * typ -> typ list |
18163 | 29 |
val instance: T -> string * typ list -> typ |
19364 | 30 |
val declare: NameSpace.naming -> (bstring * typ) * bool -> T -> T |
19098
fc736dbbe333
constrain: assert const declaration, optional type (i.e. may delete constraints);
wenzelm
parents:
19027
diff
changeset
|
31 |
val constrain: string * typ option -> T -> T |
19364 | 32 |
val expand_abbrevs: bool -> T -> T |
33 |
val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> |
|
34 |
(bstring * term) * bool -> T -> T |
|
18060 | 35 |
val hide: bool -> string -> T -> T |
36 |
val empty: T |
|
37 |
val merge: T * T -> T |
|
19364 | 38 |
end; |
18060 | 39 |
|
40 |
structure Consts: CONSTS = |
|
41 |
struct |
|
42 |
||
19364 | 43 |
|
44 |
(** consts type **) |
|
45 |
||
46 |
(* datatype T *) |
|
18060 | 47 |
|
19364 | 48 |
datatype kind = |
49 |
LogicalConst of int list list | |
|
50 |
Abbreviation of term; |
|
51 |
||
52 |
type decl = |
|
53 |
(typ * kind) * |
|
19677 | 54 |
bool; (*authentic syntax*) |
18935 | 55 |
|
18060 | 56 |
datatype T = Consts of |
20667 | 57 |
{decls: (decl * serial) NameSpace.table, |
19364 | 58 |
constraints: typ Symtab.table, |
59 |
rev_abbrevs: (term * term) list Symtab.table, |
|
60 |
expand_abbrevs: bool} * stamp; |
|
19027 | 61 |
|
62 |
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2; |
|
18060 | 63 |
|
19364 | 64 |
fun make_consts (decls, constraints, rev_abbrevs, expand_abbrevs) = |
19677 | 65 |
Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs, |
66 |
expand_abbrevs = expand_abbrevs}, stamp ()); |
|
18060 | 67 |
|
19364 | 68 |
fun map_consts f (Consts ({decls, constraints, rev_abbrevs, expand_abbrevs}, _)) = |
69 |
make_consts (f (decls, constraints, rev_abbrevs, expand_abbrevs)); |
|
70 |
||
71 |
fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes = |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19433
diff
changeset
|
72 |
maps (Symtab.lookup_list rev_abbrevs) modes; |
19364 | 73 |
|
18965 | 74 |
|
19364 | 75 |
(* dest consts *) |
76 |
||
77 |
fun dest_kind (LogicalConst _) = NONE |
|
78 |
| dest_kind (Abbreviation t) = SOME t; |
|
79 |
||
80 |
fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) = |
|
81 |
{constants = (space, |
|
82 |
Symtab.fold (fn (c, (((T, kind), _), _)) => |
|
83 |
Symtab.update (c, (T, dest_kind kind))) decls Symtab.empty), |
|
18060 | 84 |
constraints = (space, constraints)}; |
85 |
||
86 |
||
87 |
(* lookup consts *) |
|
88 |
||
19027 | 89 |
fun the_const (Consts ({decls = (_, tab), ...}, _)) c = |
19364 | 90 |
(case Symtab.lookup tab c of |
20667 | 91 |
SOME decl => decl |
19364 | 92 |
| NONE => raise TYPE ("Undeclared constant: " ^ quote c, [], [])); |
18935 | 93 |
|
94 |
fun logical_const consts c = |
|
20667 | 95 |
(case #1 (#1 (the_const consts c)) of |
96 |
(T, LogicalConst ps) => (T, ps) |
|
19364 | 97 |
| _ => raise TYPE ("Illegal abbreviation: " ^ quote c, [], [])); |
18060 | 98 |
|
19364 | 99 |
val declaration = #1 oo logical_const; |
100 |
val type_arguments = #2 oo logical_const; |
|
101 |
val monomorphic = null oo type_arguments; |
|
18935 | 102 |
|
19027 | 103 |
fun constraint (consts as Consts ({constraints, ...}, _)) c = |
18060 | 104 |
(case Symtab.lookup constraints c of |
105 |
SOME T => T |
|
20667 | 106 |
| NONE => #1 (#1 (#1 (the_const consts c)))); |
18935 | 107 |
|
108 |
||
19657 | 109 |
(* name space and syntax *) |
19364 | 110 |
|
111 |
fun space_of (Consts ({decls = (space, _), ...}, _)) = space; |
|
112 |
||
113 |
val intern = NameSpace.intern o space_of; |
|
114 |
val extern = NameSpace.extern o space_of; |
|
115 |
||
116 |
fun extern_early consts c = |
|
117 |
(case try (the_const consts) c of |
|
20667 | 118 |
SOME ((_, true), _) => Syntax.constN ^ c |
19576
179ad0076f75
extern_early: improved handling of undeclared constants;
wenzelm
parents:
19502
diff
changeset
|
119 |
| _ => extern consts c); |
19364 | 120 |
|
19657 | 121 |
fun syntax consts (c, mx) = |
122 |
let |
|
20667 | 123 |
val ((T, _), authentic) = #1 (the_const consts c) handle TYPE (msg, _, _) => error msg; |
19677 | 124 |
val c' = if authentic then Syntax.constN ^ c else NameSpace.base c; |
19657 | 125 |
in (c', T, mx) end; |
126 |
||
21181 | 127 |
fun syntax_name consts c = #1 (syntax consts (c, NoSyn)); |
128 |
||
18060 | 129 |
|
19364 | 130 |
(* read_const *) |
131 |
||
132 |
fun read_const consts raw_c = |
|
133 |
let |
|
134 |
val c = intern consts raw_c; |
|
21205 | 135 |
val (((T, _), _), _) = the_const consts c handle TYPE (msg, _, _) => error msg; |
136 |
in Const (c, T) end; |
|
19364 | 137 |
|
138 |
||
139 |
(* certify *) |
|
140 |
||
141 |
fun certify pp tsig (consts as Consts ({expand_abbrevs, ...}, _)) = |
|
18965 | 142 |
let |
143 |
fun err msg (c, T) = |
|
144 |
raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []); |
|
145 |
fun cert tm = |
|
146 |
let |
|
147 |
val (head, args) = Term.strip_comb tm; |
|
19364 | 148 |
fun comb h = Term.list_comb (h, map cert args); |
18965 | 149 |
in |
150 |
(case head of |
|
19364 | 151 |
Abs (x, T, t) => comb (Abs (x, T, cert t)) |
152 |
| Const (c, T) => |
|
153 |
let |
|
154 |
val T' = Type.cert_typ tsig T; |
|
20667 | 155 |
val (U, kind) = #1 (#1 (the_const consts c)); |
19364 | 156 |
in |
19433
c7a2b7a8c4cb
certify: ignore sort constraints of declarations (MAJOR CHANGE);
wenzelm
parents:
19364
diff
changeset
|
157 |
if not (Type.raw_instance (T', U)) then |
19364 | 158 |
err "Illegal type for constant" (c, T) |
159 |
else |
|
160 |
(case (kind, expand_abbrevs) of |
|
161 |
(Abbreviation u, true) => |
|
19433
c7a2b7a8c4cb
certify: ignore sort constraints of declarations (MAJOR CHANGE);
wenzelm
parents:
19364
diff
changeset
|
162 |
Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ => |
19364 | 163 |
err "Illegal type for abbreviation" (c, T), map cert args) |
164 |
| _ => comb head) |
|
165 |
end |
|
166 |
| _ => comb head) |
|
18965 | 167 |
end; |
168 |
in cert end; |
|
169 |
||
170 |
||
18060 | 171 |
(* typargs -- view actual const type as instance of declaration *) |
172 |
||
19364 | 173 |
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is |
174 |
| subscript T [] = T |
|
175 |
| subscript T _ = raise Subscript; |
|
18060 | 176 |
|
19364 | 177 |
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c); |
18060 | 178 |
|
18163 | 179 |
fun instance consts (c, Ts) = |
180 |
let |
|
181 |
val declT = declaration consts c; |
|
182 |
val vars = map Term.dest_TVar (typargs consts (c, declT)); |
|
20509 | 183 |
in declT |> TermSubst.instantiateT (vars ~~ Ts) end; |
18163 | 184 |
|
18060 | 185 |
|
186 |
||
19364 | 187 |
(** build consts **) |
18060 | 188 |
|
189 |
fun err_dup_consts cs = |
|
190 |
error ("Duplicate declaration of constant(s) " ^ commas_quote cs); |
|
191 |
||
192 |
fun err_inconsistent_constraints cs = |
|
193 |
error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs); |
|
194 |
||
18935 | 195 |
fun extend_decls naming decl tab = NameSpace.extend_table naming (tab, [decl]) |
196 |
handle Symtab.DUPS cs => err_dup_consts cs; |
|
18060 | 197 |
|
18935 | 198 |
|
199 |
(* name space *) |
|
18060 | 200 |
|
19364 | 201 |
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
202 |
(apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, expand_abbrevs)); |
|
18935 | 203 |
|
204 |
||
205 |
(* declarations *) |
|
206 |
||
19677 | 207 |
fun declare naming ((c, declT), authentic) = |
19364 | 208 |
map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
18060 | 209 |
let |
18935 | 210 |
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos |
211 |
| args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) |
|
212 |
| args_of (TFree _) _ = I |
|
213 |
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is |
|
214 |
| args_of_list [] _ _ = I; |
|
20667 | 215 |
val decl = |
216 |
(((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), authentic), serial ()); |
|
19364 | 217 |
in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, expand_abbrevs) end); |
218 |
||
219 |
||
220 |
(* constraints *) |
|
221 |
||
222 |
fun constrain (c, C) consts = |
|
223 |
consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
|
224 |
(the_const consts c handle TYPE (msg, _, _) => error msg; |
|
225 |
(decls, |
|
226 |
constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c), |
|
227 |
rev_abbrevs, expand_abbrevs))); |
|
18060 | 228 |
|
18935 | 229 |
|
230 |
(* abbreviations *) |
|
231 |
||
19364 | 232 |
fun expand_abbrevs b = map_consts (fn (decls, constraints, rev_abbrevs, _) => |
233 |
(decls, constraints, rev_abbrevs, b)); |
|
234 |
||
19027 | 235 |
local |
236 |
||
19364 | 237 |
fun strip_abss tm = ([], tm) :: |
238 |
(case tm of |
|
239 |
Abs (a, T, t) => |
|
240 |
if Term.loose_bvar1 (t, 0) then |
|
241 |
strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b)) |
|
242 |
else [] |
|
243 |
| _ => []); |
|
19027 | 244 |
|
19364 | 245 |
fun rev_abbrev const rhs = |
18060 | 246 |
let |
19027 | 247 |
fun abbrev (xs, body) = |
248 |
let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [] |
|
249 |
in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end; |
|
19502 | 250 |
in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end; |
19027 | 251 |
|
252 |
in |
|
18965 | 253 |
|
19677 | 254 |
fun abbreviate pp tsig naming mode ((c, raw_rhs), authentic) consts = |
18965 | 255 |
let |
19364 | 256 |
val rhs = raw_rhs |
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20509
diff
changeset
|
257 |
|> Term.map_types (Type.cert_typ tsig) |
19364 | 258 |
|> certify pp tsig (consts |> expand_abbrevs false); |
259 |
val rhs' = rhs |
|
260 |
|> certify pp tsig (consts |> expand_abbrevs true); |
|
18965 | 261 |
val T = Term.fastype_of rhs; |
19364 | 262 |
in |
263 |
consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) => |
|
264 |
let |
|
265 |
val decls' = decls |
|
20667 | 266 |
|> extend_decls naming (c, (((T, Abbreviation rhs'), authentic), serial ())); |
19364 | 267 |
val rev_abbrevs' = rev_abbrevs |
19502 | 268 |
|> fold (curry Symtab.update_list mode) (rev_abbrev (NameSpace.full naming c, T) rhs); |
19364 | 269 |
in (decls', constraints, rev_abbrevs', expand_abbrevs) end) |
270 |
end; |
|
18935 | 271 |
|
19027 | 272 |
end; |
273 |
||
18060 | 274 |
|
275 |
(* empty and merge *) |
|
276 |
||
19364 | 277 |
val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty, true); |
18060 | 278 |
|
279 |
fun merge |
|
19364 | 280 |
(Consts ({decls = decls1, constraints = constraints1, |
281 |
rev_abbrevs = rev_abbrevs1, expand_abbrevs = expand_abbrevs1}, _), |
|
282 |
Consts ({decls = decls2, constraints = constraints2, |
|
283 |
rev_abbrevs = rev_abbrevs2, expand_abbrevs = expand_abbrevs2}, _)) = |
|
18060 | 284 |
let |
18935 | 285 |
val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2) |
18060 | 286 |
handle Symtab.DUPS cs => err_dup_consts cs; |
18935 | 287 |
val constraints' = Symtab.merge (op =) (constraints1, constraints2) |
18060 | 288 |
handle Symtab.DUPS cs => err_inconsistent_constraints cs; |
19364 | 289 |
val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join |
290 |
(K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u'))); |
|
291 |
val expand_abbrevs' = expand_abbrevs1 orelse expand_abbrevs2; |
|
292 |
in make_consts (decls', constraints', rev_abbrevs', expand_abbrevs') end; |
|
18060 | 293 |
|
294 |
end; |