huffman@47325: (* Title: HOL/Transfer.thy
huffman@47325: Author: Brian Huffman, TU Muenchen
kuncar@51956: Author: Ondrej Kuncar, TU Muenchen
huffman@47325: *)
huffman@47325:
huffman@47325: header {* Generic theorem transfer using relations *}
huffman@47325:
huffman@47325: theory Transfer
haftmann@51112: imports Hilbert_Choice
huffman@47325: begin
huffman@47325:
huffman@47325: subsection {* Relator for function space *}
huffman@47325:
huffman@47325: definition
huffman@47325: fun_rel :: "('a \ 'c \ bool) \ ('b \ 'd \ bool) \ ('a \ 'b) \ ('c \ 'd) \ bool" (infixr "===>" 55)
huffman@47325: where
huffman@47325: "fun_rel A B = (\f g. \x y. A x y \ B (f x) (g y))"
huffman@47325:
huffman@47325: lemma fun_relI [intro]:
huffman@47325: assumes "\x y. A x y \ B (f x) (g y)"
huffman@47325: shows "(A ===> B) f g"
huffman@47325: using assms by (simp add: fun_rel_def)
huffman@47325:
huffman@47325: lemma fun_relD:
huffman@47325: assumes "(A ===> B) f g" and "A x y"
huffman@47325: shows "B (f x) (g y)"
huffman@47325: using assms by (simp add: fun_rel_def)
huffman@47325:
kuncar@47937: lemma fun_relD2:
kuncar@47937: assumes "(A ===> B) f g" and "A x x"
kuncar@47937: shows "B (f x) (g x)"
kuncar@47937: using assms unfolding fun_rel_def by auto
kuncar@47937:
huffman@47325: lemma fun_relE:
huffman@47325: assumes "(A ===> B) f g" and "A x y"
huffman@47325: obtains "B (f x) (g y)"
huffman@47325: using assms by (simp add: fun_rel_def)
huffman@47325:
huffman@47325: lemma fun_rel_eq:
huffman@47325: shows "((op =) ===> (op =)) = (op =)"
huffman@47325: by (auto simp add: fun_eq_iff elim: fun_relE)
huffman@47325:
huffman@47325: lemma fun_rel_eq_rel:
huffman@47325: shows "((op =) ===> R) = (\f g. \x. R (f x) (g x))"
huffman@47325: by (simp add: fun_rel_def)
huffman@47325:
huffman@47325:
huffman@47325: subsection {* Transfer method *}
huffman@47325:
huffman@47789: text {* Explicit tag for relation membership allows for
huffman@47789: backward proof methods. *}
huffman@47325:
huffman@47325: definition Rel :: "('a \ 'b \ bool) \ 'a \ 'b \ bool"
huffman@47325: where "Rel r \ r"
huffman@47325:
huffman@49975: text {* Handling of equality relations *}
huffman@49975:
huffman@49975: definition is_equality :: "('a \ 'a \ bool) \ bool"
huffman@49975: where "is_equality R \ R = (op =)"
huffman@49975:
kuncar@51437: lemma is_equality_eq: "is_equality (op =)"
kuncar@51437: unfolding is_equality_def by simp
kuncar@51437:
huffman@52354: text {* Reverse implication for monotonicity rules *}
huffman@52354:
huffman@52354: definition rev_implies where
huffman@52354: "rev_implies x y \ (y \ x)"
huffman@52354:
huffman@47325: text {* Handling of meta-logic connectives *}
huffman@47325:
huffman@47325: definition transfer_forall where
huffman@47325: "transfer_forall \ All"
huffman@47325:
huffman@47325: definition transfer_implies where
huffman@47325: "transfer_implies \ op \"
huffman@47325:
huffman@47355: definition transfer_bforall :: "('a \ bool) \ ('a \ bool) \ bool"
huffman@47355: where "transfer_bforall \ (\P Q. \x. P x \ Q x)"
huffman@47355:
huffman@47325: lemma transfer_forall_eq: "(\x. P x) \ Trueprop (transfer_forall (\x. P x))"
huffman@47325: unfolding atomize_all transfer_forall_def ..
huffman@47325:
huffman@47325: lemma transfer_implies_eq: "(A \ B) \ Trueprop (transfer_implies A B)"
huffman@47325: unfolding atomize_imp transfer_implies_def ..
huffman@47325:
huffman@47355: lemma transfer_bforall_unfold:
huffman@47355: "Trueprop (transfer_bforall P (\x. Q x)) \ (\x. P x \ Q x)"
huffman@47355: unfolding transfer_bforall_def atomize_imp atomize_all ..
huffman@47355:
huffman@47658: lemma transfer_start: "\P; Rel (op =) P Q\ \ Q"
huffman@47325: unfolding Rel_def by simp
huffman@47325:
huffman@47658: lemma transfer_start': "\P; Rel (op \) P Q\ \ Q"
huffman@47325: unfolding Rel_def by simp
huffman@47325:
huffman@47635: lemma transfer_prover_start: "\x = x'; Rel R x' y\ \ Rel R x y"
huffman@47618: by simp
huffman@47618:
huffman@52358: lemma untransfer_start: "\Q; Rel (op =) P Q\ \ P"
huffman@52358: unfolding Rel_def by simp
huffman@52358:
huffman@47325: lemma Rel_eq_refl: "Rel (op =) x x"
huffman@47325: unfolding Rel_def ..
huffman@47325:
huffman@47789: lemma Rel_app:
huffman@47523: assumes "Rel (A ===> B) f g" and "Rel A x y"
huffman@47789: shows "Rel B (f x) (g y)"
huffman@47789: using assms unfolding Rel_def fun_rel_def by fast
huffman@47523:
huffman@47789: lemma Rel_abs:
huffman@47523: assumes "\x y. Rel A x y \ Rel B (f x) (g y)"
huffman@47789: shows "Rel (A ===> B) (\x. f x) (\y. g y)"
huffman@47789: using assms unfolding Rel_def fun_rel_def by fast
huffman@47523:
wenzelm@48891: ML_file "Tools/transfer.ML"
huffman@47325: setup Transfer.setup
huffman@47325:
huffman@49975: declare refl [transfer_rule]
huffman@49975:
huffman@47503: declare fun_rel_eq [relator_eq]
huffman@47503:
huffman@47789: hide_const (open) Rel
huffman@47325:
kuncar@51956: text {* Handling of domains *}
kuncar@51956:
kuncar@51956: lemma Domaimp_refl[transfer_domain_rule]:
kuncar@51956: "Domainp T = Domainp T" ..
huffman@47325:
huffman@47325: subsection {* Predicates on relations, i.e. ``class constraints'' *}
huffman@47325:
huffman@47325: definition right_total :: "('a \ 'b \ bool) \ bool"
huffman@47325: where "right_total R \ (\y. \x. R x y)"
huffman@47325:
huffman@47325: definition right_unique :: "('a \ 'b \ bool) \ bool"
huffman@47325: where "right_unique R \ (\x y z. R x y \ R x z \ y = z)"
huffman@47325:
huffman@47325: definition bi_total :: "('a \ 'b \ bool) \ bool"
huffman@47325: where "bi_total R \ (\x. \y. R x y) \ (\y. \x. R x y)"
huffman@47325:
huffman@47325: definition bi_unique :: "('a \ 'b \ bool) \ bool"
huffman@47325: where "bi_unique R \
huffman@47325: (\x y z. R x y \ R x z \ y = z) \
huffman@47325: (\x y z. R x z \ R y z \ x = y)"
huffman@47325:
huffman@47325: lemma right_total_alt_def:
huffman@47325: "right_total R \ ((R ===> op \) ===> op \) All All"
huffman@47325: unfolding right_total_def fun_rel_def
huffman@47325: apply (rule iffI, fast)
huffman@47325: apply (rule allI)
huffman@47325: apply (drule_tac x="\x. True" in spec)
huffman@47325: apply (drule_tac x="\y. \x. R x y" in spec)
huffman@47325: apply fast
huffman@47325: done
huffman@47325:
huffman@47325: lemma right_unique_alt_def:
huffman@47325: "right_unique R \ (R ===> R ===> op \) (op =) (op =)"
huffman@47325: unfolding right_unique_def fun_rel_def by auto
huffman@47325:
huffman@47325: lemma bi_total_alt_def:
huffman@47325: "bi_total R \ ((R ===> op =) ===> op =) All All"
huffman@47325: unfolding bi_total_def fun_rel_def
huffman@47325: apply (rule iffI, fast)
huffman@47325: apply safe
huffman@47325: apply (drule_tac x="\x. \y. R x y" in spec)
huffman@47325: apply (drule_tac x="\y. True" in spec)
huffman@47325: apply fast
huffman@47325: apply (drule_tac x="\x. True" in spec)
huffman@47325: apply (drule_tac x="\y. \x. R x y" in spec)
huffman@47325: apply fast
huffman@47325: done
huffman@47325:
huffman@47325: lemma bi_unique_alt_def:
huffman@47325: "bi_unique R \ (R ===> R ===> op =) (op =) (op =)"
huffman@47325: unfolding bi_unique_def fun_rel_def by auto
huffman@47325:
huffman@47660: text {* Properties are preserved by relation composition. *}
huffman@47660:
huffman@47660: lemma OO_def: "R OO S = (\x z. \y. R x y \ S y z)"
huffman@47660: by auto
huffman@47660:
huffman@47660: lemma bi_total_OO: "\bi_total A; bi_total B\ \ bi_total (A OO B)"
huffman@47660: unfolding bi_total_def OO_def by metis
huffman@47660:
huffman@47660: lemma bi_unique_OO: "\bi_unique A; bi_unique B\ \ bi_unique (A OO B)"
huffman@47660: unfolding bi_unique_def OO_def by metis
huffman@47660:
huffman@47660: lemma right_total_OO:
huffman@47660: "\right_total A; right_total B\ \ right_total (A OO B)"
huffman@47660: unfolding right_total_def OO_def by metis
huffman@47660:
huffman@47660: lemma right_unique_OO:
huffman@47660: "\right_unique A; right_unique B\ \ right_unique (A OO B)"
huffman@47660: unfolding right_unique_def OO_def by metis
huffman@47660:
huffman@47325:
huffman@47325: subsection {* Properties of relators *}
huffman@47325:
huffman@47325: lemma right_total_eq [transfer_rule]: "right_total (op =)"
huffman@47325: unfolding right_total_def by simp
huffman@47325:
huffman@47325: lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
huffman@47325: unfolding right_unique_def by simp
huffman@47325:
huffman@47325: lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
huffman@47325: unfolding bi_total_def by simp
huffman@47325:
huffman@47325: lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
huffman@47325: unfolding bi_unique_def by simp
huffman@47325:
huffman@47325: lemma right_total_fun [transfer_rule]:
huffman@47325: "\right_unique A; right_total B\ \ right_total (A ===> B)"
huffman@47325: unfolding right_total_def fun_rel_def
huffman@47325: apply (rule allI, rename_tac g)
huffman@47325: apply (rule_tac x="\x. SOME z. B z (g (THE y. A x y))" in exI)
huffman@47325: apply clarify
huffman@47325: apply (subgoal_tac "(THE y. A x y) = y", simp)
huffman@47325: apply (rule someI_ex)
huffman@47325: apply (simp)
huffman@47325: apply (rule the_equality)
huffman@47325: apply assumption
huffman@47325: apply (simp add: right_unique_def)
huffman@47325: done
huffman@47325:
huffman@47325: lemma right_unique_fun [transfer_rule]:
huffman@47325: "\right_total A; right_unique B\ \ right_unique (A ===> B)"
huffman@47325: unfolding right_total_def right_unique_def fun_rel_def
huffman@47325: by (clarify, rule ext, fast)
huffman@47325:
huffman@47325: lemma bi_total_fun [transfer_rule]:
huffman@47325: "\bi_unique A; bi_total B\ \ bi_total (A ===> B)"
huffman@47325: unfolding bi_total_def fun_rel_def
huffman@47325: apply safe
huffman@47325: apply (rename_tac f)
huffman@47325: apply (rule_tac x="\y. SOME z. B (f (THE x. A x y)) z" in exI)
huffman@47325: apply clarify
huffman@47325: apply (subgoal_tac "(THE x. A x y) = x", simp)
huffman@47325: apply (rule someI_ex)
huffman@47325: apply (simp)
huffman@47325: apply (rule the_equality)
huffman@47325: apply assumption
huffman@47325: apply (simp add: bi_unique_def)
huffman@47325: apply (rename_tac g)
huffman@47325: apply (rule_tac x="\x. SOME z. B z (g (THE y. A x y))" in exI)
huffman@47325: apply clarify
huffman@47325: apply (subgoal_tac "(THE y. A x y) = y", simp)
huffman@47325: apply (rule someI_ex)
huffman@47325: apply (simp)
huffman@47325: apply (rule the_equality)
huffman@47325: apply assumption
huffman@47325: apply (simp add: bi_unique_def)
huffman@47325: done
huffman@47325:
huffman@47325: lemma bi_unique_fun [transfer_rule]:
huffman@47325: "\bi_total A; bi_unique B\ \ bi_unique (A ===> B)"
huffman@47325: unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
huffman@47325: by (safe, metis, fast)
huffman@47325:
huffman@47325:
huffman@47635: subsection {* Transfer rules *}
huffman@47325:
huffman@47684: text {* Transfer rules using implication instead of equality on booleans. *}
huffman@47684:
huffman@52354: lemma transfer_forall_transfer [transfer_rule]:
huffman@52354: "bi_total A \ ((A ===> op =) ===> op =) transfer_forall transfer_forall"
huffman@52354: "right_total A \ ((A ===> op =) ===> implies) transfer_forall transfer_forall"
huffman@52354: "right_total A \ ((A ===> implies) ===> implies) transfer_forall transfer_forall"
huffman@52354: "bi_total A \ ((A ===> op =) ===> rev_implies) transfer_forall transfer_forall"
huffman@52354: "bi_total A \ ((A ===> rev_implies) ===> rev_implies) transfer_forall transfer_forall"
huffman@52354: unfolding transfer_forall_def rev_implies_def fun_rel_def right_total_def bi_total_def
huffman@52354: by metis+
huffman@52354:
huffman@52354: lemma transfer_implies_transfer [transfer_rule]:
huffman@52354: "(op = ===> op = ===> op = ) transfer_implies transfer_implies"
huffman@52354: "(rev_implies ===> implies ===> implies ) transfer_implies transfer_implies"
huffman@52354: "(rev_implies ===> op = ===> implies ) transfer_implies transfer_implies"
huffman@52354: "(op = ===> implies ===> implies ) transfer_implies transfer_implies"
huffman@52354: "(op = ===> op = ===> implies ) transfer_implies transfer_implies"
huffman@52354: "(implies ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
huffman@52354: "(implies ===> op = ===> rev_implies) transfer_implies transfer_implies"
huffman@52354: "(op = ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
huffman@52354: "(op = ===> op = ===> rev_implies) transfer_implies transfer_implies"
huffman@52354: unfolding transfer_implies_def rev_implies_def fun_rel_def by auto
huffman@52354:
huffman@47684: lemma eq_imp_transfer [transfer_rule]:
huffman@47684: "right_unique A \ (A ===> A ===> op \) (op =) (op =)"
huffman@47684: unfolding right_unique_alt_def .
huffman@47684:
huffman@47636: lemma eq_transfer [transfer_rule]:
huffman@47325: assumes "bi_unique A"
huffman@47325: shows "(A ===> A ===> op =) (op =) (op =)"
huffman@47325: using assms unfolding bi_unique_def fun_rel_def by auto
huffman@47325:
kuncar@51956: lemma Domainp_iff: "Domainp T x \ (\y. T x y)"
kuncar@51956: by auto
kuncar@51956:
kuncar@51956: lemma right_total_Ex_transfer[transfer_rule]:
kuncar@51956: assumes "right_total A"
kuncar@51956: shows "((A ===> op=) ===> op=) (Bex (Collect (Domainp A))) Ex"
kuncar@51956: using assms unfolding right_total_def Bex_def fun_rel_def Domainp_iff[abs_def]
kuncar@51956: by blast
kuncar@51956:
kuncar@51956: lemma right_total_All_transfer[transfer_rule]:
kuncar@51956: assumes "right_total A"
kuncar@51956: shows "((A ===> op =) ===> op =) (Ball (Collect (Domainp A))) All"
kuncar@51956: using assms unfolding right_total_def Ball_def fun_rel_def Domainp_iff[abs_def]
kuncar@51956: by blast
kuncar@51956:
huffman@47636: lemma All_transfer [transfer_rule]:
huffman@47325: assumes "bi_total A"
huffman@47325: shows "((A ===> op =) ===> op =) All All"
huffman@47325: using assms unfolding bi_total_def fun_rel_def by fast
huffman@47325:
huffman@47636: lemma Ex_transfer [transfer_rule]:
huffman@47325: assumes "bi_total A"
huffman@47325: shows "((A ===> op =) ===> op =) Ex Ex"
huffman@47325: using assms unfolding bi_total_def fun_rel_def by fast
huffman@47325:
huffman@47636: lemma If_transfer [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
huffman@47325: unfolding fun_rel_def by simp
huffman@47325:
huffman@47636: lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
huffman@47612: unfolding fun_rel_def by simp
huffman@47612:
huffman@47636: lemma id_transfer [transfer_rule]: "(A ===> A) id id"
huffman@47625: unfolding fun_rel_def by simp
huffman@47625:
huffman@47636: lemma comp_transfer [transfer_rule]:
huffman@47325: "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \) (op \)"
huffman@47325: unfolding fun_rel_def by simp
huffman@47325:
huffman@47636: lemma fun_upd_transfer [transfer_rule]:
huffman@47325: assumes [transfer_rule]: "bi_unique A"
huffman@47325: shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
huffman@47635: unfolding fun_upd_def [abs_def] by transfer_prover
huffman@47325:
huffman@47637: lemma nat_case_transfer [transfer_rule]:
huffman@47637: "(A ===> (op = ===> A) ===> op = ===> A) nat_case nat_case"
huffman@47637: unfolding fun_rel_def by (simp split: nat.split)
huffman@47627:
huffman@47924: lemma nat_rec_transfer [transfer_rule]:
huffman@47924: "(A ===> (op = ===> A ===> A) ===> op = ===> A) nat_rec nat_rec"
huffman@47924: unfolding fun_rel_def by (clarsimp, rename_tac n, induct_tac n, simp_all)
huffman@47924:
huffman@47924: lemma funpow_transfer [transfer_rule]:
huffman@47924: "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
huffman@47924: unfolding funpow_def by transfer_prover
huffman@47924:
huffman@47627: lemma Domainp_forall_transfer [transfer_rule]:
huffman@47627: assumes "right_total A"
huffman@47627: shows "((A ===> op =) ===> op =)
huffman@47627: (transfer_bforall (Domainp A)) transfer_forall"
huffman@47627: using assms unfolding right_total_def
huffman@47627: unfolding transfer_forall_def transfer_bforall_def fun_rel_def Domainp_iff
huffman@47627: by metis
huffman@47627:
huffman@47636: lemma forall_transfer [transfer_rule]:
huffman@47627: "bi_total A \ ((A ===> op =) ===> op =) transfer_forall transfer_forall"
huffman@47636: unfolding transfer_forall_def by (rule All_transfer)
huffman@47325:
huffman@47325: end