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