src/HOL/Lifting_Option.thy
changeset 53012 cb82606b8215
child 53026 e1a548c11845
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Lifting_Option.thy	Tue Aug 13 15:59:22 2013 +0200
     1.3 @@ -0,0 +1,125 @@
     1.4 +(*  Title:      HOL/Lifting_Option.thy
     1.5 +    Author:     Brian Huffman and Ondrej Kuncar
     1.6 +*)
     1.7 +
     1.8 +header {* Setup for Lifting/Transfer for the option type *}
     1.9 +
    1.10 +theory Lifting_Option
    1.11 +imports Lifting FunDef
    1.12 +begin
    1.13 +
    1.14 +subsection {* Relator and predicator properties *}
    1.15 +
    1.16 +fun
    1.17 +  option_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> bool"
    1.18 +where
    1.19 +  "option_rel R None None = True"
    1.20 +| "option_rel R (Some x) None = False"
    1.21 +| "option_rel R None (Some x) = False"
    1.22 +| "option_rel R (Some x) (Some y) = R x y"
    1.23 +
    1.24 +lemma option_rel_unfold:
    1.25 +  "option_rel R x y = (case (x, y) of (None, None) \<Rightarrow> True
    1.26 +    | (Some x, Some y) \<Rightarrow> R x y
    1.27 +    | _ \<Rightarrow> False)"
    1.28 +  by (cases x) (cases y, simp_all)+
    1.29 +
    1.30 +fun option_pred :: "('a \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> bool"
    1.31 +where
    1.32 +  "option_pred R None = True"
    1.33 +| "option_pred R (Some x) = R x"
    1.34 +
    1.35 +lemma option_pred_unfold:
    1.36 +  "option_pred P x = (case x of None \<Rightarrow> True
    1.37 +    | Some x \<Rightarrow> P x)"
    1.38 +by (cases x) simp_all
    1.39 +
    1.40 +lemma option_rel_eq [relator_eq]:
    1.41 +  "option_rel (op =) = (op =)"
    1.42 +  by (simp add: option_rel_unfold fun_eq_iff split: option.split)
    1.43 +
    1.44 +lemma option_rel_mono[relator_mono]:
    1.45 +  assumes "A \<le> B"
    1.46 +  shows "(option_rel A) \<le> (option_rel B)"
    1.47 +using assms by (auto simp: option_rel_unfold split: option.splits)
    1.48 +
    1.49 +lemma option_rel_OO[relator_distr]:
    1.50 +  "(option_rel A) OO (option_rel B) = option_rel (A OO B)"
    1.51 +by (rule ext)+ (auto simp: option_rel_unfold OO_def split: option.split)
    1.52 +
    1.53 +lemma Domainp_option[relator_domain]:
    1.54 +  assumes "Domainp A = P"
    1.55 +  shows "Domainp (option_rel A) = (option_pred P)"
    1.56 +using assms unfolding Domainp_iff[abs_def] option_rel_unfold[abs_def] option_pred_unfold[abs_def]
    1.57 +by (auto iff: fun_eq_iff split: option.split)
    1.58 +
    1.59 +lemma reflp_option_rel[reflexivity_rule]:
    1.60 +  "reflp R \<Longrightarrow> reflp (option_rel R)"
    1.61 +  unfolding reflp_def split_option_all by simp
    1.62 +
    1.63 +lemma left_total_option_rel[reflexivity_rule]:
    1.64 +  "left_total R \<Longrightarrow> left_total (option_rel R)"
    1.65 +  unfolding left_total_def split_option_all split_option_ex by simp
    1.66 +
    1.67 +lemma left_unique_option_rel [reflexivity_rule]:
    1.68 +  "left_unique R \<Longrightarrow> left_unique (option_rel R)"
    1.69 +  unfolding left_unique_def split_option_all by simp
    1.70 +
    1.71 +lemma right_total_option_rel [transfer_rule]:
    1.72 +  "right_total R \<Longrightarrow> right_total (option_rel R)"
    1.73 +  unfolding right_total_def split_option_all split_option_ex by simp
    1.74 +
    1.75 +lemma right_unique_option_rel [transfer_rule]:
    1.76 +  "right_unique R \<Longrightarrow> right_unique (option_rel R)"
    1.77 +  unfolding right_unique_def split_option_all by simp
    1.78 +
    1.79 +lemma bi_total_option_rel [transfer_rule]:
    1.80 +  "bi_total R \<Longrightarrow> bi_total (option_rel R)"
    1.81 +  unfolding bi_total_def split_option_all split_option_ex by simp
    1.82 +
    1.83 +lemma bi_unique_option_rel [transfer_rule]:
    1.84 +  "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
    1.85 +  unfolding bi_unique_def split_option_all by simp
    1.86 +
    1.87 +lemma option_invariant_commute [invariant_commute]:
    1.88 +  "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
    1.89 +  by (auto simp add: fun_eq_iff Lifting.invariant_def split_option_all)
    1.90 +
    1.91 +subsection {* Quotient theorem for the Lifting package *}
    1.92 +
    1.93 +lemma Quotient_option[quot_map]:
    1.94 +  assumes "Quotient R Abs Rep T"
    1.95 +  shows "Quotient (option_rel R) (Option.map Abs)
    1.96 +    (Option.map Rep) (option_rel T)"
    1.97 +  using assms unfolding Quotient_alt_def option_rel_unfold
    1.98 +  by (simp split: option.split)
    1.99 +
   1.100 +subsection {* Transfer rules for the Transfer package *}
   1.101 +
   1.102 +context
   1.103 +begin
   1.104 +interpretation lifting_syntax .
   1.105 +
   1.106 +lemma None_transfer [transfer_rule]: "(option_rel A) None None"
   1.107 +  by simp
   1.108 +
   1.109 +lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
   1.110 +  unfolding fun_rel_def by simp
   1.111 +
   1.112 +lemma option_case_transfer [transfer_rule]:
   1.113 +  "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
   1.114 +  unfolding fun_rel_def split_option_all by simp
   1.115 +
   1.116 +lemma option_map_transfer [transfer_rule]:
   1.117 +  "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
   1.118 +  unfolding Option.map_def by transfer_prover
   1.119 +
   1.120 +lemma option_bind_transfer [transfer_rule]:
   1.121 +  "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
   1.122 +    Option.bind Option.bind"
   1.123 +  unfolding fun_rel_def split_option_all by simp
   1.124 +
   1.125 +end
   1.126 +
   1.127 +end
   1.128 +