src/HOL/Library/Quotient_Option.thy
author Christian Sternagel
Wed Aug 29 12:23:14 2012 +0900 (2012-08-29)
changeset 49083 01081bca31b6
parent 47982 7aa35601ff65
child 51377 7da251a6c16e
permissions -rw-r--r--
dropped ord and bot instance for list prefixes (use locale interpretation instead, which allows users to decide what order to use on lists)
     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 lemma option_rel_map1:
    28   "option_rel R (Option.map f x) y \<longleftrightarrow> option_rel (\<lambda>x. R (f x)) x y"
    29   by (simp add: option_rel_unfold split: option.split)
    30 
    31 lemma option_rel_map2:
    32   "option_rel R x (Option.map f y) \<longleftrightarrow> option_rel (\<lambda>x y. R x (f y)) x y"
    33   by (simp add: option_rel_unfold split: option.split)
    34 
    35 lemma option_map_id [id_simps]:
    36   "Option.map id = id"
    37   by (simp add: id_def Option.map.identity fun_eq_iff)
    38 
    39 lemma option_rel_eq [id_simps, relator_eq]:
    40   "option_rel (op =) = (op =)"
    41   by (simp add: option_rel_unfold fun_eq_iff split: option.split)
    42 
    43 lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
    44   by (metis option.exhaust) (* TODO: move to Option.thy *)
    45 
    46 lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
    47   by (metis option.exhaust) (* TODO: move to Option.thy *)
    48 
    49 lemma option_reflp[reflexivity_rule]:
    50   "reflp R \<Longrightarrow> reflp (option_rel R)"
    51   unfolding reflp_def split_option_all by simp
    52 
    53 lemma option_left_total[reflexivity_rule]:
    54   "left_total R \<Longrightarrow> left_total (option_rel R)"
    55   apply (intro left_totalI)
    56   unfolding split_option_ex
    57   by (case_tac x) (auto elim: left_totalE)
    58 
    59 lemma option_symp:
    60   "symp R \<Longrightarrow> symp (option_rel R)"
    61   unfolding symp_def split_option_all option_rel.simps by fast
    62 
    63 lemma option_transp:
    64   "transp R \<Longrightarrow> transp (option_rel R)"
    65   unfolding transp_def split_option_all option_rel.simps by fast
    66 
    67 lemma option_equivp [quot_equiv]:
    68   "equivp R \<Longrightarrow> equivp (option_rel R)"
    69   by (blast intro: equivpI option_reflp option_symp option_transp elim: equivpE)
    70 
    71 lemma right_total_option_rel [transfer_rule]:
    72   "right_total R \<Longrightarrow> right_total (option_rel R)"
    73   unfolding right_total_def split_option_all split_option_ex by simp
    74 
    75 lemma right_unique_option_rel [transfer_rule]:
    76   "right_unique R \<Longrightarrow> right_unique (option_rel R)"
    77   unfolding right_unique_def split_option_all by simp
    78 
    79 lemma bi_total_option_rel [transfer_rule]:
    80   "bi_total R \<Longrightarrow> bi_total (option_rel R)"
    81   unfolding bi_total_def split_option_all split_option_ex by simp
    82 
    83 lemma bi_unique_option_rel [transfer_rule]:
    84   "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
    85   unfolding bi_unique_def split_option_all by simp
    86 
    87 subsection {* Transfer rules for transfer package *}
    88 
    89 lemma None_transfer [transfer_rule]: "(option_rel A) None None"
    90   by simp
    91 
    92 lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
    93   unfolding fun_rel_def by simp
    94 
    95 lemma option_case_transfer [transfer_rule]:
    96   "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
    97   unfolding fun_rel_def split_option_all by simp
    98 
    99 lemma option_map_transfer [transfer_rule]:
   100   "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
   101   unfolding Option.map_def by transfer_prover
   102 
   103 lemma option_bind_transfer [transfer_rule]:
   104   "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
   105     Option.bind Option.bind"
   106   unfolding fun_rel_def split_option_all by simp
   107 
   108 subsection {* Setup for lifting package *}
   109 
   110 lemma Quotient_option[quot_map]:
   111   assumes "Quotient R Abs Rep T"
   112   shows "Quotient (option_rel R) (Option.map Abs)
   113     (Option.map Rep) (option_rel T)"
   114   using assms unfolding Quotient_alt_def option_rel_unfold
   115   by (simp split: option.split)
   116 
   117 fun option_pred :: "('a \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> bool"
   118 where
   119   "option_pred R None = True"
   120 | "option_pred R (Some x) = R x"
   121 
   122 lemma option_invariant_commute [invariant_commute]:
   123   "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
   124   apply (simp add: fun_eq_iff Lifting.invariant_def)
   125   apply (intro allI) 
   126   apply (case_tac x rule: option.exhaust)
   127   apply (case_tac xa rule: option.exhaust)
   128   apply auto[2]
   129   apply (case_tac xa rule: option.exhaust)
   130   apply auto
   131 done
   132 
   133 subsection {* Rules for quotient package *}
   134 
   135 lemma option_quotient [quot_thm]:
   136   assumes "Quotient3 R Abs Rep"
   137   shows "Quotient3 (option_rel R) (Option.map Abs) (Option.map Rep)"
   138   apply (rule Quotient3I)
   139   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])
   140   using Quotient3_rel [OF assms]
   141   apply (simp add: option_rel_unfold split: option.split)
   142   done
   143 
   144 declare [[mapQ3 option = (option_rel, option_quotient)]]
   145 
   146 lemma option_None_rsp [quot_respect]:
   147   assumes q: "Quotient3 R Abs Rep"
   148   shows "option_rel R None None"
   149   by (rule None_transfer)
   150 
   151 lemma option_Some_rsp [quot_respect]:
   152   assumes q: "Quotient3 R Abs Rep"
   153   shows "(R ===> option_rel R) Some Some"
   154   by (rule Some_transfer)
   155 
   156 lemma option_None_prs [quot_preserve]:
   157   assumes q: "Quotient3 R Abs Rep"
   158   shows "Option.map Abs None = None"
   159   by simp
   160 
   161 lemma option_Some_prs [quot_preserve]:
   162   assumes q: "Quotient3 R Abs Rep"
   163   shows "(Rep ---> Option.map Abs) Some = Some"
   164   apply(simp add: fun_eq_iff)
   165   apply(simp add: Quotient3_abs_rep[OF q])
   166   done
   167 
   168 end