author | haftmann |
Mon, 14 Nov 2005 16:26:40 +0100 | |
changeset 18170 | 73ce773f12de |
parent 18169 | 45def66f86cb |
child 18172 | 8ff5bcfae27a |
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 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
14 |
| IVarT of vname * sort; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
15 |
datatype ipat = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
16 |
ICons of (string * ipat list) * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
17 |
| IVarP of vname * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
18 |
datatype iexpr = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
19 |
IConst of string * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
20 |
| IVarE of vname * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
21 |
| IApp of iexpr * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
22 |
| IInst of iexpr * ClassPackage.sortlookup list list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
23 |
| IAbs of (vname * itype) * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
24 |
| ICase of iexpr * (ipat * iexpr) list; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
25 |
val eq_itype: itype * itype -> bool |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
26 |
val eq_ipat: ipat * ipat -> bool |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
27 |
val eq_iexpr: iexpr * iexpr -> bool |
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; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
30 |
val pretty_itype: itype -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
31 |
val pretty_ipat: ipat -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
32 |
val pretty_iexpr: iexpr -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
33 |
val unfold_fun: itype -> itype list * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
34 |
val unfold_app: iexpr -> iexpr * iexpr list; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
35 |
val unfold_let: iexpr -> (ipat * iexpr) list * iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
36 |
val itype_of_iexpr: iexpr -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
37 |
val itype_of_ipat: ipat -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
38 |
val ipat_of_iexpr: iexpr -> ipat; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
39 |
val vars_of_ipats: ipat list -> vname list; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
40 |
val instant_itype: vname * itype -> itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
41 |
val invent_tvar_names: itype list -> int -> vname list -> vname -> vname list; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
42 |
val invent_evar_names: iexpr list -> int -> vname list -> vname -> vname list; |
18170 | 43 |
|
44 |
datatype def = |
|
45 |
Nop |
|
46 |
| Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype) |
|
47 |
| Typsyn of (vname * string list) list * itype |
|
48 |
| Datatyp of (vname * string list) list * string list * string list |
|
49 |
| Datatypcons of string * itype list |
|
50 |
| Class of string list * string list * string list |
|
51 |
| Classmember of string * vname * itype |
|
52 |
| Classinst of string * (string * string list list) * (string * string) list; |
|
53 |
type module; |
|
54 |
type transact; |
|
55 |
type 'dst transact_fin; |
|
56 |
type ('src, 'dst) gen_codegen = 'src -> transact -> 'dst transact_fin; |
|
57 |
type gen_defgen = string -> transact -> (def * string list) transact_fin; |
|
58 |
val eq_def: def * def -> bool; |
|
59 |
val pretty_def: def -> Pretty.T; |
|
60 |
val pretty_module: module -> Pretty.T; |
|
61 |
val empty_module: module; |
|
62 |
val get_def: module -> string -> def; |
|
63 |
val map_defs: (def -> def) -> module -> module; |
|
64 |
val add_deps: (string * def -> (string * string) list) -> module -> module; |
|
65 |
val fold_defs: (string * def -> 'a -> 'a) -> module -> 'a -> 'a; |
|
66 |
val fold_map_defs: (string * def -> 'a -> def * 'a) -> module -> 'a -> module * 'a; |
|
67 |
val merge_module: module * module -> module; |
|
68 |
val partof: string list -> module -> module; |
|
69 |
val succeed: 'a -> transact -> 'a transact_fin; |
|
70 |
val fail: string -> transact -> 'a transact_fin; |
|
71 |
val gen_invoke: (string * ('src, 'dst) gen_codegen) list -> string |
|
72 |
-> 'src -> transact -> 'dst * transact; |
|
73 |
val gen_ensure_def: (string * gen_defgen) list -> string |
|
74 |
-> string -> transact -> transact; |
|
75 |
||
76 |
val debug_level : int ref; |
|
77 |
val debug : int -> ('a -> string) -> 'a -> 'a; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
78 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
79 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
80 |
signature CODEGEN_THINGOL_OP = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
81 |
sig |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
82 |
include CODEGEN_THINGOL; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
83 |
val `%% : string * itype list -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
84 |
val `-> : itype * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
85 |
val `--> : itype list * itype -> itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
86 |
val `$ : iexpr * iexpr -> iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
87 |
val `$$ : iexpr * iexpr list -> iexpr; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
88 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
89 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
90 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
91 |
structure CodegenThingolOp: CODEGEN_THINGOL_OP = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
92 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
93 |
|
18170 | 94 |
(** auxiliary **) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
95 |
|
18170 | 96 |
val debug_level = ref 0; |
97 |
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
|
98 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
99 |
fun foldl' f (l, []) = the l |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
100 |
| foldl' f (_, (r::rs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
101 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
102 |
fun itl (l, []) = l |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
103 |
| itl (l, r::rs) = itl (f (l, r), rs) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
104 |
in itl (r, rs) end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
105 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
106 |
fun foldr' f ([], r) = the r |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
107 |
| foldr' f (ls, _) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
108 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
109 |
fun itr [l] = l |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
110 |
| itr (l::ls) = f (l, itr ls) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
111 |
in itr ls end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
112 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
113 |
fun unfoldl dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
114 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
115 |
of NONE => (x, []) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
116 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
117 |
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
|
118 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
119 |
fun unfoldr dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
120 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
121 |
of NONE => ([], x) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
122 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
123 |
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
|
124 |
|
18170 | 125 |
fun get_prefix eq ([], ys) = ([], [], ys) |
126 |
| get_prefix eq (xs, []) = ([], xs, []) |
|
127 |
| get_prefix eq (xs as x::xs', ys as y::ys') = |
|
128 |
if eq (x, y) then |
|
129 |
let val (ps', xs'', ys'') = get_prefix eq (xs', ys') |
|
130 |
in (x::ps', xs'', ys'') end |
|
131 |
else ([], xs, ys); |
|
132 |
||
133 |
||
134 |
(** language core - types, pattern, expressions **) |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
135 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
136 |
(* language representation *) |
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 |
infix 8 `%%; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
139 |
infixr 6 `->; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
140 |
infixr 6 `-->; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
141 |
infix 4 `$; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
142 |
infix 4 `$$; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
143 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
144 |
type vname = string; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
145 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
146 |
datatype itype = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
147 |
IType of string * itype list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
148 |
| IFun of itype * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
149 |
| IVarT of vname * sort; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
150 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
151 |
datatype ipat = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
152 |
ICons of (string * ipat list) * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
153 |
| IVarP of vname * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
154 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
155 |
datatype iexpr = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
156 |
IConst of string * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
157 |
| IVarE of vname * itype |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
158 |
| IApp of iexpr * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
159 |
| IInst of iexpr * ClassPackage.sortlookup list list |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
160 |
| IAbs of (vname * itype) * iexpr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
161 |
| ICase of iexpr * (ipat * iexpr) list; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
162 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
163 |
val eq_itype = (op =); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
164 |
val eq_ipat = (op =); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
165 |
val eq_iexpr = (op =); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
166 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
167 |
val mk_funs = Library.foldr IFun; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
168 |
val mk_apps = Library.foldl IApp; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
169 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
170 |
fun tyco `%% tys = IType (tyco, tys); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
171 |
val op `-> = IFun; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
172 |
fun f `$ x = IApp (f, x); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
173 |
val op `--> = mk_funs; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
174 |
val op `$$ = mk_apps; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
175 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
176 |
val unfold_fun = unfoldr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
177 |
(fn IFun t => SOME t |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
178 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
179 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
180 |
val unfold_app = unfoldl |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
181 |
(fn IApp e => SOME e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
182 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
183 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
184 |
val unfold_let = unfoldr |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
185 |
(fn ICase (e, [(p, e')]) => SOME ((p, e), e') |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
186 |
| _ => NONE); |
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 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
189 |
(* simple diagnosis *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
190 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
191 |
fun pretty_itype (IType (tyco, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
192 |
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
|
193 |
| pretty_itype (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
194 |
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
|
195 |
| pretty_itype (IVarT (v, sort)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
196 |
Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort)) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
197 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
198 |
fun pretty_ipat (ICons ((cons, ps), ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
199 |
Pretty.gen_list " " "(" ")" |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
200 |
(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
|
201 |
| pretty_ipat (IVarP (v, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
202 |
Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
203 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
204 |
fun pretty_iexpr (IConst (f, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
205 |
Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
206 |
| pretty_iexpr (IVarE (v, ty)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
207 |
Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
208 |
| pretty_iexpr (IApp (e1, e2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
209 |
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
|
210 |
| pretty_iexpr (IInst (e, c)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
211 |
pretty_iexpr e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
212 |
| pretty_iexpr (IAbs ((v, ty), e)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
213 |
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
|
214 |
| pretty_iexpr (ICase (e, cs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
215 |
Pretty.enclose "(" ")" [ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
216 |
Pretty.str "case ", |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
217 |
pretty_iexpr e, |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
218 |
Pretty.enclose "(" ")" (map (fn (p, e) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
219 |
Pretty.block [ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
220 |
pretty_ipat p, |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
221 |
Pretty.str " => ", |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
222 |
pretty_iexpr e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
223 |
] |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
224 |
) cs) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
225 |
] |
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 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
228 |
(* language auxiliary *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
229 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
230 |
fun itype_of_iexpr (IConst (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
231 |
| itype_of_iexpr (IVarE (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
232 |
| 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
|
233 |
of (IFun (ty2, ty')) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
234 |
if ty2 = itype_of_iexpr e2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
235 |
then ty' |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
236 |
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
|
237 |
^ ", " ^ (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
|
238 |
| _ => 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
|
239 |
| itype_of_iexpr (IInst (e, cs)) = error "" |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
240 |
| 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
|
241 |
| itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
242 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
243 |
fun itype_of_ipat (ICons (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
244 |
| itype_of_ipat (IVarP (_, ty)) = ty |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
245 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
246 |
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
|
247 |
| ipat_of_iexpr (IVarE v) = IVarP v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
248 |
| ipat_of_iexpr (e as IApp _) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
249 |
case unfold_app e of (IConst (f, ty), es) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
250 |
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
|
251 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
252 |
fun vars_of_ipats ps = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
253 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
254 |
fun vars (ICons ((_, ps), _)) = fold vars ps |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
255 |
| vars (IVarP (v, _)) = cons v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
256 |
in fold vars ps [] end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
257 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
258 |
fun instant_itype (v, sty) ty = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
259 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
260 |
fun instant (IType (tyco, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
261 |
tyco `%% map instant tys |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
262 |
| instant (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
263 |
instant ty1 `-> instant ty2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
264 |
| instant (w as (IVarT (u, _))) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
265 |
if v = u then sty else w |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
266 |
in instant ty end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
267 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
268 |
fun invent_tvar_names tys n used a = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
269 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
270 |
fun invent (IType (_, tys)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
271 |
fold invent tys |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
272 |
| invent (IFun (ty1, ty2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
273 |
invent ty1 #> invent ty2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
274 |
| invent (IVarT (v, _)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
275 |
cons v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
276 |
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
|
277 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
278 |
fun invent_evar_names es n used a = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
279 |
let |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
280 |
fun invent (IConst (f, _)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
281 |
I |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
282 |
| invent (IVarE (v, _)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
283 |
cons v |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
284 |
| invent (IApp (e1, e2)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
285 |
invent e1 #> invent e2 |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
286 |
| invent (IAbs ((v, _), e)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
287 |
cons v #> invent e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
288 |
| invent (ICase (e, cs)) = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
289 |
invent e |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
290 |
#> |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
291 |
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
|
292 |
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
|
293 |
|
18170 | 294 |
|
295 |
(** language module system - definitions, modules, transactions **) |
|
296 |
||
297 |
||
298 |
||
299 |
(* type definitions *) |
|
300 |
||
301 |
datatype def = |
|
302 |
Nop |
|
303 |
| Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype) |
|
304 |
| Typsyn of (vname * string list) list * itype |
|
305 |
| Datatyp of (vname * string list) list * string list * string list |
|
306 |
| Datatypcons of string * itype list |
|
307 |
| Class of string list * string list * string list |
|
308 |
| Classmember of string * string * itype |
|
309 |
| Classinst of string * (string * string list list) * (string * string) list; |
|
310 |
||
311 |
datatype node = Def of def | Module of node Graph.T; |
|
312 |
type module = node Graph.T; |
|
313 |
type transact = Graph.key list * module; |
|
314 |
datatype 'dst transact_res = Succeed of 'dst | Fail of string; |
|
315 |
type 'dst transact_fin = 'dst transact_res * transact; |
|
316 |
type ('src, 'dst) gen_codegen = 'src -> transact -> 'dst transact_fin; |
|
317 |
type gen_defgen = string -> transact -> (def * string list) transact_fin; |
|
318 |
exception FAIL of string; |
|
319 |
||
320 |
val eq_def = (op =); |
|
321 |
||
322 |
(* simple diagnosis *) |
|
323 |
||
324 |
fun pretty_def Nop = |
|
325 |
Pretty.str "<NOP>" |
|
326 |
| pretty_def (Fun (eqs, (_, ty))) = |
|
327 |
Pretty.gen_list " |" "" "" ( |
|
328 |
map (fn (ps, body) => |
|
329 |
Pretty.block [ |
|
330 |
Pretty.gen_list "," "[" "]" (map pretty_ipat ps), |
|
331 |
Pretty.str " |->", |
|
332 |
Pretty.brk 1, |
|
333 |
pretty_iexpr body, |
|
334 |
Pretty.str "::", |
|
335 |
pretty_itype ty |
|
336 |
]) eqs |
|
337 |
) |
|
338 |
| pretty_def (Typsyn (vs, ty)) = |
|
339 |
Pretty.block [ |
|
340 |
Pretty.list "(" ")" (map (pretty_itype o IVarT) vs), |
|
341 |
Pretty.str " |=> ", |
|
342 |
pretty_itype ty |
|
343 |
] |
|
344 |
| pretty_def (Datatyp (vs, cs, clss)) = |
|
345 |
Pretty.block [ |
|
346 |
Pretty.list "(" ")" (map (pretty_itype o IVarT) vs), |
|
347 |
Pretty.str " |=> ", |
|
348 |
Pretty.gen_list " |" "" "" (map Pretty.str cs), |
|
349 |
Pretty.str ", instance of ", |
|
350 |
Pretty.gen_list "," "[" "]" (map Pretty.str clss) |
|
351 |
] |
|
352 |
| pretty_def (Datatypcons (dtname, tys)) = |
|
353 |
Pretty.block [ |
|
354 |
Pretty.str "cons ", |
|
355 |
Pretty.gen_list " ->" "" "" (map pretty_itype tys @ [Pretty.str dtname]) |
|
356 |
] |
|
357 |
| pretty_def (Class (supcls, mems, insts)) = |
|
358 |
Pretty.str "Class ..." |
|
359 |
| pretty_def (Classmember (cls, v, ty)) = |
|
360 |
Pretty.str "Classmember ..." |
|
361 |
| pretty_def (Classinst (cls, insts, mems)) = |
|
362 |
Pretty.str "Classinst ..." |
|
363 |
||
364 |
fun pretty_module modl = |
|
365 |
let |
|
366 |
fun pretty (name, Module modl) = |
|
367 |
Pretty.block ( |
|
368 |
Pretty.str ("module " ^ name ^ " {") |
|
369 |
:: Pretty.brk 1 |
|
370 |
:: Pretty.chunks (map pretty (AList.make (Graph.get_node modl) |
|
371 |
(Graph.strong_conn modl |> List.concat |> rev))) |
|
372 |
:: Pretty.str "}" :: nil |
|
373 |
) |
|
374 |
| pretty (name, Def def) = |
|
375 |
Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def] |
|
376 |
in pretty ("//", Module modl) end; |
|
377 |
||
378 |
||
379 |
(* name handling *) |
|
380 |
||
381 |
fun dest_name name = |
|
382 |
let |
|
383 |
val name' = NameSpace.unpack name |
|
384 |
val (name'', name_base) = split_last name' |
|
385 |
val (modl, shallow) = split_last name'' |
|
386 |
in (modl, NameSpace.pack [shallow, name_base]) end |
|
387 |
handle Empty => error ("not a qualified name: " ^ quote name); |
|
388 |
||
389 |
fun dest_modl (Module m) = m; |
|
390 |
fun dest_def (Def d) = d; |
|
391 |
||
392 |
||
393 |
(* modules *) |
|
394 |
||
395 |
val empty_module = Graph.empty; (*read: "depends on"*) |
|
396 |
||
397 |
fun get_def modl name = |
|
398 |
case dest_name name |
|
399 |
of (modlname, base) => |
|
400 |
let |
|
401 |
fun get (Module node) [] = |
|
402 |
(dest_def o Graph.get_node node) base |
|
403 |
| get (Module node) (m::ms) = |
|
404 |
get (Graph.get_node node m) ms |
|
405 |
in get (Module modl) modlname end; |
|
406 |
||
407 |
fun add_def (name, def) = |
|
408 |
let |
|
409 |
val (modl, base) = dest_name name; |
|
410 |
fun add [] = |
|
411 |
Graph.new_node (base, Def def) |
|
412 |
| add (m::ms) = |
|
413 |
Graph.default_node (m, Module empty_module) |
|
414 |
#> Graph.map_node m (Module o add ms o dest_modl) |
|
415 |
in add modl end; |
|
416 |
||
417 |
fun map_def name f = |
|
418 |
let |
|
419 |
val (modl, base) = dest_name name; |
|
420 |
fun mapp [] = |
|
421 |
Graph.map_node base (Def o f o dest_def) |
|
422 |
| mapp (m::ms) = |
|
423 |
Graph.map_node m (Module o mapp ms o dest_modl) |
|
424 |
in mapp modl end; |
|
425 |
||
426 |
fun add_dep (name1, name2) modl = |
|
427 |
if name1 = name2 then modl |
|
428 |
else |
|
429 |
let |
|
430 |
val m1 = dest_name name1 |> apsnd single |> (op @); |
|
431 |
val m2 = dest_name name2 |> apsnd single |> (op @); |
|
432 |
val (ms, r1, r2) = get_prefix (op =) (m1, m2); |
|
433 |
val (ms, s1::r1, s2::r2) = get_prefix (op =) (m1, m2); |
|
434 |
val add_edge = |
|
435 |
if null r1 andalso null r2 |
|
436 |
then Graph.add_edge |
|
437 |
else Graph.add_edge_acyclic |
|
438 |
fun add [] node = |
|
439 |
node |
|
440 |
|> add_edge (s1, s2) |
|
441 |
| add (m::ms) node = |
|
442 |
node |
|
443 |
|> Graph.map_node m (Module o add ms o dest_modl); |
|
444 |
in add ms modl end; |
|
445 |
||
446 |
fun map_defs f = |
|
447 |
let |
|
448 |
fun mapp (Def def) = |
|
449 |
(Def o f) def |
|
450 |
| mapp (Module modl) = |
|
451 |
(Module o Graph.map_nodes mapp) modl |
|
452 |
in dest_modl o mapp o Module end; |
|
453 |
||
454 |
fun fold_defs f = |
|
455 |
let |
|
456 |
fun fol prfix (name, Def def) = |
|
457 |
f (NameSpace.pack (prfix @ [name]), def) |
|
458 |
| fol prfix (name, Module modl) = |
|
459 |
Graph.fold_nodes (fol (prfix @ [name])) modl |
|
460 |
in Graph.fold_nodes (fol []) end; |
|
461 |
||
462 |
fun add_deps f modl = |
|
463 |
modl |
|
464 |
|> fold add_dep ([] |> fold_defs (append o f) modl); |
|
465 |
||
466 |
fun fold_map_defs f = |
|
467 |
let |
|
468 |
fun foldmap prfix (name, Def def) = |
|
469 |
apfst Def o f (NameSpace.pack (prfix @ [name]), def) |
|
470 |
| foldmap prfix (name, Module modl) = |
|
471 |
apfst Module o Graph.fold_map_nodes (foldmap (prfix @ [name])) modl |
|
472 |
in Graph.fold_map_nodes (foldmap []) end; |
|
473 |
||
474 |
fun merge_module modl12 = |
|
475 |
let |
|
476 |
fun join_module (Module m1, Module m2) = |
|
477 |
(SOME o Module) (merge_module (m1, m2)) |
|
478 |
| join_module (Def d1, Def d2) = |
|
479 |
if eq_def (d1, d2) then (SOME o Def) d1 else NONE |
|
480 |
| join_module _ = |
|
481 |
NONE |
|
482 |
in Graph.join (K join_module) modl12 end; |
|
483 |
||
484 |
fun partof names modl = |
|
485 |
let |
|
486 |
datatype pathnode = PN of (string list * (string * pathnode) list); |
|
487 |
fun mk_path ([], base) (PN (defs, modls)) = |
|
488 |
PN (base :: defs, modls) |
|
489 |
| mk_path (n::ns, base) (PN (defs, modls)) = |
|
490 |
modls |
|
491 |
|> AList.default (op =) (n, PN ([], [])) |
|
492 |
|> AList.map_entry (op =) n (mk_path (ns, base)) |
|
493 |
|> (pair defs #> PN); |
|
494 |
fun select (PN (defs, modls)) (Module module) = |
|
495 |
module |
|
496 |
|> Graph.subgraph (Graph.all_succs module (defs @ map fst modls)) |
|
497 |
|> fold (fn (name, modls) => Graph.map_node name (select modls)) modls |
|
498 |
|> Module; |
|
499 |
in |
|
500 |
Module modl |
|
501 |
|> select (fold (mk_path o dest_name) (filter NameSpace.is_qualified names) (PN ([], []))) |
|
502 |
|> dest_modl |
|
503 |
end; |
|
504 |
||
505 |
fun add_check_transform (name, (Datatypcons (dtname, _))) = |
|
506 |
([([dtname], |
|
507 |
fn [Datatyp (_, _, [])] => NONE | _ => "attempted to add constructor to already instantiating datatype" |> SOME)], |
|
508 |
[(dtname, |
|
509 |
fn Datatyp (vs, cs, clss) => Datatyp (vs, name::cs, clss) |
|
510 |
| def => "attempted to add datatype constructor to non-datatype: " |
|
511 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
512 |
| add_check_transform (name, Classmember (clsname, v, ty)) = |
|
513 |
let |
|
514 |
fun check_var (IType (tyco, tys)) s = |
|
515 |
fold check_var tys s |
|
516 |
| check_var (IFun (ty1, ty2)) s = |
|
517 |
s |
|
518 |
|> check_var ty1 |
|
519 |
|> check_var ty2 |
|
520 |
| check_var (IVarT (w, sort)) s = |
|
521 |
if v = w |
|
522 |
andalso member (op =) sort clsname |
|
523 |
then "additional class appears at type variable" |> SOME |
|
524 |
else NONE |
|
525 |
in |
|
526 |
([([], fn [] => check_var ty NONE), |
|
527 |
([clsname], |
|
528 |
fn [Class (_, _, [])] => NONE |
|
529 |
| _ => "attempted to add class member to witnessed class" |> SOME)], |
|
530 |
[(clsname, |
|
531 |
fn Class (supcs, mems, insts) => Class (supcs, name::mems, insts) |
|
532 |
| def => "attempted to add class member to non-class" |
|
533 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
534 |
end |
|
535 |
| add_check_transform (name, Classinst (clsname, (tyco, arity), memdefs)) = |
|
536 |
let |
|
537 |
fun check [Classmember (_, v, mtyp_c), Fun (_, (_, mtyp_i))] = |
|
538 |
let |
|
539 |
val mtyp_i' = instant_itype (v, tyco `%% |
|
540 |
map2 IVarT ((invent_tvar_names [mtyp_c] (length arity) [] "a"), arity)) mtyp_c; |
|
541 |
in if eq_itype (mtyp_i', mtyp_i) (*! PERHAPS TOO STRICT !*) |
|
542 |
then NONE |
|
543 |
else "wrong type signature for class member: " |
|
544 |
^ (Pretty.output o pretty_itype) mtyp_i' ^ " expected," |
|
545 |
^ (Pretty.output o pretty_itype) mtyp_i ^ " given" |> SOME end |
|
546 |
in |
|
547 |
(map (fn (memname, memprim) => ((writeln memname; writeln memprim; [memname, memprim]), check)) memdefs, |
|
548 |
[(clsname, |
|
549 |
fn Class (supcs, mems, insts) => Class (supcs, mems, name::insts) |
|
550 |
| def => "attempted to add class instance to non-class" |
|
551 |
^ (Pretty.output o pretty_def) def |> error), |
|
552 |
(tyco, |
|
553 |
fn Datatyp (vs, cs, clss) => Datatyp (vs, cs, clsname::clss) |
|
554 |
| Nop => Nop |
|
555 |
| def => "attempted to instantiate non-type to class instance" |
|
556 |
^ (Pretty.output o pretty_def) def |> error)]) |
|
557 |
end |
|
558 |
| add_check_transform _ = ([], []); |
|
559 |
||
560 |
fun succeed some = (pair o Succeed) some; |
|
561 |
fun fail msg = (pair o Fail) msg; |
|
562 |
||
563 |
fun check_fail msg' (Succeed dst, trns) = (dst, trns) |
|
564 |
| check_fail msg' (Fail errmsg, _) = (tracing ("ROLLBACK CHECK: " ^ errmsg ^ "\n" ^ msg'); raise FAIL errmsg); |
|
565 |
||
566 |
fun handle_fail msg f modl = |
|
567 |
f modl handle FAIL msg' => ([], modl) |> fail (msg ^ "\n" ^ msg'); |
|
568 |
||
569 |
fun select_generator print_msg src [] trns = |
|
570 |
fail ("no code generator available") trns |
|
571 |
| select_generator print_msg src [(gname, cgen)] trns = |
|
572 |
(print_msg gname; cgen src trns) |
|
573 |
| select_generator print_msg src ((gname, cgen)::cgens) trns = |
|
574 |
case cgen src trns |
|
575 |
of result as (Succeed _, _) => |
|
576 |
(print_msg gname; result) |
|
577 |
| _ => select_generator print_msg src cgens trns |
|
578 |
||
579 |
fun gen_invoke codegens msg src (deps, modl) = |
|
580 |
([], modl) |
|
581 |
|> select_generator (fn gname => "choosing code generator " ^ gname ^ " for source " ^ quote msg) |
|
582 |
src codegens |
|
583 |
|> check_fail msg |
|
584 |
||> (fn (deps', modl') => (append deps' deps, modl')); |
|
585 |
||
586 |
fun gen_ensure_def defgens msg name (deps, modl) = |
|
587 |
let |
|
588 |
fun add (name, def) (deps, modl) = |
|
589 |
let |
|
590 |
val (checks, trans) = add_check_transform (name, def); |
|
591 |
fun check (check_defs, checker) modl = |
|
592 |
case checker (check_defs |> filter NameSpace.is_qualified |> map (get_def modl)) |
|
593 |
of NONE => modl |
|
594 |
| SOME e => raise FAIL e; |
|
595 |
fun transform (name, f) modl = |
|
596 |
modl |
|
597 |
|> K (NameSpace.is_qualified name) ? map_def name f; |
|
598 |
in |
|
599 |
modl |
|
600 |
|> fold check checks |
|
601 |
|> fold (curry add_dep name) deps |
|
602 |
|> map_def name (fn _ => def) |
|
603 |
|> fold transform trans |
|
604 |
end; |
|
605 |
fun ensure_node name modl = |
|
606 |
if can (get_def modl) name |
|
607 |
then ([name], modl) |
|
608 |
else |
|
609 |
([], modl |> add_def (name, Nop)) |
|
610 |
|> select_generator (fn gname => "choosing code generator " ^ gname ^ " for definition of " ^ quote name) |
|
611 |
name defgens |
|
612 |
|> check_fail msg |
|
613 |
|-> (fn (def, names') => |
|
614 |
add (name, def) |
|
615 |
#> fold_map ensure_node names') |
|
616 |
|-> (fn names' => pair (name :: Library.flat names')) |
|
617 |
in |
|
618 |
modl |
|
619 |
|> ensure_node name |
|
620 |
|-> (fn names => pair (names@deps)) |
|
621 |
end; |
|
622 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
623 |
end; (* struct *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
624 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
625 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
626 |
structure CodegenThingol : CODEGEN_THINGOL = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
627 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
628 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
629 |
open CodegenThingolOp; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
630 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
631 |
end; (* struct *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
632 |