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