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
kuncar@53012
     1
(*  Title:      HOL/Lifting_Option.thy
kuncar@53012
     2
    Author:     Brian Huffman and Ondrej Kuncar
kuncar@53012
     3
*)
kuncar@53012
     4
kuncar@53012
     5
header {* Setup for Lifting/Transfer for the option type *}
kuncar@53012
     6
kuncar@53012
     7
theory Lifting_Option
kuncar@53012
     8
imports Lifting FunDef
kuncar@53012
     9
begin
kuncar@53012
    10
kuncar@53012
    11
subsection {* Relator and predicator properties *}
kuncar@53012
    12
kuncar@53012
    13
fun
kuncar@53012
    14
  option_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> bool"
kuncar@53012
    15
where
kuncar@53012
    16
  "option_rel R None None = True"
kuncar@53012
    17
| "option_rel R (Some x) None = False"
kuncar@53012
    18
| "option_rel R None (Some x) = False"
kuncar@53012
    19
| "option_rel R (Some x) (Some y) = R x y"
kuncar@53012
    20
kuncar@53012
    21
lemma option_rel_unfold:
kuncar@53012
    22
  "option_rel R x y = (case (x, y) of (None, None) \<Rightarrow> True
kuncar@53012
    23
    | (Some x, Some y) \<Rightarrow> R x y
kuncar@53012
    24
    | _ \<Rightarrow> False)"
kuncar@53012
    25
  by (cases x) (cases y, simp_all)+
kuncar@53012
    26
kuncar@53012
    27
fun option_pred :: "('a \<Rightarrow> bool) \<Rightarrow> 'a option \<Rightarrow> bool"
kuncar@53012
    28
where
kuncar@53012
    29
  "option_pred R None = True"
kuncar@53012
    30
| "option_pred R (Some x) = R x"
kuncar@53012
    31
kuncar@53012
    32
lemma option_pred_unfold:
kuncar@53012
    33
  "option_pred P x = (case x of None \<Rightarrow> True
kuncar@53012
    34
    | Some x \<Rightarrow> P x)"
kuncar@53012
    35
by (cases x) simp_all
kuncar@53012
    36
kuncar@53012
    37
lemma option_rel_eq [relator_eq]:
kuncar@53012
    38
  "option_rel (op =) = (op =)"
kuncar@53012
    39
  by (simp add: option_rel_unfold fun_eq_iff split: option.split)
kuncar@53012
    40
kuncar@53012
    41
lemma option_rel_mono[relator_mono]:
kuncar@53012
    42
  assumes "A \<le> B"
kuncar@53012
    43
  shows "(option_rel A) \<le> (option_rel B)"
kuncar@53012
    44
using assms by (auto simp: option_rel_unfold split: option.splits)
kuncar@53012
    45
kuncar@53012
    46
lemma option_rel_OO[relator_distr]:
kuncar@53012
    47
  "(option_rel A) OO (option_rel B) = option_rel (A OO B)"
kuncar@53012
    48
by (rule ext)+ (auto simp: option_rel_unfold OO_def split: option.split)
kuncar@53012
    49
kuncar@53012
    50
lemma Domainp_option[relator_domain]:
kuncar@53012
    51
  assumes "Domainp A = P"
kuncar@53012
    52
  shows "Domainp (option_rel A) = (option_pred P)"
kuncar@53012
    53
using assms unfolding Domainp_iff[abs_def] option_rel_unfold[abs_def] option_pred_unfold[abs_def]
kuncar@53012
    54
by (auto iff: fun_eq_iff split: option.split)
kuncar@53012
    55
kuncar@53012
    56
lemma reflp_option_rel[reflexivity_rule]:
kuncar@53012
    57
  "reflp R \<Longrightarrow> reflp (option_rel R)"
kuncar@53012
    58
  unfolding reflp_def split_option_all by simp
kuncar@53012
    59
kuncar@53012
    60
lemma left_total_option_rel[reflexivity_rule]:
kuncar@53012
    61
  "left_total R \<Longrightarrow> left_total (option_rel R)"
kuncar@53012
    62
  unfolding left_total_def split_option_all split_option_ex by simp
kuncar@53012
    63
kuncar@53012
    64
lemma left_unique_option_rel [reflexivity_rule]:
kuncar@53012
    65
  "left_unique R \<Longrightarrow> left_unique (option_rel R)"
kuncar@53012
    66
  unfolding left_unique_def split_option_all by simp
kuncar@53012
    67
kuncar@53012
    68
lemma right_total_option_rel [transfer_rule]:
kuncar@53012
    69
  "right_total R \<Longrightarrow> right_total (option_rel R)"
kuncar@53012
    70
  unfolding right_total_def split_option_all split_option_ex by simp
kuncar@53012
    71
kuncar@53012
    72
lemma right_unique_option_rel [transfer_rule]:
kuncar@53012
    73
  "right_unique R \<Longrightarrow> right_unique (option_rel R)"
kuncar@53012
    74
  unfolding right_unique_def split_option_all by simp
kuncar@53012
    75
kuncar@53012
    76
lemma bi_total_option_rel [transfer_rule]:
kuncar@53012
    77
  "bi_total R \<Longrightarrow> bi_total (option_rel R)"
kuncar@53012
    78
  unfolding bi_total_def split_option_all split_option_ex by simp
kuncar@53012
    79
kuncar@53012
    80
lemma bi_unique_option_rel [transfer_rule]:
kuncar@53012
    81
  "bi_unique R \<Longrightarrow> bi_unique (option_rel R)"
kuncar@53012
    82
  unfolding bi_unique_def split_option_all by simp
kuncar@53012
    83
kuncar@53012
    84
lemma option_invariant_commute [invariant_commute]:
kuncar@53012
    85
  "option_rel (Lifting.invariant P) = Lifting.invariant (option_pred P)"
kuncar@53012
    86
  by (auto simp add: fun_eq_iff Lifting.invariant_def split_option_all)
kuncar@53012
    87
kuncar@53012
    88
subsection {* Quotient theorem for the Lifting package *}
kuncar@53012
    89
kuncar@53012
    90
lemma Quotient_option[quot_map]:
kuncar@53012
    91
  assumes "Quotient R Abs Rep T"
kuncar@53012
    92
  shows "Quotient (option_rel R) (Option.map Abs)
kuncar@53012
    93
    (Option.map Rep) (option_rel T)"
kuncar@53012
    94
  using assms unfolding Quotient_alt_def option_rel_unfold
kuncar@53012
    95
  by (simp split: option.split)
kuncar@53012
    96
kuncar@53012
    97
subsection {* Transfer rules for the Transfer package *}
kuncar@53012
    98
kuncar@53012
    99
context
kuncar@53012
   100
begin
kuncar@53012
   101
interpretation lifting_syntax .
kuncar@53012
   102
kuncar@53012
   103
lemma None_transfer [transfer_rule]: "(option_rel A) None None"
kuncar@53012
   104
  by simp
kuncar@53012
   105
kuncar@53012
   106
lemma Some_transfer [transfer_rule]: "(A ===> option_rel A) Some Some"
kuncar@53012
   107
  unfolding fun_rel_def by simp
kuncar@53012
   108
kuncar@53012
   109
lemma option_case_transfer [transfer_rule]:
kuncar@53012
   110
  "(B ===> (A ===> B) ===> option_rel A ===> B) option_case option_case"
kuncar@53012
   111
  unfolding fun_rel_def split_option_all by simp
kuncar@53012
   112
kuncar@53012
   113
lemma option_map_transfer [transfer_rule]:
kuncar@53012
   114
  "((A ===> B) ===> option_rel A ===> option_rel B) Option.map Option.map"
kuncar@53012
   115
  unfolding Option.map_def by transfer_prover
kuncar@53012
   116
kuncar@53012
   117
lemma option_bind_transfer [transfer_rule]:
kuncar@53012
   118
  "(option_rel A ===> (A ===> option_rel B) ===> option_rel B)
kuncar@53012
   119
    Option.bind Option.bind"
kuncar@53012
   120
  unfolding fun_rel_def split_option_all by simp
kuncar@53012
   121
kuncar@53012
   122
end
kuncar@53012
   123
kuncar@53012
   124
end
kuncar@53012
   125