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