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