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