src/HOL/Lifting_Option.thy
author blanchet
Fri Jan 24 11:51:45 2014 +0100 (2014-01-24)
changeset 55129 26bd1cba3ab5
parent 55090 9475b16e520b
child 55404 5cb95b79a51f
permissions -rw-r--r--
killed 'More_BNFs' by moving its various bits where they (now) belong
kuncar@53012
     1
(*  Title:      HOL/Lifting_Option.thy
kuncar@53012
     2
    Author:     Brian Huffman and Ondrej Kuncar
blanchet@55129
     3
    Author:     Andreas Lochbihler, Karlsruhe Institute of Technology
kuncar@53012
     4
*)
kuncar@53012
     5
kuncar@53012
     6
header {* Setup for Lifting/Transfer for the option type *}
kuncar@53012
     7
kuncar@53012
     8
theory Lifting_Option
blanchet@55090
     9
imports Lifting Partial_Function
kuncar@53012
    10
begin
kuncar@53012
    11
kuncar@53012
    12
subsection {* Relator and predicator properties *}
kuncar@53012
    13
traytel@53026
    14
definition
kuncar@53012
    15
  option_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> bool"
kuncar@53012
    16
where
kuncar@53012
    17
  "option_rel R x y = (case (x, y) of (None, None) \<Rightarrow> True
kuncar@53012
    18
    | (Some x, Some y) \<Rightarrow> R x y
kuncar@53012
    19
    | _ \<Rightarrow> False)"
kuncar@53012
    20
traytel@53026
    21
lemma option_rel_simps[simp]:
traytel@53026
    22
  "option_rel R None None = True"
traytel@53026
    23
  "option_rel R (Some x) None = False"
traytel@53026
    24
  "option_rel R None (Some y) = False"
traytel@53026
    25
  "option_rel R (Some x) (Some y) = R x y"
traytel@53026
    26
  unfolding option_rel_def by simp_all
kuncar@53012
    27
traytel@53026
    28
abbreviation (input) option_pred :: "('a \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> bool" where
traytel@53026
    29
  "option_pred \<equiv> option_case True"
kuncar@53012
    30
kuncar@53012
    31
lemma option_rel_eq [relator_eq]:
kuncar@53012
    32
  "option_rel (op =) = (op =)"
traytel@53026
    33
  by (simp add: option_rel_def fun_eq_iff split: option.split)
kuncar@53012
    34
kuncar@53012
    35
lemma option_rel_mono[relator_mono]:
kuncar@53012
    36
  assumes "A \<le> B"
kuncar@53012
    37
  shows "(option_rel A) \<le> (option_rel B)"
traytel@53026
    38
using assms by (auto simp: option_rel_def split: option.splits)
kuncar@53012
    39
kuncar@53012
    40
lemma option_rel_OO[relator_distr]:
kuncar@53012
    41
  "(option_rel A) OO (option_rel B) = option_rel (A OO B)"
traytel@53026
    42
by (rule ext)+ (auto simp: option_rel_def OO_def split: option.split)
kuncar@53012
    43
kuncar@53012
    44
lemma Domainp_option[relator_domain]:
kuncar@53012
    45
  assumes "Domainp A = P"
kuncar@53012
    46
  shows "Domainp (option_rel A) = (option_pred P)"
traytel@53026
    47
using assms unfolding Domainp_iff[abs_def] option_rel_def[abs_def]
kuncar@53012
    48
by (auto iff: fun_eq_iff split: option.split)
kuncar@53012
    49
kuncar@53012
    50
lemma reflp_option_rel[reflexivity_rule]:
kuncar@53012
    51
  "reflp R \<Longrightarrow> reflp (option_rel R)"
kuncar@53012
    52
  unfolding reflp_def split_option_all by simp
kuncar@53012
    53
kuncar@53012
    54
lemma left_total_option_rel[reflexivity_rule]:
kuncar@53012
    55
  "left_total R \<Longrightarrow> left_total (option_rel R)"
kuncar@53012
    56
  unfolding left_total_def split_option_all split_option_ex by simp
kuncar@53012
    57
kuncar@53012
    58
lemma left_unique_option_rel [reflexivity_rule]:
kuncar@53012
    59
  "left_unique R \<Longrightarrow> left_unique (option_rel R)"
kuncar@53012
    60
  unfolding left_unique_def split_option_all by simp
kuncar@53012
    61
kuncar@53012
    62
lemma right_total_option_rel [transfer_rule]:
kuncar@53012
    63
  "right_total R \<Longrightarrow> right_total (option_rel R)"
kuncar@53012
    64
  unfolding right_total_def split_option_all split_option_ex by simp
kuncar@53012
    65
kuncar@53012
    66
lemma right_unique_option_rel [transfer_rule]:
kuncar@53012
    67
  "right_unique R \<Longrightarrow> right_unique (option_rel R)"
kuncar@53012
    68
  unfolding right_unique_def split_option_all by simp
kuncar@53012
    69
kuncar@53012
    70
lemma bi_total_option_rel [transfer_rule]:
kuncar@53012
    71
  "bi_total R \<Longrightarrow> bi_total (option_rel R)"
kuncar@53012
    72
  unfolding bi_total_def split_option_all split_option_ex by simp
kuncar@53012
    73
kuncar@53012
    74
lemma bi_unique_option_rel [transfer_rule]:
kuncar@53012
    75
  "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
kuncar@53012
    76
  unfolding bi_unique_def split_option_all by simp
kuncar@53012
    77
kuncar@53012
    78
lemma option_invariant_commute [invariant_commute]:
kuncar@53012
    79
  "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
kuncar@53012
    80
  by (auto simp add: fun_eq_iff Lifting.invariant_def split_option_all)
kuncar@53012
    81
kuncar@53012
    82
subsection {* Quotient theorem for the Lifting package *}
kuncar@53012
    83
kuncar@53012
    84
lemma Quotient_option[quot_map]:
kuncar@53012
    85
  assumes "Quotient R Abs Rep T"
kuncar@53012
    86
  shows "Quotient (option_rel R) (Option.map Abs)
kuncar@53012
    87
    (Option.map Rep) (option_rel T)"
traytel@53026
    88
  using assms unfolding Quotient_alt_def option_rel_def
kuncar@53012
    89
  by (simp split: option.split)
kuncar@53012
    90
kuncar@53012
    91
subsection {* Transfer rules for the Transfer package *}
kuncar@53012
    92
kuncar@53012
    93
context
kuncar@53012
    94
begin
kuncar@53012
    95
interpretation lifting_syntax .
kuncar@53012
    96
kuncar@53012
    97
lemma None_transfer [transfer_rule]: "(option_rel A) None None"
kuncar@53012
    98
  by simp
kuncar@53012
    99
kuncar@53012
   100
lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
kuncar@53012
   101
  unfolding fun_rel_def by simp
kuncar@53012
   102
kuncar@53012
   103
lemma option_case_transfer [transfer_rule]:
kuncar@53012
   104
  "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
kuncar@53012
   105
  unfolding fun_rel_def split_option_all by simp
kuncar@53012
   106
kuncar@53012
   107
lemma option_map_transfer [transfer_rule]:
kuncar@53012
   108
  "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
kuncar@53012
   109
  unfolding Option.map_def by transfer_prover
kuncar@53012
   110
kuncar@53012
   111
lemma option_bind_transfer [transfer_rule]:
kuncar@53012
   112
  "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
kuncar@53012
   113
    Option.bind Option.bind"
kuncar@53012
   114
  unfolding fun_rel_def split_option_all by simp
kuncar@53012
   115
kuncar@53012
   116
end
kuncar@53012
   117
blanchet@55129
   118
blanchet@55129
   119
subsubsection {* BNF setup *}
blanchet@55129
   120
blanchet@55129
   121
lemma option_rec_conv_option_case: "option_rec = option_case"
blanchet@55129
   122
by (simp add: fun_eq_iff split: option.split)
blanchet@55129
   123
blanchet@55129
   124
bnf "'a option"
blanchet@55129
   125
  map: Option.map
blanchet@55129
   126
  sets: Option.set
blanchet@55129
   127
  bd: natLeq
blanchet@55129
   128
  wits: None
blanchet@55129
   129
  rel: option_rel
blanchet@55129
   130
proof -
blanchet@55129
   131
  show "Option.map id = id" by (rule Option.map.id)
blanchet@55129
   132
next
blanchet@55129
   133
  fix f g
blanchet@55129
   134
  show "Option.map (g \<circ> f) = Option.map g \<circ> Option.map f"
blanchet@55129
   135
    by (auto simp add: fun_eq_iff Option.map_def split: option.split)
blanchet@55129
   136
next
blanchet@55129
   137
  fix f g x
blanchet@55129
   138
  assume "\<And>z. z \<in> Option.set x \<Longrightarrow> f z = g z"
blanchet@55129
   139
  thus "Option.map f x = Option.map g x"
blanchet@55129
   140
    by (simp cong: Option.map_cong)
blanchet@55129
   141
next
blanchet@55129
   142
  fix f
blanchet@55129
   143
  show "Option.set \<circ> Option.map f = op ` f \<circ> Option.set"
blanchet@55129
   144
    by fastforce
blanchet@55129
   145
next
blanchet@55129
   146
  show "card_order natLeq" by (rule natLeq_card_order)
blanchet@55129
   147
next
blanchet@55129
   148
  show "cinfinite natLeq" by (rule natLeq_cinfinite)
blanchet@55129
   149
next
blanchet@55129
   150
  fix x
blanchet@55129
   151
  show "|Option.set x| \<le>o natLeq"
blanchet@55129
   152
    by (cases x) (simp_all add: ordLess_imp_ordLeq finite_iff_ordLess_natLeq[symmetric])
blanchet@55129
   153
next
blanchet@55129
   154
  fix R S
blanchet@55129
   155
  show "option_rel R OO option_rel S \<le> option_rel (R OO S)"
blanchet@55129
   156
    by (auto simp: option_rel_def split: option.splits)
blanchet@55129
   157
next
blanchet@55129
   158
  fix z
blanchet@55129
   159
  assume "z \<in> Option.set None"
blanchet@55129
   160
  thus False by simp
blanchet@55129
   161
next
blanchet@55129
   162
  fix R
blanchet@55129
   163
  show "option_rel R =
blanchet@55129
   164
        (Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map fst))\<inverse>\<inverse> OO
blanchet@55129
   165
         Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map snd)"
blanchet@55129
   166
  unfolding option_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff prod.cases
blanchet@55129
   167
  by (auto simp: trans[OF eq_commute option_map_is_None] trans[OF eq_commute option_map_eq_Some]
blanchet@55129
   168
           split: option.splits)
blanchet@55129
   169
qed
blanchet@55129
   170
kuncar@53012
   171
end