author  blanchet 
Tue, 04 Sep 2012 23:09:08 +0200  
changeset 49134  846264f80f16 
parent 49130  3c26e17b2849 
child 49135  de13b454fa31 
permissions  rwrr 
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 

69 
(* TODO: check that no type variables occur in the rhss that's not in the lhss *) 

70 
(* TODO: use sort constraints on type args *) 

71 

72 
val N = length specs; 

73 

49121  74 
fun mk_T b = 
75 
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))), 

76 
As); 

77 

49129  78 
val bs = map type_binder_of specs; 
49121  79 
val Ts = map mk_T bs; 
80 

81 
val mixfixes = map mixfix_of_typ specs; 

49119  82 

83 
val _ = (case duplicates Binding.eq_name bs of [] => () 

84 
 b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b))); 

85 

49121  86 
val ctr_specss = map ctr_specs_of specs; 
49119  87 

49129  88 
val disc_binderss = map (map disc_of) ctr_specss; 
89 
val ctr_binderss = map (map ctr_of) ctr_specss; 

49121  90 
val ctr_argsss = map (map args_of) ctr_specss; 
91 
val ctr_mixfixess = map (map mixfix_of_ctr) ctr_specss; 

49119  92 

49129  93 
val sel_bindersss = map (map (map fst)) ctr_argsss; 
49121  94 
val ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss; 
95 

96 
val (Bs, C) = 

97 
lthy 

98 
> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs 

99 
> mk_TFrees N 

100 
> the_single o fst o mk_TFrees 1; 

49119  101 

49121  102 
fun freeze_rec (T as Type (s, Ts')) = 
103 
(case find_index (curry (op =) T) Ts of 

104 
~1 => Type (s, map freeze_rec Ts') 

105 
 i => nth Bs i) 

106 
 freeze_rec T = T; 

107 

108 
val ctr_TsssBs = map (map (map freeze_rec)) ctr_Tsss; 

109 
val sum_prod_TsBs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssBs; 

49119  110 

49121  111 
val eqs = map dest_TFree Bs ~~ sum_prod_TsBs; 
112 

49126  113 
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

114 
fp_bnf (if gfp then bnf_gfp else bnf_lfp) bs As' eqs lthy; 
49121  115 

49126  116 
fun mk_unf_or_fld get_foldedT Ts t = 
49124  117 
let val Type (_, Ts0) = get_foldedT (fastype_of t) in 
118 
Term.subst_atomic_types (Ts0 ~~ Ts) t 

49121  119 
end; 
49119  120 

49126  121 
val mk_unf = mk_unf_or_fld domain_type; 
122 
val mk_fld = mk_unf_or_fld range_type; 

49121  123 

49126  124 
val unfs = map (mk_unf As) raw_unfs; 
49124  125 
val flds = map (mk_fld As) raw_flds; 
126 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

127 
fun pour_some_sugar_on_type ((((((((((b, T), fld), unf), fld_unf), unf_fld), fld_inject), 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

128 
ctr_binders), ctr_Tss), disc_binders), sel_binderss) no_defs_lthy = 
49121  129 
let 
49129  130 
val n = length ctr_binders; 
49121  131 
val ks = 1 upto n; 
132 
val ms = map length ctr_Tss; 

133 

49124  134 
val unf_T = domain_type (fastype_of fld); 
49121  135 
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

136 
val case_Ts = map (fn Ts => Ts > C) ctr_Tss; 
49119  137 

49129  138 
val ((((fs, u), v), xss), _) = 
49124  139 
lthy 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

140 
> mk_Frees "f" case_Ts 
49129  141 
>> yield_singleton (mk_Frees "u") unf_T 
49124  142 
>> yield_singleton (mk_Frees "v") T 
143 
>> mk_Freess "x" ctr_Tss; 

49121  144 

49129  145 
val ctr_rhss = 
49121  146 
map2 (fn k => fn xs => 
147 
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; 

148 

49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

149 
val case_binder = Binding.suffix_name ("_" ^ caseN) b; 
49129  150 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

151 
val case_rhs = 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

152 
fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN (uncurry_fs fs xss) $ (unf $ v)); 
49129  153 

49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

154 
val (((raw_ctrs, raw_ctr_defs), (raw_case, raw_case_def)), (lthy', lthy)) = no_defs_lthy 
49121  155 
> apfst split_list o fold_map2 (fn b => fn rhs => 
156 
Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs)) #>> apsnd snd) 

49129  157 
ctr_binders ctr_rhss 
49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

158 
>> (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

159 
case_rhs)) #>> apsnd snd) 
49121  160 
> `Local_Theory.restore; 
161 

162 
(*transforms defined frees into consts (and more)*) 

163 
val phi = Proof_Context.export_morphism lthy lthy'; 

164 

165 
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

166 
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

167 

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; 
49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

169 
val casex = Morphism.term phi raw_case; 
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 

49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

202 
val case_tacs = 
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

203 
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

204 
mk_case_tac ctxt n k m case_def ctr_def unf_fld) ks ms ctr_defs; 
49121  205 

206 
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

207 

846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

208 
fun sugar_lfp lthy = 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

209 
let 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

210 
(*### 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

211 
val iter_Tss = map ( ) ctr_Tss 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

212 
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

213 

846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

214 
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

215 

846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

216 
val uncurried_fs = 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

217 
map2 (fn f => fn xs => 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

218 
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

219 
*) 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

220 
in 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

221 
lthy 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

222 
end; 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

223 

846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

224 
fun sugar_gfp lthy = lthy; 
49119  225 
in 
49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

226 
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

227 
> (if gfp then sugar_gfp else sugar_lfp) 
49119  228 
end; 
49112  229 
in 
49124  230 
lthy' 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

231 
> fold pour_some_sugar_on_type (bs ~~ Ts ~~ flds ~~ unfs ~~ fld_unfs ~~ unf_flds ~~ fld_injects ~~ 
49129  232 
ctr_binderss ~~ ctr_Tsss ~~ disc_binderss ~~ sel_bindersss) 
49112  233 
end; 
234 

49121  235 
fun data_cmd info specs lthy = 
236 
let 

237 
val fake_lthy = 

238 
Proof_Context.theory_of lthy 

239 
> Theory.copy 

240 
> Sign.add_types_global (map (fn spec => 

49129  241 
(type_binder_of spec, length (type_args_constrained_of spec), mixfix_of_typ spec)) specs) 
49121  242 
> Proof_Context.init_global 
243 
in 

244 
prepare_data Syntax.read_typ info specs fake_lthy lthy 

245 
end; 

49119  246 

49129  247 
val parse_opt_binding_colon = Scan.optional (Parse.binding  Parse.$$$ ":") no_binder 
49119  248 

49112  249 
val parse_ctr_arg = 
49119  250 
Parse.$$$ "("  parse_opt_binding_colon  Parse.typ  Parse.$$$ ")"  
49129  251 
(Parse.typ >> pair no_binder); 
49112  252 

253 
val parse_single_spec = 

254 
Parse.type_args_constrained  Parse.binding  Parse.opt_mixfix  

49119  255 
(@{keyword "="}  Parse.enum1 "" (parse_opt_binding_colon  Parse.binding  
256 
Scan.repeat parse_ctr_arg  Parse.opt_mixfix)); 

49112  257 

258 
val _ = 

259 
Outer_Syntax.local_theory @{command_spec "data"} "define BNFbased inductive datatypes" 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

260 
(Parse.and_list1 parse_single_spec >> data_cmd false); 
49112  261 

262 
val _ = 

263 
Outer_Syntax.local_theory @{command_spec "codata"} "define BNFbased coinductive datatypes" 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

264 
(Parse.and_list1 parse_single_spec >> data_cmd true); 
49112  265 

266 
end; 