author | wenzelm |
Fri, 15 Aug 2008 16:08:08 +0200 | |
changeset 27893 | 7c97cf70d663 |
parent 27711 | 5052736b8bcc |
child 28054 | 2b84d34c5d02 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_thingol.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Intermediate language ("Thin-gol") representing executable code. |
|
24918 | 6 |
Representation and translation. |
24219 | 7 |
*) |
8 |
||
9 |
infix 8 `%%; |
|
10 |
infixr 6 `->; |
|
11 |
infixr 6 `-->; |
|
12 |
infix 4 `$; |
|
13 |
infix 4 `$$; |
|
14 |
infixr 3 `|->; |
|
15 |
infixr 3 `|-->; |
|
16 |
||
17 |
signature BASIC_CODE_THINGOL = |
|
18 |
sig |
|
19 |
type vname = string; |
|
20 |
datatype dict = |
|
21 |
DictConst of string * dict list list |
|
22 |
| DictVar of string list * (vname * (int * int)); |
|
23 |
datatype itype = |
|
24 |
`%% of string * itype list |
|
25 |
| ITyVar of vname; |
|
24591 | 26 |
type const = string * (dict list list * itype list (*types of arguments*)) |
24219 | 27 |
datatype iterm = |
24591 | 28 |
IConst of const |
24219 | 29 |
| IVar of vname |
30 |
| `$ of iterm * iterm |
|
31 |
| `|-> of (vname * itype) * iterm |
|
32 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
33 |
(*((term, type), [(selector pattern, body term )]), primitive term)*) |
|
34 |
val `-> : itype * itype -> itype; |
|
35 |
val `--> : itype list * itype -> itype; |
|
36 |
val `$$ : iterm * iterm list -> iterm; |
|
37 |
val `|--> : (vname * itype) list * iterm -> iterm; |
|
38 |
type typscheme = (vname * sort) list * itype; |
|
39 |
end; |
|
40 |
||
41 |
signature CODE_THINGOL = |
|
42 |
sig |
|
43 |
include BASIC_CODE_THINGOL; |
|
44 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list; |
|
45 |
val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a; |
|
46 |
val unfold_fun: itype -> itype list * itype; |
|
47 |
val unfold_app: iterm -> iterm * iterm list; |
|
48 |
val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option; |
|
49 |
val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm; |
|
50 |
val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option; |
|
51 |
val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm; |
|
52 |
val unfold_const_app: iterm -> |
|
53 |
((string * (dict list list * itype list)) * iterm list) option; |
|
54 |
val collapse_let: ((vname * itype) * iterm) * iterm |
|
55 |
-> (iterm * itype) * (iterm * iterm) list; |
|
27711 | 56 |
val eta_expand: int -> (string * (dict list list * itype list)) * iterm list -> iterm; |
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
57 |
val contains_dictvar: iterm -> bool; |
25621 | 58 |
val locally_monomorphic: iterm -> bool; |
24219 | 59 |
val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a; |
60 |
val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a; |
|
61 |
val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a; |
|
62 |
||
24918 | 63 |
datatype stmt = |
27024 | 64 |
NoStmt |
24591 | 65 |
| Fun of typscheme * ((iterm list * iterm) * thm) list |
24219 | 66 |
| Datatype of (vname * sort) list * (string * itype list) list |
67 |
| Datatypecons of string |
|
24811 | 68 |
| Class of vname * ((class * string) list * (string * itype) list) |
24219 | 69 |
| Classrel of class * class |
24837 | 70 |
| Classparam of class |
24219 | 71 |
| Classinst of (class * (string * (vname * sort) list)) |
72 |
* ((class * (string * (string * dict list list))) list |
|
24591 | 73 |
* ((string * const) * thm) list); |
27103 | 74 |
type program = stmt Graph.T; |
75 |
val empty_funs: program -> string list; |
|
27711 | 76 |
val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm; |
77 |
val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt; |
|
27103 | 78 |
val transitivly_non_empty_funs: program -> string list -> string list; |
79 |
val is_cons: program -> string -> bool; |
|
80 |
val contr_classparam_typs: program -> string -> itype option list; |
|
24219 | 81 |
|
27103 | 82 |
val consts_program: theory -> string list -> string list * program; |
83 |
val cached_program: theory -> program; |
|
84 |
val eval_conv: theory |
|
85 |
-> (term -> term * (program -> typscheme * iterm -> string list -> thm)) |
|
86 |
-> cterm -> thm; |
|
87 |
val eval_term: theory |
|
88 |
-> (term -> term * (program -> typscheme * iterm -> string list -> 'a)) |
|
89 |
-> term -> 'a; |
|
24219 | 90 |
end; |
91 |
||
92 |
structure CodeThingol: CODE_THINGOL = |
|
93 |
struct |
|
94 |
||
95 |
(** auxiliary **) |
|
96 |
||
97 |
fun unfoldl dest x = |
|
98 |
case dest x |
|
99 |
of NONE => (x, []) |
|
100 |
| SOME (x1, x2) => |
|
101 |
let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end; |
|
102 |
||
103 |
fun unfoldr dest x = |
|
104 |
case dest x |
|
105 |
of NONE => ([], x) |
|
106 |
| SOME (x1, x2) => |
|
107 |
let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end; |
|
108 |
||
109 |
||
110 |
(** language core - types, pattern, expressions **) |
|
111 |
||
112 |
(* language representation *) |
|
113 |
||
114 |
type vname = string; |
|
115 |
||
116 |
datatype dict = |
|
117 |
DictConst of string * dict list list |
|
118 |
| DictVar of string list * (vname * (int * int)); |
|
119 |
||
120 |
datatype itype = |
|
121 |
`%% of string * itype list |
|
122 |
| ITyVar of vname; |
|
123 |
||
24591 | 124 |
type const = string * (dict list list * itype list (*types of arguments*)) |
125 |
||
24219 | 126 |
datatype iterm = |
24591 | 127 |
IConst of const |
24219 | 128 |
| IVar of vname |
129 |
| `$ of iterm * iterm |
|
130 |
| `|-> of (vname * itype) * iterm |
|
131 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
132 |
(*see also signature*) |
|
133 |
||
134 |
(* |
|
135 |
variable naming conventions |
|
136 |
||
137 |
bare names: |
|
138 |
variable names v |
|
139 |
class names class |
|
140 |
type constructor names tyco |
|
141 |
datatype names dtco |
|
24837 | 142 |
const names (general) c (const) |
24219 | 143 |
constructor names co |
24837 | 144 |
class parameter names classparam |
24219 | 145 |
arbitrary name s |
146 |
||
24837 | 147 |
v, c, co, classparam also annotated with types etc. |
24219 | 148 |
|
149 |
constructs: |
|
150 |
sort sort |
|
151 |
type parameters vs |
|
152 |
type ty |
|
153 |
type schemes tysm |
|
154 |
term t |
|
155 |
(term as pattern) p |
|
156 |
instance (class, tyco) inst |
|
157 |
*) |
|
158 |
||
159 |
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2]; |
|
160 |
val op `--> = Library.foldr (op `->); |
|
161 |
val op `$$ = Library.foldl (op `$); |
|
162 |
val op `|--> = Library.foldr (op `|->); |
|
163 |
||
164 |
val unfold_fun = unfoldr |
|
165 |
(fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2) |
|
166 |
| _ => NONE); |
|
167 |
||
168 |
val unfold_app = unfoldl |
|
169 |
(fn op `$ t => SOME t |
|
170 |
| _ => NONE); |
|
171 |
||
172 |
val split_abs = |
|
173 |
(fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) => |
|
174 |
if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t) |
|
175 |
| (v, ty) `|-> t => SOME (((v, NONE), ty), t) |
|
176 |
| _ => NONE); |
|
177 |
||
178 |
val unfold_abs = unfoldr split_abs; |
|
179 |
||
180 |
val split_let = |
|
181 |
(fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t) |
|
182 |
| _ => NONE); |
|
183 |
||
184 |
val unfold_let = unfoldr split_let; |
|
185 |
||
186 |
fun unfold_const_app t = |
|
187 |
case unfold_app t |
|
188 |
of (IConst c, ts) => SOME (c, ts) |
|
189 |
| _ => NONE; |
|
190 |
||
191 |
fun fold_aiterms f (t as IConst _) = f t |
|
192 |
| fold_aiterms f (t as IVar _) = f t |
|
193 |
| fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2 |
|
194 |
| fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t' |
|
195 |
| fold_aiterms f (ICase (_, t)) = fold_aiterms f t; |
|
196 |
||
197 |
fun fold_constnames f = |
|
198 |
let |
|
199 |
fun add (IConst (c, _)) = f c |
|
200 |
| add _ = I; |
|
201 |
in fold_aiterms add end; |
|
202 |
||
203 |
fun fold_varnames f = |
|
204 |
let |
|
205 |
fun add (IVar v) = f v |
|
206 |
| add ((v, _) `|-> _) = f v |
|
207 |
| add _ = I; |
|
208 |
in fold_aiterms add end; |
|
209 |
||
210 |
fun fold_unbound_varnames f = |
|
211 |
let |
|
212 |
fun add _ (IConst _) = I |
|
213 |
| add vs (IVar v) = if not (member (op =) vs v) then f v else I |
|
214 |
| add vs (t1 `$ t2) = add vs t1 #> add vs t2 |
|
215 |
| add vs ((v, _) `|-> t) = add (insert (op =) v vs) t |
|
216 |
| add vs (ICase (_, t)) = add vs t; |
|
217 |
in add [] end; |
|
218 |
||
219 |
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) = |
|
220 |
let |
|
221 |
fun exists_v t = fold_unbound_varnames (fn w => fn b => |
|
222 |
b orelse v = w) t false; |
|
223 |
in if v = w andalso forall (fn (t1, t2) => |
|
224 |
exists_v t1 orelse not (exists_v t2)) ds |
|
225 |
then ((se, ty), ds) |
|
226 |
else ((se, ty), [(IVar v, be)]) |
|
227 |
end |
|
228 |
| collapse_let (((v, ty), se), be) = |
|
229 |
((se, ty), [(IVar v, be)]) |
|
230 |
||
27711 | 231 |
fun eta_expand k (c as (_, (_, tys)), ts) = |
24219 | 232 |
let |
233 |
val j = length ts; |
|
234 |
val l = k - j; |
|
235 |
val ctxt = (fold o fold_varnames) Name.declare ts Name.context; |
|
236 |
val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys); |
|
237 |
in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end; |
|
238 |
||
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
239 |
fun contains_dictvar t = |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
240 |
let |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
241 |
fun contains (DictConst (_, dss)) = (fold o fold) contains dss |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
242 |
| contains (DictVar _) = K true; |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
243 |
in |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
244 |
fold_aiterms |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
245 |
(fn IConst (_, (dss, _)) => (fold o fold) contains dss | _ => I) t false |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
246 |
end; |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
247 |
|
25621 | 248 |
fun locally_monomorphic (IConst _) = false |
249 |
| locally_monomorphic (IVar _) = true |
|
250 |
| locally_monomorphic (t `$ _) = locally_monomorphic t |
|
251 |
| locally_monomorphic (_ `|-> t) = locally_monomorphic t |
|
252 |
| locally_monomorphic (ICase ((_, ds), _)) = exists (locally_monomorphic o snd) ds; |
|
253 |
||
254 |
||
24219 | 255 |
|
27103 | 256 |
(** statements, abstract programs **) |
24219 | 257 |
|
258 |
type typscheme = (vname * sort) list * itype; |
|
24918 | 259 |
datatype stmt = |
27024 | 260 |
NoStmt |
24591 | 261 |
| Fun of typscheme * ((iterm list * iterm) * thm) list |
24219 | 262 |
| Datatype of (vname * sort) list * (string * itype list) list |
263 |
| Datatypecons of string |
|
24811 | 264 |
| Class of vname * ((class * string) list * (string * itype) list) |
24219 | 265 |
| Classrel of class * class |
24837 | 266 |
| Classparam of class |
24219 | 267 |
| Classinst of (class * (string * (vname * sort) list)) |
268 |
* ((class * (string * (string * dict list list))) list |
|
24591 | 269 |
* ((string * const) * thm) list); |
24219 | 270 |
|
27103 | 271 |
type program = stmt Graph.T; |
24219 | 272 |
|
27103 | 273 |
fun empty_funs program = |
274 |
Graph.fold (fn (name, (Fun (_, []), _)) => cons name |
|
275 |
| _ => I) program []; |
|
24219 | 276 |
|
27711 | 277 |
fun map_terms_bottom_up f (t as IConst _) = f t |
278 |
| map_terms_bottom_up f (t as IVar _) = f t |
|
279 |
| map_terms_bottom_up f (t1 `$ t2) = f |
|
280 |
(map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2) |
|
281 |
| map_terms_bottom_up f ((v, ty) `|-> t) = f |
|
282 |
((v, ty) `|-> map_terms_bottom_up f t) |
|
283 |
| map_terms_bottom_up f (ICase (((t, ty), ps), t0)) = f |
|
284 |
(ICase (((map_terms_bottom_up f t, ty), (map o pairself) |
|
285 |
(map_terms_bottom_up f) ps), map_terms_bottom_up f t0)); |
|
286 |
||
287 |
fun map_terms_stmt f NoStmt = NoStmt |
|
288 |
| map_terms_stmt f (Fun (tysm, eqs)) = Fun (tysm, (map o apfst) |
|
289 |
(fn (ts, t) => (map f ts, f t)) eqs) |
|
290 |
| map_terms_stmt f (stmt as Datatype _) = stmt |
|
291 |
| map_terms_stmt f (stmt as Datatypecons _) = stmt |
|
292 |
| map_terms_stmt f (stmt as Class _) = stmt |
|
293 |
| map_terms_stmt f (stmt as Classrel _) = stmt |
|
294 |
| map_terms_stmt f (stmt as Classparam _) = stmt |
|
295 |
| map_terms_stmt f (Classinst (arity, (superarities, classparms))) = |
|
296 |
Classinst (arity, (superarities, (map o apfst o apsnd) (fn const => |
|
297 |
case f (IConst const) of IConst const' => const') classparms)); |
|
298 |
||
27103 | 299 |
fun transitivly_non_empty_funs program names_ignored = |
24219 | 300 |
let |
27103 | 301 |
val names_empty = empty_funs program; |
302 |
val names_delete = Graph.all_preds program (subtract (op =) names_ignored names_empty) |
|
303 |
in subtract (op =) names_delete (Graph.keys program) end; |
|
24219 | 304 |
|
27103 | 305 |
fun is_cons program name = case Graph.get_node program name |
24219 | 306 |
of Datatypecons _ => true |
307 |
| _ => false; |
|
308 |
||
27103 | 309 |
fun contr_classparam_typs program name = case Graph.get_node program name |
25621 | 310 |
of Classparam class => let |
27103 | 311 |
val Class (_, (_, params)) = Graph.get_node program class; |
25621 | 312 |
val SOME ty = AList.lookup (op =) params name; |
313 |
val (tys, res_ty) = unfold_fun ty; |
|
314 |
fun no_tyvar (_ `%% tys) = forall no_tyvar tys |
|
315 |
| no_tyvar (ITyVar _) = false; |
|
316 |
in if no_tyvar res_ty |
|
317 |
then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys |
|
318 |
else [] |
|
319 |
end |
|
320 |
| _ => []; |
|
321 |
||
24219 | 322 |
|
27103 | 323 |
(** translation kernel **) |
24219 | 324 |
|
27103 | 325 |
fun ensure_stmt stmtgen name (dep, program) = |
24219 | 326 |
let |
27103 | 327 |
val add_dep = case dep of NONE => I | SOME dep => Graph.add_edge (dep, name); |
328 |
fun add_stmt false = |
|
329 |
Graph.default_node (name, NoStmt) |
|
330 |
#> add_dep |
|
24918 | 331 |
#> curry stmtgen (SOME name) |
24252 | 332 |
##> snd |
27103 | 333 |
#-> (fn stmt => Graph.map_node name (K stmt)) |
334 |
| add_stmt true = |
|
335 |
add_dep; |
|
24219 | 336 |
in |
27103 | 337 |
program |
338 |
|> add_stmt (can (Graph.get_node program) name) |
|
24219 | 339 |
|> pair dep |
24918 | 340 |
|> pair name |
24219 | 341 |
end; |
342 |
||
26972 | 343 |
fun not_wellsorted thy thm ty sort e = |
344 |
let |
|
345 |
val err_class = Sorts.class_error (Syntax.pp_global thy) e; |
|
346 |
val err_thm = case thm |
|
347 |
of SOME thm => "\n(in defining equation " ^ Display.string_of_thm thm ^ ")" | NONE => ""; |
|
348 |
val err_typ = "Type " ^ Syntax.string_of_typ_global thy ty ^ " not of sort " |
|
349 |
^ Syntax.string_of_sort_global thy sort; |
|
350 |
in error ("Wellsortedness error" ^ err_thm ^ ":\n" ^ err_typ ^ "\n" ^ err_class) end; |
|
351 |
||
352 |
fun ensure_class thy (algbr as (_, algebra)) funcgr class = |
|
24918 | 353 |
let |
354 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
24969 | 355 |
val cs = #params (AxClass.get_info thy class); |
24918 | 356 |
val class' = CodeName.class thy class; |
357 |
val stmt_class = |
|
358 |
fold_map (fn superclass => ensure_class thy algbr funcgr superclass |
|
359 |
##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses |
|
360 |
##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c |
|
361 |
##>> exprgen_typ thy algbr funcgr ty) cs |
|
24969 | 362 |
#>> (fn info => Class (unprefix "'" Name.aT, info)) |
27422 | 363 |
in ensure_stmt stmt_class class' end |
24918 | 364 |
and ensure_classrel thy algbr funcgr (subclass, superclass) = |
365 |
let |
|
366 |
val classrel' = CodeName.classrel thy (subclass, superclass); |
|
367 |
val stmt_classrel = |
|
368 |
ensure_class thy algbr funcgr subclass |
|
369 |
##>> ensure_class thy algbr funcgr superclass |
|
370 |
#>> Classrel; |
|
27422 | 371 |
in ensure_stmt stmt_classrel classrel' end |
24918 | 372 |
and ensure_tyco thy algbr funcgr "fun" = |
373 |
pair "fun" |
|
374 |
| ensure_tyco thy algbr funcgr tyco = |
|
375 |
let |
|
376 |
val stmt_datatype = |
|
377 |
let |
|
378 |
val (vs, cos) = Code.get_datatype thy tyco; |
|
379 |
in |
|
380 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
381 |
##>> fold_map (fn (c, tys) => |
|
382 |
ensure_const thy algbr funcgr c |
|
383 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos |
|
384 |
#>> Datatype |
|
385 |
end; |
|
386 |
val tyco' = CodeName.tyco thy tyco; |
|
27422 | 387 |
in ensure_stmt stmt_datatype tyco' end |
26972 | 388 |
and exprgen_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) = |
24918 | 389 |
fold_map (ensure_class thy algbr funcgr) (proj_sort sort) |
390 |
#>> (fn sort => (unprefix "'" v, sort)) |
|
27422 | 391 |
and exprgen_typ thy algbr funcgr (TFree v_sort) = |
392 |
exprgen_tyvar_sort thy algbr funcgr v_sort |
|
24918 | 393 |
#>> (fn (v, sort) => ITyVar v) |
394 |
| exprgen_typ thy algbr funcgr (Type (tyco, tys)) = |
|
395 |
ensure_tyco thy algbr funcgr tyco |
|
396 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys |
|
397 |
#>> (fn (tyco, tys) => tyco `%% tys) |
|
26972 | 398 |
and exprgen_dicts thy (algbr as (proj_sort, algebra)) funcgr thm (ty, sort) = |
24918 | 399 |
let |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26011
diff
changeset
|
400 |
val pp = Syntax.pp_global thy; |
24918 | 401 |
datatype typarg = |
402 |
Global of (class * string) * typarg list list |
|
403 |
| Local of (class * class) list * (string * (int * sort)); |
|
404 |
fun class_relation (Global ((_, tyco), yss), _) class = |
|
405 |
Global ((class, tyco), yss) |
|
406 |
| class_relation (Local (classrels, v), subclass) superclass = |
|
407 |
Local ((subclass, superclass) :: classrels, v); |
|
408 |
fun type_constructor tyco yss class = |
|
409 |
Global ((class, tyco), (map o map) fst yss); |
|
410 |
fun type_variable (TFree (v, sort)) = |
|
411 |
let |
|
412 |
val sort' = proj_sort sort; |
|
413 |
in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end; |
|
414 |
val typargs = Sorts.of_sort_derivation pp algebra |
|
415 |
{class_relation = class_relation, type_constructor = type_constructor, |
|
26972 | 416 |
type_variable = type_variable} (ty, proj_sort sort) |
417 |
handle Sorts.CLASS_ERROR e => not_wellsorted thy thm ty sort e; |
|
24918 | 418 |
fun mk_dict (Global (inst, yss)) = |
419 |
ensure_inst thy algbr funcgr inst |
|
420 |
##>> (fold_map o fold_map) mk_dict yss |
|
421 |
#>> (fn (inst, dss) => DictConst (inst, dss)) |
|
422 |
| mk_dict (Local (classrels, (v, (k, sort)))) = |
|
423 |
fold_map (ensure_classrel thy algbr funcgr) classrels |
|
424 |
#>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort)))) |
|
27422 | 425 |
in fold_map mk_dict typargs end |
24918 | 426 |
and exprgen_eq thy algbr funcgr thm = |
427 |
let |
|
428 |
val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals |
|
429 |
o Logic.unvarify o prop_of) thm; |
|
430 |
in |
|
26972 | 431 |
fold_map (exprgen_term thy algbr funcgr (SOME thm)) args |
432 |
##>> exprgen_term thy algbr funcgr (SOME thm) rhs |
|
24918 | 433 |
#>> rpair thm |
434 |
end |
|
26972 | 435 |
and ensure_inst thy (algbr as (_, algebra)) funcgr (class, tyco) = |
24918 | 436 |
let |
437 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
24969 | 438 |
val classparams = these (try (#params o AxClass.get_info thy) class); |
24918 | 439 |
val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]); |
440 |
val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class]; |
|
441 |
val vs' = map2 (fn (v, sort1) => fn sort2 => (v, |
|
442 |
Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts'; |
|
443 |
val arity_typ = Type (tyco, map TFree vs); |
|
444 |
val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs'); |
|
445 |
fun exprgen_superarity superclass = |
|
446 |
ensure_class thy algbr funcgr superclass |
|
447 |
##>> ensure_classrel thy algbr funcgr (class, superclass) |
|
26972 | 448 |
##>> exprgen_dicts thy algbr funcgr NONE (arity_typ, [superclass]) |
24918 | 449 |
#>> (fn ((superclass, classrel), [DictConst (inst, dss)]) => |
450 |
(superclass, (classrel, (inst, dss)))); |
|
451 |
fun exprgen_classparam_inst (c, ty) = |
|
452 |
let |
|
453 |
val c_inst = Const (c, map_type_tfree (K arity_typ') ty); |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25485
diff
changeset
|
454 |
val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy c_inst); |
24918 | 455 |
val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd |
456 |
o Logic.dest_equals o Thm.prop_of) thm; |
|
457 |
in |
|
458 |
ensure_const thy algbr funcgr c |
|
26972 | 459 |
##>> exprgen_const thy algbr funcgr (SOME thm) c_ty |
24918 | 460 |
#>> (fn (c, IConst c_inst) => ((c, c_inst), thm)) |
461 |
end; |
|
462 |
val stmt_inst = |
|
463 |
ensure_class thy algbr funcgr class |
|
464 |
##>> ensure_tyco thy algbr funcgr tyco |
|
465 |
##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
466 |
##>> fold_map exprgen_superarity superclasses |
|
467 |
##>> fold_map exprgen_classparam_inst classparams |
|
468 |
#>> (fn ((((class, tyco), arity), superarities), classparams) => |
|
469 |
Classinst ((class, (tyco, arity)), (superarities, classparams))); |
|
470 |
val inst = CodeName.instance thy (class, tyco); |
|
27422 | 471 |
in ensure_stmt stmt_inst inst end |
26972 | 472 |
and ensure_const thy algbr funcgr c = |
24918 | 473 |
let |
474 |
val c' = CodeName.const thy c; |
|
475 |
fun stmt_datatypecons tyco = |
|
476 |
ensure_tyco thy algbr funcgr tyco |
|
25621 | 477 |
#>> Datatypecons; |
24918 | 478 |
fun stmt_classparam class = |
479 |
ensure_class thy algbr funcgr class |
|
25621 | 480 |
#>> Classparam; |
24918 | 481 |
fun stmt_fun trns = |
482 |
let |
|
483 |
val raw_thms = CodeFuncgr.funcs funcgr c; |
|
26972 | 484 |
val (vs, raw_ty) = CodeFuncgr.typ funcgr c; |
485 |
val ty = Logic.unvarifyT raw_ty; |
|
24918 | 486 |
val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty |
487 |
then raw_thms |
|
488 |
else map (CodeUnit.expand_eta 1) raw_thms; |
|
489 |
in |
|
490 |
trns |
|
491 |
|> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
492 |
||>> exprgen_typ thy algbr funcgr ty |
|
493 |
||>> fold_map (exprgen_eq thy algbr funcgr) thms |
|
494 |
|>> (fn ((vs, ty), eqs) => Fun ((vs, ty), eqs)) |
|
495 |
end; |
|
496 |
val stmtgen = case Code.get_datatype_of_constr thy c |
|
497 |
of SOME tyco => stmt_datatypecons tyco |
|
498 |
| NONE => (case AxClass.class_of_param thy c |
|
499 |
of SOME class => stmt_classparam class |
|
500 |
| NONE => stmt_fun) |
|
27422 | 501 |
in ensure_stmt stmtgen c' end |
26972 | 502 |
and exprgen_term thy algbr funcgr thm (Const (c, ty)) = |
503 |
exprgen_app thy algbr funcgr thm ((c, ty), []) |
|
504 |
| exprgen_term thy algbr funcgr thm (Free (v, _)) = |
|
24918 | 505 |
pair (IVar v) |
26972 | 506 |
| exprgen_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) = |
24918 | 507 |
let |
508 |
val (v, t) = Syntax.variant_abs abs; |
|
509 |
in |
|
510 |
exprgen_typ thy algbr funcgr ty |
|
26972 | 511 |
##>> exprgen_term thy algbr funcgr thm t |
24918 | 512 |
#>> (fn (ty, t) => (v, ty) `|-> t) |
513 |
end |
|
26972 | 514 |
| exprgen_term thy algbr funcgr thm (t as _ $ _) = |
24918 | 515 |
case strip_comb t |
516 |
of (Const (c, ty), ts) => |
|
26972 | 517 |
exprgen_app thy algbr funcgr thm ((c, ty), ts) |
24918 | 518 |
| (t', ts) => |
26972 | 519 |
exprgen_term thy algbr funcgr thm t' |
520 |
##>> fold_map (exprgen_term thy algbr funcgr thm) ts |
|
24918 | 521 |
#>> (fn (t, ts) => t `$$ ts) |
26972 | 522 |
and exprgen_const thy algbr funcgr thm (c, ty) = |
523 |
let |
|
524 |
val tys = Sign.const_typargs thy (c, ty); |
|
525 |
val sorts = (map snd o fst o CodeFuncgr.typ funcgr) c; |
|
526 |
val tys_args = (fst o Term.strip_type) ty; |
|
527 |
in |
|
528 |
ensure_const thy algbr funcgr c |
|
529 |
##>> fold_map (exprgen_dicts thy algbr funcgr thm) (tys ~~ sorts) |
|
530 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys_args |
|
531 |
#>> (fn ((c, iss), tys) => IConst (c, (iss, tys))) |
|
532 |
end |
|
533 |
and exprgen_app_default thy algbr funcgr thm (c_ty, ts) = |
|
534 |
exprgen_const thy algbr funcgr thm c_ty |
|
535 |
##>> fold_map (exprgen_term thy algbr funcgr thm) ts |
|
24918 | 536 |
#>> (fn (t, ts) => t `$$ ts) |
26972 | 537 |
and exprgen_case thy algbr funcgr thm n cases (app as ((c, ty), ts)) = |
24918 | 538 |
let |
539 |
val (tys, _) = |
|
540 |
(chop (1 + (if null cases then 1 else length cases)) o fst o strip_type) ty; |
|
541 |
val dt = nth ts n; |
|
542 |
val dty = nth tys n; |
|
543 |
fun is_undefined (Const (c, _)) = Code.is_undefined thy c |
|
544 |
| is_undefined _ = false; |
|
545 |
fun mk_case (co, n) t = |
|
546 |
let |
|
547 |
val (vs, body) = Term.strip_abs_eta n t; |
|
548 |
val selector = list_comb (Const (co, map snd vs ---> dty), map Free vs); |
|
549 |
in if is_undefined body then NONE else SOME (selector, body) end; |
|
550 |
fun mk_ds [] = |
|
551 |
let |
|
552 |
val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts)) |
|
553 |
in [(Free v_ty, body)] end |
|
554 |
| mk_ds cases = map_filter (uncurry mk_case) |
|
555 |
(AList.make (CodeUnit.no_args thy) cases ~~ nth_drop n ts); |
|
556 |
in |
|
26972 | 557 |
exprgen_term thy algbr funcgr thm dt |
24918 | 558 |
##>> exprgen_typ thy algbr funcgr dty |
26972 | 559 |
##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr thm pat |
560 |
##>> exprgen_term thy algbr funcgr thm body) (mk_ds cases) |
|
561 |
##>> exprgen_app_default thy algbr funcgr thm app |
|
24918 | 562 |
#>> (fn (((dt, dty), ds), t0) => ICase (((dt, dty), ds), t0)) |
563 |
end |
|
26972 | 564 |
and exprgen_app thy algbr funcgr thm ((c, ty), ts) = case Code.get_case_data thy c |
24918 | 565 |
of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in |
566 |
if length ts < i then |
|
567 |
let |
|
568 |
val k = length ts; |
|
569 |
val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty; |
|
570 |
val ctxt = (fold o fold_aterms) |
|
571 |
(fn Free (v, _) => Name.declare v | _ => I) ts Name.context; |
|
572 |
val vs = Name.names ctxt "a" tys; |
|
573 |
in |
|
574 |
fold_map (exprgen_typ thy algbr funcgr) tys |
|
26972 | 575 |
##>> exprgen_case thy algbr funcgr thm n cases ((c, ty), ts @ map Free vs) |
24918 | 576 |
#>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t) |
577 |
end |
|
578 |
else if length ts > i then |
|
26972 | 579 |
exprgen_case thy algbr funcgr thm n cases ((c, ty), Library.take (i, ts)) |
580 |
##>> fold_map (exprgen_term thy algbr funcgr thm) (Library.drop (i, ts)) |
|
24918 | 581 |
#>> (fn (t, ts) => t `$$ ts) |
582 |
else |
|
26972 | 583 |
exprgen_case thy algbr funcgr thm n cases ((c, ty), ts) |
24918 | 584 |
end |
26972 | 585 |
| NONE => exprgen_app_default thy algbr funcgr thm ((c, ty), ts); |
24918 | 586 |
|
25969 | 587 |
|
27103 | 588 |
(** generated programs **) |
589 |
||
590 |
(* store *) |
|
591 |
||
592 |
structure Program = CodeDataFun |
|
593 |
( |
|
594 |
type T = program; |
|
595 |
val empty = Graph.empty; |
|
27609 | 596 |
fun purge thy cs program = |
597 |
let |
|
598 |
val cs_exisiting = |
|
599 |
map_filter (CodeName.const_rev thy) (Graph.keys program); |
|
600 |
val dels = (Graph.all_preds program |
|
601 |
o map (CodeName.const thy) |
|
602 |
o filter (member (op =) cs_exisiting) |
|
603 |
) cs; |
|
604 |
in Graph.del_nodes dels program end; |
|
27103 | 605 |
); |
606 |
||
607 |
val cached_program = Program.get; |
|
25969 | 608 |
|
27103 | 609 |
fun transact f program = |
610 |
(NONE, program) |
|
611 |
|> f |
|
612 |
|-> (fn x => fn (_, program) => (x, program)); |
|
613 |
||
614 |
fun generate thy funcgr f x = |
|
615 |
Program.change_yield thy (transact (f thy (Code.operational_algebra thy) funcgr x)); |
|
616 |
||
617 |
||
618 |
(* program generation *) |
|
619 |
||
620 |
fun consts_program thy cs = |
|
621 |
let |
|
622 |
fun project_consts cs program = |
|
623 |
let |
|
624 |
val cs_all = Graph.all_succs program cs; |
|
625 |
in (cs, Graph.subgraph (member (op =) cs_all) program) end; |
|
626 |
fun generate_consts thy algebra funcgr = |
|
627 |
fold_map (ensure_const thy algebra funcgr); |
|
628 |
in |
|
629 |
generate thy (CodeFuncgr.make thy cs) generate_consts cs |
|
630 |
|-> project_consts |
|
631 |
end; |
|
632 |
||
633 |
||
634 |
(* value evaluation *) |
|
25969 | 635 |
|
24918 | 636 |
fun ensure_value thy algbr funcgr t = |
637 |
let |
|
638 |
val ty = fastype_of t; |
|
639 |
val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =) |
|
640 |
o dest_TFree))) t []; |
|
641 |
val stmt_value = |
|
642 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
643 |
##>> exprgen_typ thy algbr funcgr ty |
|
26972 | 644 |
##>> exprgen_term thy algbr funcgr NONE t |
24918 | 645 |
#>> (fn ((vs, ty), t) => Fun ((vs, ty), [(([], t), Drule.dummy_thm)])); |
27103 | 646 |
fun term_value (dep, program1) = |
25969 | 647 |
let |
648 |
val Fun ((vs, ty), [(([], t), _)]) = |
|
27103 | 649 |
Graph.get_node program1 CodeName.value_name; |
650 |
val deps = Graph.imm_succs program1 CodeName.value_name; |
|
651 |
val program2 = Graph.del_nodes [CodeName.value_name] program1; |
|
652 |
val deps_all = Graph.all_succs program2 deps; |
|
653 |
val program3 = Graph.subgraph (member (op =) deps_all) program2; |
|
654 |
in ((program3, (((vs, ty), t), deps)), (dep, program2)) end; |
|
26011 | 655 |
in |
656 |
ensure_stmt stmt_value CodeName.value_name |
|
657 |
#> snd |
|
658 |
#> term_value |
|
659 |
end; |
|
24219 | 660 |
|
27103 | 661 |
fun eval eval_kind thy evaluator = |
662 |
let |
|
663 |
fun evaluator'' evaluator''' funcgr t = |
|
664 |
let |
|
665 |
val ((program, (vs_ty_t, deps)), _) = generate thy funcgr ensure_value t; |
|
666 |
in evaluator''' program vs_ty_t deps end; |
|
667 |
fun evaluator' t = |
|
668 |
let |
|
669 |
val (t', evaluator''') = evaluator t; |
|
670 |
in (t', evaluator'' evaluator''') end; |
|
671 |
in eval_kind thy evaluator' end |
|
672 |
||
673 |
fun eval_conv thy = eval CodeFuncgr.eval_conv thy; |
|
674 |
fun eval_term thy = eval CodeFuncgr.eval_term thy; |
|
675 |
||
24219 | 676 |
end; (*struct*) |
677 |
||
678 |
||
679 |
structure BasicCodeThingol: BASIC_CODE_THINGOL = CodeThingol; |