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