blanchet@50127
|
1 |
(* Title: HOL/Codatatype/Tools/bnf_fp_sugar.ML
|
blanchet@50127
|
2 |
Author: Jasmin Blanchette, TU Muenchen
|
blanchet@50127
|
3 |
Copyright 2012
|
blanchet@50127
|
4 |
|
blanchet@50127
|
5 |
Sugar for constructing LFPs and GFPs.
|
blanchet@50127
|
6 |
*)
|
blanchet@50127
|
7 |
|
blanchet@50127
|
8 |
signature BNF_FP_SUGAR =
|
blanchet@50127
|
9 |
sig
|
blanchet@50220
|
10 |
(* TODO: programmatic interface *)
|
blanchet@50127
|
11 |
end;
|
blanchet@50127
|
12 |
|
blanchet@50127
|
13 |
structure BNF_FP_Sugar : BNF_FP_SUGAR =
|
blanchet@50127
|
14 |
struct
|
blanchet@50127
|
15 |
|
blanchet@50134
|
16 |
open BNF_Util
|
blanchet@50134
|
17 |
open BNF_Wrap
|
blanchet@50134
|
18 |
open BNF_FP_Util
|
blanchet@50134
|
19 |
open BNF_LFP
|
blanchet@50134
|
20 |
open BNF_GFP
|
blanchet@50138
|
21 |
open BNF_FP_Sugar_Tactics
|
blanchet@50134
|
22 |
|
blanchet@50144
|
23 |
val caseN = "case";
|
blanchet@50217
|
24 |
val itersN = "iters";
|
blanchet@50217
|
25 |
val recsN = "recs";
|
blanchet@50144
|
26 |
|
blanchet@50220
|
27 |
fun split_list7 xs = (map #1 xs, map #2 xs, map #3 xs, map #4 xs, map #5 xs, map #6 xs, map #7 xs);
|
blanchet@50220
|
28 |
|
blanchet@50214
|
29 |
fun retype_free (Free (s, _)) T = Free (s, T);
|
blanchet@50214
|
30 |
|
blanchet@50217
|
31 |
fun flat_list_comb (f, xss) = fold (fn xs => fn t => Term.list_comb (t, xs)) xss f
|
blanchet@50217
|
32 |
|
blanchet@50215
|
33 |
fun mk_tupled_fun x f xs = HOLogic.tupled_lambda x (Term.list_comb (f, xs));
|
blanchet@50215
|
34 |
fun mk_uncurried_fun f xs = mk_tupled_fun (HOLogic.mk_tuple xs) f xs;
|
blanchet@50217
|
35 |
fun mk_uncurried2_fun f xss =
|
blanchet@50215
|
36 |
mk_tupled_fun (HOLogic.mk_tuple (map HOLogic.mk_tuple xss)) f (flat xss);
|
blanchet@50215
|
37 |
|
blanchet@50139
|
38 |
fun cannot_merge_types () = error "Mutually recursive types must have the same type parameters";
|
blanchet@50134
|
39 |
|
blanchet@50134
|
40 |
fun merge_type_arg_constrained ctxt (T, c) (T', c') =
|
blanchet@50134
|
41 |
if T = T' then
|
blanchet@50134
|
42 |
(case (c, c') of
|
blanchet@50134
|
43 |
(_, NONE) => (T, c)
|
blanchet@50134
|
44 |
| (NONE, _) => (T, c')
|
blanchet@50134
|
45 |
| _ =>
|
blanchet@50134
|
46 |
if c = c' then
|
blanchet@50134
|
47 |
(T, c)
|
blanchet@50134
|
48 |
else
|
blanchet@50134
|
49 |
error ("Inconsistent sort constraints for type variable " ^
|
blanchet@50134
|
50 |
quote (Syntax.string_of_typ ctxt T)))
|
blanchet@50134
|
51 |
else
|
blanchet@50134
|
52 |
cannot_merge_types ();
|
blanchet@50134
|
53 |
|
blanchet@50134
|
54 |
fun merge_type_args_constrained ctxt (cAs, cAs') =
|
blanchet@50134
|
55 |
if length cAs = length cAs' then map2 (merge_type_arg_constrained ctxt) cAs cAs'
|
blanchet@50134
|
56 |
else cannot_merge_types ();
|
blanchet@50134
|
57 |
|
blanchet@50136
|
58 |
fun type_args_constrained_of (((cAs, _), _), _) = cAs;
|
blanchet@50136
|
59 |
val type_args_of = map fst o type_args_constrained_of;
|
blanchet@50144
|
60 |
fun type_binder_of (((_, b), _), _) = b;
|
blanchet@50196
|
61 |
fun mixfix_of ((_, mx), _) = mx;
|
blanchet@50136
|
62 |
fun ctr_specs_of (_, ctr_specs) = ctr_specs;
|
blanchet@50134
|
63 |
|
blanchet@50136
|
64 |
fun disc_of (((disc, _), _), _) = disc;
|
blanchet@50136
|
65 |
fun ctr_of (((_, ctr), _), _) = ctr;
|
blanchet@50136
|
66 |
fun args_of ((_, args), _) = args;
|
blanchet@50196
|
67 |
fun ctr_mixfix_of (_, mx) = mx;
|
blanchet@50134
|
68 |
|
blanchet@50219
|
69 |
fun prepare_datatype prepare_typ gfp specs fake_lthy no_defs_lthy =
|
blanchet@50127
|
70 |
let
|
blanchet@50136
|
71 |
val constrained_As =
|
blanchet@50136
|
72 |
map (map (apfst (prepare_typ fake_lthy)) o type_args_constrained_of) specs
|
blanchet@50219
|
73 |
|> Library.foldr1 (merge_type_args_constrained no_defs_lthy);
|
blanchet@50136
|
74 |
val As = map fst constrained_As;
|
blanchet@50149
|
75 |
val As' = map dest_TFree As;
|
blanchet@50134
|
76 |
|
blanchet@50136
|
77 |
val _ = (case duplicates (op =) As of [] => ()
|
blanchet@50219
|
78 |
| A :: _ => error ("Duplicate type parameter " ^
|
blanchet@50219
|
79 |
quote (Syntax.string_of_typ no_defs_lthy A)));
|
blanchet@50134
|
80 |
|
blanchet@50134
|
81 |
(* TODO: use sort constraints on type args *)
|
blanchet@50134
|
82 |
|
blanchet@50134
|
83 |
val N = length specs;
|
blanchet@50134
|
84 |
|
blanchet@50197
|
85 |
fun mk_fake_T b =
|
blanchet@50136
|
86 |
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))),
|
blanchet@50136
|
87 |
As);
|
blanchet@50136
|
88 |
|
blanchet@50144
|
89 |
val bs = map type_binder_of specs;
|
blanchet@50219
|
90 |
val fakeTs = map mk_fake_T bs;
|
blanchet@50136
|
91 |
|
blanchet@50196
|
92 |
val mixfixes = map mixfix_of specs;
|
blanchet@50134
|
93 |
|
blanchet@50134
|
94 |
val _ = (case duplicates Binding.eq_name bs of [] => ()
|
blanchet@50134
|
95 |
| b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b)));
|
blanchet@50134
|
96 |
|
blanchet@50136
|
97 |
val ctr_specss = map ctr_specs_of specs;
|
blanchet@50134
|
98 |
|
blanchet@50144
|
99 |
val disc_binderss = map (map disc_of) ctr_specss;
|
blanchet@50144
|
100 |
val ctr_binderss = map (map ctr_of) ctr_specss;
|
blanchet@50136
|
101 |
val ctr_argsss = map (map args_of) ctr_specss;
|
blanchet@50196
|
102 |
val ctr_mixfixess = map (map ctr_mixfix_of) ctr_specss;
|
blanchet@50134
|
103 |
|
blanchet@50144
|
104 |
val sel_bindersss = map (map (map fst)) ctr_argsss;
|
blanchet@50198
|
105 |
val fake_ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss;
|
blanchet@50134
|
106 |
|
blanchet@50198
|
107 |
val rhs_As' = fold (fold (fold Term.add_tfreesT)) fake_ctr_Tsss [];
|
blanchet@50182
|
108 |
val _ = (case subtract (op =) As' rhs_As' of
|
blanchet@50180
|
109 |
[] => ()
|
blanchet@50180
|
110 |
| A' :: _ => error ("Extra type variables on rhs: " ^
|
blanchet@50219
|
111 |
quote (Syntax.string_of_typ no_defs_lthy (TFree A'))));
|
blanchet@50180
|
112 |
|
blanchet@50191
|
113 |
val ((Cs, Xs), _) =
|
blanchet@50219
|
114 |
no_defs_lthy
|
blanchet@50136
|
115 |
|> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs
|
blanchet@50136
|
116 |
|> mk_TFrees N
|
blanchet@50191
|
117 |
||>> mk_TFrees N;
|
blanchet@50134
|
118 |
|
blanchet@50219
|
119 |
fun eq_fpT (T as Type (s, Us)) (Type (s', Us')) =
|
blanchet@50161
|
120 |
s = s' andalso (Us = Us' orelse error ("Illegal occurrence of recursive type " ^
|
blanchet@50161
|
121 |
quote (Syntax.string_of_typ fake_lthy T)))
|
blanchet@50219
|
122 |
| eq_fpT _ _ = false;
|
blanchet@50161
|
123 |
|
blanchet@50219
|
124 |
fun freeze_fp (T as Type (s, Us)) =
|
blanchet@50219
|
125 |
(case find_index (eq_fpT T) fakeTs of ~1 => Type (s, map freeze_fp Us) | j => nth Xs j)
|
blanchet@50219
|
126 |
| freeze_fp T = T;
|
blanchet@50134
|
127 |
|
blanchet@50219
|
128 |
val ctr_TsssXs = map (map (map freeze_fp)) fake_ctr_Tsss;
|
blanchet@50191
|
129 |
val sum_prod_TsXs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssXs;
|
blanchet@50134
|
130 |
|
blanchet@50191
|
131 |
val eqs = map dest_TFree Xs ~~ sum_prod_TsXs;
|
blanchet@50136
|
132 |
|
blanchet@50220
|
133 |
val ((unfs0, flds0, fp_iters0, fp_recs0, unf_flds, fld_unfs, fld_injects, fp_iter_thms,
|
blanchet@50220
|
134 |
fp_rec_thms), lthy) =
|
blanchet@50219
|
135 |
fp_bnf (if gfp then bnf_gfp else bnf_lfp) bs mixfixes As' eqs no_defs_lthy;
|
blanchet@50136
|
136 |
|
blanchet@50182
|
137 |
val timer = time (Timer.startRealTimer ());
|
blanchet@50182
|
138 |
|
blanchet@50191
|
139 |
fun mk_unf_or_fld get_T Ts t =
|
blanchet@50191
|
140 |
let val Type (_, Ts0) = get_T (fastype_of t) in
|
blanchet@50139
|
141 |
Term.subst_atomic_types (Ts0 ~~ Ts) t
|
blanchet@50136
|
142 |
end;
|
blanchet@50136
|
143 |
|
blanchet@50141
|
144 |
val mk_unf = mk_unf_or_fld domain_type;
|
blanchet@50141
|
145 |
val mk_fld = mk_unf_or_fld range_type;
|
blanchet@50139
|
146 |
|
blanchet@50218
|
147 |
val unfs = map (mk_unf As) unfs0;
|
blanchet@50218
|
148 |
val flds = map (mk_fld As) flds0;
|
blanchet@50136
|
149 |
|
blanchet@50216
|
150 |
val fpTs = map (domain_type o fastype_of) unfs;
|
blanchet@50219
|
151 |
val is_fpT = member (op =) fpTs;
|
blanchet@50219
|
152 |
|
blanchet@50216
|
153 |
val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctr_TsssXs;
|
blanchet@50218
|
154 |
val ns = map length ctr_Tsss;
|
blanchet@50218
|
155 |
val mss = map (map length) ctr_Tsss;
|
blanchet@50218
|
156 |
val Css = map2 replicate ns Cs;
|
blanchet@50218
|
157 |
val Cs' = flat Css;
|
blanchet@50218
|
158 |
|
blanchet@50218
|
159 |
fun mk_iter_or_rec Ts Us c =
|
blanchet@50134
|
160 |
let
|
blanchet@50216
|
161 |
val (binders, body) = strip_type (fastype_of c);
|
blanchet@50218
|
162 |
val (fst_binders, last_binder) = split_last binders;
|
blanchet@50218
|
163 |
val Type (_, Ts0) = if gfp then body else last_binder;
|
blanchet@50218
|
164 |
val Us0 = map (if gfp then domain_type else body_type) fst_binders;
|
blanchet@50191
|
165 |
in
|
blanchet@50216
|
166 |
Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) c
|
blanchet@50191
|
167 |
end;
|
blanchet@50191
|
168 |
|
blanchet@50219
|
169 |
val fp_iters as fp_iter1 :: _ = map (mk_iter_or_rec As Cs) fp_iters0;
|
blanchet@50219
|
170 |
val fp_recs as fp_rec1 :: _ = map (mk_iter_or_rec As Cs) fp_recs0;
|
blanchet@50219
|
171 |
|
blanchet@50219
|
172 |
val fp_y_Ts = map domain_type (fst (split_last (binder_types (fastype_of fp_iter1))));
|
blanchet@50219
|
173 |
val y_Tsss = map3 (fn ms => map2 dest_tupleT ms oo dest_sumTN) mss ns fp_y_Ts;
|
blanchet@50219
|
174 |
val g_Tss = map2 (map2 (curry (op --->))) y_Tsss Css;
|
blanchet@50219
|
175 |
|
blanchet@50219
|
176 |
fun dest_rec_pair (T as Type (@{type_name prod}, Us as [_, U])) =
|
blanchet@50219
|
177 |
if member (op =) Cs U then Us else [T]
|
blanchet@50219
|
178 |
| dest_rec_pair T = [T];
|
blanchet@50219
|
179 |
|
blanchet@50219
|
180 |
val fp_z_Ts = map domain_type (fst (split_last (binder_types (fastype_of fp_rec1))));
|
blanchet@50219
|
181 |
val z_Tssss =
|
blanchet@50219
|
182 |
map3 (fn ms => map2 (map dest_rec_pair oo dest_tupleT) ms oo dest_sumTN) mss ns fp_z_Ts;
|
blanchet@50219
|
183 |
val h_Tss = map2 (map2 (fold_rev (curry (op --->)))) z_Tssss Css;
|
blanchet@50219
|
184 |
|
blanchet@50219
|
185 |
val ((gss, ysss), _) =
|
blanchet@50219
|
186 |
lthy
|
blanchet@50219
|
187 |
|> mk_Freess "f" g_Tss
|
blanchet@50219
|
188 |
||>> mk_Freesss "x" y_Tsss;
|
blanchet@50219
|
189 |
|
blanchet@50219
|
190 |
val hss = map2 (map2 retype_free) gss h_Tss;
|
blanchet@50219
|
191 |
val (zssss, _) =
|
blanchet@50219
|
192 |
lthy
|
blanchet@50219
|
193 |
|> mk_Freessss "x" z_Tssss;
|
blanchet@50191
|
194 |
|
blanchet@50216
|
195 |
fun pour_sugar_on_type ((((((((((((((b, fpT), C), fld), unf), fp_iter), fp_rec), fld_unf),
|
blanchet@50191
|
196 |
unf_fld), fld_inject), ctr_binders), ctr_mixfixes), ctr_Tss), disc_binders), sel_binderss)
|
blanchet@50191
|
197 |
no_defs_lthy =
|
blanchet@50191
|
198 |
let
|
blanchet@50191
|
199 |
val n = length ctr_Tss;
|
blanchet@50136
|
200 |
val ks = 1 upto n;
|
blanchet@50136
|
201 |
val ms = map length ctr_Tss;
|
blanchet@50136
|
202 |
|
blanchet@50216
|
203 |
val unfT = domain_type (fastype_of fld);
|
blanchet@50144
|
204 |
val prod_Ts = map HOLogic.mk_tupleT ctr_Tss;
|
blanchet@50149
|
205 |
val case_Ts = map (fn Ts => Ts ---> C) ctr_Tss;
|
blanchet@50139
|
206 |
|
blanchet@50191
|
207 |
val ((((u, v), fs), xss), _) =
|
blanchet@50219
|
208 |
no_defs_lthy
|
blanchet@50216
|
209 |
|> yield_singleton (mk_Frees "u") unfT
|
blanchet@50216
|
210 |
||>> yield_singleton (mk_Frees "v") fpT
|
blanchet@50191
|
211 |
||>> mk_Frees "f" case_Ts
|
blanchet@50139
|
212 |
||>> mk_Freess "x" ctr_Tss;
|
blanchet@50136
|
213 |
|
blanchet@50144
|
214 |
val ctr_rhss =
|
blanchet@50136
|
215 |
map2 (fn k => fn xs =>
|
blanchet@50136
|
216 |
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss;
|
blanchet@50136
|
217 |
|
blanchet@50145
|
218 |
val case_binder = Binding.suffix_name ("_" ^ caseN) b;
|
blanchet@50144
|
219 |
|
blanchet@50149
|
220 |
val case_rhs =
|
blanchet@50191
|
221 |
fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN (map2 mk_uncurried_fun fs xss) $ (unf $ v));
|
blanchet@50144
|
222 |
|
blanchet@50216
|
223 |
val ((raw_case :: raw_ctrs, raw_case_def :: raw_ctr_defs), (lthy', lthy)) = no_defs_lthy
|
blanchet@50184
|
224 |
|> apfst split_list o fold_map3 (fn b => fn mx => fn rhs =>
|
blanchet@50184
|
225 |
Local_Theory.define ((b, mx), ((Thm.def_binding b, []), rhs)) #>> apsnd snd)
|
blanchet@50216
|
226 |
(case_binder :: ctr_binders) (NoSyn :: ctr_mixfixes) (case_rhs :: ctr_rhss)
|
blanchet@50136
|
227 |
||> `Local_Theory.restore;
|
blanchet@50136
|
228 |
|
blanchet@50136
|
229 |
(*transforms defined frees into consts (and more)*)
|
blanchet@50136
|
230 |
val phi = Proof_Context.export_morphism lthy lthy';
|
blanchet@50136
|
231 |
|
blanchet@50136
|
232 |
val ctr_defs = map (Morphism.thm phi) raw_ctr_defs;
|
blanchet@50145
|
233 |
val case_def = Morphism.thm phi raw_case_def;
|
blanchet@50145
|
234 |
|
blanchet@50218
|
235 |
val ctrs0 = map (Morphism.term phi) raw_ctrs;
|
blanchet@50218
|
236 |
val casex0 = Morphism.term phi raw_case;
|
blanchet@50218
|
237 |
|
blanchet@50218
|
238 |
val ctrs = map (mk_ctr As) ctrs0;
|
blanchet@50136
|
239 |
|
blanchet@50150
|
240 |
fun exhaust_tac {context = ctxt, ...} =
|
blanchet@50138
|
241 |
let
|
blanchet@50150
|
242 |
val fld_iff_unf_thm =
|
blanchet@50150
|
243 |
let
|
blanchet@50150
|
244 |
val goal =
|
blanchet@50150
|
245 |
fold_rev Logic.all [u, v]
|
blanchet@50150
|
246 |
(mk_Trueprop_eq (HOLogic.mk_eq (v, fld $ u), HOLogic.mk_eq (unf $ v, u)));
|
blanchet@50150
|
247 |
in
|
blanchet@50150
|
248 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} =>
|
blanchet@50216
|
249 |
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unfT, fpT])
|
blanchet@50191
|
250 |
(certify lthy fld) (certify lthy unf) fld_unf unf_fld)
|
blanchet@50150
|
251 |
|> Thm.close_derivation
|
blanchet@50150
|
252 |
|> Morphism.thm phi
|
blanchet@50150
|
253 |
end;
|
blanchet@50150
|
254 |
|
blanchet@50150
|
255 |
val sumEN_thm' =
|
blanchet@50150
|
256 |
Local_Defs.unfold lthy @{thms all_unit_eq}
|
blanchet@50150
|
257 |
(Drule.instantiate' (map (SOME o certifyT lthy) prod_Ts) [] (mk_sumEN n))
|
blanchet@50150
|
258 |
|> Morphism.thm phi;
|
blanchet@50138
|
259 |
in
|
blanchet@50176
|
260 |
mk_exhaust_tac ctxt n ctr_defs fld_iff_unf_thm sumEN_thm'
|
blanchet@50138
|
261 |
end;
|
blanchet@50136
|
262 |
|
blanchet@50141
|
263 |
val inject_tacss =
|
blanchet@50220
|
264 |
map2 (fn 0 => K [] | _ => fn ctr_def => [fn {context = ctxt, ...} =>
|
blanchet@50220
|
265 |
mk_inject_tac ctxt ctr_def fld_inject]) ms ctr_defs;
|
blanchet@50141
|
266 |
|
blanchet@50142
|
267 |
val half_distinct_tacss =
|
blanchet@50142
|
268 |
map (map (fn (def, def') => fn {context = ctxt, ...} =>
|
blanchet@50142
|
269 |
mk_half_distinct_tac ctxt fld_inject [def, def'])) (mk_half_pairss ctr_defs);
|
blanchet@50142
|
270 |
|
blanchet@50145
|
271 |
val case_tacs =
|
blanchet@50145
|
272 |
map3 (fn k => fn m => fn ctr_def => fn {context = ctxt, ...} =>
|
blanchet@50145
|
273 |
mk_case_tac ctxt n k m case_def ctr_def unf_fld) ks ms ctr_defs;
|
blanchet@50136
|
274 |
|
blanchet@50136
|
275 |
val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss @ [case_tacs];
|
blanchet@50149
|
276 |
|
blanchet@50214
|
277 |
fun sugar_datatype no_defs_lthy =
|
blanchet@50149
|
278 |
let
|
blanchet@50216
|
279 |
val iter_T = flat g_Tss ---> fpT --> C;
|
blanchet@50216
|
280 |
val rec_T = flat h_Tss ---> fpT --> C;
|
blanchet@50214
|
281 |
|
blanchet@50214
|
282 |
val iter_binder = Binding.suffix_name ("_" ^ iterN) b;
|
blanchet@50214
|
283 |
val rec_binder = Binding.suffix_name ("_" ^ recN) b;
|
blanchet@50214
|
284 |
|
blanchet@50214
|
285 |
val iter_free = Free (Binding.name_of iter_binder, iter_T);
|
blanchet@50214
|
286 |
val rec_free = Free (Binding.name_of rec_binder, rec_T);
|
blanchet@50214
|
287 |
|
blanchet@50214
|
288 |
val iter_spec =
|
blanchet@50217
|
289 |
mk_Trueprop_eq (flat_list_comb (iter_free, gss),
|
blanchet@50214
|
290 |
Term.list_comb (fp_iter, map2 (mk_sum_caseN oo map2 mk_uncurried_fun) gss ysss));
|
blanchet@50214
|
291 |
val rec_spec =
|
blanchet@50217
|
292 |
mk_Trueprop_eq (flat_list_comb (rec_free, hss),
|
blanchet@50217
|
293 |
Term.list_comb (fp_rec, map2 (mk_sum_caseN oo map2 mk_uncurried2_fun) hss zssss));
|
blanchet@50214
|
294 |
|
blanchet@50214
|
295 |
val (([raw_iter, raw_rec], [raw_iter_def, raw_rec_def]), (lthy', lthy)) = no_defs_lthy
|
blanchet@50216
|
296 |
|> apfst split_list o fold_map2 (fn b => fn spec =>
|
blanchet@50214
|
297 |
Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec))
|
blanchet@50216
|
298 |
#>> apsnd snd) [iter_binder, rec_binder] [iter_spec, rec_spec]
|
blanchet@50214
|
299 |
||> `Local_Theory.restore;
|
blanchet@50216
|
300 |
|
blanchet@50216
|
301 |
(*transforms defined frees into consts (and more)*)
|
blanchet@50216
|
302 |
val phi = Proof_Context.export_morphism lthy lthy';
|
blanchet@50216
|
303 |
|
blanchet@50216
|
304 |
val iter_def = Morphism.thm phi raw_iter_def;
|
blanchet@50216
|
305 |
val rec_def = Morphism.thm phi raw_rec_def;
|
blanchet@50216
|
306 |
|
blanchet@50218
|
307 |
val iter0 = Morphism.term phi raw_iter;
|
blanchet@50218
|
308 |
val rec0 = Morphism.term phi raw_rec;
|
blanchet@50218
|
309 |
|
blanchet@50218
|
310 |
val iter = mk_iter_or_rec As Cs' iter0;
|
blanchet@50218
|
311 |
val recx = mk_iter_or_rec As Cs' rec0;
|
blanchet@50149
|
312 |
in
|
blanchet@50220
|
313 |
((ctrs, iter, recx, xss, ctr_defs, iter_def, rec_def), lthy)
|
blanchet@50149
|
314 |
end;
|
blanchet@50149
|
315 |
|
blanchet@50220
|
316 |
fun sugar_codatatype no_defs_lthy =
|
blanchet@50220
|
317 |
(([], @{term True}, @{term True}, [], [], TrueI, TrueI), no_defs_lthy);
|
blanchet@50134
|
318 |
in
|
blanchet@50218
|
319 |
wrap_datatype tacss ((ctrs0, casex0), (disc_binders, sel_binderss)) lthy'
|
blanchet@50214
|
320 |
|> (if gfp then sugar_codatatype else sugar_datatype)
|
blanchet@50134
|
321 |
end;
|
blanchet@50182
|
322 |
|
blanchet@50220
|
323 |
fun pour_more_sugar_on_datatypes ((ctrss, iters, recs, xsss, ctr_defss, iter_defs, rec_defs),
|
blanchet@50220
|
324 |
lthy) =
|
blanchet@50217
|
325 |
let
|
blanchet@50217
|
326 |
val xctrss = map2 (map2 (curry Term.list_comb)) ctrss xsss;
|
blanchet@50219
|
327 |
val giters = map (fn iter => flat_list_comb (iter, gss)) iters;
|
blanchet@50219
|
328 |
val hrecs = map (fn recx => flat_list_comb (recx, hss)) recs;
|
blanchet@50216
|
329 |
|
blanchet@50217
|
330 |
val (iter_thmss, rec_thmss) =
|
blanchet@50220
|
331 |
if true then
|
blanchet@50220
|
332 |
`I (map (map (K TrueI)) ctr_defss)
|
blanchet@50220
|
333 |
else let
|
blanchet@50219
|
334 |
fun mk_goal_iter_or_rec fss fc xctr f xs xs' =
|
blanchet@50219
|
335 |
mk_Trueprop_eq (fc $ xctr, Term.list_comb (f, xs'));
|
blanchet@50216
|
336 |
|
blanchet@50219
|
337 |
fun fix_iter_free (x as Free (_, T)) =
|
blanchet@50219
|
338 |
(case find_index (eq_fpT T) fpTs of ~1 => x | j => nth giters j $ x);
|
blanchet@50219
|
339 |
fun fix_rec_free (x as Free (_, T)) =
|
blanchet@50219
|
340 |
(case find_index (eq_fpT T) fpTs of ~1 => [x] | j => [x, nth hrecs j $ x]);
|
blanchet@50219
|
341 |
|
blanchet@50219
|
342 |
val iter_xsss = map (map (map fix_iter_free)) xsss;
|
blanchet@50219
|
343 |
val rec_xsss = map (map (maps fix_rec_free)) xsss;
|
blanchet@50219
|
344 |
|
blanchet@50219
|
345 |
val goal_iterss =
|
blanchet@50219
|
346 |
map5 (map4 o mk_goal_iter_or_rec gss) giters xctrss gss xsss iter_xsss;
|
blanchet@50219
|
347 |
val goal_recss =
|
blanchet@50219
|
348 |
map5 (map4 o mk_goal_iter_or_rec hss) hrecs xctrss hss xsss rec_xsss;
|
blanchet@50219
|
349 |
|
blanchet@50218
|
350 |
val iter_tacss =
|
blanchet@50220
|
351 |
map2 (map o mk_iter_or_rec_tac bnf_map_defs iter_defs) fp_iter_thms ctr_defss;
|
blanchet@50218
|
352 |
val rec_tacss =
|
blanchet@50220
|
353 |
map2 (map o mk_iter_or_rec_tac bnf_map_defs rec_defs) fp_rec_thms ctr_defss;
|
blanchet@50217
|
354 |
in
|
blanchet@50220
|
355 |
(map2 (map2 (fn goal => fn tac => Skip_Proof.prove lthy [] [] goal (tac o #context)))
|
blanchet@50220
|
356 |
goal_iterss iter_tacss,
|
blanchet@50220
|
357 |
map2 (map2 (fn goal => fn tac => Skip_Proof.prove lthy [] [] goal (tac o #context)))
|
blanchet@50220
|
358 |
goal_recss rec_tacss)
|
blanchet@50217
|
359 |
end;
|
blanchet@50216
|
360 |
|
blanchet@50217
|
361 |
val notes =
|
blanchet@50217
|
362 |
[(itersN, iter_thmss),
|
blanchet@50217
|
363 |
(recsN, rec_thmss)]
|
blanchet@50217
|
364 |
|> maps (fn (thmN, thmss) =>
|
blanchet@50217
|
365 |
map2 (fn b => fn thms =>
|
blanchet@50217
|
366 |
((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
|
blanchet@50217
|
367 |
bs thmss);
|
blanchet@50217
|
368 |
in
|
blanchet@50217
|
369 |
lthy |> Local_Theory.notes notes |> snd
|
blanchet@50217
|
370 |
end;
|
blanchet@50217
|
371 |
|
blanchet@50219
|
372 |
val lthy' = lthy
|
blanchet@50217
|
373 |
|> fold_map pour_sugar_on_type (bs ~~ fpTs ~~ Cs ~~ flds ~~ unfs ~~ fp_iters ~~ fp_recs ~~
|
blanchet@50191
|
374 |
fld_unfs ~~ unf_flds ~~ fld_injects ~~ ctr_binderss ~~ ctr_mixfixess ~~ ctr_Tsss ~~
|
blanchet@50217
|
375 |
disc_binderss ~~ sel_bindersss)
|
blanchet@50220
|
376 |
|>> split_list7
|
blanchet@50220
|
377 |
|> (if gfp then snd else pour_more_sugar_on_datatypes);
|
blanchet@50182
|
378 |
|
blanchet@50182
|
379 |
val timer = time (timer ("Constructors, discriminators, selectors, etc., for the new " ^
|
blanchet@50182
|
380 |
(if gfp then "co" else "") ^ "datatype"));
|
blanchet@50127
|
381 |
in
|
blanchet@50219
|
382 |
(timer; lthy')
|
blanchet@50127
|
383 |
end;
|
blanchet@50127
|
384 |
|
blanchet@50214
|
385 |
fun datatype_cmd info specs lthy =
|
blanchet@50136
|
386 |
let
|
blanchet@50199
|
387 |
(*the "perhaps o try" below helps gracefully handles the case where the new type is defined in a
|
blanchet@50199
|
388 |
locale and shadows an existing global type*)
|
blanchet@50194
|
389 |
val fake_thy = Theory.copy
|
blanchet@50199
|
390 |
#> fold (fn spec => perhaps (try (Sign.add_type lthy
|
blanchet@50199
|
391 |
(type_binder_of spec, length (type_args_constrained_of spec), mixfix_of spec)))) specs;
|
blanchet@50194
|
392 |
val fake_lthy = Proof_Context.background_theory fake_thy lthy;
|
blanchet@50136
|
393 |
in
|
blanchet@50214
|
394 |
prepare_datatype Syntax.read_typ info specs fake_lthy lthy
|
blanchet@50136
|
395 |
end;
|
blanchet@50134
|
396 |
|
blanchet@50144
|
397 |
val parse_opt_binding_colon = Scan.optional (Parse.binding --| Parse.$$$ ":") no_binder
|
blanchet@50134
|
398 |
|
blanchet@50127
|
399 |
val parse_ctr_arg =
|
blanchet@50134
|
400 |
Parse.$$$ "(" |-- parse_opt_binding_colon -- Parse.typ --| Parse.$$$ ")" ||
|
blanchet@50144
|
401 |
(Parse.typ >> pair no_binder);
|
blanchet@50127
|
402 |
|
blanchet@50127
|
403 |
val parse_single_spec =
|
blanchet@50127
|
404 |
Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix --
|
blanchet@50134
|
405 |
(@{keyword "="} |-- Parse.enum1 "|" (parse_opt_binding_colon -- Parse.binding --
|
blanchet@50134
|
406 |
Scan.repeat parse_ctr_arg -- Parse.opt_mixfix));
|
blanchet@50127
|
407 |
|
blanchet@50127
|
408 |
val _ =
|
blanchet@50127
|
409 |
Outer_Syntax.local_theory @{command_spec "data"} "define BNF-based inductive datatypes"
|
blanchet@50214
|
410 |
(Parse.and_list1 parse_single_spec >> datatype_cmd false);
|
blanchet@50127
|
411 |
|
blanchet@50127
|
412 |
val _ =
|
blanchet@50127
|
413 |
Outer_Syntax.local_theory @{command_spec "codata"} "define BNF-based coinductive datatypes"
|
blanchet@50214
|
414 |
(Parse.and_list1 parse_single_spec >> datatype_cmd true);
|
blanchet@50127
|
415 |
|
blanchet@50127
|
416 |
end;
|