author | haftmann |
Fri, 24 Oct 2008 17:48:40 +0200 | |
changeset 28687 | 150a8a1eae1a |
parent 28663 | bd8438543bf2 |
child 28688 | 1a9fabb515cd |
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 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
43 |
include BASIC_CODE_THINGOL |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
44 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
45 |
val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
46 |
val unfold_fun: itype -> itype list * itype |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
47 |
val unfold_app: iterm -> iterm * iterm list |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
48 |
val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
49 |
val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
50 |
val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
51 |
val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm |
24219 | 52 |
val unfold_const_app: iterm -> |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
53 |
((string * (dict list list * itype list)) * iterm list) option |
24219 | 54 |
val collapse_let: ((vname * itype) * iterm) * iterm |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
55 |
-> (iterm * itype) * (iterm * iterm) list |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
56 |
val eta_expand: int -> (string * (dict list list * itype list)) * iterm list -> iterm |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
57 |
val contains_dictvar: iterm -> bool |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
58 |
val locally_monomorphic: iterm -> bool |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
59 |
val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
60 |
val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
61 |
val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
62 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
63 |
type naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
64 |
val lookup_class: naming -> class -> string option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
65 |
val lookup_classrel: naming -> class * class -> string option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
66 |
val lookup_tyco: naming -> string -> string option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
67 |
val lookup_instance: naming -> class * string -> string option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
68 |
val lookup_const: naming -> string -> string option |
24219 | 69 |
|
24918 | 70 |
datatype stmt = |
27024 | 71 |
NoStmt |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
72 |
| Fun of string * (typscheme * ((iterm list * iterm) * (thm * bool)) list) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
73 |
| Datatype of string * ((vname * sort) list * (string * itype list) list) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
74 |
| Datatypecons of string * string |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
75 |
| Class of class * (vname * ((class * string) list * (string * itype) list)) |
24219 | 76 |
| Classrel of class * class |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
77 |
| Classparam of string * class |
24219 | 78 |
| Classinst of (class * (string * (vname * sort) list)) |
79 |
* ((class * (string * (string * dict list list))) list |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
80 |
* ((string * const) * (thm * bool)) list) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
81 |
type program = stmt Graph.T |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
82 |
val empty_funs: program -> string list |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
83 |
val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
84 |
val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
85 |
val is_cons: program -> string -> bool |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
86 |
val contr_classparam_typs: program -> string -> itype option list |
24219 | 87 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
88 |
val consts_program: theory -> string list -> string list * (naming * program) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
89 |
val cached_program: theory -> naming * program |
27103 | 90 |
val eval_conv: theory |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
91 |
-> (term -> term * (naming -> program -> typscheme * iterm -> string list -> thm)) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
92 |
-> cterm -> thm |
27103 | 93 |
val eval_term: theory |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
94 |
-> (term -> term * (naming -> program -> typscheme * iterm -> string list -> 'a)) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
95 |
-> term -> 'a |
24219 | 96 |
end; |
97 |
||
28054 | 98 |
structure Code_Thingol: CODE_THINGOL = |
24219 | 99 |
struct |
100 |
||
101 |
(** auxiliary **) |
|
102 |
||
103 |
fun unfoldl dest x = |
|
104 |
case dest x |
|
105 |
of NONE => (x, []) |
|
106 |
| SOME (x1, x2) => |
|
107 |
let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end; |
|
108 |
||
109 |
fun unfoldr dest x = |
|
110 |
case dest x |
|
111 |
of NONE => ([], x) |
|
112 |
| SOME (x1, x2) => |
|
113 |
let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end; |
|
114 |
||
115 |
||
28369 | 116 |
(** language core - types, patterns, expressions **) |
24219 | 117 |
|
118 |
type vname = string; |
|
119 |
||
120 |
datatype dict = |
|
121 |
DictConst of string * dict list list |
|
122 |
| DictVar of string list * (vname * (int * int)); |
|
123 |
||
124 |
datatype itype = |
|
125 |
`%% of string * itype list |
|
126 |
| ITyVar of vname; |
|
127 |
||
24591 | 128 |
type const = string * (dict list list * itype list (*types of arguments*)) |
129 |
||
24219 | 130 |
datatype iterm = |
24591 | 131 |
IConst of const |
24219 | 132 |
| IVar of vname |
133 |
| `$ of iterm * iterm |
|
134 |
| `|-> of (vname * itype) * iterm |
|
135 |
| ICase of ((iterm * itype) * (iterm * iterm) list) * iterm; |
|
136 |
(*see also signature*) |
|
137 |
||
138 |
(* |
|
139 |
variable naming conventions |
|
140 |
||
141 |
bare names: |
|
142 |
variable names v |
|
143 |
class names class |
|
144 |
type constructor names tyco |
|
145 |
datatype names dtco |
|
24837 | 146 |
const names (general) c (const) |
24219 | 147 |
constructor names co |
24837 | 148 |
class parameter names classparam |
24219 | 149 |
arbitrary name s |
150 |
||
24837 | 151 |
v, c, co, classparam also annotated with types etc. |
24219 | 152 |
|
153 |
constructs: |
|
154 |
sort sort |
|
155 |
type parameters vs |
|
156 |
type ty |
|
157 |
type schemes tysm |
|
158 |
term t |
|
159 |
(term as pattern) p |
|
160 |
instance (class, tyco) inst |
|
161 |
*) |
|
162 |
||
163 |
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2]; |
|
164 |
val op `--> = Library.foldr (op `->); |
|
165 |
val op `$$ = Library.foldl (op `$); |
|
166 |
val op `|--> = Library.foldr (op `|->); |
|
167 |
||
168 |
val unfold_fun = unfoldr |
|
169 |
(fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2) |
|
170 |
| _ => NONE); |
|
171 |
||
172 |
val unfold_app = unfoldl |
|
173 |
(fn op `$ t => SOME t |
|
174 |
| _ => NONE); |
|
175 |
||
176 |
val split_abs = |
|
177 |
(fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) => |
|
178 |
if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t) |
|
179 |
| (v, ty) `|-> t => SOME (((v, NONE), ty), t) |
|
180 |
| _ => NONE); |
|
181 |
||
182 |
val unfold_abs = unfoldr split_abs; |
|
183 |
||
184 |
val split_let = |
|
185 |
(fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t) |
|
186 |
| _ => NONE); |
|
187 |
||
188 |
val unfold_let = unfoldr split_let; |
|
189 |
||
190 |
fun unfold_const_app t = |
|
191 |
case unfold_app t |
|
192 |
of (IConst c, ts) => SOME (c, ts) |
|
193 |
| _ => NONE; |
|
194 |
||
195 |
fun fold_aiterms f (t as IConst _) = f t |
|
196 |
| fold_aiterms f (t as IVar _) = f t |
|
197 |
| fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2 |
|
198 |
| fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t' |
|
199 |
| fold_aiterms f (ICase (_, t)) = fold_aiterms f t; |
|
200 |
||
201 |
fun fold_constnames f = |
|
202 |
let |
|
203 |
fun add (IConst (c, _)) = f c |
|
204 |
| add _ = I; |
|
205 |
in fold_aiterms add end; |
|
206 |
||
207 |
fun fold_varnames f = |
|
208 |
let |
|
209 |
fun add (IVar v) = f v |
|
210 |
| add ((v, _) `|-> _) = f v |
|
211 |
| add _ = I; |
|
212 |
in fold_aiterms add end; |
|
213 |
||
214 |
fun fold_unbound_varnames f = |
|
215 |
let |
|
216 |
fun add _ (IConst _) = I |
|
217 |
| add vs (IVar v) = if not (member (op =) vs v) then f v else I |
|
218 |
| add vs (t1 `$ t2) = add vs t1 #> add vs t2 |
|
219 |
| add vs ((v, _) `|-> t) = add (insert (op =) v vs) t |
|
220 |
| add vs (ICase (_, t)) = add vs t; |
|
221 |
in add [] end; |
|
222 |
||
223 |
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) = |
|
224 |
let |
|
225 |
fun exists_v t = fold_unbound_varnames (fn w => fn b => |
|
226 |
b orelse v = w) t false; |
|
227 |
in if v = w andalso forall (fn (t1, t2) => |
|
228 |
exists_v t1 orelse not (exists_v t2)) ds |
|
229 |
then ((se, ty), ds) |
|
230 |
else ((se, ty), [(IVar v, be)]) |
|
231 |
end |
|
232 |
| collapse_let (((v, ty), se), be) = |
|
233 |
((se, ty), [(IVar v, be)]) |
|
234 |
||
27711 | 235 |
fun eta_expand k (c as (_, (_, tys)), ts) = |
24219 | 236 |
let |
237 |
val j = length ts; |
|
238 |
val l = k - j; |
|
239 |
val ctxt = (fold o fold_varnames) Name.declare ts Name.context; |
|
240 |
val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys); |
|
241 |
in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end; |
|
242 |
||
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
243 |
fun contains_dictvar t = |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
244 |
let |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
245 |
fun contains (DictConst (_, dss)) = (fold o fold) contains dss |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
246 |
| contains (DictVar _) = K true; |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
247 |
in |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
248 |
fold_aiterms |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
249 |
(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
|
250 |
end; |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
251 |
|
25621 | 252 |
fun locally_monomorphic (IConst _) = false |
253 |
| locally_monomorphic (IVar _) = true |
|
254 |
| locally_monomorphic (t `$ _) = locally_monomorphic t |
|
255 |
| locally_monomorphic (_ `|-> t) = locally_monomorphic t |
|
256 |
| locally_monomorphic (ICase ((_, ds), _)) = exists (locally_monomorphic o snd) ds; |
|
257 |
||
258 |
||
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
259 |
(** naming policies **) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
260 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
261 |
(* identifier categories *) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
262 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
263 |
val suffix_class = "class"; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
264 |
val suffix_classrel = "classrel" |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
265 |
val suffix_tyco = "tyco"; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
266 |
val suffix_instance = "inst"; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
267 |
val suffix_const = "const"; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
268 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
269 |
fun add_suffix nsp NONE = NONE |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
270 |
| add_suffix nsp (SOME name) = SOME (NameSpace.append name nsp); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
271 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
272 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
273 |
(* policies *) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
274 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
275 |
local |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
276 |
fun thyname_of thy f x = the (AList.lookup (op =) (f x) Markup.theory_nameN); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
277 |
fun thyname_of_class thy = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
278 |
thyname_of thy (ProofContext.query_class (ProofContext.init thy)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
279 |
fun thyname_of_tyco thy = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
280 |
thyname_of thy (Type.the_tags (Sign.tsig_of thy)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
281 |
fun thyname_of_instance thy inst = case AxClass.arity_property thy inst Markup.theory_nameN |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
282 |
of [] => error ("no such instance: " ^ quote (snd inst ^ " :: " ^ fst inst)) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
283 |
| thyname :: _ => thyname; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
284 |
fun thyname_of_const thy c = case Code.get_datatype_of_constr thy c |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
285 |
of SOME dtco => thyname_of_tyco thy dtco |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
286 |
| NONE => (case AxClass.class_of_param thy c |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
287 |
of SOME class => thyname_of_class thy class |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
288 |
| NONE => (case AxClass.inst_of_param thy c |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
289 |
of SOME (c, tyco) => thyname_of_instance thy |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
290 |
((the o AxClass.class_of_param thy) c, tyco) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
291 |
| NONE => thyname_of thy (Consts.the_tags (Sign.consts_of thy)) c)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
292 |
fun namify thy get_basename get_thyname name = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
293 |
let |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
294 |
val prefix = get_thyname thy name; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
295 |
val base = (Code_Name.purify_base true o get_basename) name; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
296 |
in NameSpace.append prefix base end; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
297 |
in |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
298 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
299 |
fun namify_class thy = namify thy NameSpace.base thyname_of_class; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
300 |
fun namify_classrel thy = namify thy (fn (class1, class2) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
301 |
NameSpace.base class2 ^ "_" ^ NameSpace.base class1) (fn thy => thyname_of_class thy o fst); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
302 |
(*order fits nicely with composed projections*) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
303 |
fun namify_tyco thy = namify thy NameSpace.base thyname_of_tyco; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
304 |
fun namify_instance thy = namify thy (fn (class, tyco) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
305 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
306 |
fun namify_const thy = namify thy NameSpace.base thyname_of_const; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
307 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
308 |
end; (* local *) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
309 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
310 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
311 |
(* naming data with lookup and declare *) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
312 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
313 |
structure StringPairTab = Code_Name.StringPairTab; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
314 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
315 |
datatype naming = Naming of { |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
316 |
class: class Symtab.table * Name.context, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
317 |
classrel: string StringPairTab.table * Name.context, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
318 |
tyco: string Symtab.table * Name.context, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
319 |
instance: string StringPairTab.table * Name.context, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
320 |
const: string Symtab.table * Name.context |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
321 |
} |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
322 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
323 |
fun dest_Naming (Naming naming) = naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
324 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
325 |
val empty_naming = Naming { |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
326 |
class = (Symtab.empty, Name.context), |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
327 |
classrel = (StringPairTab.empty, Name.context), |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
328 |
tyco = (Symtab.empty, Name.context), |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
329 |
instance = (StringPairTab.empty, Name.context), |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
330 |
const = (Symtab.empty, Name.context) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
331 |
}; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
332 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
333 |
local |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
334 |
fun mk_naming (class, classrel, tyco, instance, const) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
335 |
Naming { class = class, classrel = classrel, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
336 |
tyco = tyco, instance = instance, const = const }; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
337 |
fun map_naming f (Naming { class, classrel, tyco, instance, const }) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
338 |
mk_naming (f (class, classrel, tyco, instance, const)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
339 |
in |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
340 |
fun map_class f = map_naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
341 |
(fn (class, classrel, tyco, inst, const) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
342 |
(f class, classrel, tyco, inst, const)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
343 |
fun map_classrel f = map_naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
344 |
(fn (class, classrel, tyco, inst, const) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
345 |
(class, f classrel, tyco, inst, const)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
346 |
fun map_tyco f = map_naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
347 |
(fn (class, classrel, tyco, inst, const) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
348 |
(class, classrel, f tyco, inst, const)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
349 |
fun map_instance f = map_naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
350 |
(fn (class, classrel, tyco, inst, const) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
351 |
(class, classrel, tyco, f inst, const)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
352 |
fun map_const f = map_naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
353 |
(fn (class, classrel, tyco, inst, const) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
354 |
(class, classrel, tyco, inst, f const)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
355 |
end; (*local*) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
356 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
357 |
fun add_variant update (thing, name) (tab, used) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
358 |
let |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
359 |
val (name', used') = yield_singleton Name.variants name used; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
360 |
val tab' = update (thing, name') tab; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
361 |
in (tab', used') end; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
362 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
363 |
fun declare thy mapp lookup update namify thing = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
364 |
mapp (add_variant update (thing, namify thy thing)) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
365 |
#> `(fn naming => the (lookup naming thing)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
366 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
367 |
val lookup_class = add_suffix suffix_class |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
368 |
oo Symtab.lookup o fst o #class o dest_Naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
369 |
val lookup_classrel = add_suffix suffix_classrel |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
370 |
oo StringPairTab.lookup o fst o #classrel o dest_Naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
371 |
val lookup_tyco = add_suffix suffix_tyco |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
372 |
oo Symtab.lookup o fst o #tyco o dest_Naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
373 |
val lookup_instance = add_suffix suffix_instance |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
374 |
oo StringPairTab.lookup o fst o #instance o dest_Naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
375 |
val lookup_const = add_suffix suffix_const |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
376 |
oo Symtab.lookup o fst o #const o dest_Naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
377 |
|
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
378 |
fun declare_class thy = declare thy map_class |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
379 |
lookup_class Symtab.update_new namify_class; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
380 |
fun declare_classrel thy = declare thy map_classrel |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
381 |
lookup_classrel StringPairTab.update_new namify_classrel; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
382 |
fun declare_tyco thy = declare thy map_tyco |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
383 |
lookup_tyco Symtab.update_new namify_tyco; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
384 |
fun declare_instance thy = declare thy map_instance |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
385 |
lookup_instance StringPairTab.update_new namify_instance; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
386 |
fun declare_const thy = declare thy map_const |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
387 |
lookup_const Symtab.update_new namify_const; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
388 |
|
24219 | 389 |
|
27103 | 390 |
(** statements, abstract programs **) |
24219 | 391 |
|
392 |
type typscheme = (vname * sort) list * itype; |
|
24918 | 393 |
datatype stmt = |
27024 | 394 |
NoStmt |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
395 |
| Fun of string * (typscheme * ((iterm list * iterm) * (thm * bool)) list) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
396 |
| Datatype of string * ((vname * sort) list * (string * itype list) list) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
397 |
| Datatypecons of string * string |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
398 |
| Class of class * (vname * ((class * string) list * (string * itype) list)) |
24219 | 399 |
| Classrel of class * class |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
400 |
| Classparam of string * class |
24219 | 401 |
| Classinst of (class * (string * (vname * sort) list)) |
402 |
* ((class * (string * (string * dict list list))) list |
|
28350 | 403 |
* ((string * const) * (thm * bool)) list); |
24219 | 404 |
|
27103 | 405 |
type program = stmt Graph.T; |
24219 | 406 |
|
27103 | 407 |
fun empty_funs program = |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
408 |
Graph.fold (fn (name, (Fun (c, (_, [])), _)) => cons c |
27103 | 409 |
| _ => I) program []; |
24219 | 410 |
|
27711 | 411 |
fun map_terms_bottom_up f (t as IConst _) = f t |
412 |
| map_terms_bottom_up f (t as IVar _) = f t |
|
413 |
| map_terms_bottom_up f (t1 `$ t2) = f |
|
414 |
(map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2) |
|
415 |
| map_terms_bottom_up f ((v, ty) `|-> t) = f |
|
416 |
((v, ty) `|-> map_terms_bottom_up f t) |
|
417 |
| map_terms_bottom_up f (ICase (((t, ty), ps), t0)) = f |
|
418 |
(ICase (((map_terms_bottom_up f t, ty), (map o pairself) |
|
419 |
(map_terms_bottom_up f) ps), map_terms_bottom_up f t0)); |
|
420 |
||
421 |
fun map_terms_stmt f NoStmt = NoStmt |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
422 |
| map_terms_stmt f (Fun (c, (tysm, eqs))) = Fun (c, (tysm, (map o apfst) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
423 |
(fn (ts, t) => (map f ts, f t)) eqs)) |
27711 | 424 |
| map_terms_stmt f (stmt as Datatype _) = stmt |
425 |
| map_terms_stmt f (stmt as Datatypecons _) = stmt |
|
426 |
| map_terms_stmt f (stmt as Class _) = stmt |
|
427 |
| map_terms_stmt f (stmt as Classrel _) = stmt |
|
428 |
| map_terms_stmt f (stmt as Classparam _) = stmt |
|
429 |
| map_terms_stmt f (Classinst (arity, (superarities, classparms))) = |
|
430 |
Classinst (arity, (superarities, (map o apfst o apsnd) (fn const => |
|
431 |
case f (IConst const) of IConst const' => const') classparms)); |
|
432 |
||
27103 | 433 |
fun is_cons program name = case Graph.get_node program name |
24219 | 434 |
of Datatypecons _ => true |
435 |
| _ => false; |
|
436 |
||
27103 | 437 |
fun contr_classparam_typs program name = case Graph.get_node program name |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
438 |
of Classparam (_, class) => let |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
439 |
val Class (_, (_, (_, params))) = Graph.get_node program class; |
25621 | 440 |
val SOME ty = AList.lookup (op =) params name; |
441 |
val (tys, res_ty) = unfold_fun ty; |
|
442 |
fun no_tyvar (_ `%% tys) = forall no_tyvar tys |
|
443 |
| no_tyvar (ITyVar _) = false; |
|
444 |
in if no_tyvar res_ty |
|
445 |
then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys |
|
446 |
else [] |
|
447 |
end |
|
448 |
| _ => []; |
|
449 |
||
24219 | 450 |
|
27103 | 451 |
(** translation kernel **) |
24219 | 452 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
453 |
fun ensure_stmt lookup declare generate thing (dep, (naming, program)) = |
24219 | 454 |
let |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
455 |
fun add_dep name = case dep |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
456 |
of NONE => I | SOME dep => Graph.add_edge (dep, name); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
457 |
in case lookup naming thing |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
458 |
of SOME name => program |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
459 |
|> add_dep name |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
460 |
|> pair naming |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
461 |
|> pair dep |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
462 |
|> pair name |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
463 |
| NONE => let |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
464 |
val (name, naming') = declare thing naming; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
465 |
in |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
466 |
program |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
467 |
|> Graph.default_node (name, NoStmt) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
468 |
|> add_dep name |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
469 |
|> pair naming' |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
470 |
|> curry generate (SOME name) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
471 |
||> snd |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
472 |
|-> (fn stmt => (apsnd o Graph.map_node name) (K stmt)) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
473 |
|> pair dep |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
474 |
|> pair name |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
475 |
end |
24219 | 476 |
end; |
477 |
||
26972 | 478 |
fun not_wellsorted thy thm ty sort e = |
479 |
let |
|
480 |
val err_class = Sorts.class_error (Syntax.pp_global thy) e; |
|
481 |
val err_thm = case thm |
|
482 |
of SOME thm => "\n(in defining equation " ^ Display.string_of_thm thm ^ ")" | NONE => ""; |
|
483 |
val err_typ = "Type " ^ Syntax.string_of_typ_global thy ty ^ " not of sort " |
|
484 |
^ Syntax.string_of_sort_global thy sort; |
|
485 |
in error ("Wellsortedness error" ^ err_thm ^ ":\n" ^ err_typ ^ "\n" ^ err_class) end; |
|
486 |
||
487 |
fun ensure_class thy (algbr as (_, algebra)) funcgr class = |
|
24918 | 488 |
let |
489 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
24969 | 490 |
val cs = #params (AxClass.get_info thy class); |
24918 | 491 |
val stmt_class = |
492 |
fold_map (fn superclass => ensure_class thy algbr funcgr superclass |
|
493 |
##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses |
|
494 |
##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c |
|
495 |
##>> exprgen_typ thy algbr funcgr ty) cs |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
496 |
#>> (fn info => Class (class, (unprefix "'" Name.aT, info))) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
497 |
in ensure_stmt lookup_class (declare_class thy) stmt_class class end |
24918 | 498 |
and ensure_classrel thy algbr funcgr (subclass, superclass) = |
499 |
let |
|
500 |
val stmt_classrel = |
|
501 |
ensure_class thy algbr funcgr subclass |
|
502 |
##>> ensure_class thy algbr funcgr superclass |
|
503 |
#>> Classrel; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
504 |
in ensure_stmt lookup_classrel (declare_classrel thy) stmt_classrel (subclass, superclass) end |
24918 | 505 |
and ensure_tyco thy algbr funcgr "fun" = |
506 |
pair "fun" |
|
507 |
| ensure_tyco thy algbr funcgr tyco = |
|
508 |
let |
|
509 |
val stmt_datatype = |
|
510 |
let |
|
511 |
val (vs, cos) = Code.get_datatype thy tyco; |
|
512 |
in |
|
513 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
514 |
##>> fold_map (fn (c, tys) => |
|
515 |
ensure_const thy algbr funcgr c |
|
516 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
517 |
#>> (fn info => Datatype (tyco, info)) |
24918 | 518 |
end; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
519 |
in ensure_stmt lookup_tyco (declare_tyco thy) stmt_datatype tyco end |
26972 | 520 |
and exprgen_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) = |
24918 | 521 |
fold_map (ensure_class thy algbr funcgr) (proj_sort sort) |
522 |
#>> (fn sort => (unprefix "'" v, sort)) |
|
27422 | 523 |
and exprgen_typ thy algbr funcgr (TFree v_sort) = |
524 |
exprgen_tyvar_sort thy algbr funcgr v_sort |
|
24918 | 525 |
#>> (fn (v, sort) => ITyVar v) |
526 |
| exprgen_typ thy algbr funcgr (Type (tyco, tys)) = |
|
527 |
ensure_tyco thy algbr funcgr tyco |
|
528 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys |
|
529 |
#>> (fn (tyco, tys) => tyco `%% tys) |
|
26972 | 530 |
and exprgen_dicts thy (algbr as (proj_sort, algebra)) funcgr thm (ty, sort) = |
24918 | 531 |
let |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26011
diff
changeset
|
532 |
val pp = Syntax.pp_global thy; |
24918 | 533 |
datatype typarg = |
534 |
Global of (class * string) * typarg list list |
|
535 |
| Local of (class * class) list * (string * (int * sort)); |
|
536 |
fun class_relation (Global ((_, tyco), yss), _) class = |
|
537 |
Global ((class, tyco), yss) |
|
538 |
| class_relation (Local (classrels, v), subclass) superclass = |
|
539 |
Local ((subclass, superclass) :: classrels, v); |
|
540 |
fun type_constructor tyco yss class = |
|
541 |
Global ((class, tyco), (map o map) fst yss); |
|
542 |
fun type_variable (TFree (v, sort)) = |
|
543 |
let |
|
544 |
val sort' = proj_sort sort; |
|
545 |
in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end; |
|
546 |
val typargs = Sorts.of_sort_derivation pp algebra |
|
547 |
{class_relation = class_relation, type_constructor = type_constructor, |
|
26972 | 548 |
type_variable = type_variable} (ty, proj_sort sort) |
549 |
handle Sorts.CLASS_ERROR e => not_wellsorted thy thm ty sort e; |
|
24918 | 550 |
fun mk_dict (Global (inst, yss)) = |
551 |
ensure_inst thy algbr funcgr inst |
|
552 |
##>> (fold_map o fold_map) mk_dict yss |
|
553 |
#>> (fn (inst, dss) => DictConst (inst, dss)) |
|
554 |
| mk_dict (Local (classrels, (v, (k, sort)))) = |
|
555 |
fold_map (ensure_classrel thy algbr funcgr) classrels |
|
556 |
#>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort)))) |
|
27422 | 557 |
in fold_map mk_dict typargs end |
28350 | 558 |
and exprgen_eq thy algbr funcgr (thm, linear) = |
24918 | 559 |
let |
560 |
val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals |
|
561 |
o Logic.unvarify o prop_of) thm; |
|
562 |
in |
|
26972 | 563 |
fold_map (exprgen_term thy algbr funcgr (SOME thm)) args |
564 |
##>> exprgen_term thy algbr funcgr (SOME thm) rhs |
|
28350 | 565 |
#>> rpair (thm, linear) |
24918 | 566 |
end |
26972 | 567 |
and ensure_inst thy (algbr as (_, algebra)) funcgr (class, tyco) = |
24918 | 568 |
let |
569 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
24969 | 570 |
val classparams = these (try (#params o AxClass.get_info thy) class); |
24918 | 571 |
val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]); |
572 |
val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class]; |
|
573 |
val vs' = map2 (fn (v, sort1) => fn sort2 => (v, |
|
574 |
Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts'; |
|
575 |
val arity_typ = Type (tyco, map TFree vs); |
|
576 |
val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs'); |
|
577 |
fun exprgen_superarity superclass = |
|
578 |
ensure_class thy algbr funcgr superclass |
|
579 |
##>> ensure_classrel thy algbr funcgr (class, superclass) |
|
26972 | 580 |
##>> exprgen_dicts thy algbr funcgr NONE (arity_typ, [superclass]) |
24918 | 581 |
#>> (fn ((superclass, classrel), [DictConst (inst, dss)]) => |
582 |
(superclass, (classrel, (inst, dss)))); |
|
583 |
fun exprgen_classparam_inst (c, ty) = |
|
584 |
let |
|
585 |
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
|
586 |
val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy c_inst); |
24918 | 587 |
val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd |
588 |
o Logic.dest_equals o Thm.prop_of) thm; |
|
589 |
in |
|
590 |
ensure_const thy algbr funcgr c |
|
26972 | 591 |
##>> exprgen_const thy algbr funcgr (SOME thm) c_ty |
28350 | 592 |
#>> (fn (c, IConst c_inst) => ((c, c_inst), (thm, true))) |
24918 | 593 |
end; |
594 |
val stmt_inst = |
|
595 |
ensure_class thy algbr funcgr class |
|
596 |
##>> ensure_tyco thy algbr funcgr tyco |
|
597 |
##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
598 |
##>> fold_map exprgen_superarity superclasses |
|
599 |
##>> fold_map exprgen_classparam_inst classparams |
|
600 |
#>> (fn ((((class, tyco), arity), superarities), classparams) => |
|
601 |
Classinst ((class, (tyco, arity)), (superarities, classparams))); |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
602 |
in ensure_stmt lookup_instance (declare_instance thy) stmt_inst (class, tyco) end |
26972 | 603 |
and ensure_const thy algbr funcgr c = |
24918 | 604 |
let |
605 |
fun stmt_datatypecons tyco = |
|
606 |
ensure_tyco thy algbr funcgr tyco |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
607 |
#>> (fn tyco => Datatypecons (c, tyco)); |
24918 | 608 |
fun stmt_classparam class = |
609 |
ensure_class thy algbr funcgr class |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
610 |
#>> (fn class => Classparam (c, class)); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
611 |
fun stmt_fun ((vs, raw_ty), raw_thms) = |
24918 | 612 |
let |
26972 | 613 |
val ty = Logic.unvarifyT raw_ty; |
24918 | 614 |
val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty |
615 |
then raw_thms |
|
28423 | 616 |
else (map o apfst) (Code_Unit.expand_eta thy 1) raw_thms; |
24918 | 617 |
in |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
618 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
619 |
##>> exprgen_typ thy algbr funcgr ty |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
620 |
##>> fold_map (exprgen_eq thy algbr funcgr) thms |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
621 |
#>> (fn info => Fun (c, info)) |
24918 | 622 |
end; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
623 |
val stmt_const = case Code.get_datatype_of_constr thy c |
24918 | 624 |
of SOME tyco => stmt_datatypecons tyco |
625 |
| NONE => (case AxClass.class_of_param thy c |
|
626 |
of SOME class => stmt_classparam class |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
627 |
| NONE => stmt_fun (Code_Funcgr.typ funcgr c, Code_Funcgr.eqns funcgr c)) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
628 |
in ensure_stmt lookup_const (declare_const thy) stmt_const c end |
26972 | 629 |
and exprgen_term thy algbr funcgr thm (Const (c, ty)) = |
630 |
exprgen_app thy algbr funcgr thm ((c, ty), []) |
|
631 |
| exprgen_term thy algbr funcgr thm (Free (v, _)) = |
|
24918 | 632 |
pair (IVar v) |
26972 | 633 |
| exprgen_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) = |
24918 | 634 |
let |
635 |
val (v, t) = Syntax.variant_abs abs; |
|
636 |
in |
|
637 |
exprgen_typ thy algbr funcgr ty |
|
26972 | 638 |
##>> exprgen_term thy algbr funcgr thm t |
24918 | 639 |
#>> (fn (ty, t) => (v, ty) `|-> t) |
640 |
end |
|
26972 | 641 |
| exprgen_term thy algbr funcgr thm (t as _ $ _) = |
24918 | 642 |
case strip_comb t |
643 |
of (Const (c, ty), ts) => |
|
26972 | 644 |
exprgen_app thy algbr funcgr thm ((c, ty), ts) |
24918 | 645 |
| (t', ts) => |
26972 | 646 |
exprgen_term thy algbr funcgr thm t' |
647 |
##>> fold_map (exprgen_term thy algbr funcgr thm) ts |
|
24918 | 648 |
#>> (fn (t, ts) => t `$$ ts) |
26972 | 649 |
and exprgen_const thy algbr funcgr thm (c, ty) = |
650 |
let |
|
651 |
val tys = Sign.const_typargs thy (c, ty); |
|
28054 | 652 |
val sorts = (map snd o fst o Code_Funcgr.typ funcgr) c; |
26972 | 653 |
val tys_args = (fst o Term.strip_type) ty; |
654 |
in |
|
655 |
ensure_const thy algbr funcgr c |
|
656 |
##>> fold_map (exprgen_dicts thy algbr funcgr thm) (tys ~~ sorts) |
|
657 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys_args |
|
658 |
#>> (fn ((c, iss), tys) => IConst (c, (iss, tys))) |
|
659 |
end |
|
660 |
and exprgen_app_default thy algbr funcgr thm (c_ty, ts) = |
|
661 |
exprgen_const thy algbr funcgr thm c_ty |
|
662 |
##>> fold_map (exprgen_term thy algbr funcgr thm) ts |
|
24918 | 663 |
#>> (fn (t, ts) => t `$$ ts) |
26972 | 664 |
and exprgen_case thy algbr funcgr thm n cases (app as ((c, ty), ts)) = |
24918 | 665 |
let |
666 |
val (tys, _) = |
|
667 |
(chop (1 + (if null cases then 1 else length cases)) o fst o strip_type) ty; |
|
668 |
val dt = nth ts n; |
|
669 |
val dty = nth tys n; |
|
670 |
fun is_undefined (Const (c, _)) = Code.is_undefined thy c |
|
671 |
| is_undefined _ = false; |
|
672 |
fun mk_case (co, n) t = |
|
673 |
let |
|
674 |
val (vs, body) = Term.strip_abs_eta n t; |
|
675 |
val selector = list_comb (Const (co, map snd vs ---> dty), map Free vs); |
|
676 |
in if is_undefined body then NONE else SOME (selector, body) end; |
|
677 |
fun mk_ds [] = |
|
678 |
let |
|
679 |
val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts)) |
|
680 |
in [(Free v_ty, body)] end |
|
681 |
| mk_ds cases = map_filter (uncurry mk_case) |
|
28054 | 682 |
(AList.make (Code_Unit.no_args thy) cases ~~ nth_drop n ts); |
24918 | 683 |
in |
26972 | 684 |
exprgen_term thy algbr funcgr thm dt |
24918 | 685 |
##>> exprgen_typ thy algbr funcgr dty |
26972 | 686 |
##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr thm pat |
687 |
##>> exprgen_term thy algbr funcgr thm body) (mk_ds cases) |
|
688 |
##>> exprgen_app_default thy algbr funcgr thm app |
|
24918 | 689 |
#>> (fn (((dt, dty), ds), t0) => ICase (((dt, dty), ds), t0)) |
690 |
end |
|
26972 | 691 |
and exprgen_app thy algbr funcgr thm ((c, ty), ts) = case Code.get_case_data thy c |
24918 | 692 |
of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in |
693 |
if length ts < i then |
|
694 |
let |
|
695 |
val k = length ts; |
|
696 |
val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty; |
|
697 |
val ctxt = (fold o fold_aterms) |
|
698 |
(fn Free (v, _) => Name.declare v | _ => I) ts Name.context; |
|
699 |
val vs = Name.names ctxt "a" tys; |
|
700 |
in |
|
701 |
fold_map (exprgen_typ thy algbr funcgr) tys |
|
26972 | 702 |
##>> exprgen_case thy algbr funcgr thm n cases ((c, ty), ts @ map Free vs) |
24918 | 703 |
#>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t) |
704 |
end |
|
705 |
else if length ts > i then |
|
26972 | 706 |
exprgen_case thy algbr funcgr thm n cases ((c, ty), Library.take (i, ts)) |
707 |
##>> fold_map (exprgen_term thy algbr funcgr thm) (Library.drop (i, ts)) |
|
24918 | 708 |
#>> (fn (t, ts) => t `$$ ts) |
709 |
else |
|
26972 | 710 |
exprgen_case thy algbr funcgr thm n cases ((c, ty), ts) |
24918 | 711 |
end |
26972 | 712 |
| NONE => exprgen_app_default thy algbr funcgr thm ((c, ty), ts); |
24918 | 713 |
|
25969 | 714 |
|
27103 | 715 |
(** generated programs **) |
716 |
||
717 |
(* store *) |
|
718 |
||
719 |
structure Program = CodeDataFun |
|
720 |
( |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
721 |
type T = naming * program; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
722 |
val empty = (empty_naming, Graph.empty); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
723 |
fun purge thy cs (naming, program) = empty (*FIXME: problem: un-declaration of names |
27609 | 724 |
let |
725 |
val cs_exisiting = |
|
28054 | 726 |
map_filter (Code_Name.const_rev thy) (Graph.keys program); |
27609 | 727 |
val dels = (Graph.all_preds program |
28054 | 728 |
o map (Code_Name.const thy) |
27609 | 729 |
o filter (member (op =) cs_exisiting) |
730 |
) cs; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
731 |
in Graph.del_nodes dels program end;*) |
27103 | 732 |
); |
733 |
||
734 |
val cached_program = Program.get; |
|
25969 | 735 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
736 |
fun generate thy funcgr f name = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
737 |
Program.change_yield thy (fn naming_program => (NONE, naming_program) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
738 |
|> f thy (Code.operational_algebra thy) funcgr name |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
739 |
|-> (fn name => fn (_, naming_program) => (name, naming_program))); |
27103 | 740 |
|
741 |
||
742 |
(* program generation *) |
|
743 |
||
744 |
fun consts_program thy cs = |
|
745 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
746 |
fun project_consts cs (naming, program) = |
27103 | 747 |
let |
748 |
val cs_all = Graph.all_succs program cs; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
749 |
in (cs, (naming, Graph.subgraph (member (op =) cs_all) program)) end; |
27103 | 750 |
fun generate_consts thy algebra funcgr = |
751 |
fold_map (ensure_const thy algebra funcgr); |
|
752 |
in |
|
28054 | 753 |
generate thy (Code_Funcgr.make thy cs) generate_consts cs |
27103 | 754 |
|-> project_consts |
755 |
end; |
|
756 |
||
757 |
||
758 |
(* value evaluation *) |
|
25969 | 759 |
|
24918 | 760 |
fun ensure_value thy algbr funcgr t = |
761 |
let |
|
762 |
val ty = fastype_of t; |
|
763 |
val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =) |
|
764 |
o dest_TFree))) t []; |
|
765 |
val stmt_value = |
|
766 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
767 |
##>> exprgen_typ thy algbr funcgr ty |
|
26972 | 768 |
##>> exprgen_term thy algbr funcgr NONE t |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
769 |
#>> (fn ((vs, ty), t) => Fun |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
770 |
(Term.dummy_patternN, ((vs, ty), [(([], t), (Drule.dummy_thm, true))]))); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
771 |
fun term_value (dep, (naming, program1)) = |
25969 | 772 |
let |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
773 |
val Fun (_, ((vs, ty), [(([], t), _)])) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
774 |
Graph.get_node program1 Term.dummy_patternN; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
775 |
val deps = Graph.imm_succs program1 Term.dummy_patternN; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
776 |
val program2 = Graph.del_nodes [Term.dummy_patternN] program1; |
27103 | 777 |
val deps_all = Graph.all_succs program2 deps; |
778 |
val program3 = Graph.subgraph (member (op =) deps_all) program2; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
779 |
in (((naming, program3), (((vs, ty), t), deps)), (dep, (naming, program2))) end; |
26011 | 780 |
in |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
781 |
ensure_stmt ((K o K) NONE) pair stmt_value Term.dummy_patternN |
26011 | 782 |
#> snd |
783 |
#> term_value |
|
784 |
end; |
|
24219 | 785 |
|
27103 | 786 |
fun eval eval_kind thy evaluator = |
787 |
let |
|
788 |
fun evaluator'' evaluator''' funcgr t = |
|
789 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
790 |
val (((naming, program), (vs_ty_t, deps)), _) = generate thy funcgr ensure_value t; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
791 |
in evaluator''' naming program vs_ty_t deps end; |
27103 | 792 |
fun evaluator' t = |
793 |
let |
|
794 |
val (t', evaluator''') = evaluator t; |
|
795 |
in (t', evaluator'' evaluator''') end; |
|
796 |
in eval_kind thy evaluator' end |
|
797 |
||
28054 | 798 |
fun eval_conv thy = eval Code_Funcgr.eval_conv thy; |
799 |
fun eval_term thy = eval Code_Funcgr.eval_term thy; |
|
27103 | 800 |
|
24219 | 801 |
end; (*struct*) |
802 |
||
803 |
||
28054 | 804 |
structure Basic_Code_Thingol: BASIC_CODE_THINGOL = Code_Thingol; |