src/HOL/Library/Quotient_Option.thy
author kuncar
Tue Aug 13 15:59:22 2013 +0200 (2013-08-13)
changeset 53010 ec5e6f69bd65
parent 51994 82cc2aeb7d13
child 53012 cb82606b8215
permissions -rw-r--r--
move useful lemmas to Main
     1 (*  Title:      HOL/Library/Quotient_Option.thy
     2     Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
     3 *)
     4 
     5 header {* Quotient infrastructure for the option type *}
     6 
     7 theory Quotient_Option
     8 imports Main Quotient_Syntax
     9 begin
    10 
    11 subsection {* Relator for option type *}
    12 
    13 fun
    14   option_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> bool"
    15 where
    16   "option_rel R None None = True"
    17 | "option_rel R (Some x) None = False"
    18 | "option_rel R None (Some x) = False"
    19 | "option_rel R (Some x) (Some y) = R x y"
    20 
    21 lemma option_rel_unfold:
    22   "option_rel R x y = (case (x, y) of (None, None) \<Rightarrow> True
    23     | (Some x, Some y) \<Rightarrow> R x y
    24     | _ \<Rightarrow> False)"
    25   by (cases x) (cases y, simp_all)+
    26 
    27 fun option_pred :: "('a \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> bool"
    28 where
    29   "option_pred R None = True"
    30 | "option_pred R (Some x) = R x"
    31 
    32 lemma option_pred_unfold:
    33   "option_pred P x = (case x of None \<Rightarrow> True
    34     | Some x \<Rightarrow> P x)"
    35 by (cases x) simp_all
    36 
    37 lemma option_rel_map1:
    38   "option_rel R (Option.map f x) y \<longleftrightarrow> option_rel (\<lambda>x. R (f x)) x y"
    39   by (simp add: option_rel_unfold split: option.split)
    40 
    41 lemma option_rel_map2:
    42   "option_rel R x (Option.map f y) \<longleftrightarrow> option_rel (\<lambda>x y. R x (f y)) x y"
    43   by (simp add: option_rel_unfold split: option.split)
    44 
    45 lemma option_map_id [id_simps]:
    46   "Option.map id = id"
    47   by (simp add: id_def Option.map.identity fun_eq_iff)
    48 
    49 lemma option_rel_eq [id_simps, relator_eq]:
    50   "option_rel (op =) = (op =)"
    51   by (simp add: option_rel_unfold fun_eq_iff split: option.split)
    52 
    53 lemma option_rel_mono[relator_mono]:
    54   assumes "A \<le> B"
    55   shows "(option_rel A) \<le> (option_rel B)"
    56 using assms by (auto simp: option_rel_unfold split: option.splits)
    57 
    58 lemma option_rel_OO[relator_distr]:
    59   "(option_rel A) OO (option_rel B) = option_rel (A OO B)"
    60 by (rule ext)+ (auto simp: option_rel_unfold OO_def split: option.split)
    61 
    62 lemma Domainp_option[relator_domain]:
    63   assumes "Domainp A = P"
    64   shows "Domainp (option_rel A) = (option_pred P)"
    65 using assms unfolding Domainp_iff[abs_def] option_rel_unfold[abs_def] option_pred_unfold[abs_def]
    66 by (auto iff: fun_eq_iff split: option.split)
    67 
    68 lemma reflp_option_rel[reflexivity_rule]:
    69   "reflp R \<Longrightarrow> reflp (option_rel R)"
    70   unfolding reflp_def split_option_all by simp
    71 
    72 lemma left_total_option_rel[reflexivity_rule]:
    73   "left_total R \<Longrightarrow> left_total (option_rel R)"
    74   unfolding left_total_def split_option_all split_option_ex by simp
    75 
    76 lemma left_unique_option_rel [reflexivity_rule]:
    77   "left_unique R \<Longrightarrow> left_unique (option_rel R)"
    78   unfolding left_unique_def split_option_all by simp
    79 
    80 lemma option_symp:
    81   "symp R \<Longrightarrow> symp (option_rel R)"
    82   unfolding symp_def split_option_all option_rel.simps by fast
    83 
    84 lemma option_transp:
    85   "transp R \<Longrightarrow> transp (option_rel R)"
    86   unfolding transp_def split_option_all option_rel.simps by fast
    87 
    88 lemma option_equivp [quot_equiv]:
    89   "equivp R \<Longrightarrow> equivp (option_rel R)"
    90   by (blast intro: equivpI reflp_option_rel option_symp option_transp elim: equivpE)
    91 
    92 lemma right_total_option_rel [transfer_rule]:
    93   "right_total R \<Longrightarrow> right_total (option_rel R)"
    94   unfolding right_total_def split_option_all split_option_ex by simp
    95 
    96 lemma right_unique_option_rel [transfer_rule]:
    97   "right_unique R \<Longrightarrow> right_unique (option_rel R)"
    98   unfolding right_unique_def split_option_all by simp
    99 
   100 lemma bi_total_option_rel [transfer_rule]:
   101   "bi_total R \<Longrightarrow> bi_total (option_rel R)"
   102   unfolding bi_total_def split_option_all split_option_ex by simp
   103 
   104 lemma bi_unique_option_rel [transfer_rule]:
   105   "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
   106   unfolding bi_unique_def split_option_all by simp
   107 
   108 subsection {* Transfer rules for transfer package *}
   109 
   110 lemma None_transfer [transfer_rule]: "(option_rel A) None None"
   111   by simp
   112 
   113 lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
   114   unfolding fun_rel_def by simp
   115 
   116 lemma option_case_transfer [transfer_rule]:
   117   "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
   118   unfolding fun_rel_def split_option_all by simp
   119 
   120 lemma option_map_transfer [transfer_rule]:
   121   "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
   122   unfolding Option.map_def by transfer_prover
   123 
   124 lemma option_bind_transfer [transfer_rule]:
   125   "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
   126     Option.bind Option.bind"
   127   unfolding fun_rel_def split_option_all by simp
   128 
   129 subsection {* Setup for lifting package *}
   130 
   131 lemma Quotient_option[quot_map]:
   132   assumes "Quotient R Abs Rep T"
   133   shows "Quotient (option_rel R) (Option.map Abs)
   134     (Option.map Rep) (option_rel T)"
   135   using assms unfolding Quotient_alt_def option_rel_unfold
   136   by (simp split: option.split)
   137 
   138 lemma option_invariant_commute [invariant_commute]:
   139   "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
   140   apply (simp add: fun_eq_iff Lifting.invariant_def)
   141   apply (intro allI) 
   142   apply (case_tac x rule: option.exhaust)
   143   apply (case_tac xa rule: option.exhaust)
   144   apply auto[2]
   145   apply (case_tac xa rule: option.exhaust)
   146   apply auto
   147 done
   148 
   149 subsection {* Rules for quotient package *}
   150 
   151 lemma option_quotient [quot_thm]:
   152   assumes "Quotient3 R Abs Rep"
   153   shows "Quotient3 (option_rel R) (Option.map Abs) (Option.map Rep)"
   154   apply (rule Quotient3I)
   155   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])
   156   using Quotient3_rel [OF assms]
   157   apply (simp add: option_rel_unfold split: option.split)
   158   done
   159 
   160 declare [[mapQ3 option = (option_rel, option_quotient)]]
   161 
   162 lemma option_None_rsp [quot_respect]:
   163   assumes q: "Quotient3 R Abs Rep"
   164   shows "option_rel R None None"
   165   by (rule None_transfer)
   166 
   167 lemma option_Some_rsp [quot_respect]:
   168   assumes q: "Quotient3 R Abs Rep"
   169   shows "(R ===> option_rel R) Some Some"
   170   by (rule Some_transfer)
   171 
   172 lemma option_None_prs [quot_preserve]:
   173   assumes q: "Quotient3 R Abs Rep"
   174   shows "Option.map Abs None = None"
   175   by simp
   176 
   177 lemma option_Some_prs [quot_preserve]:
   178   assumes q: "Quotient3 R Abs Rep"
   179   shows "(Rep ---> Option.map Abs) Some = Some"
   180   apply(simp add: fun_eq_iff)
   181   apply(simp add: Quotient3_abs_rep[OF q])
   182   done
   183 
   184 end