author | haftmann |
Wed, 05 Dec 2007 14:15:45 +0100 | |
changeset 25534 | d0b74fdd6067 |
parent 25505 | 4d531475129a |
child 25569 | c597835d5de4 |
permissions | -rw-r--r-- |
24589 | 1 |
(* Title: HOL/Tools/datatype_codegen.ML |
12445 | 2 |
ID: $Id$ |
20105 | 3 |
Author: Stefan Berghofer & Florian Haftmann, TU Muenchen |
12445 | 4 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
5 |
Code generator facilities for inductive datatypes. |
12445 | 6 |
*) |
7 |
||
8 |
signature DATATYPE_CODEGEN = |
|
9 |
sig |
|
20426 | 10 |
val get_eq: theory -> string -> thm list |
23811 | 11 |
val get_case_cert: theory -> string -> thm |
18708 | 12 |
val setup: theory -> theory |
12445 | 13 |
end; |
14 |
||
15 |
structure DatatypeCodegen : DATATYPE_CODEGEN = |
|
16 |
struct |
|
17 |
||
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
18 |
(** SML code generator **) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
19 |
|
12445 | 20 |
open Codegen; |
21 |
||
22 |
fun mk_tuple [p] = p |
|
23 |
| mk_tuple ps = Pretty.block (Pretty.str "(" :: |
|
15570 | 24 |
List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single ps)) @ |
12445 | 25 |
[Pretty.str ")"]); |
26 |
||
27 |
(**** datatype definition ****) |
|
28 |
||
14104 | 29 |
(* find shortest path to constructor with no recursive arguments *) |
30 |
||
31 |
fun find_nonempty (descr: DatatypeAux.descr) is i = |
|
32 |
let |
|
17521 | 33 |
val (_, _, constrs) = valOf (AList.lookup (op =) descr i); |
15531 | 34 |
fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE |
15570 | 35 |
else Option.map (curry op + 1 o snd) (find_nonempty descr (i::is) i) |
15531 | 36 |
| arg_nonempty _ = SOME 0; |
15570 | 37 |
fun max xs = Library.foldl |
15531 | 38 |
(fn (NONE, _) => NONE |
39 |
| (SOME i, SOME j) => SOME (Int.max (i, j)) |
|
40 |
| (_, NONE) => NONE) (SOME 0, xs); |
|
14104 | 41 |
val xs = sort (int_ord o pairself snd) |
15570 | 42 |
(List.mapPartial (fn (s, dts) => Option.map (pair s) |
14104 | 43 |
(max (map (arg_nonempty o DatatypeAux.strip_dtyp) dts))) constrs) |
15531 | 44 |
in case xs of [] => NONE | x :: _ => SOME x end; |
14104 | 45 |
|
17144 | 46 |
fun add_dt_defs thy defs dep module gr (descr: DatatypeAux.descr) = |
12445 | 47 |
let |
15570 | 48 |
val descr' = List.filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr; |
49 |
val rtnames = map (#1 o snd) (List.filter (fn (_, (_, _, cs)) => |
|
14104 | 50 |
exists (exists DatatypeAux.is_rec_type o snd) cs) descr'); |
12445 | 51 |
|
17144 | 52 |
val (_, (tname, _, _)) :: _ = descr'; |
53 |
val node_id = tname ^ " (type)"; |
|
54 |
val module' = if_library (thyname_of_type tname thy) module; |
|
12445 | 55 |
|
56 |
fun mk_dtdef gr prfx [] = (gr, []) |
|
57 |
| mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) = |
|
58 |
let |
|
59 |
val tvs = map DatatypeAux.dest_DtTFree dts; |
|
60 |
val sorts = map (rpair []) tvs; |
|
61 |
val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs; |
|
17144 | 62 |
val (gr', (_, type_id)) = mk_type_id module' tname gr; |
63 |
val (gr'', ps) = |
|
64 |
foldl_map (fn (gr, (cname, cargs)) => |
|
65 |
foldl_map (invoke_tycodegen thy defs node_id module' false) |
|
66 |
(gr, cargs) |>>> |
|
67 |
mk_const_id module' cname) (gr', cs'); |
|
68 |
val (gr''', rest) = mk_dtdef gr'' "and " xs |
|
12445 | 69 |
in |
17144 | 70 |
(gr''', |
12445 | 71 |
Pretty.block (Pretty.str prfx :: |
72 |
(if null tvs then [] else |
|
73 |
[mk_tuple (map Pretty.str tvs), Pretty.str " "]) @ |
|
17144 | 74 |
[Pretty.str (type_id ^ " ="), Pretty.brk 1] @ |
15570 | 75 |
List.concat (separate [Pretty.brk 1, Pretty.str "| "] |
17144 | 76 |
(map (fn (ps', (_, cname)) => [Pretty.block |
77 |
(Pretty.str cname :: |
|
12445 | 78 |
(if null ps' then [] else |
15570 | 79 |
List.concat ([Pretty.str " of", Pretty.brk 1] :: |
12445 | 80 |
separate [Pretty.str " *", Pretty.brk 1] |
81 |
(map single ps'))))]) ps))) :: rest) |
|
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
82 |
end; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
83 |
|
17144 | 84 |
fun mk_term_of_def gr prfx [] = [] |
85 |
| mk_term_of_def gr prfx ((_, (tname, dts, cs)) :: xs) = |
|
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
86 |
let |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
87 |
val tvs = map DatatypeAux.dest_DtTFree dts; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
88 |
val sorts = map (rpair []) tvs; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
89 |
val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
90 |
val dts' = map (DatatypeAux.typ_of_dtyp descr sorts) dts; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
91 |
val T = Type (tname, dts'); |
17144 | 92 |
val rest = mk_term_of_def gr "and " xs; |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
93 |
val (_, eqs) = foldl_map (fn (prfx, (cname, Ts)) => |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
94 |
let val args = map (fn i => |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
95 |
Pretty.str ("x" ^ string_of_int i)) (1 upto length Ts) |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
96 |
in (" | ", Pretty.blk (4, |
17144 | 97 |
[Pretty.str prfx, mk_term_of gr module' false T, Pretty.brk 1, |
98 |
if null Ts then Pretty.str (snd (get_const_id cname gr)) |
|
16645 | 99 |
else parens (Pretty.block |
17144 | 100 |
[Pretty.str (snd (get_const_id cname gr)), |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
101 |
Pretty.brk 1, mk_tuple args]), |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
102 |
Pretty.str " =", Pretty.brk 1] @ |
15570 | 103 |
List.concat (separate [Pretty.str " $", Pretty.brk 1] |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
104 |
([Pretty.str ("Const (\"" ^ cname ^ "\","), Pretty.brk 1, |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
105 |
mk_type false (Ts ---> T), Pretty.str ")"] :: |
17144 | 106 |
map (fn (x, U) => [Pretty.block [mk_term_of gr module' false U, |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
107 |
Pretty.brk 1, x]]) (args ~~ Ts))))) |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
108 |
end) (prfx, cs') |
14104 | 109 |
in eqs @ rest end; |
110 |
||
17144 | 111 |
fun mk_gen_of_def gr prfx [] = [] |
112 |
| mk_gen_of_def gr prfx ((i, (tname, dts, cs)) :: xs) = |
|
14104 | 113 |
let |
114 |
val tvs = map DatatypeAux.dest_DtTFree dts; |
|
115 |
val sorts = map (rpair []) tvs; |
|
116 |
val (cs1, cs2) = |
|
15570 | 117 |
List.partition (exists DatatypeAux.is_rec_type o snd) cs; |
15531 | 118 |
val SOME (cname, _) = find_nonempty descr [i] i; |
14104 | 119 |
|
120 |
fun mk_delay p = Pretty.block |
|
121 |
[Pretty.str "fn () =>", Pretty.brk 1, p]; |
|
122 |
||
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
123 |
fun mk_constr s b (cname, dts) = |
14104 | 124 |
let |
17144 | 125 |
val gs = map (fn dt => mk_app false (mk_gen gr module' false rtnames s |
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
126 |
(DatatypeAux.typ_of_dtyp descr sorts dt)) |
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
127 |
[Pretty.str (if b andalso DatatypeAux.is_rec_type dt then "0" |
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
128 |
else "j")]) dts; |
17144 | 129 |
val (_, id) = get_const_id cname gr |
14104 | 130 |
in case gs of |
131 |
_ :: _ :: _ => Pretty.block |
|
132 |
[Pretty.str id, Pretty.brk 1, mk_tuple gs] |
|
133 |
| _ => mk_app false (Pretty.str id) (map parens gs) |
|
134 |
end; |
|
135 |
||
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
136 |
fun mk_choice [c] = mk_constr "(i-1)" false c |
14104 | 137 |
| mk_choice cs = Pretty.block [Pretty.str "one_of", |
138 |
Pretty.brk 1, Pretty.blk (1, Pretty.str "[" :: |
|
15570 | 139 |
List.concat (separate [Pretty.str ",", Pretty.fbrk] |
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
140 |
(map (single o mk_delay o mk_constr "(i-1)" false) cs)) @ |
14104 | 141 |
[Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]; |
142 |
||
143 |
val gs = map (Pretty.str o suffix "G" o strip_tname) tvs; |
|
17144 | 144 |
val gen_name = "gen_" ^ snd (get_type_id tname gr) |
14104 | 145 |
|
146 |
in |
|
147 |
Pretty.blk (4, separate (Pretty.brk 1) |
|
148 |
(Pretty.str (prfx ^ gen_name ^ |
|
149 |
(if null cs1 then "" else "'")) :: gs @ |
|
150 |
(if null cs1 then [] else [Pretty.str "i"]) @ |
|
151 |
[Pretty.str "j"]) @ |
|
152 |
[Pretty.str " =", Pretty.brk 1] @ |
|
153 |
(if not (null cs1) andalso not (null cs2) |
|
154 |
then [Pretty.str "frequency", Pretty.brk 1, |
|
155 |
Pretty.blk (1, [Pretty.str "[", |
|
156 |
mk_tuple [Pretty.str "i", mk_delay (mk_choice cs1)], |
|
157 |
Pretty.str ",", Pretty.fbrk, |
|
158 |
mk_tuple [Pretty.str "1", mk_delay (mk_choice cs2)], |
|
159 |
Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"] |
|
160 |
else if null cs2 then |
|
161 |
[Pretty.block [Pretty.str "(case", Pretty.brk 1, |
|
162 |
Pretty.str "i", Pretty.brk 1, Pretty.str "of", |
|
163 |
Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1, |
|
17521 | 164 |
mk_constr "0" true (cname, valOf (AList.lookup (op =) cs cname)), |
14104 | 165 |
Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1, |
166 |
mk_choice cs1, Pretty.str ")"]] |
|
167 |
else [mk_choice cs2])) :: |
|
168 |
(if null cs1 then [] |
|
169 |
else [Pretty.blk (4, separate (Pretty.brk 1) |
|
170 |
(Pretty.str ("and " ^ gen_name) :: gs @ [Pretty.str "i"]) @ |
|
171 |
[Pretty.str " =", Pretty.brk 1] @ |
|
172 |
separate (Pretty.brk 1) (Pretty.str (gen_name ^ "'") :: gs @ |
|
173 |
[Pretty.str "i", Pretty.str "i"]))]) @ |
|
17144 | 174 |
mk_gen_of_def gr "and " xs |
14104 | 175 |
end |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
176 |
|
12445 | 177 |
in |
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
178 |
((add_edge_acyclic (node_id, dep) gr |
12445 | 179 |
handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ => |
180 |
let |
|
17144 | 181 |
val gr1 = add_edge (node_id, dep) |
182 |
(new_node (node_id, (NONE, "", "")) gr); |
|
12445 | 183 |
val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr'; |
184 |
in |
|
17144 | 185 |
map_node node_id (K (NONE, module', |
12445 | 186 |
Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk dtdef @ |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
187 |
[Pretty.str ";"])) ^ "\n\n" ^ |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
188 |
(if "term_of" mem !mode then |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
189 |
Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk |
17144 | 190 |
(mk_term_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n" |
14104 | 191 |
else "") ^ |
192 |
(if "test" mem !mode then |
|
193 |
Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk |
|
17144 | 194 |
(mk_gen_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n" |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
195 |
else ""))) gr2 |
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
196 |
end, |
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
197 |
module') |
12445 | 198 |
end; |
199 |
||
200 |
||
201 |
(**** case expressions ****) |
|
202 |
||
17144 | 203 |
fun pretty_case thy defs gr dep module brack constrs (c as Const (_, T)) ts = |
12445 | 204 |
let val i = length constrs |
205 |
in if length ts <= i then |
|
17144 | 206 |
invoke_codegen thy defs dep module brack (gr, eta_expand c ts (i+1)) |
12445 | 207 |
else |
208 |
let |
|
15570 | 209 |
val ts1 = Library.take (i, ts); |
210 |
val t :: ts2 = Library.drop (i, ts); |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
211 |
val names = foldr add_term_names |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
212 |
(map (fst o fst o dest_Var) (foldr add_term_vars [] ts1)) ts1; |
15570 | 213 |
val (Ts, dT) = split_last (Library.take (i+1, fst (strip_type T))); |
12445 | 214 |
|
215 |
fun pcase gr [] [] [] = ([], gr) |
|
216 |
| pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) = |
|
217 |
let |
|
218 |
val j = length cargs; |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19886
diff
changeset
|
219 |
val xs = Name.variant_list names (replicate j "x"); |
15570 | 220 |
val Us' = Library.take (j, fst (strip_type U)); |
12445 | 221 |
val frees = map Free (xs ~~ Us'); |
17144 | 222 |
val (gr0, cp) = invoke_codegen thy defs dep module false |
12445 | 223 |
(gr, list_comb (Const (cname, Us' ---> dT), frees)); |
224 |
val t' = Envir.beta_norm (list_comb (t, frees)); |
|
17144 | 225 |
val (gr1, p) = invoke_codegen thy defs dep module false (gr0, t'); |
12445 | 226 |
val (ps, gr2) = pcase gr1 cs ts Us; |
227 |
in |
|
228 |
([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2) |
|
229 |
end; |
|
230 |
||
231 |
val (ps1, gr1) = pcase gr constrs ts1 Ts; |
|
15570 | 232 |
val ps = List.concat (separate [Pretty.brk 1, Pretty.str "| "] ps1); |
17144 | 233 |
val (gr2, p) = invoke_codegen thy defs dep module false (gr1, t); |
234 |
val (gr3, ps2) = foldl_map (invoke_codegen thy defs dep module true) (gr2, ts2) |
|
12445 | 235 |
in (gr3, (if not (null ts2) andalso brack then parens else I) |
236 |
(Pretty.block (separate (Pretty.brk 1) |
|
237 |
(Pretty.block ([Pretty.str "(case ", p, Pretty.str " of", |
|
238 |
Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2)))) |
|
239 |
end |
|
240 |
end; |
|
241 |
||
242 |
||
243 |
(**** constructors ****) |
|
244 |
||
17144 | 245 |
fun pretty_constr thy defs gr dep module brack args (c as Const (s, T)) ts = |
12445 | 246 |
let val i = length args |
247 |
in if i > 1 andalso length ts < i then |
|
17144 | 248 |
invoke_codegen thy defs dep module brack (gr, eta_expand c ts i) |
12445 | 249 |
else |
250 |
let |
|
17144 | 251 |
val id = mk_qual_id module (get_const_id s gr); |
16645 | 252 |
val (gr', ps) = foldl_map |
17144 | 253 |
(invoke_codegen thy defs dep module (i = 1)) (gr, ts); |
12445 | 254 |
in (case args of |
255 |
_ :: _ :: _ => (gr', (if brack then parens else I) |
|
256 |
(Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps])) |
|
257 |
| _ => (gr', mk_app brack (Pretty.str id) ps)) |
|
258 |
end |
|
259 |
end; |
|
260 |
||
261 |
||
262 |
(**** code generators for terms and types ****) |
|
263 |
||
17144 | 264 |
fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of |
12445 | 265 |
(c as Const (s, T), ts) => |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
266 |
(case DatatypePackage.datatype_of_case thy s of |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
267 |
SOME {index, descr, ...} => |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
268 |
if is_some (get_assoc_code thy (s, T)) then NONE else |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
269 |
SOME (pretty_case thy defs gr dep module brack |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
270 |
(#3 (the (AList.lookup op = descr index))) c ts) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
271 |
| NONE => case (DatatypePackage.datatype_of_constr thy s, strip_type T) of |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
272 |
(SOME {index, descr, ...}, (_, U as Type _)) => |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
273 |
if is_some (get_assoc_code thy (s, T)) then NONE else |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
274 |
let val SOME args = AList.lookup op = |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
275 |
(#3 (the (AList.lookup op = descr index))) s |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
276 |
in |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
277 |
SOME (pretty_constr thy defs |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
278 |
(fst (invoke_tycodegen thy defs dep module false (gr, U))) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
279 |
dep module brack args c ts) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
280 |
end |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
281 |
| _ => NONE) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
282 |
| _ => NONE); |
12445 | 283 |
|
17144 | 284 |
fun datatype_tycodegen thy defs gr dep module brack (Type (s, Ts)) = |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
285 |
(case DatatypePackage.get_datatype thy s of |
15531 | 286 |
NONE => NONE |
287 |
| SOME {descr, ...} => |
|
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
288 |
if is_some (get_assoc_type thy s) then NONE else |
16645 | 289 |
let |
290 |
val (gr', ps) = foldl_map |
|
17144 | 291 |
(invoke_tycodegen thy defs dep module false) (gr, Ts); |
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
292 |
val (gr'', module') = add_dt_defs thy defs dep module gr' descr; |
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
293 |
val (gr''', tyid) = mk_type_id module' s gr'' |
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
294 |
in SOME (gr''', |
12445 | 295 |
Pretty.block ((if null Ts then [] else |
296 |
[mk_tuple ps, Pretty.str " "]) @ |
|
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
297 |
[Pretty.str (mk_qual_id module tyid)])) |
12445 | 298 |
end) |
16645 | 299 |
| datatype_tycodegen _ _ _ _ _ _ _ = NONE; |
12445 | 300 |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18220
diff
changeset
|
301 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
302 |
(** generic code generator **) |
20105 | 303 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
304 |
(* specification *) |
20426 | 305 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
306 |
fun add_datatype_spec vs dtco cos thy = |
20835 | 307 |
let |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
308 |
val cs = map (fn (c, tys) => (c, tys ---> Type (dtco, map TFree vs))) cos; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
309 |
in |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
310 |
thy |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
311 |
|> try (Code.add_datatype cs) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
312 |
|> the_default thy |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
313 |
end; |
21516 | 314 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
315 |
|
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
316 |
(* case certificates *) |
19346 | 317 |
|
23811 | 318 |
fun get_case_cert thy tyco = |
319 |
let |
|
320 |
val raw_thms = |
|
321 |
(#case_rewrites o DatatypePackage.the_datatype thy) tyco; |
|
322 |
val thms as hd_thm :: _ = raw_thms |
|
323 |
|> Conjunction.intr_balanced |
|
24976
821628d16552
moved Drule.unvarify to Thm.unvarify (cf. more_thm.ML);
wenzelm
parents:
24845
diff
changeset
|
324 |
|> Thm.unvarify |
23811 | 325 |
|> Conjunction.elim_balanced (length raw_thms) |
326 |
|> map Simpdata.mk_meta_eq |
|
327 |
|> map Drule.zero_var_indexes |
|
328 |
val params = fold_aterms (fn (Free (v, _)) => insert (op =) v |
|
329 |
| _ => I) (Thm.prop_of hd_thm) []; |
|
330 |
val rhs = hd_thm |
|
331 |
|> Thm.prop_of |
|
332 |
|> Logic.dest_equals |
|
333 |
|> fst |
|
334 |
|> Term.strip_comb |
|
335 |
|> apsnd (fst o split_last) |
|
336 |
|> list_comb; |
|
337 |
val lhs = Free (Name.variant params "case", Term.fastype_of rhs); |
|
338 |
val asm = (Thm.cterm_of thy o Logic.mk_equals) (lhs, rhs); |
|
339 |
in |
|
340 |
thms |
|
341 |
|> Conjunction.intr_balanced |
|
342 |
|> MetaSimplifier.rewrite_rule [(Thm.symmetric o Thm.assume) asm] |
|
343 |
|> Thm.implies_intr asm |
|
344 |
|> Thm.generalize ([], params) 0 |
|
25485 | 345 |
|> Class.unoverload thy |
23811 | 346 |
|> Thm.varifyT |
347 |
end; |
|
348 |
||
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
349 |
fun add_datatype_cases dtco thy = |
25505 | 350 |
let |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
351 |
val {case_rewrites, ...} = DatatypePackage.the_datatype thy dtco; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
352 |
val certs = get_case_cert thy dtco; |
20426 | 353 |
in |
354 |
thy |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
355 |
|> Code.add_case certs |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
356 |
|> fold_rev Code.add_default_func case_rewrites |
20426 | 357 |
end; |
358 |
||
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
359 |
|
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
360 |
(* equality *) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
361 |
|
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
362 |
local |
20426 | 363 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
364 |
val not_sym = thm "HOL.not_sym"; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
365 |
val not_false_true = iffD2 OF [nth (thms "HOL.simp_thms") 7, TrueI]; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
366 |
val refl = thm "refl"; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
367 |
val eqTrueI = thm "eqTrueI"; |
20835 | 368 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
369 |
fun mk_distinct cos = |
22423 | 370 |
let |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
371 |
fun sym_product [] = [] |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
372 |
| sym_product (x::xs) = map (pair x) xs @ sym_product xs; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
373 |
fun mk_co_args (co, tys) ctxt = |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
374 |
let |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
375 |
val names = Name.invents ctxt "a" (length tys); |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
376 |
val ctxt' = fold Name.declare names ctxt; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
377 |
val vs = map2 (curry Free) names tys; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
378 |
in (vs, ctxt') end; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
379 |
fun mk_dist ((co1, tys1), (co2, tys2)) = |
22423 | 380 |
let |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
381 |
val ((xs1, xs2), _) = Name.context |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
382 |
|> mk_co_args (co1, tys1) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
383 |
||>> mk_co_args (co2, tys2); |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
384 |
val prem = HOLogic.mk_eq |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
385 |
(list_comb (co1, xs1), list_comb (co2, xs2)); |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
386 |
val t = HOLogic.mk_not prem; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
387 |
in HOLogic.mk_Trueprop t end; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
388 |
in map mk_dist (sym_product cos) end; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
389 |
|
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
390 |
in |
20426 | 391 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
392 |
fun get_eq thy dtco = |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
393 |
let |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
394 |
val (vs, cs) = DatatypePackage.the_datatype_spec thy dtco; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
395 |
fun mk_triv_inject co = |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
396 |
let |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
397 |
val ct' = Thm.cterm_of thy |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
398 |
(Const (co, Type (dtco, map (fn (v, sort) => TVar ((v, 0), sort)) vs))) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
399 |
val cty' = Thm.ctyp_of_term ct'; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
400 |
val SOME (ct, cty) = fold_aterms (fn Var (v, ty) => |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
401 |
(K o SOME) (Thm.cterm_of thy (Var (v, Thm.typ_of cty')), Thm.ctyp_of thy ty) | _ => I) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
402 |
(Thm.prop_of refl) NONE; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
403 |
in eqTrueI OF [Thm.instantiate ([(cty, cty')], [(ct, ct')]) refl] end; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
404 |
val inject1 = map_filter (fn (co, []) => SOME (mk_triv_inject co) | _ => NONE) cs |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
405 |
val inject2 = (#inject o DatatypePackage.the_datatype thy) dtco; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
406 |
val ctxt = ProofContext.init thy; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
407 |
val simpset = Simplifier.context ctxt |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
408 |
(MetaSimplifier.empty_ss addsimprocs [distinct_simproc]); |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
409 |
val cos = map (fn (co, tys) => |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
410 |
(Const (co, tys ---> Type (dtco, map TFree vs)), tys)) cs; |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
411 |
val tac = ALLGOALS (simp_tac simpset) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
412 |
THEN ALLGOALS (ProofContext.fact_tac [not_false_true, TrueI]); |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
413 |
val distinct = |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
414 |
mk_distinct cos |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
415 |
|> map (fn t => Goal.prove_global thy [] [] t (K tac)) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
416 |
|> (fn thms => thms @ map (fn thm => not_sym OF [thm]) thms) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
417 |
in inject1 @ inject2 @ distinct end; |
20426 | 418 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
419 |
end; |
20835 | 420 |
|
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
421 |
fun add_datatypes_equality vs dtcos thy = |
20597 | 422 |
let |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
423 |
fun get_eq' thy dtco = get_eq thy dtco |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
424 |
|> map (CodeUnit.constrain_thm [HOLogic.class_eq]) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
425 |
|> maps ((#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy); |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
426 |
fun add_eq_thms dtco thy = |
20597 | 427 |
let |
24137
8d7896398147
replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents:
23811
diff
changeset
|
428 |
val thy_ref = Theory.check_thy thy; |
25485 | 429 |
val const = Class.param_of_inst thy ("op =", dtco); |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
430 |
val get_thms = (fn () => get_eq' (Theory.deref thy_ref) dtco |> rev); |
20597 | 431 |
in |
24219 | 432 |
Code.add_funcl (const, Susp.delay get_thms) thy |
20597 | 433 |
end; |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
434 |
val sorts_eq = |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
435 |
map (curry (Sorts.inter_sort (Sign.classes_of thy)) [HOLogic.class_eq] o snd) vs; |
20597 | 436 |
in |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
437 |
thy |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
438 |
|> Instance.instantiate (dtcos, sorts_eq, [HOLogic.class_eq]) (pair ()) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
439 |
((K o K) (Class.intro_classes_tac [])) |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
440 |
|> fold add_eq_thms dtcos |
20597 | 441 |
end; |
442 |
||
20426 | 443 |
|
444 |
(** theory setup **) |
|
445 |
||
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
446 |
fun add_datatype_code dtcos thy = |
23513 | 447 |
let |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
448 |
val (vs :: _, coss) = (split_list o map (DatatypePackage.the_datatype_spec thy)) dtcos; |
25489 | 449 |
in |
25505 | 450 |
thy |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
451 |
|> fold2 (add_datatype_spec vs) dtcos coss |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
452 |
|> fold add_datatype_cases dtcos |
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
453 |
|> add_datatypes_equality vs dtcos |
25489 | 454 |
end; |
455 |
||
19008 | 456 |
val setup = |
20597 | 457 |
add_codegen "datatype" datatype_codegen |
24626
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
changeset
|
458 |
#> add_tycodegen "datatype" datatype_tycodegen |
25534
d0b74fdd6067
simplified infrastructure for code generator operational equality
haftmann
parents:
25505
diff
changeset
|
459 |
#> DatatypePackage.interpretation add_datatype_code |
20597 | 460 |
|
12445 | 461 |
end; |