author | blanchet |
Tue, 04 Sep 2012 18:14:58 +0200 | |
changeset 49129 | b5413cb7d860 |
parent 49127 | f7326a0d7f19 |
child 49130 | 3c26e17b2849 |
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 |
|
55 |
val lfp_info = bnf_lfp; |
|
56 |
val gfp_info = bnf_gfp; |
|
57 |
||
49121 | 58 |
fun prepare_data prepare_typ construct 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; |
49119 | 64 |
|
49121 | 65 |
val _ = (case duplicates (op =) As of [] => () |
49119 | 66 |
| T :: _ => error ("Duplicate type parameter " ^ quote (Syntax.string_of_typ lthy T))); |
67 |
||
68 |
(* TODO: check that no type variables occur in the rhss that's not in the lhss *) |
|
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 |
||
95 |
val (Bs, C) = |
|
96 |
lthy |
|
97 |
|> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs |
|
98 |
|> mk_TFrees N |
|
99 |
||> the_single o fst o mk_TFrees 1; |
|
49119 | 100 |
|
49121 | 101 |
fun freeze_rec (T as Type (s, Ts')) = |
102 |
(case find_index (curry (op =) T) Ts of |
|
103 |
~1 => Type (s, map freeze_rec Ts') |
|
104 |
| i => nth Bs i) |
|
105 |
| freeze_rec T = T; |
|
106 |
||
107 |
val ctr_TsssBs = map (map (map freeze_rec)) ctr_Tsss; |
|
108 |
val sum_prod_TsBs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssBs; |
|
49119 | 109 |
|
49121 | 110 |
val eqs = map dest_TFree Bs ~~ sum_prod_TsBs; |
111 |
||
49126 | 112 |
val ((raw_unfs, raw_flds, unf_flds, fld_unfs, fld_injects), lthy') = |
113 |
fp_bnf construct bs eqs lthy; |
|
49121 | 114 |
|
49126 | 115 |
fun mk_unf_or_fld get_foldedT Ts t = |
49124 | 116 |
let val Type (_, Ts0) = get_foldedT (fastype_of t) in |
117 |
Term.subst_atomic_types (Ts0 ~~ Ts) t |
|
49121 | 118 |
end; |
49119 | 119 |
|
49126 | 120 |
val mk_unf = mk_unf_or_fld domain_type; |
121 |
val mk_fld = mk_unf_or_fld range_type; |
|
49121 | 122 |
|
49126 | 123 |
val unfs = map (mk_unf As) raw_unfs; |
49124 | 124 |
val flds = map (mk_fld As) raw_flds; |
125 |
||
49129 | 126 |
fun wrap_type ((((((((((b, T), fld), unf), fld_unf), unf_fld), fld_inject), ctr_binders), |
127 |
ctr_Tss), disc_binders), sel_binderss) no_defs_lthy = |
|
49121 | 128 |
let |
49129 | 129 |
val n = length ctr_binders; |
49121 | 130 |
val ks = 1 upto n; |
131 |
val ms = map length ctr_Tss; |
|
132 |
||
49124 | 133 |
val unf_T = domain_type (fastype_of fld); |
49121 | 134 |
val prod_Ts = map HOLogic.mk_tupleT ctr_Tss; |
49129 | 135 |
val caseofC_Ts = map (fn Ts => Ts ---> C) ctr_Tss; |
49119 | 136 |
|
49129 | 137 |
val ((((fs, u), v), xss), _) = |
49124 | 138 |
lthy |
49129 | 139 |
|> mk_Frees "f" caseofC_Ts |
140 |
||>> yield_singleton (mk_Frees "u") unf_T |
|
49124 | 141 |
||>> yield_singleton (mk_Frees "v") T |
142 |
||>> mk_Freess "x" ctr_Tss; |
|
49121 | 143 |
|
49129 | 144 |
val uncurried_fs = |
145 |
map2 (fn f => fn xs => |
|
146 |
HOLogic.tupled_lambda (HOLogic.mk_tuple xs) (Term.list_comb (f, xs))) fs xss; |
|
147 |
||
148 |
val ctr_rhss = |
|
49121 | 149 |
map2 (fn k => fn xs => |
150 |
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; |
|
151 |
||
49129 | 152 |
val caseof_binder = Binding.suffix_name ("_" ^ caseN) b; |
153 |
||
154 |
val caseof_rhs = fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN uncurried_fs $ (unf $ v)); |
|
155 |
||
156 |
val (((raw_ctrs, raw_ctr_defs), (raw_caseof, raw_caseof_def)), (lthy', lthy)) = no_defs_lthy |
|
49121 | 157 |
|> apfst split_list o fold_map2 (fn b => fn rhs => |
158 |
Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs)) #>> apsnd snd) |
|
49129 | 159 |
ctr_binders ctr_rhss |
160 |
||>> (Local_Theory.define ((caseof_binder, NoSyn), ((Thm.def_binding caseof_binder, []), |
|
161 |
caseof_rhs)) #>> apsnd snd) |
|
49121 | 162 |
||> `Local_Theory.restore; |
163 |
||
164 |
(*transforms defined frees into consts (and more)*) |
|
165 |
val phi = Proof_Context.export_morphism lthy lthy'; |
|
166 |
||
167 |
val ctr_defs = map (Morphism.thm phi) raw_ctr_defs; |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
168 |
val ctrs = map (Morphism.term phi) raw_ctrs; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
169 |
val caseof = Morphism.term phi raw_caseof; |
49121 | 170 |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
171 |
val fld_iff_unf_thm = |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
172 |
let |
49124 | 173 |
val goal = |
174 |
fold_rev Logic.all [u, v] |
|
175 |
(mk_Trueprop_eq (HOLogic.mk_eq (v, fld $ u), HOLogic.mk_eq (unf $ v, u))); |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
176 |
in |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
177 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
49124 | 178 |
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unf_T, T]) (certify lthy fld) |
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
179 |
(certify lthy unf) fld_unf unf_fld) |
49125 | 180 |
|> Thm.close_derivation |
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
181 |
end; |
49121 | 182 |
|
49125 | 183 |
val sumEN_thm = mk_sumEN n; |
184 |
val sumEN_thm' = |
|
185 |
let val cTs = map (SOME o certifyT lthy) prod_Ts in |
|
186 |
Local_Defs.unfold lthy @{thms all_unit_eq} (Drule.instantiate' cTs [] sumEN_thm) |
|
187 |
end; |
|
188 |
||
189 |
fun exhaust_tac {context = ctxt, ...} = |
|
190 |
mk_exhaust_tac ctxt n ms ctr_defs fld_iff_unf_thm sumEN_thm'; |
|
191 |
||
49126 | 192 |
val inject_tacss = |
193 |
map2 (fn 0 => K [] |
|
194 |
| _ => fn ctr_def => [fn {context = ctxt, ...} => |
|
195 |
mk_inject_tac ctxt ctr_def fld_inject]) |
|
196 |
ms ctr_defs; |
|
197 |
||
49127 | 198 |
val half_distinct_tacss = |
199 |
map (map (fn (def, def') => fn {context = ctxt, ...} => |
|
200 |
mk_half_distinct_tac ctxt fld_inject [def, def'])) (mk_half_pairss ctr_defs); |
|
201 |
||
49126 | 202 |
(*###*) |
49121 | 203 |
fun cheat_tac {context = ctxt, ...} = Skip_Proof.cheat_tac (Proof_Context.theory_of ctxt); |
204 |
||
205 |
val case_tacs = map (K cheat_tac) ks; |
|
206 |
||
207 |
val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss @ [case_tacs]; |
|
49119 | 208 |
in |
49129 | 209 |
wrap_data tacss ((ctrs, caseof), (disc_binders, sel_binderss)) lthy' |
49119 | 210 |
end; |
49112 | 211 |
in |
49124 | 212 |
lthy' |
49129 | 213 |
|> fold wrap_type (bs ~~ Ts ~~ flds ~~ unfs ~~ fld_unfs ~~ unf_flds ~~ fld_injects ~~ |
214 |
ctr_binderss ~~ ctr_Tsss ~~ disc_binderss ~~ sel_bindersss) |
|
49112 | 215 |
end; |
216 |
||
49121 | 217 |
fun data_cmd info specs lthy = |
218 |
let |
|
219 |
val fake_lthy = |
|
220 |
Proof_Context.theory_of lthy |
|
221 |
|> Theory.copy |
|
222 |
|> Sign.add_types_global (map (fn spec => |
|
49129 | 223 |
(type_binder_of spec, length (type_args_constrained_of spec), mixfix_of_typ spec)) specs) |
49121 | 224 |
|> Proof_Context.init_global |
225 |
in |
|
226 |
prepare_data Syntax.read_typ info specs fake_lthy lthy |
|
227 |
end; |
|
49119 | 228 |
|
49129 | 229 |
val parse_opt_binding_colon = Scan.optional (Parse.binding --| Parse.$$$ ":") no_binder |
49119 | 230 |
|
49112 | 231 |
val parse_ctr_arg = |
49119 | 232 |
Parse.$$$ "(" |-- parse_opt_binding_colon -- Parse.typ --| Parse.$$$ ")" || |
49129 | 233 |
(Parse.typ >> pair no_binder); |
49112 | 234 |
|
235 |
val parse_single_spec = |
|
236 |
Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix -- |
|
49119 | 237 |
(@{keyword "="} |-- Parse.enum1 "|" (parse_opt_binding_colon -- Parse.binding -- |
238 |
Scan.repeat parse_ctr_arg -- Parse.opt_mixfix)); |
|
49112 | 239 |
|
240 |
val _ = |
|
241 |
Outer_Syntax.local_theory @{command_spec "data"} "define BNF-based inductive datatypes" |
|
49119 | 242 |
(Parse.and_list1 parse_single_spec >> data_cmd lfp_info); |
49112 | 243 |
|
244 |
val _ = |
|
245 |
Outer_Syntax.local_theory @{command_spec "codata"} "define BNF-based coinductive datatypes" |
|
49119 | 246 |
(Parse.and_list1 parse_single_spec >> data_cmd gfp_info); |
49112 | 247 |
|
248 |
end; |