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