author | haftmann |
Mon, 21 Nov 2005 15:15:32 +0100 | |
changeset 18216 | db7d43b25c99 |
parent 18172 | 8ff5bcfae27a |
child 18231 | 2eea98bbf650 |
permissions | -rw-r--r-- |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_thingol.ML |
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 eq_itype: itype * itype -> bool |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
29 |
val eq_ipat: ipat * ipat -> bool |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
30 |
val eq_iexpr: iexpr * iexpr -> bool |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
31 |
val mk_funs: itype list * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
32 |
val mk_apps: iexpr * iexpr list -> iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
33 |
val pretty_itype: itype -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
34 |
val pretty_ipat: ipat -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
35 |
val pretty_iexpr: iexpr -> Pretty.T; |
18216 | 36 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list; |
37 |
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
|
38 |
val unfold_fun: itype -> itype list * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
39 |
val unfold_app: iexpr -> iexpr * iexpr list; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
40 |
val unfold_let: iexpr -> (ipat * iexpr) list * iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
41 |
val itype_of_iexpr: iexpr -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
42 |
val ipat_of_iexpr: iexpr -> ipat; |
18172 | 43 |
val invent_var_t_names: itype list -> int -> vname list -> vname -> vname list; |
44 |
val invent_var_e_names: iexpr list -> int -> vname list -> vname -> vname list; |
|
18170 | 45 |
|
46 |
datatype def = |
|
47 |
Nop |
|
48 |
| Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype) |
|
18172 | 49 |
| Typesyn of (vname * string list) list * itype |
50 |
| Datatype of (vname * string list) list * string list * string list |
|
51 |
| Datatypecons of string * itype list |
|
18170 | 52 |
| Class of string list * string list * string list |
53 |
| Classmember of string * vname * itype |
|
54 |
| Classinst of string * (string * string list list) * (string * string) list; |
|
55 |
type module; |
|
56 |
type transact; |
|
57 |
type 'dst transact_fin; |
|
58 |
type ('src, 'dst) gen_codegen = 'src -> transact -> 'dst transact_fin; |
|
59 |
type gen_defgen = string -> transact -> (def * string list) transact_fin; |
|
60 |
val eq_def: def * def -> bool; |
|
61 |
val pretty_def: def -> Pretty.T; |
|
62 |
val pretty_module: module -> Pretty.T; |
|
63 |
val empty_module: module; |
|
64 |
val get_def: module -> string -> def; |
|
65 |
val merge_module: module * module -> module; |
|
66 |
val partof: string list -> module -> module; |
|
67 |
val succeed: 'a -> transact -> 'a transact_fin; |
|
68 |
val fail: string -> transact -> 'a transact_fin; |
|
69 |
val gen_invoke: (string * ('src, 'dst) gen_codegen) list -> string |
|
70 |
-> 'src -> transact -> 'dst * transact; |
|
71 |
val gen_ensure_def: (string * gen_defgen) list -> string |
|
72 |
-> string -> transact -> transact; |
|
18216 | 73 |
val start_transact: (transact -> 'a * transact) -> module -> 'a * module; |
74 |
||
75 |
val class_eq: string; |
|
76 |
val type_bool: string; |
|
77 |
val type_pair: string; |
|
78 |
val type_list: string; |
|
79 |
val type_integer: string; |
|
80 |
val cons_pair: string; |
|
81 |
val fun_fst: string; |
|
82 |
val fun_snd: string; |
|
83 |
val Type_integer: itype; |
|
84 |
val Cons_true: iexpr; |
|
85 |
val Cons_false: iexpr; |
|
86 |
val Cons_pair: iexpr; |
|
87 |
val Cons_nil: iexpr; |
|
88 |
val Cons_cons: iexpr; |
|
89 |
val Fun_eq: iexpr; |
|
90 |
val Fun_not: iexpr; |
|
91 |
val Fun_and: iexpr; |
|
92 |
val Fun_or: iexpr; |
|
93 |
val Fun_if: iexpr; |
|
94 |
val Fun_fst: iexpr; |
|
95 |
val Fun_snd: iexpr; |
|
96 |
val Fun_0: iexpr; |
|
97 |
val Fun_1: iexpr; |
|
98 |
val Fun_add: iexpr; |
|
99 |
val Fun_mult: iexpr; |
|
100 |
val Fun_minus: iexpr; |
|
101 |
val Fun_lt: iexpr; |
|
102 |
val Fun_le: iexpr; |
|
103 |
val Fun_wfrec: iexpr; |
|
18170 | 104 |
|
18172 | 105 |
val prims: string list; |
106 |
val extract_defs: iexpr -> string list; |
|
107 |
val eta_expand: (string -> int) -> module -> module; |
|
18216 | 108 |
val eta_expand_poly: module -> module; |
18172 | 109 |
val connect_datatypes_clsdecls: module -> module; |
110 |
val tupelize_cons: module -> module; |
|
111 |
val eliminate_classes: module -> module; |
|
112 |
||
18170 | 113 |
val debug_level : int ref; |
114 |
val debug : int -> ('a -> string) -> 'a -> 'a; |
|
18216 | 115 |
|
116 |
val serialize: |
|
117 |
((string -> string) -> (string * def) list -> Pretty.T) |
|
118 |
-> (string * Pretty.T list -> Pretty.T) |
|
119 |
-> (string -> string option) |
|
120 |
-> string list list -> string -> module -> Pretty.T |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
121 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
122 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
123 |
signature CODEGEN_THINGOL_OP = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
124 |
sig |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
125 |
include CODEGEN_THINGOL; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
126 |
val `%% : string * itype list -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
127 |
val `-> : itype * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
128 |
val `--> : itype list * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
129 |
val `$ : iexpr * iexpr -> iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
130 |
val `$$ : iexpr * iexpr list -> iexpr; |
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 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
134 |
structure CodegenThingolOp: CODEGEN_THINGOL_OP = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
135 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
136 |
|
18170 | 137 |
(** auxiliary **) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
138 |
|
18170 | 139 |
val debug_level = ref 0; |
140 |
fun debug d f x = (if d <= !debug_level then Output.debug (f x) else (); x); |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
141 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
142 |
fun foldl' f (l, []) = the l |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
143 |
| foldl' f (_, (r::rs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
144 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
145 |
fun itl (l, []) = l |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
146 |
| itl (l, r::rs) = itl (f (l, r), rs) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
147 |
in itl (r, rs) end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
148 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
149 |
fun foldr' f ([], r) = the r |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
150 |
| foldr' f (ls, _) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
151 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
152 |
fun itr [l] = l |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
153 |
| itr (l::ls) = f (l, itr ls) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
154 |
in itr ls end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
155 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
156 |
fun unfoldl dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
157 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
158 |
of NONE => (x, []) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
159 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
160 |
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
|
161 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
162 |
fun unfoldr dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
163 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
164 |
of NONE => ([], x) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
165 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
166 |
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
|
167 |
|
18172 | 168 |
fun map_yield f [] = ([], []) |
169 |
| map_yield f (x::xs) = |
|
170 |
let |
|
171 |
val (y, x') = f x |
|
172 |
val (ys, xs') = map_yield f xs |
|
173 |
in (y::ys, x'::xs') end; |
|
174 |
||
18170 | 175 |
fun get_prefix eq ([], ys) = ([], [], ys) |
176 |
| get_prefix eq (xs, []) = ([], xs, []) |
|
177 |
| get_prefix eq (xs as x::xs', ys as y::ys') = |
|
178 |
if eq (x, y) then |
|
179 |
let val (ps', xs'', ys'') = get_prefix eq (xs', ys') |
|
180 |
in (x::ps', xs'', ys'') end |
|
181 |
else ([], xs, ys); |
|
182 |
||
183 |
||
184 |
(** language core - types, pattern, expressions **) |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
185 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
186 |
(* language representation *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
187 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
188 |
infix 8 `%%; |
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 |
infixr 6 `-->; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
191 |
infix 4 `$; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
192 |
infix 4 `$$; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
193 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
194 |
type vname = string; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
195 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
196 |
datatype itype = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
197 |
IType of string * itype list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
198 |
| IFun of itype * itype |
18172 | 199 |
| IVarT of vname * sort |
200 |
(*ML auxiliary*) |
|
201 |
| IDictT of (string * itype) list; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
202 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
203 |
datatype ipat = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
204 |
ICons of (string * ipat list) * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
205 |
| IVarP of vname * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
206 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
207 |
datatype iexpr = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
208 |
IConst of string * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
209 |
| IVarE of vname * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
210 |
| IApp of iexpr * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
211 |
| IInst of iexpr * ClassPackage.sortlookup list list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
212 |
| IAbs of (vname * itype) * iexpr |
18172 | 213 |
| ICase of iexpr * (ipat * iexpr) list |
214 |
(*ML auxiliary*) |
|
215 |
| IDictE of (string * iexpr) list |
|
216 |
| ILookup of (string list * vname); |
|
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 eq_itype = (op =); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
219 |
val eq_ipat = (op =); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
220 |
val eq_iexpr = (op =); |
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 mk_funs = Library.foldr IFun; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
223 |
val mk_apps = Library.foldl IApp; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
224 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
225 |
fun tyco `%% tys = IType (tyco, tys); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
226 |
val op `-> = IFun; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
227 |
fun f `$ x = IApp (f, x); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
228 |
val op `--> = mk_funs; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
229 |
val op `$$ = mk_apps; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
230 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
231 |
val unfold_fun = unfoldr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
232 |
(fn IFun t => SOME t |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
233 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
234 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
235 |
val unfold_app = unfoldl |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
236 |
(fn IApp e => SOME e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
237 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
238 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
239 |
val unfold_let = unfoldr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
240 |
(fn ICase (e, [(p, e')]) => SOME ((p, e), e') |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
241 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
242 |
|
18172 | 243 |
fun map_itype f_itype (IType (tyco, tys)) = |
244 |
tyco `%% map f_itype tys |
|
245 |
| map_itype f_itype (IFun (t1, t2)) = |
|
246 |
f_itype t1 `-> f_itype t2 |
|
247 |
| map_itype _ (ty as IVarT _) = |
|
248 |
ty; |
|
249 |
||
250 |
fun map_ipat f_itype f_ipat (ICons ((c, ps), ty)) = |
|
251 |
ICons ((c, map f_ipat ps), f_itype ty) |
|
252 |
| map_ipat _ _ (p as IVarP _) = |
|
253 |
p; |
|
254 |
||
255 |
fun map_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) = |
|
256 |
f_iexpr e1 `$ f_iexpr e2 |
|
257 |
| map_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) = |
|
258 |
IInst (f_iexpr e, c) |
|
259 |
| map_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) = |
|
260 |
IAbs (v, f_iexpr e) |
|
261 |
| map_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) = |
|
262 |
ICase (f_iexpr e, map (fn (p, e) => (f_ipat p, f_iexpr e)) ps) |
|
263 |
| map_iexpr _ _ _ (e as IConst _) = |
|
264 |
e |
|
265 |
| map_iexpr _ _ _ (e as IVarE _) = |
|
266 |
e; |
|
267 |
||
268 |
fun fold_itype f_itype (IFun (t1, t2)) = |
|
269 |
f_itype t1 #> f_itype t2 |
|
270 |
| fold_itype _ (ty as IType _) = |
|
271 |
I |
|
272 |
| fold_itype _ (ty as IVarT _) = |
|
273 |
I; |
|
274 |
||
275 |
fun fold_ipat f_itype f_ipat (ICons ((_, ps), ty)) = |
|
276 |
f_itype ty #> fold f_ipat ps |
|
277 |
| fold_ipat f_itype f_ipat (p as IVarP _) = |
|
278 |
I; |
|
279 |
||
280 |
fun fold_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) = |
|
281 |
f_iexpr e1 #> f_iexpr e2 |
|
282 |
| fold_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) = |
|
283 |
f_iexpr e |
|
284 |
| fold_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) = |
|
285 |
f_iexpr e |
|
286 |
| fold_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) = |
|
287 |
f_iexpr e #> fold (fn (p, e) => f_ipat p #> f_iexpr e) ps |
|
288 |
| fold_iexpr _ _ _ (e as IConst _) = |
|
289 |
I |
|
290 |
| fold_iexpr _ _ _ (e as IVarE _) = |
|
291 |
I; |
|
292 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
293 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
294 |
(* simple diagnosis *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
295 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
296 |
fun pretty_itype (IType (tyco, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
297 |
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
|
298 |
| pretty_itype (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
299 |
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
|
300 |
| pretty_itype (IVarT (v, sort)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
301 |
Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort)) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
302 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
303 |
fun pretty_ipat (ICons ((cons, ps), ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
304 |
Pretty.gen_list " " "(" ")" |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
305 |
(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
|
306 |
| pretty_ipat (IVarP (v, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
307 |
Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
308 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
309 |
fun pretty_iexpr (IConst (f, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
310 |
Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
311 |
| pretty_iexpr (IVarE (v, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
312 |
Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
313 |
| pretty_iexpr (IApp (e1, e2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
314 |
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
|
315 |
| pretty_iexpr (IInst (e, c)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
316 |
pretty_iexpr e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
317 |
| pretty_iexpr (IAbs ((v, ty), e)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
318 |
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
|
319 |
| pretty_iexpr (ICase (e, cs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
320 |
Pretty.enclose "(" ")" [ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
321 |
Pretty.str "case ", |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
322 |
pretty_iexpr e, |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
323 |
Pretty.enclose "(" ")" (map (fn (p, e) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
324 |
Pretty.block [ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
325 |
pretty_ipat p, |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
326 |
Pretty.str " => ", |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
327 |
pretty_iexpr e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
328 |
] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
329 |
) cs) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
330 |
] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
331 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
332 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
333 |
(* language auxiliary *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
334 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
335 |
fun itype_of_iexpr (IConst (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
336 |
| itype_of_iexpr (IVarE (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
337 |
| 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
|
338 |
of (IFun (ty2, ty')) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
339 |
if ty2 = itype_of_iexpr e2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
340 |
then ty' |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
341 |
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
|
342 |
^ ", " ^ (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
|
343 |
| _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1))) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
344 |
| itype_of_iexpr (IInst (e, cs)) = error "" |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
345 |
| 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
|
346 |
| itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
347 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
348 |
fun itype_of_ipat (ICons (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
349 |
| itype_of_ipat (IVarP (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
350 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
351 |
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
|
352 |
| ipat_of_iexpr (IVarE v) = IVarP v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
353 |
| ipat_of_iexpr (e as IApp _) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
354 |
case unfold_app e of (IConst (f, ty), es) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
355 |
ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
356 |
|
18216 | 357 |
fun vars_of_itype ty = |
358 |
let |
|
359 |
fun vars (IType (_, tys)) = fold vars tys |
|
360 |
| vars (IFun (ty1, ty2)) = vars ty1 #> vars ty2 |
|
361 |
| vars (IVarT (v, _)) = cons v |
|
362 |
in vars ty [] end; |
|
363 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
364 |
fun vars_of_ipats ps = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
365 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
366 |
fun vars (ICons ((_, ps), _)) = fold vars ps |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
367 |
| vars (IVarP (v, _)) = cons v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
368 |
in fold vars ps [] end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
369 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
370 |
fun instant_itype (v, sty) ty = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
371 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
372 |
fun instant (IType (tyco, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
373 |
tyco `%% map instant tys |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
374 |
| instant (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
375 |
instant ty1 `-> instant ty2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
376 |
| instant (w as (IVarT (u, _))) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
377 |
if v = u then sty else w |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
378 |
in instant ty end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
379 |
|
18172 | 380 |
fun invent_var_t_names tys n used a = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
381 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
382 |
fun invent (IType (_, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
383 |
fold invent tys |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
384 |
| invent (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
385 |
invent ty1 #> invent ty2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
386 |
| invent (IVarT (v, _)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
387 |
cons v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
388 |
in Term.invent_names (fold invent tys used) a n end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
389 |
|
18172 | 390 |
fun invent_var_e_names es n used a = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
391 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
392 |
fun invent (IConst (f, _)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
393 |
I |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
394 |
| invent (IVarE (v, _)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
395 |
cons v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
396 |
| invent (IApp (e1, e2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
397 |
invent e1 #> invent e2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
398 |
| invent (IAbs ((v, _), e)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
399 |
cons v #> invent e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
400 |
| invent (ICase (e, cs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
401 |
invent e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
402 |
#> |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
403 |
fold (fn (p, e) => append (vars_of_ipats [p]) #> invent e) cs |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
404 |
in Term.invent_names (fold invent es used) a n end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
405 |
|
18170 | 406 |
|
407 |
(** language module system - definitions, modules, transactions **) |
|
408 |
||
409 |
||
410 |
||
411 |
(* type definitions *) |
|
412 |
||
413 |
datatype def = |
|
414 |
Nop |
|
415 |
| Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype) |
|
18172 | 416 |
| Typesyn of (vname * string list) list * itype |
417 |
| Datatype of (vname * string list) list * string list * string list |
|
418 |
| Datatypecons of string * itype list |
|
18170 | 419 |
| Class of string list * string list * string list |
420 |
| Classmember of string * string * itype |
|
421 |
| Classinst of string * (string * string list list) * (string * string) list; |
|
422 |
||
423 |
datatype node = Def of def | Module of node Graph.T; |
|
424 |
type module = node Graph.T; |
|
425 |
type transact = Graph.key list * module; |
|
426 |
datatype 'dst transact_res = Succeed of 'dst | Fail of string; |
|
427 |
type 'dst transact_fin = 'dst transact_res * transact; |
|
428 |
type ('src, 'dst) gen_codegen = 'src -> transact -> 'dst transact_fin; |
|
429 |
type gen_defgen = string -> transact -> (def * string list) transact_fin; |
|
430 |
exception FAIL of string; |
|
431 |
||
432 |
val eq_def = (op =); |
|
433 |
||
434 |
(* simple diagnosis *) |
|
435 |
||
436 |
fun pretty_def Nop = |
|
437 |
Pretty.str "<NOP>" |
|
438 |
| pretty_def (Fun (eqs, (_, ty))) = |
|
439 |
Pretty.gen_list " |" "" "" ( |
|
440 |
map (fn (ps, body) => |
|
441 |
Pretty.block [ |
|
442 |
Pretty.gen_list "," "[" "]" (map pretty_ipat ps), |
|
443 |
Pretty.str " |->", |
|
444 |
Pretty.brk 1, |
|
445 |
pretty_iexpr body, |
|
446 |
Pretty.str "::", |
|
447 |
pretty_itype ty |
|
448 |
]) eqs |
|
449 |
) |
|
18172 | 450 |
| pretty_def (Typesyn (vs, ty)) = |
18170 | 451 |
Pretty.block [ |
452 |
Pretty.list "(" ")" (map (pretty_itype o IVarT) vs), |
|
453 |
Pretty.str " |=> ", |
|
454 |
pretty_itype ty |
|
455 |
] |
|
18172 | 456 |
| pretty_def (Datatype (vs, cs, clss)) = |
18170 | 457 |
Pretty.block [ |
458 |
Pretty.list "(" ")" (map (pretty_itype o IVarT) vs), |
|
459 |
Pretty.str " |=> ", |
|
460 |
Pretty.gen_list " |" "" "" (map Pretty.str cs), |
|
461 |
Pretty.str ", instance of ", |
|
462 |
Pretty.gen_list "," "[" "]" (map Pretty.str clss) |
|
463 |
] |
|
18172 | 464 |
| pretty_def (Datatypecons (dtname, tys)) = |
18170 | 465 |
Pretty.block [ |
466 |
Pretty.str "cons ", |
|
467 |
Pretty.gen_list " ->" "" "" (map pretty_itype tys @ [Pretty.str dtname]) |
|
468 |
] |
|
469 |
| pretty_def (Class (supcls, mems, insts)) = |
|
470 |
Pretty.str "Class ..." |
|
471 |
| pretty_def (Classmember (cls, v, ty)) = |
|
472 |
Pretty.str "Classmember ..." |
|
473 |
| pretty_def (Classinst (cls, insts, mems)) = |
|
474 |
Pretty.str "Classinst ..." |
|
475 |
||
476 |
fun pretty_module modl = |
|
477 |
let |
|
478 |
fun pretty (name, Module modl) = |
|
479 |
Pretty.block ( |
|
480 |
Pretty.str ("module " ^ name ^ " {") |
|
481 |
:: Pretty.brk 1 |
|
482 |
:: Pretty.chunks (map pretty (AList.make (Graph.get_node modl) |
|
483 |
(Graph.strong_conn modl |> List.concat |> rev))) |
|
484 |
:: Pretty.str "}" :: nil |
|
485 |
) |
|
486 |
| pretty (name, Def def) = |
|
487 |
Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def] |
|
488 |
in pretty ("//", Module modl) end; |
|
489 |
||
490 |
||
491 |
(* name handling *) |
|
492 |
||
493 |
fun dest_name name = |
|
494 |
let |
|
495 |
val name' = NameSpace.unpack name |
|
496 |
val (name'', name_base) = split_last name' |
|
497 |
val (modl, shallow) = split_last name'' |
|
498 |
in (modl, NameSpace.pack [shallow, name_base]) end |
|
499 |
handle Empty => error ("not a qualified name: " ^ quote name); |
|
500 |
||
501 |
fun dest_modl (Module m) = m; |
|
502 |
fun dest_def (Def d) = d; |
|
503 |
||
504 |
||
505 |
(* modules *) |
|
506 |
||
507 |
val empty_module = Graph.empty; (*read: "depends on"*) |
|
508 |
||
509 |
fun get_def modl name = |
|
510 |
case dest_name name |
|
511 |
of (modlname, base) => |
|
512 |
let |
|
513 |
fun get (Module node) [] = |
|
514 |
(dest_def o Graph.get_node node) base |
|
515 |
| get (Module node) (m::ms) = |
|
516 |
get (Graph.get_node node m) ms |
|
517 |
in get (Module modl) modlname end; |
|
518 |
||
519 |
fun add_def (name, def) = |
|
520 |
let |
|
521 |
val (modl, base) = dest_name name; |
|
522 |
fun add [] = |
|
523 |
Graph.new_node (base, Def def) |
|
524 |
| add (m::ms) = |
|
525 |
Graph.default_node (m, Module empty_module) |
|
526 |
#> Graph.map_node m (Module o add ms o dest_modl) |
|
527 |
in add modl end; |
|
528 |
||
529 |
fun map_def name f = |
|
530 |
let |
|
531 |
val (modl, base) = dest_name name; |
|
532 |
fun mapp [] = |
|
533 |
Graph.map_node base (Def o f o dest_def) |
|
534 |
| mapp (m::ms) = |
|
535 |
Graph.map_node m (Module o mapp ms o dest_modl) |
|
536 |
in mapp modl end; |
|
537 |
||
538 |
fun add_dep (name1, name2) modl = |
|
539 |
if name1 = name2 then modl |
|
540 |
else |
|
541 |
let |
|
542 |
val m1 = dest_name name1 |> apsnd single |> (op @); |
|
543 |
val m2 = dest_name name2 |> apsnd single |> (op @); |
|
544 |
val (ms, r1, r2) = get_prefix (op =) (m1, m2); |
|
545 |
val (ms, s1::r1, s2::r2) = get_prefix (op =) (m1, m2); |
|
546 |
val add_edge = |
|
547 |
if null r1 andalso null r2 |
|
548 |
then Graph.add_edge |
|
549 |
else Graph.add_edge_acyclic |
|
550 |
fun add [] node = |
|
551 |
node |
|
552 |
|> add_edge (s1, s2) |
|
553 |
| add (m::ms) node = |
|
554 |
node |
|
555 |
|> Graph.map_node m (Module o add ms o dest_modl); |
|
556 |
in add ms modl end; |
|
557 |
||
558 |
fun map_defs f = |
|
559 |
let |
|
560 |
fun mapp (Def def) = |
|
561 |
(Def o f) def |
|
562 |
| mapp (Module modl) = |
|
563 |
(Module o Graph.map_nodes mapp) modl |
|
564 |
in dest_modl o mapp o Module end; |
|
565 |
||
566 |
fun fold_defs f = |
|
567 |
let |
|
568 |
fun fol prfix (name, Def def) = |
|
569 |
f (NameSpace.pack (prfix @ [name]), def) |
|
570 |
| fol prfix (name, Module modl) = |
|
571 |
Graph.fold_nodes (fol (prfix @ [name])) modl |
|
572 |
in Graph.fold_nodes (fol []) end; |
|
573 |
||
574 |
fun add_deps f modl = |
|
575 |
modl |
|
576 |
|> fold add_dep ([] |> fold_defs (append o f) modl); |
|
577 |
||
578 |
fun fold_map_defs f = |
|
579 |
let |
|
580 |
fun foldmap prfix (name, Def def) = |
|
581 |
apfst Def o f (NameSpace.pack (prfix @ [name]), def) |
|
582 |
| foldmap prfix (name, Module modl) = |
|
583 |
apfst Module o Graph.fold_map_nodes (foldmap (prfix @ [name])) modl |
|
584 |
in Graph.fold_map_nodes (foldmap []) end; |
|
585 |
||
18172 | 586 |
fun map_def_fun f_ipat f_iexpr (Fun (eqs, cty)) = |
587 |
Fun (map (fn (ps, rhs) => (map f_ipat ps, f_iexpr rhs)) eqs, cty) |
|
588 |
| map_def_fun _ _ def = def; |
|
589 |
||
590 |
fun transform_defs f_def f_ipat f_iexpr s modl = |
|
591 |
let |
|
592 |
val (modl', s') = fold_map_defs f_def modl s |
|
593 |
in |
|
594 |
modl' |
|
595 |
|> map_defs (map_def_fun (f_ipat s') (f_iexpr s')) |
|
596 |
end; |
|
597 |
||
18170 | 598 |
fun merge_module modl12 = |
599 |
let |
|
600 |
fun join_module (Module m1, Module m2) = |
|
601 |
(SOME o Module) (merge_module (m1, m2)) |
|
602 |
| join_module (Def d1, Def d2) = |
|
603 |
if eq_def (d1, d2) then (SOME o Def) d1 else NONE |
|
604 |
| join_module _ = |
|
605 |
NONE |
|
606 |
in Graph.join (K join_module) modl12 end; |
|
607 |
||
608 |
fun partof names modl = |
|
609 |
let |
|
610 |
datatype pathnode = PN of (string list * (string * pathnode) list); |
|
18172 | 611 |
fun mk_ipath ([], base) (PN (defs, modls)) = |
18170 | 612 |
PN (base :: defs, modls) |
18172 | 613 |
| mk_ipath (n::ns, base) (PN (defs, modls)) = |
18170 | 614 |
modls |
615 |
|> AList.default (op =) (n, PN ([], [])) |
|
18172 | 616 |
|> AList.map_entry (op =) n (mk_ipath (ns, base)) |
18170 | 617 |
|> (pair defs #> PN); |
618 |
fun select (PN (defs, modls)) (Module module) = |
|
619 |
module |
|
620 |
|> Graph.subgraph (Graph.all_succs module (defs @ map fst modls)) |
|
621 |
|> fold (fn (name, modls) => Graph.map_node name (select modls)) modls |
|
622 |
|> Module; |
|
623 |
in |
|
624 |
Module modl |
|
18172 | 625 |
|> select (fold (mk_ipath o dest_name) (filter NameSpace.is_qualified names) (PN ([], []))) |
18170 | 626 |
|> dest_modl |
627 |
end; |
|
628 |
||
18172 | 629 |
fun add_check_transform (name, (Datatypecons (dtname, _))) = |
18170 | 630 |
([([dtname], |
18172 | 631 |
fn [Datatype (_, _, [])] => NONE | _ => "attempted to add constructor to already instantiating datatype" |> SOME)], |
18170 | 632 |
[(dtname, |
18172 | 633 |
fn Datatype (vs, cs, clss) => Datatype (vs, name::cs, clss) |
18170 | 634 |
| def => "attempted to add datatype constructor to non-datatype: " |
635 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
636 |
| add_check_transform (name, Classmember (clsname, v, ty)) = |
|
637 |
let |
|
638 |
fun check_var (IType (tyco, tys)) s = |
|
639 |
fold check_var tys s |
|
640 |
| check_var (IFun (ty1, ty2)) s = |
|
641 |
s |
|
642 |
|> check_var ty1 |
|
643 |
|> check_var ty2 |
|
644 |
| check_var (IVarT (w, sort)) s = |
|
645 |
if v = w |
|
646 |
andalso member (op =) sort clsname |
|
647 |
then "additional class appears at type variable" |> SOME |
|
648 |
else NONE |
|
649 |
in |
|
650 |
([([], fn [] => check_var ty NONE), |
|
651 |
([clsname], |
|
652 |
fn [Class (_, _, [])] => NONE |
|
653 |
| _ => "attempted to add class member to witnessed class" |> SOME)], |
|
654 |
[(clsname, |
|
655 |
fn Class (supcs, mems, insts) => Class (supcs, name::mems, insts) |
|
656 |
| def => "attempted to add class member to non-class" |
|
657 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
658 |
end |
|
659 |
| add_check_transform (name, Classinst (clsname, (tyco, arity), memdefs)) = |
|
660 |
let |
|
661 |
fun check [Classmember (_, v, mtyp_c), Fun (_, (_, mtyp_i))] = |
|
662 |
let |
|
663 |
val mtyp_i' = instant_itype (v, tyco `%% |
|
18172 | 664 |
map2 IVarT ((invent_var_t_names [mtyp_c] (length arity) [] "a"), arity)) mtyp_c; |
18170 | 665 |
in if eq_itype (mtyp_i', mtyp_i) (*! PERHAPS TOO STRICT !*) |
666 |
then NONE |
|
667 |
else "wrong type signature for class member: " |
|
668 |
^ (Pretty.output o pretty_itype) mtyp_i' ^ " expected," |
|
669 |
^ (Pretty.output o pretty_itype) mtyp_i ^ " given" |> SOME end |
|
670 |
in |
|
671 |
(map (fn (memname, memprim) => ((writeln memname; writeln memprim; [memname, memprim]), check)) memdefs, |
|
672 |
[(clsname, |
|
673 |
fn Class (supcs, mems, insts) => Class (supcs, mems, name::insts) |
|
674 |
| def => "attempted to add class instance to non-class" |
|
675 |
^ (Pretty.output o pretty_def) def |> error), |
|
676 |
(tyco, |
|
18172 | 677 |
fn Datatype (vs, cs, clss) => Datatype (vs, cs, clsname::clss) |
18170 | 678 |
| Nop => Nop |
679 |
| def => "attempted to instantiate non-type to class instance" |
|
680 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
681 |
end |
|
682 |
| add_check_transform _ = ([], []); |
|
683 |
||
684 |
fun succeed some = (pair o Succeed) some; |
|
685 |
fun fail msg = (pair o Fail) msg; |
|
686 |
||
687 |
fun check_fail msg' (Succeed dst, trns) = (dst, trns) |
|
688 |
| check_fail msg' (Fail errmsg, _) = (tracing ("ROLLBACK CHECK: " ^ errmsg ^ "\n" ^ msg'); raise FAIL errmsg); |
|
689 |
||
690 |
fun handle_fail msg f modl = |
|
691 |
f modl handle FAIL msg' => ([], modl) |> fail (msg ^ "\n" ^ msg'); |
|
692 |
||
693 |
fun select_generator print_msg src [] trns = |
|
694 |
fail ("no code generator available") trns |
|
695 |
| select_generator print_msg src [(gname, cgen)] trns = |
|
696 |
(print_msg gname; cgen src trns) |
|
697 |
| select_generator print_msg src ((gname, cgen)::cgens) trns = |
|
698 |
case cgen src trns |
|
699 |
of result as (Succeed _, _) => |
|
700 |
(print_msg gname; result) |
|
701 |
| _ => select_generator print_msg src cgens trns |
|
702 |
||
703 |
fun gen_invoke codegens msg src (deps, modl) = |
|
704 |
([], modl) |
|
705 |
|> select_generator (fn gname => "choosing code generator " ^ gname ^ " for source " ^ quote msg) |
|
706 |
src codegens |
|
707 |
|> check_fail msg |
|
708 |
||> (fn (deps', modl') => (append deps' deps, modl')); |
|
709 |
||
710 |
fun gen_ensure_def defgens msg name (deps, modl) = |
|
711 |
let |
|
712 |
fun add (name, def) (deps, modl) = |
|
713 |
let |
|
714 |
val (checks, trans) = add_check_transform (name, def); |
|
715 |
fun check (check_defs, checker) modl = |
|
716 |
case checker (check_defs |> filter NameSpace.is_qualified |> map (get_def modl)) |
|
717 |
of NONE => modl |
|
718 |
| SOME e => raise FAIL e; |
|
719 |
fun transform (name, f) modl = |
|
720 |
modl |
|
721 |
|> K (NameSpace.is_qualified name) ? map_def name f; |
|
722 |
in |
|
723 |
modl |
|
724 |
|> fold check checks |
|
725 |
|> fold (curry add_dep name) deps |
|
726 |
|> map_def name (fn _ => def) |
|
727 |
|> fold transform trans |
|
728 |
end; |
|
729 |
fun ensure_node name modl = |
|
730 |
if can (get_def modl) name |
|
731 |
then ([name], modl) |
|
732 |
else |
|
733 |
([], modl |> add_def (name, Nop)) |
|
734 |
|> select_generator (fn gname => "choosing code generator " ^ gname ^ " for definition of " ^ quote name) |
|
735 |
name defgens |
|
736 |
|> check_fail msg |
|
737 |
|-> (fn (def, names') => |
|
738 |
add (name, def) |
|
739 |
#> fold_map ensure_node names') |
|
740 |
|-> (fn names' => pair (name :: Library.flat names')) |
|
741 |
in |
|
742 |
modl |
|
743 |
|> ensure_node name |
|
744 |
|-> (fn names => pair (names@deps)) |
|
745 |
end; |
|
746 |
||
18216 | 747 |
fun start_transact f module = |
748 |
([], module) |
|
749 |
|> f |
|
750 |
|-> (fn x => fn (_, module) => (x, module)); |
|
18172 | 751 |
|
752 |
||
753 |
(** primitive language constructs **) |
|
754 |
||
755 |
val class_eq = "Eqtype"; (*defined for all primitve types and extensionally for all datatypes*) |
|
756 |
val type_bool = "Bool"; |
|
757 |
val type_integer = "Integer"; (*infinite!*) |
|
758 |
val type_float = "Float"; |
|
759 |
val type_pair = "Pair"; |
|
760 |
val type_list = "List"; |
|
761 |
val cons_true = "True"; |
|
762 |
val cons_false = "False"; |
|
763 |
val cons_not = "not"; |
|
764 |
val cons_pair = "Pair"; |
|
765 |
val cons_nil = "Nil"; |
|
766 |
val cons_cons = "Cons"; |
|
767 |
val fun_primeq = "primeq"; (*defined for all primitive types*) |
|
768 |
val fun_eq = "eq"; (*to class eq*) |
|
769 |
val fun_not = "not"; |
|
770 |
val fun_and = "and"; |
|
771 |
val fun_or = "or"; |
|
772 |
val fun_if = "if"; |
|
773 |
val fun_fst = "fst"; |
|
774 |
val fun_snd = "snd"; |
|
775 |
val fun_add = "add"; |
|
776 |
val fun_mult = "mult"; |
|
777 |
val fun_minus = "minus"; |
|
778 |
val fun_lt = "lt"; |
|
779 |
val fun_le = "le"; |
|
780 |
val fun_wfrec = "wfrec"; |
|
781 |
||
782 |
local |
|
783 |
||
784 |
val A = IVarT ("a", []); |
|
785 |
val B = IVarT ("b", []); |
|
786 |
val E = IVarT ("e", [class_eq]); |
|
787 |
||
788 |
in |
|
789 |
||
790 |
val Type_bool = type_bool `%% []; |
|
791 |
val Type_integer = type_integer `%% []; |
|
792 |
val Type_float = type_float `%% []; |
|
793 |
fun Type_pair a b = type_pair `%% [a, b]; |
|
794 |
fun Type_list a = type_list `%% [a]; |
|
795 |
val Cons_true = IConst (cons_true, Type_bool); |
|
796 |
val Cons_false = IConst (cons_false, Type_bool); |
|
797 |
val Cons_pair = IConst (cons_pair, A `-> B `-> Type_pair A B); |
|
798 |
val Cons_nil = IConst (cons_nil, Type_list A); |
|
799 |
val Cons_cons = IConst (cons_cons, A `-> Type_list A `-> Type_list A); |
|
800 |
val Fun_eq = IConst (fun_eq, E `-> E `-> Type_bool); |
|
801 |
val Fun_not = IConst (fun_not, Type_bool `-> Type_bool); |
|
802 |
val Fun_and = IConst (fun_and, Type_bool `-> Type_bool `-> Type_bool); |
|
803 |
val Fun_or = IConst (fun_or, Type_bool `-> Type_bool `-> Type_bool); |
|
804 |
val Fun_if = IConst (fun_if, Type_bool `-> A `-> A `-> A); |
|
805 |
val Fun_fst = IConst (fun_fst, Type_pair A B `-> A); |
|
806 |
val Fun_snd = IConst (fun_snd, Type_pair A B `-> B); |
|
807 |
val Fun_0 = IConst ("0", Type_integer); |
|
808 |
val Fun_1 = IConst ("1", Type_integer); |
|
809 |
val Fun_add = IConst (fun_add, Type_integer `-> Type_integer `-> Type_integer); |
|
810 |
val Fun_mult = IConst (fun_mult, Type_integer `-> Type_integer `-> Type_integer); |
|
811 |
val Fun_minus = IConst (fun_minus, Type_integer `-> Type_integer); |
|
812 |
val Fun_lt = IConst (fun_lt, Type_integer `-> Type_integer `-> Type_bool); |
|
813 |
val Fun_le = IConst (fun_le, Type_integer `-> Type_integer `-> Type_bool); |
|
814 |
val Fun_wfrec = IConst (fun_wfrec, ((A `-> B) `-> A `-> B) `-> A `-> B); |
|
815 |
||
816 |
infix 7 xx; |
|
817 |
infix 5 **; |
|
818 |
infix 5 &&; |
|
819 |
||
820 |
fun a xx b = Type_pair a b; |
|
821 |
fun a ** b = |
|
822 |
let |
|
823 |
val ty_a = itype_of_iexpr a; |
|
824 |
val ty_b = itype_of_iexpr b; |
|
825 |
in IConst (cons_pair, ty_a `-> ty_b `-> ty_a xx ty_b) `$ a `$ b end; |
|
826 |
fun a && b = |
|
827 |
let |
|
828 |
val ty_a = itype_of_ipat a; |
|
829 |
val ty_b = itype_of_ipat b; |
|
830 |
in ICons ((cons_pair, [a, b]), ty_a xx ty_b) end; |
|
831 |
||
832 |
end; (* local *) |
|
833 |
||
834 |
val prims = [class_eq, type_bool, type_integer, type_float, type_pair, type_list, |
|
835 |
cons_true, cons_false, cons_pair, cons_nil, cons_cons, fun_primeq, fun_eq, fun_not, fun_and, |
|
836 |
fun_or, fun_if, fun_fst, fun_snd, fun_add, fun_mult, fun_minus, fun_lt, fun_le, fun_wfrec]; |
|
837 |
||
838 |
fun extract_defs e = |
|
839 |
let |
|
840 |
fun extr_itype (ty as IType (tyco, _)) = |
|
841 |
cons tyco #> fold_itype extr_itype ty |
|
842 |
| extr_itype ty = |
|
843 |
fold_itype extr_itype ty |
|
844 |
fun extr_ipat (p as ICons ((c, _), _)) = |
|
845 |
cons c #> fold_ipat extr_itype extr_ipat p |
|
846 |
| extr_ipat p = |
|
847 |
fold_ipat extr_itype extr_ipat p |
|
848 |
fun extr_iexpr (e as IConst (f, _)) = |
|
849 |
cons f #> fold_iexpr extr_itype extr_ipat extr_iexpr e |
|
850 |
| extr_iexpr e = |
|
851 |
fold_iexpr extr_itype extr_ipat extr_iexpr e |
|
852 |
in extr_iexpr e [] end; |
|
853 |
||
854 |
||
855 |
||
856 |
(** generic transformation **) |
|
857 |
||
858 |
fun eta_expand query = |
|
859 |
let |
|
860 |
fun eta_app ((f, ty), es) = |
|
861 |
let |
|
862 |
val delta = query f - length es; |
|
863 |
val add_n = if delta < 0 then 0 else delta; |
|
864 |
val add_vars = |
|
865 |
invent_var_e_names es add_n [] "x" ~~ Library.drop (length es, (fst o unfold_fun) ty); |
|
866 |
in |
|
867 |
Library.foldr IAbs (add_vars, IConst (f, ty) `$$ es `$$ (map IVarE add_vars)) |
|
868 |
end; |
|
869 |
fun eta_iexpr' e = map_iexpr I I eta_iexpr e |
|
870 |
and eta_iexpr (IConst (f, ty)) = |
|
871 |
eta_app ((f, ty), []) |
|
872 |
| eta_iexpr (e as IApp _) = |
|
873 |
(case (unfold_app e) |
|
874 |
of (IConst (f, ty), es) => |
|
875 |
eta_app ((f, ty), map eta_iexpr es) |
|
876 |
| _ => eta_iexpr' e) |
|
877 |
| eta_iexpr e = eta_iexpr' e; |
|
878 |
in map_defs (map_def_fun I eta_iexpr) end; |
|
879 |
||
18216 | 880 |
val eta_expand_poly = |
881 |
let |
|
882 |
fun map_def_fun (def as Fun ([([], e)], cty as (sortctxt, (ty as IFun (ty1, ty2))))) = |
|
883 |
if (not o null) sortctxt |
|
884 |
orelse (null o vars_of_itype) ty |
|
885 |
then def |
|
886 |
else |
|
887 |
let |
|
888 |
val add_var = (hd (invent_var_e_names [e] 1 [] "x"), ty1) |
|
889 |
in (Fun ([([IVarP add_var], IAbs (add_var, e))], cty)) end |
|
890 |
| map_def_fun def = def; |
|
891 |
in map_defs map_def_fun end; |
|
892 |
||
18172 | 893 |
fun connect_datatypes_clsdecls module = |
894 |
let |
|
895 |
fun extract_dep (name, Datatypecons (dtname, _)) = |
|
896 |
[(dtname, name)] |
|
897 |
| extract_dep (name, Classmember (cls, _, _)) = |
|
898 |
[(cls, name)] |
|
899 |
| extract_dep (name, def) = [] |
|
900 |
in add_deps extract_dep module end; |
|
901 |
||
902 |
fun tupelize_cons module = |
|
903 |
let |
|
904 |
fun replace_def (_, (def as Datatypecons (_, []))) acc = |
|
905 |
(def, acc) |
|
906 |
| replace_def (_, (def as Datatypecons (_, [_]))) acc = |
|
907 |
(def, acc) |
|
908 |
| replace_def (name, (Datatypecons (tyco, tys))) acc = |
|
909 |
(Datatypecons (tyco, |
|
910 |
[foldl' (op xx) (NONE, tys)]), name::acc) |
|
911 |
| replace_def (_, def) acc = (def, acc); |
|
912 |
fun replace_app cs ((f, ty), es) = |
|
913 |
if member (op =) cs f |
|
914 |
then |
|
915 |
let |
|
916 |
val (tys, ty') = unfold_fun ty |
|
917 |
in IConst (f, foldr' (op xx) (tys, NONE) `-> ty') `$ foldl' (op **) (NONE, es) end |
|
918 |
else IConst (f, ty) `$$ es; |
|
919 |
fun replace_iexpr cs (IConst (f, ty)) = |
|
920 |
replace_app cs ((f, ty), []) |
|
921 |
| replace_iexpr cs (e as IApp _) = |
|
922 |
(case unfold_app e |
|
923 |
of (IConst fty, es) => replace_app cs (fty, map (replace_iexpr cs) es) |
|
924 |
| _ => map_iexpr I I (replace_iexpr cs) e) |
|
925 |
| replace_iexpr cs e = map_iexpr I I (replace_iexpr cs) e; |
|
926 |
fun replace_ipat cs (p as ICons ((c, ps), ty)) = |
|
927 |
if member (op =) cs c then |
|
928 |
ICons ((c, [(foldl' (op &&) (NONE, map (replace_ipat cs) ps))]), ty) |
|
929 |
else map_ipat I (replace_ipat cs) p |
|
930 |
| replace_ipat cs p = map_ipat I (replace_ipat cs) p; |
|
931 |
in |
|
932 |
transform_defs replace_def replace_ipat replace_iexpr [cons_cons] module |
|
933 |
end; |
|
934 |
||
935 |
fun eliminate_classes module = |
|
936 |
let |
|
937 |
fun mk_cls_typ_map memberdecls ty_inst = |
|
938 |
map (fn (memname, (v, ty)) => |
|
939 |
(memname, ty |> instant_itype (v, ty_inst))) memberdecls; |
|
940 |
fun transform_dicts (Class (supcls, members, insts)) = |
|
941 |
let |
|
942 |
val memberdecls = AList.make |
|
943 |
((fn Classmember (_, v, ty) => (v, ty)) o get_def module) members; |
|
944 |
val varname_cls = invent_var_t_names (map (snd o snd) memberdecls) 1 [] "a" |> hd; |
|
945 |
in |
|
946 |
Typesyn ([(varname_cls, [])], IDictT (mk_cls_typ_map memberdecls (IVarT (varname_cls, [])))) |
|
947 |
end |
|
948 |
| transform_dicts (Classinst (tyco, (cls, arity), memdefs)) = |
|
949 |
let |
|
950 |
val Class (_, members, _) = get_def module cls; |
|
951 |
val memberdecls = AList.make |
|
952 |
((fn Classmember (_, v, ty) => (v, ty)) o get_def module) members; |
|
953 |
val ty_arity = tyco `%% map IVarT (invent_var_t_names (map (snd o snd) memberdecls) |
|
954 |
(length arity) [] "a" ~~ arity); |
|
955 |
val inst_typ_map = mk_cls_typ_map memberdecls ty_arity; |
|
956 |
val memdefs_ty = map (fn (memname, memprim) => |
|
957 |
(memname, (memprim, (the o AList.lookup (op =) inst_typ_map) memname))) memdefs; |
|
958 |
in |
|
959 |
Fun ([([], IDictE (map (apsnd IConst) memdefs_ty))], |
|
960 |
([], IDictT inst_typ_map)) |
|
961 |
end |
|
962 |
| transform_dicts d = d |
|
963 |
fun transform_defs (Fun (ds, (sortctxt, ty))) = |
|
964 |
let |
|
965 |
fun reduce f xs = foldl' f (NONE, xs); |
|
966 |
val varnames_ctxt = |
|
967 |
sortctxt |
|
968 |
|> length o Library.flat o map snd |
|
969 |
|> (fn used => invent_var_e_names (map snd ds) used ((vars_of_ipats o fst o hd) ds) "d") |
|
970 |
|> unflat (map snd sortctxt); |
|
971 |
val vname_alist = map2 (fn ((vt, sort), vs) => (vt, vs ~~ sort)) (sortctxt, varnames_ctxt); |
|
972 |
fun add_typarms ty = |
|
973 |
map (reduce (op xx) o (fn (vt, vss) => map (fn (_, cls) => cls `%% [IVarT (vt, [])]) vss)) vname_alist |
|
974 |
`--> ty; |
|
975 |
fun add_parms ps = |
|
976 |
map (reduce (op &&) o (fn (vt, vss) => map (fn (v, cls) => IVarP (v, cls `%% [IVarT (vt, [])])) vss)) vname_alist |
|
977 |
@ ps; |
|
978 |
fun transform_itype (IVarT (v, s)) = |
|
979 |
IVarT (v, []) |
|
980 |
| transform_itype ty = |
|
981 |
map_itype transform_itype ty; |
|
982 |
fun transform_ipat p = |
|
983 |
map_ipat transform_itype transform_ipat p; |
|
984 |
fun transform_lookup (ClassPackage.Instance ((cdict, idict), ls)) = |
|
985 |
ls |
|
986 |
|> transform_lookups |
|
987 |
|-> (fn ty => |
|
988 |
curry mk_apps (IConst (idict, cdict `%% ty)) |
|
989 |
#> pair (cdict `%% ty)) |
|
990 |
| transform_lookup (ClassPackage.Lookup (deriv, (v, i))) = |
|
991 |
let |
|
992 |
val (v', cls) = |
|
993 |
(nth o the oo AList.lookup (op =)) vname_alist v i; |
|
994 |
fun mk_parm tyco = tyco `%% [IVarT (v, [])]; |
|
995 |
in (mk_parm (hd (deriv)), ILookup (rev deriv, v')) end |
|
996 |
and transform_lookups lss = |
|
997 |
map_yield (map_yield transform_lookup |
|
998 |
#> apfst (reduce (op xx)) |
|
999 |
#> apsnd (reduce (op **))) lss; |
|
1000 |
fun transform_iexpr (IInst (e, ls)) = |
|
1001 |
transform_iexpr e `$$ (snd o transform_lookups) ls |
|
1002 |
| transform_iexpr e = |
|
1003 |
map_iexpr transform_itype transform_ipat transform_iexpr e; |
|
1004 |
fun transform_rhs (ps, rhs) = (add_parms ps, transform_iexpr rhs) |
|
1005 |
in Fun (map transform_rhs ds, ([], add_typarms ty)) end |
|
1006 |
| transform_defs d = d |
|
1007 |
in |
|
1008 |
module |
|
1009 |
|> map_defs transform_dicts |
|
1010 |
|> map_defs transform_defs |
|
1011 |
end; |
|
1012 |
||
18216 | 1013 |
|
1014 |
(** generic serialization **) |
|
1015 |
||
1016 |
(* resolving *) |
|
1017 |
||
1018 |
fun mk_resolvtab nspgrp validate module = |
|
1019 |
let |
|
1020 |
fun ensure_unique prfix prfix' name name' (locals, tab) = |
|
1021 |
let |
|
1022 |
fun uniquify name n = |
|
1023 |
let |
|
1024 |
val name' = if n = 0 then name else name ^ "_" ^ string_of_int n |
|
1025 |
in |
|
1026 |
if member (op =) locals name' |
|
1027 |
then uniquify name (n+1) |
|
1028 |
else case validate name |
|
1029 |
of NONE => name' |
|
1030 |
| SOME name' => uniquify name' n |
|
1031 |
end; |
|
1032 |
val name'' = uniquify name' 0; |
|
1033 |
in |
|
1034 |
(locals, tab) |
|
1035 |
|> apsnd (Symtab.update_new |
|
1036 |
(NameSpace.pack (prfix @ [name]), NameSpace.pack (prfix' @ [name'']))) |
|
1037 |
|> apfst (cons name'') |
|
1038 |
|> pair name'' |
|
1039 |
end; |
|
1040 |
fun fill_in prfix prfix' node tab = |
|
1041 |
let |
|
1042 |
val keys = Graph.keys node; |
|
1043 |
val nodes = AList.make (Graph.get_node node) keys; |
|
1044 |
val (mods, defs) = |
|
1045 |
nodes |
|
1046 |
|> List.partition (fn (_, Module _) => true | _ => false) |
|
1047 |
|> apfst (map (fn (name, Module m) => (name, m))) |
|
1048 |
|> apsnd (map fst) |
|
1049 |
fun modl_validate (name, modl) (locals, tab) = |
|
1050 |
(locals, tab) |
|
1051 |
|> ensure_unique prfix prfix' name name |
|
1052 |
|-> (fn name' => apsnd (fill_in (prfix @ [name]) (prfix @ [name']) modl)) |
|
1053 |
fun ensure_unique_sidf sidf = |
|
1054 |
let |
|
1055 |
val [shallow, name] = NameSpace.unpack sidf; |
|
1056 |
in |
|
1057 |
nspgrp |
|
1058 |
|> get_first |
|
1059 |
(fn grp => if member (op =) grp shallow |
|
1060 |
then grp |> remove (op =) shallow |> SOME else NONE) |
|
1061 |
|> these |
|
1062 |
|> map (fn s => NameSpace.pack [s, name]) |
|
1063 |
|> exists (member (op =) defs) |
|
1064 |
|> (fn b => if b then sidf else name) |
|
1065 |
end; |
|
1066 |
fun def_validate sidf (locals, tab) = |
|
1067 |
(locals, tab) |
|
1068 |
|> ensure_unique prfix prfix' sidf (ensure_unique_sidf sidf) |
|
1069 |
|> snd |
|
1070 |
in |
|
1071 |
([], tab) |
|
1072 |
|> fold modl_validate mods |
|
1073 |
|> fold def_validate defs |
|
1074 |
|> snd |
|
1075 |
end; |
|
1076 |
in |
|
1077 |
Symtab.empty |
|
1078 |
|> fill_in [] [] module |
|
1079 |
end; |
|
1080 |
||
1081 |
fun mk_resolv tab = |
|
1082 |
let |
|
1083 |
fun resolver modl name = |
|
1084 |
if NameSpace.is_qualified name then |
|
1085 |
let |
|
1086 |
val modl' = if null modl then [] else (NameSpace.unpack o the o Symtab.lookup tab o NameSpace.pack) modl; |
|
1087 |
val name' = (NameSpace.unpack o the o Symtab.lookup tab) name |
|
1088 |
in |
|
1089 |
(NameSpace.pack o #3 o get_prefix (op =)) (modl', name') |
|
1090 |
end |
|
1091 |
else name |
|
1092 |
in resolver end; |
|
1093 |
||
1094 |
||
1095 |
(* serialization *) |
|
1096 |
||
1097 |
fun serialize s_def s_module validate nspgrp name_root module = |
|
1098 |
let |
|
1099 |
val resolvtab = mk_resolvtab nspgrp validate module; |
|
1100 |
val resolver = mk_resolv resolvtab; |
|
1101 |
fun seri prfx ([(name, Module module)]) = |
|
1102 |
s_module (name, |
|
1103 |
(map (seri (prfx @ [name])) |
|
1104 |
((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module))) |
|
1105 |
| seri prfx ds = |
|
1106 |
s_def (resolver prfx) (map (fn (name, Def def) => (name, def)) ds) |
|
1107 |
in |
|
1108 |
seri [] [(name_root, Module module)] |
|
1109 |
end; |
|
1110 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1111 |
end; (* struct *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1112 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1113 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1114 |
structure CodegenThingol : CODEGEN_THINGOL = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1115 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1116 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1117 |
open CodegenThingolOp; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1118 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1119 |
end; (* struct *) |