
wenzelm@47455: (*  Title:      HOL/Library/Quotient_Option.thy
huffman@47624:     Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
kaliszyk@35222: *)
wenzelm@35788: 
wenzelm@35788: header {* Quotient infrastructure for the option type *}
wenzelm@35788: 
kaliszyk@35222: theory Quotient_Option
kaliszyk@35222: imports Main Quotient_Syntax
kaliszyk@35222: begin
kaliszyk@35222: 
huffman@47624: subsection {* Relator for option type *}
huffman@47624: 
kaliszyk@35222: fun
haftmann@40542:   option_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> bool"
kaliszyk@35222: where
kaliszyk@35222:   "option_rel R None None = True"
kaliszyk@35222: | "option_rel R (Some x) None = False"
kaliszyk@35222: | "option_rel R None (Some x) = False"
kaliszyk@35222: | "option_rel R (Some x) (Some y) = R x y"
kaliszyk@35222: 
haftmann@40820: lemma option_rel_unfold:
haftmann@40820:   "option_rel R x y = (case (x, y) of (None, None) \<Rightarrow> True
haftmann@40820:     | (Some x, Some y) \<Rightarrow> R x y
haftmann@40820:     | _ \<Rightarrow> False)"
haftmann@40820:   by (cases x) (cases y, simp_all)+
haftmann@40820: 
haftmann@40820: lemma option_rel_map1:
haftmann@40820:   "option_rel R (Option.map f x) y \<longleftrightarrow> option_rel (\<lambda>x. R (f x)) x y"
haftmann@40820:   by (simp add: option_rel_unfold split: option.split)
haftmann@40820: 
haftmann@40820: lemma option_rel_map2:
haftmann@40820:   "option_rel R x (Option.map f y) \<longleftrightarrow> option_rel (\<lambda>x y. R x (f y)) x y"
haftmann@40820:   by (simp add: option_rel_unfold split: option.split)
haftmann@40820: 
haftmann@40820: lemma option_map_id [id_simps]:
haftmann@40820:   "Option.map id = id"
haftmann@40820:   by (simp add: id_def Option.map.identity fun_eq_iff)
haftmann@40820: 
huffman@47624: lemma option_rel_eq [id_simps, relator_eq]:
haftmann@40820:   "option_rel (op =) = (op =)"
haftmann@40820:   by (simp add: option_rel_unfold fun_eq_iff split: option.split)
haftmann@40820: 
huffman@47624: lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
huffman@47624:   by (metis option.exhaust) (* TODO: move to Option.thy *)
huffman@47624: 
huffman@47624: lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
huffman@47624:   by (metis option.exhaust) (* TODO: move to Option.thy *)
huffman@47624: 
kuncar@47982: lemma option_reflp[reflexivity_rule]:
haftmann@40820:   "reflp R \<Longrightarrow> reflp (option_rel R)"
huffman@47624:   unfolding reflp_def split_option_all by simp
haftmann@40820: 
kuncar@47982: lemma option_left_total[reflexivity_rule]:
kuncar@47982:   "left_total R \<Longrightarrow> left_total (option_rel R)"
kuncar@47982:   apply (intro left_totalI)
kuncar@47982:   unfolding split_option_ex
kuncar@47982:   by (case_tac x) (auto elim: left_totalE)
kuncar@47982: 
haftmann@40820: lemma option_symp:
haftmann@40820:   "symp R \<Longrightarrow> symp (option_rel R)"
huffman@47624:   unfolding symp_def split_option_all option_rel.simps by fast
haftmann@40820: 
haftmann@40820: lemma option_transp:
haftmann@40820:   "transp R \<Longrightarrow> transp (option_rel R)"
huffman@47624:   unfolding transp_def split_option_all option_rel.simps by fast
haftmann@40820: 
haftmann@40820: lemma option_equivp [quot_equiv]:
haftmann@40820:   "equivp R \<Longrightarrow> equivp (option_rel R)"
haftmann@40820:   by (blast intro: equivpI option_reflp option_symp option_transp elim: equivpE)
haftmann@40820: 
huffman@47624: lemma right_total_option_rel [transfer_rule]:
huffman@47624:   "right_total R \<Longrightarrow> right_total (option_rel R)"
huffman@47624:   unfolding right_total_def split_option_all split_option_ex by simp
huffman@47624: 
huffman@47624: lemma right_unique_option_rel [transfer_rule]:
huffman@47624:   "right_unique R \<Longrightarrow> right_unique (option_rel R)"
huffman@47624:   unfolding right_unique_def split_option_all by simp
huffman@47624: 
huffman@47624: lemma bi_total_option_rel [transfer_rule]:
huffman@47624:   "bi_total R \<Longrightarrow> bi_total (option_rel R)"
huffman@47624:   unfolding bi_total_def split_option_all split_option_ex by simp
huffman@47624: 
huffman@47624: lemma bi_unique_option_rel [transfer_rule]:
huffman@47624:   "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
huffman@47624:   unfolding bi_unique_def split_option_all by simp
huffman@47624: 
huffman@47635: subsection {* Transfer rules for transfer package *}
huffman@47624: 
huffman@47624: lemma None_transfer [transfer_rule]: "(option_rel A) None None"
huffman@47624:   by simp
huffman@47624: 
huffman@47624: lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
huffman@47624:   unfolding fun_rel_def by simp
huffman@47624: 
huffman@47624: lemma option_case_transfer [transfer_rule]:
huffman@47624:   "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
huffman@47624:   unfolding fun_rel_def split_option_all by simp
huffman@47624: 
huffman@47624: lemma option_map_transfer [transfer_rule]:
huffman@47624:   "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
huffman@47635:   unfolding Option.map_def by transfer_prover
huffman@47624: 
huffman@47624: lemma option_bind_transfer [transfer_rule]:
huffman@47624:   "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
huffman@47624:     Option.bind Option.bind"
huffman@47624:   unfolding fun_rel_def split_option_all by simp
huffman@47624: 
huffman@47624: subsection {* Setup for lifting package *}
huffman@47624: 
kuncar@47777: lemma Quotient_option[quot_map]:
huffman@47624:   assumes "Quotient R Abs Rep T"
huffman@47624:   shows "Quotient (option_rel R) (Option.map Abs)
huffman@47624:     (Option.map Rep) (option_rel T)"
huffman@47624:   using assms unfolding Quotient_alt_def option_rel_unfold
huffman@47624:   by (simp split: option.split)
huffman@47624: 
kuncar@47634: fun option_pred :: "('a \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> bool"
kuncar@47634: where
kuncar@47634:   "option_pred R None = True"
kuncar@47634: | "option_pred R (Some x) = R x"
kuncar@47634: 
kuncar@47634: lemma option_invariant_commute [invariant_commute]:
kuncar@47634:   "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
kuncar@47634:   apply (simp add: fun_eq_iff Lifting.invariant_def)
kuncar@47634:   apply (intro allI) 
kuncar@47634:   apply (case_tac x rule: option.exhaust)
kuncar@47634:   apply (case_tac xa rule: option.exhaust)
kuncar@47634:   apply auto[2]
kuncar@47634:   apply (case_tac xa rule: option.exhaust)
kuncar@47634:   apply auto
kuncar@47634: done
kuncar@47634: 
huffman@47624: subsection {* Rules for quotient package *}
huffman@47624: 
haftmann@40820: lemma option_quotient [quot_thm]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kuncar@47308:   shows "Quotient3 (option_rel R) (Option.map Abs) (Option.map Rep)"
kuncar@47308:   apply (rule Quotient3I)
kuncar@47308:   apply (simp_all add: Option.map.compositionality comp_def Option.map.identity option_rel_eq option_rel_map1 option_rel_map2 Quotient3_abs_rep [OF assms] Quotient3_rel_rep [OF assms])
kuncar@47308:   using Quotient3_rel [OF assms]
haftmann@40820:   apply (simp add: option_rel_unfold split: option.split)
kaliszyk@35222:   done
kaliszyk@35222: 
kuncar@47308: declare [[mapQ3 option = (option_rel, option_quotient)]]
kuncar@47094: 
haftmann@40820: lemma option_None_rsp [quot_respect]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222:   shows "option_rel R None None"
huffman@47624:   by (rule None_transfer)
kaliszyk@35222: 
haftmann@40820: lemma option_Some_rsp [quot_respect]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222:   shows "(R ===> option_rel R) Some Some"
huffman@47624:   by (rule Some_transfer)
kaliszyk@35222: 
haftmann@40820: lemma option_None_prs [quot_preserve]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222:   shows "Option.map Abs None = None"
kaliszyk@35222:   by simp
kaliszyk@35222: 
haftmann@40820: lemma option_Some_prs [quot_preserve]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222:   shows "(Rep ---> Option.map Abs) Some = Some"
nipkow@39302:   apply(simp add: fun_eq_iff)
kuncar@47308:   apply(simp add: Quotient3_abs_rep[OF q])
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: end




