src/HOL/Library/Quotient_Sum.thy
author kuncar
Fri Apr 20 18:29:21 2012 +0200 (2012-04-20)
changeset 47634 091bcd569441
parent 47624 16d433895d2e
child 47635 ebb79474262c
permissions -rw-r--r--
hide the invariant constant for relators: invariant_commute infrastracture
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
haftmann@40820
    27
lemma sum_rel_map1:
haftmann@40820
    28
  "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
    29
  by (simp add: sum_rel_unfold split: sum.split)
haftmann@40820
    30
haftmann@40820
    31
lemma sum_rel_map2:
haftmann@40820
    32
  "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
    33
  by (simp add: sum_rel_unfold split: sum.split)
haftmann@40820
    34
haftmann@40820
    35
lemma sum_map_id [id_simps]:
haftmann@40820
    36
  "sum_map id id = id"
haftmann@40820
    37
  by (simp add: id_def sum_map.identity fun_eq_iff)
kaliszyk@35222
    38
huffman@47624
    39
lemma sum_rel_eq [id_simps, relator_eq]:
haftmann@40820
    40
  "sum_rel (op =) (op =) = (op =)"
haftmann@40820
    41
  by (simp add: sum_rel_unfold fun_eq_iff split: sum.split)
haftmann@40820
    42
huffman@47624
    43
lemma split_sum_all: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (Inl x)) \<and> (\<forall>x. P (Inr x))"
huffman@47624
    44
  by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
huffman@47624
    45
huffman@47624
    46
lemma split_sum_ex: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P (Inl x)) \<or> (\<exists>x. P (Inr x))"
huffman@47624
    47
  by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
huffman@47624
    48
haftmann@40820
    49
lemma sum_reflp:
haftmann@40820
    50
  "reflp R1 \<Longrightarrow> reflp R2 \<Longrightarrow> reflp (sum_rel R1 R2)"
huffman@47624
    51
  unfolding reflp_def split_sum_all sum_rel.simps by fast
kaliszyk@35222
    52
haftmann@40820
    53
lemma sum_symp:
haftmann@40820
    54
  "symp R1 \<Longrightarrow> symp R2 \<Longrightarrow> symp (sum_rel R1 R2)"
huffman@47624
    55
  unfolding symp_def split_sum_all sum_rel.simps by fast
haftmann@40820
    56
haftmann@40820
    57
lemma sum_transp:
haftmann@40820
    58
  "transp R1 \<Longrightarrow> transp R2 \<Longrightarrow> transp (sum_rel R1 R2)"
huffman@47624
    59
  unfolding transp_def split_sum_all sum_rel.simps by fast
haftmann@40820
    60
haftmann@40820
    61
lemma sum_equivp [quot_equiv]:
haftmann@40820
    62
  "equivp R1 \<Longrightarrow> equivp R2 \<Longrightarrow> equivp (sum_rel R1 R2)"
haftmann@40820
    63
  by (blast intro: equivpI sum_reflp sum_symp sum_transp elim: equivpE)
huffman@47624
    64
huffman@47624
    65
lemma right_total_sum_rel [transfer_rule]:
huffman@47624
    66
  "right_total R1 \<Longrightarrow> right_total R2 \<Longrightarrow> right_total (sum_rel R1 R2)"
huffman@47624
    67
  unfolding right_total_def split_sum_all split_sum_ex by simp
huffman@47624
    68
huffman@47624
    69
lemma right_unique_sum_rel [transfer_rule]:
huffman@47624
    70
  "right_unique R1 \<Longrightarrow> right_unique R2 \<Longrightarrow> right_unique (sum_rel R1 R2)"
huffman@47624
    71
  unfolding right_unique_def split_sum_all by simp
huffman@47624
    72
huffman@47624
    73
lemma bi_total_sum_rel [transfer_rule]:
huffman@47624
    74
  "bi_total R1 \<Longrightarrow> bi_total R2 \<Longrightarrow> bi_total (sum_rel R1 R2)"
huffman@47624
    75
  using assms unfolding bi_total_def split_sum_all split_sum_ex by simp
huffman@47624
    76
huffman@47624
    77
lemma bi_unique_sum_rel [transfer_rule]:
huffman@47624
    78
  "bi_unique R1 \<Longrightarrow> bi_unique R2 \<Longrightarrow> bi_unique (sum_rel R1 R2)"
huffman@47624
    79
  using assms unfolding bi_unique_def split_sum_all by simp
huffman@47624
    80
huffman@47624
    81
subsection {* Correspondence rules for transfer package *}
huffman@47624
    82
huffman@47624
    83
lemma Inl_transfer [transfer_rule]: "(A ===> sum_rel A B) Inl Inl"
huffman@47624
    84
  unfolding fun_rel_def by simp
huffman@47624
    85
huffman@47624
    86
lemma Inr_transfer [transfer_rule]: "(B ===> sum_rel A B) Inr Inr"
huffman@47624
    87
  unfolding fun_rel_def by simp
huffman@47624
    88
huffman@47624
    89
lemma sum_case_transfer [transfer_rule]:
huffman@47624
    90
  "((A ===> C) ===> (B ===> C) ===> sum_rel A B ===> C) sum_case sum_case"
huffman@47624
    91
  unfolding fun_rel_def sum_rel_unfold by (simp split: sum.split)
huffman@47624
    92
huffman@47624
    93
subsection {* Setup for lifting package *}
huffman@47624
    94
huffman@47624
    95
lemma Quotient_sum:
huffman@47624
    96
  assumes "Quotient R1 Abs1 Rep1 T1"
huffman@47624
    97
  assumes "Quotient R2 Abs2 Rep2 T2"
huffman@47624
    98
  shows "Quotient (sum_rel R1 R2) (sum_map Abs1 Abs2)
huffman@47624
    99
    (sum_map Rep1 Rep2) (sum_rel T1 T2)"
huffman@47624
   100
  using assms unfolding Quotient_alt_def
huffman@47624
   101
  by (simp add: split_sum_all)
huffman@47624
   102
huffman@47624
   103
declare [[map sum = (sum_rel, Quotient_sum)]]
huffman@47624
   104
kuncar@47634
   105
fun sum_pred :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
kuncar@47634
   106
where
kuncar@47634
   107
  "sum_pred R1 R2 (Inl a) = R1 a"
kuncar@47634
   108
| "sum_pred R1 R2 (Inr a) = R2 a"
kuncar@47634
   109
kuncar@47634
   110
lemma sum_invariant_commute [invariant_commute]: 
kuncar@47634
   111
  "sum_rel (Lifting.invariant P1) (Lifting.invariant P2) = Lifting.invariant (sum_pred P1 P2)"
kuncar@47634
   112
  apply (simp add: fun_eq_iff Lifting.invariant_def)
kuncar@47634
   113
  apply (intro allI) 
kuncar@47634
   114
  apply (case_tac x rule: sum.exhaust)
kuncar@47634
   115
  apply (case_tac xa rule: sum.exhaust)
kuncar@47634
   116
  apply auto[2]
kuncar@47634
   117
  apply (case_tac xa rule: sum.exhaust)
kuncar@47634
   118
  apply auto
kuncar@47634
   119
done
kuncar@47634
   120
huffman@47624
   121
subsection {* Rules for quotient package *}
huffman@47624
   122
haftmann@40820
   123
lemma sum_quotient [quot_thm]:
kuncar@47308
   124
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   125
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kuncar@47308
   126
  shows "Quotient3 (sum_rel R1 R2) (sum_map Abs1 Abs2) (sum_map Rep1 Rep2)"
kuncar@47308
   127
  apply (rule Quotient3I)
haftmann@41372
   128
  apply (simp_all add: sum_map.compositionality comp_def sum_map.identity sum_rel_eq sum_rel_map1 sum_rel_map2
kuncar@47308
   129
    Quotient3_abs_rep [OF q1] Quotient3_rel_rep [OF q1] Quotient3_abs_rep [OF q2] Quotient3_rel_rep [OF q2])
kuncar@47308
   130
  using Quotient3_rel [OF q1] Quotient3_rel [OF q2]
haftmann@41372
   131
  apply (simp add: sum_rel_unfold comp_def split: sum.split)
kaliszyk@35222
   132
  done
kaliszyk@35222
   133
kuncar@47308
   134
declare [[mapQ3 sum = (sum_rel, sum_quotient)]]
kuncar@47094
   135
haftmann@40820
   136
lemma sum_Inl_rsp [quot_respect]:
kuncar@47308
   137
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   138
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   139
  shows "(R1 ===> sum_rel R1 R2) Inl Inl"
haftmann@40465
   140
  by auto
kaliszyk@35222
   141
haftmann@40820
   142
lemma sum_Inr_rsp [quot_respect]:
kuncar@47308
   143
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   144
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   145
  shows "(R2 ===> sum_rel R1 R2) Inr Inr"
haftmann@40465
   146
  by auto
kaliszyk@35222
   147
haftmann@40820
   148
lemma sum_Inl_prs [quot_preserve]:
kuncar@47308
   149
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   150
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   151
  shows "(Rep1 ---> sum_map Abs1 Abs2) Inl = Inl"
nipkow@39302
   152
  apply(simp add: fun_eq_iff)
kuncar@47308
   153
  apply(simp add: Quotient3_abs_rep[OF q1])
kaliszyk@35222
   154
  done
kaliszyk@35222
   155
haftmann@40820
   156
lemma sum_Inr_prs [quot_preserve]:
kuncar@47308
   157
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   158
  assumes q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@35222
   159
  shows "(Rep2 ---> sum_map Abs1 Abs2) Inr = Inr"
nipkow@39302
   160
  apply(simp add: fun_eq_iff)
kuncar@47308
   161
  apply(simp add: Quotient3_abs_rep[OF q2])
kaliszyk@35222
   162
  done
kaliszyk@35222
   163
kaliszyk@35222
   164
end