1(*  Title:      HOL/HOLCF/Ssum.thy
2    Author:     Franz Regensburger
3    Author:     Brian Huffman
4*)
5
6section \<open>The type of strict sums\<close>
7
8theory Ssum
9  imports Tr
10begin
11
12default_sort pcpo
13
14
15subsection \<open>Definition of strict sum type\<close>
16
17definition "ssum =
18  {p :: tr \<times> ('a \<times> 'b). p = \<bottom> \<or>
19    (fst p = TT \<and> fst (snd p) \<noteq> \<bottom> \<and> snd (snd p) = \<bottom>) \<or>
20    (fst p = FF \<and> fst (snd p) = \<bottom> \<and> snd (snd p) \<noteq> \<bottom>)}"
21
22pcpodef ('a, 'b) ssum  ("(_ \<oplus>/ _)" [21, 20] 20) = "ssum :: (tr \<times> 'a \<times> 'b) set"
23  by (simp_all add: ssum_def)
24
25instance ssum :: ("{chfin,pcpo}", "{chfin,pcpo}") chfin
26  by (rule typedef_chfin [OF type_definition_ssum below_ssum_def])
27
28type_notation (ASCII)
29  ssum  (infixr "++" 10)
30
31
32subsection \<open>Definitions of constructors\<close>
33
34definition sinl :: "'a \<rightarrow> ('a ++ 'b)"
35  where "sinl = (\<Lambda> a. Abs_ssum (seq\<cdot>a\<cdot>TT, a, \<bottom>))"
36
37definition sinr :: "'b \<rightarrow> ('a ++ 'b)"
38  where "sinr = (\<Lambda> b. Abs_ssum (seq\<cdot>b\<cdot>FF, \<bottom>, b))"
39
40lemma sinl_ssum: "(seq\<cdot>a\<cdot>TT, a, \<bottom>) \<in> ssum"
41  by (simp add: ssum_def seq_conv_if)
42
43lemma sinr_ssum: "(seq\<cdot>b\<cdot>FF, \<bottom>, b) \<in> ssum"
44  by (simp add: ssum_def seq_conv_if)
45
46lemma Rep_ssum_sinl: "Rep_ssum (sinl\<cdot>a) = (seq\<cdot>a\<cdot>TT, a, \<bottom>)"
47  by (simp add: sinl_def cont_Abs_ssum Abs_ssum_inverse sinl_ssum)
48
49lemma Rep_ssum_sinr: "Rep_ssum (sinr\<cdot>b) = (seq\<cdot>b\<cdot>FF, \<bottom>, b)"
50  by (simp add: sinr_def cont_Abs_ssum Abs_ssum_inverse sinr_ssum)
51
52lemmas Rep_ssum_simps =
53  Rep_ssum_inject [symmetric] below_ssum_def
54  prod_eq_iff below_prod_def
55  Rep_ssum_strict Rep_ssum_sinl Rep_ssum_sinr
56
57
58subsection \<open>Properties of \emph{sinl} and \emph{sinr}\<close>
59
60text \<open>Ordering\<close>
61
62lemma sinl_below [simp]: "sinl\<cdot>x \<sqsubseteq> sinl\<cdot>y \<longleftrightarrow> x \<sqsubseteq> y"
63  by (simp add: Rep_ssum_simps seq_conv_if)
64
65lemma sinr_below [simp]: "sinr\<cdot>x \<sqsubseteq> sinr\<cdot>y \<longleftrightarrow> x \<sqsubseteq> y"
66  by (simp add: Rep_ssum_simps seq_conv_if)
67
68lemma sinl_below_sinr [simp]: "sinl\<cdot>x \<sqsubseteq> sinr\<cdot>y \<longleftrightarrow> x = \<bottom>"
69  by (simp add: Rep_ssum_simps seq_conv_if)
70
71lemma sinr_below_sinl [simp]: "sinr\<cdot>x \<sqsubseteq> sinl\<cdot>y \<longleftrightarrow> x = \<bottom>"
72  by (simp add: Rep_ssum_simps seq_conv_if)
73
74text \<open>Equality\<close>
75
76lemma sinl_eq [simp]: "sinl\<cdot>x = sinl\<cdot>y \<longleftrightarrow> x = y"
77  by (simp add: po_eq_conv)
78
79lemma sinr_eq [simp]: "sinr\<cdot>x = sinr\<cdot>y \<longleftrightarrow> x = y"
80  by (simp add: po_eq_conv)
81
82lemma sinl_eq_sinr [simp]: "sinl\<cdot>x = sinr\<cdot>y \<longleftrightarrow> x = \<bottom> \<and> y = \<bottom>"
83  by (subst po_eq_conv) simp
84
85lemma sinr_eq_sinl [simp]: "sinr\<cdot>x = sinl\<cdot>y \<longleftrightarrow> x = \<bottom> \<and> y = \<bottom>"
86  by (subst po_eq_conv) simp
87
88lemma sinl_inject: "sinl\<cdot>x = sinl\<cdot>y \<Longrightarrow> x = y"
89  by (rule sinl_eq [THEN iffD1])
90
91lemma sinr_inject: "sinr\<cdot>x = sinr\<cdot>y \<Longrightarrow> x = y"
92  by (rule sinr_eq [THEN iffD1])
93
94text \<open>Strictness\<close>
95
96lemma sinl_strict [simp]: "sinl\<cdot>\<bottom> = \<bottom>"
97  by (simp add: Rep_ssum_simps)
98
99lemma sinr_strict [simp]: "sinr\<cdot>\<bottom> = \<bottom>"
100  by (simp add: Rep_ssum_simps)
101
102lemma sinl_bottom_iff [simp]: "sinl\<cdot>x = \<bottom> \<longleftrightarrow> x = \<bottom>"
103  using sinl_eq [of "x" "\<bottom>"] by simp
104
105lemma sinr_bottom_iff [simp]: "sinr\<cdot>x = \<bottom> \<longleftrightarrow> x = \<bottom>"
106  using sinr_eq [of "x" "\<bottom>"] by simp
107
108lemma sinl_defined: "x \<noteq> \<bottom> \<Longrightarrow> sinl\<cdot>x \<noteq> \<bottom>"
109  by simp
110
111lemma sinr_defined: "x \<noteq> \<bottom> \<Longrightarrow> sinr\<cdot>x \<noteq> \<bottom>"
112  by simp
113
114text \<open>Compactness\<close>
115
116lemma compact_sinl: "compact x \<Longrightarrow> compact (sinl\<cdot>x)"
117  by (rule compact_ssum) (simp add: Rep_ssum_sinl)
118
119lemma compact_sinr: "compact x \<Longrightarrow> compact (sinr\<cdot>x)"
120  by (rule compact_ssum) (simp add: Rep_ssum_sinr)
121
122lemma compact_sinlD: "compact (sinl\<cdot>x) \<Longrightarrow> compact x"
123  unfolding compact_def
124  by (drule adm_subst [OF cont_Rep_cfun2 [where f=sinl]], simp)
125
126lemma compact_sinrD: "compact (sinr\<cdot>x) \<Longrightarrow> compact x"
127  unfolding compact_def
128  by (drule adm_subst [OF cont_Rep_cfun2 [where f=sinr]], simp)
129
130lemma compact_sinl_iff [simp]: "compact (sinl\<cdot>x) = compact x"
131  by (safe elim!: compact_sinl compact_sinlD)
132
133lemma compact_sinr_iff [simp]: "compact (sinr\<cdot>x) = compact x"
134  by (safe elim!: compact_sinr compact_sinrD)
135
136
137subsection \<open>Case analysis\<close>
138
139lemma ssumE [case_names bottom sinl sinr, cases type: ssum]:
140  obtains "p = \<bottom>"
141  | x where "p = sinl\<cdot>x" and "x \<noteq> \<bottom>"
142  | y where "p = sinr\<cdot>y" and "y \<noteq> \<bottom>"
143  using Rep_ssum [of p] by (auto simp add: ssum_def Rep_ssum_simps)
144
145lemma ssum_induct [case_names bottom sinl sinr, induct type: ssum]:
146  "\<lbrakk>P \<bottom>;
147   \<And>x. x \<noteq> \<bottom> \<Longrightarrow> P (sinl\<cdot>x);
148   \<And>y. y \<noteq> \<bottom> \<Longrightarrow> P (sinr\<cdot>y)\<rbrakk> \<Longrightarrow> P x"
149  by (cases x) simp_all
150
151lemma ssumE2 [case_names sinl sinr]:
152  "\<lbrakk>\<And>x. p = sinl\<cdot>x \<Longrightarrow> Q; \<And>y. p = sinr\<cdot>y \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
153  by (cases p, simp only: sinl_strict [symmetric], simp, simp)
154
155lemma below_sinlD: "p \<sqsubseteq> sinl\<cdot>x \<Longrightarrow> \<exists>y. p = sinl\<cdot>y \<and> y \<sqsubseteq> x"
156  by (cases p, rule_tac x="\<bottom>" in exI, simp_all)
157
158lemma below_sinrD: "p \<sqsubseteq> sinr\<cdot>x \<Longrightarrow> \<exists>y. p = sinr\<cdot>y \<and> y \<sqsubseteq> x"
159  by (cases p, rule_tac x="\<bottom>" in exI, simp_all)
160
161
162subsection \<open>Case analysis combinator\<close>
163
164definition sscase :: "('a \<rightarrow> 'c) \<rightarrow> ('b \<rightarrow> 'c) \<rightarrow> ('a ++ 'b) \<rightarrow> 'c"
165  where "sscase = (\<Lambda> f g s. (\<lambda>(t, x, y). If t then f\<cdot>x else g\<cdot>y) (Rep_ssum s))"
166
167translations
168  "case s of XCONST sinl\<cdot>x \<Rightarrow> t1 | XCONST sinr\<cdot>y \<Rightarrow> t2" \<rightleftharpoons> "CONST sscase\<cdot>(\<Lambda> x. t1)\<cdot>(\<Lambda> y. t2)\<cdot>s"
169  "case s of (XCONST sinl :: 'a)\<cdot>x \<Rightarrow> t1 | XCONST sinr\<cdot>y \<Rightarrow> t2" \<rightharpoonup> "CONST sscase\<cdot>(\<Lambda> x. t1)\<cdot>(\<Lambda> y. t2)\<cdot>s"
170
171translations
172  "\<Lambda>(XCONST sinl\<cdot>x). t" \<rightleftharpoons> "CONST sscase\<cdot>(\<Lambda> x. t)\<cdot>\<bottom>"
173  "\<Lambda>(XCONST sinr\<cdot>y). t" \<rightleftharpoons> "CONST sscase\<cdot>\<bottom>\<cdot>(\<Lambda> y. t)"
174
175lemma beta_sscase: "sscase\<cdot>f\<cdot>g\<cdot>s = (\<lambda>(t, x, y). If t then f\<cdot>x else g\<cdot>y) (Rep_ssum s)"
176  by (simp add: sscase_def cont_Rep_ssum)
177
178lemma sscase1 [simp]: "sscase\<cdot>f\<cdot>g\<cdot>\<bottom> = \<bottom>"
179  by (simp add: beta_sscase Rep_ssum_strict)
180
181lemma sscase2 [simp]: "x \<noteq> \<bottom> \<Longrightarrow> sscase\<cdot>f\<cdot>g\<cdot>(sinl\<cdot>x) = f\<cdot>x"
182  by (simp add: beta_sscase Rep_ssum_sinl)
183
184lemma sscase3 [simp]: "y \<noteq> \<bottom> \<Longrightarrow> sscase\<cdot>f\<cdot>g\<cdot>(sinr\<cdot>y) = g\<cdot>y"
185  by (simp add: beta_sscase Rep_ssum_sinr)
186
187lemma sscase4 [simp]: "sscase\<cdot>sinl\<cdot>sinr\<cdot>z = z"
188  by (cases z) simp_all
189
190
191subsection \<open>Strict sum preserves flatness\<close>
192
193instance ssum :: (flat, flat) flat
194  apply (intro_classes, clarify)
195  apply (case_tac x, simp)
196   apply (case_tac y, simp_all add: flat_below_iff)
197  apply (case_tac y, simp_all add: flat_below_iff)
198  done
199
200end
201