1 (* Title: HOL/HOLCF/Tools/Domain/domain_take_proofs.ML
4 Defines take functions for the given domain equation
5 and proves related theorems.
8 signature DOMAIN_TAKE_PROOFS =
21 take_consts : term list,
23 chain_take_thms : thm list,
24 take_0_thms : thm list,
25 take_Suc_thms : thm list,
26 deflation_take_thms : thm list,
27 take_strict_thms : thm list,
28 finite_consts : term list,
29 finite_defs : thm list
31 type take_induct_info =
33 take_consts : term list,
35 chain_take_thms : thm list,
36 take_0_thms : thm list,
37 take_Suc_thms : thm list,
38 deflation_take_thms : thm list,
39 take_strict_thms : thm list,
40 finite_consts : term list,
41 finite_defs : thm list,
42 lub_take_thms : thm list,
43 reach_thms : thm list,
44 take_lemma_thms : thm list,
46 take_induct_thms : thm list
48 val define_take_functions :
49 (binding * iso_info) list -> theory -> take_info * theory
51 val add_lub_take_theorems :
52 (binding * iso_info) list -> take_info -> thm list ->
53 theory -> take_induct_info * theory
56 theory -> (typ * term) list -> typ -> term
58 val add_rec_type : (string * bool list) -> theory -> theory
59 val get_rec_tab : theory -> (bool list) Symtab.table
60 val add_deflation_thm : thm -> theory -> theory
61 val get_deflation_thms : theory -> thm list
62 val map_ID_add : attribute
63 val get_map_ID_thms : theory -> thm list
64 val setup : theory -> theory
67 structure Domain_Take_Proofs : DOMAIN_TAKE_PROOFS =
81 { take_consts : term list,
83 chain_take_thms : thm list,
84 take_0_thms : thm list,
85 take_Suc_thms : thm list,
86 deflation_take_thms : thm list,
87 take_strict_thms : thm list,
88 finite_consts : term list,
89 finite_defs : thm list
92 type take_induct_info =
94 take_consts : term list,
96 chain_take_thms : thm list,
97 take_0_thms : thm list,
98 take_Suc_thms : thm list,
99 deflation_take_thms : thm list,
100 take_strict_thms : thm list,
101 finite_consts : term list,
102 finite_defs : thm list,
103 lub_take_thms : thm list,
104 reach_thms : thm list,
105 take_lemma_thms : thm list,
107 take_induct_thms : thm list
111 HOL_basic_ss addsimps simp_thms addsimprocs [@{simproc beta_cfun_proc}]
113 (******************************************************************************)
114 (******************************** theory data *********************************)
115 (******************************************************************************)
117 structure Rec_Data = Theory_Data
119 (* list indicates which type arguments allow indirect recursion *)
120 type T = (bool list) Symtab.table
121 val empty = Symtab.empty
123 fun merge data = Symtab.merge (K true) data
126 structure DeflMapData = Named_Thms
128 val name = @{binding domain_deflation}
129 val description = "theorems like deflation a ==> deflation (foo_map$a)"
132 structure Map_Id_Data = Named_Thms
134 val name = @{binding domain_map_ID}
135 val description = "theorems like foo_map$ID = ID"
138 fun add_rec_type (tname, bs) =
139 Rec_Data.map (Symtab.insert (K true) (tname, bs))
141 fun add_deflation_thm thm =
142 Context.theory_map (DeflMapData.add_thm thm)
144 val get_rec_tab = Rec_Data.get
145 fun get_deflation_thms thy = DeflMapData.get (Proof_Context.init_global thy)
147 val map_ID_add = Map_Id_Data.add
148 val get_map_ID_thms = Map_Id_Data.get o Proof_Context.init_global
150 val setup = DeflMapData.setup #> Map_Id_Data.setup
152 (******************************************************************************)
153 (************************** building types and terms **************************)
154 (******************************************************************************)
163 Const (@{const_name deflation}, Term.fastype_of t --> boolT) $ t
165 fun mk_eqs (t, u) = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
167 (******************************************************************************)
168 (****************************** isomorphism info ******************************)
169 (******************************************************************************)
171 fun deflation_abs_rep (info : iso_info) : thm =
173 val abs_iso = #abs_inverse info
174 val rep_iso = #rep_inverse info
175 val thm = @{thm deflation_abs_rep} OF [abs_iso, rep_iso]
177 Drule.zero_var_indexes thm
180 (******************************************************************************)
181 (********************* building map functions over types **********************)
182 (******************************************************************************)
184 fun map_of_typ (thy : theory) (sub : (typ * term) list) (T : typ) : term =
186 val thms = get_map_ID_thms thy
187 val rules = map (Thm.concl_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq) thms
188 val rules' = map (apfst mk_ID) sub @ map swap rules
191 |> Pattern.rewrite_term thy rules' []
192 |> Pattern.rewrite_term thy rules []
195 (******************************************************************************)
196 (********************* declaring definitions and theorems *********************)
197 (******************************************************************************)
199 fun add_qualified_def name (dbind, eqn) =
200 yield_singleton (Global_Theory.add_defs false)
201 ((Binding.qualified true name dbind, eqn), [])
203 fun add_qualified_thm name (dbind, thm) =
204 yield_singleton Global_Theory.add_thms
205 ((Binding.qualified true name dbind, thm), [])
207 fun add_qualified_simp_thm name (dbind, thm) =
208 yield_singleton Global_Theory.add_thms
209 ((Binding.qualified true name dbind, thm), [Simplifier.simp_add])
211 (******************************************************************************)
212 (************************** defining take functions ***************************)
213 (******************************************************************************)
215 fun define_take_functions
216 (spec : (binding * iso_info) list)
220 (* retrieve components of spec *)
221 val dbinds = map fst spec
222 val iso_infos = map snd spec
223 val dom_eqns = map (fn x => (#absT x, #repT x)) iso_infos
224 val rep_abs_consts = map (fn x => (#rep_const x, #abs_const x)) iso_infos
226 fun mk_projs [] _ = []
227 | mk_projs (x::[]) t = [(x, t)]
228 | mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t)
230 fun mk_cfcomp2 ((rep_const, abs_const), f) =
231 mk_cfcomp (abs_const, mk_cfcomp (f, rep_const))
233 (* define take functional *)
234 val newTs : typ list = map fst dom_eqns
235 val copy_arg_type = mk_tupleT (map (fn T => T ->> T) newTs)
236 val copy_arg = Free ("f", copy_arg_type)
237 val copy_args = map snd (mk_projs dbinds copy_arg)
238 fun one_copy_rhs (rep_abs, (_, rhsT)) =
240 val body = map_of_typ thy (newTs ~~ copy_args) rhsT
242 mk_cfcomp2 (rep_abs, body)
244 val take_functional =
246 (mk_tuple (map one_copy_rhs (rep_abs_consts ~~ dom_eqns)))
249 val n = Free ("n", HOLogic.natT)
250 val rhs = mk_iterate (n, take_functional)
252 map (lambda n o snd) (mk_projs dbinds rhs)
255 (* define take constants *)
256 fun define_take_const ((dbind, take_rhs), (lhsT, _)) thy =
258 val take_type = HOLogic.natT --> lhsT ->> lhsT
259 val take_bind = Binding.suffix_name "_take" dbind
260 val (take_const, thy) =
261 Sign.declare_const_global ((take_bind, take_type), NoSyn) thy
262 val take_eqn = Logic.mk_equals (take_const, take_rhs)
263 val (take_def_thm, thy) =
264 add_qualified_def "take_def" (dbind, take_eqn) thy
265 in ((take_const, take_def_thm), thy) end
266 val ((take_consts, take_defs), thy) = thy
267 |> fold_map define_take_const (dbinds ~~ take_rhss ~~ dom_eqns)
270 (* prove chain_take lemmas *)
271 fun prove_chain_take (take_const, dbind) thy =
273 val goal = mk_trp (mk_chain take_const)
274 val rules = take_defs @ @{thms chain_iterate ch2ch_fst ch2ch_snd}
275 val tac = simp_tac (HOL_basic_ss addsimps rules) 1
276 val thm = Goal.prove_global thy [] [] goal (K tac)
278 add_qualified_simp_thm "chain_take" (dbind, thm) thy
280 val (chain_take_thms, thy) =
281 fold_map prove_chain_take (take_consts ~~ dbinds) thy
283 (* prove take_0 lemmas *)
284 fun prove_take_0 ((take_const, dbind), (lhsT, _)) thy =
286 val lhs = take_const $ @{term "0::nat"}
287 val goal = mk_eqs (lhs, mk_bottom (lhsT ->> lhsT))
288 val rules = take_defs @ @{thms iterate_0 fst_strict snd_strict}
289 val tac = simp_tac (HOL_basic_ss addsimps rules) 1
290 val take_0_thm = Goal.prove_global thy [] [] goal (K tac)
292 add_qualified_simp_thm "take_0" (dbind, take_0_thm) thy
294 val (take_0_thms, thy) =
295 fold_map prove_take_0 (take_consts ~~ dbinds ~~ dom_eqns) thy
297 (* prove take_Suc lemmas *)
298 val n = Free ("n", natT)
299 val take_is = map (fn t => t $ n) take_consts
301 (((take_const, rep_abs), dbind), (_, rhsT)) thy =
303 val lhs = take_const $ (@{term Suc} $ n)
304 val body = map_of_typ thy (newTs ~~ take_is) rhsT
305 val rhs = mk_cfcomp2 (rep_abs, body)
306 val goal = mk_eqs (lhs, rhs)
307 val simps = @{thms iterate_Suc fst_conv snd_conv}
308 val rules = take_defs @ simps
309 val tac = simp_tac (beta_ss addsimps rules) 1
310 val take_Suc_thm = Goal.prove_global thy [] [] goal (K tac)
312 add_qualified_thm "take_Suc" (dbind, take_Suc_thm) thy
314 val (take_Suc_thms, thy) =
315 fold_map prove_take_Suc
316 (take_consts ~~ rep_abs_consts ~~ dbinds ~~ dom_eqns) thy
318 (* prove deflation theorems for take functions *)
319 val deflation_abs_rep_thms = map deflation_abs_rep iso_infos
320 val deflation_take_thm =
322 val n = Free ("n", natT)
323 fun mk_goal take_const = mk_deflation (take_const $ n)
324 val goal = mk_trp (foldr1 mk_conj (map mk_goal take_consts))
326 take_0_thms @ @{thms deflation_bottom simp_thms}
327 val deflation_rules =
328 @{thms conjI deflation_ID}
329 @ deflation_abs_rep_thms
330 @ get_deflation_thms thy
332 Goal.prove_global thy [] [] goal (fn _ =>
334 [rtac @{thm nat.induct} 1,
335 simp_tac (HOL_basic_ss addsimps bottom_rules) 1,
336 asm_simp_tac (HOL_basic_ss addsimps take_Suc_thms) 1,
337 REPEAT (etac @{thm conjE} 1
338 ORELSE resolve_tac deflation_rules 1
341 fun conjuncts [] _ = []
342 | conjuncts (n::[]) thm = [(n, thm)]
343 | conjuncts (n::ns) thm = let
344 val thmL = thm RS @{thm conjunct1}
345 val thmR = thm RS @{thm conjunct2}
346 in (n, thmL):: conjuncts ns thmR end
347 val (deflation_take_thms, thy) =
348 fold_map (add_qualified_thm "deflation_take")
349 (map (apsnd Drule.zero_var_indexes)
350 (conjuncts dbinds deflation_take_thm)) thy
352 (* prove strictness of take functions *)
353 fun prove_take_strict (deflation_take, dbind) thy =
355 val take_strict_thm =
356 Drule.zero_var_indexes
357 (@{thm deflation_strict} OF [deflation_take])
359 add_qualified_simp_thm "take_strict" (dbind, take_strict_thm) thy
361 val (take_strict_thms, thy) =
362 fold_map prove_take_strict
363 (deflation_take_thms ~~ dbinds) thy
365 (* prove take/take rules *)
366 fun prove_take_take ((chain_take, deflation_take), dbind) thy =
369 Drule.zero_var_indexes
370 (@{thm deflation_chain_min} OF [chain_take, deflation_take])
372 add_qualified_thm "take_take" (dbind, take_take_thm) thy
375 fold_map prove_take_take
376 (chain_take_thms ~~ deflation_take_thms ~~ dbinds) thy
378 (* prove take_below rules *)
379 fun prove_take_below (deflation_take, dbind) thy =
382 Drule.zero_var_indexes
383 (@{thm deflation.below} OF [deflation_take])
385 add_qualified_thm "take_below" (dbind, take_below_thm) thy
388 fold_map prove_take_below
389 (deflation_take_thms ~~ dbinds) thy
391 (* define finiteness predicates *)
392 fun define_finite_const ((dbind, take_const), (lhsT, _)) thy =
394 val finite_type = lhsT --> boolT
395 val finite_bind = Binding.suffix_name "_finite" dbind
396 val (finite_const, thy) =
397 Sign.declare_const_global ((finite_bind, finite_type), NoSyn) thy
398 val x = Free ("x", lhsT)
399 val n = Free ("n", natT)
401 lambda x (HOLogic.exists_const natT $
402 (lambda n (mk_eq (mk_capply (take_const $ n, x), x))))
403 val finite_eqn = Logic.mk_equals (finite_const, finite_rhs)
404 val (finite_def_thm, thy) =
405 add_qualified_def "finite_def" (dbind, finite_eqn) thy
406 in ((finite_const, finite_def_thm), thy) end
407 val ((finite_consts, finite_defs), thy) = thy
408 |> fold_map define_finite_const (dbinds ~~ take_consts ~~ dom_eqns)
413 take_consts = take_consts,
414 take_defs = take_defs,
415 chain_take_thms = chain_take_thms,
416 take_0_thms = take_0_thms,
417 take_Suc_thms = take_Suc_thms,
418 deflation_take_thms = deflation_take_thms,
419 take_strict_thms = take_strict_thms,
420 finite_consts = finite_consts,
421 finite_defs = finite_defs
428 fun prove_finite_take_induct
429 (spec : (binding * iso_info) list)
430 (take_info : take_info)
431 (lub_take_thms : thm list)
434 val dbinds = map fst spec
435 val iso_infos = map snd spec
436 val absTs = map #absT iso_infos
437 val {take_consts, ...} = take_info
438 val {chain_take_thms, take_0_thms, take_Suc_thms, ...} = take_info
439 val {finite_consts, finite_defs, ...} = take_info
443 fun iso_locale (info : iso_info) =
444 @{thm iso.intro} OF [#abs_inverse info, #rep_inverse info]
445 val iso_locale_thms = map iso_locale iso_infos
446 val decisive_abs_rep_thms =
447 map (fn x => @{thm decisive_abs_rep} OF [x]) iso_locale_thms
448 val n = Free ("n", @{typ nat})
450 Const (@{const_name decisive}, fastype_of t --> boolT) $ t
451 fun f take_const = mk_decisive (take_const $ n)
452 val goal = mk_trp (foldr1 mk_conj (map f take_consts))
453 val rules0 = @{thm decisive_bottom} :: take_0_thms
455 take_Suc_thms @ decisive_abs_rep_thms
456 @ @{thms decisive_ID decisive_ssum_map decisive_sprod_map}
458 rtac @{thm nat.induct} 1,
459 simp_tac (HOL_ss addsimps rules0) 1,
460 asm_simp_tac (HOL_ss addsimps rules1) 1]
461 in Goal.prove_global thy [] [] goal (K tac) end
462 fun conjuncts 1 thm = [thm]
463 | conjuncts n thm = let
464 val thmL = thm RS @{thm conjunct1}
465 val thmR = thm RS @{thm conjunct2}
466 in thmL :: conjuncts (n-1) thmR end
467 val decisive_thms = conjuncts (length spec) decisive_lemma
469 fun prove_finite_thm (absT, finite_const) =
471 val goal = mk_trp (finite_const $ Free ("x", absT))
474 rewrite_goals_tac finite_defs,
475 rtac @{thm lub_ID_finite} 1,
476 resolve_tac chain_take_thms 1,
477 resolve_tac lub_take_thms 1,
478 resolve_tac decisive_thms 1]
480 Goal.prove_global thy [] [] goal (K tac)
483 map prove_finite_thm (absTs ~~ finite_consts)
485 fun prove_take_induct ((ch_take, lub_take), decisive) =
486 Drule.export_without_context
487 (@{thm lub_ID_finite_take_induct} OF [ch_take, lub_take, decisive])
488 val take_induct_thms =
489 map prove_take_induct
490 (chain_take_thms ~~ lub_take_thms ~~ decisive_thms)
493 |> fold (snd oo add_qualified_thm "finite")
494 (dbinds ~~ finite_thms)
495 |> fold (snd oo add_qualified_thm "take_induct")
496 (dbinds ~~ take_induct_thms)
498 ((finite_thms, take_induct_thms), thy)
501 fun add_lub_take_theorems
502 (spec : (binding * iso_info) list)
503 (take_info : take_info)
504 (lub_take_thms : thm list)
508 (* retrieve components of spec *)
509 val dbinds = map fst spec
510 val iso_infos = map snd spec
511 val absTs = map #absT iso_infos
512 val repTs = map #repT iso_infos
513 val {chain_take_thms, ...} = take_info
515 (* prove take lemmas *)
516 fun prove_take_lemma ((chain_take, lub_take), dbind) thy =
519 Drule.export_without_context
520 (@{thm lub_ID_take_lemma} OF [chain_take, lub_take])
522 add_qualified_thm "take_lemma" (dbind, take_lemma) thy
524 val (take_lemma_thms, thy) =
525 fold_map prove_take_lemma
526 (chain_take_thms ~~ lub_take_thms ~~ dbinds) thy
528 (* prove reach lemmas *)
529 fun prove_reach_lemma ((chain_take, lub_take), dbind) thy =
532 Drule.zero_var_indexes
533 (@{thm lub_ID_reach} OF [chain_take, lub_take])
535 add_qualified_thm "reach" (dbind, thm) thy
537 val (reach_thms, thy) =
538 fold_map prove_reach_lemma
539 (chain_take_thms ~~ lub_take_thms ~~ dbinds) thy
541 (* test for finiteness of domain definitions *)
543 val types = [@{type_name ssum}, @{type_name sprod}]
544 fun finite d T = if member (op =) absTs T then d else finite' d T
545 and finite' d (Type (c, Ts)) =
546 let val d' = d andalso member (op =) types c
547 in forall (finite d') Ts end
550 val is_finite = forall (finite true) repTs
553 val ((_, take_induct_thms), thy) =
557 val ((finites, take_inducts), thy) =
558 prove_finite_take_induct spec take_info lub_take_thms thy
560 ((SOME finites, take_inducts), thy)
564 fun prove_take_induct (chain_take, lub_take) =
565 Drule.zero_var_indexes
566 (@{thm lub_ID_take_induct} OF [chain_take, lub_take])
568 map prove_take_induct (chain_take_thms ~~ lub_take_thms)
569 val thy = fold (snd oo add_qualified_thm "take_induct")
570 (dbinds ~~ take_inducts) thy
572 ((NONE, take_inducts), thy)
577 take_consts = #take_consts take_info,
578 take_defs = #take_defs take_info,
579 chain_take_thms = #chain_take_thms take_info,
580 take_0_thms = #take_0_thms take_info,
581 take_Suc_thms = #take_Suc_thms take_info,
582 deflation_take_thms = #deflation_take_thms take_info,
583 take_strict_thms = #take_strict_thms take_info,
584 finite_consts = #finite_consts take_info,
585 finite_defs = #finite_defs take_info,
586 lub_take_thms = lub_take_thms,
587 reach_thms = reach_thms,
588 take_lemma_thms = take_lemma_thms,
589 is_finite = is_finite,
590 take_induct_thms = take_induct_thms