
wenzelm@35788: (*  Title:      HOL/Library/Quotient_Option.thy
kaliszyk@35222:     Author:     Cezary Kaliszyk and Christian Urban
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: 
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: 
kuncar@45802: declare [[map option = option_rel]]
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: 
haftmann@40820: lemma option_rel_eq [id_simps]:
haftmann@40820:   "option_rel (op =) = (op =)"
haftmann@40820:   by (simp add: option_rel_unfold fun_eq_iff split: option.split)
haftmann@40820: 
haftmann@40820: lemma option_reflp:
haftmann@40820:   "reflp R \<Longrightarrow> reflp (option_rel R)"
haftmann@40820:   by (auto simp add: option_rel_unfold split: option.splits intro!: reflpI elim: reflpE)
haftmann@40820: 
haftmann@40820: lemma option_symp:
haftmann@40820:   "symp R \<Longrightarrow> symp (option_rel R)"
haftmann@40820:   by (auto simp add: option_rel_unfold split: option.splits intro!: sympI elim: sympE)
haftmann@40820: 
haftmann@40820: lemma option_transp:
haftmann@40820:   "transp R \<Longrightarrow> transp (option_rel R)"
haftmann@40820:   by (auto simp add: option_rel_unfold split: option.splits intro!: transpI elim: transpE)
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: 
haftmann@40820: lemma option_quotient [quot_thm]:
haftmann@40820:   assumes "Quotient R Abs Rep"
haftmann@40820:   shows "Quotient (option_rel R) (Option.map Abs) (Option.map Rep)"
haftmann@40820:   apply (rule QuotientI)
haftmann@41372:   apply (simp_all add: Option.map.compositionality comp_def Option.map.identity option_rel_eq option_rel_map1 option_rel_map2 Quotient_abs_rep [OF assms] Quotient_rel_rep [OF assms])
haftmann@40820:   using Quotient_rel [OF assms]
haftmann@40820:   apply (simp add: option_rel_unfold split: option.split)
kaliszyk@35222:   done
kaliszyk@35222: 
haftmann@40820: lemma option_None_rsp [quot_respect]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@35222:   shows "option_rel R None None"
kaliszyk@35222:   by simp
kaliszyk@35222: 
haftmann@40820: lemma option_Some_rsp [quot_respect]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@35222:   shows "(R ===> option_rel R) Some Some"
haftmann@40464:   by auto
kaliszyk@35222: 
haftmann@40820: lemma option_None_prs [quot_preserve]:
kaliszyk@35222:   assumes q: "Quotient 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]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@35222:   shows "(Rep ---> Option.map Abs) Some = Some"
nipkow@39302:   apply(simp add: fun_eq_iff)
kaliszyk@35222:   apply(simp add: Quotient_abs_rep[OF q])
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: end




