author | haftmann |
Tue, 06 Jun 2006 14:55:19 +0200 | |
changeset 19785 | 52d71ee5c8a8 |
parent 19616 | 2545e8ab59a5 |
child 19816 | a8c8ed1c85e0 |
permissions | -rw-r--r-- |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_thingol.ML |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
5 |
Intermediate language ("Thin-gol") for code extraction. |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
6 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
8 |
infix 8 `%%; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
9 |
infixr 6 `->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
10 |
infixr 6 `-->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
11 |
infix 4 `$; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
12 |
infix 4 `$$; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
13 |
infixr 3 `|->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
14 |
infixr 3 `|-->; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
15 |
|
19136 | 16 |
signature BASIC_CODEGEN_THINGOL = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
17 |
sig |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
18 |
type vname = string; |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
19 |
type sortcontext = ClassPackage.sortcontext; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
20 |
datatype iclasslookup = Instance of string * iclasslookup list list |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
21 |
| Lookup of class list * (string * int); |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
22 |
datatype itype = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
23 |
`%% of string * itype list |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
24 |
| `-> of itype * itype |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
25 |
| ITyVar of vname; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
26 |
datatype iexpr = |
19202 | 27 |
IConst of string * (iclasslookup list list * itype) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
28 |
| IVar of vname |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
29 |
| `$ of iexpr * iexpr |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
30 |
| `|-> of (vname * itype) * iexpr |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
31 |
| INum of IntInf.int (*positive!*) * unit |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
32 |
| IChar of string (*length one!*) * iexpr |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
33 |
| IAbs of ((iexpr * itype) * iexpr) * iexpr |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
34 |
(* (((binding expression (ve), binding type (vty)), |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
35 |
body expression (be)), native expression (e0)) *) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
36 |
| ICase of ((iexpr * itype) * (iexpr * iexpr) list) * iexpr; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
37 |
(* ((discrimendum expression (de), discrimendum type (dty)), |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
38 |
[(selector expression (se), body expression (be))]), |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
39 |
native expression (e0)) *) |
19136 | 40 |
end; |
41 |
||
42 |
signature CODEGEN_THINGOL = |
|
43 |
sig |
|
44 |
include BASIC_CODEGEN_THINGOL; |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
45 |
val `--> : itype list * itype -> itype; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
46 |
val `$$ : iexpr * iexpr list -> iexpr; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
47 |
val `|--> : (vname * itype) list * iexpr -> iexpr; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
48 |
val pretty_itype: itype -> Pretty.T; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
49 |
val pretty_iexpr: iexpr -> Pretty.T; |
18216 | 50 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list; |
51 |
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
|
52 |
val unfold_fun: itype -> itype list * itype; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
53 |
val unfold_app: iexpr -> iexpr * iexpr list; |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
54 |
val unfold_abs: iexpr -> (iexpr * itype) list * iexpr; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
55 |
val unfold_let: iexpr -> ((iexpr * itype) * iexpr) list * iexpr; |
18865 | 56 |
val unfold_const_app: iexpr -> |
19202 | 57 |
((string * (iclasslookup list list * itype)) * iexpr list) option; |
58 |
val add_constnames: iexpr -> string list -> string list; |
|
59 |
val add_varnames: iexpr -> string list -> string list; |
|
60 |
val is_pat: iexpr -> bool; |
|
61 |
val map_pure: (iexpr -> 'a) -> iexpr -> 'a; |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
62 |
val resolve_tycos: (string -> string) -> itype * iexpr list -> itype * iexpr list; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
63 |
val resolve_consts: (string -> string) -> iexpr -> iexpr; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
64 |
val eta_expand: (string * (iclasslookup list list * itype)) * iexpr list -> int -> iexpr; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
65 |
|
18170 | 66 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
67 |
type funn = (iexpr list * iexpr) list * (sortcontext * itype); |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
68 |
type datatyp = sortcontext * (string * itype list) list; |
18850 | 69 |
datatype prim = |
70 |
Pretty of Pretty.T |
|
18963 | 71 |
| Name; |
18170 | 72 |
datatype def = |
18702 | 73 |
Undef |
18963 | 74 |
| Prim of (string * prim list) list |
18702 | 75 |
| Fun of funn |
19038 | 76 |
| Typesyn of (vname * sort) list * itype |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
77 |
| Datatype of datatyp |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
78 |
| Datatypecons of string |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
79 |
| Class of class list * (vname * (string * (sortcontext * itype)) list) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
80 |
| Classmember of class |
18865 | 81 |
| Classinst of ((class * (string * (vname * sort) list)) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
82 |
* (class * (string * iclasslookup list list)) list) |
19253 | 83 |
* (string * ((string * funn) * iclasslookup list list)) list |
19213 | 84 |
| Classinstmember; |
18170 | 85 |
type module; |
86 |
type transact; |
|
87 |
type 'dst transact_fin; |
|
88 |
val pretty_def: def -> Pretty.T; |
|
18282 | 89 |
val pretty_module: module -> Pretty.T; |
18360 | 90 |
val pretty_deps: module -> Pretty.T; |
18170 | 91 |
val empty_module: module; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
92 |
val get_def: module -> string -> def; |
18963 | 93 |
val add_prim: string -> (string * prim list) -> module -> module; |
18702 | 94 |
val ensure_prim: string -> string -> module -> module; |
18170 | 95 |
val merge_module: module * module -> module; |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
96 |
val diff_module: module * module -> (string * def) list; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
97 |
val project_module: string list -> module -> module; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
98 |
val purge_module: string list -> module -> module; |
18516 | 99 |
val has_nsp: string -> string -> bool; |
18170 | 100 |
val succeed: 'a -> transact -> 'a transact_fin; |
101 |
val fail: string -> transact -> 'a transact_fin; |
|
19136 | 102 |
val ensure_def: (string * (string -> transact -> def transact_fin)) list -> string |
18170 | 103 |
-> string -> transact -> transact; |
18963 | 104 |
val start_transact: string option -> (transact -> 'a * transact) -> module -> 'a * module; |
18216 | 105 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
106 |
val debug: bool ref; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
107 |
val debug_msg: ('a -> string) -> 'a -> 'a; |
18231 | 108 |
val soft_exc: bool ref; |
18216 | 109 |
|
110 |
val serialize: |
|
19038 | 111 |
((string -> string -> string) -> string -> (string * def) list -> 'a option) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
112 |
-> ((string -> string) -> string list -> (string * string) * 'a list -> 'a option) |
18216 | 113 |
-> (string -> string option) |
18919 | 114 |
-> (string * string -> string) |
18850 | 115 |
-> string list list -> string -> module -> 'a option; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
116 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
117 |
|
18850 | 118 |
structure CodegenThingol: CODEGEN_THINGOL = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
119 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
120 |
|
18170 | 121 |
(** auxiliary **) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
122 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
123 |
val debug = ref false; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
124 |
fun debug_msg f x = (if !debug then Output.debug (f x) else (); x); |
18231 | 125 |
val soft_exc = ref true; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
126 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
127 |
fun unfoldl dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
128 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
129 |
of NONE => (x, []) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
130 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
131 |
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
|
132 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
133 |
fun unfoldr dest x = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
134 |
case dest x |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
135 |
of NONE => ([], x) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
136 |
| SOME (x1, x2) => |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
137 |
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
|
138 |
|
18172 | 139 |
fun map_yield f [] = ([], []) |
140 |
| map_yield f (x::xs) = |
|
141 |
let |
|
142 |
val (y, x') = f x |
|
143 |
val (ys, xs') = map_yield f xs |
|
144 |
in (y::ys, x'::xs') end; |
|
145 |
||
18454 | 146 |
fun get_prefix eq ([], ys) = ([], ([], ys)) |
147 |
| get_prefix eq (xs, []) = ([], (xs, [])) |
|
18170 | 148 |
| get_prefix eq (xs as x::xs', ys as y::ys') = |
149 |
if eq (x, y) then |
|
18454 | 150 |
let val (ps', xys'') = get_prefix eq (xs', ys') |
151 |
in (x::ps', xys'') end |
|
152 |
else ([], (xs, ys)); |
|
18170 | 153 |
|
154 |
||
155 |
(** language core - types, pattern, expressions **) |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
156 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
157 |
(* language representation *) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
158 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
159 |
type vname = string; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
160 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
161 |
type sortcontext = ClassPackage.sortcontext; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
162 |
datatype iclasslookup = Instance of string * iclasslookup list list |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
163 |
| Lookup of class list * (string * int); |
18885 | 164 |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
165 |
datatype itype = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
166 |
`%% of string * itype list |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
167 |
| `-> of itype * itype |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
168 |
| ITyVar of vname; |
18169
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 |
datatype iexpr = |
19202 | 171 |
IConst of string * (iclasslookup list list * itype) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
172 |
| IVar of vname |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
173 |
| `$ of iexpr * iexpr |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
174 |
| `|-> of (vname * itype) * iexpr |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
175 |
| INum of IntInf.int (*positive!*) * unit |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
176 |
| IChar of string (*length one!*) * iexpr |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
177 |
| IAbs of ((iexpr * itype) * iexpr) * iexpr |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
178 |
| ICase of ((iexpr * itype) * (iexpr * iexpr) list) * iexpr; |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
179 |
(*see also signature*) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
180 |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
181 |
(* |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
182 |
variable naming conventions |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
183 |
|
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
184 |
bare names: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
185 |
variable names v |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
186 |
class names cls |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
187 |
type constructor names tyco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
188 |
datatype names dtco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
189 |
const names (general) c |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
190 |
constructor names co |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
191 |
class member names m |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
192 |
arbitrary name s |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
193 |
|
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
194 |
constructs: |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
195 |
sort sort |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
196 |
type ty |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
197 |
expression e |
18702 | 198 |
pattern p, pat |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
199 |
instance (cls, tyco) inst |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
200 |
variable (v, ty) var |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
201 |
class member (m, ty) membr |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
202 |
constructors (co, tys) constr |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
203 |
*) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
204 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
205 |
val op `--> = Library.foldr (op `->); |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
206 |
val op `$$ = Library.foldl (op `$); |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
207 |
val op `|--> = Library.foldr (op `|->); |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
208 |
|
19150 | 209 |
val pretty_sortcontext = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
210 |
Pretty.list "(" ")" o Pretty.commas o map (fn (v, sort) => (Pretty.block o Pretty.breaks) |
19150 | 211 |
[Pretty.str v, Pretty.str "::", Pretty.enum "&" "" "" (map Pretty.str sort)]); |
212 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
213 |
fun pretty_itype (tyco `%% tys) = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
214 |
Pretty.enum "" "(" ")" (Pretty.str tyco :: map pretty_itype tys) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
215 |
| pretty_itype (ty1 `-> ty2) = |
18885 | 216 |
Pretty.enum "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2] |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
217 |
| pretty_itype (ITyVar v) = |
19150 | 218 |
Pretty.str v; |
18885 | 219 |
|
19202 | 220 |
fun pretty_iexpr (IConst (c, _)) = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
221 |
Pretty.str c |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
222 |
| pretty_iexpr (IVar v) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
223 |
Pretty.str ("?" ^ v) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
224 |
| pretty_iexpr (e1 `$ e2) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
225 |
(Pretty.enclose "(" ")" o Pretty.breaks) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
226 |
[pretty_iexpr e1, pretty_iexpr e2] |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
227 |
| pretty_iexpr ((v, ty) `|-> e) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
228 |
(Pretty.enclose "(" ")" o Pretty.breaks) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
229 |
[Pretty.str v, Pretty.str "::", pretty_itype ty, Pretty.str "|->", pretty_iexpr e] |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
230 |
| pretty_iexpr (INum (n, _)) = |
19202 | 231 |
(Pretty.str o IntInf.toString) n |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
232 |
| pretty_iexpr (IChar (c, _)) = |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
233 |
(Pretty.str o quote) c |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
234 |
| pretty_iexpr (IAbs (((e1, _), e2), _)) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
235 |
(Pretty.enclose "(" ")" o Pretty.breaks) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
236 |
[pretty_iexpr e1, Pretty.str "|->", pretty_iexpr e2] |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
237 |
| pretty_iexpr (ICase (((e, _), cs), _)) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
238 |
(Pretty.enclose "(" ")" o Pretty.breaks) [ |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
239 |
Pretty.str "case", |
18885 | 240 |
pretty_iexpr e, |
241 |
Pretty.enclose "(" ")" (map (fn (p, e) => |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
242 |
(Pretty.block o Pretty.breaks) [ |
18885 | 243 |
pretty_iexpr p, |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
244 |
Pretty.str "=>", |
18885 | 245 |
pretty_iexpr e |
246 |
] |
|
247 |
) cs) |
|
248 |
]; |
|
249 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
250 |
val unfold_fun = unfoldr |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
251 |
(fn op `-> t => SOME t |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
252 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
253 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
254 |
val unfold_app = unfoldl |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
255 |
(fn op `$ e => SOME e |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
256 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
257 |
|
18282 | 258 |
val unfold_abs = unfoldr |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
259 |
(fn (v, ty) `|-> e => SOME ((IVar v, ty), e) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
260 |
| IAbs (((e1, ty), e2), _) => SOME ((e1, ty), e2) |
18282 | 261 |
| _ => NONE) |
262 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
263 |
val unfold_let = unfoldr |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
264 |
(fn ICase (((de, dty), [(se, be)]), _) => SOME (((se, dty), de), be) |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
265 |
| _ => NONE); |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
266 |
|
18865 | 267 |
fun unfold_const_app e = |
268 |
case unfold_app e |
|
269 |
of (IConst x, es) => SOME (x, es) |
|
270 |
| _ => NONE; |
|
271 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
272 |
fun map_itype _ (ty as ITyVar _) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
273 |
ty |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
274 |
| map_itype f (tyco `%% tys) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
275 |
tyco `%% map f tys |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
276 |
| map_itype f (t1 `-> t2) = |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
277 |
f t1 `-> f t2; |
18172 | 278 |
|
19150 | 279 |
fun eq_ityp ((sctxt1, ty1), (sctxt2, ty2)) = |
18282 | 280 |
let |
281 |
exception NO_MATCH; |
|
19150 | 282 |
fun eq_sctxt subs sctxt1 sctxt2 = |
19597 | 283 |
map (fn (v : string, sort : string list) => case AList.lookup (op =) subs v |
19150 | 284 |
of NONE => raise NO_MATCH |
19597 | 285 |
| SOME (v' : string) => case AList.lookup (op =) sctxt2 v' |
19150 | 286 |
of NONE => raise NO_MATCH |
287 |
| SOME sort' => if sort <> sort' then raise NO_MATCH else ()) sctxt1 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
288 |
fun eq (ITyVar v1) (ITyVar v2) subs = |
19150 | 289 |
(case AList.lookup (op =) subs v1 |
290 |
of NONE => subs |> AList.update (op =) (v1, v2) |
|
291 |
| SOME v1' => |
|
292 |
if v1' <> v2 |
|
293 |
then raise NO_MATCH |
|
294 |
else subs) |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
295 |
| eq (tyco1 `%% tys1) (tyco2 `%% tys2) subs = |
18282 | 296 |
if tyco1 <> tyco2 |
297 |
then raise NO_MATCH |
|
298 |
else subs |> fold2 eq tys1 tys2 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
299 |
| eq (ty11 `-> ty12) (ty21 `-> ty22) subs = |
18282 | 300 |
subs |> eq ty11 ty21 |> eq ty12 ty22 |
301 |
| eq _ _ _ = raise NO_MATCH; |
|
302 |
in |
|
303 |
(eq ty1 ty2 []; true) |
|
304 |
handle NO_MATCH => false |
|
305 |
end; |
|
306 |
||
18885 | 307 |
fun instant_itype f = |
308 |
let |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
309 |
fun instant (ITyVar x) = f x |
18885 | 310 |
| instant y = map_itype instant y; |
19215 | 311 |
in instant end; |
18885 | 312 |
|
19202 | 313 |
fun is_pat (e as IConst (_, ([], _))) = true |
314 |
| is_pat (e as IVar _) = true |
|
315 |
| is_pat (e as (e1 `$ e2)) = |
|
316 |
is_pat e1 andalso is_pat e2 |
|
317 |
| is_pat (e as INum _) = true |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
318 |
| is_pat (e as IChar _) = true |
19202 | 319 |
| is_pat e = false; |
320 |
||
321 |
fun map_pure f (e as IConst _) = |
|
322 |
f e |
|
323 |
| map_pure f (e as IVar _) = |
|
324 |
f e |
|
325 |
| map_pure f (e as _ `$ _) = |
|
326 |
f e |
|
327 |
| map_pure f (e as _ `|-> _) = |
|
328 |
f e |
|
329 |
| map_pure _ (INum _) = |
|
330 |
error ("sorry, no pure representation of numerals so far") |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
331 |
| map_pure f (IChar (_, e0)) = |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
332 |
f e0 |
19202 | 333 |
| map_pure f (IAbs (_, e0)) = |
334 |
f e0 |
|
335 |
| map_pure f (ICase (_, e0)) = |
|
336 |
f e0; |
|
18912 | 337 |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
338 |
fun resolve_tycos _ = error ""; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
339 |
fun resolve_consts _ = error ""; |
18216 | 340 |
|
19202 | 341 |
fun add_constnames (IConst (c, _)) = |
342 |
insert (op =) c |
|
343 |
| add_constnames (IVar _) = |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
344 |
I |
19202 | 345 |
| add_constnames (e1 `$ e2) = |
346 |
add_constnames e1 #> add_constnames e2 |
|
347 |
| add_constnames (_ `|-> e) = |
|
348 |
add_constnames e |
|
349 |
| add_constnames (INum _) = |
|
350 |
I |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
351 |
| add_constnames (IChar _) = |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
352 |
I |
19202 | 353 |
| add_constnames (IAbs (_, e)) = |
354 |
add_constnames e |
|
355 |
| add_constnames (ICase (_, e)) = |
|
356 |
add_constnames e; |
|
357 |
||
358 |
fun add_varnames (IConst _) = |
|
359 |
I |
|
360 |
| add_varnames (IVar v) = |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
361 |
insert (op =) v |
19202 | 362 |
| add_varnames (e1 `$ e2) = |
363 |
add_varnames e1 #> add_varnames e2 |
|
364 |
| add_varnames ((v, _) `|-> e) = |
|
365 |
insert (op =) v #> add_varnames e |
|
366 |
| add_varnames (INum _) = |
|
367 |
I |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
368 |
| add_varnames (IChar _) = |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
369 |
I |
19202 | 370 |
| add_varnames (IAbs (((ve, _), be), _)) = |
371 |
add_varnames ve #> add_varnames be |
|
372 |
| add_varnames (ICase (((de, _), bses), _)) = |
|
373 |
add_varnames de #> fold (fn (be, se) => add_varnames be #> add_varnames se) bses; |
|
18885 | 374 |
|
375 |
fun invent seed used = |
|
376 |
let |
|
377 |
val x = Term.variant used seed |
|
378 |
in (x, x :: used) end; |
|
379 |
||
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
380 |
fun eta_expand (c as (_, (_, ty)), es) k = |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
381 |
let |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
382 |
val j = length es; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
383 |
val l = k - j; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
384 |
val tys = (curry Library.take l o curry Library.drop j o fst o unfold_fun) ty; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
385 |
val vs = [] |> fold add_varnames es |> fold_map invent (replicate l "x") |> fst; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
386 |
in vs ~~ tys `|--> IConst c `$$ es @ map IVar vs end; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
387 |
|
18304 | 388 |
|
18282 | 389 |
|
18170 | 390 |
(** language module system - definitions, modules, transactions **) |
391 |
||
392 |
(* type definitions *) |
|
393 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
394 |
type funn = (iexpr list * iexpr) list * (sortcontext * itype); |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
395 |
type datatyp = sortcontext * (string * itype list) list; |
18702 | 396 |
|
18850 | 397 |
datatype prim = |
398 |
Pretty of Pretty.T |
|
18963 | 399 |
| Name; |
18850 | 400 |
|
18170 | 401 |
datatype def = |
18702 | 402 |
Undef |
18963 | 403 |
| Prim of (string * prim list) list |
18702 | 404 |
| Fun of funn |
19038 | 405 |
| Typesyn of (vname * sort) list * itype |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
406 |
| Datatype of datatyp |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
407 |
| Datatypecons of string |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
408 |
| Class of class list * (vname * (string * (sortcontext * itype)) list) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
409 |
| Classmember of class |
18865 | 410 |
| Classinst of ((class * (string * (vname * sort) list)) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
411 |
* (class * (string * iclasslookup list list)) list) |
19253 | 412 |
* (string * ((string * funn) * iclasslookup list list)) list |
19213 | 413 |
| Classinstmember; |
18170 | 414 |
|
415 |
datatype node = Def of def | Module of node Graph.T; |
|
416 |
type module = node Graph.T; |
|
18702 | 417 |
type transact = Graph.key option * module; |
18231 | 418 |
datatype 'dst transact_res = Succeed of 'dst | Fail of string list * exn option; |
18702 | 419 |
type 'dst transact_fin = 'dst transact_res * module; |
18231 | 420 |
exception FAIL of string list * exn option; |
18170 | 421 |
|
19597 | 422 |
val eq_def = (op =) : def * def -> bool; |
18170 | 423 |
|
424 |
(* simple diagnosis *) |
|
425 |
||
18702 | 426 |
fun pretty_def Undef = |
427 |
Pretty.str "<UNDEF>" |
|
428 |
| pretty_def (Prim prims) = |
|
429 |
Pretty.str ("<PRIM " ^ (commas o map fst) prims ^ ">") |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
430 |
| pretty_def (Fun (eqs, (sortctxt, ty))) = |
18812 | 431 |
Pretty.enum " |" "" "" ( |
18170 | 432 |
map (fn (ps, body) => |
433 |
Pretty.block [ |
|
18865 | 434 |
Pretty.enum "," "[" "]" (map pretty_iexpr ps), |
18170 | 435 |
Pretty.str " |->", |
436 |
Pretty.brk 1, |
|
437 |
pretty_iexpr body, |
|
438 |
Pretty.str "::", |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
439 |
pretty_sortcontext sortctxt, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
440 |
Pretty.str "/", |
18170 | 441 |
pretty_itype ty |
442 |
]) eqs |
|
443 |
) |
|
18172 | 444 |
| pretty_def (Typesyn (vs, ty)) = |
18170 | 445 |
Pretty.block [ |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
446 |
pretty_sortcontext vs, |
18170 | 447 |
Pretty.str " |=> ", |
448 |
pretty_itype ty |
|
449 |
] |
|
19038 | 450 |
| pretty_def (Datatype (vs, cs)) = |
18170 | 451 |
Pretty.block [ |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
452 |
pretty_sortcontext vs, |
18170 | 453 |
Pretty.str " |=> ", |
18852 | 454 |
Pretty.enum " |" "" "" |
18850 | 455 |
(map (fn (c, tys) => (Pretty.block o Pretty.breaks) |
19038 | 456 |
(Pretty.str c :: map pretty_itype tys)) cs) |
18170 | 457 |
] |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
458 |
| pretty_def (Datatypecons dtname) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
459 |
Pretty.str ("cons " ^ dtname) |
19038 | 460 |
| pretty_def (Class (supcls, (v, mems))) = |
18170 | 461 |
Pretty.block [ |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
462 |
Pretty.str ("class var " ^ v ^ "extending "), |
18812 | 463 |
Pretty.enum "," "[" "]" (map Pretty.str supcls), |
18282 | 464 |
Pretty.str " with ", |
18852 | 465 |
Pretty.enum "," "[" "]" |
18850 | 466 |
(map (fn (m, (_, ty)) => Pretty.block |
19038 | 467 |
[Pretty.str (m ^ "::"), pretty_itype ty]) mems) |
18231 | 468 |
] |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
469 |
| pretty_def (Classmember clsname) = |
18231 | 470 |
Pretty.block [ |
471 |
Pretty.str "class member belonging to ", |
|
18282 | 472 |
Pretty.str clsname |
18231 | 473 |
] |
18865 | 474 |
| pretty_def (Classinst (((clsname, (tyco, arity)), _), _)) = |
18231 | 475 |
Pretty.block [ |
476 |
Pretty.str "class instance (", |
|
18282 | 477 |
Pretty.str clsname, |
18231 | 478 |
Pretty.str ", (", |
479 |
Pretty.str tyco, |
|
480 |
Pretty.str ", ", |
|
18852 | 481 |
Pretty.enum "," "[" "]" (map (Pretty.enum "," "{" "}" o |
18850 | 482 |
map Pretty.str o snd) arity), |
18515 | 483 |
Pretty.str "))" |
19213 | 484 |
] |
485 |
| pretty_def Classinstmember = |
|
486 |
Pretty.str "class instance member"; |
|
18170 | 487 |
|
488 |
fun pretty_module modl = |
|
489 |
let |
|
490 |
fun pretty (name, Module modl) = |
|
491 |
Pretty.block ( |
|
492 |
Pretty.str ("module " ^ name ^ " {") |
|
493 |
:: Pretty.brk 1 |
|
494 |
:: Pretty.chunks (map pretty (AList.make (Graph.get_node modl) |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
495 |
(Graph.strong_conn modl |> flat |> rev))) |
18170 | 496 |
:: Pretty.str "}" :: nil |
497 |
) |
|
498 |
| pretty (name, Def def) = |
|
499 |
Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def] |
|
500 |
in pretty ("//", Module modl) end; |
|
501 |
||
18360 | 502 |
fun pretty_deps modl = |
503 |
let |
|
504 |
fun one_node key = |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
505 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
506 |
val preds_ = Graph.imm_preds modl key; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
507 |
val succs_ = Graph.imm_succs modl key; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
508 |
val mutbs = gen_inter (op =) (preds_, succs_); |
19300 | 509 |
val preds = subtract (op =) mutbs preds_; |
510 |
val succs = subtract (op =) mutbs succs_; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
511 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
512 |
(Pretty.block o Pretty.fbreaks) ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
513 |
Pretty.str key |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
514 |
:: map (fn s => Pretty.str ("<-> " ^ s)) mutbs |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
515 |
@ map (fn s => Pretty.str ("<-- " ^ s)) preds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
516 |
@ map (fn s => Pretty.str ("--> " ^ s)) succs |
18850 | 517 |
@ (the_list oo Option.mapPartial) |
518 |
((fn Module modl' => SOME (pretty_deps modl') |
|
519 |
| _ => NONE) o Graph.get_node modl) (SOME key) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
520 |
) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
521 |
end |
18360 | 522 |
in |
523 |
modl |
|
524 |
|> Graph.strong_conn |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
525 |
|> flat |
18360 | 526 |
|> rev |
527 |
|> map one_node |
|
528 |
|> Pretty.chunks |
|
529 |
end; |
|
530 |
||
18170 | 531 |
|
532 |
(* name handling *) |
|
533 |
||
534 |
fun dest_name name = |
|
535 |
let |
|
536 |
val name' = NameSpace.unpack name |
|
537 |
val (name'', name_base) = split_last name' |
|
538 |
val (modl, shallow) = split_last name'' |
|
539 |
in (modl, NameSpace.pack [shallow, name_base]) end |
|
540 |
handle Empty => error ("not a qualified name: " ^ quote name); |
|
541 |
||
18516 | 542 |
fun has_nsp name shallow = |
543 |
NameSpace.is_qualified name |
|
544 |
andalso let |
|
545 |
val name' = NameSpace.unpack name |
|
546 |
val (name'', _) = split_last name' |
|
547 |
val (_, shallow') = split_last name'' |
|
548 |
in shallow' = shallow end; |
|
549 |
||
18170 | 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 add_dep (name1, name2) modl = |
|
579 |
if name1 = name2 then modl |
|
580 |
else |
|
581 |
let |
|
582 |
val m1 = dest_name name1 |> apsnd single |> (op @); |
|
583 |
val m2 = dest_name name2 |> apsnd single |> (op @); |
|
18454 | 584 |
val (ms, (r1, r2)) = get_prefix (op =) (m1, m2); |
585 |
val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2); |
|
18170 | 586 |
val add_edge = |
587 |
if null r1 andalso null r2 |
|
588 |
then Graph.add_edge |
|
19785 | 589 |
else fn edge => fn gr => (Graph.add_edge_acyclic edge gr |
19038 | 590 |
handle Graph.CYCLES _ => error ("adding dependency " |
591 |
^ quote name1 ^ " -> " ^ quote name2 ^ " would result in module dependency cycle")) |
|
18170 | 592 |
fun add [] node = |
593 |
node |
|
594 |
|> add_edge (s1, s2) |
|
595 |
| add (m::ms) node = |
|
596 |
node |
|
597 |
|> Graph.map_node m (Module o add ms o dest_modl); |
|
598 |
in add ms modl end; |
|
599 |
||
18885 | 600 |
fun map_def name f = |
601 |
let |
|
602 |
val (modl, base) = dest_name name; |
|
603 |
fun mapp [] = |
|
604 |
Graph.map_node base (Def o f o dest_def) |
|
605 |
| mapp (m::ms) = |
|
606 |
Graph.map_node m (Module o mapp ms o dest_modl) |
|
607 |
in mapp modl end; |
|
608 |
||
609 |
fun map_defs f = |
|
610 |
let |
|
611 |
fun mapp (Def def) = |
|
612 |
(Def o f) def |
|
613 |
| mapp (Module modl) = |
|
614 |
(Module o Graph.map_nodes mapp) modl |
|
615 |
in dest_modl o mapp o Module end; |
|
616 |
||
617 |
fun fold_defs f = |
|
618 |
let |
|
19616 | 619 |
fun fol prfix (name, (Def def, _)) = |
18885 | 620 |
f (NameSpace.pack (prfix @ [name]), def) |
19616 | 621 |
| fol prfix (name, (Module modl, _)) = |
622 |
Graph.fold (fol (prfix @ [name])) modl |
|
623 |
in Graph.fold (fol []) end; |
|
18885 | 624 |
|
625 |
fun add_deps f modl = |
|
626 |
modl |
|
627 |
|> fold add_dep ([] |> fold_defs (append o f) modl); |
|
628 |
||
19136 | 629 |
fun add_def_incr (name, Undef) module = |
18885 | 630 |
(case try (get_def module) name |
631 |
of NONE => (error "attempted to add Undef to module") |
|
632 |
| SOME Undef => (error "attempted to add Undef to module") |
|
633 |
| SOME def' => map_def name (K def') module) |
|
19136 | 634 |
| add_def_incr (name, def) module = |
18885 | 635 |
(case try (get_def module) name |
636 |
of NONE => add_def (name, def) module |
|
637 |
| SOME Undef => map_def name (K def) module |
|
638 |
| SOME def' => if eq_def (def, def') |
|
639 |
then module |
|
640 |
else error ("tried to overwrite definition " ^ name)); |
|
641 |
||
18963 | 642 |
fun add_prim name (target, primdef as _::_) = |
18516 | 643 |
let |
644 |
val (modl, base) = dest_name name; |
|
645 |
fun add [] module = |
|
646 |
(case try (Graph.get_node module) base |
|
647 |
of NONE => |
|
648 |
module |
|
18963 | 649 |
|> Graph.new_node (base, (Def o Prim) [(target, primdef)]) |
18516 | 650 |
| SOME (Def (Prim prim)) => |
18702 | 651 |
if AList.defined (op =) prim target |
18850 | 652 |
then error ("already primitive definition (" ^ target |
653 |
^ ") present for " ^ name) |
|
18516 | 654 |
else |
655 |
module |
|
18850 | 656 |
|> Graph.map_node base ((K o Def o Prim) (AList.update (op =) |
18963 | 657 |
(target, primdef) prim)) |
18516 | 658 |
| _ => error ("already non-primitive definition present for " ^ name)) |
659 |
| add (m::ms) module = |
|
660 |
module |
|
661 |
|> Graph.default_node (m, Module empty_module) |
|
662 |
|> Graph.map_node m (Module o add ms o dest_modl) |
|
18963 | 663 |
in add modl end; |
18516 | 664 |
|
18702 | 665 |
fun ensure_prim name target = |
18516 | 666 |
let |
667 |
val (modl, base) = dest_name name; |
|
668 |
fun ensure [] module = |
|
669 |
(case try (Graph.get_node module) base |
|
670 |
of NONE => |
|
671 |
module |
|
18963 | 672 |
|> Graph.new_node (base, (Def o Prim) [(target, [])]) |
18702 | 673 |
| SOME (Def (Prim prim)) => |
18516 | 674 |
module |
18850 | 675 |
|> Graph.map_node base ((K o Def o Prim) (AList.default (op =) |
18963 | 676 |
(target, []) prim)) |
18850 | 677 |
| _ => module) |
18516 | 678 |
| ensure (m::ms) module = |
679 |
module |
|
680 |
|> Graph.default_node (m, Module empty_module) |
|
681 |
|> Graph.map_node m (Module o ensure ms o dest_modl) |
|
682 |
in ensure modl end; |
|
683 |
||
18170 | 684 |
fun merge_module modl12 = |
685 |
let |
|
19025 | 686 |
fun join_module _ (Module m1, Module m2) = |
687 |
Module (merge_module (m1, m2)) |
|
688 |
| join_module name (Def d1, Def d2) = |
|
19597 | 689 |
if eq_def (d1, d2) then Def d1 else Def d1 (*raise Graph.DUP name*) |
19025 | 690 |
| join_module name _ = raise Graph.DUP name |
691 |
in Graph.join join_module modl12 end; |
|
18170 | 692 |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
693 |
fun diff_module modl12 = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
694 |
let |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
695 |
fun diff_entry prefix modl2 (name, Def def1) = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
696 |
let |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
697 |
val e2 = try (Graph.get_node modl2) name |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
698 |
in if is_some e2 andalso eq_def (def1, (dest_def o the) e2) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
699 |
then I |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
700 |
else cons (NameSpace.pack (prefix @ [name]), def1) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
701 |
end |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
702 |
| diff_entry prefix modl2 (name, Module modl1) = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
703 |
diff_modl (prefix @ [name]) (modl1, |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
704 |
(the_default empty_module o Option.map dest_modl o try (Graph.get_node modl2)) name) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
705 |
and diff_modl prefix (modl1, modl2) = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
706 |
fold (diff_entry prefix modl2) |
19466 | 707 |
((AList.make (Graph.get_node modl1) o flat o Graph.strong_conn) modl1) |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
708 |
in diff_modl [] modl12 [] end; |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
709 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
710 |
local |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
711 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
712 |
fun project_trans f names modl = |
18335 | 713 |
let |
714 |
datatype pathnode = PN of (string list * (string * pathnode) list); |
|
715 |
fun mk_ipath ([], base) (PN (defs, modls)) = |
|
716 |
PN (base :: defs, modls) |
|
717 |
| mk_ipath (n::ns, base) (PN (defs, modls)) = |
|
718 |
modls |
|
719 |
|> AList.default (op =) (n, PN ([], [])) |
|
720 |
|> AList.map_entry (op =) n (mk_ipath (ns, base)) |
|
721 |
|> (pair defs #> PN); |
|
722 |
fun select (PN (defs, modls)) (Module module) = |
|
723 |
module |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
724 |
|> f (Graph.all_succs module (defs @ map fst modls)) |
18335 | 725 |
|> fold (fn (name, modls) => Graph.map_node name (select modls)) modls |
726 |
|> Module; |
|
727 |
in |
|
728 |
Module modl |
|
18850 | 729 |
|> select (fold (mk_ipath o dest_name) |
730 |
(filter NameSpace.is_qualified names) (PN ([], []))) |
|
18335 | 731 |
|> dest_modl |
732 |
end; |
|
18170 | 733 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
734 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
735 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
736 |
val project_module = project_trans Graph.subgraph; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
737 |
val purge_module = project_trans Graph.del_nodes; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
738 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
739 |
end; (*local*) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
740 |
|
18850 | 741 |
fun imports_of modl name = |
18702 | 742 |
let |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
743 |
(*fun submodules prfx modl = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
744 |
cons prfx |
19616 | 745 |
#> Graph.fold |
746 |
(fn (m, (Module modl, _)) => submodules (prfx @ [m]) modl |
|
747 |
| (_, (Def _, _)) => I) modl; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
748 |
fun get_modl name = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
749 |
fold (fn n => fn modl => (dest_modl oo Graph.get_node) modl n) name modl*) |
18702 | 750 |
fun imports prfx [] modl = |
751 |
[] |
|
752 |
| imports prfx (m::ms) modl = |
|
753 |
map (cons m) (imports (prfx @ [m]) ms ((dest_modl oo Graph.get_node) modl m)) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
754 |
@ map single (Graph.imm_succs modl m) |
18702 | 755 |
in |
18850 | 756 |
modl |
757 |
|> imports [] name |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
758 |
(*|> cons name |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
759 |
|> map (fn name => submodules name (get_modl name) []) |
19466 | 760 |
|> flat |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
761 |
|> remove (op =) name*) |
18702 | 762 |
|> map NameSpace.pack |
763 |
end; |
|
764 |
||
765 |
fun check_samemodule names = |
|
766 |
fold (fn name => |
|
767 |
let |
|
768 |
val modn = (fst o dest_name) name |
|
769 |
in |
|
770 |
fn NONE => SOME modn |
|
18850 | 771 |
| SOME mod' => if modn = mod' then SOME modn |
772 |
else error "inconsistent name prefix for simultanous names" |
|
18702 | 773 |
end |
774 |
) names NONE; |
|
775 |
||
776 |
fun check_funeqs eqs = |
|
777 |
(fold (fn (pats, _) => |
|
778 |
let |
|
779 |
val l = length pats |
|
780 |
in |
|
781 |
fn NONE => SOME l |
|
18850 | 782 |
| SOME l' => if l = l' then SOME l |
783 |
else error "function definition with different number of arguments" |
|
18702 | 784 |
end |
785 |
) eqs NONE; eqs); |
|
786 |
||
787 |
fun check_prep_def modl Undef = |
|
788 |
Undef |
|
789 |
| check_prep_def modl (d as Prim _) = |
|
790 |
d |
|
791 |
| check_prep_def modl (Fun (eqs, d)) = |
|
792 |
Fun (check_funeqs eqs, d) |
|
793 |
| check_prep_def modl (d as Typesyn _) = |
|
794 |
d |
|
19038 | 795 |
| check_prep_def modl (d as Datatype _) = |
796 |
d |
|
18702 | 797 |
| check_prep_def modl (Datatypecons dtco) = |
798 |
error "attempted to add bare datatype constructor" |
|
19038 | 799 |
| check_prep_def modl (d as Class _) = |
800 |
d |
|
18702 | 801 |
| check_prep_def modl (Classmember _) = |
802 |
error "attempted to add bare class member" |
|
18865 | 803 |
| check_prep_def modl (Classinst ((d as ((class, (tyco, arity)), _), memdefs))) = |
18170 | 804 |
let |
19038 | 805 |
val Class (_, (v, membrs)) = get_def modl class; |
18702 | 806 |
val _ = if length memdefs > length memdefs |
807 |
then error "too many member definitions given" |
|
808 |
else (); |
|
19150 | 809 |
fun instant (w, ty) v = |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
810 |
if v = w then ty else ITyVar v; |
19150 | 811 |
fun mk_memdef (m, (sortctxt, ty)) = |
18702 | 812 |
case AList.lookup (op =) memdefs m |
813 |
of NONE => error ("missing definition for member " ^ quote m) |
|
19253 | 814 |
| SOME ((m', (eqs, (sortctxt', ty'))), lss) => |
19215 | 815 |
let |
816 |
val sortctxt'' = sortctxt |> fold (fn v_sort => AList.update (op =) v_sort) arity; |
|
817 |
val ty'' = instant_itype (instant (v, tyco `%% map (ITyVar o fst) arity)) ty; |
|
818 |
in if eq_ityp ((sortctxt'', ty''), (sortctxt', ty')) |
|
19253 | 819 |
then (m, ((m', (check_funeqs eqs, (sortctxt', ty'))), lss)) |
19215 | 820 |
else |
821 |
error ("inconsistent type for member definition " ^ quote m ^ " [" ^ v ^ "]: " |
|
822 |
^ (Pretty.output o Pretty.block o Pretty.breaks) [ |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
823 |
pretty_sortcontext sortctxt'', |
19215 | 824 |
Pretty.str "|=>", |
825 |
pretty_itype ty'' |
|
826 |
] ^ " vs. " ^ (Pretty.output o Pretty.block o Pretty.breaks) [ |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
827 |
pretty_sortcontext sortctxt', |
19215 | 828 |
Pretty.str "|=>", |
829 |
pretty_itype ty' |
|
830 |
] |
|
831 |
) |
|
832 |
end |
|
19213 | 833 |
in Classinst (d, map mk_memdef membrs) end |
834 |
| check_prep_def modl Classinstmember = |
|
835 |
error "attempted to add bare class instance member"; |
|
18170 | 836 |
|
19038 | 837 |
fun postprocess_def (name, Datatype (_, constrs)) = |
18702 | 838 |
(check_samemodule (name :: map fst constrs); |
839 |
fold (fn (co, _) => |
|
19136 | 840 |
add_def_incr (co, Datatypecons name) |
18702 | 841 |
#> add_dep (co, name) |
842 |
#> add_dep (name, co) |
|
843 |
) constrs |
|
844 |
) |
|
19038 | 845 |
| postprocess_def (name, Class (_, (_, membrs))) = |
18702 | 846 |
(check_samemodule (name :: map fst membrs); |
847 |
fold (fn (m, _) => |
|
19136 | 848 |
add_def_incr (m, Classmember name) |
18702 | 849 |
#> add_dep (m, name) |
850 |
#> add_dep (name, m) |
|
851 |
) membrs |
|
852 |
) |
|
19213 | 853 |
| postprocess_def (name, Classinst (_, memdefs)) = |
19253 | 854 |
(check_samemodule (name :: map (fst o fst o snd) memdefs); |
855 |
fold (fn (_, ((m', _), _)) => |
|
19213 | 856 |
add_def_incr (m', Classinstmember) |
857 |
) memdefs |
|
858 |
) |
|
18702 | 859 |
| postprocess_def _ = |
860 |
I; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
861 |
|
18702 | 862 |
fun succeed some (_, modl) = (Succeed some, modl); |
863 |
fun fail msg (_, modl) = (Fail ([msg], NONE), modl); |
|
18170 | 864 |
|
18231 | 865 |
fun check_fail _ (Succeed dst, trns) = (dst, trns) |
866 |
| check_fail msg (Fail (msgs, e), _) = raise FAIL (msg::msgs, e); |
|
18170 | 867 |
|
18702 | 868 |
fun select_generator _ src [] modl = |
869 |
(SOME src, modl) |> fail ("no code generator available") |
|
18231 | 870 |
| select_generator mk_msg src gens modl = |
871 |
let |
|
872 |
fun handle_fail msgs f = |
|
873 |
let |
|
874 |
in |
|
875 |
if ! soft_exc |
|
876 |
then |
|
18702 | 877 |
(SOME src, modl) |> f |
878 |
handle FAIL exc => (Fail exc, modl) |
|
879 |
| e => (Fail (msgs, SOME e), modl) |
|
18231 | 880 |
else |
18702 | 881 |
(SOME src, modl) |> f |
882 |
handle FAIL exc => (Fail exc, modl) |
|
18231 | 883 |
end; |
884 |
fun select msgs [(gname, gen)] = |
|
18702 | 885 |
handle_fail (msgs @ [mk_msg gname]) (gen src) |
886 |
| select msgs ((gname, gen)::gens) = |
|
887 |
let |
|
888 |
val msgs' = msgs @ [mk_msg gname] |
|
889 |
in case handle_fail msgs' (gen src) |
|
890 |
of (Fail (_, NONE), _) => |
|
891 |
select msgs' gens |
|
892 |
| result => result |
|
18231 | 893 |
end; |
894 |
in select [] gens end; |
|
18170 | 895 |
|
19136 | 896 |
fun ensure_def defgens msg name (dep, modl) = |
18170 | 897 |
let |
18702 | 898 |
val msg' = case dep |
899 |
of NONE => msg |
|
900 |
| SOME dep => msg ^ ", with dependency " ^ quote dep; |
|
901 |
fun add_dp NONE = I |
|
902 |
| add_dp (SOME dep) = |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
903 |
debug_msg (fn _ => "adding dependency " ^ quote dep ^ " -> " ^ quote name) |
18702 | 904 |
#> add_dep (dep, name); |
905 |
fun prep_def def modl = |
|
906 |
(check_prep_def modl def, modl); |
|
18170 | 907 |
in |
908 |
modl |
|
18702 | 909 |
|> (if can (get_def modl) name |
910 |
then |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
911 |
debug_msg (fn _ => "asserting node " ^ quote name) |
18702 | 912 |
#> add_dp dep |
913 |
else |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
914 |
debug_msg (fn _ => "allocating node " ^ quote name) |
18702 | 915 |
#> add_def (name, Undef) |
916 |
#> add_dp dep |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
917 |
#> debug_msg (fn _ => "creating node " ^ quote name) |
18850 | 918 |
#> select_generator (fn gname => "trying code generator " |
919 |
^ gname ^ " for definition of " ^ quote name) name defgens |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
920 |
#> debug_msg (fn _ => "checking creation of node " ^ quote name) |
18702 | 921 |
#> check_fail msg' |
922 |
#-> (fn def => prep_def def) |
|
923 |
#-> (fn def => |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
924 |
debug_msg (fn _ => "addition of " ^ name ^ " := " ^ (Pretty.output o pretty_def) def) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
925 |
#> debug_msg (fn _ => "adding") |
19136 | 926 |
#> add_def_incr (name, def) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
927 |
#> debug_msg (fn _ => "postprocessing") |
18702 | 928 |
#> postprocess_def (name, def) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
929 |
#> debug_msg (fn _ => "adding done") |
18702 | 930 |
)) |
931 |
|> pair dep |
|
18170 | 932 |
end; |
933 |
||
18963 | 934 |
fun start_transact init f modl = |
18231 | 935 |
let |
18963 | 936 |
fun handle_fail f x = |
937 |
(f x |
|
18231 | 938 |
handle FAIL (msgs, NONE) => |
939 |
(error o cat_lines) ("code generation failed, while:" :: msgs)) |
|
940 |
handle FAIL (msgs, SOME e) => |
|
19597 | 941 |
((Output.error_msg o cat_lines) ("code generation failed, while:" :: msgs); raise e); |
18231 | 942 |
in |
18963 | 943 |
(init, modl) |
18231 | 944 |
|> handle_fail f |
945 |
|-> (fn x => fn (_, module) => (x, module)) |
|
946 |
end; |
|
18172 | 947 |
|
948 |
||
18335 | 949 |
|
18216 | 950 |
(** generic serialization **) |
951 |
||
952 |
(* resolving *) |
|
953 |
||
18885 | 954 |
structure NameMangler = NameManglerFun ( |
955 |
type ctxt = (string * string -> string) * (string -> string option); |
|
18516 | 956 |
type src = string * string; |
957 |
val ord = prod_ord string_ord string_ord; |
|
18963 | 958 |
fun mk (postprocess, validate) ((shallow, name), 0) = |
959 |
let |
|
960 |
val name' = postprocess (shallow, name); |
|
961 |
in case validate name' |
|
962 |
of NONE => name' |
|
963 |
| _ => mk (postprocess, validate) ((shallow, name), 1) |
|
964 |
end |
|
965 |
| mk (postprocess, validate) (("", name), i) = |
|
19150 | 966 |
postprocess ("", name ^ replicate_string i "'") |
967 |
|> perhaps validate |
|
968 |
| mk (postprocess, validate) ((shallow, name), 1) = |
|
969 |
postprocess (shallow, shallow ^ "_" ^ name) |
|
18885 | 970 |
|> perhaps validate |
18963 | 971 |
| mk (postprocess, validate) ((shallow, name), i) = |
19150 | 972 |
postprocess (shallow, name ^ replicate_string i "'") |
18516 | 973 |
|> perhaps validate; |
974 |
fun is_valid _ _ = true; |
|
975 |
fun maybe_unique _ _ = NONE; |
|
976 |
fun re_mangle _ dst = error ("no such definition name: " ^ quote dst); |
|
977 |
); |
|
978 |
||
18963 | 979 |
fun mk_deresolver module nsp_conn postprocess validate = |
18885 | 980 |
let |
981 |
datatype tabnode = N of string * tabnode Symtab.table option; |
|
982 |
fun mk module manglers tab = |
|
983 |
let |
|
984 |
fun mk_name name = |
|
985 |
case NameSpace.unpack name |
|
986 |
of [n] => ("", n) |
|
987 |
| [s, n] => (s, n); |
|
988 |
fun in_conn (shallow, conn) = |
|
989 |
member (op = : string * string -> bool) conn shallow; |
|
990 |
fun add_name name = |
|
991 |
let |
|
992 |
val n as (shallow, _) = mk_name name; |
|
993 |
in |
|
994 |
AList.map_entry_yield in_conn shallow ( |
|
18963 | 995 |
NameMangler.declare (postprocess, validate) n |
18885 | 996 |
#-> (fn n' => pair (name, n')) |
18963 | 997 |
) #> apfst the |
18885 | 998 |
end; |
999 |
val (renamings, manglers') = |
|
1000 |
fold_map add_name (Graph.keys module) manglers; |
|
1001 |
fun extend_tab (n, n') = |
|
1002 |
if (length o NameSpace.unpack) n = 1 |
|
1003 |
then |
|
1004 |
Symtab.update_new |
|
1005 |
(n, N (n', SOME (mk ((dest_modl o Graph.get_node module) n) manglers' Symtab.empty))) |
|
1006 |
else |
|
1007 |
Symtab.update_new (n, N (n', NONE)); |
|
1008 |
in fold extend_tab renamings tab end; |
|
1009 |
fun get_path_name [] tab = |
|
1010 |
([], SOME tab) |
|
1011 |
| get_path_name [p] tab = |
|
1012 |
let |
|
1013 |
val SOME (N (p', tab')) = Symtab.lookup tab p |
|
1014 |
in ([p'], tab') end |
|
1015 |
| get_path_name [p1, p2] tab = |
|
18919 | 1016 |
(case Symtab.lookup tab p1 |
18885 | 1017 |
of SOME (N (p', SOME tab')) => |
1018 |
let |
|
1019 |
val (ps', tab'') = get_path_name [p2] tab' |
|
1020 |
in (p' :: ps', tab'') end |
|
1021 |
| NONE => |
|
1022 |
let |
|
1023 |
val SOME (N (p', NONE)) = Symtab.lookup tab (NameSpace.pack [p1, p2]) |
|
18919 | 1024 |
in ([p'], NONE) end) |
18885 | 1025 |
| get_path_name (p::ps) tab = |
1026 |
let |
|
1027 |
val SOME (N (p', SOME tab')) = Symtab.lookup tab p |
|
1028 |
val (ps', tab'') = get_path_name ps tab' |
|
1029 |
in (p' :: ps', tab'') end; |
|
1030 |
fun deresolv tab prefix name = |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1031 |
let |
18885 | 1032 |
val (common, (_, rem)) = get_prefix (op =) (prefix, NameSpace.unpack name); |
1033 |
val (_, SOME tab') = get_path_name common tab; |
|
1034 |
val (name', _) = get_path_name rem tab'; |
|
1035 |
in NameSpace.pack name' end; |
|
1036 |
in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end; |
|
1037 |
||
18216 | 1038 |
|
1039 |
(* serialization *) |
|
1040 |
||
18963 | 1041 |
fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module = |
18216 | 1042 |
let |
18963 | 1043 |
val resolver = mk_deresolver module nsp_conn postprocess validate; |
19038 | 1044 |
fun sresolver s = (resolver o NameSpace.unpack) s |
18702 | 1045 |
fun mk_name prfx name = |
18850 | 1046 |
let |
1047 |
val name_qual = NameSpace.pack (prfx @ [name]) |
|
1048 |
in (name_qual, resolver prfx name_qual) end; |
|
18756 | 1049 |
fun mk_contents prfx module = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
1050 |
map_filter (seri prfx) |
18850 | 1051 |
((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module) |
18756 | 1052 |
and seri prfx ([(name, Module modl)]) = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1053 |
seri_module (resolver []) (map (resolver []) (imports_of module (prfx @ [name]))) |
18850 | 1054 |
(mk_name prfx name, mk_contents (prfx @ [name]) modl) |
18756 | 1055 |
| seri prfx ds = |
19038 | 1056 |
seri_defs sresolver (NameSpace.pack prfx) |
18912 | 1057 |
(map (fn (name, Def def) => (fst (mk_name prfx name), def)) ds) |
18216 | 1058 |
in |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19300
diff
changeset
|
1059 |
seri_module (resolver []) (imports_of module []) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
1060 |
(*map (resolver []) (Graph.strong_conn module |> flat |> rev)*) |
18850 | 1061 |
(("", name_root), (mk_contents [] module)) |
18216 | 1062 |
end; |
1063 |
||
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1064 |
end; (* struct *) |
19136 | 1065 |
|
19300 | 1066 |
structure BasicCodegenThingol: BASIC_CODEGEN_THINGOL = CodegenThingol; |