author | haftmann |
Tue, 29 Jan 2008 10:20:00 +0100 | |
changeset 26011 | d55224947082 |
parent 25969 | d3f8ab2726ed |
child 26939 | 1035c89b4c02 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_thingol.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Intermediate language ("Thin-gol") representing executable code. |
|
24918 | 6 |
Representation and translation. |
24219 | 7 |
*) |
8 |
||
9 |
infix 8 `%%; |
|
10 |
infixr 6 `->; |
|
11 |
infixr 6 `-->; |
|
12 |
infix 4 `$; |
|
13 |
infix 4 `$$; |
|
14 |
infixr 3 `|->; |
|
15 |
infixr 3 `|-->; |
|
16 |
||
17 |
signature BASIC_CODE_THINGOL = |
|
18 |
sig |
|
19 |
type vname = string; |
|
20 |
datatype dict = |
|
21 |
DictConst of string * dict list list |
|
22 |
| DictVar of string list * (vname * (int * int)); |
|
23 |
datatype itype = |
|
24 |
`%% of string * itype list |
|
25 |
| ITyVar of vname; |
|
24591 | 26 |
type const = string * (dict list list * itype list (*types of arguments*)) |
24219 | 27 |
datatype iterm = |
24591 | 28 |
IConst of const |
24219 | 29 |
| IVar of vname |
30 |
| `$ of iterm * iterm |
|
31 |
| `|-> of (vname * itype) * iterm |
|
32 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
33 |
(*((term, type), [(selector pattern, body term )]), primitive term)*) |
|
34 |
val `-> : itype * itype -> itype; |
|
35 |
val `--> : itype list * itype -> itype; |
|
36 |
val `$$ : iterm * iterm list -> iterm; |
|
37 |
val `|--> : (vname * itype) list * iterm -> iterm; |
|
38 |
type typscheme = (vname * sort) list * itype; |
|
39 |
end; |
|
40 |
||
41 |
signature CODE_THINGOL = |
|
42 |
sig |
|
43 |
include BASIC_CODE_THINGOL; |
|
44 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list; |
|
45 |
val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a; |
|
46 |
val unfold_fun: itype -> itype list * itype; |
|
47 |
val unfold_app: iterm -> iterm * iterm list; |
|
48 |
val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option; |
|
49 |
val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm; |
|
50 |
val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option; |
|
51 |
val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm; |
|
52 |
val unfold_const_app: iterm -> |
|
53 |
((string * (dict list list * itype list)) * iterm list) option; |
|
54 |
val collapse_let: ((vname * itype) * iterm) * iterm |
|
55 |
-> (iterm * itype) * (iterm * iterm) list; |
|
56 |
val eta_expand: (string * (dict list list * itype list)) * iterm list -> int -> iterm; |
|
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
57 |
val contains_dictvar: iterm -> bool; |
25621 | 58 |
val locally_monomorphic: iterm -> bool; |
24219 | 59 |
val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a; |
60 |
val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a; |
|
61 |
val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a; |
|
62 |
||
24918 | 63 |
datatype stmt = |
24219 | 64 |
Bot |
24591 | 65 |
| Fun of typscheme * ((iterm list * iterm) * thm) list |
24219 | 66 |
| Datatype of (vname * sort) list * (string * itype list) list |
67 |
| Datatypecons of string |
|
24811 | 68 |
| Class of vname * ((class * string) list * (string * itype) list) |
24219 | 69 |
| Classrel of class * class |
24837 | 70 |
| Classparam of class |
24219 | 71 |
| Classinst of (class * (string * (vname * sort) list)) |
72 |
* ((class * (string * (string * dict list list))) list |
|
24591 | 73 |
* ((string * const) * thm) list); |
24918 | 74 |
type code = stmt Graph.T; |
24219 | 75 |
val empty_code: code; |
76 |
val merge_code: code * code -> code; |
|
77 |
val project_code: bool (*delete empty funs*) |
|
78 |
-> string list (*hidden*) -> string list option (*selected*) |
|
79 |
-> code -> code; |
|
80 |
val empty_funs: code -> string list; |
|
81 |
val is_cons: code -> string -> bool; |
|
25621 | 82 |
val contr_classparam_typs: code -> string -> itype option list; |
24219 | 83 |
|
24918 | 84 |
type transact; |
85 |
val ensure_const: theory -> ((sort -> sort) * Sorts.algebra) * Consts.T |
|
86 |
-> CodeFuncgr.T -> string -> transact -> string * transact; |
|
87 |
val ensure_value: theory -> ((sort -> sort) * Sorts.algebra) * Consts.T |
|
26011 | 88 |
-> CodeFuncgr.T -> term |
89 |
-> transact -> (code * ((typscheme * iterm) * string list)) * transact; |
|
25969 | 90 |
val transact: theory -> CodeFuncgr.T |
91 |
-> (theory -> ((sort -> sort) * Sorts.algebra) * Consts.T -> CodeFuncgr.T |
|
92 |
-> transact -> 'a * transact) -> code -> 'a * code; |
|
26011 | 93 |
val add_value_stmt: iterm * itype -> code -> code; |
24219 | 94 |
end; |
95 |
||
96 |
structure CodeThingol: CODE_THINGOL = |
|
97 |
struct |
|
98 |
||
99 |
(** auxiliary **) |
|
100 |
||
101 |
fun unfoldl dest x = |
|
102 |
case dest x |
|
103 |
of NONE => (x, []) |
|
104 |
| SOME (x1, x2) => |
|
105 |
let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end; |
|
106 |
||
107 |
fun unfoldr dest x = |
|
108 |
case dest x |
|
109 |
of NONE => ([], x) |
|
110 |
| SOME (x1, x2) => |
|
111 |
let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end; |
|
112 |
||
113 |
||
114 |
(** language core - types, pattern, expressions **) |
|
115 |
||
116 |
(* language representation *) |
|
117 |
||
118 |
type vname = string; |
|
119 |
||
120 |
datatype dict = |
|
121 |
DictConst of string * dict list list |
|
122 |
| DictVar of string list * (vname * (int * int)); |
|
123 |
||
124 |
datatype itype = |
|
125 |
`%% of string * itype list |
|
126 |
| ITyVar of vname; |
|
127 |
||
24591 | 128 |
type const = string * (dict list list * itype list (*types of arguments*)) |
129 |
||
24219 | 130 |
datatype iterm = |
24591 | 131 |
IConst of const |
24219 | 132 |
| IVar of vname |
133 |
| `$ of iterm * iterm |
|
134 |
| `|-> of (vname * itype) * iterm |
|
135 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
136 |
(*see also signature*) |
|
137 |
||
138 |
(* |
|
139 |
variable naming conventions |
|
140 |
||
141 |
bare names: |
|
142 |
variable names v |
|
143 |
class names class |
|
144 |
type constructor names tyco |
|
145 |
datatype names dtco |
|
24837 | 146 |
const names (general) c (const) |
24219 | 147 |
constructor names co |
24837 | 148 |
class parameter names classparam |
24219 | 149 |
arbitrary name s |
150 |
||
24837 | 151 |
v, c, co, classparam also annotated with types etc. |
24219 | 152 |
|
153 |
constructs: |
|
154 |
sort sort |
|
155 |
type parameters vs |
|
156 |
type ty |
|
157 |
type schemes tysm |
|
158 |
term t |
|
159 |
(term as pattern) p |
|
160 |
instance (class, tyco) inst |
|
161 |
*) |
|
162 |
||
163 |
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2]; |
|
164 |
val op `--> = Library.foldr (op `->); |
|
165 |
val op `$$ = Library.foldl (op `$); |
|
166 |
val op `|--> = Library.foldr (op `|->); |
|
167 |
||
168 |
val unfold_fun = unfoldr |
|
169 |
(fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2) |
|
170 |
| _ => NONE); |
|
171 |
||
172 |
val unfold_app = unfoldl |
|
173 |
(fn op `$ t => SOME t |
|
174 |
| _ => NONE); |
|
175 |
||
176 |
val split_abs = |
|
177 |
(fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) => |
|
178 |
if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t) |
|
179 |
| (v, ty) `|-> t => SOME (((v, NONE), ty), t) |
|
180 |
| _ => NONE); |
|
181 |
||
182 |
val unfold_abs = unfoldr split_abs; |
|
183 |
||
184 |
val split_let = |
|
185 |
(fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t) |
|
186 |
| _ => NONE); |
|
187 |
||
188 |
val unfold_let = unfoldr split_let; |
|
189 |
||
190 |
fun unfold_const_app t = |
|
191 |
case unfold_app t |
|
192 |
of (IConst c, ts) => SOME (c, ts) |
|
193 |
| _ => NONE; |
|
194 |
||
195 |
fun fold_aiterms f (t as IConst _) = f t |
|
196 |
| fold_aiterms f (t as IVar _) = f t |
|
197 |
| fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2 |
|
198 |
| fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t' |
|
199 |
| fold_aiterms f (ICase (_, t)) = fold_aiterms f t; |
|
200 |
||
201 |
fun fold_constnames f = |
|
202 |
let |
|
203 |
fun add (IConst (c, _)) = f c |
|
204 |
| add _ = I; |
|
205 |
in fold_aiterms add end; |
|
206 |
||
207 |
fun fold_varnames f = |
|
208 |
let |
|
209 |
fun add (IVar v) = f v |
|
210 |
| add ((v, _) `|-> _) = f v |
|
211 |
| add _ = I; |
|
212 |
in fold_aiterms add end; |
|
213 |
||
214 |
fun fold_unbound_varnames f = |
|
215 |
let |
|
216 |
fun add _ (IConst _) = I |
|
217 |
| add vs (IVar v) = if not (member (op =) vs v) then f v else I |
|
218 |
| add vs (t1 `$ t2) = add vs t1 #> add vs t2 |
|
219 |
| add vs ((v, _) `|-> t) = add (insert (op =) v vs) t |
|
220 |
| add vs (ICase (_, t)) = add vs t; |
|
221 |
in add [] end; |
|
222 |
||
223 |
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) = |
|
224 |
let |
|
225 |
fun exists_v t = fold_unbound_varnames (fn w => fn b => |
|
226 |
b orelse v = w) t false; |
|
227 |
in if v = w andalso forall (fn (t1, t2) => |
|
228 |
exists_v t1 orelse not (exists_v t2)) ds |
|
229 |
then ((se, ty), ds) |
|
230 |
else ((se, ty), [(IVar v, be)]) |
|
231 |
end |
|
232 |
| collapse_let (((v, ty), se), be) = |
|
233 |
((se, ty), [(IVar v, be)]) |
|
234 |
||
235 |
fun eta_expand (c as (_, (_, tys)), ts) k = |
|
236 |
let |
|
237 |
val j = length ts; |
|
238 |
val l = k - j; |
|
239 |
val ctxt = (fold o fold_varnames) Name.declare ts Name.context; |
|
240 |
val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys); |
|
241 |
in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end; |
|
242 |
||
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
243 |
fun contains_dictvar t = |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
244 |
let |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
245 |
fun contains (DictConst (_, dss)) = (fold o fold) contains dss |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
246 |
| contains (DictVar _) = K true; |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
247 |
in |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
248 |
fold_aiterms |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
249 |
(fn IConst (_, (dss, _)) => (fold o fold) contains dss | _ => I) t false |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
250 |
end; |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
251 |
|
25621 | 252 |
fun locally_monomorphic (IConst _) = false |
253 |
| locally_monomorphic (IVar _) = true |
|
254 |
| locally_monomorphic (t `$ _) = locally_monomorphic t |
|
255 |
| locally_monomorphic (_ `|-> t) = locally_monomorphic t |
|
256 |
| locally_monomorphic (ICase ((_, ds), _)) = exists (locally_monomorphic o snd) ds; |
|
257 |
||
258 |
||
24219 | 259 |
|
260 |
(** definitions, transactions **) |
|
261 |
||
262 |
type typscheme = (vname * sort) list * itype; |
|
24918 | 263 |
datatype stmt = |
24219 | 264 |
Bot |
24591 | 265 |
| Fun of typscheme * ((iterm list * iterm) * thm) list |
24219 | 266 |
| Datatype of (vname * sort) list * (string * itype list) list |
267 |
| Datatypecons of string |
|
24811 | 268 |
| Class of vname * ((class * string) list * (string * itype) list) |
24219 | 269 |
| Classrel of class * class |
24837 | 270 |
| Classparam of class |
24219 | 271 |
| Classinst of (class * (string * (vname * sort) list)) |
272 |
* ((class * (string * (string * dict list list))) list |
|
24591 | 273 |
* ((string * const) * thm) list); |
24219 | 274 |
|
24918 | 275 |
type code = stmt Graph.T; |
24219 | 276 |
|
277 |
||
278 |
(* abstract code *) |
|
279 |
||
280 |
val empty_code = Graph.empty : code; (*read: "depends on"*) |
|
281 |
||
282 |
fun ensure_bot name = Graph.default_node (name, Bot); |
|
283 |
||
284 |
fun add_def_incr (name, Bot) code = |
|
285 |
(case the_default Bot (try (Graph.get_node code) name) |
|
286 |
of Bot => error "Attempted to add Bot to code" |
|
287 |
| _ => code) |
|
288 |
| add_def_incr (name, def) code = |
|
289 |
(case try (Graph.get_node code) name |
|
290 |
of NONE => Graph.new_node (name, def) code |
|
291 |
| SOME Bot => Graph.map_node name (K def) code |
|
292 |
| SOME _ => error ("Tried to overwrite definition " ^ quote name)); |
|
293 |
||
24811 | 294 |
fun add_dep (NONE, _) = I |
295 |
| add_dep (SOME name1, name2) = |
|
296 |
if name1 = name2 then I else Graph.add_edge (name1, name2); |
|
24219 | 297 |
|
298 |
val merge_code : code * code -> code = Graph.merge (K true); |
|
299 |
||
300 |
fun project_code delete_empty_funs hidden raw_selected code = |
|
301 |
let |
|
302 |
fun is_empty_fun name = case Graph.get_node code name |
|
24381 | 303 |
of Fun (_, []) => true |
24219 | 304 |
| _ => false; |
305 |
val names = subtract (op =) hidden (Graph.keys code); |
|
306 |
val deleted = Graph.all_preds code (filter is_empty_fun names); |
|
307 |
val selected = case raw_selected |
|
308 |
of NONE => names |> subtract (op =) deleted |
|
309 |
| SOME sel => sel |
|
310 |
|> delete_empty_funs ? subtract (op =) deleted |
|
311 |
|> subtract (op =) hidden |
|
312 |
|> Graph.all_succs code |
|
313 |
|> delete_empty_funs ? subtract (op =) deleted |
|
314 |
|> subtract (op =) hidden; |
|
315 |
in |
|
316 |
code |
|
317 |
|> Graph.subgraph (member (op =) selected) |
|
318 |
end; |
|
319 |
||
320 |
fun empty_funs code = |
|
24381 | 321 |
Graph.fold (fn (name, (Fun (_, []), _)) => cons name |
24219 | 322 |
| _ => I) code []; |
323 |
||
324 |
fun is_cons code name = case Graph.get_node code name |
|
325 |
of Datatypecons _ => true |
|
326 |
| _ => false; |
|
327 |
||
25621 | 328 |
fun contr_classparam_typs code name = case Graph.get_node code name |
329 |
of Classparam class => let |
|
330 |
val Class (_, (_, params)) = Graph.get_node code class; |
|
331 |
val SOME ty = AList.lookup (op =) params name; |
|
332 |
val (tys, res_ty) = unfold_fun ty; |
|
333 |
fun no_tyvar (_ `%% tys) = forall no_tyvar tys |
|
334 |
| no_tyvar (ITyVar _) = false; |
|
335 |
in if no_tyvar res_ty |
|
336 |
then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys |
|
337 |
else [] |
|
338 |
end |
|
339 |
| _ => []; |
|
340 |
||
24219 | 341 |
|
342 |
(* transaction protocol *) |
|
343 |
||
24283 | 344 |
type transact = Graph.key option * code; |
345 |
||
24918 | 346 |
fun ensure_stmt stmtgen name (dep, code) = |
24219 | 347 |
let |
348 |
fun add_def false = |
|
349 |
ensure_bot name |
|
24811 | 350 |
#> add_dep (dep, name) |
24918 | 351 |
#> curry stmtgen (SOME name) |
24252 | 352 |
##> snd |
24811 | 353 |
#-> (fn def => add_def_incr (name, def)) |
24219 | 354 |
| add_def true = |
24811 | 355 |
add_dep (dep, name); |
24219 | 356 |
in |
357 |
code |
|
358 |
|> add_def (can (Graph.get_node code) name) |
|
359 |
|> pair dep |
|
24918 | 360 |
|> pair name |
24219 | 361 |
end; |
362 |
||
25969 | 363 |
fun transact thy funcgr f code = |
364 |
let |
|
365 |
val naming = NameSpace.qualified_names NameSpace.default_naming; |
|
366 |
val consttab = Consts.empty |
|
367 |
|> fold (fn c => Consts.declare true naming [] (c, CodeFuncgr.typ funcgr c)) |
|
368 |
(CodeFuncgr.all funcgr); |
|
369 |
val algbr = (Code.operational_algebra thy, consttab); |
|
370 |
in |
|
371 |
(NONE, code) |
|
372 |
|> f thy algbr funcgr |
|
373 |
|-> (fn x => fn (_, code) => (x, code)) |
|
374 |
end; |
|
24219 | 375 |
|
24918 | 376 |
|
377 |
(* translation kernel *) |
|
378 |
||
379 |
fun ensure_class thy (algbr as ((_, algebra), _)) funcgr class = |
|
380 |
let |
|
381 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
24969 | 382 |
val cs = #params (AxClass.get_info thy class); |
24918 | 383 |
val class' = CodeName.class thy class; |
384 |
val stmt_class = |
|
385 |
fold_map (fn superclass => ensure_class thy algbr funcgr superclass |
|
386 |
##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses |
|
387 |
##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c |
|
388 |
##>> exprgen_typ thy algbr funcgr ty) cs |
|
24969 | 389 |
#>> (fn info => Class (unprefix "'" Name.aT, info)) |
24918 | 390 |
in |
391 |
ensure_stmt stmt_class class' |
|
392 |
end |
|
393 |
and ensure_classrel thy algbr funcgr (subclass, superclass) = |
|
394 |
let |
|
395 |
val classrel' = CodeName.classrel thy (subclass, superclass); |
|
396 |
val stmt_classrel = |
|
397 |
ensure_class thy algbr funcgr subclass |
|
398 |
##>> ensure_class thy algbr funcgr superclass |
|
399 |
#>> Classrel; |
|
400 |
in |
|
401 |
ensure_stmt stmt_classrel classrel' |
|
402 |
end |
|
403 |
and ensure_tyco thy algbr funcgr "fun" = |
|
404 |
pair "fun" |
|
405 |
| ensure_tyco thy algbr funcgr tyco = |
|
406 |
let |
|
407 |
val stmt_datatype = |
|
408 |
let |
|
409 |
val (vs, cos) = Code.get_datatype thy tyco; |
|
410 |
in |
|
411 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
412 |
##>> fold_map (fn (c, tys) => |
|
413 |
ensure_const thy algbr funcgr c |
|
414 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos |
|
415 |
#>> Datatype |
|
416 |
end; |
|
417 |
val tyco' = CodeName.tyco thy tyco; |
|
418 |
in |
|
419 |
ensure_stmt stmt_datatype tyco' |
|
420 |
end |
|
421 |
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) = |
|
422 |
fold_map (ensure_class thy algbr funcgr) (proj_sort sort) |
|
423 |
#>> (fn sort => (unprefix "'" v, sort)) |
|
424 |
and exprgen_typ thy algbr funcgr (TFree vs) = |
|
425 |
exprgen_tyvar_sort thy algbr funcgr vs |
|
426 |
#>> (fn (v, sort) => ITyVar v) |
|
427 |
| exprgen_typ thy algbr funcgr (Type (tyco, tys)) = |
|
428 |
ensure_tyco thy algbr funcgr tyco |
|
429 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys |
|
430 |
#>> (fn (tyco, tys) => tyco `%% tys) |
|
431 |
and exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) = |
|
432 |
let |
|
433 |
val pp = Sign.pp thy; |
|
434 |
datatype typarg = |
|
435 |
Global of (class * string) * typarg list list |
|
436 |
| Local of (class * class) list * (string * (int * sort)); |
|
437 |
fun class_relation (Global ((_, tyco), yss), _) class = |
|
438 |
Global ((class, tyco), yss) |
|
439 |
| class_relation (Local (classrels, v), subclass) superclass = |
|
440 |
Local ((subclass, superclass) :: classrels, v); |
|
441 |
fun type_constructor tyco yss class = |
|
442 |
Global ((class, tyco), (map o map) fst yss); |
|
443 |
fun type_variable (TFree (v, sort)) = |
|
444 |
let |
|
445 |
val sort' = proj_sort sort; |
|
446 |
in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end; |
|
447 |
val typargs = Sorts.of_sort_derivation pp algebra |
|
448 |
{class_relation = class_relation, type_constructor = type_constructor, |
|
449 |
type_variable = type_variable} |
|
450 |
(ty_ctxt, proj_sort sort_decl); |
|
451 |
fun mk_dict (Global (inst, yss)) = |
|
452 |
ensure_inst thy algbr funcgr inst |
|
453 |
##>> (fold_map o fold_map) mk_dict yss |
|
454 |
#>> (fn (inst, dss) => DictConst (inst, dss)) |
|
455 |
| mk_dict (Local (classrels, (v, (k, sort)))) = |
|
456 |
fold_map (ensure_classrel thy algbr funcgr) classrels |
|
457 |
#>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort)))) |
|
458 |
in |
|
459 |
fold_map mk_dict typargs |
|
460 |
end |
|
461 |
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) = |
|
462 |
let |
|
25042
a33b78d63114
renamed Consts.the_declaration to Consts.the_type;
wenzelm
parents:
24969
diff
changeset
|
463 |
val ty_decl = Consts.the_type consts c; |
24918 | 464 |
val (tys, tys_decl) = pairself (curry (Consts.typargs consts) c) (ty_ctxt, ty_decl); |
465 |
val sorts = map (snd o dest_TVar) tys_decl; |
|
466 |
in |
|
467 |
fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts) |
|
468 |
end |
|
469 |
and exprgen_eq thy algbr funcgr thm = |
|
470 |
let |
|
471 |
val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals |
|
472 |
o Logic.unvarify o prop_of) thm; |
|
473 |
in |
|
474 |
fold_map (exprgen_term thy algbr funcgr) args |
|
475 |
##>> exprgen_term thy algbr funcgr rhs |
|
476 |
#>> rpair thm |
|
477 |
end |
|
478 |
and ensure_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) = |
|
479 |
let |
|
480 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
24969 | 481 |
val classparams = these (try (#params o AxClass.get_info thy) class); |
24918 | 482 |
val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]); |
483 |
val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class]; |
|
484 |
val vs' = map2 (fn (v, sort1) => fn sort2 => (v, |
|
485 |
Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts'; |
|
486 |
val arity_typ = Type (tyco, map TFree vs); |
|
487 |
val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs'); |
|
488 |
fun exprgen_superarity superclass = |
|
489 |
ensure_class thy algbr funcgr superclass |
|
490 |
##>> ensure_classrel thy algbr funcgr (class, superclass) |
|
491 |
##>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass]) |
|
492 |
#>> (fn ((superclass, classrel), [DictConst (inst, dss)]) => |
|
493 |
(superclass, (classrel, (inst, dss)))); |
|
494 |
fun exprgen_classparam_inst (c, ty) = |
|
495 |
let |
|
496 |
val c_inst = Const (c, map_type_tfree (K arity_typ') ty); |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25485
diff
changeset
|
497 |
val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy c_inst); |
24918 | 498 |
val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd |
499 |
o Logic.dest_equals o Thm.prop_of) thm; |
|
500 |
in |
|
501 |
ensure_const thy algbr funcgr c |
|
502 |
##>> exprgen_const thy algbr funcgr c_ty |
|
503 |
#>> (fn (c, IConst c_inst) => ((c, c_inst), thm)) |
|
504 |
end; |
|
505 |
val stmt_inst = |
|
506 |
ensure_class thy algbr funcgr class |
|
507 |
##>> ensure_tyco thy algbr funcgr tyco |
|
508 |
##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
509 |
##>> fold_map exprgen_superarity superclasses |
|
510 |
##>> fold_map exprgen_classparam_inst classparams |
|
511 |
#>> (fn ((((class, tyco), arity), superarities), classparams) => |
|
512 |
Classinst ((class, (tyco, arity)), (superarities, classparams))); |
|
513 |
val inst = CodeName.instance thy (class, tyco); |
|
514 |
in |
|
515 |
ensure_stmt stmt_inst inst |
|
516 |
end |
|
517 |
and ensure_const thy (algbr as (_, consts)) funcgr c = |
|
518 |
let |
|
519 |
val c' = CodeName.const thy c; |
|
520 |
fun stmt_datatypecons tyco = |
|
521 |
ensure_tyco thy algbr funcgr tyco |
|
25621 | 522 |
#>> Datatypecons; |
24918 | 523 |
fun stmt_classparam class = |
524 |
ensure_class thy algbr funcgr class |
|
25621 | 525 |
#>> Classparam; |
24918 | 526 |
fun stmt_fun trns = |
527 |
let |
|
528 |
val raw_thms = CodeFuncgr.funcs funcgr c; |
|
529 |
val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c; |
|
530 |
val vs = (map dest_TFree o Consts.typargs consts) (c, ty); |
|
531 |
val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty |
|
532 |
then raw_thms |
|
533 |
else map (CodeUnit.expand_eta 1) raw_thms; |
|
534 |
in |
|
535 |
trns |
|
536 |
|> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
537 |
||>> exprgen_typ thy algbr funcgr ty |
|
538 |
||>> fold_map (exprgen_eq thy algbr funcgr) thms |
|
539 |
|>> (fn ((vs, ty), eqs) => Fun ((vs, ty), eqs)) |
|
540 |
end; |
|
541 |
val stmtgen = case Code.get_datatype_of_constr thy c |
|
542 |
of SOME tyco => stmt_datatypecons tyco |
|
543 |
| NONE => (case AxClass.class_of_param thy c |
|
544 |
of SOME class => stmt_classparam class |
|
545 |
| NONE => stmt_fun) |
|
546 |
in |
|
547 |
ensure_stmt stmtgen c' |
|
548 |
end |
|
549 |
and exprgen_term thy algbr funcgr (Const (c, ty)) = |
|
550 |
exprgen_app thy algbr funcgr ((c, ty), []) |
|
551 |
| exprgen_term thy algbr funcgr (Free (v, _)) = |
|
552 |
pair (IVar v) |
|
553 |
| exprgen_term thy algbr funcgr (Abs (abs as (_, ty, _))) = |
|
554 |
let |
|
555 |
val (v, t) = Syntax.variant_abs abs; |
|
556 |
in |
|
557 |
exprgen_typ thy algbr funcgr ty |
|
558 |
##>> exprgen_term thy algbr funcgr t |
|
559 |
#>> (fn (ty, t) => (v, ty) `|-> t) |
|
560 |
end |
|
561 |
| exprgen_term thy algbr funcgr (t as _ $ _) = |
|
562 |
case strip_comb t |
|
563 |
of (Const (c, ty), ts) => |
|
564 |
exprgen_app thy algbr funcgr ((c, ty), ts) |
|
565 |
| (t', ts) => |
|
566 |
exprgen_term thy algbr funcgr t' |
|
567 |
##>> fold_map (exprgen_term thy algbr funcgr) ts |
|
568 |
#>> (fn (t, ts) => t `$$ ts) |
|
569 |
and exprgen_const thy algbr funcgr (c, ty) = |
|
570 |
ensure_const thy algbr funcgr c |
|
571 |
##>> exprgen_dict_parms thy algbr funcgr (c, ty) |
|
572 |
##>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty) |
|
573 |
#>> (fn ((c, iss), tys) => IConst (c, (iss, tys))) |
|
574 |
and exprgen_app_default thy algbr funcgr (c_ty, ts) = |
|
575 |
exprgen_const thy algbr funcgr c_ty |
|
576 |
##>> fold_map (exprgen_term thy algbr funcgr) ts |
|
577 |
#>> (fn (t, ts) => t `$$ ts) |
|
578 |
and exprgen_case thy algbr funcgr n cases (app as ((c, ty), ts)) = |
|
579 |
let |
|
580 |
val (tys, _) = |
|
581 |
(chop (1 + (if null cases then 1 else length cases)) o fst o strip_type) ty; |
|
582 |
val dt = nth ts n; |
|
583 |
val dty = nth tys n; |
|
584 |
fun is_undefined (Const (c, _)) = Code.is_undefined thy c |
|
585 |
| is_undefined _ = false; |
|
586 |
fun mk_case (co, n) t = |
|
587 |
let |
|
588 |
val (vs, body) = Term.strip_abs_eta n t; |
|
589 |
val selector = list_comb (Const (co, map snd vs ---> dty), map Free vs); |
|
590 |
in if is_undefined body then NONE else SOME (selector, body) end; |
|
591 |
fun mk_ds [] = |
|
592 |
let |
|
593 |
val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts)) |
|
594 |
in [(Free v_ty, body)] end |
|
595 |
| mk_ds cases = map_filter (uncurry mk_case) |
|
596 |
(AList.make (CodeUnit.no_args thy) cases ~~ nth_drop n ts); |
|
597 |
in |
|
598 |
exprgen_term thy algbr funcgr dt |
|
599 |
##>> exprgen_typ thy algbr funcgr dty |
|
600 |
##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr pat |
|
601 |
##>> exprgen_term thy algbr funcgr body) (mk_ds cases) |
|
602 |
##>> exprgen_app_default thy algbr funcgr app |
|
603 |
#>> (fn (((dt, dty), ds), t0) => ICase (((dt, dty), ds), t0)) |
|
604 |
end |
|
605 |
and exprgen_app thy algbr funcgr ((c, ty), ts) = case Code.get_case_data thy c |
|
606 |
of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in |
|
607 |
if length ts < i then |
|
608 |
let |
|
609 |
val k = length ts; |
|
610 |
val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty; |
|
611 |
val ctxt = (fold o fold_aterms) |
|
612 |
(fn Free (v, _) => Name.declare v | _ => I) ts Name.context; |
|
613 |
val vs = Name.names ctxt "a" tys; |
|
614 |
in |
|
615 |
fold_map (exprgen_typ thy algbr funcgr) tys |
|
616 |
##>> exprgen_case thy algbr funcgr n cases ((c, ty), ts @ map Free vs) |
|
617 |
#>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t) |
|
618 |
end |
|
619 |
else if length ts > i then |
|
620 |
exprgen_case thy algbr funcgr n cases ((c, ty), Library.take (i, ts)) |
|
621 |
##>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts)) |
|
622 |
#>> (fn (t, ts) => t `$$ ts) |
|
623 |
else |
|
624 |
exprgen_case thy algbr funcgr n cases ((c, ty), ts) |
|
625 |
end |
|
626 |
| NONE => exprgen_app_default thy algbr funcgr ((c, ty), ts); |
|
627 |
||
25969 | 628 |
|
629 |
(** evaluation **) |
|
630 |
||
631 |
fun add_value_stmt (t, ty) code = |
|
632 |
code |
|
633 |
|> Graph.new_node (CodeName.value_name, Fun (([], ty), [(([], t), Drule.dummy_thm)])) |
|
634 |
|> fold (curry Graph.add_edge CodeName.value_name) (Graph.keys code); |
|
635 |
||
24918 | 636 |
fun ensure_value thy algbr funcgr t = |
637 |
let |
|
638 |
val ty = fastype_of t; |
|
639 |
val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =) |
|
640 |
o dest_TFree))) t []; |
|
641 |
val stmt_value = |
|
642 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
643 |
##>> exprgen_typ thy algbr funcgr ty |
|
644 |
##>> exprgen_term thy algbr funcgr t |
|
645 |
#>> (fn ((vs, ty), t) => Fun ((vs, ty), [(([], t), Drule.dummy_thm)])); |
|
26011 | 646 |
fun term_value (dep, code1) = |
25969 | 647 |
let |
648 |
val Fun ((vs, ty), [(([], t), _)]) = |
|
26011 | 649 |
Graph.get_node code1 CodeName.value_name; |
650 |
val deps = Graph.imm_succs code1 CodeName.value_name; |
|
651 |
val code2 = Graph.del_nodes [CodeName.value_name] code1; |
|
652 |
val code3 = project_code false [] (SOME deps) code2; |
|
653 |
in ((code3, (((vs, ty), t), deps)), (dep, code2)) end; |
|
654 |
in |
|
655 |
ensure_stmt stmt_value CodeName.value_name |
|
656 |
#> snd |
|
657 |
#> term_value |
|
658 |
end; |
|
24219 | 659 |
|
660 |
end; (*struct*) |
|
661 |
||
662 |
||
663 |
structure BasicCodeThingol: BASIC_CODE_THINGOL = CodeThingol; |