author | blanchet |
Wed, 05 Sep 2012 15:40:13 +0200 | |
changeset 49157 | 6407346b74c7 |
parent 49146 | e32b1f748854 |
child 49161 | a8e74375d971 |
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 [] => () |
49119 | 67 |
| T :: _ => error ("Duplicate type parameter " ^ quote (Syntax.string_of_typ lthy T))); |
68 |
||
49157 | 69 |
(* TODO: print timing information for sugar *) |
49119 | 70 |
(* TODO: check that no type variables occur in the rhss that's not in the lhss *) |
71 |
(* TODO: use sort constraints on type args *) |
|
49157 | 72 |
(* TODO: use mixfix *) |
49119 | 73 |
|
74 |
val N = length specs; |
|
75 |
||
49121 | 76 |
fun mk_T b = |
77 |
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))), |
|
78 |
As); |
|
79 |
||
49129 | 80 |
val bs = map type_binder_of specs; |
49121 | 81 |
val Ts = map mk_T bs; |
82 |
||
83 |
val mixfixes = map mixfix_of_typ specs; |
|
49119 | 84 |
|
85 |
val _ = (case duplicates Binding.eq_name bs of [] => () |
|
86 |
| b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b))); |
|
87 |
||
49121 | 88 |
val ctr_specss = map ctr_specs_of specs; |
49119 | 89 |
|
49129 | 90 |
val disc_binderss = map (map disc_of) ctr_specss; |
91 |
val ctr_binderss = map (map ctr_of) ctr_specss; |
|
49121 | 92 |
val ctr_argsss = map (map args_of) ctr_specss; |
93 |
val ctr_mixfixess = map (map mixfix_of_ctr) ctr_specss; |
|
49119 | 94 |
|
49129 | 95 |
val sel_bindersss = map (map (map fst)) ctr_argsss; |
49121 | 96 |
val ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss; |
97 |
||
98 |
val (Bs, C) = |
|
99 |
lthy |
|
100 |
|> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs |
|
101 |
|> mk_TFrees N |
|
102 |
||> the_single o fst o mk_TFrees 1; |
|
49119 | 103 |
|
49146 | 104 |
fun is_same_rec (T as Type (s, Us)) (Type (s', Us')) = |
105 |
s = s' andalso (Us = Us' orelse error ("Illegal occurrence of recursive type " ^ |
|
106 |
quote (Syntax.string_of_typ fake_lthy T))) |
|
107 |
| is_same_rec _ _ = false |
|
108 |
||
109 |
fun freeze_rec (T as Type (s, Us)) = |
|
110 |
(case find_index (is_same_rec T) Ts of |
|
111 |
~1 => Type (s, map freeze_rec Us) |
|
49121 | 112 |
| i => nth Bs i) |
113 |
| freeze_rec T = T; |
|
114 |
||
115 |
val ctr_TsssBs = map (map (map freeze_rec)) ctr_Tsss; |
|
116 |
val sum_prod_TsBs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssBs; |
|
49119 | 117 |
|
49121 | 118 |
val eqs = map dest_TFree Bs ~~ sum_prod_TsBs; |
119 |
||
49126 | 120 |
val ((raw_unfs, raw_flds, unf_flds, fld_unfs, fld_injects), lthy') = |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
121 |
fp_bnf (if gfp then bnf_gfp else bnf_lfp) bs As' eqs lthy; |
49121 | 122 |
|
49126 | 123 |
fun mk_unf_or_fld get_foldedT Ts t = |
49124 | 124 |
let val Type (_, Ts0) = get_foldedT (fastype_of t) in |
125 |
Term.subst_atomic_types (Ts0 ~~ Ts) t |
|
49121 | 126 |
end; |
49119 | 127 |
|
49126 | 128 |
val mk_unf = mk_unf_or_fld domain_type; |
129 |
val mk_fld = mk_unf_or_fld range_type; |
|
49121 | 130 |
|
49126 | 131 |
val unfs = map (mk_unf As) raw_unfs; |
49124 | 132 |
val flds = map (mk_fld As) raw_flds; |
133 |
||
49135 | 134 |
fun pour_sugar_on_type ((((((((((b, T), fld), unf), fld_unf), unf_fld), fld_inject), |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
135 |
ctr_binders), ctr_Tss), disc_binders), sel_binderss) no_defs_lthy = |
49121 | 136 |
let |
49129 | 137 |
val n = length ctr_binders; |
49121 | 138 |
val ks = 1 upto n; |
139 |
val ms = map length ctr_Tss; |
|
140 |
||
49124 | 141 |
val unf_T = domain_type (fastype_of fld); |
49121 | 142 |
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
|
143 |
val case_Ts = map (fn Ts => Ts ---> C) ctr_Tss; |
49119 | 144 |
|
49129 | 145 |
val ((((fs, u), v), xss), _) = |
49124 | 146 |
lthy |
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
147 |
|> mk_Frees "f" case_Ts |
49129 | 148 |
||>> yield_singleton (mk_Frees "u") unf_T |
49124 | 149 |
||>> yield_singleton (mk_Frees "v") T |
150 |
||>> mk_Freess "x" ctr_Tss; |
|
49121 | 151 |
|
49129 | 152 |
val ctr_rhss = |
49121 | 153 |
map2 (fn k => fn xs => |
154 |
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; |
|
155 |
||
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
156 |
val case_binder = Binding.suffix_name ("_" ^ caseN) b; |
49129 | 157 |
|
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
158 |
val case_rhs = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
159 |
fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN (uncurry_fs fs xss) $ (unf $ v)); |
49129 | 160 |
|
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
161 |
val (((raw_ctrs, raw_ctr_defs), (raw_case, raw_case_def)), (lthy', lthy)) = no_defs_lthy |
49121 | 162 |
|> apfst split_list o fold_map2 (fn b => fn rhs => |
163 |
Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs)) #>> apsnd snd) |
|
49129 | 164 |
ctr_binders ctr_rhss |
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
165 |
||>> (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
|
166 |
case_rhs)) #>> apsnd snd) |
49121 | 167 |
||> `Local_Theory.restore; |
168 |
||
169 |
(*transforms defined frees into consts (and more)*) |
|
170 |
val phi = Proof_Context.export_morphism lthy lthy'; |
|
171 |
||
172 |
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
|
173 |
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
|
174 |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
175 |
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
|
176 |
val casex = Morphism.term phi raw_case; |
49121 | 177 |
|
49135 | 178 |
fun exhaust_tac {context = ctxt, ...} = |
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
179 |
let |
49135 | 180 |
val fld_iff_unf_thm = |
181 |
let |
|
182 |
val goal = |
|
183 |
fold_rev Logic.all [u, v] |
|
184 |
(mk_Trueprop_eq (HOLogic.mk_eq (v, fld $ u), HOLogic.mk_eq (unf $ v, u))); |
|
185 |
in |
|
186 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
|
187 |
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unf_T, T]) (certify lthy fld) |
|
188 |
(certify lthy unf) fld_unf unf_fld) |
|
189 |
|> Thm.close_derivation |
|
190 |
|> Morphism.thm phi |
|
191 |
end; |
|
192 |
||
193 |
val sumEN_thm' = |
|
194 |
Local_Defs.unfold lthy @{thms all_unit_eq} |
|
195 |
(Drule.instantiate' (map (SOME o certifyT lthy) prod_Ts) [] (mk_sumEN n)) |
|
196 |
|> Morphism.thm phi; |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
197 |
in |
49135 | 198 |
mk_exhaust_tac ctxt n ms ctr_defs fld_iff_unf_thm sumEN_thm' |
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
199 |
end; |
49121 | 200 |
|
49126 | 201 |
val inject_tacss = |
202 |
map2 (fn 0 => K [] |
|
203 |
| _ => fn ctr_def => [fn {context = ctxt, ...} => |
|
204 |
mk_inject_tac ctxt ctr_def fld_inject]) |
|
205 |
ms ctr_defs; |
|
206 |
||
49127 | 207 |
val half_distinct_tacss = |
208 |
map (map (fn (def, def') => fn {context = ctxt, ...} => |
|
209 |
mk_half_distinct_tac ctxt fld_inject [def, def'])) (mk_half_pairss ctr_defs); |
|
210 |
||
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
211 |
val case_tacs = |
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
212 |
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
|
213 |
mk_case_tac ctxt n k m case_def ctr_def unf_fld) ks ms ctr_defs; |
49121 | 214 |
|
215 |
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
|
216 |
|
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
217 |
fun sugar_lfp lthy = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
218 |
let |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
219 |
(*### |
49146 | 220 |
val fld_iter = @{term True}; (*###*) |
221 |
||
222 |
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
|
223 |
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
|
224 |
|
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
225 |
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
|
226 |
|
49146 | 227 |
val iter_rhs = |
228 |
fold_rev Term.lambda fs (fld_iter $ mk_sum_caseN (uncurry_fs fs xss) $ (unf $ v)); |
|
229 |
||
230 |
||
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
231 |
val uncurried_fs = |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
232 |
map2 (fn f => fn xs => |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
233 |
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
|
234 |
*) |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
235 |
in |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
236 |
lthy |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
237 |
end; |
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
238 |
|
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset
|
239 |
fun sugar_gfp lthy = lthy; |
49119 | 240 |
in |
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
241 |
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
|
242 |
|> (if gfp then sugar_gfp else sugar_lfp) |
49119 | 243 |
end; |
49112 | 244 |
in |
49124 | 245 |
lthy' |
49135 | 246 |
|> fold pour_sugar_on_type (bs ~~ Ts ~~ flds ~~ unfs ~~ fld_unfs ~~ unf_flds ~~ fld_injects ~~ |
49129 | 247 |
ctr_binderss ~~ ctr_Tsss ~~ disc_binderss ~~ sel_bindersss) |
49112 | 248 |
end; |
249 |
||
49121 | 250 |
fun data_cmd info specs lthy = |
251 |
let |
|
252 |
val fake_lthy = |
|
253 |
Proof_Context.theory_of lthy |
|
254 |
|> Theory.copy |
|
255 |
|> Sign.add_types_global (map (fn spec => |
|
49129 | 256 |
(type_binder_of spec, length (type_args_constrained_of spec), mixfix_of_typ spec)) specs) |
49121 | 257 |
|> Proof_Context.init_global |
258 |
in |
|
259 |
prepare_data Syntax.read_typ info specs fake_lthy lthy |
|
260 |
end; |
|
49119 | 261 |
|
49129 | 262 |
val parse_opt_binding_colon = Scan.optional (Parse.binding --| Parse.$$$ ":") no_binder |
49119 | 263 |
|
49112 | 264 |
val parse_ctr_arg = |
49119 | 265 |
Parse.$$$ "(" |-- parse_opt_binding_colon -- Parse.typ --| Parse.$$$ ")" || |
49129 | 266 |
(Parse.typ >> pair no_binder); |
49112 | 267 |
|
268 |
val parse_single_spec = |
|
269 |
Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix -- |
|
49119 | 270 |
(@{keyword "="} |-- Parse.enum1 "|" (parse_opt_binding_colon -- Parse.binding -- |
271 |
Scan.repeat parse_ctr_arg -- Parse.opt_mixfix)); |
|
49112 | 272 |
|
273 |
val _ = |
|
274 |
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
|
275 |
(Parse.and_list1 parse_single_spec >> data_cmd false); |
49112 | 276 |
|
277 |
val _ = |
|
278 |
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
|
279 |
(Parse.and_list1 parse_single_spec >> data_cmd true); |
49112 | 280 |
|
281 |
end; |