author | haftmann |
Wed, 26 Feb 2014 11:57:52 +0100 | |
changeset 55757 | 9fc71814b8c1 |
parent 55190 | 81070502914c |
child 55828 | 42ac3cfb89f6 |
permissions | -rw-r--r-- |
37744 | 1 |
(* Title: Tools/Code/code_thingol.ML |
24219 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
4 |
Intermediate language ("Thin-gol") representing executable code. |
|
24918 | 5 |
Representation and translation. |
24219 | 6 |
*) |
7 |
||
8 |
infix 8 `%%; |
|
9 |
infix 4 `$; |
|
10 |
infix 4 `$$; |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
11 |
infixr 3 `->; |
31724 | 12 |
infixr 3 `|=>; |
13 |
infixr 3 `|==>; |
|
24219 | 14 |
|
15 |
signature BASIC_CODE_THINGOL = |
|
16 |
sig |
|
17 |
type vname = string; |
|
18 |
datatype dict = |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
19 |
Dict of (class * class) list * plain_dict |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
20 |
and plain_dict = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
21 |
Dict_Const of (string * class) * dict list list |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
22 |
| Dict_Var of vname * (int * int); |
24219 | 23 |
datatype itype = |
24 |
`%% of string * itype list |
|
25 |
| ITyVar of vname; |
|
55150 | 26 |
type const = { sym: Code_Symbol.T, typargs: itype list, dicts: dict list list, |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
27 |
dom: itype list, range: itype, annotate: bool }; |
24219 | 28 |
datatype iterm = |
24591 | 29 |
IConst of const |
31889 | 30 |
| IVar of vname option |
24219 | 31 |
| `$ of iterm * iterm |
31888 | 32 |
| `|=> of (vname option * itype) * iterm |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
33 |
| ICase of { term: iterm, typ: itype, clauses: (iterm * iterm) list, primitive: iterm }; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
34 |
val `-> : itype * itype -> itype; |
24219 | 35 |
val `$$ : iterm * iterm list -> iterm; |
31888 | 36 |
val `|==> : (vname option * itype) list * iterm -> iterm; |
24219 | 37 |
type typscheme = (vname * sort) list * itype; |
38 |
end; |
|
39 |
||
40 |
signature CODE_THINGOL = |
|
41 |
sig |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
42 |
include BASIC_CODE_THINGOL |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
43 |
val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
44 |
val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
45 |
val unfold_fun: itype -> itype list * itype |
37640 | 46 |
val unfold_fun_n: int -> itype -> itype list * itype |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
47 |
val unfold_app: iterm -> iterm * iterm list |
31888 | 48 |
val unfold_abs: iterm -> (vname option * itype) list * iterm |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
49 |
val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
50 |
val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm |
31889 | 51 |
val split_pat_abs: iterm -> ((iterm * itype) * iterm) option |
52 |
val unfold_pat_abs: iterm -> (iterm * itype) list * iterm |
|
31049 | 53 |
val unfold_const_app: iterm -> (const * iterm list) option |
32909 | 54 |
val is_IVar: iterm -> bool |
41782 | 55 |
val is_IAbs: iterm -> bool |
31049 | 56 |
val eta_expand: int -> const * iterm list -> iterm |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40844
diff
changeset
|
57 |
val contains_dict_var: iterm -> bool |
55150 | 58 |
val add_constsyms: iterm -> Code_Symbol.T list -> Code_Symbol.T list |
32917 | 59 |
val add_tyconames: iterm -> string list -> string list |
28663
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 |
|
24918 | 62 |
datatype stmt = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
63 |
NoStmt |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
64 |
| Fun of (typscheme * ((iterm list * iterm) * (thm option * bool)) list) * thm option |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
65 |
| Datatype of vname list * |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
66 |
((string * vname list (*type argument wrt. canonical order*)) * itype list) list |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
67 |
| Datatypecons of string |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
68 |
| Class of vname * ((class * class) list * (string * itype) list) |
24219 | 69 |
| Classrel of class * class |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
70 |
| Classparam of class |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
71 |
| Classinst of { class: string, tyco: string, vs: (vname * sort) list, |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
72 |
superinsts: (class * dict list list) list, |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
73 |
inst_params: ((string * (const * int)) * (thm * bool)) list, |
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
74 |
superinst_params: ((string * (const * int)) * (thm * bool)) list }; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
75 |
type program = stmt Code_Symbol.Graph.T |
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
76 |
val unimplemented: program -> string list |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
77 |
val implemented_deps: program -> string list |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
78 |
val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
79 |
val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt |
55150 | 80 |
val is_constr: program -> Code_Symbol.T -> bool |
37440
a5d44161ba2a
maintain cong rules for case combinators; more precise permissiveness
haftmann
parents:
37437
diff
changeset
|
81 |
val is_case: stmt -> bool |
55757 | 82 |
val group_stmts: Proof.context -> program |
55150 | 83 |
-> ((Code_Symbol.T * stmt) list * (Code_Symbol.T * stmt) list |
84 |
* ((Code_Symbol.T * stmt) list * (Code_Symbol.T * stmt) list)) list |
|
24219 | 85 |
|
55757 | 86 |
val read_const_exprs: Proof.context -> string list -> string list |
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
87 |
val consts_program: theory -> string list -> program |
55757 | 88 |
val dynamic_conv: Proof.context -> (program |
55150 | 89 |
-> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> conv) |
38672
f1f64375f662
preliminary versions of static_eval_conv(_simple)
haftmann
parents:
38669
diff
changeset
|
90 |
-> conv |
55757 | 91 |
val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'a) -> (program |
55150 | 92 |
-> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> 'a) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
93 |
-> term -> 'a |
55757 | 94 |
val static_conv: Proof.context -> string list -> (program -> string list |
95 |
-> Proof.context -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> conv) |
|
96 |
-> Proof.context -> conv |
|
97 |
val static_conv_simple: Proof.context -> string list |
|
98 |
-> (program -> Proof.context -> (string * sort) list -> term -> conv) |
|
99 |
-> Proof.context -> conv |
|
100 |
val static_value: Proof.context -> ((term -> term) -> 'a -> 'a) -> string list -> |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
101 |
(program -> string list |
55757 | 102 |
-> Proof.context -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> 'a) |
103 |
-> Proof.context -> term -> 'a |
|
24219 | 104 |
end; |
105 |
||
28054 | 106 |
structure Code_Thingol: CODE_THINGOL = |
24219 | 107 |
struct |
108 |
||
55150 | 109 |
open Basic_Code_Symbol; |
110 |
||
24219 | 111 |
(** auxiliary **) |
112 |
||
113 |
fun unfoldl dest x = |
|
114 |
case dest x |
|
115 |
of NONE => (x, []) |
|
116 |
| SOME (x1, x2) => |
|
117 |
let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end; |
|
118 |
||
119 |
fun unfoldr dest x = |
|
120 |
case dest x |
|
121 |
of NONE => ([], x) |
|
122 |
| SOME (x1, x2) => |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
123 |
let val (xs', x') = unfoldr dest x2 in (x1 :: xs', x') end; |
24219 | 124 |
|
125 |
||
29962
bd4dc7fa742d
tuned comments, stripped ID, deleted superfluous code
haftmann
parents:
29952
diff
changeset
|
126 |
(** language core - types, terms **) |
24219 | 127 |
|
128 |
type vname = string; |
|
129 |
||
130 |
datatype dict = |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
131 |
Dict of (class * class) list * plain_dict |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
132 |
and plain_dict = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
133 |
Dict_Const of (string * class) * dict list list |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
134 |
| Dict_Var of vname * (int * int); |
24219 | 135 |
|
136 |
datatype itype = |
|
137 |
`%% of string * itype list |
|
138 |
| ITyVar of vname; |
|
139 |
||
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
140 |
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2]; |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
141 |
|
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
142 |
val unfold_fun = unfoldr |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
143 |
(fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2) |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
144 |
| _ => NONE); |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
145 |
|
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
146 |
fun unfold_fun_n n ty = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
147 |
let |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
148 |
val (tys1, ty1) = unfold_fun ty; |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
149 |
val (tys3, tys2) = chop n tys1; |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
150 |
val ty3 = Library.foldr (op `->) (tys2, ty1); |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
151 |
in (tys3, ty3) end; |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
152 |
|
55150 | 153 |
type const = { sym: Code_Symbol.T, typargs: itype list, dicts: dict list list, |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
154 |
dom: itype list, range: itype, annotate: bool }; |
24591 | 155 |
|
24219 | 156 |
datatype iterm = |
24591 | 157 |
IConst of const |
31889 | 158 |
| IVar of vname option |
24219 | 159 |
| `$ of iterm * iterm |
31888 | 160 |
| `|=> of (vname option * itype) * iterm |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
161 |
| ICase of { term: iterm, typ: itype, clauses: (iterm * iterm) list, primitive: iterm }; |
24219 | 162 |
(*see also signature*) |
163 |
||
32909 | 164 |
fun is_IVar (IVar _) = true |
165 |
| is_IVar _ = false; |
|
166 |
||
41782 | 167 |
fun is_IAbs (_ `|=> _) = true |
168 |
| is_IAbs _ = false; |
|
169 |
||
24219 | 170 |
val op `$$ = Library.foldl (op `$); |
31724 | 171 |
val op `|==> = Library.foldr (op `|=>); |
24219 | 172 |
|
173 |
val unfold_app = unfoldl |
|
174 |
(fn op `$ t => SOME t |
|
175 |
| _ => NONE); |
|
176 |
||
31874 | 177 |
val unfold_abs = unfoldr |
178 |
(fn op `|=> t => SOME t |
|
24219 | 179 |
| _ => NONE); |
180 |
||
181 |
val split_let = |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
182 |
(fn ICase { term = t, typ = ty, clauses = [(p, body)], ... } => SOME (((p, ty), t), body) |
24219 | 183 |
| _ => NONE); |
184 |
||
185 |
val unfold_let = unfoldr split_let; |
|
186 |
||
187 |
fun unfold_const_app t = |
|
188 |
case unfold_app t |
|
189 |
of (IConst c, ts) => SOME (c, ts) |
|
190 |
| _ => NONE; |
|
191 |
||
32917 | 192 |
fun fold_constexprs f = |
193 |
let |
|
194 |
fun fold' (IConst c) = f c |
|
195 |
| fold' (IVar _) = I |
|
196 |
| fold' (t1 `$ t2) = fold' t1 #> fold' t2 |
|
197 |
| fold' (_ `|=> t) = fold' t |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
198 |
| fold' (ICase { term = t, clauses = clauses, ... }) = fold' t |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
199 |
#> fold (fn (p, body) => fold' p #> fold' body) clauses |
32917 | 200 |
in fold' end; |
201 |
||
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
202 |
val add_constsyms = fold_constexprs (fn { sym, ... } => insert (op =) sym); |
32917 | 203 |
|
204 |
fun add_tycos (tyco `%% tys) = insert (op =) tyco #> fold add_tycos tys |
|
205 |
| add_tycos (ITyVar _) = I; |
|
206 |
||
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
207 |
val add_tyconames = fold_constexprs (fn { typargs = tys, ... } => fold add_tycos tys); |
24219 | 208 |
|
209 |
fun fold_varnames f = |
|
210 |
let |
|
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
211 |
fun fold_aux add f = |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
212 |
let |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
213 |
fun fold_term _ (IConst _) = I |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
214 |
| fold_term vs (IVar (SOME v)) = if member (op =) vs v then I else f v |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
215 |
| fold_term _ (IVar NONE) = I |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
216 |
| fold_term vs (t1 `$ t2) = fold_term vs t1 #> fold_term vs t2 |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
217 |
| fold_term vs ((SOME v, _) `|=> t) = fold_term (insert (op =) v vs) t |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
218 |
| fold_term vs ((NONE, _) `|=> t) = fold_term vs t |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
219 |
| fold_term vs (ICase { term = t, clauses = clauses, ... }) = fold_term vs t #> fold (fold_case vs) clauses |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
220 |
and fold_case vs (p, t) = fold_term (add p vs) t; |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
221 |
in fold_term [] end; |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
222 |
fun add t = fold_aux add (insert (op =)) t; |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
223 |
in fold_aux add f end; |
24219 | 224 |
|
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
225 |
fun exists_var t v = fold_varnames (fn w => fn b => v = w orelse b) t false; |
31874 | 226 |
|
31889 | 227 |
fun split_pat_abs ((NONE, ty) `|=> t) = SOME ((IVar NONE, ty), t) |
31888 | 228 |
| split_pat_abs ((SOME v, ty) `|=> t) = SOME (case t |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
229 |
of ICase { term = IVar (SOME w), clauses = [(p, body)], ... } => |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
230 |
if v = w andalso (exists_var p v orelse not (exists_var body v)) |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
231 |
then ((p, ty), body) |
31889 | 232 |
else ((IVar (SOME v), ty), t) |
233 |
| _ => ((IVar (SOME v), ty), t)) |
|
31888 | 234 |
| split_pat_abs _ = NONE; |
31874 | 235 |
|
236 |
val unfold_pat_abs = unfoldr split_pat_abs; |
|
24219 | 237 |
|
31890
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
238 |
fun unfold_abs_eta [] t = ([], t) |
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
239 |
| unfold_abs_eta (_ :: tys) (v_ty `|=> t) = |
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
240 |
let |
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
241 |
val (vs_tys, t') = unfold_abs_eta tys t; |
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
242 |
in (v_ty :: vs_tys, t') end |
31892 | 243 |
| unfold_abs_eta tys t = |
31890
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
244 |
let |
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
245 |
val ctxt = fold_varnames Name.declare t Name.context; |
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
246 |
val vs_tys = (map o apfst) SOME (Name.invent_names ctxt "a" tys); |
31890
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
247 |
in (vs_tys, t `$$ map (IVar o fst) vs_tys) end; |
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31889
diff
changeset
|
248 |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
249 |
fun eta_expand k (const as { dom = tys, ... }, ts) = |
24219 | 250 |
let |
251 |
val j = length ts; |
|
252 |
val l = k - j; |
|
37841 | 253 |
val _ = if l > length tys |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
254 |
then error "Impossible eta-expansion" else (); |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
255 |
val vars = (fold o fold_varnames) Name.declare ts Name.context; |
31889 | 256 |
val vs_tys = (map o apfst) SOME |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
257 |
(Name.invent_names vars "a" ((take l o drop j) tys)); |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
258 |
in vs_tys `|==> IConst const `$$ ts @ map (IVar o fst) vs_tys end; |
24219 | 259 |
|
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40844
diff
changeset
|
260 |
fun contains_dict_var t = |
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24591
diff
changeset
|
261 |
let |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
262 |
fun cont_dict (Dict (_, d)) = cont_plain_dict d |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
263 |
and cont_plain_dict (Dict_Const (_, dss)) = (exists o exists) cont_dict dss |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
264 |
| cont_plain_dict (Dict_Var _) = true; |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
265 |
fun cont_term (IConst { dicts = dss, ... }) = (exists o exists) cont_dict dss |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
266 |
| cont_term (IVar _) = false |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
267 |
| cont_term (t1 `$ t2) = cont_term t1 orelse cont_term t2 |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
268 |
| cont_term (_ `|=> t) = cont_term t |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
269 |
| cont_term (ICase { primitive = t, ... }) = cont_term t; |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
270 |
in cont_term t end; |
25621 | 271 |
|
272 |
||
27103 | 273 |
(** statements, abstract programs **) |
24219 | 274 |
|
275 |
type typscheme = (vname * sort) list * itype; |
|
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
276 |
datatype stmt = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
277 |
NoStmt |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
278 |
| Fun of (typscheme * ((iterm list * iterm) * (thm option * bool)) list) * thm option |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
279 |
| Datatype of vname list * ((string * vname list) * itype list) list |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
280 |
| Datatypecons of string |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
281 |
| Class of vname * ((class * class) list * (string * itype) list) |
24219 | 282 |
| Classrel of class * class |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
283 |
| Classparam of class |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
284 |
| Classinst of { class: string, tyco: string, vs: (vname * sort) list, |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
285 |
superinsts: (class * dict list list) list, |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
286 |
inst_params: ((string * (const * int)) * (thm * bool)) list, |
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
287 |
superinst_params: ((string * (const * int)) * (thm * bool)) list }; |
24219 | 288 |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
289 |
type program = stmt Code_Symbol.Graph.T; |
24219 | 290 |
|
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
291 |
fun unimplemented program = |
55150 | 292 |
Code_Symbol.Graph.fold (fn (Constant c, (NoStmt, _)) => cons c | _ => I) program []; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
293 |
|
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
294 |
fun implemented_deps program = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
295 |
Code_Symbol.Graph.keys program |
55150 | 296 |
|> subtract (op =) (Code_Symbol.Graph.all_preds program (map Constant (unimplemented program))) |
297 |
|> map_filter (fn Constant c => SOME c | _ => NONE); |
|
24219 | 298 |
|
27711 | 299 |
fun map_terms_bottom_up f (t as IConst _) = f t |
300 |
| map_terms_bottom_up f (t as IVar _) = f t |
|
301 |
| map_terms_bottom_up f (t1 `$ t2) = f |
|
302 |
(map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2) |
|
31724 | 303 |
| map_terms_bottom_up f ((v, ty) `|=> t) = f |
304 |
((v, ty) `|=> map_terms_bottom_up f t) |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
305 |
| map_terms_bottom_up f (ICase { term = t, typ = ty, clauses = clauses, primitive = t0 }) = f |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
306 |
(ICase { term = map_terms_bottom_up f t, typ = ty, |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
307 |
clauses = (map o pairself) (map_terms_bottom_up f) clauses, |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
308 |
primitive = map_terms_bottom_up f t0 }); |
27711 | 309 |
|
37448
3bd4b3809bee
explicit type variable arguments for constructors
haftmann
parents:
37447
diff
changeset
|
310 |
fun map_classparam_instances_as_term f = |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
311 |
(map o apfst o apsnd o apfst) (fn const => case f (IConst const) of IConst const' => const') |
37448
3bd4b3809bee
explicit type variable arguments for constructors
haftmann
parents:
37447
diff
changeset
|
312 |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
313 |
fun map_terms_stmt f NoStmt = NoStmt |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
314 |
| map_terms_stmt f (Fun ((tysm, eqs), case_cong)) = Fun ((tysm, (map o apfst) |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
315 |
(fn (ts, t) => (map f ts, f t)) eqs), case_cong) |
27711 | 316 |
| map_terms_stmt f (stmt as Datatype _) = stmt |
317 |
| map_terms_stmt f (stmt as Datatypecons _) = stmt |
|
318 |
| map_terms_stmt f (stmt as Class _) = stmt |
|
319 |
| map_terms_stmt f (stmt as Classrel _) = stmt |
|
320 |
| map_terms_stmt f (stmt as Classparam _) = stmt |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
321 |
| map_terms_stmt f (Classinst { class, tyco, vs, superinsts, |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
322 |
inst_params, superinst_params }) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
323 |
Classinst { class = class, tyco = tyco, vs = vs, superinsts = superinsts, |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
324 |
inst_params = map_classparam_instances_as_term f inst_params, |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
325 |
superinst_params = map_classparam_instances_as_term f superinst_params }; |
27711 | 326 |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
327 |
fun is_constr program sym = case Code_Symbol.Graph.get_node program sym |
24219 | 328 |
of Datatypecons _ => true |
329 |
| _ => false; |
|
330 |
||
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
331 |
fun is_case (Fun (_, SOME _)) = true |
37440
a5d44161ba2a
maintain cong rules for case combinators; more precise permissiveness
haftmann
parents:
37437
diff
changeset
|
332 |
| is_case _ = false; |
a5d44161ba2a
maintain cong rules for case combinators; more precise permissiveness
haftmann
parents:
37437
diff
changeset
|
333 |
|
a5d44161ba2a
maintain cong rules for case combinators; more precise permissiveness
haftmann
parents:
37437
diff
changeset
|
334 |
fun linear_stmts program = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
335 |
rev (Code_Symbol.Graph.strong_conn program) |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
336 |
|> map (AList.make (Code_Symbol.Graph.get_node program)); |
37440
a5d44161ba2a
maintain cong rules for case combinators; more precise permissiveness
haftmann
parents:
37437
diff
changeset
|
337 |
|
55757 | 338 |
fun group_stmts ctxt program = |
32895 | 339 |
let |
340 |
fun is_fun (_, Fun _) = true | is_fun _ = false; |
|
341 |
fun is_datatypecons (_, Datatypecons _) = true | is_datatypecons _ = false; |
|
342 |
fun is_datatype (_, Datatype _) = true | is_datatype _ = false; |
|
343 |
fun is_class (_, Class _) = true | is_class _ = false; |
|
344 |
fun is_classrel (_, Classrel _) = true | is_classrel _ = false; |
|
345 |
fun is_classparam (_, Classparam _) = true | is_classparam _ = false; |
|
346 |
fun is_classinst (_, Classinst _) = true | is_classinst _ = false; |
|
347 |
fun group stmts = |
|
348 |
if forall (is_datatypecons orf is_datatype) stmts |
|
349 |
then (filter is_datatype stmts, [], ([], [])) |
|
350 |
else if forall (is_class orf is_classrel orf is_classparam) stmts |
|
351 |
then ([], filter is_class stmts, ([], [])) |
|
352 |
else if forall (is_fun orf is_classinst) stmts |
|
353 |
then ([], [], List.partition is_fun stmts) |
|
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
51685
diff
changeset
|
354 |
else error ("Illegal mutual dependencies: " ^ (commas |
55757 | 355 |
o map (Code_Symbol.quote ctxt o fst)) stmts); |
32895 | 356 |
in |
37440
a5d44161ba2a
maintain cong rules for case combinators; more precise permissiveness
haftmann
parents:
37437
diff
changeset
|
357 |
linear_stmts program |
32895 | 358 |
|> map group |
359 |
end; |
|
360 |
||
24219 | 361 |
|
27103 | 362 |
(** translation kernel **) |
24219 | 363 |
|
28724 | 364 |
(* generic mechanisms *) |
365 |
||
55190 | 366 |
fun ensure_stmt symbolize generate x (deps, program) = |
24219 | 367 |
let |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
368 |
val sym = symbolize x; |
55190 | 369 |
val add_dep = case deps of [] => I |
370 |
| dep :: _ => Code_Symbol.Graph.add_edge (dep, sym); |
|
47576 | 371 |
in |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
372 |
if can (Code_Symbol.Graph.get_node program) sym |
47576 | 373 |
then |
374 |
program |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
375 |
|> add_dep |
55190 | 376 |
|> pair deps |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
377 |
|> pair x |
47576 | 378 |
else |
379 |
program |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
380 |
|> Code_Symbol.Graph.default_node (sym, NoStmt) |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
381 |
|> add_dep |
55190 | 382 |
|> curry generate (sym :: deps) |
47576 | 383 |
||> snd |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
384 |
|-> (fn stmt => (Code_Symbol.Graph.map_node sym) (K stmt)) |
55190 | 385 |
|> pair deps |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
386 |
|> pair x |
24219 | 387 |
end; |
388 |
||
36272
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
389 |
exception PERMISSIVE of unit; |
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
390 |
|
55757 | 391 |
fun translation_error ctxt program permissive some_thm deps msg sub_msg = |
36272
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
392 |
if permissive |
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
393 |
then raise PERMISSIVE () |
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
394 |
else |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
395 |
let |
55190 | 396 |
val thm_msg = |
397 |
Option.map (fn thm => "in code equation " ^ Display.string_of_thm ctxt thm) some_thm; |
|
398 |
val dep_msg = if null (tl deps) then NONE |
|
399 |
else SOME ("with dependency " |
|
400 |
^ space_implode " -> " (map (Code_Symbol.quote ctxt) (rev deps))); |
|
401 |
val thm_dep_msg = case (thm_msg, dep_msg) |
|
402 |
of (SOME thm_msg, SOME dep_msg) => "\n(" ^ thm_msg ^ ",\n" ^ dep_msg ^ ")" |
|
403 |
| (SOME thm_msg, NONE) => "\n(" ^ thm_msg ^ ")" |
|
404 |
| (NONE, SOME dep_msg) => "\n(" ^ dep_msg ^ ")" |
|
405 |
| (NONE, NONE) => "" |
|
406 |
in error (msg ^ thm_dep_msg ^ ":\n" ^ sub_msg) end; |
|
37698 | 407 |
|
48074 | 408 |
fun maybe_permissive f prgrm = |
409 |
f prgrm |>> SOME handle PERMISSIVE () => (NONE, prgrm); |
|
410 |
||
55757 | 411 |
fun not_wellsorted ctxt program permissive some_thm deps ty sort e = |
37698 | 412 |
let |
55757 | 413 |
val err_class = Sorts.class_error (Context.pretty ctxt) e; |
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
414 |
val err_typ = |
55757 | 415 |
"Type " ^ Syntax.string_of_typ ctxt ty ^ " not of sort " ^ |
416 |
Syntax.string_of_sort ctxt sort; |
|
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
417 |
in |
55757 | 418 |
translation_error ctxt program permissive some_thm deps |
55190 | 419 |
"Wellsortedness error" (err_typ ^ "\n" ^ err_class) |
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
420 |
end; |
26972 | 421 |
|
47555 | 422 |
|
44790
c13fdf710a40
adding type inference for disambiguation annotations in code equation
bulwahn
parents:
44789
diff
changeset
|
423 |
(* inference of type annotations for disambiguation with type classes *) |
c13fdf710a40
adding type inference for disambiguation annotations in code equation
bulwahn
parents:
44789
diff
changeset
|
424 |
|
45000
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
425 |
fun mk_tagged_type (true, T) = Type ("", [T]) |
47555 | 426 |
| mk_tagged_type (false, T) = T; |
45000
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
427 |
|
44998
f12ef61ea76e
determining the fastype of a case-pattern but ignoring dummy type constructors that were added as markers for type annotations
bulwahn
parents:
44997
diff
changeset
|
428 |
fun dest_tagged_type (Type ("", [T])) = (true, T) |
47555 | 429 |
| dest_tagged_type T = (false, T); |
44998
f12ef61ea76e
determining the fastype of a case-pattern but ignoring dummy type constructors that were added as markers for type annotations
bulwahn
parents:
44997
diff
changeset
|
430 |
|
47555 | 431 |
val untag_term = map_types (snd o dest_tagged_type); |
44998
f12ef61ea76e
determining the fastype of a case-pattern but ignoring dummy type constructors that were added as markers for type annotations
bulwahn
parents:
44997
diff
changeset
|
432 |
|
45000
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
433 |
fun tag_term (proj_sort, _) eqngr = |
44997 | 434 |
let |
47555 | 435 |
val has_sort_constraints = exists (not o null) o map proj_sort o Code_Preproc.sortargs eqngr; |
47576 | 436 |
fun tag (Const (_, T')) (Const (c, T)) = |
45000
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
437 |
Const (c, |
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
438 |
mk_tagged_type (not (null (Term.add_tvarsT T' [])) andalso has_sort_constraints c, T)) |
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
439 |
| tag (t1 $ u1) (t $ u) = tag t1 t $ tag u1 u |
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
440 |
| tag (Abs (_, _, t1)) (Abs (x, T, t)) = Abs (x, T, tag t1 t) |
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
441 |
| tag (Free _) (t as Free _) = t |
0febe2089425
adding abstraction layer; more precise function names
bulwahn
parents:
44999
diff
changeset
|
442 |
| tag (Var _) (t as Var _) = t |
47555 | 443 |
| tag (Bound _) (t as Bound _) = t; |
55757 | 444 |
in tag end |
44790
c13fdf710a40
adding type inference for disambiguation annotations in code equation
bulwahn
parents:
44789
diff
changeset
|
445 |
|
55757 | 446 |
fun annotate ctxt algbr eqngr (c, ty) args rhs = |
44790
c13fdf710a40
adding type inference for disambiguation annotations in code equation
bulwahn
parents:
44789
diff
changeset
|
447 |
let |
55757 | 448 |
val erase = map_types (fn _ => Type_Infer.anyT []); |
449 |
val reinfer = singleton (Type_Infer_Context.infer_types ctxt); |
|
450 |
val lhs = list_comb (Const (c, ty), map (map_types Type.strip_sorts o fst) args); |
|
451 |
val reinferred_rhs = snd (Logic.dest_equals (reinfer (Logic.mk_equals (lhs, erase rhs)))); |
|
452 |
in tag_term algbr eqngr reinferred_rhs rhs end |
|
453 |
||
454 |
fun annotate_eqns ctxt algbr eqngr (c, ty) eqns = |
|
455 |
let |
|
456 |
val ctxt' = ctxt |> Proof_Context.theory_of |> Proof_Context.init_global |
|
457 |
|> Config.put Type_Infer_Context.const_sorts false; |
|
458 |
(*avoid spurious fixed variables: there is no eigen context for equations*) |
|
44790
c13fdf710a40
adding type inference for disambiguation annotations in code equation
bulwahn
parents:
44789
diff
changeset
|
459 |
in |
55757 | 460 |
map (apfst (fn (args, (rhs, some_abs)) => (args, |
461 |
(annotate ctxt' algbr eqngr (c, ty) args rhs, some_abs)))) eqns |
|
462 |
end; |
|
47555 | 463 |
|
55189 | 464 |
(* abstract dictionary construction *) |
465 |
||
466 |
datatype typarg_witness = |
|
467 |
Weakening of (class * class) list * plain_typarg_witness |
|
468 |
and plain_typarg_witness = |
|
469 |
Global of (string * class) * typarg_witness list list |
|
470 |
| Local of string * (int * sort); |
|
471 |
||
55757 | 472 |
fun construct_dictionaries ctxt (proj_sort, algebra) permissive some_thm (ty, sort) (deps, program) = |
55189 | 473 |
let |
474 |
fun class_relation ((Weakening (classrels, x)), sub_class) super_class = |
|
475 |
Weakening ((sub_class, super_class) :: classrels, x); |
|
476 |
fun type_constructor (tyco, _) dss class = |
|
477 |
Weakening ([], Global ((tyco, class), (map o map) fst dss)); |
|
478 |
fun type_variable (TFree (v, sort)) = |
|
479 |
let |
|
480 |
val sort' = proj_sort sort; |
|
481 |
in map_index (fn (n, class) => (Weakening ([], Local (v, (n, sort'))), class)) sort' end; |
|
482 |
val typarg_witnesses = Sorts.of_sort_derivation algebra |
|
483 |
{class_relation = K (Sorts.classrel_derivation algebra class_relation), |
|
484 |
type_constructor = type_constructor, |
|
485 |
type_variable = type_variable} (ty, proj_sort sort) |
|
55757 | 486 |
handle Sorts.CLASS_ERROR e => not_wellsorted ctxt program permissive some_thm deps ty sort e; |
55190 | 487 |
in (typarg_witnesses, (deps, program)) end; |
55189 | 488 |
|
489 |
||
28724 | 490 |
(* translation *) |
491 |
||
55757 | 492 |
fun ensure_tyco ctxt algbr eqngr permissive tyco = |
30932 | 493 |
let |
55757 | 494 |
val thy = Proof_Context.theory_of ctxt; |
40726
16dcfedc4eb7
keep type variable arguments of datatype constructors in bookkeeping
haftmann
parents:
40711
diff
changeset
|
495 |
val ((vs, cos), _) = Code.get_type thy tyco; |
30932 | 496 |
val stmt_datatype = |
55757 | 497 |
fold_map (translate_tyvar_sort ctxt algbr eqngr permissive) vs |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47576
diff
changeset
|
498 |
#>> map fst |
40726
16dcfedc4eb7
keep type variable arguments of datatype constructors in bookkeeping
haftmann
parents:
40711
diff
changeset
|
499 |
##>> fold_map (fn (c, (vs, tys)) => |
55757 | 500 |
ensure_const ctxt algbr eqngr permissive c |
40726
16dcfedc4eb7
keep type variable arguments of datatype constructors in bookkeeping
haftmann
parents:
40711
diff
changeset
|
501 |
##>> pair (map (unprefix "'" o fst) vs) |
55757 | 502 |
##>> fold_map (translate_typ ctxt algbr eqngr permissive) tys) cos |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
503 |
#>> Datatype; |
55150 | 504 |
in ensure_stmt Type_Constructor stmt_datatype tyco end |
55757 | 505 |
and ensure_const ctxt algbr eqngr permissive c = |
30932 | 506 |
let |
55757 | 507 |
val thy = Proof_Context.theory_of ctxt; |
30932 | 508 |
fun stmt_datatypecons tyco = |
55757 | 509 |
ensure_tyco ctxt algbr eqngr permissive tyco |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
510 |
#>> Datatypecons; |
30932 | 511 |
fun stmt_classparam class = |
55757 | 512 |
ensure_class ctxt algbr eqngr permissive class |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
513 |
#>> Classparam; |
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
514 |
fun stmt_fun cert = case Code.equations_of_cert thy cert |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
515 |
of (_, NONE) => pair NoStmt |
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
516 |
| ((vs, ty), SOME eqns) => |
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
517 |
let |
55757 | 518 |
val eqns' = annotate_eqns ctxt algbr eqngr (c, ty) eqns |
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
519 |
val some_case_cong = Code.get_case_cong thy c; |
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
520 |
in |
55757 | 521 |
fold_map (translate_tyvar_sort ctxt algbr eqngr permissive) vs |
522 |
##>> translate_typ ctxt algbr eqngr permissive ty |
|
523 |
##>> translate_eqns ctxt algbr eqngr permissive eqns' |
|
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
524 |
#>> |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
525 |
(fn (_, NONE) => NoStmt |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
526 |
| (tyscm, SOME eqns) => Fun ((tyscm, eqns), some_case_cong)) |
54889
4121d64fde90
explicit distinction between empty code equations and no code equations, including convenient declaration attributes
haftmann
parents:
52801
diff
changeset
|
527 |
end; |
35299
4f4d5bf4ea08
proper distinction of code datatypes and abstypes
haftmann
parents:
35226
diff
changeset
|
528 |
val stmt_const = case Code.get_type_of_constr_or_abstr thy c |
35226 | 529 |
of SOME (tyco, _) => stmt_datatypecons tyco |
51685
385ef6706252
more standard module name Axclass (according to file name);
wenzelm
parents:
51658
diff
changeset
|
530 |
| NONE => (case Axclass.class_of_param thy c |
30932 | 531 |
of SOME class => stmt_classparam class |
34891
99b9a6290446
code certificates as integral part of code generation
haftmann
parents:
34251
diff
changeset
|
532 |
| NONE => stmt_fun (Code_Preproc.cert eqngr c)) |
55150 | 533 |
in ensure_stmt Constant stmt_const c end |
55757 | 534 |
and ensure_class ctxt (algbr as (_, algebra)) eqngr permissive class = |
24918 | 535 |
let |
55757 | 536 |
val thy = Proof_Context.theory_of ctxt; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
537 |
val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class; |
51685
385ef6706252
more standard module name Axclass (according to file name);
wenzelm
parents:
51658
diff
changeset
|
538 |
val cs = #params (Axclass.get_info thy class); |
24918 | 539 |
val stmt_class = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
540 |
fold_map (fn super_class => |
55757 | 541 |
ensure_classrel ctxt algbr eqngr permissive (class, super_class)) super_classes |
542 |
##>> fold_map (fn (c, ty) => ensure_const ctxt algbr eqngr permissive c |
|
543 |
##>> translate_typ ctxt algbr eqngr permissive ty) cs |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
544 |
#>> (fn info => Class (unprefix "'" Name.aT, info)) |
55150 | 545 |
in ensure_stmt Type_Class stmt_class class end |
55757 | 546 |
and ensure_classrel ctxt algbr eqngr permissive (sub_class, super_class) = |
24918 | 547 |
let |
548 |
val stmt_classrel = |
|
55757 | 549 |
ensure_class ctxt algbr eqngr permissive sub_class |
550 |
##>> ensure_class ctxt algbr eqngr permissive super_class |
|
24918 | 551 |
#>> Classrel; |
55150 | 552 |
in ensure_stmt Class_Relation stmt_classrel (sub_class, super_class) end |
55757 | 553 |
and ensure_inst ctxt (algbr as (_, algebra)) eqngr permissive (tyco, class) = |
24918 | 554 |
let |
55757 | 555 |
val thy = Proof_Context.theory_of ctxt; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
556 |
val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class; |
51685
385ef6706252
more standard module name Axclass (according to file name);
wenzelm
parents:
51658
diff
changeset
|
557 |
val these_class_params = these o try (#params o Axclass.get_info thy); |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
558 |
val class_params = these_class_params class; |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
559 |
val superclass_params = maps these_class_params |
37448
3bd4b3809bee
explicit type variable arguments for constructors
haftmann
parents:
37447
diff
changeset
|
560 |
((Sorts.complete_sort algebra o Sorts.super_classes algebra) class); |
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
561 |
val vs = Name.invent_names Name.context "'a" (Sorts.mg_domain algebra tyco [class]); |
24918 | 562 |
val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class]; |
563 |
val vs' = map2 (fn (v, sort1) => fn sort2 => (v, |
|
564 |
Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts'; |
|
565 |
val arity_typ = Type (tyco, map TFree vs); |
|
566 |
val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs'); |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
567 |
fun translate_super_instance super_class = |
55757 | 568 |
ensure_class ctxt algbr eqngr permissive super_class |
569 |
##>> translate_dicts ctxt algbr eqngr permissive NONE (arity_typ, [super_class]) |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
570 |
#>> (fn (super_class, [Dict ([], Dict_Const (_, dss))]) => (super_class, dss)); |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
571 |
fun translate_classparam_instance (c, ty) = |
24918 | 572 |
let |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
573 |
val raw_const = Const (c, map_type_tfree (K arity_typ') ty); |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
574 |
val dom_length = length (fst (strip_type ty)) |
51685
385ef6706252
more standard module name Axclass (according to file name);
wenzelm
parents:
51658
diff
changeset
|
575 |
val thm = Axclass.unoverload_conv thy (Thm.cterm_of thy raw_const); |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
576 |
val const = (apsnd Logic.unvarifyT_global o dest_Const o snd |
24918 | 577 |
o Logic.dest_equals o Thm.prop_of) thm; |
578 |
in |
|
55757 | 579 |
ensure_const ctxt algbr eqngr permissive c |
580 |
##>> translate_const ctxt algbr eqngr permissive (SOME thm) (const, NONE) |
|
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52161
diff
changeset
|
581 |
#>> (fn (c, IConst const') => ((c, (const', dom_length)), (thm, true))) |
24918 | 582 |
end; |
583 |
val stmt_inst = |
|
55757 | 584 |
ensure_class ctxt algbr eqngr permissive class |
585 |
##>> ensure_tyco ctxt algbr eqngr permissive tyco |
|
586 |
##>> fold_map (translate_tyvar_sort ctxt algbr eqngr permissive) vs |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37216
diff
changeset
|
587 |
##>> fold_map translate_super_instance super_classes |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
588 |
##>> fold_map translate_classparam_instance class_params |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
589 |
##>> fold_map translate_classparam_instance superclass_params |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
590 |
#>> (fn (((((class, tyco), vs), superinsts), inst_params), superinst_params) => |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
591 |
Classinst { class = class, tyco = tyco, vs = vs, |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
592 |
superinsts = superinsts, inst_params = inst_params, superinst_params = superinst_params }); |
55150 | 593 |
in ensure_stmt Class_Instance stmt_inst (tyco, class) end |
55757 | 594 |
and translate_typ ctxt algbr eqngr permissive (TFree (v, _)) = |
30932 | 595 |
pair (ITyVar (unprefix "'" v)) |
55757 | 596 |
| translate_typ ctxt algbr eqngr permissive (Type (tyco, tys)) = |
597 |
ensure_tyco ctxt algbr eqngr permissive tyco |
|
598 |
##>> fold_map (translate_typ ctxt algbr eqngr permissive) tys |
|
30932 | 599 |
#>> (fn (tyco, tys) => tyco `%% tys) |
55757 | 600 |
and translate_term ctxt algbr eqngr permissive some_thm (Const (c, ty), some_abs) = |
601 |
translate_app ctxt algbr eqngr permissive some_thm (((c, ty), []), some_abs) |
|
602 |
| translate_term ctxt algbr eqngr permissive some_thm (Free (v, _), some_abs) = |
|
31889 | 603 |
pair (IVar (SOME v)) |
55757 | 604 |
| translate_term ctxt algbr eqngr permissive some_thm (Abs (v, ty, t), some_abs) = |
24918 | 605 |
let |
42284 | 606 |
val (v', t') = Syntax_Trans.variant_abs (Name.desymbolize false v, ty, t); |
32273 | 607 |
val v'' = if member (op =) (Term.add_free_names t' []) v' |
608 |
then SOME v' else NONE |
|
24918 | 609 |
in |
55757 | 610 |
translate_typ ctxt algbr eqngr permissive ty |
611 |
##>> translate_term ctxt algbr eqngr permissive some_thm (t', some_abs) |
|
32273 | 612 |
#>> (fn (ty, t) => (v'', ty) `|=> t) |
24918 | 613 |
end |
55757 | 614 |
| translate_term ctxt algbr eqngr permissive some_thm (t as _ $ _, some_abs) = |
24918 | 615 |
case strip_comb t |
616 |
of (Const (c, ty), ts) => |
|
55757 | 617 |
translate_app ctxt algbr eqngr permissive some_thm (((c, ty), ts), some_abs) |
24918 | 618 |
| (t', ts) => |
55757 | 619 |
translate_term ctxt algbr eqngr permissive some_thm (t', some_abs) |
620 |
##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
|
24918 | 621 |
#>> (fn (t, ts) => t `$$ ts) |
55757 | 622 |
and translate_eqn ctxt algbr eqngr permissive ((args, (rhs, some_abs)), (some_thm, proper)) = |
623 |
fold_map (translate_term ctxt algbr eqngr permissive some_thm) args |
|
624 |
##>> translate_term ctxt algbr eqngr permissive some_thm (rhs, some_abs) |
|
35226 | 625 |
#>> rpair (some_thm, proper) |
55757 | 626 |
and translate_eqns ctxt algbr eqngr permissive eqns = |
627 |
maybe_permissive (fold_map (translate_eqn ctxt algbr eqngr permissive) eqns) |
|
628 |
and translate_const ctxt algbr eqngr permissive some_thm ((c, ty), some_abs) (deps, program) = |
|
30932 | 629 |
let |
55757 | 630 |
val thy = Proof_Context.theory_of ctxt; |
37698 | 631 |
val _ = if (case some_abs of NONE => true | SOME abs => not (c = abs)) |
35226 | 632 |
andalso Code.is_abstr thy c |
55757 | 633 |
then translation_error ctxt program permissive some_thm deps |
37698 | 634 |
"Abstraction violation" ("constant " ^ Code.string_of_const thy c) |
635 |
else () |
|
55757 | 636 |
in translate_const_proper ctxt algbr eqngr permissive some_thm (c, ty) (deps, program) end |
637 |
and translate_const_proper ctxt algbr eqngr permissive some_thm (c, ty) = |
|
55190 | 638 |
let |
55757 | 639 |
val thy = Proof_Context.theory_of ctxt; |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
640 |
val (annotate, ty') = dest_tagged_type ty; |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
641 |
val typargs = Sign.const_typargs thy (c, ty'); |
32873 | 642 |
val sorts = Code_Preproc.sortargs eqngr c; |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
643 |
val (dom, range) = Term.strip_type ty'; |
26972 | 644 |
in |
55757 | 645 |
ensure_const ctxt algbr eqngr permissive c |
646 |
##>> fold_map (translate_typ ctxt algbr eqngr permissive) typargs |
|
647 |
##>> fold_map (translate_dicts ctxt algbr eqngr permissive some_thm) (typargs ~~ sorts) |
|
648 |
##>> fold_map (translate_typ ctxt algbr eqngr permissive) (range :: dom) |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
649 |
#>> (fn (((c, typargs), dss), range :: dom) => |
55150 | 650 |
IConst { sym = Constant c, typargs = typargs, dicts = dss, |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
651 |
dom = dom, range = range, annotate = annotate }) |
26972 | 652 |
end |
55757 | 653 |
and translate_app_const ctxt algbr eqngr permissive some_thm ((c_ty, ts), some_abs) = |
654 |
translate_const ctxt algbr eqngr permissive some_thm (c_ty, some_abs) |
|
655 |
##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
|
24918 | 656 |
#>> (fn (t, ts) => t `$$ ts) |
55757 | 657 |
and translate_case ctxt algbr eqngr permissive some_thm (num_args, (t_pos, case_pats)) (c_ty, ts) = |
24918 | 658 |
let |
55757 | 659 |
val thy = Proof_Context.theory_of ctxt; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
660 |
val undefineds = Code.undefineds thy; |
40844 | 661 |
fun arg_types num_args ty = fst (chop num_args (binder_types ty)); |
31892 | 662 |
val tys = arg_types num_args (snd c_ty); |
29952
9aed85067721
unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents:
29277
diff
changeset
|
663 |
val ty = nth tys t_pos; |
47437
4625ee486ff6
generalise case certificates to allow ignored parameters
Andreas Lochbihler
parents:
47005
diff
changeset
|
664 |
fun mk_constr NONE t = NONE |
47555 | 665 |
| mk_constr (SOME c) t = |
666 |
let |
|
667 |
val n = Code.args_number thy c; |
|
668 |
in SOME ((c, arg_types n (fastype_of (untag_term t)) ---> ty), n) end; |
|
669 |
val constrs = |
|
670 |
if null case_pats then [] |
|
671 |
else map_filter I (map2 mk_constr case_pats (nth_drop t_pos ts)); |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
672 |
fun casify constrs ty t_app ts = |
24918 | 673 |
let |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
674 |
fun collapse_clause vs_map ts body = |
48074 | 675 |
case body |
55150 | 676 |
of IConst { sym = Constant c, ... } => if member (op =) undefineds c |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
677 |
then [] |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
678 |
else [(ts, body)] |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
679 |
| ICase { term = IVar (SOME v), clauses = clauses, ... } => |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
680 |
if forall (fn (pat', body') => exists_var pat' v |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
681 |
orelse not (exists_var body' v)) clauses |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
682 |
then case AList.lookup (op =) vs_map v |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
683 |
of SOME i => maps (fn (pat', body') => |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
684 |
collapse_clause (AList.delete (op =) v vs_map) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
685 |
(nth_map i (K pat') ts) body') clauses |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
686 |
| NONE => [(ts, body)] |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
687 |
else [(ts, body)] |
48074 | 688 |
| _ => [(ts, body)]; |
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
689 |
fun mk_clause mk tys t = |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
690 |
let |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
691 |
val (vs, body) = unfold_abs_eta tys t; |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
692 |
val vs_map = fold_index (fn (i, (SOME v, _)) => cons (v, i) | _ => I) vs []; |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
693 |
val ts = map (IVar o fst) vs; |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
694 |
in map mk (collapse_clause vs_map ts body) end; |
31892 | 695 |
val t = nth ts t_pos; |
696 |
val ts_clause = nth_drop t_pos ts; |
|
31935
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
697 |
val clauses = if null case_pats |
3896169e6ff9
cleaned up fundamental iml term functions; nested patterns
haftmann
parents:
31892
diff
changeset
|
698 |
then mk_clause (fn ([t], body) => (t, body)) [ty] (the_single ts_clause) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
699 |
else maps (fn ((constr as IConst { dom = tys, ... }, n), t) => |
33957 | 700 |
mk_clause (fn (ts, body) => (constr `$$ ts, body)) (take n tys) t) |
47555 | 701 |
(constrs ~~ (map_filter (fn (NONE, _) => NONE | (SOME _, t) => SOME t) |
702 |
(case_pats ~~ ts_clause))); |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
703 |
in ICase { term = t, typ = ty, clauses = clauses, primitive = t_app `$$ ts } end; |
24918 | 704 |
in |
55757 | 705 |
translate_const ctxt algbr eqngr permissive some_thm (c_ty, NONE) |
706 |
##>> fold_map (fn (constr, n) => translate_const ctxt algbr eqngr permissive some_thm (constr, NONE) |
|
47555 | 707 |
#>> rpair n) constrs |
55757 | 708 |
##>> translate_typ ctxt algbr eqngr permissive ty |
709 |
##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
710 |
#>> (fn (((t, constrs), ty), ts) => |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
711 |
casify constrs ty t ts) |
24918 | 712 |
end |
55757 | 713 |
and translate_app_case ctxt algbr eqngr permissive some_thm (case_scheme as (num_args, _)) ((c, ty), ts) = |
29973 | 714 |
if length ts < num_args then |
715 |
let |
|
716 |
val k = length ts; |
|
33957 | 717 |
val tys = (take (num_args - k) o drop k o fst o strip_type) ty; |
55757 | 718 |
val names = (fold o fold_aterms) Term.declare_term_frees ts Name.context; |
719 |
val vs = Name.invent_names names "a" tys; |
|
29973 | 720 |
in |
55757 | 721 |
fold_map (translate_typ ctxt algbr eqngr permissive) tys |
722 |
##>> translate_case ctxt algbr eqngr permissive some_thm case_scheme ((c, ty), ts @ map Free vs) |
|
31888 | 723 |
#>> (fn (tys, t) => map2 (fn (v, _) => pair (SOME v)) vs tys `|==> t) |
29973 | 724 |
end |
725 |
else if length ts > num_args then |
|
55757 | 726 |
translate_case ctxt algbr eqngr permissive some_thm case_scheme ((c, ty), take num_args ts) |
727 |
##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) (drop num_args ts) |
|
29973 | 728 |
#>> (fn (t, ts) => t `$$ ts) |
729 |
else |
|
55757 | 730 |
translate_case ctxt algbr eqngr permissive some_thm case_scheme ((c, ty), ts) |
731 |
and translate_app ctxt algbr eqngr permissive some_thm (c_ty_ts as ((c, _), _), some_abs) = |
|
732 |
case Code.get_case_scheme (Proof_Context.theory_of ctxt) c |
|
733 |
of SOME case_scheme => translate_app_case ctxt algbr eqngr permissive some_thm case_scheme c_ty_ts |
|
734 |
| NONE => translate_app_const ctxt algbr eqngr permissive some_thm (c_ty_ts, some_abs) |
|
735 |
and translate_tyvar_sort ctxt (algbr as (proj_sort, _)) eqngr permissive (v, sort) = |
|
736 |
fold_map (ensure_class ctxt algbr eqngr permissive) (proj_sort sort) |
|
30932 | 737 |
#>> (fn sort => (unprefix "'" v, sort)) |
55757 | 738 |
and translate_dicts ctxt algbr eqngr permissive some_thm (ty, sort) = |
30932 | 739 |
let |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
740 |
fun mk_dict (Weakening (classrels, x)) = |
55757 | 741 |
fold_map (ensure_classrel ctxt algbr eqngr permissive) classrels |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
742 |
##>> mk_plain_dict x |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
743 |
#>> Dict |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
744 |
and mk_plain_dict (Global (inst, dss)) = |
55757 | 745 |
ensure_inst ctxt algbr eqngr permissive inst |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40844
diff
changeset
|
746 |
##>> (fold_map o fold_map) mk_dict dss |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
747 |
#>> (fn (inst, dss) => Dict_Const (inst, dss)) |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
748 |
| mk_plain_dict (Local (v, (n, sort))) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
749 |
pair (Dict_Var (unprefix "'" v, (n, length sort))) |
55189 | 750 |
in |
55757 | 751 |
construct_dictionaries ctxt algbr permissive some_thm (ty, sort) |
55189 | 752 |
#-> (fn typarg_witnesses => fold_map mk_dict typarg_witnesses) |
753 |
end; |
|
24918 | 754 |
|
25969 | 755 |
|
27103 | 756 |
(* store *) |
757 |
||
34173
458ced35abb8
reduced code generator cache to the baremost minimum
haftmann
parents:
34084
diff
changeset
|
758 |
structure Program = Code_Data |
27103 | 759 |
( |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
760 |
type T = program; |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
761 |
val empty = Code_Symbol.Graph.empty; |
27103 | 762 |
); |
763 |
||
47571 | 764 |
fun invoke_generation ignore_cache thy (algebra, eqngr) generate thing = |
39397 | 765 |
Program.change_yield (if ignore_cache then NONE else SOME thy) |
55190 | 766 |
(fn program => ([], program) |
55757 | 767 |
|> generate (Proof_Context.init_global thy) algebra eqngr thing |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
768 |
|-> (fn thing => fn (_, program) => (thing, program))); |
36272
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
769 |
|
27103 | 770 |
|
771 |
(* program generation *) |
|
772 |
||
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
773 |
fun consts_program_internal thy permissive consts = |
27103 | 774 |
let |
55757 | 775 |
fun generate_consts ctxt algebra eqngr = |
776 |
fold_map (ensure_const ctxt algebra eqngr permissive); |
|
27103 | 777 |
in |
39487
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
778 |
invoke_generation permissive thy (Code_Preproc.obtain false thy consts []) |
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
779 |
generate_consts consts |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
780 |
|> snd |
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
781 |
end; |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
782 |
|
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
783 |
fun consts_program_permissive thy = consts_program_internal thy true; |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
784 |
|
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
785 |
fun consts_program thy consts = |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
786 |
let |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
787 |
fun project program = Code_Symbol.Graph.restrict |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
788 |
(member (op =) (Code_Symbol.Graph.all_succs program |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
789 |
(map Constant consts))) program; |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
790 |
in |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
791 |
consts_program_internal thy false consts |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
792 |
|> project |
27103 | 793 |
end; |
794 |
||
795 |
||
796 |
(* value evaluation *) |
|
25969 | 797 |
|
55757 | 798 |
fun ensure_value ctxt algbr eqngr t = |
24918 | 799 |
let |
800 |
val ty = fastype_of t; |
|
801 |
val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =) |
|
802 |
o dest_TFree))) t []; |
|
55757 | 803 |
val t' = annotate ctxt algbr eqngr (@{const_name dummy_pattern}, ty) [] t; |
55150 | 804 |
val dummy_constant = Constant @{const_name dummy_pattern}; |
24918 | 805 |
val stmt_value = |
55757 | 806 |
fold_map (translate_tyvar_sort ctxt algbr eqngr false) vs |
807 |
##>> translate_typ ctxt algbr eqngr false ty |
|
808 |
##>> translate_term ctxt algbr eqngr false NONE (t', NONE) |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28423
diff
changeset
|
809 |
#>> (fn ((vs, ty), t) => Fun |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
810 |
(((vs, ty), [(([], t), (NONE, true))]), NONE)); |
55190 | 811 |
fun term_value (deps, program1) = |
25969 | 812 |
let |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
813 |
val Fun ((vs_ty, [(([], t), _)]), _) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
814 |
Code_Symbol.Graph.get_node program1 dummy_constant; |
55190 | 815 |
val deps' = Code_Symbol.Graph.immediate_succs program1 dummy_constant; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
816 |
val program2 = Code_Symbol.Graph.del_node dummy_constant program1; |
55190 | 817 |
val deps_all = Code_Symbol.Graph.all_succs program2 deps'; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
818 |
val program3 = Code_Symbol.Graph.restrict (member (op =) deps_all) program2; |
55190 | 819 |
in ((program3, ((vs_ty, t), deps')), (deps, program2)) end; |
26011 | 820 |
in |
55150 | 821 |
ensure_stmt Constant stmt_value @{const_name dummy_pattern} |
26011 | 822 |
#> snd |
31063
88aaab83b6fc
dropped explicit suppport for frees in evaluation conversion stack
haftmann
parents:
31054
diff
changeset
|
823 |
#> term_value |
26011 | 824 |
end; |
24219 | 825 |
|
39487
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
826 |
fun original_sorts vs = |
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
827 |
map (fn (v, _) => (v, (the o AList.lookup (op =) vs o prefix "'") v)); |
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
828 |
|
55757 | 829 |
fun dynamic_evaluator ctxt evaluator algebra eqngr vs t = |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
830 |
let |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
831 |
val ((program, (((vs', ty'), t'), deps)), _) = |
55757 | 832 |
invoke_generation false (Proof_Context.theory_of ctxt) (algebra, eqngr) ensure_value t; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
833 |
in evaluator program ((original_sorts vs vs', (vs', ty')), t') deps end; |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
834 |
|
55757 | 835 |
fun dynamic_conv ctxt conv = |
836 |
Code_Preproc.dynamic_conv ctxt (dynamic_evaluator ctxt conv); |
|
39475 | 837 |
|
55757 | 838 |
fun dynamic_value ctxt postproc evaluator = |
839 |
Code_Preproc.dynamic_value ctxt postproc (dynamic_evaluator ctxt evaluator); |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
840 |
|
55757 | 841 |
fun lift_evaluation ctxt evaluation algebra eqngr program vs t = |
39487
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
842 |
let |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
843 |
val ((_, (((vs', ty'), t'), deps)), _) = |
55757 | 844 |
ensure_value ctxt algebra eqngr t ([], program); |
845 |
in evaluation ctxt ((original_sorts vs vs', (vs', ty')), t') deps end; |
|
41365 | 846 |
|
55757 | 847 |
fun lift_evaluator ctxt evaluator consts algebra eqngr = |
41365 | 848 |
let |
55757 | 849 |
fun generate_consts ctxt algebra eqngr = |
850 |
fold_map (ensure_const ctxt algebra eqngr false); |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
851 |
val (consts', program) = |
55757 | 852 |
invoke_generation true (Proof_Context.theory_of ctxt) (algebra, eqngr) generate_consts consts; |
853 |
val evaluation = evaluator program consts'; |
|
854 |
in fn ctxt' => lift_evaluation ctxt' evaluation algebra eqngr program end; |
|
41365 | 855 |
|
55757 | 856 |
fun lift_evaluator_simple ctxt evaluator consts algebra eqngr = |
41365 | 857 |
let |
55757 | 858 |
fun generate_consts ctxt algebra eqngr = |
859 |
fold_map (ensure_const ctxt algebra eqngr false); |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54932
diff
changeset
|
860 |
val (_, program) = |
55757 | 861 |
invoke_generation true (Proof_Context.theory_of ctxt) (algebra, eqngr) generate_consts consts; |
862 |
in evaluator program end; |
|
39487
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
863 |
|
55757 | 864 |
fun static_conv ctxt consts conv = |
865 |
Code_Preproc.static_conv ctxt consts (lift_evaluator ctxt conv consts); |
|
38672
f1f64375f662
preliminary versions of static_eval_conv(_simple)
haftmann
parents:
38669
diff
changeset
|
866 |
|
55757 | 867 |
fun static_conv_simple ctxt consts conv = |
868 |
Code_Preproc.static_conv ctxt consts (lift_evaluator_simple ctxt conv consts); |
|
38672
f1f64375f662
preliminary versions of static_eval_conv(_simple)
haftmann
parents:
38669
diff
changeset
|
869 |
|
55757 | 870 |
fun static_value ctxt postproc consts evaluator = |
871 |
Code_Preproc.static_value ctxt postproc consts (lift_evaluator ctxt evaluator consts); |
|
39487
80b2cf3b0779
proper closures for static evaluation; no need for FIXMEs any longer
haftmann
parents:
39475
diff
changeset
|
872 |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
873 |
|
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
874 |
(** constant expressions **) |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
875 |
|
55757 | 876 |
fun read_const_exprs_internal ctxt = |
31036 | 877 |
let |
55757 | 878 |
val thy = Proof_Context.theory_of ctxt; |
36272
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
879 |
fun consts_of thy' = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I) |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46961
diff
changeset
|
880 |
((snd o #constants o Consts.dest o Sign.consts_of) thy') []; |
36272
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
881 |
fun belongs_here thy' c = forall |
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
882 |
(fn thy'' => not (Sign.declared_const thy'' c)) (Theory.parents_of thy'); |
4d358c582ffb
optionally ignore errors during translation of equations; tuned representation of abstraction points
haftmann
parents:
36210
diff
changeset
|
883 |
fun consts_of_select thy' = filter (belongs_here thy') (consts_of thy'); |
52801 | 884 |
fun read_const_expr str = |
885 |
(case Syntax.parse_token ctxt (K NONE) Markup.empty (SOME o Symbol_Pos.implode o #1) str of |
|
886 |
SOME "_" => ([], consts_of thy) |
|
887 |
| SOME s => |
|
888 |
if String.isSuffix "._" s |
|
40711
81bc73585eec
globbing constant expressions use more idiomatic underscore rather than star
haftmann
parents:
39566
diff
changeset
|
889 |
then ([], consts_of_select (Context.this_theory thy (unsuffix "._" s))) |
52801 | 890 |
else ([Code.read_const thy str], []) |
891 |
| NONE => ([Code.read_const thy str], [])); |
|
31036 | 892 |
in pairself flat o split_list o map read_const_expr end; |
893 |
||
55757 | 894 |
fun read_const_exprs_all ctxt = op @ o read_const_exprs_internal ctxt; |
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
895 |
|
55757 | 896 |
fun read_const_exprs ctxt const_exprs = |
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
897 |
let |
55757 | 898 |
val (consts, consts_permissive) = read_const_exprs_internal ctxt const_exprs; |
899 |
val consts' = implemented_deps |
|
900 |
(consts_program_permissive ((Proof_Context.theory_of ctxt)) consts_permissive); |
|
55188
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
901 |
in union (op =) consts' consts end; |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
902 |
|
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
903 |
|
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
904 |
(** diagnostic commands **) |
7ca0204ece66
reduced prominence of "permissive code generation"
haftmann
parents:
55150
diff
changeset
|
905 |
|
55757 | 906 |
fun code_depgr ctxt consts = |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
907 |
let |
55757 | 908 |
val (_, eqngr) = Code_Preproc.obtain true ((Proof_Context.theory_of ctxt)) consts []; |
34173
458ced35abb8
reduced code generator cache to the baremost minimum
haftmann
parents:
34084
diff
changeset
|
909 |
val all_consts = Graph.all_succs eqngr consts; |
46614
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
wenzelm
parents:
45987
diff
changeset
|
910 |
in Graph.restrict (member (op =) all_consts) eqngr end; |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
911 |
|
55757 | 912 |
fun code_thms ctxt = Pretty.writeln o Code_Preproc.pretty ctxt o code_depgr ctxt; |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
913 |
|
55757 | 914 |
fun code_deps ctxt consts = |
27103 | 915 |
let |
55757 | 916 |
val thy = Proof_Context.theory_of ctxt; |
917 |
val eqngr = code_depgr ctxt consts; |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
918 |
val constss = Graph.strong_conn eqngr; |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
919 |
val mapping = Symtab.empty |> fold (fn consts => fold (fn const => |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
920 |
Symtab.update (const, consts)) consts) constss; |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
921 |
fun succs consts = consts |
44338
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents:
43329
diff
changeset
|
922 |
|> maps (Graph.immediate_succs eqngr) |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
923 |
|> subtract (op =) consts |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
924 |
|> map (the o Symtab.lookup mapping) |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
925 |
|> distinct (op =); |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
926 |
val conn = [] |> fold (fn consts => cons (consts, succs consts)) constss; |
31156 | 927 |
fun namify consts = map (Code.string_of_const thy) consts |
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
928 |
|> commas; |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
929 |
val prgr = map (fn (consts, constss) => |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
930 |
{ name = namify consts, ID = namify consts, dir = "", unfold = true, |
49561 | 931 |
path = "", parents = map namify constss, content = [] }) conn; |
932 |
in Graph_Display.display_graph prgr end; |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
933 |
|
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
934 |
local |
27103 | 935 |
|
55757 | 936 |
fun code_thms_cmd ctxt = code_thms ctxt o read_const_exprs_all ctxt; |
937 |
fun code_deps_cmd ctxt = code_deps ctxt o read_const_exprs_all ctxt; |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
938 |
|
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
939 |
in |
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
940 |
|
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
941 |
val _ = |
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46665
diff
changeset
|
942 |
Outer_Syntax.improper_command @{command_spec "code_thms"} |
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46665
diff
changeset
|
943 |
"print system of code equations for code" |
52801 | 944 |
(Scan.repeat1 Parse.term >> (fn cs => |
55757 | 945 |
Toplevel.unknown_context o |
946 |
Toplevel.keep (fn state => code_thms_cmd (Toplevel.context_of state) cs))); |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
947 |
|
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
948 |
val _ = |
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46665
diff
changeset
|
949 |
Outer_Syntax.improper_command @{command_spec "code_deps"} |
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46665
diff
changeset
|
950 |
"visualize dependencies of code equations for code" |
52801 | 951 |
(Scan.repeat1 Parse.term >> (fn cs => |
55757 | 952 |
Toplevel.unknown_context o |
953 |
Toplevel.keep (fn state => code_deps_cmd (Toplevel.context_of state) cs))); |
|
30942
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
954 |
|
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents:
30932
diff
changeset
|
955 |
end; |
27103 | 956 |
|
24219 | 957 |
end; (*struct*) |
958 |
||
959 |
||
28054 | 960 |
structure Basic_Code_Thingol: BASIC_CODE_THINGOL = Code_Thingol; |