author | haftmann |
Tue, 27 Dec 2005 15:24:40 +0100 | |
changeset 18515 | 1cad5c2b2a0b |
parent 18454 | 6720b5010a57 |
child 18516 | 4424e2bce9af |
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 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
8 |
signature CODEGEN_THINGOL = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
sig |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
type vname = string; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
11 |
datatype itype = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
12 |
IType of string * itype list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
13 |
| IFun of itype * itype |
18172 | 14 |
| IVarT of vname * sort |
15 |
| IDictT of (string * itype) list; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
16 |
datatype ipat = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
17 |
ICons of (string * ipat list) * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
18 |
| IVarP of vname * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
19 |
datatype iexpr = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
20 |
IConst of string * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
21 |
| IVarE of vname * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
22 |
| IApp of iexpr * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
23 |
| IInst of iexpr * ClassPackage.sortlookup list list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
24 |
| IAbs of (vname * itype) * iexpr |
18172 | 25 |
| ICase of iexpr * (ipat * iexpr) list |
26 |
| IDictE of (string * iexpr) list |
|
27 |
| ILookup of (string list * vname); |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
28 |
val mk_funs: itype list * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
29 |
val mk_apps: iexpr * iexpr list -> iexpr; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
30 |
val mk_abss: (vname * itype) list * iexpr -> iexpr; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
31 |
val pretty_itype: itype -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
32 |
val pretty_ipat: ipat -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
33 |
val pretty_iexpr: iexpr -> Pretty.T; |
18216 | 34 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list; |
35 |
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
|
36 |
val unfold_fun: itype -> itype list * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
37 |
val unfold_app: iexpr -> iexpr * iexpr list; |
18282 | 38 |
val unfold_abs: iexpr -> (vname * itype) list * iexpr; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
39 |
val unfold_let: iexpr -> (ipat * iexpr) list * iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
40 |
val itype_of_iexpr: iexpr -> itype; |
18385 | 41 |
val itype_of_ipat: ipat -> itype; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
42 |
val ipat_of_iexpr: iexpr -> ipat; |
18282 | 43 |
val eq_itype: itype * itype -> bool; |
18304 | 44 |
val tvars_of_itypes: itype list -> string list; |
45 |
val vars_of_ipats: ipat list -> string list; |
|
46 |
val vars_of_iexprs: iexpr list -> string list; |
|
18170 | 47 |
|
48 |
datatype def = |
|
49 |
Nop |
|
50 |
| Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype) |
|
18172 | 51 |
| Typesyn of (vname * string list) list * itype |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
52 |
| Datatype of (vname * string list) list * (string * itype list) list * string list |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
53 |
| Datatypecons of string |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
54 |
| Class of class list * vname * (string * (ClassPackage.sortcontext * itype)) list * string list |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
55 |
| Classmember of class |
18515 | 56 |
| Classinst of (class * (string * (vname * sort) list)) |
57 |
* ((string * (string * ClassPackage.sortlookup list list)) list |
|
58 |
* (string * (string * ClassPackage.sortlookup list list)) list); |
|
18170 | 59 |
type module; |
60 |
type transact; |
|
61 |
type 'dst transact_fin; |
|
18304 | 62 |
type ('src, 'dst) gen_exprgen = 'src -> transact -> 'dst transact_fin; |
18170 | 63 |
type gen_defgen = string -> transact -> (def * string list) transact_fin; |
64 |
val pretty_def: def -> Pretty.T; |
|
18282 | 65 |
val pretty_module: module -> Pretty.T; |
18360 | 66 |
val pretty_deps: module -> Pretty.T; |
18170 | 67 |
val empty_module: module; |
68 |
val get_def: module -> string -> def; |
|
69 |
val merge_module: module * module -> module; |
|
70 |
val partof: string list -> module -> module; |
|
71 |
val succeed: 'a -> transact -> 'a transact_fin; |
|
72 |
val fail: string -> transact -> 'a transact_fin; |
|
18304 | 73 |
val gen_invoke: (string * ('src, 'dst) gen_exprgen) list -> string |
18170 | 74 |
-> 'src -> transact -> 'dst * transact; |
75 |
val gen_ensure_def: (string * gen_defgen) list -> string |
|
76 |
-> string -> transact -> transact; |
|
18216 | 77 |
val start_transact: (transact -> 'a * transact) -> module -> 'a * module; |
78 |
||
79 |
val class_eq: string; |
|
80 |
val type_bool: string; |
|
81 |
val type_pair: string; |
|
82 |
val type_list: string; |
|
83 |
val type_integer: string; |
|
84 |
val cons_pair: string; |
|
18335 | 85 |
val fun_eq: string; |
18216 | 86 |
val fun_fst: string; |
87 |
val fun_snd: string; |
|
88 |
val Type_integer: itype; |
|
89 |
val Cons_true: iexpr; |
|
90 |
val Cons_false: iexpr; |
|
91 |
val Cons_pair: iexpr; |
|
92 |
val Cons_nil: iexpr; |
|
93 |
val Cons_cons: iexpr; |
|
94 |
val Fun_eq: iexpr; |
|
95 |
val Fun_not: iexpr; |
|
96 |
val Fun_and: iexpr; |
|
97 |
val Fun_or: iexpr; |
|
98 |
val Fun_if: iexpr; |
|
99 |
val Fun_fst: iexpr; |
|
100 |
val Fun_snd: iexpr; |
|
101 |
val Fun_0: iexpr; |
|
102 |
val Fun_1: iexpr; |
|
103 |
val Fun_add: iexpr; |
|
104 |
val Fun_mult: iexpr; |
|
105 |
val Fun_minus: iexpr; |
|
106 |
val Fun_lt: iexpr; |
|
107 |
val Fun_le: iexpr; |
|
108 |
val Fun_wfrec: iexpr; |
|
18170 | 109 |
|
18172 | 110 |
val prims: string list; |
18385 | 111 |
val invoke_eq: ('a -> transact -> itype * transact) |
112 |
-> (string * (def * (string * sort) list) -> transact -> transact) |
|
113 |
-> 'a -> transact -> bool * transact; |
|
18172 | 114 |
val extract_defs: iexpr -> string list; |
115 |
val eta_expand: (string -> int) -> module -> module; |
|
18216 | 116 |
val eta_expand_poly: module -> module; |
18172 | 117 |
val tupelize_cons: module -> module; |
118 |
val eliminate_classes: module -> module; |
|
119 |
||
18170 | 120 |
val debug_level : int ref; |
121 |
val debug : int -> ('a -> string) -> 'a -> 'a; |
|
18231 | 122 |
val soft_exc: bool ref; |
18216 | 123 |
|
124 |
val serialize: |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
125 |
((string -> string) -> (string * def) list -> Pretty.T option) |
18216 | 126 |
-> (string * Pretty.T list -> Pretty.T) |
127 |
-> (string -> string option) |
|
128 |
-> string list list -> string -> module -> Pretty.T |
|
18454 | 129 |
|
130 |
val get_prefix: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list * ('a list * 'a list) |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
131 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
132 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
133 |
signature CODEGEN_THINGOL_OP = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
134 |
sig |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
135 |
include CODEGEN_THINGOL; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
136 |
val `%% : string * itype list -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
137 |
val `-> : itype * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
138 |
val `--> : itype list * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
139 |
val `$ : iexpr * iexpr -> iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
140 |
val `$$ : iexpr * iexpr list -> iexpr; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
141 |
val `|-> : (vname * itype) * iexpr -> iexpr; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
142 |
val `|--> : (vname * itype) list * iexpr -> iexpr; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
143 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
144 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
145 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
146 |
structure CodegenThingolOp: CODEGEN_THINGOL_OP = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
147 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
148 |
|
18170 | 149 |
(** auxiliary **) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
150 |
|
18170 | 151 |
val debug_level = ref 0; |
152 |
fun debug d f x = (if d <= !debug_level then Output.debug (f x) else (); x); |
|
18231 | 153 |
val soft_exc = ref true; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
154 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
155 |
fun unfoldl dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
156 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
157 |
of NONE => (x, []) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
158 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
159 |
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
|
160 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
161 |
fun unfoldr dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
162 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
163 |
of NONE => ([], x) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
164 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
165 |
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
|
166 |
|
18172 | 167 |
fun map_yield f [] = ([], []) |
168 |
| map_yield f (x::xs) = |
|
169 |
let |
|
170 |
val (y, x') = f x |
|
171 |
val (ys, xs') = map_yield f xs |
|
172 |
in (y::ys, x'::xs') end; |
|
173 |
||
18454 | 174 |
fun get_prefix eq ([], ys) = ([], ([], ys)) |
175 |
| get_prefix eq (xs, []) = ([], (xs, [])) |
|
18170 | 176 |
| get_prefix eq (xs as x::xs', ys as y::ys') = |
177 |
if eq (x, y) then |
|
18454 | 178 |
let val (ps', xys'') = get_prefix eq (xs', ys') |
179 |
in (x::ps', xys'') end |
|
180 |
else ([], (xs, ys)); |
|
18170 | 181 |
|
182 |
||
183 |
(** language core - types, pattern, expressions **) |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
184 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
185 |
(* language representation *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
186 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
187 |
infix 8 `%%; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
188 |
infixr 6 `->; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
189 |
infixr 6 `-->; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
190 |
infix 4 `$; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
191 |
infix 4 `$$; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
192 |
infixr 5 `|->; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
193 |
infixr 5 `|-->; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
194 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
195 |
type vname = string; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
196 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
197 |
datatype itype = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
198 |
IType of string * itype list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
199 |
| IFun of itype * itype |
18172 | 200 |
| IVarT of vname * sort |
201 |
(*ML auxiliary*) |
|
202 |
| IDictT of (string * itype) list; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
203 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
204 |
datatype ipat = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
205 |
ICons of (string * ipat list) * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
206 |
| IVarP of vname * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
207 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
208 |
datatype iexpr = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
209 |
IConst of string * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
210 |
| IVarE of vname * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
211 |
| IApp of iexpr * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
212 |
| IInst of iexpr * ClassPackage.sortlookup list list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
213 |
| IAbs of (vname * itype) * iexpr |
18172 | 214 |
| ICase of iexpr * (ipat * iexpr) list |
215 |
(*ML auxiliary*) |
|
216 |
| IDictE of (string * iexpr) list |
|
217 |
| ILookup of (string list * vname); |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
218 |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
219 |
(* |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
220 |
variable naming conventions |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
221 |
|
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
222 |
bare names: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
223 |
variable names v |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
224 |
class names cls |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
225 |
type constructor names tyco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
226 |
datatype names dtco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
227 |
const names (general) c |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
228 |
constructor names co |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
229 |
class member names m |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
230 |
arbitrary name s |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
231 |
|
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
232 |
constructs: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
233 |
sort sort |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
234 |
type ty |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
235 |
expression e |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
236 |
pattern p |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
237 |
instance (cls, tyco) inst |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
238 |
variable (v, ty) var |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
239 |
class member (m, ty) membr |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
240 |
constructors (co, tys) constr |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
241 |
*) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
242 |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
243 |
val mk_funs = Library.foldr IFun; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
244 |
val mk_apps = Library.foldl IApp; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
245 |
val mk_abss = Library.foldr IAbs; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
246 |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
247 |
val op `%% = IType; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
248 |
val op `-> = IFun; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
249 |
val op `$ = IApp; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
250 |
val op `|-> = IAbs; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
251 |
val op `--> = mk_funs; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
252 |
val op `$$ = mk_apps; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
253 |
val op `|--> = mk_abss; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
254 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
255 |
val unfold_fun = unfoldr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
256 |
(fn IFun t => SOME t |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
257 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
258 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
259 |
val unfold_app = unfoldl |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
260 |
(fn IApp e => SOME e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
261 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
262 |
|
18282 | 263 |
val unfold_abs = unfoldr |
264 |
(fn IAbs b => SOME b |
|
265 |
| _ => NONE) |
|
266 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
267 |
val unfold_let = unfoldr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
268 |
(fn ICase (e, [(p, e')]) => SOME ((p, e), e') |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
269 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
270 |
|
18172 | 271 |
fun map_itype f_itype (IType (tyco, tys)) = |
272 |
tyco `%% map f_itype tys |
|
273 |
| map_itype f_itype (IFun (t1, t2)) = |
|
274 |
f_itype t1 `-> f_itype t2 |
|
275 |
| map_itype _ (ty as IVarT _) = |
|
276 |
ty; |
|
277 |
||
278 |
fun map_ipat f_itype f_ipat (ICons ((c, ps), ty)) = |
|
279 |
ICons ((c, map f_ipat ps), f_itype ty) |
|
280 |
| map_ipat _ _ (p as IVarP _) = |
|
281 |
p; |
|
282 |
||
283 |
fun map_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) = |
|
284 |
f_iexpr e1 `$ f_iexpr e2 |
|
285 |
| map_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) = |
|
286 |
IInst (f_iexpr e, c) |
|
287 |
| map_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) = |
|
288 |
IAbs (v, f_iexpr e) |
|
289 |
| map_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) = |
|
290 |
ICase (f_iexpr e, map (fn (p, e) => (f_ipat p, f_iexpr e)) ps) |
|
291 |
| map_iexpr _ _ _ (e as IConst _) = |
|
292 |
e |
|
293 |
| map_iexpr _ _ _ (e as IVarE _) = |
|
18282 | 294 |
e |
295 |
| map_iexpr f_itype f_ipat f_iexpr (IDictE ms) = |
|
296 |
IDictE (map (apsnd f_iexpr) ms) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
297 |
| map_iexpr _ _ _ (e as ILookup _) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
298 |
e ; |
18172 | 299 |
|
300 |
fun fold_itype f_itype (IFun (t1, t2)) = |
|
301 |
f_itype t1 #> f_itype t2 |
|
302 |
| fold_itype _ (ty as IType _) = |
|
303 |
I |
|
304 |
| fold_itype _ (ty as IVarT _) = |
|
305 |
I; |
|
306 |
||
307 |
fun fold_ipat f_itype f_ipat (ICons ((_, ps), ty)) = |
|
308 |
f_itype ty #> fold f_ipat ps |
|
309 |
| fold_ipat f_itype f_ipat (p as IVarP _) = |
|
310 |
I; |
|
311 |
||
312 |
fun fold_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) = |
|
313 |
f_iexpr e1 #> f_iexpr e2 |
|
314 |
| fold_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) = |
|
315 |
f_iexpr e |
|
316 |
| fold_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) = |
|
317 |
f_iexpr e |
|
318 |
| fold_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) = |
|
18282 | 319 |
f_iexpr e #> fold (fn (p, e) => f_ipat p #> f_iexpr e) ps |
18172 | 320 |
| fold_iexpr _ _ _ (e as IConst _) = |
321 |
I |
|
322 |
| fold_iexpr _ _ _ (e as IVarE _) = |
|
323 |
I; |
|
324 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
325 |
|
18282 | 326 |
(* simple type matching *) |
327 |
||
328 |
fun eq_itype (ty1, ty2) = |
|
329 |
let |
|
330 |
exception NO_MATCH; |
|
331 |
fun eq (IVarT (v1, sort1)) (IVarT (v2, sort2)) subs = |
|
332 |
if sort1 <> sort2 |
|
333 |
then raise NO_MATCH |
|
334 |
else |
|
335 |
(case AList.lookup (op =) subs v1 |
|
336 |
of NONE => subs |> AList.update (op =) (v1, v2) |
|
337 |
| (SOME v1') => |
|
338 |
if v1' <> v2 |
|
339 |
then raise NO_MATCH |
|
340 |
else subs) |
|
341 |
| eq (IType (tyco1, tys1)) (IType (tyco2, tys2)) subs = |
|
342 |
if tyco1 <> tyco2 |
|
343 |
then raise NO_MATCH |
|
344 |
else subs |> fold2 eq tys1 tys2 |
|
345 |
| eq (IFun (ty11, ty12)) (IFun (ty21, ty22)) subs = |
|
346 |
subs |> eq ty11 ty21 |> eq ty12 ty22 |
|
347 |
| eq _ _ _ = raise NO_MATCH; |
|
348 |
in |
|
349 |
(eq ty1 ty2 []; true) |
|
350 |
handle NO_MATCH => false |
|
351 |
end; |
|
352 |
||
353 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
354 |
(* simple diagnosis *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
355 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
356 |
fun pretty_itype (IType (tyco, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
357 |
Pretty.gen_list "" "(" ")" (Pretty.str tyco :: map pretty_itype tys) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
358 |
| pretty_itype (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
359 |
Pretty.gen_list "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
360 |
| pretty_itype (IVarT (v, sort)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
361 |
Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort)) |
18282 | 362 |
| pretty_itype (IDictT _) = |
363 |
Pretty.str "<DictT>"; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
364 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
365 |
fun pretty_ipat (ICons ((cons, ps), ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
366 |
Pretty.gen_list " " "(" ")" |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
367 |
(Pretty.str cons :: map pretty_ipat ps @ [Pretty.str ":: ", pretty_itype ty]) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
368 |
| pretty_ipat (IVarP (v, ty)) = |
18282 | 369 |
Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
370 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
371 |
fun pretty_iexpr (IConst (f, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
372 |
Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
373 |
| pretty_iexpr (IVarE (v, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
374 |
Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
375 |
| pretty_iexpr (IApp (e1, e2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
376 |
Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, pretty_iexpr e2] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
377 |
| pretty_iexpr (IInst (e, c)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
378 |
pretty_iexpr e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
379 |
| pretty_iexpr (IAbs ((v, ty), e)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
380 |
Pretty.enclose "(" ")" [Pretty.str ("?" ^ v ^ " |->"), Pretty.brk 1, pretty_iexpr e] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
381 |
| pretty_iexpr (ICase (e, cs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
382 |
Pretty.enclose "(" ")" [ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
383 |
Pretty.str "case ", |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
384 |
pretty_iexpr e, |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
385 |
Pretty.enclose "(" ")" (map (fn (p, e) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
386 |
Pretty.block [ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
387 |
pretty_ipat p, |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
388 |
Pretty.str " => ", |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
389 |
pretty_iexpr e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
390 |
] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
391 |
) cs) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
392 |
] |
18282 | 393 |
| pretty_iexpr (IDictE _) = |
394 |
Pretty.str "<DictE>" |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
395 |
| pretty_iexpr (ILookup (ls, v)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
396 |
Pretty.str ("<Lookup: " ^ commas ls ^ " in " ^ v ^ ">"); |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
397 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
398 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
399 |
(* language auxiliary *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
400 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
401 |
fun itype_of_iexpr (IConst (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
402 |
| itype_of_iexpr (IVarE (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
403 |
| itype_of_iexpr (e as IApp (e1, e2)) = (case itype_of_iexpr e1 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
404 |
of (IFun (ty2, ty')) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
405 |
if ty2 = itype_of_iexpr e2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
406 |
then ty' |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
407 |
else error ("inconsistent application: in " ^ Pretty.output (pretty_iexpr e) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
408 |
^ ", " ^ (Pretty.output o pretty_itype) ty2 ^ " vs. " ^ (Pretty.output o pretty_itype o itype_of_iexpr) e2) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
409 |
| _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1))) |
18282 | 410 |
| itype_of_iexpr (IInst (e, cs)) = itype_of_iexpr e |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
411 |
| itype_of_iexpr (IAbs ((_, ty1), e2)) = ty1 `-> itype_of_iexpr e2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
412 |
| itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
413 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
414 |
fun itype_of_ipat (ICons (_, ty)) = ty |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
415 |
| itype_of_ipat (IVarP (_, ty)) = ty; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
416 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
417 |
fun ipat_of_iexpr (IConst (f, ty)) = ICons ((f, []), ty) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
418 |
| ipat_of_iexpr (IVarE v) = IVarP v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
419 |
| ipat_of_iexpr (e as IApp _) = |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
420 |
(case unfold_app e |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
421 |
of (IConst (f, ty), es) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
422 |
ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
423 |
| (IInst (IConst (f, ty), _), es) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
424 |
ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
425 |
| _ => error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e)) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
426 |
| ipat_of_iexpr e = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
427 |
error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e); |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
428 |
|
18304 | 429 |
fun tvars_of_itypes tys = |
18216 | 430 |
let |
18304 | 431 |
fun vars (IType (_, tys)) = |
432 |
fold vars tys |
|
433 |
| vars (IFun (ty1, ty2)) = |
|
434 |
vars ty1 #> vars ty2 |
|
435 |
| vars (IVarT (v, _)) = |
|
436 |
insert (op =) v |
|
437 |
in fold vars tys [] end; |
|
18216 | 438 |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
439 |
fun vars_of_ipats ps = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
440 |
let |
18304 | 441 |
fun vars (ICons ((_, ps), _)) = |
442 |
fold vars ps |
|
443 |
| vars (IVarP (v, _)) = |
|
444 |
insert (op =) v |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
445 |
in fold vars ps [] end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
446 |
|
18304 | 447 |
fun vars_of_iexprs es = |
448 |
let |
|
449 |
fun vars (IConst (f, _)) = |
|
450 |
I |
|
451 |
| vars (IVarE (v, _)) = |
|
452 |
insert (op =) v |
|
453 |
| vars (IApp (e1, e2)) = |
|
454 |
vars e1 #> vars e2 |
|
455 |
| vars (IAbs ((v, _), e)) = |
|
456 |
insert (op =) v |
|
457 |
#> vars e |
|
458 |
| vars (ICase (e, cs)) = |
|
459 |
vars e |
|
460 |
#> fold (fn (p, e) => fold (insert (op =)) (vars_of_ipats [p]) #> vars e) cs |
|
461 |
| vars (IInst (e, lookup)) = |
|
462 |
vars e |
|
463 |
| vars (IDictE ms) = |
|
464 |
fold (vars o snd) ms |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
465 |
| vars (ILookup (_, v)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
466 |
cons v |
18304 | 467 |
in fold vars es [] end; |
468 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
469 |
fun instant_itype (v, sty) ty = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
470 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
471 |
fun instant (IType (tyco, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
472 |
tyco `%% map instant tys |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
473 |
| instant (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
474 |
instant ty1 `-> instant ty2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
475 |
| instant (w as (IVarT (u, _))) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
476 |
if v = u then sty else w |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
477 |
in instant ty end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
478 |
|
18170 | 479 |
|
18282 | 480 |
|
18170 | 481 |
(** language module system - definitions, modules, transactions **) |
482 |
||
483 |
(* type definitions *) |
|
484 |
||
485 |
datatype def = |
|
486 |
Nop |
|
487 |
| Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype) |
|
18172 | 488 |
| Typesyn of (vname * string list) list * itype |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
489 |
| Datatype of (vname * string list) list * (string * itype list) list * string list |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
490 |
| Datatypecons of string |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
491 |
| Class of class list * vname * (string * (ClassPackage.sortcontext * itype)) list * string list |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
492 |
| Classmember of class |
18515 | 493 |
| Classinst of (class * (string * (vname * sort) list)) |
494 |
* ((string * (string * ClassPackage.sortlookup list list)) list |
|
495 |
* (string * (string * ClassPackage.sortlookup list list)) list); |
|
18170 | 496 |
|
497 |
datatype node = Def of def | Module of node Graph.T; |
|
498 |
type module = node Graph.T; |
|
499 |
type transact = Graph.key list * module; |
|
18231 | 500 |
datatype 'dst transact_res = Succeed of 'dst | Fail of string list * exn option; |
18170 | 501 |
type 'dst transact_fin = 'dst transact_res * transact; |
18304 | 502 |
type ('src, 'dst) gen_exprgen = 'src -> transact -> 'dst transact_fin; |
18170 | 503 |
type gen_defgen = string -> transact -> (def * string list) transact_fin; |
18231 | 504 |
exception FAIL of string list * exn option; |
18170 | 505 |
|
506 |
val eq_def = (op =); |
|
507 |
||
508 |
(* simple diagnosis *) |
|
509 |
||
510 |
fun pretty_def Nop = |
|
511 |
Pretty.str "<NOP>" |
|
512 |
| pretty_def (Fun (eqs, (_, ty))) = |
|
513 |
Pretty.gen_list " |" "" "" ( |
|
514 |
map (fn (ps, body) => |
|
515 |
Pretty.block [ |
|
516 |
Pretty.gen_list "," "[" "]" (map pretty_ipat ps), |
|
517 |
Pretty.str " |->", |
|
518 |
Pretty.brk 1, |
|
519 |
pretty_iexpr body, |
|
520 |
Pretty.str "::", |
|
521 |
pretty_itype ty |
|
522 |
]) eqs |
|
523 |
) |
|
18172 | 524 |
| pretty_def (Typesyn (vs, ty)) = |
18170 | 525 |
Pretty.block [ |
526 |
Pretty.list "(" ")" (map (pretty_itype o IVarT) vs), |
|
527 |
Pretty.str " |=> ", |
|
528 |
pretty_itype ty |
|
529 |
] |
|
18335 | 530 |
| pretty_def (Datatype (vs, cs, insts)) = |
18170 | 531 |
Pretty.block [ |
532 |
Pretty.list "(" ")" (map (pretty_itype o IVarT) vs), |
|
533 |
Pretty.str " |=> ", |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
534 |
Pretty.gen_list " |" "" "" |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
535 |
(map (fn (c, tys) => (Pretty.block o Pretty.breaks) (Pretty.str c :: map pretty_itype tys)) cs), |
18335 | 536 |
Pretty.str ", instances ", |
537 |
Pretty.gen_list "," "[" "]" (map Pretty.str insts) |
|
18170 | 538 |
] |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
539 |
| pretty_def (Datatypecons dtname) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
540 |
Pretty.str ("cons " ^ dtname) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
541 |
| pretty_def (Class (supcls, v, mems, insts)) = |
18170 | 542 |
Pretty.block [ |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
543 |
Pretty.str ("class var " ^ v ^ "extending "), |
18231 | 544 |
Pretty.gen_list "," "[" "]" (map Pretty.str supcls), |
18282 | 545 |
Pretty.str " with ", |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
546 |
Pretty.gen_list "," "[" "]" |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
547 |
(map (fn (m, (_, ty)) => Pretty.block [Pretty.str (m ^ "::"), pretty_itype ty]) mems), |
18282 | 548 |
Pretty.str " instances ", |
18231 | 549 |
Pretty.gen_list "," "[" "]" (map Pretty.str insts) |
550 |
] |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
551 |
| pretty_def (Classmember clsname) = |
18231 | 552 |
Pretty.block [ |
553 |
Pretty.str "class member belonging to ", |
|
18282 | 554 |
Pretty.str clsname |
18231 | 555 |
] |
18515 | 556 |
| pretty_def (Classinst ((clsname, (tyco, arity)), _)) = |
18231 | 557 |
Pretty.block [ |
558 |
Pretty.str "class instance (", |
|
18282 | 559 |
Pretty.str clsname, |
18231 | 560 |
Pretty.str ", (", |
561 |
Pretty.str tyco, |
|
562 |
Pretty.str ", ", |
|
18282 | 563 |
Pretty.gen_list "," "[" "]" (map (Pretty.gen_list "," "{" "}" o map Pretty.str o snd) arity), |
18515 | 564 |
Pretty.str "))" |
18231 | 565 |
]; |
18170 | 566 |
|
567 |
fun pretty_module modl = |
|
568 |
let |
|
569 |
fun pretty (name, Module modl) = |
|
570 |
Pretty.block ( |
|
571 |
Pretty.str ("module " ^ name ^ " {") |
|
572 |
:: Pretty.brk 1 |
|
573 |
:: Pretty.chunks (map pretty (AList.make (Graph.get_node modl) |
|
574 |
(Graph.strong_conn modl |> List.concat |> rev))) |
|
575 |
:: Pretty.str "}" :: nil |
|
576 |
) |
|
577 |
| pretty (name, Def def) = |
|
578 |
Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def] |
|
579 |
in pretty ("//", Module modl) end; |
|
580 |
||
18360 | 581 |
fun pretty_deps modl = |
582 |
let |
|
583 |
fun one_node key = |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
584 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
585 |
val preds_ = Graph.imm_preds modl key; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
586 |
val succs_ = Graph.imm_succs modl key; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
587 |
val mutbs = gen_inter (op =) (preds_, succs_); |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
588 |
val preds = fold (remove (op =)) mutbs preds_; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
589 |
val succs = fold (remove (op =)) mutbs succs_; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
590 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
591 |
(Pretty.block o Pretty.fbreaks) ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
592 |
Pretty.str key |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
593 |
:: map (fn s => Pretty.str ("<-> " ^ s)) mutbs |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
594 |
@ map (fn s => Pretty.str ("<-- " ^ s)) preds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
595 |
@ map (fn s => Pretty.str ("--> " ^ s)) succs |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
596 |
@ (the_list oo Option.mapPartial) ((fn Module modl' => SOME (pretty_deps modl') | _ => NONE) o Graph.get_node modl) (SOME key) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
597 |
) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
598 |
end |
18360 | 599 |
in |
600 |
modl |
|
601 |
|> Graph.strong_conn |
|
602 |
|> List.concat |
|
603 |
|> rev |
|
604 |
|> map one_node |
|
605 |
|> Pretty.chunks |
|
606 |
end; |
|
607 |
||
18170 | 608 |
|
609 |
(* name handling *) |
|
610 |
||
611 |
fun dest_name name = |
|
612 |
let |
|
613 |
val name' = NameSpace.unpack name |
|
614 |
val (name'', name_base) = split_last name' |
|
615 |
val (modl, shallow) = split_last name'' |
|
616 |
in (modl, NameSpace.pack [shallow, name_base]) end |
|
617 |
handle Empty => error ("not a qualified name: " ^ quote name); |
|
618 |
||
619 |
fun dest_modl (Module m) = m; |
|
620 |
fun dest_def (Def d) = d; |
|
621 |
||
622 |
||
623 |
(* modules *) |
|
624 |
||
625 |
val empty_module = Graph.empty; (*read: "depends on"*) |
|
626 |
||
627 |
fun get_def modl name = |
|
628 |
case dest_name name |
|
629 |
of (modlname, base) => |
|
630 |
let |
|
631 |
fun get (Module node) [] = |
|
632 |
(dest_def o Graph.get_node node) base |
|
633 |
| get (Module node) (m::ms) = |
|
634 |
get (Graph.get_node node m) ms |
|
635 |
in get (Module modl) modlname end; |
|
636 |
||
637 |
fun add_def (name, def) = |
|
638 |
let |
|
639 |
val (modl, base) = dest_name name; |
|
640 |
fun add [] = |
|
641 |
Graph.new_node (base, Def def) |
|
642 |
| add (m::ms) = |
|
643 |
Graph.default_node (m, Module empty_module) |
|
644 |
#> Graph.map_node m (Module o add ms o dest_modl) |
|
645 |
in add modl end; |
|
646 |
||
647 |
fun map_def name f = |
|
648 |
let |
|
649 |
val (modl, base) = dest_name name; |
|
650 |
fun mapp [] = |
|
651 |
Graph.map_node base (Def o f o dest_def) |
|
652 |
| mapp (m::ms) = |
|
653 |
Graph.map_node m (Module o mapp ms o dest_modl) |
|
654 |
in mapp modl end; |
|
655 |
||
656 |
fun add_dep (name1, name2) modl = |
|
657 |
if name1 = name2 then modl |
|
658 |
else |
|
659 |
let |
|
660 |
val m1 = dest_name name1 |> apsnd single |> (op @); |
|
661 |
val m2 = dest_name name2 |> apsnd single |> (op @); |
|
18454 | 662 |
val (ms, (r1, r2)) = get_prefix (op =) (m1, m2); |
663 |
val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2); |
|
18170 | 664 |
val add_edge = |
665 |
if null r1 andalso null r2 |
|
666 |
then Graph.add_edge |
|
667 |
else Graph.add_edge_acyclic |
|
668 |
fun add [] node = |
|
669 |
node |
|
670 |
|> add_edge (s1, s2) |
|
671 |
| add (m::ms) node = |
|
672 |
node |
|
673 |
|> Graph.map_node m (Module o add ms o dest_modl); |
|
674 |
in add ms modl end; |
|
675 |
||
676 |
fun map_defs f = |
|
677 |
let |
|
678 |
fun mapp (Def def) = |
|
679 |
(Def o f) def |
|
680 |
| mapp (Module modl) = |
|
681 |
(Module o Graph.map_nodes mapp) modl |
|
682 |
in dest_modl o mapp o Module end; |
|
683 |
||
684 |
fun fold_defs f = |
|
685 |
let |
|
686 |
fun fol prfix (name, Def def) = |
|
687 |
f (NameSpace.pack (prfix @ [name]), def) |
|
688 |
| fol prfix (name, Module modl) = |
|
689 |
Graph.fold_nodes (fol (prfix @ [name])) modl |
|
690 |
in Graph.fold_nodes (fol []) end; |
|
691 |
||
692 |
fun add_deps f modl = |
|
693 |
modl |
|
694 |
|> fold add_dep ([] |> fold_defs (append o f) modl); |
|
695 |
||
696 |
fun fold_map_defs f = |
|
697 |
let |
|
698 |
fun foldmap prfix (name, Def def) = |
|
699 |
apfst Def o f (NameSpace.pack (prfix @ [name]), def) |
|
700 |
| foldmap prfix (name, Module modl) = |
|
701 |
apfst Module o Graph.fold_map_nodes (foldmap (prfix @ [name])) modl |
|
702 |
in Graph.fold_map_nodes (foldmap []) end; |
|
703 |
||
18172 | 704 |
fun map_def_fun f_ipat f_iexpr (Fun (eqs, cty)) = |
705 |
Fun (map (fn (ps, rhs) => (map f_ipat ps, f_iexpr rhs)) eqs, cty) |
|
706 |
| map_def_fun _ _ def = def; |
|
707 |
||
708 |
fun transform_defs f_def f_ipat f_iexpr s modl = |
|
709 |
let |
|
710 |
val (modl', s') = fold_map_defs f_def modl s |
|
711 |
in |
|
712 |
modl' |
|
713 |
|> map_defs (map_def_fun (f_ipat s') (f_iexpr s')) |
|
714 |
end; |
|
715 |
||
18170 | 716 |
fun merge_module modl12 = |
717 |
let |
|
718 |
fun join_module (Module m1, Module m2) = |
|
719 |
(SOME o Module) (merge_module (m1, m2)) |
|
720 |
| join_module (Def d1, Def d2) = |
|
721 |
if eq_def (d1, d2) then (SOME o Def) d1 else NONE |
|
722 |
| join_module _ = |
|
723 |
NONE |
|
724 |
in Graph.join (K join_module) modl12 end; |
|
725 |
||
726 |
fun partof names modl = |
|
18335 | 727 |
let |
728 |
datatype pathnode = PN of (string list * (string * pathnode) list); |
|
729 |
fun mk_ipath ([], base) (PN (defs, modls)) = |
|
730 |
PN (base :: defs, modls) |
|
731 |
| mk_ipath (n::ns, base) (PN (defs, modls)) = |
|
732 |
modls |
|
733 |
|> AList.default (op =) (n, PN ([], [])) |
|
734 |
|> AList.map_entry (op =) n (mk_ipath (ns, base)) |
|
735 |
|> (pair defs #> PN); |
|
736 |
fun select (PN (defs, modls)) (Module module) = |
|
737 |
module |
|
738 |
|> Graph.subgraph (Graph.all_succs module (defs @ map fst modls)) |
|
739 |
|> fold (fn (name, modls) => Graph.map_node name (select modls)) modls |
|
740 |
|> Module; |
|
741 |
in |
|
742 |
Module modl |
|
743 |
|> select (fold (mk_ipath o dest_name) (filter NameSpace.is_qualified names) (PN ([], []))) |
|
744 |
|> dest_modl |
|
745 |
end; |
|
18170 | 746 |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
747 |
fun (*add_check_transform (name, (Datatypecons dtname)) = |
18231 | 748 |
(debug 7 (fn _ => "transformation for datatype constructor " ^ quote name |
749 |
^ " of datatype " ^ quote dtname) (); |
|
18170 | 750 |
([([dtname], |
18282 | 751 |
fn [Datatype (_, _, [])] => NONE |
752 |
| _ => "attempted to add constructor to already instantiating datatype" |> SOME)], |
|
18170 | 753 |
[(dtname, |
18335 | 754 |
fn Datatype (vs, cs, insts) => Datatype (vs, name::cs, insts) |
18170 | 755 |
| def => "attempted to add datatype constructor to non-datatype: " |
756 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
18231 | 757 |
) |
18170 | 758 |
| add_check_transform (name, Classmember (clsname, v, ty)) = |
759 |
let |
|
18231 | 760 |
val _ = debug 7 (fn _ => "transformation for class member " ^ quote name |
761 |
^ " of class " ^ quote clsname) (); |
|
18170 | 762 |
fun check_var (IType (tyco, tys)) s = |
763 |
fold check_var tys s |
|
764 |
| check_var (IFun (ty1, ty2)) s = |
|
765 |
s |
|
766 |
|> check_var ty1 |
|
767 |
|> check_var ty2 |
|
768 |
| check_var (IVarT (w, sort)) s = |
|
769 |
if v = w |
|
770 |
andalso member (op =) sort clsname |
|
771 |
then "additional class appears at type variable" |> SOME |
|
772 |
else NONE |
|
773 |
in |
|
774 |
([([], fn [] => check_var ty NONE), |
|
775 |
([clsname], |
|
18304 | 776 |
fn [Class (_, _, _, [])] => NONE |
18170 | 777 |
| _ => "attempted to add class member to witnessed class" |> SOME)], |
778 |
[(clsname, |
|
18304 | 779 |
fn Class (supcs, v, mems, insts) => Class (supcs, v, name::mems, insts) |
18170 | 780 |
| def => "attempted to add class member to non-class" |
781 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
782 |
end |
|
18515 | 783 |
| *) add_check_transform (name, Classinst ((clsname, (tyco, arity)), (_, memdefs))) = |
18170 | 784 |
let |
18231 | 785 |
val _ = debug 7 (fn _ => "transformation for class instance " ^ quote tyco |
18515 | 786 |
^ " of class " ^ quote clsname) (); |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
787 |
(* fun check [Classmember (_, v, mtyp_c), Fun (_, (_, mtyp_i))] = |
18231 | 788 |
let |
18282 | 789 |
val mtyp_i' = instant_itype (v, tyco `%% map IVarT arity) mtyp_c; |
790 |
in if eq_itype (mtyp_i', mtyp_i) |
|
791 |
then NONE |
|
792 |
else "wrong type signature for class member: " |
|
793 |
^ (Pretty.output o pretty_itype) mtyp_i' ^ " expected, " |
|
794 |
^ (Pretty.output o pretty_itype) mtyp_i ^ " given" |> SOME |
|
795 |
end |
|
18231 | 796 |
| check defs = |
797 |
"non-well-formed definitions encountered for classmembers: " |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
798 |
^ (commas o map (quote o Pretty.output o pretty_def)) defs |> SOME *) |
18170 | 799 |
in |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
800 |
((* map (fn (memname, memprim) => ([memname, memprim], check)) memdefs*) [], |
18170 | 801 |
[(clsname, |
18304 | 802 |
fn Class (supcs, v, mems, insts) => Class (supcs, v, mems, name::insts) |
18170 | 803 |
| def => "attempted to add class instance to non-class" |
804 |
^ (Pretty.output o pretty_def) def |> error), |
|
805 |
(tyco, |
|
18335 | 806 |
fn Datatype (vs, cs, insts) => Datatype (vs, cs, name::insts) |
18170 | 807 |
| Nop => Nop |
808 |
| def => "attempted to instantiate non-type to class instance" |
|
809 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
810 |
end |
|
811 |
| add_check_transform _ = ([], []); |
|
812 |
||
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
813 |
(* checks to be implemented here lateron: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
814 |
- well-formedness of function equations |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
815 |
- only possible to add defined constructors and class members |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
816 |
- right type abstraction with class members |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
817 |
- correct typing of instance definitions |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
818 |
*) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
819 |
|
18231 | 820 |
fun succeed some = pair (Succeed some); |
821 |
fun fail msg = pair (Fail ([msg], NONE)); |
|
18170 | 822 |
|
18231 | 823 |
fun check_fail _ (Succeed dst, trns) = (dst, trns) |
824 |
| check_fail msg (Fail (msgs, e), _) = raise FAIL (msg::msgs, e); |
|
18170 | 825 |
|
18231 | 826 |
fun select_generator _ _ [] modl = |
827 |
([], modl) |> fail ("no code generator available") |
|
828 |
| select_generator mk_msg src gens modl = |
|
829 |
let |
|
830 |
fun handle_fail msgs f = |
|
831 |
let |
|
832 |
in |
|
833 |
if ! soft_exc |
|
834 |
then |
|
18282 | 835 |
([], modl) |> f |
836 |
handle FAIL exc => (Fail exc, ([], modl)) |
|
837 |
| e => (Fail (msgs, SOME e), ([], modl)) |
|
18231 | 838 |
else |
18282 | 839 |
([], modl) |> f |
840 |
handle FAIL exc => (Fail exc, ([], modl)) |
|
18231 | 841 |
end; |
842 |
fun select msgs [(gname, gen)] = |
|
843 |
handle_fail (msgs @ [mk_msg gname]) (gen src) |
|
844 |
fun select msgs ((gname, gen)::gens) = |
|
845 |
let |
|
846 |
val msgs' = msgs @ [mk_msg gname] |
|
847 |
in case handle_fail msgs' (gen src) |
|
848 |
of (Fail (_, NONE), _) => |
|
849 |
select msgs' gens |
|
850 |
| result => |
|
851 |
result |
|
852 |
end; |
|
853 |
in select [] gens end; |
|
18170 | 854 |
|
855 |
fun gen_invoke codegens msg src (deps, modl) = |
|
18231 | 856 |
modl |
857 |
|> select_generator (fn gname => "trying code generator " ^ gname ^ " for source " ^ quote msg) |
|
18170 | 858 |
src codegens |
859 |
|> check_fail msg |
|
860 |
||> (fn (deps', modl') => (append deps' deps, modl')); |
|
861 |
||
862 |
fun gen_ensure_def defgens msg name (deps, modl) = |
|
863 |
let |
|
864 |
fun add (name, def) (deps, modl) = |
|
865 |
let |
|
866 |
val (checks, trans) = add_check_transform (name, def); |
|
867 |
fun check (check_defs, checker) modl = |
|
18231 | 868 |
let |
869 |
fun get_def' s = |
|
870 |
if NameSpace.is_qualified s |
|
871 |
then get_def modl s |
|
872 |
else Nop |
|
873 |
val defs = |
|
874 |
check_defs |
|
875 |
|> map get_def'; |
|
876 |
in |
|
18282 | 877 |
case checker defs |
18231 | 878 |
of NONE => modl |
18282 | 879 |
| SOME msg => raise FAIL ([msg], NONE) |
18231 | 880 |
end; |
18170 | 881 |
fun transform (name, f) modl = |
882 |
modl |
|
18231 | 883 |
|> debug 9 (fn _ => "transforming node " ^ name) |
884 |
|> (if NameSpace.is_qualified name then map_def name f else I); |
|
18170 | 885 |
in |
886 |
modl |
|
18231 | 887 |
|> debug 10 (fn _ => "considering addition of " ^ name |
888 |
^ " := " ^ (Pretty.output o pretty_def) def) |
|
889 |
|> debug 10 (fn _ => "consistency checks") |
|
18170 | 890 |
|> fold check checks |
18231 | 891 |
|> debug 10 (fn _ => "dependencies") |
18170 | 892 |
|> fold (curry add_dep name) deps |
18231 | 893 |
|> debug 10 (fn _ => "adding") |
18170 | 894 |
|> map_def name (fn _ => def) |
18231 | 895 |
|> debug 10 (fn _ => "transforming") |
18170 | 896 |
|> fold transform trans |
18231 | 897 |
|> debug 10 (fn _ => "adding done") |
18170 | 898 |
end; |
899 |
fun ensure_node name modl = |
|
18231 | 900 |
(debug 9 (fn _ => "testing node " ^ quote name) (); |
18170 | 901 |
if can (get_def modl) name |
18231 | 902 |
then |
903 |
modl |
|
904 |
|> debug 9 (fn _ => "asserting node " ^ quote name) |
|
905 |
|> pair [name] |
|
18170 | 906 |
else |
18231 | 907 |
modl |
908 |
|> debug 9 (fn _ => "creating node " ^ quote name) |
|
909 |
|> add_def (name, Nop) |
|
18454 | 910 |
|> debug 9 (fn _ => "checking creation of node " ^ quote name) |
18231 | 911 |
|> select_generator (fn gname => "trying code generator " ^ gname ^ " for definition of " ^ quote name) |
18170 | 912 |
name defgens |
18454 | 913 |
|> debug 9 (fn _ => "checking creation of node " ^ quote name) |
18170 | 914 |
|> check_fail msg |
915 |
|-> (fn (def, names') => |
|
916 |
add (name, def) |
|
917 |
#> fold_map ensure_node names') |
|
918 |
|-> (fn names' => pair (name :: Library.flat names')) |
|
18231 | 919 |
) |
18170 | 920 |
in |
921 |
modl |
|
922 |
|> ensure_node name |
|
923 |
|-> (fn names => pair (names@deps)) |
|
924 |
end; |
|
925 |
||
18231 | 926 |
fun start_transact f modl = |
927 |
let |
|
928 |
fun handle_fail f modl = |
|
929 |
((([], modl) |> f) |
|
930 |
handle FAIL (msgs, NONE) => |
|
931 |
(error o cat_lines) ("code generation failed, while:" :: msgs)) |
|
932 |
handle FAIL (msgs, SOME e) => |
|
933 |
((writeln o cat_lines) ("code generation failed, while:" :: msgs); raise e); |
|
934 |
in |
|
935 |
modl |
|
936 |
|> handle_fail f |
|
937 |
|-> (fn x => fn (_, module) => (x, module)) |
|
938 |
end; |
|
18172 | 939 |
|
940 |
||
941 |
(** primitive language constructs **) |
|
942 |
||
18304 | 943 |
val class_eq = "Eq"; (*defined for all primitve types and extensionally for all datatypes*) |
18172 | 944 |
val type_bool = "Bool"; |
945 |
val type_integer = "Integer"; (*infinite!*) |
|
946 |
val type_float = "Float"; |
|
947 |
val type_pair = "Pair"; |
|
948 |
val type_list = "List"; |
|
949 |
val cons_true = "True"; |
|
950 |
val cons_false = "False"; |
|
951 |
val cons_not = "not"; |
|
952 |
val cons_pair = "Pair"; |
|
953 |
val cons_nil = "Nil"; |
|
954 |
val cons_cons = "Cons"; |
|
955 |
val fun_eq = "eq"; (*to class eq*) |
|
956 |
val fun_not = "not"; |
|
957 |
val fun_and = "and"; |
|
958 |
val fun_or = "or"; |
|
959 |
val fun_if = "if"; |
|
960 |
val fun_fst = "fst"; |
|
961 |
val fun_snd = "snd"; |
|
962 |
val fun_add = "add"; |
|
963 |
val fun_mult = "mult"; |
|
964 |
val fun_minus = "minus"; |
|
965 |
val fun_lt = "lt"; |
|
966 |
val fun_le = "le"; |
|
967 |
val fun_wfrec = "wfrec"; |
|
968 |
||
969 |
local |
|
970 |
||
971 |
val A = IVarT ("a", []); |
|
972 |
val B = IVarT ("b", []); |
|
973 |
val E = IVarT ("e", [class_eq]); |
|
974 |
||
975 |
in |
|
976 |
||
977 |
val Type_bool = type_bool `%% []; |
|
978 |
val Type_integer = type_integer `%% []; |
|
979 |
val Type_float = type_float `%% []; |
|
980 |
fun Type_pair a b = type_pair `%% [a, b]; |
|
981 |
fun Type_list a = type_list `%% [a]; |
|
982 |
val Cons_true = IConst (cons_true, Type_bool); |
|
983 |
val Cons_false = IConst (cons_false, Type_bool); |
|
984 |
val Cons_pair = IConst (cons_pair, A `-> B `-> Type_pair A B); |
|
985 |
val Cons_nil = IConst (cons_nil, Type_list A); |
|
986 |
val Cons_cons = IConst (cons_cons, A `-> Type_list A `-> Type_list A); |
|
987 |
val Fun_eq = IConst (fun_eq, E `-> E `-> Type_bool); |
|
988 |
val Fun_not = IConst (fun_not, Type_bool `-> Type_bool); |
|
989 |
val Fun_and = IConst (fun_and, Type_bool `-> Type_bool `-> Type_bool); |
|
990 |
val Fun_or = IConst (fun_or, Type_bool `-> Type_bool `-> Type_bool); |
|
991 |
val Fun_if = IConst (fun_if, Type_bool `-> A `-> A `-> A); |
|
992 |
val Fun_fst = IConst (fun_fst, Type_pair A B `-> A); |
|
993 |
val Fun_snd = IConst (fun_snd, Type_pair A B `-> B); |
|
994 |
val Fun_0 = IConst ("0", Type_integer); |
|
995 |
val Fun_1 = IConst ("1", Type_integer); |
|
996 |
val Fun_add = IConst (fun_add, Type_integer `-> Type_integer `-> Type_integer); |
|
997 |
val Fun_mult = IConst (fun_mult, Type_integer `-> Type_integer `-> Type_integer); |
|
998 |
val Fun_minus = IConst (fun_minus, Type_integer `-> Type_integer); |
|
999 |
val Fun_lt = IConst (fun_lt, Type_integer `-> Type_integer `-> Type_bool); |
|
1000 |
val Fun_le = IConst (fun_le, Type_integer `-> Type_integer `-> Type_bool); |
|
1001 |
val Fun_wfrec = IConst (fun_wfrec, ((A `-> B) `-> A `-> B) `-> A `-> B); |
|
1002 |
||
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1003 |
fun foldl1 f (x::xs) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1004 |
Library.foldl f (x, xs); |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1005 |
val ** = foldl1 (uncurry Type_pair); |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1006 |
val XXp = foldl1 (fn (a, b) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1007 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1008 |
val ty_a = itype_of_ipat a; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1009 |
val ty_b = itype_of_ipat b; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1010 |
in ICons ((cons_pair, [a, b]), Type_pair ty_a ty_b) end); |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1011 |
val XXe = foldl1 (fn (a, b) => |
18172 | 1012 |
let |
1013 |
val ty_a = itype_of_iexpr a; |
|
1014 |
val ty_b = itype_of_iexpr b; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1015 |
in IConst (cons_pair, ty_a `-> ty_b `-> Type_pair ty_a ty_b) `$ a `$ b end); |
18172 | 1016 |
|
1017 |
end; (* local *) |
|
1018 |
||
1019 |
val prims = [class_eq, type_bool, type_integer, type_float, type_pair, type_list, |
|
18385 | 1020 |
cons_true, cons_false, cons_pair, cons_nil, cons_cons, fun_eq, fun_not, fun_and, |
18172 | 1021 |
fun_or, fun_if, fun_fst, fun_snd, fun_add, fun_mult, fun_minus, fun_lt, fun_le, fun_wfrec]; |
1022 |
||
18335 | 1023 |
|
1024 |
(** equality handling **) |
|
1025 |
||
18385 | 1026 |
fun invoke_eq gen_ty gen_eq x (trns as (_ , modl)) = |
18335 | 1027 |
let |
18385 | 1028 |
fun mk_eqpred dtname = |
1029 |
let |
|
1030 |
val (vs, cons, _) = case get_def modl dtname of Datatype info => info; |
|
1031 |
val arity = map (rpair [class_eq] o fst) vs |
|
1032 |
val ty = IType (dtname, map IVarT arity); |
|
1033 |
fun mk_eq (c, []) = |
|
1034 |
([ICons ((c, []), ty), ICons ((c, []), ty)], Cons_true) |
|
1035 |
| mk_eq (c, tys) = |
|
1036 |
let |
|
1037 |
val vars1 = Term.invent_names [] "a" (length tys); |
|
1038 |
val vars2 = Term.invent_names vars1 "b" (length tys); |
|
1039 |
fun mk_eq_cons ty' (v1, v2) = |
|
1040 |
IConst (fun_eq, ty' `-> ty' `-> Type_bool) `$ IVarE (v1, ty) `$ IVarE (v2, ty) |
|
1041 |
fun mk_conj (e1, e2) = |
|
1042 |
Fun_and `$ e1 `$ e2; |
|
1043 |
in |
|
1044 |
([ICons ((c, map2 (curry IVarP) vars1 tys), ty), |
|
1045 |
ICons ((c, map2 (curry IVarP) vars2 tys), ty)], |
|
1046 |
foldr1 mk_conj (map2 mk_eq_cons tys (vars1 ~~ vars2))) |
|
1047 |
end; |
|
1048 |
val eqs = map mk_eq cons @ [([IVarP ("_", ty), IVarP ("_", ty)], Cons_false)]; |
|
1049 |
in |
|
1050 |
(Fun (eqs, (arity, ty `-> ty `-> Type_bool)), arity) |
|
1051 |
end; |
|
1052 |
fun invoke' (IType (tyco, tys)) trns = |
|
1053 |
trns |
|
1054 |
|> fold_map invoke' tys |
|
1055 |
|-> (fn is_eq => |
|
1056 |
if forall I is_eq |
|
1057 |
then if NameSpace.is_qualified tyco |
|
1058 |
then |
|
1059 |
gen_eq (tyco, mk_eqpred tyco) |
|
1060 |
#> pair true |
|
1061 |
else |
|
1062 |
pair true |
|
1063 |
else |
|
1064 |
pair false) |
|
1065 |
| invoke' (IFun _) trns = |
|
1066 |
trns |
|
1067 |
|> pair false |
|
1068 |
| invoke' (IVarT (_, sort)) trns = |
|
1069 |
trns |
|
1070 |
|> pair (member (op =) sort class_eq) |
|
18335 | 1071 |
in |
18385 | 1072 |
trns |
1073 |
|> gen_ty x |
|
1074 |
|-> (fn ty => invoke' ty) |
|
18335 | 1075 |
end; |
1076 |
||
1077 |
||
1078 |
(** generic transformation **) |
|
18304 | 1079 |
|
18172 | 1080 |
fun extract_defs e = |
1081 |
let |
|
1082 |
fun extr_itype (ty as IType (tyco, _)) = |
|
1083 |
cons tyco #> fold_itype extr_itype ty |
|
1084 |
| extr_itype ty = |
|
1085 |
fold_itype extr_itype ty |
|
1086 |
fun extr_ipat (p as ICons ((c, _), _)) = |
|
1087 |
cons c #> fold_ipat extr_itype extr_ipat p |
|
1088 |
| extr_ipat p = |
|
1089 |
fold_ipat extr_itype extr_ipat p |
|
1090 |
fun extr_iexpr (e as IConst (f, _)) = |
|
1091 |
cons f #> fold_iexpr extr_itype extr_ipat extr_iexpr e |
|
1092 |
| extr_iexpr e = |
|
1093 |
fold_iexpr extr_itype extr_ipat extr_iexpr e |
|
1094 |
in extr_iexpr e [] end; |
|
1095 |
||
1096 |
fun eta_expand query = |
|
1097 |
let |
|
1098 |
fun eta_app ((f, ty), es) = |
|
1099 |
let |
|
1100 |
val delta = query f - length es; |
|
1101 |
val add_n = if delta < 0 then 0 else delta; |
|
18282 | 1102 |
val tys = |
1103 |
(fst o unfold_fun) ty |
|
1104 |
|> curry Library.drop (length es) |
|
1105 |
|> curry Library.take add_n |
|
18172 | 1106 |
val add_vars = |
18304 | 1107 |
Term.invent_names (vars_of_iexprs es) "x" add_n ~~ tys; |
18172 | 1108 |
in |
1109 |
Library.foldr IAbs (add_vars, IConst (f, ty) `$$ es `$$ (map IVarE add_vars)) |
|
1110 |
end; |
|
1111 |
fun eta_iexpr' e = map_iexpr I I eta_iexpr e |
|
1112 |
and eta_iexpr (IConst (f, ty)) = |
|
1113 |
eta_app ((f, ty), []) |
|
1114 |
| eta_iexpr (e as IApp _) = |
|
1115 |
(case (unfold_app e) |
|
1116 |
of (IConst (f, ty), es) => |
|
1117 |
eta_app ((f, ty), map eta_iexpr es) |
|
1118 |
| _ => eta_iexpr' e) |
|
1119 |
| eta_iexpr e = eta_iexpr' e; |
|
1120 |
in map_defs (map_def_fun I eta_iexpr) end; |
|
1121 |
||
18216 | 1122 |
val eta_expand_poly = |
1123 |
let |
|
1124 |
fun map_def_fun (def as Fun ([([], e)], cty as (sortctxt, (ty as IFun (ty1, ty2))))) = |
|
1125 |
if (not o null) sortctxt |
|
18304 | 1126 |
orelse (null o tvars_of_itypes) [ty] |
18216 | 1127 |
then def |
1128 |
else |
|
1129 |
let |
|
18304 | 1130 |
val add_var = (hd (Term.invent_names (vars_of_iexprs [e]) "x" 1), ty1) |
18216 | 1131 |
in (Fun ([([IVarP add_var], IAbs (add_var, e))], cty)) end |
1132 |
| map_def_fun def = def; |
|
1133 |
in map_defs map_def_fun end; |
|
1134 |
||
18172 | 1135 |
fun tupelize_cons module = |
1136 |
let |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1137 |
fun replace_cons (cons as (_, [])) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1138 |
pair cons |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1139 |
| replace_cons (cons as (_, [_])) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1140 |
pair cons |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1141 |
| replace_cons (con, tys) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1142 |
cons con |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1143 |
#> pair (con, [** tys]) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1144 |
fun replace_def (_, (def as Datatype (vs, cs, insts))) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1145 |
fold_map replace_cons cs |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1146 |
#-> (fn cs => pair (Datatype (vs, cs, insts))) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1147 |
| replace_def (_, def) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1148 |
pair def |
18172 | 1149 |
fun replace_app cs ((f, ty), es) = |
1150 |
if member (op =) cs f |
|
1151 |
then |
|
1152 |
let |
|
1153 |
val (tys, ty') = unfold_fun ty |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1154 |
in IConst (f, ** tys `-> ty') `$ XXe es end |
18172 | 1155 |
else IConst (f, ty) `$$ es; |
1156 |
fun replace_iexpr cs (IConst (f, ty)) = |
|
1157 |
replace_app cs ((f, ty), []) |
|
1158 |
| replace_iexpr cs (e as IApp _) = |
|
1159 |
(case unfold_app e |
|
1160 |
of (IConst fty, es) => replace_app cs (fty, map (replace_iexpr cs) es) |
|
1161 |
| _ => map_iexpr I I (replace_iexpr cs) e) |
|
1162 |
| replace_iexpr cs e = map_iexpr I I (replace_iexpr cs) e; |
|
1163 |
fun replace_ipat cs (p as ICons ((c, ps), ty)) = |
|
1164 |
if member (op =) cs c then |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1165 |
ICons ((c, [XXp (map (replace_ipat cs) ps)]), ty) |
18172 | 1166 |
else map_ipat I (replace_ipat cs) p |
1167 |
| replace_ipat cs p = map_ipat I (replace_ipat cs) p; |
|
1168 |
in |
|
1169 |
transform_defs replace_def replace_ipat replace_iexpr [cons_cons] module |
|
1170 |
end; |
|
1171 |
||
1172 |
fun eliminate_classes module = |
|
1173 |
let |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1174 |
fun transform_itype (IVarT (v, s)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1175 |
IVarT (v, []) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1176 |
| transform_itype (ty as IDictT _) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1177 |
ty |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1178 |
| transform_itype ty = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1179 |
map_itype transform_itype ty; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1180 |
fun transform_ipat p = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1181 |
map_ipat transform_itype transform_ipat p; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1182 |
fun transform_iexpr vname_alist (IInst (e, ls)) = |
18172 | 1183 |
let |
18282 | 1184 |
fun transform_lookup (ClassPackage.Instance ((cdict, idict), ls)) = |
18172 | 1185 |
ls |
1186 |
|> transform_lookups |
|
18282 | 1187 |
|-> (fn tys => |
1188 |
curry mk_apps (IConst (idict, cdict `%% tys)) |
|
1189 |
#> pair (cdict `%% tys)) |
|
18172 | 1190 |
| transform_lookup (ClassPackage.Lookup (deriv, (v, i))) = |
1191 |
let |
|
1192 |
val (v', cls) = |
|
1193 |
(nth o the oo AList.lookup (op =)) vname_alist v i; |
|
1194 |
fun mk_parm tyco = tyco `%% [IVarT (v, [])]; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1195 |
in (mk_parm cls, ILookup (deriv, v')) end |
18172 | 1196 |
and transform_lookups lss = |
1197 |
map_yield (map_yield transform_lookup |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1198 |
#> apfst ** |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1199 |
#> apsnd XXe) lss |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1200 |
in transform_iexpr vname_alist e `$$ (snd o transform_lookups) ls end |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1201 |
| transform_iexpr vname_alist e = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1202 |
map_iexpr transform_itype transform_ipat (transform_iexpr vname_alist) e; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1203 |
fun elim_sorts (Fun (eqs, ([], ty))) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1204 |
Fun (map (fn (ps, rhs) => (map transform_ipat ps, transform_iexpr [] rhs)) eqs, |
18454 | 1205 |
([], transform_itype ty)) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1206 |
| elim_sorts (Fun (eqs, (sortctxt, ty))) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1207 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1208 |
val varnames_ctxt = |
18441 | 1209 |
burrow |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1210 |
(Term.invent_names ((vars_of_iexprs o map snd) eqs @ |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1211 |
(vars_of_ipats o Library.flat o map fst) eqs) "d" o length) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1212 |
(map snd sortctxt); |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1213 |
val vname_alist = map2 (fn (vt, sort) => fn vs => (vt, vs ~~ sort)) |
18454 | 1214 |
sortctxt varnames_ctxt; |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1215 |
val ty' = map (op ** o (fn (vt, vss) => map (fn (_, cls) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1216 |
cls `%% [IVarT (vt, [])]) vss)) vname_alist |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1217 |
`--> transform_itype ty; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1218 |
val ps_add = map (XXp o (fn (vt, vss) => map (fn (v, cls) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1219 |
IVarP (v, cls `%% [IVarT (vt, [])])) vss)) vname_alist; |
18454 | 1220 |
in Fun (map (fn (ps, rhs) => (ps_add @ map transform_ipat ps, transform_iexpr vname_alist rhs)) eqs, ([], ty')) end |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1221 |
| elim_sorts (Datatype (vars, constrs, insts)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1222 |
Datatype (map (fn (v, _) => (v, [])) vars, map (apsnd (map transform_itype)) constrs, insts) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1223 |
| elim_sorts (Typesyn (vars, ty)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1224 |
Typesyn (map (fn (v, _) => (v, [])) vars, transform_itype ty) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1225 |
| elim_sorts d = d; |
18515 | 1226 |
fun mk_cls_typ_map v (supclss, membrs) ty_inst = |
1227 |
(map (fn class => (class, IType (class, [ty_inst]))) supclss, |
|
1228 |
map (fn (m, (mctxt, ty)) => |
|
1229 |
(m, ty |> instant_itype (v, ty_inst))) membrs); |
|
1230 |
fun extract_members (cls, Class (supclss, v, membrs, _)) = |
|
1231 |
let |
|
1232 |
val ty_cls = cls `%% [IVarT (v, [])]; |
|
1233 |
val w = "d"; |
|
1234 |
val add_supclss = if null supclss then I else cons (v, supclss); |
|
1235 |
fun mk_fun (m, (mctxt, ty)) = (m, Fun ([([IVarP (w, ty_cls)], ILookup ([m], w))], |
|
1236 |
(add_supclss mctxt, ty `-> ty_cls))); |
|
1237 |
in fold (cons o mk_fun) membrs end |
|
1238 |
| extract_members _ = I; |
|
1239 |
fun introduce_dicts (Class (supclss, v, membrs, insts)) = |
|
1240 |
let |
|
1241 |
val varname_cls = Term.invent_names (tvars_of_itypes (map (snd o snd) membrs)) "a" 1 |> hd |
|
1242 |
in |
|
1243 |
Typesyn ([(varname_cls, supclss)], IDictT ((op @) (mk_cls_typ_map v (supclss, membrs) (IVarT (varname_cls, []))))) |
|
1244 |
end |
|
1245 |
| introduce_dicts (Classinst ((clsname, (tyco, arity)), (supinsts, memdefs))) = |
|
1246 |
let |
|
1247 |
val Class (supclss, v, members, _) = |
|
1248 |
if clsname = class_eq |
|
1249 |
then |
|
1250 |
Class ([], "a", [(fun_eq, ([], IVarT ("a", []) `-> IVarT ("a", []) `-> Type_bool))], []) |
|
1251 |
else |
|
1252 |
get_def module clsname; |
|
1253 |
val ty = tyco `%% map IVarT arity; |
|
1254 |
val (supinst_typ_map, mem_typ_map) = mk_cls_typ_map v (supclss, members) ty; |
|
1255 |
fun mk_meminst (m, ty) = |
|
1256 |
let |
|
1257 |
val (instname, instlookup) = (the o AList.lookup (op =) memdefs) m; |
|
1258 |
in |
|
1259 |
IInst (IConst (instname, ty), instlookup) |
|
1260 |
|> pair m |
|
1261 |
end; |
|
1262 |
val memdefs_ty = map mk_meminst mem_typ_map; |
|
1263 |
fun mk_supinst (supcls, dictty) = |
|
1264 |
let |
|
1265 |
val (instname, instlookup) = (the o AList.lookup (op =) supinsts) supcls; |
|
1266 |
in |
|
1267 |
IInst (IConst (instname, dictty), instlookup) |
|
1268 |
|> pair supcls |
|
1269 |
end; |
|
1270 |
val instdefs_ty = map mk_supinst supinst_typ_map; |
|
1271 |
in |
|
1272 |
Fun ([([], IDictE (instdefs_ty @ memdefs_ty))], |
|
1273 |
(arity, IType (clsname, [ty]))) |
|
1274 |
end |
|
1275 |
| introduce_dicts d = d; |
|
18172 | 1276 |
in |
1277 |
module |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1278 |
|> `(fn module => fold_defs extract_members module []) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1279 |
|-> (fn membrs => fold (fn (name, f) => map_def name (K f)) membrs) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1280 |
|> map_defs introduce_dicts |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1281 |
|> map_defs elim_sorts |
18172 | 1282 |
end; |
1283 |
||
18216 | 1284 |
|
1285 |
(** generic serialization **) |
|
1286 |
||
1287 |
(* resolving *) |
|
1288 |
||
1289 |
fun mk_resolvtab nspgrp validate module = |
|
1290 |
let |
|
1291 |
fun ensure_unique prfix prfix' name name' (locals, tab) = |
|
1292 |
let |
|
1293 |
fun uniquify name n = |
|
1294 |
let |
|
1295 |
val name' = if n = 0 then name else name ^ "_" ^ string_of_int n |
|
1296 |
in |
|
1297 |
if member (op =) locals name' |
|
1298 |
then uniquify name (n+1) |
|
1299 |
else case validate name |
|
1300 |
of NONE => name' |
|
1301 |
| SOME name' => uniquify name' n |
|
1302 |
end; |
|
1303 |
val name'' = uniquify name' 0; |
|
1304 |
in |
|
1305 |
(locals, tab) |
|
1306 |
|> apsnd (Symtab.update_new |
|
1307 |
(NameSpace.pack (prfix @ [name]), NameSpace.pack (prfix' @ [name'']))) |
|
1308 |
|> apfst (cons name'') |
|
1309 |
|> pair name'' |
|
1310 |
end; |
|
1311 |
fun fill_in prfix prfix' node tab = |
|
1312 |
let |
|
1313 |
val keys = Graph.keys node; |
|
1314 |
val nodes = AList.make (Graph.get_node node) keys; |
|
1315 |
val (mods, defs) = |
|
1316 |
nodes |
|
1317 |
|> List.partition (fn (_, Module _) => true | _ => false) |
|
1318 |
|> apfst (map (fn (name, Module m) => (name, m))) |
|
1319 |
|> apsnd (map fst) |
|
1320 |
fun modl_validate (name, modl) (locals, tab) = |
|
1321 |
(locals, tab) |
|
1322 |
|> ensure_unique prfix prfix' name name |
|
1323 |
|-> (fn name' => apsnd (fill_in (prfix @ [name]) (prfix @ [name']) modl)) |
|
1324 |
fun ensure_unique_sidf sidf = |
|
1325 |
let |
|
1326 |
val [shallow, name] = NameSpace.unpack sidf; |
|
1327 |
in |
|
1328 |
nspgrp |
|
1329 |
|> get_first |
|
1330 |
(fn grp => if member (op =) grp shallow |
|
1331 |
then grp |> remove (op =) shallow |> SOME else NONE) |
|
1332 |
|> these |
|
1333 |
|> map (fn s => NameSpace.pack [s, name]) |
|
1334 |
|> exists (member (op =) defs) |
|
1335 |
|> (fn b => if b then sidf else name) |
|
1336 |
end; |
|
1337 |
fun def_validate sidf (locals, tab) = |
|
1338 |
(locals, tab) |
|
1339 |
|> ensure_unique prfix prfix' sidf (ensure_unique_sidf sidf) |
|
1340 |
|> snd |
|
1341 |
in |
|
1342 |
([], tab) |
|
1343 |
|> fold modl_validate mods |
|
1344 |
|> fold def_validate defs |
|
1345 |
|> snd |
|
1346 |
end; |
|
1347 |
in |
|
1348 |
Symtab.empty |
|
1349 |
|> fill_in [] [] module |
|
1350 |
end; |
|
1351 |
||
18282 | 1352 |
fun mk_resolv tab = |
18216 | 1353 |
let |
1354 |
fun resolver modl name = |
|
1355 |
if NameSpace.is_qualified name then |
|
1356 |
let |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1357 |
val _ = debug 12 (fn name' => "resolving " ^ quote name ^ " in " ^ (quote o NameSpace.pack) modl) (); |
18216 | 1358 |
val modl' = if null modl then [] else (NameSpace.unpack o the o Symtab.lookup tab o NameSpace.pack) modl; |
1359 |
val name' = (NameSpace.unpack o the o Symtab.lookup tab) name |
|
1360 |
in |
|
18454 | 1361 |
(NameSpace.pack o snd o snd o get_prefix (op =)) (modl', name') |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1362 |
|> debug 12 (fn name' => "resolving " ^ quote name ^ " to " ^ quote name' ^ " in " ^ (quote o NameSpace.pack) modl) |
18216 | 1363 |
end |
1364 |
else name |
|
1365 |
in resolver end; |
|
1366 |
||
1367 |
||
1368 |
(* serialization *) |
|
1369 |
||
1370 |
fun serialize s_def s_module validate nspgrp name_root module = |
|
1371 |
let |
|
1372 |
val resolvtab = mk_resolvtab nspgrp validate module; |
|
1373 |
val resolver = mk_resolv resolvtab; |
|
1374 |
fun seri prfx ([(name, Module module)]) = |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1375 |
s_module (resolver prfx (prfx @ [name] |> NameSpace.pack), |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1376 |
List.mapPartial (seri (prfx @ [name])) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1377 |
((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)) |
18454 | 1378 |
|> SOME |
18216 | 1379 |
| seri prfx ds = |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1380 |
s_def (resolver prfx) (map |
18454 | 1381 |
(fn (name, Def def) => (resolver prfx (prfx @ [name] |> NameSpace.pack), def)) ds) |
18216 | 1382 |
in |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1383 |
setmp print_mode [] (fn _ => s_module (name_root, (List.mapPartial (seri []) |
18361 | 1384 |
((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)))) () |
18216 | 1385 |
end; |
1386 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1387 |
end; (* struct *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1388 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1389 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1390 |
structure CodegenThingol : CODEGEN_THINGOL = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1391 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1392 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1393 |
open CodegenThingolOp; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1394 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1395 |
end; (* struct *) |