src/HOL/Library/Quotient_Sum.thy
author kuncar
Mon May 13 13:59:04 2013 +0200 (2013-05-13)
changeset 51956 a4d81cdebf8b
parent 51377 7da251a6c16e
child 51994 82cc2aeb7d13
permissions -rw-r--r--
better support for domains in Lifting/Transfer = replace Domainp T by the actual invariant in a transferred goal
wenzelm@47455
     1
(*  Title:      HOL/Library/Quotient_Sum.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 sum type *}
wenzelm@35788
     6
kaliszyk@35222
     7
theory Quotient_Sum
kaliszyk@35222
     8
imports Main Quotient_Syntax
kaliszyk@35222
     9
begin
kaliszyk@35222
    10
huffman@47624
    11
subsection {* Relator for sum type *}
huffman@47624
    12
kaliszyk@35222
    13
fun
haftmann@40542
    14
  sum_rel :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> 'c + 'd \<Rightarrow> bool"
kaliszyk@35222
    15
where
kaliszyk@35222
    16
  "sum_rel R1 R2 (Inl a1) (Inl b1) = R1 a1 b1"
kaliszyk@35222
    17
| "sum_rel R1 R2 (Inl a1) (Inr b2) = False"
kaliszyk@35222
    18
| "sum_rel R1 R2 (Inr a2) (Inl b1) = False"
kaliszyk@35222
    19
| "sum_rel R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
kaliszyk@35222
    20
haftmann@40820
    21
lemma sum_rel_unfold:
haftmann@40820
    22
  "sum_rel R1 R2 x y = (case (x, y) of (Inl x, Inl y) \<Rightarrow> R1 x y
haftmann@40820
    23
    | (Inr x, Inr y) \<Rightarrow> R2 x y
haftmann@40820
    24
    | _ \<Rightarrow> False)"
haftmann@40820
    25
  by (cases x) (cases y, simp_all)+
kaliszyk@35222
    26
kuncar@51956
    27
fun sum_pred :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
kuncar@51956
    28
where
kuncar@51956
    29
  "sum_pred P1 P2 (Inl a) = P1 a"
kuncar@51956
    30
| "sum_pred P1 P2 (Inr a) = P2 a"
kuncar@51956
    31
kuncar@51956
    32
lemma sum_pred_unfold:
kuncar@51956
    33
  "sum_pred P1 P2 x = (case x of Inl x \<Rightarrow> P1 x
kuncar@51956
    34
    | Inr x \<Rightarrow> P2 x)"
kuncar@51956
    35
by (cases x) simp_all
kuncar@51956
    36
haftmann@40820
    37
lemma sum_rel_map1:
haftmann@40820
    38
  "sum_rel R1 R2 (sum_map f1 f2 x) y \<longleftrightarrow> sum_rel (\<lambda>x. R1 (f1 x)) (\<lambda>x. R2 (f2 x)) x y"
haftmann@40820
    39
  by (simp add: sum_rel_unfold split: sum.split)
haftmann@40820
    40
haftmann@40820
    41
lemma sum_rel_map2:
haftmann@40820
    42
  "sum_rel R1 R2 x (sum_map f1 f2 y) \<longleftrightarrow> sum_rel (\<lambda>x y. R1 x (f1 y)) (\<lambda>x y. R2 x (f2 y)) x y"
haftmann@40820
    43
  by (simp add: sum_rel_unfold split: sum.split)
haftmann@40820
    44
haftmann@40820
    45
lemma sum_map_id [id_simps]:
haftmann@40820
    46
  "sum_map id id = id"
haftmann@40820
    47
  by (simp add: id_def sum_map.identity fun_eq_iff)
kaliszyk@35222
    48
huffman@47624
    49
lemma sum_rel_eq [id_simps, relator_eq]:
haftmann@40820
    50
  "sum_rel (op =) (op =) = (op =)"
haftmann@40820
    51
  by (simp add: sum_rel_unfold fun_eq_iff split: sum.split)
haftmann@40820
    52
huffman@47624
    53
lemma split_sum_all: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (Inl x)) \<and> (\<forall>x. P (Inr x))"
huffman@47624
    54
  by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
huffman@47624
    55
huffman@47624
    56
lemma split_sum_ex: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P (Inl x)) \<or> (\<exists>x. P (Inr x))"
huffman@47624
    57
  by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
huffman@47624
    58
kuncar@51377
    59
lemma sum_rel_mono[relator_mono]:
kuncar@51377
    60
  assumes "A \<le> C"
kuncar@51377
    61
  assumes "B \<le> D"
kuncar@51377
    62
  shows "(sum_rel A B) \<le> (sum_rel C D)"
kuncar@51377
    63
using assms by (auto simp: sum_rel_unfold split: sum.splits)
kuncar@51377
    64
kuncar@51377
    65
lemma sum_rel_OO[relator_distr]:
kuncar@51377
    66
  "(sum_rel A B) OO (sum_rel C D) = sum_rel (A OO C) (B OO D)"
kuncar@51377
    67
by (rule ext)+ (auto simp add: sum_rel_unfold OO_def split_sum_ex split: sum.split)
kuncar@51377
    68
kuncar@51956
    69
lemma Domainp_sum[relator_domain]:
kuncar@51956
    70
  assumes "Domainp R1 = P1"
kuncar@51956
    71
  assumes "Domainp R2 = P2"
kuncar@51956
    72
  shows "Domainp (sum_rel R1 R2) = (sum_pred P1 P2)"
kuncar@51956
    73
using assms
kuncar@51956
    74
by (auto simp add: Domainp_iff split_sum_ex sum_pred_unfold iff: fun_eq_iff split: sum.split)
kuncar@51956
    75
kuncar@47982
    76
lemma sum_reflp[reflexivity_rule]:
haftmann@40820
    77
  "reflp R1 \<Longrightarrow> reflp R2 \<Longrightarrow> reflp (sum_rel R1 R2)"
huffman@47624
    78
  unfolding reflp_def split_sum_all sum_rel.simps by fast
kaliszyk@35222
    79
kuncar@47982
    80
lemma sum_left_total[reflexivity_rule]:
kuncar@47982
    81
  "left_total R1 \<Longrightarrow> left_total R2 \<Longrightarrow> left_total (sum_rel R1 R2)"
kuncar@47982
    82
  apply (intro left_totalI)
kuncar@47982
    83
  unfolding split_sum_ex 
kuncar@47982
    84
  by (case_tac x) (auto elim: left_totalE)
kuncar@47982
    85
haftmann@40820
    86
lemma sum_symp:
haftmann@40820
    87
  "symp R1 \<Longrightarrow> symp R2 \<Longrightarrow> symp (sum_rel R1 R2)"
huffman@47624
    88
  unfolding symp_def split_sum_all sum_rel.simps by fast
haftmann@40820
    89
haftmann@40820
    90
lemma sum_transp:
haftmann@40820
    91
  "transp R1 \<Longrightarrow> transp R2 \<Longrightarrow> transp (sum_rel R1 R2)"
huffman@47624
    92
  unfolding transp_def split_sum_all sum_rel.simps by fast
haftmann@40820
    93
haftmann@40820
    94
lemma sum_equivp [quot_equiv]:
haftmann@40820
    95
  "equivp R1 \<Longrightarrow> equivp R2 \<Longrightarrow> equivp (sum_rel R1 R2)"
haftmann@40820
    96
  by (blast intro: equivpI sum_reflp sum_symp sum_transp elim: equivpE)
huffman@47624
    97
huffman@47624
    98
lemma right_total_sum_rel [transfer_rule]:
huffman@47624
    99
  "right_total R1 \<Longrightarrow> right_total R2 \<Longrightarrow> right_total (sum_rel R1 R2)"
huffman@47624
   100
  unfolding right_total_def split_sum_all split_sum_ex by simp
huffman@47624
   101
huffman@47624
   102
lemma right_unique_sum_rel [transfer_rule]:
huffman@47624
   103
  "right_unique R1 \<Longrightarrow> right_unique R2 \<Longrightarrow> right_unique (sum_rel R1 R2)"
huffman@47624
   104
  unfolding right_unique_def split_sum_all by simp
huffman@47624
   105
huffman@47624
   106
lemma bi_total_sum_rel [transfer_rule]:
huffman@47624
   107
  "bi_total R1 \<Longrightarrow> bi_total R2 \<Longrightarrow> bi_total (sum_rel R1 R2)"
huffman@47624
   108
  using assms unfolding bi_total_def split_sum_all split_sum_ex by simp
huffman@47624
   109
huffman@47624
   110
lemma bi_unique_sum_rel [transfer_rule]:
huffman@47624
   111
  "bi_unique R1 \<Longrightarrow> bi_unique R2 \<Longrightarrow> bi_unique (sum_rel R1 R2)"
huffman@47624
   112
  using assms unfolding bi_unique_def split_sum_all by simp
huffman@47624
   113
huffman@47635
   114
subsection {* Transfer rules for transfer package *}
huffman@47624
   115
huffman@47624
   116
lemma Inl_transfer [transfer_rule]: "(A ===> sum_rel A B) Inl Inl"
huffman@47624
   117
  unfolding fun_rel_def by simp
huffman@47624
   118
huffman@47624
   119
lemma Inr_transfer [transfer_rule]: "(B ===> sum_rel A B) Inr Inr"
huffman@47624
   120
  unfolding fun_rel_def by simp
huffman@47624
   121
huffman@47624
   122
lemma sum_case_transfer [transfer_rule]:
huffman@47624
   123
  "((A ===> C) ===> (B ===> C) ===> sum_rel A B ===> C) sum_case sum_case"
huffman@47624
   124
  unfolding fun_rel_def sum_rel_unfold by (simp split: sum.split)
huffman@47624
   125
huffman@47624
   126
subsection {* Setup for lifting package *}
huffman@47624
   127
kuncar@47777
   128
lemma Quotient_sum[quot_map]:
huffman@47624
   129
  assumes "Quotient R1 Abs1 Rep1 T1"
huffman@47624
   130
  assumes "Quotient R2 Abs2 Rep2 T2"
huffman@47624
   131
  shows "Quotient (sum_rel R1 R2) (sum_map Abs1 Abs2)
huffman@47624
   132
    (sum_map Rep1 Rep2) (sum_rel T1 T2)"
huffman@47624
   133
  using assms unfolding Quotient_alt_def
huffman@47624
   134
  by (simp add: split_sum_all)
huffman@47624
   135
kuncar@47634
   136
lemma sum_invariant_commute [invariant_commute]: 
kuncar@47634
   137
  "sum_rel (Lifting.invariant P1) (Lifting.invariant P2) = Lifting.invariant (sum_pred P1 P2)"
kuncar@51956
   138
  by (auto simp add: fun_eq_iff Lifting.invariant_def sum_rel_unfold sum_pred_unfold split: sum.split)
kuncar@47634
   139
huffman@47624
   140
subsection {* Rules for quotient package *}
huffman@47624
   141
haftmann@40820
   142
lemma sum_quotient [quot_thm]:
kuncar@47308
   143
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   144
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kuncar@47308
   145
  shows "Quotient3 (sum_rel R1 R2) (sum_map Abs1 Abs2) (sum_map Rep1 Rep2)"
kuncar@47308
   146
  apply (rule Quotient3I)
haftmann@41372
   147
  apply (simp_all add: sum_map.compositionality comp_def sum_map.identity sum_rel_eq sum_rel_map1 sum_rel_map2
kuncar@47308
   148
    Quotient3_abs_rep [OF q1] Quotient3_rel_rep [OF q1] Quotient3_abs_rep [OF q2] Quotient3_rel_rep [OF q2])
kuncar@47308
   149
  using Quotient3_rel [OF q1] Quotient3_rel [OF q2]
haftmann@41372
   150
  apply (simp add: sum_rel_unfold comp_def split: sum.split)
kaliszyk@35222
   151
  done
kaliszyk@35222
   152
kuncar@47308
   153
declare [[mapQ3 sum = (sum_rel, sum_quotient)]]
kuncar@47094
   154
haftmann@40820
   155
lemma sum_Inl_rsp [quot_respect]:
kuncar@47308
   156
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   157
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   158
  shows "(R1 ===> sum_rel R1 R2) Inl Inl"
haftmann@40465
   159
  by auto
kaliszyk@35222
   160
haftmann@40820
   161
lemma sum_Inr_rsp [quot_respect]:
kuncar@47308
   162
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   163
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   164
  shows "(R2 ===> sum_rel R1 R2) Inr Inr"
haftmann@40465
   165
  by auto
kaliszyk@35222
   166
haftmann@40820
   167
lemma sum_Inl_prs [quot_preserve]:
kuncar@47308
   168
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   169
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   170
  shows "(Rep1 ---> sum_map Abs1 Abs2) Inl = Inl"
nipkow@39302
   171
  apply(simp add: fun_eq_iff)
kuncar@47308
   172
  apply(simp add: Quotient3_abs_rep[OF q1])
kaliszyk@35222
   173
  done
kaliszyk@35222
   174
haftmann@40820
   175
lemma sum_Inr_prs [quot_preserve]:
kuncar@47308
   176
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   177
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   178
  shows "(Rep2 ---> sum_map Abs1 Abs2) Inr = Inr"
nipkow@39302
   179
  apply(simp add: fun_eq_iff)
kuncar@47308
   180
  apply(simp add: Quotient3_abs_rep[OF q2])
kaliszyk@35222
   181
  done
kaliszyk@35222
   182
kaliszyk@35222
   183
end