author | haftmann |
Fri, 10 Aug 2007 17:04:34 +0200 | |
changeset 24219 | e558fe311376 |
parent 24137 | 8d7896398147 |
child 24423 | ae9cd0e92423 |
permissions | -rw-r--r-- |
12446 | 1 |
(* Title: HOL/datatype_codegen.ML |
12445 | 2 |
ID: $Id$ |
20105 | 3 |
Author: Stefan Berghofer & Florian Haftmann, TU Muenchen |
12445 | 4 |
|
20855 | 5 |
Code generator for inductive datatypes. |
12445 | 6 |
*) |
7 |
||
8 |
signature DATATYPE_CODEGEN = |
|
9 |
sig |
|
20426 | 10 |
val get_eq: theory -> string -> thm list |
20597 | 11 |
val get_eq_datatype: theory -> string -> thm list |
20105 | 12 |
val dest_case_expr: theory -> term |
20177
0af885e3dabf
hooks now take string list as arguments (mutual datatypes); some nice combinators in datatype_codegen
haftmann
parents:
20105
diff
changeset
|
13 |
-> ((string * typ) list * ((term * typ) * (term * term) list)) option |
23811 | 14 |
val get_case_cert: theory -> string -> thm |
20426 | 15 |
|
16 |
type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list |
|
17 |
-> theory -> theory |
|
20597 | 18 |
val codetype_hook: hook |
19 |
val eq_hook: hook |
|
20426 | 20 |
val codetypes_dependency: theory -> (string * bool) list list |
21 |
val add_codetypes_hook_bootstrap: hook -> theory -> theory |
|
22 |
val the_codetypes_mut_specs: theory -> (string * bool) list |
|
23 |
-> ((string * sort) list * (string * (bool * (string * typ list) list)) list) |
|
24 |
val get_codetypes_arities: theory -> (string * bool) list -> sort |
|
22331 | 25 |
-> (string * (arity * term list)) list option |
21924 | 26 |
val prove_codetypes_arities: tactic -> (string * bool) list -> sort |
22331 | 27 |
-> (arity list -> (string * term list) list -> theory |
22423 | 28 |
-> ((bstring * Attrib.src list) * term) list * theory) |
22331 | 29 |
-> (arity list -> (string * term list) list -> theory -> theory) |
20835 | 30 |
-> theory -> theory |
20426 | 31 |
|
18708 | 32 |
val setup: theory -> theory |
20835 | 33 |
val setup_hooks: theory -> theory |
12445 | 34 |
end; |
35 |
||
36 |
structure DatatypeCodegen : DATATYPE_CODEGEN = |
|
37 |
struct |
|
38 |
||
39 |
open Codegen; |
|
40 |
||
41 |
fun mk_tuple [p] = p |
|
42 |
| mk_tuple ps = Pretty.block (Pretty.str "(" :: |
|
15570 | 43 |
List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single ps)) @ |
12445 | 44 |
[Pretty.str ")"]); |
45 |
||
46 |
(**** datatype definition ****) |
|
47 |
||
14104 | 48 |
(* find shortest path to constructor with no recursive arguments *) |
49 |
||
50 |
fun find_nonempty (descr: DatatypeAux.descr) is i = |
|
51 |
let |
|
17521 | 52 |
val (_, _, constrs) = valOf (AList.lookup (op =) descr i); |
15531 | 53 |
fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE |
15570 | 54 |
else Option.map (curry op + 1 o snd) (find_nonempty descr (i::is) i) |
15531 | 55 |
| arg_nonempty _ = SOME 0; |
15570 | 56 |
fun max xs = Library.foldl |
15531 | 57 |
(fn (NONE, _) => NONE |
58 |
| (SOME i, SOME j) => SOME (Int.max (i, j)) |
|
59 |
| (_, NONE) => NONE) (SOME 0, xs); |
|
14104 | 60 |
val xs = sort (int_ord o pairself snd) |
15570 | 61 |
(List.mapPartial (fn (s, dts) => Option.map (pair s) |
14104 | 62 |
(max (map (arg_nonempty o DatatypeAux.strip_dtyp) dts))) constrs) |
15531 | 63 |
in case xs of [] => NONE | x :: _ => SOME x end; |
14104 | 64 |
|
17144 | 65 |
fun add_dt_defs thy defs dep module gr (descr: DatatypeAux.descr) = |
12445 | 66 |
let |
15570 | 67 |
val descr' = List.filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr; |
68 |
val rtnames = map (#1 o snd) (List.filter (fn (_, (_, _, cs)) => |
|
14104 | 69 |
exists (exists DatatypeAux.is_rec_type o snd) cs) descr'); |
12445 | 70 |
|
17144 | 71 |
val (_, (tname, _, _)) :: _ = descr'; |
72 |
val node_id = tname ^ " (type)"; |
|
73 |
val module' = if_library (thyname_of_type tname thy) module; |
|
12445 | 74 |
|
75 |
fun mk_dtdef gr prfx [] = (gr, []) |
|
76 |
| mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) = |
|
77 |
let |
|
78 |
val tvs = map DatatypeAux.dest_DtTFree dts; |
|
79 |
val sorts = map (rpair []) tvs; |
|
80 |
val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs; |
|
17144 | 81 |
val (gr', (_, type_id)) = mk_type_id module' tname gr; |
82 |
val (gr'', ps) = |
|
83 |
foldl_map (fn (gr, (cname, cargs)) => |
|
84 |
foldl_map (invoke_tycodegen thy defs node_id module' false) |
|
85 |
(gr, cargs) |>>> |
|
86 |
mk_const_id module' cname) (gr', cs'); |
|
87 |
val (gr''', rest) = mk_dtdef gr'' "and " xs |
|
12445 | 88 |
in |
17144 | 89 |
(gr''', |
12445 | 90 |
Pretty.block (Pretty.str prfx :: |
91 |
(if null tvs then [] else |
|
92 |
[mk_tuple (map Pretty.str tvs), Pretty.str " "]) @ |
|
17144 | 93 |
[Pretty.str (type_id ^ " ="), Pretty.brk 1] @ |
15570 | 94 |
List.concat (separate [Pretty.brk 1, Pretty.str "| "] |
17144 | 95 |
(map (fn (ps', (_, cname)) => [Pretty.block |
96 |
(Pretty.str cname :: |
|
12445 | 97 |
(if null ps' then [] else |
15570 | 98 |
List.concat ([Pretty.str " of", Pretty.brk 1] :: |
12445 | 99 |
separate [Pretty.str " *", Pretty.brk 1] |
100 |
(map single ps'))))]) ps))) :: rest) |
|
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
101 |
end; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
102 |
|
17144 | 103 |
fun mk_term_of_def gr prfx [] = [] |
104 |
| 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
|
105 |
let |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
106 |
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
|
107 |
val sorts = map (rpair []) tvs; |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
val T = Type (tname, dts'); |
17144 | 111 |
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
|
112 |
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
|
113 |
let val args = map (fn i => |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
114 |
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
|
115 |
in (" | ", Pretty.blk (4, |
17144 | 116 |
[Pretty.str prfx, mk_term_of gr module' false T, Pretty.brk 1, |
117 |
if null Ts then Pretty.str (snd (get_const_id cname gr)) |
|
16645 | 118 |
else parens (Pretty.block |
17144 | 119 |
[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
|
120 |
Pretty.brk 1, mk_tuple args]), |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
121 |
Pretty.str " =", Pretty.brk 1] @ |
15570 | 122 |
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
|
123 |
([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
|
124 |
mk_type false (Ts ---> T), Pretty.str ")"] :: |
17144 | 125 |
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
|
126 |
Pretty.brk 1, x]]) (args ~~ Ts))))) |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
127 |
end) (prfx, cs') |
14104 | 128 |
in eqs @ rest end; |
129 |
||
17144 | 130 |
fun mk_gen_of_def gr prfx [] = [] |
131 |
| mk_gen_of_def gr prfx ((i, (tname, dts, cs)) :: xs) = |
|
14104 | 132 |
let |
133 |
val tvs = map DatatypeAux.dest_DtTFree dts; |
|
134 |
val sorts = map (rpair []) tvs; |
|
135 |
val (cs1, cs2) = |
|
15570 | 136 |
List.partition (exists DatatypeAux.is_rec_type o snd) cs; |
15531 | 137 |
val SOME (cname, _) = find_nonempty descr [i] i; |
14104 | 138 |
|
139 |
fun mk_delay p = Pretty.block |
|
140 |
[Pretty.str "fn () =>", Pretty.brk 1, p]; |
|
141 |
||
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
142 |
fun mk_constr s b (cname, dts) = |
14104 | 143 |
let |
17144 | 144 |
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
|
145 |
(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
|
146 |
[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
|
147 |
else "j")]) dts; |
17144 | 148 |
val (_, id) = get_const_id cname gr |
14104 | 149 |
in case gs of |
150 |
_ :: _ :: _ => Pretty.block |
|
151 |
[Pretty.str id, Pretty.brk 1, mk_tuple gs] |
|
152 |
| _ => mk_app false (Pretty.str id) (map parens gs) |
|
153 |
end; |
|
154 |
||
15397
5260ac75e07c
Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents:
14981
diff
changeset
|
155 |
fun mk_choice [c] = mk_constr "(i-1)" false c |
14104 | 156 |
| mk_choice cs = Pretty.block [Pretty.str "one_of", |
157 |
Pretty.brk 1, Pretty.blk (1, Pretty.str "[" :: |
|
15570 | 158 |
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
|
159 |
(map (single o mk_delay o mk_constr "(i-1)" false) cs)) @ |
14104 | 160 |
[Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]; |
161 |
||
162 |
val gs = map (Pretty.str o suffix "G" o strip_tname) tvs; |
|
17144 | 163 |
val gen_name = "gen_" ^ snd (get_type_id tname gr) |
14104 | 164 |
|
165 |
in |
|
166 |
Pretty.blk (4, separate (Pretty.brk 1) |
|
167 |
(Pretty.str (prfx ^ gen_name ^ |
|
168 |
(if null cs1 then "" else "'")) :: gs @ |
|
169 |
(if null cs1 then [] else [Pretty.str "i"]) @ |
|
170 |
[Pretty.str "j"]) @ |
|
171 |
[Pretty.str " =", Pretty.brk 1] @ |
|
172 |
(if not (null cs1) andalso not (null cs2) |
|
173 |
then [Pretty.str "frequency", Pretty.brk 1, |
|
174 |
Pretty.blk (1, [Pretty.str "[", |
|
175 |
mk_tuple [Pretty.str "i", mk_delay (mk_choice cs1)], |
|
176 |
Pretty.str ",", Pretty.fbrk, |
|
177 |
mk_tuple [Pretty.str "1", mk_delay (mk_choice cs2)], |
|
178 |
Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"] |
|
179 |
else if null cs2 then |
|
180 |
[Pretty.block [Pretty.str "(case", Pretty.brk 1, |
|
181 |
Pretty.str "i", Pretty.brk 1, Pretty.str "of", |
|
182 |
Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1, |
|
17521 | 183 |
mk_constr "0" true (cname, valOf (AList.lookup (op =) cs cname)), |
14104 | 184 |
Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1, |
185 |
mk_choice cs1, Pretty.str ")"]] |
|
186 |
else [mk_choice cs2])) :: |
|
187 |
(if null cs1 then [] |
|
188 |
else [Pretty.blk (4, separate (Pretty.brk 1) |
|
189 |
(Pretty.str ("and " ^ gen_name) :: gs @ [Pretty.str "i"]) @ |
|
190 |
[Pretty.str " =", Pretty.brk 1] @ |
|
191 |
separate (Pretty.brk 1) (Pretty.str (gen_name ^ "'") :: gs @ |
|
192 |
[Pretty.str "i", Pretty.str "i"]))]) @ |
|
17144 | 193 |
mk_gen_of_def gr "and " xs |
14104 | 194 |
end |
13754
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
195 |
|
12445 | 196 |
in |
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
197 |
((add_edge_acyclic (node_id, dep) gr |
12445 | 198 |
handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ => |
199 |
let |
|
17144 | 200 |
val gr1 = add_edge (node_id, dep) |
201 |
(new_node (node_id, (NONE, "", "")) gr); |
|
12445 | 202 |
val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr'; |
203 |
in |
|
17144 | 204 |
map_node node_id (K (NONE, module', |
12445 | 205 |
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
|
206 |
[Pretty.str ";"])) ^ "\n\n" ^ |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
207 |
(if "term_of" mem !mode then |
021cf00435a9
Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents:
12890
diff
changeset
|
208 |
Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk |
17144 | 209 |
(mk_term_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n" |
14104 | 210 |
else "") ^ |
211 |
(if "test" mem !mode then |
|
212 |
Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk |
|
17144 | 213 |
(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
|
214 |
else ""))) gr2 |
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
215 |
end, |
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
216 |
module') |
12445 | 217 |
end; |
218 |
||
219 |
||
220 |
(**** case expressions ****) |
|
221 |
||
17144 | 222 |
fun pretty_case thy defs gr dep module brack constrs (c as Const (_, T)) ts = |
12445 | 223 |
let val i = length constrs |
224 |
in if length ts <= i then |
|
17144 | 225 |
invoke_codegen thy defs dep module brack (gr, eta_expand c ts (i+1)) |
12445 | 226 |
else |
227 |
let |
|
15570 | 228 |
val ts1 = Library.take (i, ts); |
229 |
val t :: ts2 = Library.drop (i, ts); |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
230 |
val names = foldr add_term_names |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
231 |
(map (fst o fst o dest_Var) (foldr add_term_vars [] ts1)) ts1; |
15570 | 232 |
val (Ts, dT) = split_last (Library.take (i+1, fst (strip_type T))); |
12445 | 233 |
|
234 |
fun pcase gr [] [] [] = ([], gr) |
|
235 |
| pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) = |
|
236 |
let |
|
237 |
val j = length cargs; |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19886
diff
changeset
|
238 |
val xs = Name.variant_list names (replicate j "x"); |
15570 | 239 |
val Us' = Library.take (j, fst (strip_type U)); |
12445 | 240 |
val frees = map Free (xs ~~ Us'); |
17144 | 241 |
val (gr0, cp) = invoke_codegen thy defs dep module false |
12445 | 242 |
(gr, list_comb (Const (cname, Us' ---> dT), frees)); |
243 |
val t' = Envir.beta_norm (list_comb (t, frees)); |
|
17144 | 244 |
val (gr1, p) = invoke_codegen thy defs dep module false (gr0, t'); |
12445 | 245 |
val (ps, gr2) = pcase gr1 cs ts Us; |
246 |
in |
|
247 |
([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2) |
|
248 |
end; |
|
249 |
||
250 |
val (ps1, gr1) = pcase gr constrs ts1 Ts; |
|
15570 | 251 |
val ps = List.concat (separate [Pretty.brk 1, Pretty.str "| "] ps1); |
17144 | 252 |
val (gr2, p) = invoke_codegen thy defs dep module false (gr1, t); |
253 |
val (gr3, ps2) = foldl_map (invoke_codegen thy defs dep module true) (gr2, ts2) |
|
12445 | 254 |
in (gr3, (if not (null ts2) andalso brack then parens else I) |
255 |
(Pretty.block (separate (Pretty.brk 1) |
|
256 |
(Pretty.block ([Pretty.str "(case ", p, Pretty.str " of", |
|
257 |
Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2)))) |
|
258 |
end |
|
259 |
end; |
|
260 |
||
261 |
||
262 |
(**** constructors ****) |
|
263 |
||
17144 | 264 |
fun pretty_constr thy defs gr dep module brack args (c as Const (s, T)) ts = |
12445 | 265 |
let val i = length args |
266 |
in if i > 1 andalso length ts < i then |
|
17144 | 267 |
invoke_codegen thy defs dep module brack (gr, eta_expand c ts i) |
12445 | 268 |
else |
269 |
let |
|
17144 | 270 |
val id = mk_qual_id module (get_const_id s gr); |
16645 | 271 |
val (gr', ps) = foldl_map |
17144 | 272 |
(invoke_codegen thy defs dep module (i = 1)) (gr, ts); |
12445 | 273 |
in (case args of |
274 |
_ :: _ :: _ => (gr', (if brack then parens else I) |
|
275 |
(Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps])) |
|
276 |
| _ => (gr', mk_app brack (Pretty.str id) ps)) |
|
277 |
end |
|
278 |
end; |
|
279 |
||
280 |
||
281 |
(**** code generators for terms and types ****) |
|
282 |
||
17144 | 283 |
fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of |
12445 | 284 |
(c as Const (s, T), ts) => |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
285 |
(case DatatypePackage.datatype_of_case thy s of |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
286 |
SOME {index, descr, ...} => |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
287 |
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
|
288 |
SOME (pretty_case thy defs gr dep module brack |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
289 |
(#3 (the (AList.lookup op = descr index))) c ts) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
290 |
| 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
|
291 |
(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
|
292 |
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
|
293 |
let val SOME args = AList.lookup op = |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
294 |
(#3 (the (AList.lookup op = descr index))) s |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
295 |
in |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
296 |
SOME (pretty_constr thy defs |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
297 |
(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
|
298 |
dep module brack args c ts) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
299 |
end |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
300 |
| _ => NONE) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
301 |
| _ => NONE); |
12445 | 302 |
|
17144 | 303 |
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
|
304 |
(case DatatypePackage.get_datatype thy s of |
15531 | 305 |
NONE => NONE |
306 |
| SOME {descr, ...} => |
|
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22778
diff
changeset
|
307 |
if is_some (get_assoc_type thy s) then NONE else |
16645 | 308 |
let |
309 |
val (gr', ps) = foldl_map |
|
17144 | 310 |
(invoke_tycodegen thy defs dep module false) (gr, Ts); |
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
311 |
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
|
312 |
val (gr''', tyid) = mk_type_id module' s gr'' |
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
313 |
in SOME (gr''', |
12445 | 314 |
Pretty.block ((if null Ts then [] else |
315 |
[mk_tuple ps, Pretty.str " "]) @ |
|
20681
0e4df994ad34
Fixed bug concerning the generation of identifiers for
berghofe
parents:
20608
diff
changeset
|
316 |
[Pretty.str (mk_qual_id module tyid)])) |
12445 | 317 |
end) |
16645 | 318 |
| datatype_tycodegen _ _ _ _ _ _ _ = NONE; |
12445 | 319 |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18220
diff
changeset
|
320 |
|
20426 | 321 |
(** datatypes for code 2nd generation **) |
20177
0af885e3dabf
hooks now take string list as arguments (mutual datatypes); some nice combinators in datatype_codegen
haftmann
parents:
20105
diff
changeset
|
322 |
|
20105 | 323 |
fun dtyp_of_case_const thy c = |
22778
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
324 |
Option.map (fn {descr, index, ...} => #1 (the (AList.lookup op = descr index))) |
a5b87573f427
Streamlined datatype_codegen function using new datatype_of_case
berghofe
parents:
22746
diff
changeset
|
325 |
(DatatypePackage.datatype_of_case thy c); |
20105 | 326 |
|
327 |
fun dest_case_app cs ts tys = |
|
328 |
let |
|
22052 | 329 |
val names = (Name.make_context o map fst) (fold Term.add_tfrees ts []); |
330 |
val abs = Name.names names "a" (Library.drop (length ts, tys)); |
|
20105 | 331 |
val (ts', t) = split_last (ts @ map Free abs); |
332 |
val (tys', sty) = split_last tys; |
|
22051 | 333 |
fun dest_case ((c, tys_decl), ty) t = |
334 |
let |
|
335 |
val (vs, t') = Term.strip_abs_eta (length tys_decl) t; |
|
336 |
val c' = list_comb (Const (c, map snd vs ---> sty), map Free vs); |
|
337 |
in case t' |
|
22480 | 338 |
of Const ("HOL.undefined", _) => NONE |
22051 | 339 |
| _ => SOME (c', t') |
340 |
end; |
|
22047 | 341 |
in (abs, ((t, sty), map2 dest_case (cs ~~ tys') ts' |> map_filter I)) end; |
20105 | 342 |
|
343 |
fun dest_case_expr thy t = |
|
344 |
case strip_comb t |
|
345 |
of (Const (c, ty), ts) => |
|
346 |
(case dtyp_of_case_const thy c |
|
347 |
of SOME dtco => |
|
348 |
let val (vs, cs) = (the o DatatypePackage.get_datatype_spec thy) dtco; |
|
349 |
in SOME (dest_case_app cs ts (Library.take (length cs + 1, (fst o strip_type) ty))) end |
|
350 |
| _ => NONE) |
|
351 |
| _ => NONE; |
|
352 |
||
20426 | 353 |
fun mk_distinct cos = |
354 |
let |
|
355 |
fun sym_product [] = [] |
|
356 |
| sym_product (x::xs) = map (pair x) xs @ sym_product xs; |
|
357 |
fun mk_co_args (co, tys) ctxt = |
|
358 |
let |
|
359 |
val names = Name.invents ctxt "a" (length tys); |
|
360 |
val ctxt' = fold Name.declare names ctxt; |
|
361 |
val vs = map2 (curry Free) names tys; |
|
20835 | 362 |
in (vs, ctxt') end; |
20426 | 363 |
fun mk_dist ((co1, tys1), (co2, tys2)) = |
364 |
let |
|
365 |
val ((xs1, xs2), _) = Name.context |
|
366 |
|> mk_co_args (co1, tys1) |
|
367 |
||>> mk_co_args (co2, tys2); |
|
368 |
val prem = HOLogic.mk_eq |
|
369 |
(list_comb (co1, xs1), list_comb (co2, xs2)); |
|
370 |
val t = HOLogic.mk_not prem; |
|
371 |
in HOLogic.mk_Trueprop t end; |
|
372 |
in map mk_dist (sym_product cos) end; |
|
373 |
||
374 |
local |
|
20835 | 375 |
val not_sym = thm "HOL.not_sym"; |
376 |
val not_false_true = iffD2 OF [nth (thms "HOL.simp_thms") 7, TrueI]; |
|
21546 | 377 |
val refl = thm "refl"; |
378 |
val eqTrueI = thm "eqTrueI"; |
|
21516 | 379 |
in |
380 |
||
381 |
fun get_eq_datatype thy dtco = |
|
20835 | 382 |
let |
383 |
val SOME (vs, cs) = DatatypePackage.get_datatype_spec thy dtco; |
|
384 |
fun mk_triv_inject co = |
|
385 |
let |
|
386 |
val ct' = Thm.cterm_of thy |
|
387 |
(Const (co, Type (dtco, map (fn (v, sort) => TVar ((v, 0), sort)) vs))) |
|
388 |
val cty' = Thm.ctyp_of_term ct'; |
|
389 |
val SOME (ct, cty) = fold_aterms (fn Var (v, ty) => |
|
390 |
(K o SOME) (Thm.cterm_of thy (Var (v, Thm.typ_of cty')), Thm.ctyp_of thy ty) | _ => I) |
|
21546 | 391 |
(Thm.prop_of refl) NONE; |
392 |
in eqTrueI OF [Thm.instantiate ([(cty, cty')], [(ct, ct')]) refl] end; |
|
20835 | 393 |
val inject1 = map_filter (fn (co, []) => SOME (mk_triv_inject co) | _ => NONE) cs |
394 |
val inject2 = (#inject o DatatypePackage.the_datatype thy) dtco; |
|
21516 | 395 |
val ctxt = ProofContext.init thy; |
20835 | 396 |
val simpset = Simplifier.context ctxt |
397 |
(MetaSimplifier.empty_ss addsimprocs [distinct_simproc]); |
|
398 |
val cos = map (fn (co, tys) => |
|
399 |
(Const (co, tys ---> Type (dtco, map TFree vs)), tys)) cs; |
|
400 |
val tac = ALLGOALS (simp_tac simpset) |
|
401 |
THEN ALLGOALS (ProofContext.fact_tac [not_false_true, TrueI]); |
|
402 |
val distinct = |
|
403 |
mk_distinct cos |
|
404 |
|> map (fn t => Goal.prove_global thy [] [] t (K tac)) |
|
405 |
|> (fn thms => thms @ map (fn thm => not_sym OF [thm]) thms) |
|
406 |
in inject1 @ inject2 @ distinct end; |
|
21516 | 407 |
|
408 |
end; |
|
19346 | 409 |
|
23811 | 410 |
fun get_case_cert thy tyco = |
411 |
let |
|
412 |
val raw_thms = |
|
413 |
(#case_rewrites o DatatypePackage.the_datatype thy) tyco; |
|
414 |
val thms as hd_thm :: _ = raw_thms |
|
415 |
|> Conjunction.intr_balanced |
|
416 |
|> Drule.unvarify |
|
417 |
|> Conjunction.elim_balanced (length raw_thms) |
|
418 |
|> map Simpdata.mk_meta_eq |
|
419 |
|> map Drule.zero_var_indexes |
|
420 |
val params = fold_aterms (fn (Free (v, _)) => insert (op =) v |
|
421 |
| _ => I) (Thm.prop_of hd_thm) []; |
|
422 |
val rhs = hd_thm |
|
423 |
|> Thm.prop_of |
|
424 |
|> Logic.dest_equals |
|
425 |
|> fst |
|
426 |
|> Term.strip_comb |
|
427 |
|> apsnd (fst o split_last) |
|
428 |
|> list_comb; |
|
429 |
val lhs = Free (Name.variant params "case", Term.fastype_of rhs); |
|
430 |
val asm = (Thm.cterm_of thy o Logic.mk_equals) (lhs, rhs); |
|
431 |
in |
|
432 |
thms |
|
433 |
|> Conjunction.intr_balanced |
|
434 |
|> MetaSimplifier.rewrite_rule [(Thm.symmetric o Thm.assume) asm] |
|
435 |
|> Thm.implies_intr asm |
|
436 |
|> Thm.generalize ([], params) 0 |
|
437 |
|> Thm.varifyT |
|
438 |
end; |
|
439 |
||
19818 | 440 |
|
20426 | 441 |
|
442 |
(** codetypes for code 2nd generation **) |
|
443 |
||
20835 | 444 |
(* abstraction over datatypes vs. type copies *) |
20426 | 445 |
|
446 |
fun codetypes_dependency thy = |
|
447 |
let |
|
448 |
val names = |
|
449 |
map (rpair true) (Symtab.keys (DatatypePackage.get_datatypes thy)) |
|
450 |
@ map (rpair false) (TypecopyPackage.get_typecopies thy); |
|
451 |
fun add_node (name, is_dt) = |
|
452 |
let |
|
453 |
fun add_tycos (Type (tyco, tys)) = insert (op =) tyco #> fold add_tycos tys |
|
454 |
| add_tycos _ = I; |
|
455 |
val tys = if is_dt then |
|
456 |
(maps snd o snd o the o DatatypePackage.get_datatype_spec thy) name |
|
457 |
else |
|
458 |
[(#typ o the o TypecopyPackage.get_typecopy_info thy) name] |
|
459 |
val deps = (filter (AList.defined (op =) names) o maps (fn ty => |
|
460 |
add_tycos ty [])) tys; |
|
461 |
in |
|
462 |
Graph.default_node (name, ()) |
|
463 |
#> fold (fn name' => |
|
464 |
Graph.default_node (name', ()) |
|
465 |
#> Graph.add_edge (name', name) |
|
466 |
) deps |
|
467 |
end |
|
468 |
in |
|
469 |
Graph.empty |
|
470 |
|> fold add_node names |
|
471 |
|> Graph.strong_conn |
|
472 |
|> map (AList.make (the o AList.lookup (op =) names)) |
|
473 |
end; |
|
474 |
||
475 |
fun get_spec thy (dtco, true) = |
|
476 |
(the o DatatypePackage.get_datatype_spec thy) dtco |
|
477 |
| get_spec thy (tyco, false) = |
|
20835 | 478 |
TypecopyPackage.get_spec thy tyco; |
479 |
||
480 |
local |
|
481 |
fun get_eq_thms thy tyco = case DatatypePackage.get_datatype thy tyco |
|
482 |
of SOME _ => get_eq_datatype thy tyco |
|
483 |
| NONE => [TypecopyPackage.get_eq thy tyco]; |
|
21546 | 484 |
fun constrain_op_eq_thms thy thms = |
485 |
let |
|
486 |
fun add_eq (Const ("op =", ty)) = |
|
23712 | 487 |
fold (insert (eq_fst (op =))) (Term.add_tvarsT ty []) |
21546 | 488 |
| add_eq _ = |
489 |
I |
|
490 |
val eqs = fold (fold_aterms add_eq o Thm.prop_of) thms []; |
|
491 |
val instT = map (fn (v_i, sort) => |
|
492 |
(Thm.ctyp_of thy (TVar (v_i, sort)), |
|
23712 | 493 |
Thm.ctyp_of thy (TVar (v_i, Sorts.inter_sort (Sign.classes_of thy) |
494 |
(sort, [HOLogic.class_eq]))))) eqs; |
|
21546 | 495 |
in |
496 |
thms |
|
497 |
|> map (Thm.instantiate (instT, [])) |
|
498 |
end; |
|
20835 | 499 |
in |
500 |
fun get_eq thy tyco = |
|
501 |
get_eq_thms thy tyco |
|
502 |
|> maps ((#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy) |
|
21546 | 503 |
|> constrain_op_eq_thms thy |
20835 | 504 |
end; |
505 |
||
506 |
type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list |
|
507 |
-> theory -> theory; |
|
20426 | 508 |
|
509 |
fun add_codetypes_hook_bootstrap hook thy = |
|
510 |
let |
|
20835 | 511 |
fun add_spec thy (tyco, is_dt) = |
512 |
(tyco, (is_dt, get_spec thy (tyco, is_dt))); |
|
20426 | 513 |
fun datatype_hook dtcos thy = |
514 |
hook (map (add_spec thy) (map (rpair true) dtcos)) thy; |
|
20835 | 515 |
fun typecopy_hook ((tyco, _)) thy = |
516 |
hook ([(tyco, (false, TypecopyPackage.get_spec thy tyco))]) thy; |
|
20426 | 517 |
in |
518 |
thy |
|
519 |
|> fold hook ((map o map) (add_spec thy) (codetypes_dependency thy)) |
|
520 |
|> DatatypeHooks.add datatype_hook |
|
521 |
|> TypecopyPackage.add_hook typecopy_hook |
|
522 |
end; |
|
523 |
||
524 |
fun the_codetypes_mut_specs thy ([(tyco, is_dt)]) = |
|
525 |
let |
|
526 |
val (vs, cs) = get_spec thy (tyco, is_dt) |
|
527 |
in (vs, [(tyco, (is_dt, cs))]) end |
|
528 |
| the_codetypes_mut_specs thy (tycos' as (tyco, true) :: _) = |
|
529 |
let |
|
530 |
val tycos = map fst tycos'; |
|
531 |
val tycos'' = (map (#1 o snd) o #descr o DatatypePackage.the_datatype thy) tyco; |
|
532 |
val _ = if gen_subset (op =) (tycos, tycos'') then () else |
|
22175 | 533 |
error ("type constructors are not mutually recursive: " ^ (commas o map quote) tycos); |
20426 | 534 |
val (vs::_, css) = split_list (map (the o DatatypePackage.get_datatype_spec thy) tycos); |
535 |
in (vs, map2 (fn (tyco, is_dt) => fn cs => (tyco, (is_dt, cs))) tycos' css) end; |
|
536 |
||
20835 | 537 |
|
538 |
(* registering code types in code generator *) |
|
539 |
||
22435 | 540 |
fun codetype_hook dtspecs = |
24219 | 541 |
fold (fn (dtco, (_, spec)) => Code.add_datatype (dtco, spec)) dtspecs; |
20835 | 542 |
|
543 |
||
544 |
(* instrumentalizing the sort algebra *) |
|
545 |
||
22423 | 546 |
fun get_codetypes_arities thy tycos sort = |
547 |
let |
|
548 |
val pp = Sign.pp thy; |
|
549 |
val algebra = Sign.classes_of thy; |
|
550 |
val (vs_proto, css_proto) = the_codetypes_mut_specs thy tycos; |
|
551 |
val vs = map (fn (v, vsort) => (v, Sorts.inter_sort algebra (vsort, sort))) vs_proto; |
|
552 |
val css = map (fn (tyco, (_, cs)) => (tyco, cs)) css_proto; |
|
553 |
val algebra' = algebra |
|
554 |
|> fold (fn (tyco, _) => |
|
555 |
Sorts.add_arities pp (tyco, map (fn class => (class, map snd vs)) sort)) css; |
|
24219 | 556 |
fun typ_sort_inst ty = CodeUnit.typ_sort_inst algebra' (Logic.varifyT ty, sort); |
22423 | 557 |
val venv = Vartab.empty |
558 |
|> fold (fn (v, sort) => Vartab.update_new ((v, 0), sort)) vs |
|
559 |
|> fold (fn (_, cs) => fold (fn (_, tys) => fold typ_sort_inst tys) cs) css; |
|
560 |
fun inst (v, _) = (v, (the o Vartab.lookup venv) (v, 0)); |
|
561 |
val vs' = map inst vs; |
|
562 |
fun mk_arity tyco = (tyco, map snd vs', sort); |
|
563 |
fun mk_cons tyco (c, tys) = |
|
564 |
let |
|
565 |
val tys' = (map o Term.map_type_tfree) (TFree o inst) tys; |
|
566 |
val ts = Name.names Name.context "a" tys'; |
|
567 |
val ty = (tys' ---> Type (tyco, map TFree vs')); |
|
568 |
in list_comb (Const (c, ty), map Free ts) end; |
|
569 |
in |
|
570 |
map (fn (tyco, cs) => (tyco, (mk_arity tyco, map (mk_cons tyco) cs))) css |> SOME |
|
571 |
end handle Class_Error => NONE; |
|
20426 | 572 |
|
20597 | 573 |
fun prove_codetypes_arities tac tycos sort f after_qed thy = |
20426 | 574 |
case get_codetypes_arities thy tycos sort |
575 |
of NONE => thy |
|
576 |
| SOME insts => let |
|
22331 | 577 |
fun proven (tyco, asorts, sort) = |
20426 | 578 |
Sorts.of_sort (Sign.classes_of thy) |
579 |
(Type (tyco, map TFree (Name.names Name.context "'a" asorts)), sort); |
|
580 |
val (arities, css) = (split_list o map_filter |
|
581 |
(fn (tyco, (arity, cs)) => if proven arity |
|
582 |
then NONE else SOME (arity, (tyco, cs)))) insts; |
|
583 |
in |
|
584 |
thy |
|
21565 | 585 |
|> not (null arities) ? ( |
20835 | 586 |
f arities css |
587 |
#-> (fn defs => |
|
24219 | 588 |
Class.prove_instance_arity tac arities defs |
20835 | 589 |
#> after_qed arities css)) |
20426 | 590 |
end; |
591 |
||
20835 | 592 |
|
593 |
(* operational equality *) |
|
594 |
||
21046
fe1db2f991a7
moved HOL code generator setup to Code_Generator
haftmann
parents:
21012
diff
changeset
|
595 |
fun eq_hook specs = |
20597 | 596 |
let |
20835 | 597 |
fun add_eq_thms (dtco, (_, (vs, cs))) thy = |
20597 | 598 |
let |
24137
8d7896398147
replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents:
23811
diff
changeset
|
599 |
val thy_ref = Theory.check_thy thy; |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22480
diff
changeset
|
600 |
val const = ("op =", SOME dtco); |
20835 | 601 |
val get_thms = (fn () => get_eq (Theory.deref thy_ref) dtco |> rev); |
20597 | 602 |
in |
24219 | 603 |
Code.add_funcl (const, Susp.delay get_thms) thy |
20597 | 604 |
end; |
605 |
in |
|
24219 | 606 |
prove_codetypes_arities (Class.intro_classes_tac []) |
20835 | 607 |
(map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs) |
21046
fe1db2f991a7
moved HOL code generator setup to Code_Generator
haftmann
parents:
21012
diff
changeset
|
608 |
[HOLogic.class_eq] ((K o K o pair) []) ((K o K) (fold add_eq_thms specs)) |
20597 | 609 |
end; |
610 |
||
20426 | 611 |
|
612 |
||
613 |
(** theory setup **) |
|
614 |
||
23513 | 615 |
fun add_datatype_case_const dtco thy = |
616 |
let |
|
617 |
val {case_name, index, descr, ...} = DatatypePackage.the_datatype thy dtco; |
|
618 |
in |
|
24219 | 619 |
CodePackage.add_appconst (case_name, CodePackage.appgen_case dest_case_expr) thy |
23513 | 620 |
end; |
621 |
||
622 |
fun add_datatype_case_defs dtco thy = |
|
623 |
let |
|
624 |
val {case_rewrites, ...} = DatatypePackage.the_datatype thy dtco |
|
625 |
in |
|
24219 | 626 |
fold_rev (Code.add_func true) case_rewrites thy |
23513 | 627 |
end; |
628 |
||
19008 | 629 |
val setup = |
20597 | 630 |
add_codegen "datatype" datatype_codegen |
631 |
#> add_tycodegen "datatype" datatype_tycodegen |
|
632 |
#> DatatypeHooks.add (fold add_datatype_case_const) |
|
633 |
#> DatatypeHooks.add (fold add_datatype_case_defs) |
|
634 |
||
20835 | 635 |
val setup_hooks = |
20597 | 636 |
add_codetypes_hook_bootstrap codetype_hook |
637 |
#> add_codetypes_hook_bootstrap eq_hook |
|
638 |
||
12445 | 639 |
|
640 |
end; |