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