author | haftmann |
Tue, 19 Sep 2006 15:22:26 +0200 | |
changeset 20600 | 6d75e02ed285 |
parent 20466 | 7c20ddbd911b |
child 20709 | 645236e80885 |
permissions | -rw-r--r-- |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_thingol.ML |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
5 |
Intermediate language ("Thin-gol") for code extraction. |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
6 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
8 |
infix 8 `%%; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
9 |
infixr 6 `->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
10 |
infixr 6 `-->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
11 |
infix 4 `$; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
12 |
infix 4 `$$; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
13 |
infixr 3 `|->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
14 |
infixr 3 `|-->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
15 |
|
19136 | 16 |
signature BASIC_CODEGEN_THINGOL = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
17 |
sig |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
18 |
type vname = string; |
20456 | 19 |
datatype inst = |
20 |
Instance of string * inst list list |
|
21 |
| Context of class list * (vname * int); |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
22 |
datatype itype = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
23 |
`%% of string * itype list |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
24 |
| `-> of itype * itype |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
25 |
| ITyVar of vname; |
20105 | 26 |
datatype iterm = |
20456 | 27 |
IConst of string * (inst list list * itype) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
28 |
| IVar of vname |
20105 | 29 |
| `$ of iterm * iterm |
30 |
| `|-> of (vname * itype) * iterm |
|
20600 | 31 |
| INum of IntInf.int * iterm |
20105 | 32 |
| IChar of string (*length one!*) * iterm |
33 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
20439 | 34 |
(*((discriminendum term (td), discriminendum type (ty)), |
35 |
[(selector pattern (p), body term (t))] (bs)), |
|
36 |
pure term (t0))*) |
|
19136 | 37 |
end; |
38 |
||
39 |
signature CODEGEN_THINGOL = |
|
40 |
sig |
|
41 |
include BASIC_CODEGEN_THINGOL; |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
42 |
val `--> : itype list * itype -> itype; |
20105 | 43 |
val `$$ : iterm * iterm list -> iterm; |
44 |
val `|--> : (vname * itype) list * iterm -> iterm; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
45 |
val pretty_itype: itype -> Pretty.T; |
20105 | 46 |
val pretty_iterm: iterm -> Pretty.T; |
18216 | 47 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list; |
48 |
val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
49 |
val unfold_fun: itype -> itype list * itype; |
20105 | 50 |
val unfold_app: iterm -> iterm * iterm list; |
51 |
val unfold_abs: iterm -> (iterm * itype) list * iterm; |
|
52 |
val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm; |
|
53 |
val unfold_const_app: iterm -> |
|
20456 | 54 |
((string * (inst list list * itype)) * iterm list) option; |
20105 | 55 |
val add_constnames: iterm -> string list -> string list; |
56 |
val add_varnames: iterm -> string list -> string list; |
|
57 |
val is_pat: (string -> bool) -> iterm -> bool; |
|
58 |
val vars_distinct: iterm list -> bool; |
|
59 |
val map_pure: (iterm -> 'a) -> iterm -> 'a; |
|
20456 | 60 |
val eta_expand: (string * (inst list list * itype)) * iterm list -> int -> iterm; |
20105 | 61 |
val resolve_tycos: (string -> string) -> itype * iterm list -> itype * iterm list; |
62 |
val resolve_consts: (string -> string) -> iterm -> iterm; |
|
18170 | 63 |
|
20456 | 64 |
type typscheme = (vname * sort) list * itype; |
18170 | 65 |
datatype def = |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
66 |
Bot |
20456 | 67 |
| Fun of (iterm list * iterm) list * typscheme |
68 |
| Typesyn of typscheme |
|
69 |
| Datatype of (vname * sort) list * (string * itype list) list |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
70 |
| Datatypecons of string |
20456 | 71 |
| Class of class list * (vname * (string * itype) list) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
72 |
| Classmember of class |
20389 | 73 |
| Classinst of (class * (string * (vname * sort) list)) |
20466 | 74 |
* ((class * (string * inst list list)) list |
20389 | 75 |
* (string * iterm) list); |
18170 | 76 |
type module; |
77 |
type transact; |
|
78 |
type 'dst transact_fin; |
|
79 |
val pretty_def: def -> Pretty.T; |
|
20191 | 80 |
val pretty_module: module -> Pretty.T; |
18360 | 81 |
val pretty_deps: module -> Pretty.T; |
18170 | 82 |
val empty_module: module; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
83 |
val get_def: module -> string -> def; |
18170 | 84 |
val merge_module: module * module -> module; |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
85 |
val diff_module: module * module -> (string * def) list; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
86 |
val project_module: string list -> module -> module; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
87 |
val purge_module: string list -> module -> module; |
20428 | 88 |
(* val flat_funs_datatypes: module -> (string * def) list; *) |
20216 | 89 |
val add_eval_def: string (*shallow name space*) * iterm -> module -> string * module; |
20191 | 90 |
val delete_garbage: string list (*hidden definitions*) -> module -> module; |
18516 | 91 |
val has_nsp: string -> string -> bool; |
19884 | 92 |
val ensure_def: (string -> transact -> def transact_fin) -> bool -> string |
93 |
-> string -> transact -> transact; |
|
18170 | 94 |
val succeed: 'a -> transact -> 'a transact_fin; |
95 |
val fail: string -> transact -> 'a transact_fin; |
|
19884 | 96 |
val message: string -> (transact -> 'a) -> transact -> 'a; |
18963 | 97 |
val start_transact: string option -> (transact -> 'a * transact) -> module -> 'a * module; |
20456 | 98 |
val elim_classes: module -> (iterm list * iterm) list * typscheme -> (iterm list * iterm) list * itype; |
18216 | 99 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
100 |
val debug: bool ref; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
101 |
val debug_msg: ('a -> string) -> 'a -> 'a; |
18231 | 102 |
val soft_exc: bool ref; |
18216 | 103 |
|
104 |
val serialize: |
|
19038 | 105 |
((string -> string -> string) -> string -> (string * def) list -> 'a option) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
106 |
-> ((string -> string) -> string list -> (string * string) * 'a list -> 'a option) |
18216 | 107 |
-> (string -> string option) |
18919 | 108 |
-> (string * string -> string) |
18850 | 109 |
-> string list list -> string -> module -> 'a option; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
110 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
111 |
|
18850 | 112 |
structure CodegenThingol: CODEGEN_THINGOL = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
113 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
114 |
|
18170 | 115 |
(** auxiliary **) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
116 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
117 |
val debug = ref false; |
20405 | 118 |
fun debug_msg f x = (if !debug then Output.tracing (f x) else (); x); |
18231 | 119 |
val soft_exc = ref true; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
120 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
121 |
fun unfoldl dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
122 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
123 |
of NONE => (x, []) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
124 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
125 |
let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
126 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
127 |
fun unfoldr dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
128 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
129 |
of NONE => ([], x) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
130 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
131 |
let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
132 |
|
18170 | 133 |
|
134 |
||
135 |
(** language core - types, pattern, expressions **) |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
136 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
137 |
(* language representation *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
138 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
139 |
type vname = string; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
140 |
|
20456 | 141 |
datatype inst = |
142 |
Instance of string * inst list list |
|
143 |
| Context of class list * (vname * int); |
|
18885 | 144 |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
145 |
datatype itype = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
146 |
`%% of string * itype list |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
147 |
| `-> of itype * itype |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
148 |
| ITyVar of vname; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
149 |
|
20105 | 150 |
datatype iterm = |
20456 | 151 |
IConst of string * (inst list list * itype) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
152 |
| IVar of vname |
20105 | 153 |
| `$ of iterm * iterm |
154 |
| `|-> of (vname * itype) * iterm |
|
155 |
| INum of IntInf.int * iterm |
|
156 |
| IChar of string * iterm |
|
157 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
158 |
(*see also signature*) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
159 |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
160 |
(* |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
161 |
variable naming conventions |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
162 |
|
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
163 |
bare names: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
164 |
variable names v |
20439 | 165 |
class names class |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
166 |
type constructor names tyco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
167 |
datatype names dtco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
168 |
const names (general) c |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
169 |
constructor names co |
20439 | 170 |
class operation names clsop (op) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
171 |
arbitrary name s |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
172 |
|
20439 | 173 |
v, c, co, clsop also annotated with types usw. |
174 |
||
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
175 |
constructs: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
176 |
sort sort |
20456 | 177 |
type parameters vs |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
178 |
type ty |
20456 | 179 |
type schemes tysm |
20439 | 180 |
term t |
181 |
(term as pattern) p |
|
182 |
instance (classs, tyco) inst |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
183 |
*) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
184 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
185 |
val op `--> = Library.foldr (op `->); |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
186 |
val op `$$ = Library.foldl (op `$); |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
187 |
val op `|--> = Library.foldr (op `|->); |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
188 |
|
20456 | 189 |
val pretty_typparms = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
190 |
Pretty.list "(" ")" o Pretty.commas o map (fn (v, sort) => (Pretty.block o Pretty.breaks) |
19150 | 191 |
[Pretty.str v, Pretty.str "::", Pretty.enum "&" "" "" (map Pretty.str sort)]); |
192 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
193 |
fun pretty_itype (tyco `%% tys) = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
194 |
Pretty.enum "" "(" ")" (Pretty.str tyco :: map pretty_itype tys) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
195 |
| pretty_itype (ty1 `-> ty2) = |
18885 | 196 |
Pretty.enum "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2] |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
197 |
| pretty_itype (ITyVar v) = |
19150 | 198 |
Pretty.str v; |
18885 | 199 |
|
20105 | 200 |
fun pretty_iterm (IConst (c, _)) = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
201 |
Pretty.str c |
20105 | 202 |
| pretty_iterm (IVar v) = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
203 |
Pretty.str ("?" ^ v) |
20439 | 204 |
| pretty_iterm (t1 `$ t2) = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
205 |
(Pretty.enclose "(" ")" o Pretty.breaks) |
20439 | 206 |
[pretty_iterm t1, pretty_iterm t2] |
207 |
| pretty_iterm ((v, ty) `|-> t) = |
|
20105 | 208 |
(Pretty.enclose "(" ")" o Pretty.breaks) |
20439 | 209 |
[Pretty.str v, Pretty.str "::", pretty_itype ty, Pretty.str "|->", pretty_iterm t] |
20105 | 210 |
| pretty_iterm (INum (n, _)) = |
19202 | 211 |
(Pretty.str o IntInf.toString) n |
20439 | 212 |
| pretty_iterm (IChar (h, _)) = |
213 |
(Pretty.str o quote) h |
|
214 |
| pretty_iterm (ICase (((t, _), bs), _)) = |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
215 |
(Pretty.enclose "(" ")" o Pretty.breaks) [ |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
216 |
Pretty.str "case", |
20439 | 217 |
pretty_iterm t, |
218 |
Pretty.enclose "(" ")" (map (fn (p, t) => |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
219 |
(Pretty.block o Pretty.breaks) [ |
20105 | 220 |
pretty_iterm p, |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
221 |
Pretty.str "=>", |
20439 | 222 |
pretty_iterm t |
18885 | 223 |
] |
20439 | 224 |
) bs) |
18885 | 225 |
]; |
226 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
227 |
val unfold_fun = unfoldr |
20439 | 228 |
(fn op `-> ty => SOME ty |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
229 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
230 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
231 |
val unfold_app = unfoldl |
20439 | 232 |
(fn op `$ t => SOME t |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
233 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
234 |
|
18282 | 235 |
val unfold_abs = unfoldr |
20439 | 236 |
(fn (v, ty) `|-> (e as ICase (((IVar w, _), [(p, t)]), _)) => |
237 |
if v = w then SOME ((p, ty), t) else SOME ((IVar v, ty), t) |
|
238 |
| (v, ty) `|-> t => SOME ((IVar v, ty), t) |
|
18282 | 239 |
| _ => NONE) |
240 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
241 |
val unfold_let = unfoldr |
20439 | 242 |
(fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
243 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
244 |
|
20439 | 245 |
fun unfold_const_app t = |
246 |
case unfold_app t |
|
247 |
of (IConst c, ts) => SOME (c, ts) |
|
18865 | 248 |
| _ => NONE; |
249 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
250 |
fun map_itype _ (ty as ITyVar _) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
251 |
ty |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
252 |
| map_itype f (tyco `%% tys) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
253 |
tyco `%% map f tys |
20439 | 254 |
| map_itype f (ty1 `-> ty2) = |
255 |
f ty1 `-> f ty2; |
|
18172 | 256 |
|
20456 | 257 |
fun eq_ityp ((vs1, ty1), (vs2, ty2)) = |
18282 | 258 |
let |
259 |
exception NO_MATCH; |
|
20456 | 260 |
fun eq_typparms subs vs1 vs2 = |
19597 | 261 |
map (fn (v : string, sort : string list) => case AList.lookup (op =) subs v |
19150 | 262 |
of NONE => raise NO_MATCH |
20456 | 263 |
| SOME (v' : string) => case AList.lookup (op =) vs2 v' |
19150 | 264 |
of NONE => raise NO_MATCH |
20456 | 265 |
| SOME sort' => if sort <> sort' then raise NO_MATCH else ()) vs1 |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
266 |
fun eq (ITyVar v1) (ITyVar v2) subs = |
19150 | 267 |
(case AList.lookup (op =) subs v1 |
268 |
of NONE => subs |> AList.update (op =) (v1, v2) |
|
269 |
| SOME v1' => |
|
270 |
if v1' <> v2 |
|
271 |
then raise NO_MATCH |
|
272 |
else subs) |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
273 |
| eq (tyco1 `%% tys1) (tyco2 `%% tys2) subs = |
18282 | 274 |
if tyco1 <> tyco2 |
275 |
then raise NO_MATCH |
|
276 |
else subs |> fold2 eq tys1 tys2 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
277 |
| eq (ty11 `-> ty12) (ty21 `-> ty22) subs = |
18282 | 278 |
subs |> eq ty11 ty21 |> eq ty12 ty22 |
279 |
| eq _ _ _ = raise NO_MATCH; |
|
280 |
in |
|
20456 | 281 |
(eq_typparms vs1 vs2; eq ty1 ty2 []; true) |
18282 | 282 |
handle NO_MATCH => false |
283 |
end; |
|
284 |
||
18885 | 285 |
fun instant_itype f = |
286 |
let |
|
20439 | 287 |
fun instant (ITyVar v) = f v |
288 |
| instant ty = map_itype instant ty; |
|
19215 | 289 |
in instant end; |
18885 | 290 |
|
20456 | 291 |
fun is_pat is_cons (IConst (c, _)) = is_cons c |
20439 | 292 |
| is_pat _ (IVar _) = true |
293 |
| is_pat is_cons (t1 `$ t2) = |
|
294 |
is_pat is_cons t1 andalso is_pat is_cons t2 |
|
295 |
| is_pat _ (INum _) = true |
|
296 |
| is_pat _ (IChar _) = true |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19937
diff
changeset
|
297 |
| is_pat _ _ = false; |
19202 | 298 |
|
20439 | 299 |
fun map_pure f (t as IConst _) = |
300 |
f t |
|
301 |
| map_pure f (t as IVar _) = |
|
302 |
f t |
|
303 |
| map_pure f (t as _ `$ _) = |
|
304 |
f t |
|
305 |
| map_pure f (t as _ `|-> _) = |
|
306 |
f t |
|
307 |
| map_pure f (INum (_, t0)) = |
|
308 |
f t0 |
|
309 |
| map_pure f (IChar (_, t0)) = |
|
310 |
f t0 |
|
311 |
| map_pure f (ICase (_, t0)) = |
|
312 |
f t0; |
|
18912 | 313 |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
314 |
fun resolve_tycos _ = error ""; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
315 |
fun resolve_consts _ = error ""; |
18216 | 316 |
|
19202 | 317 |
fun add_constnames (IConst (c, _)) = |
318 |
insert (op =) c |
|
319 |
| add_constnames (IVar _) = |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
320 |
I |
20439 | 321 |
| add_constnames (t1 `$ t2) = |
322 |
add_constnames t1 #> add_constnames t2 |
|
323 |
| add_constnames (_ `|-> t) = |
|
324 |
add_constnames t |
|
325 |
| add_constnames (INum (_, t0)) = |
|
326 |
add_constnames t0 |
|
327 |
| add_constnames (IChar (_, t0)) = |
|
328 |
add_constnames t0 |
|
329 |
| add_constnames (ICase (_, t0)) = |
|
330 |
add_constnames t0; |
|
19202 | 331 |
|
332 |
fun add_varnames (IConst _) = |
|
333 |
I |
|
334 |
| add_varnames (IVar v) = |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
335 |
insert (op =) v |
20439 | 336 |
| add_varnames (t1 `$ t2) = |
337 |
add_varnames t1 #> add_varnames t2 |
|
338 |
| add_varnames ((v, _) `|-> t) = |
|
339 |
insert (op =) v #> add_varnames t |
|
340 |
| add_varnames (INum (_, t)) = |
|
341 |
add_varnames t |
|
342 |
| add_varnames (IChar (_, t)) = |
|
343 |
add_varnames t |
|
344 |
| add_varnames (ICase (((td, _), bs), _)) = |
|
345 |
add_varnames td #> fold (fn (p, t) => add_varnames p #> add_varnames t) bs; |
|
18885 | 346 |
|
20439 | 347 |
fun vars_distinct ts = |
18885 | 348 |
let |
20105 | 349 |
fun distinct _ NONE = |
350 |
NONE |
|
351 |
| distinct (IConst _) x = |
|
352 |
x |
|
353 |
| distinct (IVar v) (SOME vs) = |
|
354 |
if member (op =) vs v then NONE else SOME (v::vs) |
|
20439 | 355 |
| distinct (t1 `$ t2) x = |
356 |
x |> distinct t1 |> distinct t2 |
|
357 |
| distinct (_ `|-> t) x = |
|
358 |
x |> distinct t |
|
20105 | 359 |
| distinct (INum _) x = |
360 |
x |
|
361 |
| distinct (IChar _) x = |
|
362 |
x |
|
20439 | 363 |
| distinct (ICase (((td, _), bs), _)) x = |
364 |
x |> distinct td |> fold (fn (p, t) => distinct p #> distinct t) bs; |
|
365 |
in is_some (fold distinct ts (SOME [])) end; |
|
20105 | 366 |
|
20439 | 367 |
fun eta_expand (c as (_, (_, ty)), ts) k = |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
368 |
let |
20439 | 369 |
val j = length ts; |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
370 |
val l = k - j; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
371 |
val tys = (curry Library.take l o curry Library.drop j o fst o unfold_fun) ty; |
20439 | 372 |
val vs_tys = Name.names (fold Name.declare (fold add_varnames ts []) Name.context) "a" tys; |
373 |
in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end; |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
374 |
|
18304 | 375 |
|
18282 | 376 |
|
18170 | 377 |
(** language module system - definitions, modules, transactions **) |
378 |
||
379 |
(* type definitions *) |
|
380 |
||
20456 | 381 |
type typscheme = (vname * sort) list * itype; |
18170 | 382 |
datatype def = |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
383 |
Bot |
20456 | 384 |
| Fun of (iterm list * iterm) list * typscheme |
385 |
| Typesyn of typscheme |
|
386 |
| Datatype of (vname * sort) list * (string * itype list) list |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
387 |
| Datatypecons of string |
20456 | 388 |
| Class of class list * (vname * (string * itype) list) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
389 |
| Classmember of class |
20389 | 390 |
| Classinst of (class * (string * (vname * sort) list)) |
20466 | 391 |
* ((class * (string * inst list list)) list |
20456 | 392 |
* (string * iterm) list); |
18170 | 393 |
|
394 |
datatype node = Def of def | Module of node Graph.T; |
|
395 |
type module = node Graph.T; |
|
18702 | 396 |
type transact = Graph.key option * module; |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
397 |
type 'dst transact_fin = 'dst * module; |
18231 | 398 |
exception FAIL of string list * exn option; |
18170 | 399 |
|
19597 | 400 |
val eq_def = (op =) : def * def -> bool; |
18170 | 401 |
|
402 |
(* simple diagnosis *) |
|
403 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
404 |
fun pretty_def Bot = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
405 |
Pretty.str "<Bot>" |
20456 | 406 |
| pretty_def (Fun (eqs, (vs, ty))) = |
18812 | 407 |
Pretty.enum " |" "" "" ( |
18170 | 408 |
map (fn (ps, body) => |
409 |
Pretty.block [ |
|
20105 | 410 |
Pretty.enum "," "[" "]" (map pretty_iterm ps), |
18170 | 411 |
Pretty.str " |->", |
412 |
Pretty.brk 1, |
|
20105 | 413 |
pretty_iterm body, |
18170 | 414 |
Pretty.str "::", |
20456 | 415 |
pretty_typparms vs, |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
416 |
Pretty.str "/", |
18170 | 417 |
pretty_itype ty |
418 |
]) eqs |
|
419 |
) |
|
18172 | 420 |
| pretty_def (Typesyn (vs, ty)) = |
18170 | 421 |
Pretty.block [ |
20456 | 422 |
pretty_typparms vs, |
18170 | 423 |
Pretty.str " |=> ", |
424 |
pretty_itype ty |
|
425 |
] |
|
19038 | 426 |
| pretty_def (Datatype (vs, cs)) = |
18170 | 427 |
Pretty.block [ |
20456 | 428 |
pretty_typparms vs, |
18170 | 429 |
Pretty.str " |=> ", |
18852 | 430 |
Pretty.enum " |" "" "" |
18850 | 431 |
(map (fn (c, tys) => (Pretty.block o Pretty.breaks) |
19038 | 432 |
(Pretty.str c :: map pretty_itype tys)) cs) |
18170 | 433 |
] |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
434 |
| pretty_def (Datatypecons dtname) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
435 |
Pretty.str ("cons " ^ dtname) |
19038 | 436 |
| pretty_def (Class (supcls, (v, mems))) = |
18170 | 437 |
Pretty.block [ |
20386 | 438 |
Pretty.str ("class var " ^ v ^ " extending "), |
18812 | 439 |
Pretty.enum "," "[" "]" (map Pretty.str supcls), |
18282 | 440 |
Pretty.str " with ", |
18852 | 441 |
Pretty.enum "," "[" "]" |
20456 | 442 |
(map (fn (m, ty) => Pretty.block |
19038 | 443 |
[Pretty.str (m ^ "::"), pretty_itype ty]) mems) |
18231 | 444 |
] |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
445 |
| pretty_def (Classmember clsname) = |
18231 | 446 |
Pretty.block [ |
447 |
Pretty.str "class member belonging to ", |
|
18282 | 448 |
Pretty.str clsname |
18231 | 449 |
] |
20389 | 450 |
| pretty_def (Classinst ((clsname, (tyco, arity)), _)) = |
18231 | 451 |
Pretty.block [ |
452 |
Pretty.str "class instance (", |
|
18282 | 453 |
Pretty.str clsname, |
18231 | 454 |
Pretty.str ", (", |
455 |
Pretty.str tyco, |
|
456 |
Pretty.str ", ", |
|
18852 | 457 |
Pretty.enum "," "[" "]" (map (Pretty.enum "," "{" "}" o |
18850 | 458 |
map Pretty.str o snd) arity), |
18515 | 459 |
Pretty.str "))" |
20389 | 460 |
]; |
18170 | 461 |
|
462 |
fun pretty_module modl = |
|
463 |
let |
|
464 |
fun pretty (name, Module modl) = |
|
465 |
Pretty.block ( |
|
466 |
Pretty.str ("module " ^ name ^ " {") |
|
467 |
:: Pretty.brk 1 |
|
468 |
:: Pretty.chunks (map pretty (AList.make (Graph.get_node modl) |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
469 |
(Graph.strong_conn modl |> flat |> rev))) |
18170 | 470 |
:: Pretty.str "}" :: nil |
471 |
) |
|
472 |
| pretty (name, Def def) = |
|
473 |
Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def] |
|
474 |
in pretty ("//", Module modl) end; |
|
475 |
||
18360 | 476 |
fun pretty_deps modl = |
477 |
let |
|
478 |
fun one_node key = |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
479 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
480 |
val preds_ = Graph.imm_preds modl key; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
481 |
val succs_ = Graph.imm_succs modl key; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
482 |
val mutbs = gen_inter (op =) (preds_, succs_); |
19300 | 483 |
val preds = subtract (op =) mutbs preds_; |
484 |
val succs = subtract (op =) mutbs succs_; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
485 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
486 |
(Pretty.block o Pretty.fbreaks) ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
487 |
Pretty.str key |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
488 |
:: map (fn s => Pretty.str ("<-> " ^ s)) mutbs |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
489 |
@ map (fn s => Pretty.str ("<-- " ^ s)) preds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
490 |
@ map (fn s => Pretty.str ("--> " ^ s)) succs |
18850 | 491 |
@ (the_list oo Option.mapPartial) |
492 |
((fn Module modl' => SOME (pretty_deps modl') |
|
493 |
| _ => NONE) o Graph.get_node modl) (SOME key) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
494 |
) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
495 |
end |
18360 | 496 |
in |
497 |
modl |
|
498 |
|> Graph.strong_conn |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
499 |
|> flat |
18360 | 500 |
|> rev |
501 |
|> map one_node |
|
502 |
|> Pretty.chunks |
|
503 |
end; |
|
504 |
||
18170 | 505 |
|
506 |
(* name handling *) |
|
507 |
||
508 |
fun dest_name name = |
|
509 |
let |
|
510 |
val name' = NameSpace.unpack name |
|
511 |
val (name'', name_base) = split_last name' |
|
512 |
val (modl, shallow) = split_last name'' |
|
513 |
in (modl, NameSpace.pack [shallow, name_base]) end |
|
20389 | 514 |
handle Empty => error ("Not a qualified name: " ^ quote name); |
18170 | 515 |
|
18516 | 516 |
fun has_nsp name shallow = |
517 |
NameSpace.is_qualified name |
|
518 |
andalso let |
|
519 |
val name' = NameSpace.unpack name |
|
520 |
val (name'', _) = split_last name' |
|
521 |
val (_, shallow') = split_last name'' |
|
522 |
in shallow' = shallow end; |
|
523 |
||
18170 | 524 |
fun dest_modl (Module m) = m; |
525 |
fun dest_def (Def d) = d; |
|
526 |
||
527 |
||
528 |
(* modules *) |
|
529 |
||
530 |
val empty_module = Graph.empty; (*read: "depends on"*) |
|
531 |
||
532 |
fun get_def modl name = |
|
533 |
case dest_name name |
|
534 |
of (modlname, base) => |
|
535 |
let |
|
536 |
fun get (Module node) [] = |
|
537 |
(dest_def o Graph.get_node node) base |
|
538 |
| get (Module node) (m::ms) = |
|
539 |
get (Graph.get_node node m) ms |
|
540 |
in get (Module modl) modlname end; |
|
541 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
542 |
fun is_def modl name = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
543 |
case try (get_def modl) name |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
544 |
of NONE => false |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
545 |
| SOME Bot => false |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
546 |
| _ => true; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
547 |
|
18170 | 548 |
fun add_def (name, def) = |
549 |
let |
|
550 |
val (modl, base) = dest_name name; |
|
551 |
fun add [] = |
|
552 |
Graph.new_node (base, Def def) |
|
553 |
| add (m::ms) = |
|
554 |
Graph.default_node (m, Module empty_module) |
|
555 |
#> Graph.map_node m (Module o add ms o dest_modl) |
|
556 |
in add modl end; |
|
557 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
558 |
fun map_def name f = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
559 |
let |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
560 |
val (modl, base) = dest_name name; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
561 |
fun mapp [] = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
562 |
Graph.map_node base (Def o f o dest_def) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
563 |
| mapp (m::ms) = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
564 |
Graph.map_node m (Module o mapp ms o dest_modl) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
565 |
in mapp modl end; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
566 |
|
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
567 |
fun ensure_bot name = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
568 |
let |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
569 |
val (modl, base) = dest_name name; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
570 |
fun ensure [] module = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
571 |
(case try (Graph.get_node module) base |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
572 |
of NONE => |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
573 |
module |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
574 |
|> Graph.new_node (base, Def Bot) |
20389 | 575 |
| SOME (Module _) => error ("Module already present: " ^ quote name) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
576 |
| _ => module) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
577 |
| ensure (m::ms) module = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
578 |
module |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
579 |
|> Graph.default_node (m, Module empty_module) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
580 |
|> Graph.map_node m (Module o ensure ms o dest_modl) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
581 |
in ensure modl end; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
582 |
|
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
583 |
fun add_def_incr strict (name, Bot) module = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
584 |
(case try (get_def module) name |
20389 | 585 |
of NONE => if strict then error "Attempted to add Bot to module" |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
586 |
else map_def name (K Bot) module |
20389 | 587 |
| SOME Bot => if strict then error "Attempted to add Bot to module" |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
588 |
else map_def name (K Bot) module |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
589 |
| SOME _ => module) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
590 |
| add_def_incr _ (name, def) module = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
591 |
(case try (get_def module) name |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
592 |
of NONE => add_def (name, def) module |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
593 |
| SOME Bot => map_def name (K def) module |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
594 |
| SOME def' => if eq_def (def, def') |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
595 |
then module |
20389 | 596 |
else error ("Tried to overwrite definition " ^ quote name)); |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
597 |
|
18170 | 598 |
fun add_dep (name1, name2) modl = |
599 |
if name1 = name2 then modl |
|
600 |
else |
|
601 |
let |
|
602 |
val m1 = dest_name name1 |> apsnd single |> (op @); |
|
603 |
val m2 = dest_name name2 |> apsnd single |> (op @); |
|
20105 | 604 |
val (ms, (r1, r2)) = chop_prefix (op =) (m1, m2); |
605 |
val (ms, (s1::r1, s2::r2)) = chop_prefix (op =) (m1, m2); |
|
18170 | 606 |
val add_edge = |
607 |
if null r1 andalso null r2 |
|
608 |
then Graph.add_edge |
|
19785 | 609 |
else fn edge => fn gr => (Graph.add_edge_acyclic edge gr |
19884 | 610 |
handle Graph.CYCLES _ => |
20389 | 611 |
error ("Adding dependency " |
19884 | 612 |
^ quote name1 ^ " -> " ^ quote name2 ^ " would result in module dependency cycle")) |
18170 | 613 |
fun add [] node = |
614 |
node |
|
615 |
|> add_edge (s1, s2) |
|
616 |
| add (m::ms) node = |
|
617 |
node |
|
618 |
|> Graph.map_node m (Module o add ms o dest_modl); |
|
619 |
in add ms modl end; |
|
620 |
||
621 |
fun merge_module modl12 = |
|
622 |
let |
|
19025 | 623 |
fun join_module _ (Module m1, Module m2) = |
624 |
Module (merge_module (m1, m2)) |
|
625 |
| join_module name (Def d1, Def d2) = |
|
19884 | 626 |
if eq_def (d1, d2) then Def d1 else Def Bot |
19025 | 627 |
| join_module name _ = raise Graph.DUP name |
628 |
in Graph.join join_module modl12 end; |
|
18170 | 629 |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
630 |
fun diff_module modl12 = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
631 |
let |
20191 | 632 |
fun diff_entry prefix modl2 (name, Def def1) = |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
633 |
let |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
634 |
val e2 = try (Graph.get_node modl2) name |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
635 |
in if is_some e2 andalso eq_def (def1, (dest_def o the) e2) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
636 |
then I |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
637 |
else cons (NameSpace.pack (prefix @ [name]), def1) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
638 |
end |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
639 |
| diff_entry prefix modl2 (name, Module modl1) = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
640 |
diff_modl (prefix @ [name]) (modl1, |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
641 |
(the_default empty_module o Option.map dest_modl o try (Graph.get_node modl2)) name) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
642 |
and diff_modl prefix (modl1, modl2) = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
643 |
fold (diff_entry prefix modl2) |
19466 | 644 |
((AList.make (Graph.get_node modl1) o flat o Graph.strong_conn) modl1) |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
645 |
in diff_modl [] modl12 [] end; |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
646 |
|
19884 | 647 |
fun project_module names modl = |
18335 | 648 |
let |
649 |
datatype pathnode = PN of (string list * (string * pathnode) list); |
|
650 |
fun mk_ipath ([], base) (PN (defs, modls)) = |
|
651 |
PN (base :: defs, modls) |
|
652 |
| mk_ipath (n::ns, base) (PN (defs, modls)) = |
|
653 |
modls |
|
654 |
|> AList.default (op =) (n, PN ([], [])) |
|
655 |
|> AList.map_entry (op =) n (mk_ipath (ns, base)) |
|
656 |
|> (pair defs #> PN); |
|
657 |
fun select (PN (defs, modls)) (Module module) = |
|
658 |
module |
|
20191 | 659 |
|> Graph.project (member (op =) ((*!*) Graph.all_succs module (defs @ map fst modls))) |
18335 | 660 |
|> fold (fn (name, modls) => Graph.map_node name (select modls)) modls |
661 |
|> Module; |
|
662 |
in |
|
663 |
Module modl |
|
18850 | 664 |
|> select (fold (mk_ipath o dest_name) |
665 |
(filter NameSpace.is_qualified names) (PN ([], []))) |
|
18335 | 666 |
|> dest_modl |
667 |
end; |
|
18170 | 668 |
|
19884 | 669 |
fun purge_module names modl = |
18702 | 670 |
let |
19884 | 671 |
fun split_names names = |
672 |
fold |
|
673 |
(fn ([], name) => apfst (cons name) |
|
674 |
| (m::ms, name) => apsnd (AList.default (op =) (m : string, []) |
|
675 |
#> AList.map_entry (op =) m (cons (ms, name)))) |
|
676 |
names ([], []); |
|
677 |
fun purge names (Module modl) = |
|
678 |
let |
|
679 |
val (ndefs, nmodls) = split_names names; |
|
680 |
in |
|
20191 | 681 |
modl |
19884 | 682 |
|> Graph.del_nodes (Graph.all_preds modl ndefs) |
20191 | 683 |
|> Graph.del_nodes ndefs |
684 |
|> Graph.del_nodes (Graph.all_preds modl (map fst nmodls)) |
|
19884 | 685 |
|> fold (fn (nmodl, names') => Graph.map_node nmodl (purge names')) nmodls |
686 |
|> Module |
|
687 |
end; |
|
18702 | 688 |
in |
19884 | 689 |
Module modl |
690 |
|> purge (map dest_name names) |
|
691 |
|> dest_modl |
|
18702 | 692 |
end; |
693 |
||
20428 | 694 |
fun flat_module modl = |
695 |
maps ( |
|
696 |
fn (name, Module modl) => map (apfst (NameSpace.append name)) (flat_module modl) |
|
697 |
| (name, Def def) => [(name, def)] |
|
698 |
) ((AList.make (Graph.get_node modl) o flat o Graph.strong_conn) modl) |
|
699 |
||
700 |
(* |
|
20466 | 701 |
(*FIXME: graph-based approach is better. |
702 |
* build graph |
|
703 |
* implement flat_classops on sort level, not class level |
|
704 |
* flat_instances bleibt wie es ist |
|
705 |
*) |
|
706 |
fun flat_classops modl = |
|
707 |
let |
|
708 |
fun add_ancestry class anc = |
|
709 |
let |
|
710 |
val SOME (Class (super_classes, (v, ops))) = AList.lookup (op =) modl class |
|
711 |
val super_classees' = filter (not o member (fn (c', (c, _)) => c = c') anc) super_classes; |
|
712 |
in |
|
713 |
[(class, ops)] @ anc |
|
714 |
|> fold add_ancestry super_classees' |
|
715 |
end; |
|
716 |
in |
|
717 |
Symtab.empty |
|
718 |
|> fold ( |
|
719 |
fn (class, Class _) => |
|
720 |
Symtab.update_new (class, maps snd (add_ancestry class [])) |
|
721 |
| _ => I |
|
722 |
) modl |
|
723 |
|> the oo Symtab.lookup |
|
724 |
end; |
|
725 |
||
726 |
fun flat_instances modl = |
|
727 |
let |
|
728 |
fun add_ancestry instance instsss anc = |
|
729 |
let |
|
730 |
val SOME (Classinst (_, (super_instances, ops))) = AList.lookup (op =) modl instance; |
|
731 |
val super_instances' = filter (not o member (eq_fst (op =)) anc) super_instances; |
|
732 |
val ops' = map (apsnd (rpair instsss)) ops; |
|
733 |
(*FIXME: build types*) |
|
734 |
in |
|
735 |
[(instance, ops')] @ anc |
|
736 |
|> fold (fn (_, (instance, instss)) => add_ancestry instance (instss :: instsss)) super_instances' |
|
737 |
end; |
|
738 |
in |
|
739 |
Symtab.empty |
|
740 |
|> fold ( |
|
741 |
fn (instance, Classinst _) => |
|
742 |
Symtab.update_new (instance, maps snd (add_ancestry instance [] [])) |
|
743 |
| _ => I |
|
744 |
) modl |
|
745 |
|> the oo Symtab.lookup |
|
746 |
end; |
|
747 |
||
748 |
fun flat_fundef classops instdefs is_classop (eqs, (vs, ty)) = |
|
749 |
let |
|
750 |
fun fold_map_snd' f (x, ys) = fold_map (f x) ys; |
|
751 |
fun fold_map_snd f (x, ys) = fold_map f ys #-> (fn zs => pair (x, zs)); |
|
752 |
val names = |
|
753 |
Name.context |
|
754 |
|> fold Name.declare |
|
755 |
(fold (fn (rhs, lhs) => fold add_varnames rhs #> add_varnames lhs) eqs []); |
|
756 |
val opmap = [] : (string * (string * (string * itype) list) list) list; |
|
757 |
val (params, tys) = (split_list o maps snd o maps snd) opmap; |
|
758 |
(*fun name_ops v' class = |
|
759 |
(fold_map o fold_map_snd') |
|
760 |
(fn (class, v) => fn (c, ty) => Name.variants [c] #-> (fn [p] => |
|
761 |
pair (class, v') (c, (ty, p)))) |
|
762 |
(classops class); |
|
763 |
val (opsmap, _) = (fold_map o fold_map_snd') name_ops vs names; |
|
764 |
(* --> (iterm * itype) list *)*) |
|
765 |
fun flat_inst (Instance (instance, instss)) = |
|
766 |
let |
|
767 |
val xs : (string * (iterm * (itype * inst list list list))) list = instdefs instance |
|
768 |
fun mk_t (t, (ty, instsss)) = |
|
769 |
(Library.foldl (fn (t, instss) => t `$$ map (fst o snd) ((maps o maps) flat_inst instss)) |
|
770 |
(t, instss :: instsss), ty) |
|
771 |
in |
|
772 |
map (apsnd mk_t) xs |
|
773 |
end |
|
774 |
| flat_inst (Context (classes, (v, k))) = |
|
775 |
let |
|
776 |
val _ : 'a = classops (hd classes); |
|
777 |
in |
|
778 |
[] |
|
779 |
end |
|
780 |
(* |
|
781 |
val parm_map = nth ((the o AList.lookup (op =) octxt) v) |
|
782 |
(if k = ~1 then 0 else k); |
|
783 |
in map (apfst IVar o swap o snd) (case classes |
|
784 |
of class::_ => (the o AList.lookup (op =) parm_map) class |
|
785 |
| _ => (snd o hd) parm_map)*) |
|
786 |
and flat_iterm (e as IConst (c, (lss, ty))) = |
|
787 |
if is_classop c then let |
|
788 |
val tab = (maps o maps) flat_inst lss; |
|
789 |
val SOME (t, _) = AList.lookup (op =) tab c; |
|
790 |
in t end else let |
|
791 |
val (es, tys) = (split_list o map snd) ((maps o maps) flat_inst lss) |
|
792 |
in IConst (c, (replicate (length lss) [], tys `--> ty)) `$$ es end |
|
793 |
| flat_iterm (e as IVar _) = |
|
794 |
e |
|
795 |
| flat_iterm (e1 `$ e2) = |
|
796 |
flat_iterm e1 `$ flat_iterm e2 |
|
797 |
| flat_iterm (v_ty `|-> e) = |
|
798 |
v_ty `|-> flat_iterm e |
|
799 |
| flat_iterm (INum (k, e)) = |
|
800 |
INum (k, flat_iterm e) |
|
801 |
| flat_iterm (IChar (s, e)) = |
|
802 |
IChar (s, flat_iterm e) |
|
803 |
| flat_iterm (ICase (((de, dty), es), e)) = |
|
804 |
ICase (((flat_iterm de, dty), map (pairself flat_iterm) es), flat_iterm e); |
|
805 |
fun flat_eq (lhs, rhs) = (map IVar params @ lhs, flat_iterm rhs); |
|
806 |
in (map flat_eq eqs, (map (apsnd (K [])) vs, tys `--> ty)) end; |
|
807 |
||
20428 | 808 |
fun flat_funs_datatypes modl = |
20466 | 809 |
let |
810 |
val modl = flat_module modl; |
|
811 |
val classops = flat_classops modl; |
|
812 |
val instdefs = flat_instances modl; |
|
813 |
val is_classop = is_some o AList.lookup (op =) modl; |
|
814 |
in map_filter ( |
|
815 |
fn def as (_, Datatype _) => SOME def |
|
816 |
| (name, Fun funn) => SOME (name, (Fun (flat_fundef classops instdefs is_classop funn))) |
|
817 |
| _ => NONE |
|
818 |
) end; |
|
20428 | 819 |
*) |
820 |
||
20456 | 821 |
val add_deps_of_typparms = |
20191 | 822 |
fold (fn (v : vname, sort : sort) => fold (insert (op =)) sort); |
823 |
||
20600 | 824 |
fun add_deps_of_classlookup (Instance (inst, lss)) = |
825 |
insert (op =) inst |
|
20191 | 826 |
#> (fold o fold) add_deps_of_classlookup lss |
20456 | 827 |
| add_deps_of_classlookup (Context (clss, _)) = |
20191 | 828 |
fold (insert (op =)) clss; |
829 |
||
830 |
fun add_deps_of_type (tyco `%% tys) = |
|
831 |
insert (op =) tyco |
|
832 |
#> fold add_deps_of_type tys |
|
833 |
| add_deps_of_type (ty1 `-> ty2) = |
|
834 |
add_deps_of_type ty1 |
|
835 |
#> add_deps_of_type ty2 |
|
836 |
| add_deps_of_type (ITyVar v) = |
|
837 |
I; |
|
838 |
||
839 |
fun add_deps_of_term (IConst (c, (lss, ty))) = |
|
840 |
insert (op =) c |
|
841 |
#> add_deps_of_type ty |
|
842 |
#> (fold o fold) add_deps_of_classlookup lss |
|
843 |
| add_deps_of_term (IVar _) = |
|
844 |
I |
|
845 |
| add_deps_of_term (e1 `$ e2) = |
|
846 |
add_deps_of_term e1 #> add_deps_of_term e2 |
|
847 |
| add_deps_of_term ((_, ty) `|-> e) = |
|
848 |
add_deps_of_type ty |
|
849 |
#> add_deps_of_term e |
|
850 |
| add_deps_of_term (INum _) = |
|
851 |
I |
|
852 |
| add_deps_of_term (IChar (_, e)) = |
|
853 |
add_deps_of_term e |
|
854 |
| add_deps_of_term (ICase (_, e)) = |
|
855 |
add_deps_of_term e; |
|
856 |
||
857 |
fun deps_of Bot = |
|
858 |
[] |
|
20456 | 859 |
| deps_of (Fun (eqs, (vs, ty))) = |
20191 | 860 |
[] |
20456 | 861 |
|> add_deps_of_typparms vs |
20191 | 862 |
|> add_deps_of_type ty |
863 |
|> fold (fn (lhs, rhs) => fold add_deps_of_term lhs #> add_deps_of_term rhs) eqs |
|
20456 | 864 |
| deps_of (Typesyn (vs, ty)) = |
20191 | 865 |
[] |
20456 | 866 |
|> add_deps_of_typparms vs |
20191 | 867 |
|> add_deps_of_type ty |
20456 | 868 |
| deps_of (Datatype (vs, cos)) = |
20191 | 869 |
[] |
20456 | 870 |
|> add_deps_of_typparms vs |
20191 | 871 |
|> fold (fn (c, tys) => insert (op =) c #> fold add_deps_of_type tys) cos |
872 |
| deps_of (Datatypecons dtco) = |
|
873 |
[dtco] |
|
874 |
| deps_of (Class (supclss, (_, memdecls))) = |
|
875 |
[] |
|
876 |
|> fold (insert (op =)) supclss |
|
20456 | 877 |
|> fold (fn (name, ty) => |
20191 | 878 |
insert (op =) name |
879 |
#> add_deps_of_type ty |
|
880 |
) memdecls |
|
881 |
| deps_of (Classmember class) = |
|
882 |
[class] |
|
20456 | 883 |
| deps_of (Classinst ((class, (tyco, vs)), (suparities, memdefs))) = |
20191 | 884 |
[] |
885 |
|> insert (op =) class |
|
886 |
|> insert (op =) tyco |
|
20456 | 887 |
|> add_deps_of_typparms vs |
20466 | 888 |
|> fold (fn (supclass, (supinst, lss)) => |
20191 | 889 |
insert (op =) supclass |
20466 | 890 |
#> insert (op =) supinst |
891 |
#> (fold o fold) add_deps_of_classlookup lss |
|
20191 | 892 |
) suparities |
20389 | 893 |
|> fold (fn (name, e) => |
20191 | 894 |
insert (op =) name |
20389 | 895 |
#> add_deps_of_term e |
896 |
) memdefs; |
|
20191 | 897 |
|
898 |
fun delete_garbage hidden modl = |
|
899 |
let |
|
900 |
fun allnames modl = |
|
901 |
let |
|
902 |
val entries = AList.make (Graph.get_node modl) (Graph.keys modl) |
|
903 |
fun is_def (name, Module _) = NONE |
|
904 |
| is_def (name, _) = SOME name; |
|
905 |
fun is_modl (name, Module modl) = SOME (name, modl) |
|
906 |
| is_modl (name, _) = NONE; |
|
907 |
val defs = map_filter is_def entries; |
|
908 |
val modls = map_filter is_modl entries; |
|
909 |
in |
|
910 |
defs |
|
911 |
@ maps (fn (name, modl) => map (NameSpace.append name) (allnames modl)) modls |
|
912 |
end; |
|
913 |
fun alldeps modl = |
|
914 |
let |
|
915 |
val entries = AList.make (Graph.get_node modl) (Graph.keys modl) |
|
916 |
fun is_def (name, Module _) = NONE |
|
917 |
| is_def (name, _) = SOME name; |
|
918 |
fun is_modl (name, Module modl) = SOME (name, modl) |
|
919 |
| is_modl (name, _) = NONE; |
|
920 |
val defs = map_filter is_def entries; |
|
921 |
val modls = map_filter is_modl entries; |
|
922 |
in |
|
923 |
maps (fn name => map (pair (name)) (Graph.imm_succs modl name)) defs |
|
924 |
@ maps (fn (name, modl) => (map o pairself) (NameSpace.append name) (alldeps modl)) modls |
|
925 |
end; |
|
926 |
val names = subtract (op =) hidden (allnames modl); |
|
20353 | 927 |
(* val _ = writeln "HIDDEN"; *) |
928 |
(* val _ = (writeln o commas) hidden; *) |
|
929 |
(* val _ = writeln "NAMES"; *) |
|
930 |
(* val _ = (writeln o commas) names; *) |
|
20191 | 931 |
fun is_bot name = |
932 |
case get_def modl name of Bot => true | _ => false; |
|
933 |
val bots = filter is_bot names; |
|
934 |
val defs = filter (not o is_bot) names; |
|
935 |
val expldeps = |
|
936 |
Graph.empty |
|
937 |
|> fold (fn name => Graph.new_node (name, ())) names |
|
938 |
|> fold (fn name => fold (curry Graph.add_edge name) |
|
939 |
(deps_of (get_def modl name) |> subtract (op =) hidden)) names |
|
940 |
val bots' = fold (insert op =) bots (Graph.all_preds expldeps bots); |
|
941 |
val selected = subtract (op =) bots' names; |
|
942 |
(* val deps = filter (fn (x, y) => member (op =) selected x andalso member (op =) selected y) *) |
|
943 |
val adddeps = maps (fn (n, ns) => map (pair n) ns) (expldeps |> Graph.del_nodes bots' |> Graph.dest); |
|
20353 | 944 |
(* val _ = writeln "SELECTED"; |
20191 | 945 |
val _ = (writeln o commas) selected; |
946 |
val _ = writeln "DEPS"; |
|
20353 | 947 |
val _ = (writeln o cat_lines o map (fn (x, y) => x ^ " -> " ^ y)) adddeps; *) |
20191 | 948 |
in |
949 |
empty_module |
|
950 |
|> fold (fn name => add_def (name, get_def modl name)) selected |
|
951 |
(* |> fold ensure_bot (hidden @ bots') *) |
|
20216 | 952 |
|> fold (fn (x, y) => ((*writeln ("adding " ^ x ^ " -> " ^ y);*) add_dep (x, y))) adddeps |
20191 | 953 |
end; |
954 |
||
19884 | 955 |
fun allimports_of modl = |
956 |
let |
|
957 |
fun imps_of prfx (Module modl) imps tab = |
|
958 |
let |
|
959 |
val this = NameSpace.pack prfx; |
|
960 |
val name_con = (rev o Graph.strong_conn) modl; |
|
961 |
in |
|
962 |
tab |
|
963 |
|> pair [] |
|
964 |
|> fold (fn names => fn (imps', tab) => |
|
965 |
tab |
|
20191 | 966 |
|> fold_map (fn name => |
19884 | 967 |
imps_of (prfx @ [name]) (Graph.get_node modl name) (imps' @ imps)) names |
968 |
|-> (fn imps'' => pair (flat imps'' @ imps'))) name_con |
|
20191 | 969 |
|-> (fn imps' => |
19884 | 970 |
Symtab.update_new (this, imps' @ imps) |
971 |
#> pair (this :: imps')) |
|
972 |
end |
|
973 |
| imps_of prfx (Def _) imps tab = |
|
974 |
([], tab); |
|
975 |
in snd (imps_of [] (Module modl) [] Symtab.empty) end; |
|
976 |
||
18702 | 977 |
fun check_samemodule names = |
978 |
fold (fn name => |
|
979 |
let |
|
980 |
val modn = (fst o dest_name) name |
|
981 |
in |
|
982 |
fn NONE => SOME modn |
|
18850 | 983 |
| SOME mod' => if modn = mod' then SOME modn |
20386 | 984 |
else error ("Inconsistent name prefix for simultanous names: " ^ commas_quote names) |
18702 | 985 |
end |
986 |
) names NONE; |
|
987 |
||
988 |
fun check_funeqs eqs = |
|
989 |
(fold (fn (pats, _) => |
|
990 |
let |
|
991 |
val l = length pats |
|
992 |
in |
|
993 |
fn NONE => SOME l |
|
18850 | 994 |
| SOME l' => if l = l' then SOME l |
20389 | 995 |
else error "Function definition with different number of arguments" |
18702 | 996 |
end |
997 |
) eqs NONE; eqs); |
|
998 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
999 |
fun check_prep_def modl Bot = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1000 |
Bot |
18702 | 1001 |
| check_prep_def modl (Fun (eqs, d)) = |
1002 |
Fun (check_funeqs eqs, d) |
|
1003 |
| check_prep_def modl (d as Typesyn _) = |
|
1004 |
d |
|
19038 | 1005 |
| check_prep_def modl (d as Datatype _) = |
1006 |
d |
|
18702 | 1007 |
| check_prep_def modl (Datatypecons dtco) = |
20389 | 1008 |
error "Attempted to add bare datatype constructor" |
19038 | 1009 |
| check_prep_def modl (d as Class _) = |
1010 |
d |
|
18702 | 1011 |
| check_prep_def modl (Classmember _) = |
20389 | 1012 |
error "Attempted to add bare class member" |
1013 |
| check_prep_def modl (d as Classinst ((class, (tyco, arity)), (_, memdefs))) = |
|
18170 | 1014 |
let |
19038 | 1015 |
val Class (_, (v, membrs)) = get_def modl class; |
18702 | 1016 |
val _ = if length memdefs > length memdefs |
20389 | 1017 |
then error "Too many member definitions given" |
18702 | 1018 |
else (); |
20389 | 1019 |
fun check_memdef (m, _) = |
1020 |
if AList.defined (op =) memdefs m |
|
1021 |
then () else error ("Missing definition for member " ^ quote m); |
|
1022 |
val _ = map check_memdef memdefs; |
|
1023 |
in d end |
|
19213 | 1024 |
| check_prep_def modl Classinstmember = |
20389 | 1025 |
error "Attempted to add bare class instance member"; |
18170 | 1026 |
|
19038 | 1027 |
fun postprocess_def (name, Datatype (_, constrs)) = |
18702 | 1028 |
(check_samemodule (name :: map fst constrs); |
1029 |
fold (fn (co, _) => |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1030 |
add_def_incr true (co, Datatypecons name) |
18702 | 1031 |
#> add_dep (co, name) |
1032 |
#> add_dep (name, co) |
|
1033 |
) constrs |
|
1034 |
) |
|
19038 | 1035 |
| postprocess_def (name, Class (_, (_, membrs))) = |
18702 | 1036 |
(check_samemodule (name :: map fst membrs); |
1037 |
fold (fn (m, _) => |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1038 |
add_def_incr true (m, Classmember name) |
18702 | 1039 |
#> add_dep (m, name) |
1040 |
#> add_dep (name, m) |
|
1041 |
) membrs |
|
1042 |
) |
|
1043 |
| postprocess_def _ = |
|
1044 |
I; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1045 |
|
19884 | 1046 |
|
1047 |
(* transaction protocol *) |
|
18170 | 1048 |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1049 |
fun ensure_def defgen strict msg name (dep, modl) = |
18170 | 1050 |
let |
20389 | 1051 |
(*FIXME represent dependencies as tuple (name, name -> string), for better error msgs*) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1052 |
val msg' = (case dep |
18702 | 1053 |
of NONE => msg |
19884 | 1054 |
| SOME dep => msg ^ ", required for " ^ quote dep) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1055 |
^ (if strict then " (strict)" else " (non-strict)"); |
18702 | 1056 |
fun add_dp NONE = I |
1057 |
| add_dp (SOME dep) = |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1058 |
debug_msg (fn _ => "adding dependency " ^ quote dep ^ " -> " ^ quote name) |
18702 | 1059 |
#> add_dep (dep, name); |
1060 |
fun prep_def def modl = |
|
1061 |
(check_prep_def modl def, modl); |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1062 |
fun invoke_generator name defgen modl = |
20191 | 1063 |
if ! soft_exc (*that "!" isn't a "not"...*) |
19956 | 1064 |
then defgen name (SOME name, modl) |
1065 |
handle FAIL (msgs, exc) => |
|
1066 |
if strict then raise FAIL (msg' :: msgs, exc) |
|
1067 |
else (Bot, modl) |
|
20191 | 1068 |
| e => raise |
19956 | 1069 |
FAIL (["definition generator for " ^ quote name, msg'], SOME e) |
1070 |
else defgen name (SOME name, modl) |
|
1071 |
handle FAIL (msgs, exc) => |
|
19884 | 1072 |
if strict then raise FAIL (msg' :: msgs, exc) |
19956 | 1073 |
else (Bot, modl); |
18170 | 1074 |
in |
1075 |
modl |
|
18702 | 1076 |
|> (if can (get_def modl) name |
1077 |
then |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1078 |
debug_msg (fn _ => "asserting node " ^ quote name) |
18702 | 1079 |
#> add_dp dep |
1080 |
else |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1081 |
debug_msg (fn _ => "allocating node " ^ quote name ^ (if strict then " (strict)" else " (non-strict)")) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1082 |
#> ensure_bot name |
18702 | 1083 |
#> add_dp dep |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1084 |
#> debug_msg (fn _ => "creating node " ^ quote name) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1085 |
#> invoke_generator name defgen |
18702 | 1086 |
#-> (fn def => prep_def def) |
1087 |
#-> (fn def => |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1088 |
debug_msg (fn _ => "addition of " ^ name ^ " := " ^ (Pretty.output o pretty_def) def) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1089 |
#> debug_msg (fn _ => "adding") |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1090 |
#> add_def_incr strict (name, def) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1091 |
#> debug_msg (fn _ => "postprocessing") |
18702 | 1092 |
#> postprocess_def (name, def) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1093 |
#> debug_msg (fn _ => "adding done") |
18702 | 1094 |
)) |
1095 |
|> pair dep |
|
18170 | 1096 |
end; |
1097 |
||
19884 | 1098 |
fun succeed some (_, modl) = (some, modl); |
1099 |
||
1100 |
fun fail msg (_, modl) = raise FAIL ([msg], NONE); |
|
1101 |
||
1102 |
fun message msg f trns = |
|
1103 |
f trns handle FAIL (msgs, exc) => |
|
1104 |
raise FAIL (msg :: msgs, exc); |
|
1105 |
||
18963 | 1106 |
fun start_transact init f modl = |
18231 | 1107 |
let |
18963 | 1108 |
fun handle_fail f x = |
1109 |
(f x |
|
18231 | 1110 |
handle FAIL (msgs, NONE) => |
20389 | 1111 |
(error o cat_lines) ("Code generation failed, while:" :: msgs)) |
18231 | 1112 |
handle FAIL (msgs, SOME e) => |
20389 | 1113 |
((Output.error_msg o cat_lines) ("Code generation failed, while:" :: msgs); raise e); |
18231 | 1114 |
in |
19884 | 1115 |
modl |
1116 |
|> (if is_some init then ensure_bot (the init) else I) |
|
1117 |
|> pair init |
|
18231 | 1118 |
|> handle_fail f |
19884 | 1119 |
|-> (fn x => fn (_, modl) => (x, modl)) |
18231 | 1120 |
end; |
18172 | 1121 |
|
20216 | 1122 |
fun add_eval_def (shallow, e) modl = |
20191 | 1123 |
let |
20216 | 1124 |
val name = "VALUE"; |
1125 |
val sname = NameSpace.pack [shallow, name]; |
|
20191 | 1126 |
in |
1127 |
modl |
|
20456 | 1128 |
|> add_def (sname, Fun ([([], e)], ([("_", [])], ITyVar "_"))) |
20216 | 1129 |
|> fold (curry add_dep sname) (add_deps_of_term e []) |
20191 | 1130 |
|> pair name |
1131 |
end; |
|
18172 | 1132 |
|
18335 | 1133 |
|
20389 | 1134 |
(** eliminating classes in definitions **) |
1135 |
||
20456 | 1136 |
fun elim_classes modl (eqs, (vs, ty)) = |
20389 | 1137 |
let |
1138 |
fun elim_expr _ = (); |
|
1139 |
in (error ""; (eqs, ty)) end; |
|
1140 |
||
18216 | 1141 |
(** generic serialization **) |
1142 |
||
1143 |
(* resolving *) |
|
1144 |
||
18885 | 1145 |
structure NameMangler = NameManglerFun ( |
1146 |
type ctxt = (string * string -> string) * (string -> string option); |
|
18516 | 1147 |
type src = string * string; |
1148 |
val ord = prod_ord string_ord string_ord; |
|
18963 | 1149 |
fun mk (postprocess, validate) ((shallow, name), 0) = |
1150 |
let |
|
1151 |
val name' = postprocess (shallow, name); |
|
1152 |
in case validate name' |
|
1153 |
of NONE => name' |
|
1154 |
| _ => mk (postprocess, validate) ((shallow, name), 1) |
|
1155 |
end |
|
1156 |
| mk (postprocess, validate) (("", name), i) = |
|
19150 | 1157 |
postprocess ("", name ^ replicate_string i "'") |
1158 |
|> perhaps validate |
|
1159 |
| mk (postprocess, validate) ((shallow, name), 1) = |
|
1160 |
postprocess (shallow, shallow ^ "_" ^ name) |
|
18885 | 1161 |
|> perhaps validate |
18963 | 1162 |
| mk (postprocess, validate) ((shallow, name), i) = |
19150 | 1163 |
postprocess (shallow, name ^ replicate_string i "'") |
18516 | 1164 |
|> perhaps validate; |
1165 |
fun is_valid _ _ = true; |
|
1166 |
fun maybe_unique _ _ = NONE; |
|
20389 | 1167 |
fun re_mangle _ dst = error ("No such definition name: " ^ quote dst); |
18516 | 1168 |
); |
1169 |
||
18963 | 1170 |
fun mk_deresolver module nsp_conn postprocess validate = |
18885 | 1171 |
let |
1172 |
datatype tabnode = N of string * tabnode Symtab.table option; |
|
1173 |
fun mk module manglers tab = |
|
1174 |
let |
|
1175 |
fun mk_name name = |
|
1176 |
case NameSpace.unpack name |
|
1177 |
of [n] => ("", n) |
|
1178 |
| [s, n] => (s, n); |
|
1179 |
fun in_conn (shallow, conn) = |
|
1180 |
member (op = : string * string -> bool) conn shallow; |
|
1181 |
fun add_name name = |
|
1182 |
let |
|
1183 |
val n as (shallow, _) = mk_name name; |
|
1184 |
in |
|
1185 |
AList.map_entry_yield in_conn shallow ( |
|
18963 | 1186 |
NameMangler.declare (postprocess, validate) n |
18885 | 1187 |
#-> (fn n' => pair (name, n')) |
18963 | 1188 |
) #> apfst the |
18885 | 1189 |
end; |
1190 |
val (renamings, manglers') = |
|
1191 |
fold_map add_name (Graph.keys module) manglers; |
|
1192 |
fun extend_tab (n, n') = |
|
1193 |
if (length o NameSpace.unpack) n = 1 |
|
1194 |
then |
|
1195 |
Symtab.update_new |
|
1196 |
(n, N (n', SOME (mk ((dest_modl o Graph.get_node module) n) manglers' Symtab.empty))) |
|
1197 |
else |
|
1198 |
Symtab.update_new (n, N (n', NONE)); |
|
1199 |
in fold extend_tab renamings tab end; |
|
1200 |
fun get_path_name [] tab = |
|
1201 |
([], SOME tab) |
|
1202 |
| get_path_name [p] tab = |
|
1203 |
let |
|
1204 |
val SOME (N (p', tab')) = Symtab.lookup tab p |
|
1205 |
in ([p'], tab') end |
|
1206 |
| get_path_name [p1, p2] tab = |
|
18919 | 1207 |
(case Symtab.lookup tab p1 |
20191 | 1208 |
of SOME (N (p', SOME tab')) => |
18885 | 1209 |
let |
1210 |
val (ps', tab'') = get_path_name [p2] tab' |
|
1211 |
in (p' :: ps', tab'') end |
|
1212 |
| NONE => |
|
1213 |
let |
|
1214 |
val SOME (N (p', NONE)) = Symtab.lookup tab (NameSpace.pack [p1, p2]) |
|
18919 | 1215 |
in ([p'], NONE) end) |
18885 | 1216 |
| get_path_name (p::ps) tab = |
1217 |
let |
|
1218 |
val SOME (N (p', SOME tab')) = Symtab.lookup tab p |
|
1219 |
val (ps', tab'') = get_path_name ps tab' |
|
1220 |
in (p' :: ps', tab'') end; |
|
1221 |
fun deresolv tab prefix name = |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1222 |
let |
20105 | 1223 |
val (common, (_, rem)) = chop_prefix (op =) (prefix, NameSpace.unpack name); |
18885 | 1224 |
val (_, SOME tab') = get_path_name common tab; |
1225 |
val (name', _) = get_path_name rem tab'; |
|
20428 | 1226 |
in NameSpace.pack name' end handle BIND => (error ("Missing name: " ^ quote name ^ ", in " ^ quote (NameSpace.pack prefix))); |
18885 | 1227 |
in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end; |
1228 |
||
18216 | 1229 |
|
1230 |
(* serialization *) |
|
1231 |
||
18963 | 1232 |
fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module = |
18216 | 1233 |
let |
19884 | 1234 |
val imptab = allimports_of module; |
18963 | 1235 |
val resolver = mk_deresolver module nsp_conn postprocess validate; |
19038 | 1236 |
fun sresolver s = (resolver o NameSpace.unpack) s |
18702 | 1237 |
fun mk_name prfx name = |
18850 | 1238 |
let |
1239 |
val name_qual = NameSpace.pack (prfx @ [name]) |
|
1240 |
in (name_qual, resolver prfx name_qual) end; |
|
20191 | 1241 |
fun is_bot (_, (Def Bot)) = true |
1242 |
| is_bot _ = false; |
|
18756 | 1243 |
fun mk_contents prfx module = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
1244 |
map_filter (seri prfx) |
18850 | 1245 |
((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19785
diff
changeset
|
1246 |
and seri prfx [(name, Module modl)] = |
19884 | 1247 |
seri_module (resolver []) (map (resolver []) ((the o Symtab.lookup imptab) (NameSpace.pack (prfx @ [name])))) |
18850 | 1248 |
(mk_name prfx name, mk_contents (prfx @ [name]) modl) |
18756 | 1249 |
| seri prfx ds = |
20191 | 1250 |
case filter_out is_bot ds |
1251 |
of [] => NONE |
|
1252 |
| ds' => seri_defs sresolver (NameSpace.pack prfx) |
|
1253 |
(map (fn (name, Def def) => (fst (mk_name prfx name), def (*|> tap (Pretty.writeln o pretty_def)*))) ds') |
|
18216 | 1254 |
in |
19937 | 1255 |
seri_module (resolver []) (map (resolver []) ((the o Symtab.lookup imptab) "")) |
18850 | 1256 |
(("", name_root), (mk_contents [] module)) |
18216 | 1257 |
end; |
1258 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1259 |
end; (* struct *) |
19136 | 1260 |
|
19300 | 1261 |
structure BasicCodegenThingol: BASIC_CODEGEN_THINGOL = CodegenThingol; |