src/HOL/Lifting_Option.thy
author kuncar
Tue Aug 13 15:59:22 2013 +0200 (2013-08-13)
changeset 53012 cb82606b8215
child 53026 e1a548c11845
permissions -rw-r--r--
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
     1 (*  Title:      HOL/Lifting_Option.thy
     2     Author:     Brian Huffman and Ondrej Kuncar
     3 *)
     4 
     5 header {* Setup for Lifting/Transfer for the option type *}
     6 
     7 theory Lifting_Option
     8 imports Lifting FunDef
     9 begin
    10 
    11 subsection {* Relator and predicator properties *}
    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_eq [relator_eq]:
    38   "option_rel (op =) = (op =)"
    39   by (simp add: option_rel_unfold fun_eq_iff split: option.split)
    40 
    41 lemma option_rel_mono[relator_mono]:
    42   assumes "A \<le> B"
    43   shows "(option_rel A) \<le> (option_rel B)"
    44 using assms by (auto simp: option_rel_unfold split: option.splits)
    45 
    46 lemma option_rel_OO[relator_distr]:
    47   "(option_rel A) OO (option_rel B) = option_rel (A OO B)"
    48 by (rule ext)+ (auto simp: option_rel_unfold OO_def split: option.split)
    49 
    50 lemma Domainp_option[relator_domain]:
    51   assumes "Domainp A = P"
    52   shows "Domainp (option_rel A) = (option_pred P)"
    53 using assms unfolding Domainp_iff[abs_def] option_rel_unfold[abs_def] option_pred_unfold[abs_def]
    54 by (auto iff: fun_eq_iff split: option.split)
    55 
    56 lemma reflp_option_rel[reflexivity_rule]:
    57   "reflp R \<Longrightarrow> reflp (option_rel R)"
    58   unfolding reflp_def split_option_all by simp
    59 
    60 lemma left_total_option_rel[reflexivity_rule]:
    61   "left_total R \<Longrightarrow> left_total (option_rel R)"
    62   unfolding left_total_def split_option_all split_option_ex by simp
    63 
    64 lemma left_unique_option_rel [reflexivity_rule]:
    65   "left_unique R \<Longrightarrow> left_unique (option_rel R)"
    66   unfolding left_unique_def split_option_all by simp
    67 
    68 lemma right_total_option_rel [transfer_rule]:
    69   "right_total R \<Longrightarrow> right_total (option_rel R)"
    70   unfolding right_total_def split_option_all split_option_ex by simp
    71 
    72 lemma right_unique_option_rel [transfer_rule]:
    73   "right_unique R \<Longrightarrow> right_unique (option_rel R)"
    74   unfolding right_unique_def split_option_all by simp
    75 
    76 lemma bi_total_option_rel [transfer_rule]:
    77   "bi_total R \<Longrightarrow> bi_total (option_rel R)"
    78   unfolding bi_total_def split_option_all split_option_ex by simp
    79 
    80 lemma bi_unique_option_rel [transfer_rule]:
    81   "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
    82   unfolding bi_unique_def split_option_all by simp
    83 
    84 lemma option_invariant_commute [invariant_commute]:
    85   "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
    86   by (auto simp add: fun_eq_iff Lifting.invariant_def split_option_all)
    87 
    88 subsection {* Quotient theorem for the Lifting package *}
    89 
    90 lemma Quotient_option[quot_map]:
    91   assumes "Quotient R Abs Rep T"
    92   shows "Quotient (option_rel R) (Option.map Abs)
    93     (Option.map Rep) (option_rel T)"
    94   using assms unfolding Quotient_alt_def option_rel_unfold
    95   by (simp split: option.split)
    96 
    97 subsection {* Transfer rules for the Transfer package *}
    98 
    99 context
   100 begin
   101 interpretation lifting_syntax .
   102 
   103 lemma None_transfer [transfer_rule]: "(option_rel A) None None"
   104   by simp
   105 
   106 lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
   107   unfolding fun_rel_def by simp
   108 
   109 lemma option_case_transfer [transfer_rule]:
   110   "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
   111   unfolding fun_rel_def split_option_all by simp
   112 
   113 lemma option_map_transfer [transfer_rule]:
   114   "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
   115   unfolding Option.map_def by transfer_prover
   116 
   117 lemma option_bind_transfer [transfer_rule]:
   118   "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
   119     Option.bind Option.bind"
   120   unfolding fun_rel_def split_option_all by simp
   121 
   122 end
   123 
   124 end
   125