author | blanchet |
Wed, 05 Sep 2012 19:58:09 +0200 | |
changeset 49169 | 937a0fadddfb |
parent 49167 | 68623861e0f2 |
child 49176 | 6d29d2db5f88 |
permissions | -rw-r--r-- |
49112 | 1 |
(* Title: HOL/Codatatype/Tools/bnf_fp_sugar.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2012 |
|
4 |
||
5 |
Sugar for constructing LFPs and GFPs. |
|
6 |
*) |
|
7 |
||
8 |
signature BNF_FP_SUGAR = |
|
9 |
sig |
|
10 |
end; |
|
11 |
||
12 |
structure BNF_FP_Sugar : BNF_FP_SUGAR = |
|
13 |
struct |
|
14 |
||
49119 | 15 |
open BNF_Util |
16 |
open BNF_Wrap |
|
17 |
open BNF_FP_Util |
|
18 |
open BNF_LFP |
|
19 |
open BNF_GFP |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
20 |
open BNF_FP_Sugar_Tactics |
49119 | 21 |
|
49129 | 22 |
val caseN = "case"; |
23 |
||
49124 | 24 |
fun cannot_merge_types () = error "Mutually recursive types must have the same type parameters"; |
49119 | 25 |
|
26 |
fun merge_type_arg_constrained ctxt (T, c) (T', c') = |
|
27 |
if T = T' then |
|
28 |
(case (c, c') of |
|
29 |
(_, NONE) => (T, c) |
|
30 |
| (NONE, _) => (T, c') |
|
31 |
| _ => |
|
32 |
if c = c' then |
|
33 |
(T, c) |
|
34 |
else |
|
35 |
error ("Inconsistent sort constraints for type variable " ^ |
|
36 |
quote (Syntax.string_of_typ ctxt T))) |
|
37 |
else |
|
38 |
cannot_merge_types (); |
|
39 |
||
40 |
fun merge_type_args_constrained ctxt (cAs, cAs') = |
|
41 |
if length cAs = length cAs' then map2 (merge_type_arg_constrained ctxt) cAs cAs' |
|
42 |
else cannot_merge_types (); |
|
43 |
||
49121 | 44 |
fun type_args_constrained_of (((cAs, _), _), _) = cAs; |
45 |
val type_args_of = map fst o type_args_constrained_of; |
|
49129 | 46 |
fun type_binder_of (((_, b), _), _) = b; |
49121 | 47 |
fun mixfix_of_typ ((_, mx), _) = mx; |
48 |
fun ctr_specs_of (_, ctr_specs) = ctr_specs; |
|
49119 | 49 |
|
49121 | 50 |
fun disc_of (((disc, _), _), _) = disc; |
51 |
fun ctr_of (((_, ctr), _), _) = ctr; |
|
52 |
fun args_of ((_, args), _) = args; |
|
53 |
fun mixfix_of_ctr (_, mx) = mx; |
|
49119 | 54 |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
55 |
val uncurry_fs = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
56 |
map2 (fn f => fn xs => HOLogic.tupled_lambda (HOLogic.mk_tuple xs) (Term.list_comb (f, xs))); |
49119 | 57 |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
58 |
fun prepare_data prepare_typ gfp specs fake_lthy lthy = |
49112 | 59 |
let |
49121 | 60 |
val constrained_As = |
61 |
map (map (apfst (prepare_typ fake_lthy)) o type_args_constrained_of) specs |
|
49119 | 62 |
|> Library.foldr1 (merge_type_args_constrained lthy); |
49121 | 63 |
val As = map fst constrained_As; |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
64 |
val As' = map dest_TFree As; |
49119 | 65 |
|
49121 | 66 |
val _ = (case duplicates (op =) As of [] => () |
49165 | 67 |
| A :: _ => error ("Duplicate type parameter " ^ quote (Syntax.string_of_typ lthy A))); |
49119 | 68 |
|
69 |
(* TODO: use sort constraints on type args *) |
|
70 |
||
71 |
val N = length specs; |
|
72 |
||
49121 | 73 |
fun mk_T b = |
74 |
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))), |
|
75 |
As); |
|
76 |
||
49129 | 77 |
val bs = map type_binder_of specs; |
49121 | 78 |
val Ts = map mk_T bs; |
79 |
||
80 |
val mixfixes = map mixfix_of_typ specs; |
|
49119 | 81 |
|
82 |
val _ = (case duplicates Binding.eq_name bs of [] => () |
|
83 |
| b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b))); |
|
84 |
||
49121 | 85 |
val ctr_specss = map ctr_specs_of specs; |
49119 | 86 |
|
49129 | 87 |
val disc_binderss = map (map disc_of) ctr_specss; |
88 |
val ctr_binderss = map (map ctr_of) ctr_specss; |
|
49121 | 89 |
val ctr_argsss = map (map args_of) ctr_specss; |
90 |
val ctr_mixfixess = map (map mixfix_of_ctr) ctr_specss; |
|
49119 | 91 |
|
49129 | 92 |
val sel_bindersss = map (map (map fst)) ctr_argsss; |
49121 | 93 |
val ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss; |
94 |
||
49167 | 95 |
val rhs_As' = fold (fold (fold Term.add_tfreesT)) ctr_Tsss []; |
96 |
val _ = (case subtract (op =) As' rhs_As' of |
|
49165 | 97 |
[] => () |
98 |
| A' :: _ => error ("Extra type variables on rhs: " ^ |
|
99 |
quote (Syntax.string_of_typ lthy (TFree A')))); |
|
100 |
||
49121 | 101 |
val (Bs, C) = |
102 |
lthy |
|
103 |
|> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs |
|
104 |
|> mk_TFrees N |
|
105 |
||> the_single o fst o mk_TFrees 1; |
|
49119 | 106 |
|
49146 | 107 |
fun is_same_rec (T as Type (s, Us)) (Type (s', Us')) = |
108 |
s = s' andalso (Us = Us' orelse error ("Illegal occurrence of recursive type " ^ |
|
109 |
quote (Syntax.string_of_typ fake_lthy T))) |
|
110 |
| is_same_rec _ _ = false |
|
111 |
||
112 |
fun freeze_rec (T as Type (s, Us)) = |
|
113 |
(case find_index (is_same_rec T) Ts of |
|
114 |
~1 => Type (s, map freeze_rec Us) |
|
49121 | 115 |
| i => nth Bs i) |
116 |
| freeze_rec T = T; |
|
117 |
||
118 |
val ctr_TsssBs = map (map (map freeze_rec)) ctr_Tsss; |
|
119 |
val sum_prod_TsBs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssBs; |
|
49119 | 120 |
|
49121 | 121 |
val eqs = map dest_TFree Bs ~~ sum_prod_TsBs; |
122 |
||
49126 | 123 |
val ((raw_unfs, raw_flds, unf_flds, fld_unfs, fld_injects), lthy') = |
49169 | 124 |
fp_bnf (if gfp then bnf_gfp else bnf_lfp) bs mixfixes As' eqs lthy; |
49121 | 125 |
|
49167 | 126 |
val timer = time (Timer.startRealTimer ()); |
127 |
||
49126 | 128 |
fun mk_unf_or_fld get_foldedT Ts t = |
49124 | 129 |
let val Type (_, Ts0) = get_foldedT (fastype_of t) in |
130 |
Term.subst_atomic_types (Ts0 ~~ Ts) t |
|
49121 | 131 |
end; |
49119 | 132 |
|
49126 | 133 |
val mk_unf = mk_unf_or_fld domain_type; |
134 |
val mk_fld = mk_unf_or_fld range_type; |
|
49121 | 135 |
|
49126 | 136 |
val unfs = map (mk_unf As) raw_unfs; |
49124 | 137 |
val flds = map (mk_fld As) raw_flds; |
138 |
||
49169 | 139 |
fun pour_sugar_on_type (((((((((((b, T), fld), unf), fld_unf), unf_fld), fld_inject), |
140 |
ctr_binders), ctr_mixfixes), ctr_Tss), disc_binders), sel_binderss) no_defs_lthy = |
|
49121 | 141 |
let |
49129 | 142 |
val n = length ctr_binders; |
49121 | 143 |
val ks = 1 upto n; |
144 |
val ms = map length ctr_Tss; |
|
145 |
||
49124 | 146 |
val unf_T = domain_type (fastype_of fld); |
49121 | 147 |
val prod_Ts = map HOLogic.mk_tupleT ctr_Tss; |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
148 |
val case_Ts = map (fn Ts => Ts ---> C) ctr_Tss; |
49119 | 149 |
|
49129 | 150 |
val ((((fs, u), v), xss), _) = |
49124 | 151 |
lthy |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
152 |
|> mk_Frees "f" case_Ts |
49129 | 153 |
||>> yield_singleton (mk_Frees "u") unf_T |
49124 | 154 |
||>> yield_singleton (mk_Frees "v") T |
155 |
||>> mk_Freess "x" ctr_Tss; |
|
49121 | 156 |
|
49129 | 157 |
val ctr_rhss = |
49121 | 158 |
map2 (fn k => fn xs => |
159 |
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; |
|
160 |
||
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
161 |
val case_binder = Binding.suffix_name ("_" ^ caseN) b; |
49129 | 162 |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
163 |
val case_rhs = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
164 |
fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN (uncurry_fs fs xss) $ (unf $ v)); |
49129 | 165 |
|
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
166 |
val (((raw_ctrs, raw_ctr_defs), (raw_case, raw_case_def)), (lthy', lthy)) = no_defs_lthy |
49169 | 167 |
|> apfst split_list o fold_map3 (fn b => fn mx => fn rhs => |
168 |
Local_Theory.define ((b, mx), ((Thm.def_binding b, []), rhs)) #>> apsnd snd) |
|
169 |
ctr_binders ctr_mixfixes ctr_rhss |
|
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
170 |
||>> (Local_Theory.define ((case_binder, NoSyn), ((Thm.def_binding case_binder, []), |
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
171 |
case_rhs)) #>> apsnd snd) |
49121 | 172 |
||> `Local_Theory.restore; |
173 |
||
174 |
(*transforms defined frees into consts (and more)*) |
|
175 |
val phi = Proof_Context.export_morphism lthy lthy'; |
|
176 |
||
177 |
val ctr_defs = map (Morphism.thm phi) raw_ctr_defs; |
|
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
178 |
val case_def = Morphism.thm phi raw_case_def; |
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
179 |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
180 |
val ctrs = map (Morphism.term phi) raw_ctrs; |
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
181 |
val casex = Morphism.term phi raw_case; |
49121 | 182 |
|
49135 | 183 |
fun exhaust_tac {context = ctxt, ...} = |
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
184 |
let |
49135 | 185 |
val fld_iff_unf_thm = |
186 |
let |
|
187 |
val goal = |
|
188 |
fold_rev Logic.all [u, v] |
|
189 |
(mk_Trueprop_eq (HOLogic.mk_eq (v, fld $ u), HOLogic.mk_eq (unf $ v, u))); |
|
190 |
in |
|
191 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
|
192 |
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unf_T, T]) (certify lthy fld) |
|
193 |
(certify lthy unf) fld_unf unf_fld) |
|
194 |
|> Thm.close_derivation |
|
195 |
|> Morphism.thm phi |
|
196 |
end; |
|
197 |
||
198 |
val sumEN_thm' = |
|
199 |
Local_Defs.unfold lthy @{thms all_unit_eq} |
|
200 |
(Drule.instantiate' (map (SOME o certifyT lthy) prod_Ts) [] (mk_sumEN n)) |
|
201 |
|> Morphism.thm phi; |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
202 |
in |
49161
a8e74375d971
fixed (n + 1)st bug in "mk_exhaust_tac" -- arose with uncurried constructors
blanchet
parents:
49157
diff
changeset
|
203 |
mk_exhaust_tac ctxt n ctr_defs fld_iff_unf_thm sumEN_thm' |
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
204 |
end; |
49121 | 205 |
|
49126 | 206 |
val inject_tacss = |
207 |
map2 (fn 0 => K [] |
|
208 |
| _ => fn ctr_def => [fn {context = ctxt, ...} => |
|
209 |
mk_inject_tac ctxt ctr_def fld_inject]) |
|
210 |
ms ctr_defs; |
|
211 |
||
49127 | 212 |
val half_distinct_tacss = |
213 |
map (map (fn (def, def') => fn {context = ctxt, ...} => |
|
214 |
mk_half_distinct_tac ctxt fld_inject [def, def'])) (mk_half_pairss ctr_defs); |
|
215 |
||
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
216 |
val case_tacs = |
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
217 |
map3 (fn k => fn m => fn ctr_def => fn {context = ctxt, ...} => |
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
218 |
mk_case_tac ctxt n k m case_def ctr_def unf_fld) ks ms ctr_defs; |
49121 | 219 |
|
220 |
val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss @ [case_tacs]; |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
221 |
|
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
222 |
fun sugar_lfp lthy = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
223 |
let |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
224 |
(*### |
49146 | 225 |
val fld_iter = @{term True}; (*###*) |
226 |
||
227 |
val iter_Tss = map (fn Ts => Ts) (*###*) ctr_Tss; |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
228 |
val iter_Ts = map (fn Ts => Ts ---> C) iter_Tss; |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
229 |
|
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
230 |
val iter_fs = map2 (fn Free (s, _) => fn T => Free (s, T)) fs iter_Ts |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
231 |
|
49146 | 232 |
val iter_rhs = |
233 |
fold_rev Term.lambda fs (fld_iter $ mk_sum_caseN (uncurry_fs fs xss) $ (unf $ v)); |
|
234 |
||
235 |
||
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
236 |
val uncurried_fs = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
237 |
map2 (fn f => fn xs => |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
238 |
HOLogic.tupled_lambda (HOLogic.mk_tuple xs) (Term.list_comb (f, xs))) fs xss; |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
239 |
*) |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
240 |
in |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
241 |
lthy |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
242 |
end; |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
243 |
|
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
244 |
fun sugar_gfp lthy = lthy; |
49119 | 245 |
in |
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
246 |
wrap_data tacss ((ctrs, casex), (disc_binders, sel_binderss)) lthy' |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
247 |
|> (if gfp then sugar_gfp else sugar_lfp) |
49119 | 248 |
end; |
49167 | 249 |
|
250 |
val lthy'' = |
|
251 |
fold pour_sugar_on_type (bs ~~ Ts ~~ flds ~~ unfs ~~ fld_unfs ~~ unf_flds ~~ fld_injects ~~ |
|
49169 | 252 |
ctr_binderss ~~ ctr_mixfixess ~~ ctr_Tsss ~~ disc_binderss ~~ sel_bindersss) lthy'; |
49167 | 253 |
|
254 |
val timer = time (timer ("Constructors, discriminators, selectors, etc., for the new " ^ |
|
255 |
(if gfp then "co" else "") ^ "datatype")); |
|
49112 | 256 |
in |
49167 | 257 |
(timer; lthy'') |
49112 | 258 |
end; |
259 |
||
49121 | 260 |
fun data_cmd info specs lthy = |
261 |
let |
|
262 |
val fake_lthy = |
|
263 |
Proof_Context.theory_of lthy |
|
264 |
|> Theory.copy |
|
265 |
|> Sign.add_types_global (map (fn spec => |
|
49129 | 266 |
(type_binder_of spec, length (type_args_constrained_of spec), mixfix_of_typ spec)) specs) |
49121 | 267 |
|> Proof_Context.init_global |
268 |
in |
|
269 |
prepare_data Syntax.read_typ info specs fake_lthy lthy |
|
270 |
end; |
|
49119 | 271 |
|
49129 | 272 |
val parse_opt_binding_colon = Scan.optional (Parse.binding --| Parse.$$$ ":") no_binder |
49119 | 273 |
|
49112 | 274 |
val parse_ctr_arg = |
49119 | 275 |
Parse.$$$ "(" |-- parse_opt_binding_colon -- Parse.typ --| Parse.$$$ ")" || |
49129 | 276 |
(Parse.typ >> pair no_binder); |
49112 | 277 |
|
278 |
val parse_single_spec = |
|
279 |
Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix -- |
|
49119 | 280 |
(@{keyword "="} |-- Parse.enum1 "|" (parse_opt_binding_colon -- Parse.binding -- |
281 |
Scan.repeat parse_ctr_arg -- Parse.opt_mixfix)); |
|
49112 | 282 |
|
283 |
val _ = |
|
284 |
Outer_Syntax.local_theory @{command_spec "data"} "define BNF-based inductive datatypes" |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
285 |
(Parse.and_list1 parse_single_spec >> data_cmd false); |
49112 | 286 |
|
287 |
val _ = |
|
288 |
Outer_Syntax.local_theory @{command_spec "codata"} "define BNF-based coinductive datatypes" |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
289 |
(Parse.and_list1 parse_single_spec >> data_cmd true); |
49112 | 290 |
|
291 |
end; |