7 % |
7 % |
8 \begin{isamarkuptext}% |
8 \begin{isamarkuptext}% |
9 \label{sec:adv-typedef} |
9 \label{sec:adv-typedef} |
10 For most applications, a combination of predefined types like \isa{bool} and |
10 For most applications, a combination of predefined types like \isa{bool} and |
11 \isa{{\isasymRightarrow}} with recursive datatypes and records is quite sufficient. Very |
11 \isa{{\isasymRightarrow}} with recursive datatypes and records is quite sufficient. Very |
12 occasionally you may feel the need for a more advanced type. If you cannot do |
12 occasionally you may feel the need for a more advanced type. If you |
13 without that type, and you are certain that it is not definable by any of the |
13 are certain that your type is not definable by any of the |
14 standard means, then read on. |
14 standard means, then read on. |
15 \begin{warn} |
15 \begin{warn} |
16 Types in HOL must be non-empty; otherwise the quantifier rules would be |
16 Types in HOL must be non-empty; otherwise the quantifier rules would be |
17 unsound, because $\exists x.\ x=x$ is a theorem. |
17 unsound, because $\exists x.\ x=x$ is a theorem. |
18 \end{warn}% |
18 \end{warn}% |
163 about type \isa{three} to characterize it completely. And those theorems |
163 about type \isa{three} to characterize it completely. And those theorems |
164 should be phrased in terms of \isa{A}, \isa{B} and \isa{C}, not \isa{Abs{\isacharunderscore}three} and \isa{Rep{\isacharunderscore}three}. Because of the simplicity of the example, |
164 should be phrased in terms of \isa{A}, \isa{B} and \isa{C}, not \isa{Abs{\isacharunderscore}three} and \isa{Rep{\isacharunderscore}three}. Because of the simplicity of the example, |
165 we merely need to prove that \isa{A}, \isa{B} and \isa{C} are distinct |
165 we merely need to prove that \isa{A}, \isa{B} and \isa{C} are distinct |
166 and that they exhaust the type. |
166 and that they exhaust the type. |
167 |
167 |
168 We start with a helpful version of injectivity of \isa{Abs{\isacharunderscore}three} on the |
168 In processing our \isacommand{typedef} declaration, |
169 representing subset:% |
169 Isabelle helpfully proves several lemmas. |
170 \end{isamarkuptext}% |
170 One, \isa{Abs{\isacharunderscore}three{\isacharunderscore}inject}, |
171 \isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline |
171 expresses that \isa{Abs{\isacharunderscore}three} is injective on the representing subset: |
172 \ {\isachardoublequote}{\isasymlbrakk}\ x\ {\isasymin}\ three{\isacharsemicolon}\ y\ {\isasymin}\ three\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}Abs{\isacharunderscore}three\ x\ {\isacharequal}\ Abs{\isacharunderscore}three\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x{\isacharequal}y{\isacharparenright}{\isachardoublequote}% |
172 \begin{center} |
173 \begin{isamarkuptxt}% |
173 \isa{{\isasymlbrakk}x\ {\isasymin}\ three{\isacharsemicolon}\ y\ {\isasymin}\ three{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}Abs{\isacharunderscore}three\ x\ {\isacharequal}\ Abs{\isacharunderscore}three\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x\ {\isacharequal}\ y{\isacharparenright}} |
174 \noindent |
174 \end{center} |
175 We prove each direction separately. From \isa{Abs{\isacharunderscore}three\ x\ {\isacharequal}\ Abs{\isacharunderscore}three\ y} |
175 Another, \isa{Rep{\isacharunderscore}three{\isacharunderscore}inject}, expresses that the representation |
176 we use \isa{arg{\isacharunderscore}cong} to apply \isa{Rep{\isacharunderscore}three} to both sides, |
176 function is also injective: |
177 deriving \begin{isabelle}% |
177 \begin{center} |
178 Rep{\isacharunderscore}three\ {\isacharparenleft}Abs{\isacharunderscore}three\ x{\isacharparenright}\ {\isacharequal}\ Rep{\isacharunderscore}three\ {\isacharparenleft}Abs{\isacharunderscore}three\ y{\isacharparenright}% |
178 \isa{{\isacharparenleft}Rep{\isacharunderscore}three\ x\ {\isacharequal}\ Rep{\isacharunderscore}three\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x\ {\isacharequal}\ y{\isacharparenright}} |
179 \end{isabelle} |
179 \end{center} |
180 Thus we get the required \isa{x\ {\isacharequal}\ y} by simplification with \isa{Abs{\isacharunderscore}three{\isacharunderscore}inverse}. |
|
181 The other direction |
|
182 is trivial by simplification:% |
|
183 \end{isamarkuptxt}% |
|
184 \isacommand{apply}{\isacharparenleft}rule\ iffI{\isacharparenright}\isanewline |
|
185 \ \isacommand{apply}{\isacharparenleft}drule{\isacharunderscore}tac\ f\ {\isacharequal}\ Rep{\isacharunderscore}three\ \isakeyword{in}\ arg{\isacharunderscore}cong{\isacharparenright}\isanewline |
|
186 \ \isacommand{apply}{\isacharparenleft}simp\ add{\isacharcolon}Abs{\isacharunderscore}three{\isacharunderscore}inverse{\isacharparenright}\isanewline |
|
187 \isacommand{by}\ simp% |
|
188 \begin{isamarkuptext}% |
|
189 \noindent |
|
190 Analogous lemmas can be proved in the same way for arbitrary type definitions. |
|
191 |
|
192 Distinctness of \isa{A}, \isa{B} and \isa{C} follows immediately |
180 Distinctness of \isa{A}, \isa{B} and \isa{C} follows immediately |
193 if we expand their definitions and rewrite with the above simplification rule:% |
181 if we expand their definitions and rewrite with the injectivity |
|
182 of \isa{Abs{\isacharunderscore}three}:% |
194 \end{isamarkuptext}% |
183 \end{isamarkuptext}% |
195 \isacommand{lemma}\ {\isachardoublequote}A\ {\isasymnoteq}\ B\ {\isasymand}\ B\ {\isasymnoteq}\ A\ {\isasymand}\ A\ {\isasymnoteq}\ C\ {\isasymand}\ C\ {\isasymnoteq}\ A\ {\isasymand}\ B\ {\isasymnoteq}\ C\ {\isasymand}\ C\ {\isasymnoteq}\ B{\isachardoublequote}\isanewline |
184 \isacommand{lemma}\ {\isachardoublequote}A\ {\isasymnoteq}\ B\ {\isasymand}\ B\ {\isasymnoteq}\ A\ {\isasymand}\ A\ {\isasymnoteq}\ C\ {\isasymand}\ C\ {\isasymnoteq}\ A\ {\isasymand}\ B\ {\isasymnoteq}\ C\ {\isasymand}\ C\ {\isasymnoteq}\ B{\isachardoublequote}\isanewline |
196 \isacommand{by}{\isacharparenleft}simp\ add{\isacharcolon}A{\isacharunderscore}def\ B{\isacharunderscore}def\ C{\isacharunderscore}def\ three{\isacharunderscore}def{\isacharparenright}% |
185 \isacommand{by}{\isacharparenleft}simp\ add{\isacharcolon}\ Abs{\isacharunderscore}three{\isacharunderscore}inject\ A{\isacharunderscore}def\ B{\isacharunderscore}def\ C{\isacharunderscore}def\ three{\isacharunderscore}def{\isacharparenright}% |
197 \begin{isamarkuptext}% |
186 \begin{isamarkuptext}% |
198 \noindent |
187 \noindent |
199 Of course we rely on the simplifier to solve goals like \isa{{\isadigit{0}}\ {\isasymnoteq}\ {\isadigit{1}}}. |
188 Of course we rely on the simplifier to solve goals like \isa{{\isadigit{0}}\ {\isasymnoteq}\ {\isadigit{1}}}. |
200 |
189 |
201 The fact that \isa{A}, \isa{B} and \isa{C} exhaust type \isa{three} is |
190 The fact that \isa{A}, \isa{B} and \isa{C} exhaust type \isa{three} is |
218 \isacommand{apply}{\isacharparenleft}simp\ add{\isacharcolon}three{\isacharunderscore}def{\isacharparenright}\isanewline |
207 \isacommand{apply}{\isacharparenleft}simp\ add{\isacharcolon}three{\isacharunderscore}def{\isacharparenright}\isanewline |
219 \isacommand{apply}{\isacharparenleft}{\isacharparenleft}erule\ le{\isacharunderscore}SucE{\isacharparenright}{\isacharplus}{\isacharparenright}\isanewline |
208 \isacommand{apply}{\isacharparenleft}{\isacharparenleft}erule\ le{\isacharunderscore}SucE{\isacharparenright}{\isacharplus}{\isacharparenright}\isanewline |
220 \isacommand{apply}\ simp{\isacharunderscore}all\isanewline |
209 \isacommand{apply}\ simp{\isacharunderscore}all\isanewline |
221 \isacommand{done}% |
210 \isacommand{done}% |
222 \begin{isamarkuptext}% |
211 \begin{isamarkuptext}% |
223 Now the case distinction lemma on type \isa{three} is easy to derive if you know how to:% |
212 Now the case distinction lemma on type \isa{three} is easy to derive if you |
|
213 know how:% |
224 \end{isamarkuptext}% |
214 \end{isamarkuptext}% |
225 \isacommand{lemma}\ three{\isacharunderscore}cases{\isacharcolon}\ {\isachardoublequote}{\isasymlbrakk}\ P\ A{\isacharsemicolon}\ P\ B{\isacharsemicolon}\ P\ C\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ P\ x{\isachardoublequote}% |
215 \isacommand{lemma}\ three{\isacharunderscore}cases{\isacharcolon}\ {\isachardoublequote}{\isasymlbrakk}\ P\ A{\isacharsemicolon}\ P\ B{\isacharsemicolon}\ P\ C\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ P\ x{\isachardoublequote}% |
226 \begin{isamarkuptxt}% |
216 \begin{isamarkuptxt}% |
227 \noindent |
217 \noindent |
228 We start by replacing the \isa{x} by \isa{Abs{\isacharunderscore}three\ {\isacharparenleft}Rep{\isacharunderscore}three\ x{\isacharparenright}}:% |
218 We start by replacing the \isa{x} by \isa{Abs{\isacharunderscore}three\ {\isacharparenleft}Rep{\isacharunderscore}three\ x{\isacharparenright}}:% |