src/HOL/Lifting_Option.thy
author blanchet
Wed Feb 12 08:35:57 2014 +0100 (2014-02-12)
changeset 55415 05f5fdb8d093
parent 55404 5cb95b79a51f
child 55466 786edc984c98
permissions -rw-r--r--
renamed 'nat_{case,rec}' to '{case,rec}_nat'
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
blanchet@55404
    29
  "option_pred \<equiv> case_option 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
blanchet@55404
   103
lemma case_option_transfer [transfer_rule]:
blanchet@55404
   104
  "(B ===> (A ===> B) ===> option_rel A ===> B) case_option case_option"
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
kuncar@53012
   118
end