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.
|
|
6 |
*)
|
|
7 |
|
|
8 |
infix 8 `%%;
|
|
9 |
infixr 6 `->;
|
|
10 |
infixr 6 `-->;
|
|
11 |
infix 4 `$;
|
|
12 |
infix 4 `$$;
|
|
13 |
infixr 3 `|->;
|
|
14 |
infixr 3 `|-->;
|
|
15 |
|
|
16 |
signature BASIC_CODE_THINGOL =
|
|
17 |
sig
|
|
18 |
type vname = string;
|
|
19 |
datatype dict =
|
|
20 |
DictConst of string * dict list list
|
|
21 |
| DictVar of string list * (vname * (int * int));
|
|
22 |
datatype itype =
|
|
23 |
`%% of string * itype list
|
|
24 |
| ITyVar of vname;
|
|
25 |
datatype iterm =
|
|
26 |
IConst of string * (dict list list * itype list (*types of arguments*))
|
|
27 |
| IVar of vname
|
|
28 |
| `$ of iterm * iterm
|
|
29 |
| `|-> of (vname * itype) * iterm
|
|
30 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
|
|
31 |
(*((term, type), [(selector pattern, body term )]), primitive term)*)
|
|
32 |
val `-> : itype * itype -> itype;
|
|
33 |
val `--> : itype list * itype -> itype;
|
|
34 |
val `$$ : iterm * iterm list -> iterm;
|
|
35 |
val `|--> : (vname * itype) list * iterm -> iterm;
|
|
36 |
type typscheme = (vname * sort) list * itype;
|
|
37 |
end;
|
|
38 |
|
|
39 |
signature CODE_THINGOL =
|
|
40 |
sig
|
|
41 |
include BASIC_CODE_THINGOL;
|
|
42 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
|
|
43 |
val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
|
|
44 |
val unfold_fun: itype -> itype list * itype;
|
|
45 |
val unfold_app: iterm -> iterm * iterm list;
|
|
46 |
val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
|
|
47 |
val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
|
|
48 |
val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
|
|
49 |
val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
|
|
50 |
val unfold_const_app: iterm ->
|
|
51 |
((string * (dict list list * itype list)) * iterm list) option;
|
|
52 |
val collapse_let: ((vname * itype) * iterm) * iterm
|
|
53 |
-> (iterm * itype) * (iterm * iterm) list;
|
|
54 |
val eta_expand: (string * (dict list list * itype list)) * iterm list -> int -> iterm;
|
|
55 |
val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
|
|
56 |
val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
|
|
57 |
val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
|
|
58 |
|
|
59 |
datatype def =
|
|
60 |
Bot
|
|
61 |
| Fun of (iterm list * iterm) list * typscheme
|
|
62 |
| Datatype of (vname * sort) list * (string * itype list) list
|
|
63 |
| Datatypecons of string
|
|
64 |
| Class of (class * string) list * (vname * (string * itype) list)
|
|
65 |
| Classop of class
|
|
66 |
| Classrel of class * class
|
|
67 |
| Classinst of (class * (string * (vname * sort) list))
|
|
68 |
* ((class * (string * (string * dict list list))) list
|
|
69 |
* (string * iterm) list);
|
|
70 |
type code = def Graph.T;
|
|
71 |
type transact;
|
|
72 |
val empty_code: code;
|
|
73 |
val merge_code: code * code -> code;
|
|
74 |
val project_code: bool (*delete empty funs*)
|
|
75 |
-> string list (*hidden*) -> string list option (*selected*)
|
|
76 |
-> code -> code;
|
|
77 |
val empty_funs: code -> string list;
|
|
78 |
val is_cons: code -> string -> bool;
|
24283
|
79 |
val add_eval_def: string (*bind name*) * (iterm * itype) -> code -> code;
|
24219
|
80 |
|
24252
|
81 |
val ensure_def: (string -> string) -> (transact -> def * transact) -> string
|
24219
|
82 |
-> string -> transact -> transact;
|
|
83 |
val start_transact: (transact -> 'a * transact) -> code -> 'a * code;
|
|
84 |
end;
|
|
85 |
|
|
86 |
structure CodeThingol: CODE_THINGOL =
|
|
87 |
struct
|
|
88 |
|
|
89 |
(** auxiliary **)
|
|
90 |
|
|
91 |
fun unfoldl dest x =
|
|
92 |
case dest x
|
|
93 |
of NONE => (x, [])
|
|
94 |
| SOME (x1, x2) =>
|
|
95 |
let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
|
|
96 |
|
|
97 |
fun unfoldr dest x =
|
|
98 |
case dest x
|
|
99 |
of NONE => ([], x)
|
|
100 |
| SOME (x1, x2) =>
|
|
101 |
let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
|
|
102 |
|
|
103 |
|
|
104 |
(** language core - types, pattern, expressions **)
|
|
105 |
|
|
106 |
(* language representation *)
|
|
107 |
|
|
108 |
type vname = string;
|
|
109 |
|
|
110 |
datatype dict =
|
|
111 |
DictConst of string * dict list list
|
|
112 |
| DictVar of string list * (vname * (int * int));
|
|
113 |
|
|
114 |
datatype itype =
|
|
115 |
`%% of string * itype list
|
|
116 |
| ITyVar of vname;
|
|
117 |
|
|
118 |
datatype iterm =
|
|
119 |
IConst of string * (dict list list * itype list)
|
|
120 |
| IVar of vname
|
|
121 |
| `$ of iterm * iterm
|
|
122 |
| `|-> of (vname * itype) * iterm
|
|
123 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
|
|
124 |
(*see also signature*)
|
|
125 |
|
|
126 |
(*
|
|
127 |
variable naming conventions
|
|
128 |
|
|
129 |
bare names:
|
|
130 |
variable names v
|
|
131 |
class names class
|
|
132 |
type constructor names tyco
|
|
133 |
datatype names dtco
|
|
134 |
const names (general) c
|
|
135 |
constructor names co
|
|
136 |
class operation names clsop (op)
|
|
137 |
arbitrary name s
|
|
138 |
|
|
139 |
v, c, co, clsop also annotated with types etc.
|
|
140 |
|
|
141 |
constructs:
|
|
142 |
sort sort
|
|
143 |
type parameters vs
|
|
144 |
type ty
|
|
145 |
type schemes tysm
|
|
146 |
term t
|
|
147 |
(term as pattern) p
|
|
148 |
instance (class, tyco) inst
|
|
149 |
*)
|
|
150 |
|
|
151 |
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2];
|
|
152 |
val op `--> = Library.foldr (op `->);
|
|
153 |
val op `$$ = Library.foldl (op `$);
|
|
154 |
val op `|--> = Library.foldr (op `|->);
|
|
155 |
|
|
156 |
val unfold_fun = unfoldr
|
|
157 |
(fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2)
|
|
158 |
| _ => NONE);
|
|
159 |
|
|
160 |
val unfold_app = unfoldl
|
|
161 |
(fn op `$ t => SOME t
|
|
162 |
| _ => NONE);
|
|
163 |
|
|
164 |
val split_abs =
|
|
165 |
(fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) =>
|
|
166 |
if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t)
|
|
167 |
| (v, ty) `|-> t => SOME (((v, NONE), ty), t)
|
|
168 |
| _ => NONE);
|
|
169 |
|
|
170 |
val unfold_abs = unfoldr split_abs;
|
|
171 |
|
|
172 |
val split_let =
|
|
173 |
(fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
|
|
174 |
| _ => NONE);
|
|
175 |
|
|
176 |
val unfold_let = unfoldr split_let;
|
|
177 |
|
|
178 |
fun unfold_const_app t =
|
|
179 |
case unfold_app t
|
|
180 |
of (IConst c, ts) => SOME (c, ts)
|
|
181 |
| _ => NONE;
|
|
182 |
|
|
183 |
fun fold_aiterms f (t as IConst _) = f t
|
|
184 |
| fold_aiterms f (t as IVar _) = f t
|
|
185 |
| fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2
|
|
186 |
| fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t'
|
|
187 |
| fold_aiterms f (ICase (_, t)) = fold_aiterms f t;
|
|
188 |
|
|
189 |
fun fold_constnames f =
|
|
190 |
let
|
|
191 |
fun add (IConst (c, _)) = f c
|
|
192 |
| add _ = I;
|
|
193 |
in fold_aiterms add end;
|
|
194 |
|
|
195 |
fun fold_varnames f =
|
|
196 |
let
|
|
197 |
fun add (IVar v) = f v
|
|
198 |
| add ((v, _) `|-> _) = f v
|
|
199 |
| add _ = I;
|
|
200 |
in fold_aiterms add end;
|
|
201 |
|
|
202 |
fun fold_unbound_varnames f =
|
|
203 |
let
|
|
204 |
fun add _ (IConst _) = I
|
|
205 |
| add vs (IVar v) = if not (member (op =) vs v) then f v else I
|
|
206 |
| add vs (t1 `$ t2) = add vs t1 #> add vs t2
|
|
207 |
| add vs ((v, _) `|-> t) = add (insert (op =) v vs) t
|
|
208 |
| add vs (ICase (_, t)) = add vs t;
|
|
209 |
in add [] end;
|
|
210 |
|
|
211 |
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) =
|
|
212 |
let
|
|
213 |
fun exists_v t = fold_unbound_varnames (fn w => fn b =>
|
|
214 |
b orelse v = w) t false;
|
|
215 |
in if v = w andalso forall (fn (t1, t2) =>
|
|
216 |
exists_v t1 orelse not (exists_v t2)) ds
|
|
217 |
then ((se, ty), ds)
|
|
218 |
else ((se, ty), [(IVar v, be)])
|
|
219 |
end
|
|
220 |
| collapse_let (((v, ty), se), be) =
|
|
221 |
((se, ty), [(IVar v, be)])
|
|
222 |
|
|
223 |
fun eta_expand (c as (_, (_, tys)), ts) k =
|
|
224 |
let
|
|
225 |
val j = length ts;
|
|
226 |
val l = k - j;
|
|
227 |
val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
|
|
228 |
val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys);
|
|
229 |
in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
|
|
230 |
|
|
231 |
|
|
232 |
(** definitions, transactions **)
|
|
233 |
|
|
234 |
(* type definitions *)
|
|
235 |
|
|
236 |
type typscheme = (vname * sort) list * itype;
|
|
237 |
datatype def =
|
|
238 |
Bot
|
|
239 |
| Fun of (iterm list * iterm) list * typscheme
|
|
240 |
| Datatype of (vname * sort) list * (string * itype list) list
|
|
241 |
| Datatypecons of string
|
|
242 |
| Class of (class * string) list * (vname * (string * itype) list)
|
|
243 |
| Classop of class
|
|
244 |
| Classrel of class * class
|
|
245 |
| Classinst of (class * (string * (vname * sort) list))
|
|
246 |
* ((class * (string * (string * dict list list))) list
|
|
247 |
* (string * iterm) list);
|
|
248 |
|
|
249 |
type code = def Graph.T;
|
|
250 |
|
|
251 |
|
|
252 |
(* abstract code *)
|
|
253 |
|
|
254 |
val empty_code = Graph.empty : code; (*read: "depends on"*)
|
|
255 |
|
|
256 |
fun ensure_bot name = Graph.default_node (name, Bot);
|
|
257 |
|
|
258 |
fun add_def_incr (name, Bot) code =
|
|
259 |
(case the_default Bot (try (Graph.get_node code) name)
|
|
260 |
of Bot => error "Attempted to add Bot to code"
|
|
261 |
| _ => code)
|
|
262 |
| add_def_incr (name, def) code =
|
|
263 |
(case try (Graph.get_node code) name
|
|
264 |
of NONE => Graph.new_node (name, def) code
|
|
265 |
| SOME Bot => Graph.map_node name (K def) code
|
|
266 |
| SOME _ => error ("Tried to overwrite definition " ^ quote name));
|
|
267 |
|
|
268 |
fun add_dep (dep as (name1, name2)) =
|
|
269 |
if name1 = name2 then I else Graph.add_edge dep;
|
|
270 |
|
|
271 |
val merge_code : code * code -> code = Graph.merge (K true);
|
|
272 |
|
|
273 |
fun project_code delete_empty_funs hidden raw_selected code =
|
|
274 |
let
|
|
275 |
fun is_empty_fun name = case Graph.get_node code name
|
|
276 |
of Fun ([], _) => true
|
|
277 |
| _ => false;
|
|
278 |
val names = subtract (op =) hidden (Graph.keys code);
|
|
279 |
val deleted = Graph.all_preds code (filter is_empty_fun names);
|
|
280 |
val selected = case raw_selected
|
|
281 |
of NONE => names |> subtract (op =) deleted
|
|
282 |
| SOME sel => sel
|
|
283 |
|> delete_empty_funs ? subtract (op =) deleted
|
|
284 |
|> subtract (op =) hidden
|
|
285 |
|> Graph.all_succs code
|
|
286 |
|> delete_empty_funs ? subtract (op =) deleted
|
|
287 |
|> subtract (op =) hidden;
|
|
288 |
in
|
|
289 |
code
|
|
290 |
|> Graph.subgraph (member (op =) selected)
|
|
291 |
end;
|
|
292 |
|
|
293 |
fun empty_funs code =
|
|
294 |
Graph.fold (fn (name, (Fun ([], _), _)) => cons name
|
|
295 |
| _ => I) code [];
|
|
296 |
|
|
297 |
fun is_cons code name = case Graph.get_node code name
|
|
298 |
of Datatypecons _ => true
|
|
299 |
| _ => false;
|
|
300 |
|
|
301 |
fun check_samemodule names =
|
|
302 |
fold (fn name =>
|
|
303 |
let
|
|
304 |
val module_name = (NameSpace.qualifier o NameSpace.qualifier) name
|
|
305 |
in
|
|
306 |
fn NONE => SOME module_name
|
|
307 |
| SOME module_name' => if module_name = module_name' then SOME module_name
|
|
308 |
else error ("Inconsistent name prefix for simultanous names: " ^ commas_quote names)
|
|
309 |
end
|
|
310 |
) names NONE;
|
|
311 |
|
|
312 |
fun check_funeqs eqs =
|
|
313 |
(fold (fn (pats, _) =>
|
|
314 |
let
|
|
315 |
val l = length pats
|
|
316 |
in
|
|
317 |
fn NONE => SOME l
|
|
318 |
| SOME l' => if l = l' then SOME l
|
|
319 |
else error "Function definition with different number of arguments"
|
|
320 |
end
|
|
321 |
) eqs NONE; eqs);
|
|
322 |
|
|
323 |
fun check_prep_def code Bot =
|
|
324 |
Bot
|
|
325 |
| check_prep_def code (Fun (eqs, d)) =
|
|
326 |
Fun (check_funeqs eqs, d)
|
|
327 |
| check_prep_def code (d as Datatype _) =
|
|
328 |
d
|
|
329 |
| check_prep_def code (Datatypecons dtco) =
|
|
330 |
error "Attempted to add bare term constructor"
|
|
331 |
| check_prep_def code (d as Class _) =
|
|
332 |
d
|
|
333 |
| check_prep_def code (Classop _) =
|
|
334 |
error "Attempted to add bare class operation"
|
|
335 |
| check_prep_def code (Classrel _) =
|
|
336 |
error "Attempted to add bare class relation"
|
|
337 |
| check_prep_def code (d as Classinst ((class, (tyco, arity)), (_, inst_classops))) =
|
|
338 |
let
|
|
339 |
val Class (_, (_, classops)) = Graph.get_node code class;
|
|
340 |
val _ = if length inst_classops > length classops
|
|
341 |
then error "Too many class operations given"
|
|
342 |
else ();
|
|
343 |
fun check_classop (f, _) =
|
|
344 |
if AList.defined (op =) inst_classops f
|
|
345 |
then () else error ("Missing definition for class operation " ^ quote f);
|
|
346 |
val _ = map check_classop classops;
|
|
347 |
in d end;
|
|
348 |
|
|
349 |
fun postprocess_def (name, Datatype (_, constrs)) =
|
|
350 |
tap (fn _ => check_samemodule (name :: map fst constrs))
|
|
351 |
#> fold (fn (co, _) =>
|
|
352 |
add_def_incr (co, Datatypecons name)
|
|
353 |
#> add_dep (co, name)
|
|
354 |
#> add_dep (name, co)
|
|
355 |
) constrs
|
|
356 |
| postprocess_def (name, Class (classrels, (_, classops))) =
|
|
357 |
tap (fn _ => check_samemodule (name :: map fst classops @ map snd classrels))
|
|
358 |
#> fold (fn (f, _) =>
|
|
359 |
add_def_incr (f, Classop name)
|
|
360 |
#> add_dep (f, name)
|
|
361 |
#> add_dep (name, f)
|
|
362 |
) classops
|
|
363 |
#> fold (fn (superclass, classrel) =>
|
|
364 |
add_def_incr (classrel, Classrel (name, superclass))
|
|
365 |
#> add_dep (classrel, name)
|
|
366 |
#> add_dep (name, classrel)
|
|
367 |
) classrels
|
|
368 |
| postprocess_def _ =
|
|
369 |
I;
|
|
370 |
|
|
371 |
|
|
372 |
(* transaction protocol *)
|
|
373 |
|
24283
|
374 |
type transact = Graph.key option * code;
|
|
375 |
|
24219
|
376 |
fun ensure_def labelled_name defgen msg name (dep, code) =
|
|
377 |
let
|
|
378 |
val msg' = (case dep
|
|
379 |
of NONE => msg
|
|
380 |
| SOME dep => msg ^ ", required for " ^ labelled_name dep);
|
|
381 |
fun add_dp NONE = I
|
|
382 |
| add_dp (SOME dep) = add_dep (dep, name);
|
|
383 |
fun prep_def def code =
|
|
384 |
(check_prep_def code def, code);
|
|
385 |
fun add_def false =
|
|
386 |
ensure_bot name
|
|
387 |
#> add_dp dep
|
24252
|
388 |
#> curry defgen (SOME name)
|
|
389 |
##> snd
|
24219
|
390 |
#-> (fn def => prep_def def)
|
|
391 |
#-> (fn def => add_def_incr (name, def)
|
|
392 |
#> postprocess_def (name, def))
|
|
393 |
| add_def true =
|
|
394 |
add_dp dep;
|
|
395 |
in
|
|
396 |
code
|
|
397 |
|> add_def (can (Graph.get_node code) name)
|
|
398 |
|> pair dep
|
|
399 |
end;
|
|
400 |
|
|
401 |
fun start_transact f code =
|
24252
|
402 |
(NONE, code)
|
|
403 |
|> f
|
|
404 |
|-> (fn x => fn (_, code) => (x, code));
|
24219
|
405 |
|
24283
|
406 |
fun add_eval_def (name, (t, ty)) code =
|
24219
|
407 |
code
|
24283
|
408 |
|> Graph.new_node (name, Fun ([([], t)], ([], ty)))
|
24219
|
409 |
|> fold (curry Graph.add_edge name) (Graph.keys code);
|
|
410 |
|
|
411 |
end; (*struct*)
|
|
412 |
|
|
413 |
|
|
414 |
structure BasicCodeThingol: BASIC_CODE_THINGOL = CodeThingol;
|