11898
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Typedefs}%
|
17056
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
|
10 |
%
|
|
11 |
\endisatagtheory
|
|
12 |
{\isafoldtheory}%
|
|
13 |
%
|
|
14 |
\isadelimtheory
|
|
15 |
%
|
|
16 |
\endisadelimtheory
|
11898
|
17 |
%
|
|
18 |
\isamarkupsection{Introducing New Types%
|
|
19 |
}
|
|
20 |
\isamarkuptrue%
|
|
21 |
%
|
|
22 |
\begin{isamarkuptext}%
|
|
23 |
\label{sec:adv-typedef}
|
|
24 |
For most applications, a combination of predefined types like \isa{bool} and
|
40406
|
25 |
\isa{{\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}} with recursive datatypes and records is quite sufficient. Very
|
11898
|
26 |
occasionally you may feel the need for a more advanced type. If you
|
|
27 |
are certain that your type is not definable by any of the
|
|
28 |
standard means, then read on.
|
|
29 |
\begin{warn}
|
|
30 |
Types in HOL must be non-empty; otherwise the quantifier rules would be
|
|
31 |
unsound, because $\exists x.\ x=x$ is a theorem.
|
|
32 |
\end{warn}%
|
|
33 |
\end{isamarkuptext}%
|
|
34 |
\isamarkuptrue%
|
|
35 |
%
|
|
36 |
\isamarkupsubsection{Declaring New Types%
|
|
37 |
}
|
|
38 |
\isamarkuptrue%
|
|
39 |
%
|
|
40 |
\begin{isamarkuptext}%
|
|
41 |
\label{sec:typedecl}
|
|
42 |
\index{types!declaring|(}%
|
|
43 |
\index{typedecl@\isacommand {typedecl} (command)}%
|
|
44 |
The most trivial way of introducing a new type is by a \textbf{type
|
|
45 |
declaration}:%
|
|
46 |
\end{isamarkuptext}%
|
17175
|
47 |
\isamarkuptrue%
|
|
48 |
\isacommand{typedecl}\isamarkupfalse%
|
40406
|
49 |
\ my{\isaliteral{5F}{\isacharunderscore}}new{\isaliteral{5F}{\isacharunderscore}}type%
|
11898
|
50 |
\begin{isamarkuptext}%
|
|
51 |
\noindent
|
40406
|
52 |
This does not define \isa{my{\isaliteral{5F}{\isacharunderscore}}new{\isaliteral{5F}{\isacharunderscore}}type} at all but merely introduces its
|
11898
|
53 |
name. Thus we know nothing about this type, except that it is
|
|
54 |
non-empty. Such declarations without definitions are
|
|
55 |
useful if that type can be viewed as a parameter of the theory.
|
|
56 |
A typical example is given in \S\ref{sec:VMC}, where we define a transition
|
|
57 |
relation over an arbitrary type of states.
|
|
58 |
|
|
59 |
In principle we can always get rid of such type declarations by making those
|
|
60 |
types parameters of every other type, thus keeping the theory generic. In
|
|
61 |
practice, however, the resulting clutter can make types hard to read.
|
|
62 |
|
|
63 |
If you are looking for a quick and dirty way of introducing a new type
|
|
64 |
together with its properties: declare the type and state its properties as
|
|
65 |
axioms. Example:%
|
|
66 |
\end{isamarkuptext}%
|
17175
|
67 |
\isamarkuptrue%
|
|
68 |
\isacommand{axioms}\isamarkupfalse%
|
|
69 |
\isanewline
|
40406
|
70 |
just{\isaliteral{5F}{\isacharunderscore}}one{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C6578697374733E}{\isasymexists}}x{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}my{\isaliteral{5F}{\isacharunderscore}}new{\isaliteral{5F}{\isacharunderscore}}type{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C666F72616C6C3E}{\isasymforall}}y{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ y{\isaliteral{22}{\isachardoublequoteclose}}%
|
11898
|
71 |
\begin{isamarkuptext}%
|
|
72 |
\noindent
|
|
73 |
However, we strongly discourage this approach, except at explorative stages
|
|
74 |
of your development. It is extremely easy to write down contradictory sets of
|
|
75 |
axioms, in which case you will be able to prove everything but it will mean
|
|
76 |
nothing. In the example above, the axiomatic approach is
|
|
77 |
unnecessary: a one-element type called \isa{unit} is already defined in HOL.
|
|
78 |
\index{types!declaring|)}%
|
|
79 |
\end{isamarkuptext}%
|
|
80 |
\isamarkuptrue%
|
|
81 |
%
|
|
82 |
\isamarkupsubsection{Defining New Types%
|
|
83 |
}
|
|
84 |
\isamarkuptrue%
|
|
85 |
%
|
|
86 |
\begin{isamarkuptext}%
|
|
87 |
\label{sec:typedef}
|
|
88 |
\index{types!defining|(}%
|
|
89 |
\index{typedecl@\isacommand {typedef} (command)|(}%
|
|
90 |
Now we come to the most general means of safely introducing a new type, the
|
|
91 |
\textbf{type definition}. All other means, for example
|
|
92 |
\isacommand{datatype}, are based on it. The principle is extremely simple:
|
12334
|
93 |
any non-empty subset of an existing type can be turned into a new type.
|
|
94 |
More precisely, the new type is specified to be isomorphic to some
|
|
95 |
non-empty subset of an existing type.
|
11898
|
96 |
|
|
97 |
Let us work a simple example, the definition of a three-element type.
|
|
98 |
It is easily represented by the first three natural numbers:%
|
|
99 |
\end{isamarkuptext}%
|
17175
|
100 |
\isamarkuptrue%
|
|
101 |
\isacommand{typedef}\isamarkupfalse%
|
40406
|
102 |
\ three\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{7B}{\isacharbraceleft}}{\isadigit{0}}{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}nat{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}%
|
17056
|
103 |
\isadelimproof
|
|
104 |
%
|
|
105 |
\endisadelimproof
|
|
106 |
%
|
|
107 |
\isatagproof
|
16353
|
108 |
%
|
|
109 |
\begin{isamarkuptxt}%
|
|
110 |
\noindent
|
|
111 |
In order to enforce that the representing set on the right-hand side is
|
|
112 |
non-empty, this definition actually starts a proof to that effect:
|
|
113 |
\begin{isabelle}%
|
40406
|
114 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6578697374733E}{\isasymexists}}x{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}{\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{7D}{\isacharbraceright}}%
|
16353
|
115 |
\end{isabelle}
|
|
116 |
Fortunately, this is easy enough to show, even \isa{auto} could do it.
|
|
117 |
In general, one has to provide a witness, in our case 0:%
|
|
118 |
\end{isamarkuptxt}%
|
17175
|
119 |
\isamarkuptrue%
|
|
120 |
\isacommand{apply}\isamarkupfalse%
|
40406
|
121 |
{\isaliteral{28}{\isacharparenleft}}rule{\isaliteral{5F}{\isacharunderscore}}tac\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ \isakeyword{in}\ exI{\isaliteral{29}{\isacharparenright}}\isanewline
|
17175
|
122 |
\isacommand{by}\isamarkupfalse%
|
|
123 |
\ simp%
|
17056
|
124 |
\endisatagproof
|
|
125 |
{\isafoldproof}%
|
|
126 |
%
|
|
127 |
\isadelimproof
|
|
128 |
%
|
|
129 |
\endisadelimproof
|
11898
|
130 |
%
|
|
131 |
\begin{isamarkuptext}%
|
|
132 |
This type definition introduces the new type \isa{three} and asserts
|
40406
|
133 |
that it is a copy of the set \isa{{\isaliteral{7B}{\isacharbraceleft}}{\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{7D}{\isacharbraceright}}}. This assertion
|
11898
|
134 |
is expressed via a bijection between the \emph{type} \isa{three} and the
|
40406
|
135 |
\emph{set} \isa{{\isaliteral{7B}{\isacharbraceleft}}{\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{7D}{\isacharbraceright}}}. To this end, the command declares the following
|
11898
|
136 |
constants behind the scenes:
|
|
137 |
\begin{center}
|
|
138 |
\begin{tabular}{rcl}
|
|
139 |
\isa{three} &::& \isa{nat\ set} \\
|
40406
|
140 |
\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three} &::& \isa{three\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat}\\
|
|
141 |
\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three} &::& \isa{nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ three}
|
11898
|
142 |
\end{tabular}
|
|
143 |
\end{center}
|
|
144 |
where constant \isa{three} is explicitly defined as the representing set:
|
|
145 |
\begin{center}
|
40406
|
146 |
\isa{three\ {\isaliteral{5C3C65717569763E}{\isasymequiv}}\ {\isaliteral{7B}{\isacharbraceleft}}{\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{7D}{\isacharbraceright}}}\hfill(\isa{three{\isaliteral{5F}{\isacharunderscore}}def})
|
11898
|
147 |
\end{center}
|
|
148 |
The situation is best summarized with the help of the following diagram,
|
12543
|
149 |
where squares denote types and the irregular region denotes a set:
|
11898
|
150 |
\begin{center}
|
12627
|
151 |
\includegraphics[scale=.8]{typedef}
|
11898
|
152 |
\end{center}
|
40406
|
153 |
Finally, \isacommand{typedef} asserts that \isa{Rep{\isaliteral{5F}{\isacharunderscore}}three} is
|
|
154 |
surjective on the subset \isa{three} and \isa{Abs{\isaliteral{5F}{\isacharunderscore}}three} and \isa{Rep{\isaliteral{5F}{\isacharunderscore}}three} are inverses of each other:
|
11898
|
155 |
\begin{center}
|
|
156 |
\begin{tabular}{@ {}r@ {\qquad\qquad}l@ {}}
|
40406
|
157 |
\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three} & (\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three}) \\
|
|
158 |
\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three\ {\isaliteral{28}{\isacharparenleft}}Rep{\isaliteral{5F}{\isacharunderscore}}three\ x{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ x} & (\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}inverse}) \\
|
|
159 |
\isa{y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ Rep{\isaliteral{5F}{\isacharunderscore}}three\ {\isaliteral{28}{\isacharparenleft}}Abs{\isaliteral{5F}{\isacharunderscore}}three\ y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ y} & (\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}inverse})
|
11898
|
160 |
\end{tabular}
|
|
161 |
\end{center}
|
|
162 |
%
|
|
163 |
From this example it should be clear what \isacommand{typedef} does
|
|
164 |
in general given a name (here \isa{three}) and a set
|
40406
|
165 |
(here \isa{{\isaliteral{7B}{\isacharbraceleft}}{\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{7D}{\isacharbraceright}}}).
|
11898
|
166 |
|
|
167 |
Our next step is to define the basic functions expected on the new type.
|
|
168 |
Although this depends on the type at hand, the following strategy works well:
|
|
169 |
\begin{itemize}
|
|
170 |
\item define a small kernel of basic functions that can express all other
|
|
171 |
functions you anticipate.
|
|
172 |
\item define the kernel in terms of corresponding functions on the
|
|
173 |
representing type using \isa{Abs} and \isa{Rep} to convert between the
|
|
174 |
two levels.
|
|
175 |
\end{itemize}
|
|
176 |
In our example it suffices to give the three elements of type \isa{three}
|
|
177 |
names:%
|
|
178 |
\end{isamarkuptext}%
|
17175
|
179 |
\isamarkuptrue%
|
27027
|
180 |
\isacommand{definition}\isamarkupfalse%
|
40406
|
181 |
\ A\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ three\ \isakeyword{where}\ {\isaliteral{22}{\isachardoublequoteopen}}A\ {\isaliteral{5C3C65717569763E}{\isasymequiv}}\ Abs{\isaliteral{5F}{\isacharunderscore}}three\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline
|
27027
|
182 |
\isacommand{definition}\isamarkupfalse%
|
40406
|
183 |
\ B\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ three\ \isakeyword{where}\ {\isaliteral{22}{\isachardoublequoteopen}}B\ {\isaliteral{5C3C65717569763E}{\isasymequiv}}\ Abs{\isaliteral{5F}{\isacharunderscore}}three\ {\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline
|
27027
|
184 |
\isacommand{definition}\isamarkupfalse%
|
40406
|
185 |
\ C\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ three\ \isakeyword{where}\ {\isaliteral{22}{\isachardoublequoteopen}}C\ {\isaliteral{5C3C65717569763E}{\isasymequiv}}\ Abs{\isaliteral{5F}{\isacharunderscore}}three\ {\isadigit{2}}{\isaliteral{22}{\isachardoublequoteclose}}%
|
11898
|
186 |
\begin{isamarkuptext}%
|
|
187 |
So far, everything was easy. But it is clear that reasoning about \isa{three} will be hell if we have to go back to \isa{nat} every time. Thus our
|
|
188 |
aim must be to raise our level of abstraction by deriving enough theorems
|
|
189 |
about type \isa{three} to characterize it completely. And those theorems
|
40406
|
190 |
should be phrased in terms of \isa{A}, \isa{B} and \isa{C}, not \isa{Abs{\isaliteral{5F}{\isacharunderscore}}three} and \isa{Rep{\isaliteral{5F}{\isacharunderscore}}three}. Because of the simplicity of the example,
|
11898
|
191 |
we merely need to prove that \isa{A}, \isa{B} and \isa{C} are distinct
|
|
192 |
and that they exhaust the type.
|
|
193 |
|
|
194 |
In processing our \isacommand{typedef} declaration,
|
12473
|
195 |
Isabelle proves several helpful lemmas. The first two
|
40406
|
196 |
express injectivity of \isa{Rep{\isaliteral{5F}{\isacharunderscore}}three} and \isa{Abs{\isaliteral{5F}{\isacharunderscore}}three}:
|
11898
|
197 |
\begin{center}
|
12473
|
198 |
\begin{tabular}{@ {}r@ {\qquad}l@ {}}
|
40406
|
199 |
\isa{{\isaliteral{28}{\isacharparenleft}}Rep{\isaliteral{5F}{\isacharunderscore}}three\ x\ {\isaliteral{3D}{\isacharequal}}\ Rep{\isaliteral{5F}{\isacharunderscore}}three\ y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{3D}{\isacharequal}}\ y{\isaliteral{29}{\isacharparenright}}} & (\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}inject}) \\
|
12473
|
200 |
\begin{tabular}{@ {}l@ {}}
|
40406
|
201 |
\isa{{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three{\isaliteral{3B}{\isacharsemicolon}}\ y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three\ {\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}} \\
|
|
202 |
\isa{{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}Abs{\isaliteral{5F}{\isacharunderscore}}three\ x\ {\isaliteral{3D}{\isacharequal}}\ Abs{\isaliteral{5F}{\isacharunderscore}}three\ y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{3D}{\isacharequal}}\ y{\isaliteral{29}{\isacharparenright}}}
|
|
203 |
\end{tabular} & (\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}inject}) \\
|
12473
|
204 |
\end{tabular}
|
11898
|
205 |
\end{center}
|
40406
|
206 |
The following ones allow to replace some \isa{x{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}three} by
|
|
207 |
\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{28}{\isacharparenleft}}y{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}nat{\isaliteral{29}{\isacharparenright}}}, and conversely \isa{y} by \isa{Rep{\isaliteral{5F}{\isacharunderscore}}three\ x}:
|
11898
|
208 |
\begin{center}
|
12473
|
209 |
\begin{tabular}{@ {}r@ {\qquad}l@ {}}
|
40406
|
210 |
\isa{{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}x{\isaliteral{2E}{\isachardot}}\ y\ {\isaliteral{3D}{\isacharequal}}\ Rep{\isaliteral{5F}{\isacharunderscore}}three\ x\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P} & (\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}cases}) \\
|
|
211 |
\isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C416E643E}{\isasymAnd}}y{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}x\ {\isaliteral{3D}{\isacharequal}}\ Abs{\isaliteral{5F}{\isacharunderscore}}three\ y{\isaliteral{3B}{\isacharsemicolon}}\ y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P} & (\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}cases}) \\
|
|
212 |
\isa{{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}x{\isaliteral{2E}{\isachardot}}\ P\ {\isaliteral{28}{\isacharparenleft}}Rep{\isaliteral{5F}{\isacharunderscore}}three\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ y} & (\isa{Rep{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}induct}) \\
|
|
213 |
\isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C416E643E}{\isasymAnd}}y{\isaliteral{2E}{\isachardot}}\ y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ {\isaliteral{28}{\isacharparenleft}}Abs{\isaliteral{5F}{\isacharunderscore}}three\ y{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ x} & (\isa{Abs{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}induct}) \\
|
12473
|
214 |
\end{tabular}
|
11898
|
215 |
\end{center}
|
12473
|
216 |
These theorems are proved for any type definition, with \isa{three}
|
|
217 |
replaced by the name of the type in question.
|
|
218 |
|
11898
|
219 |
Distinctness of \isa{A}, \isa{B} and \isa{C} follows immediately
|
|
220 |
if we expand their definitions and rewrite with the injectivity
|
40406
|
221 |
of \isa{Abs{\isaliteral{5F}{\isacharunderscore}}three}:%
|
11898
|
222 |
\end{isamarkuptext}%
|
17175
|
223 |
\isamarkuptrue%
|
|
224 |
\isacommand{lemma}\isamarkupfalse%
|
40406
|
225 |
\ {\isaliteral{22}{\isachardoublequoteopen}}A\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ B\ {\isaliteral{5C3C616E643E}{\isasymand}}\ B\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ A\ {\isaliteral{5C3C616E643E}{\isasymand}}\ A\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ C\ {\isaliteral{5C3C616E643E}{\isasymand}}\ C\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ A\ {\isaliteral{5C3C616E643E}{\isasymand}}\ B\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ C\ {\isaliteral{5C3C616E643E}{\isasymand}}\ C\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ B{\isaliteral{22}{\isachardoublequoteclose}}\isanewline
|
17056
|
226 |
%
|
|
227 |
\isadelimproof
|
|
228 |
%
|
|
229 |
\endisadelimproof
|
|
230 |
%
|
|
231 |
\isatagproof
|
17175
|
232 |
\isacommand{by}\isamarkupfalse%
|
40406
|
233 |
{\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}\ Abs{\isaliteral{5F}{\isacharunderscore}}three{\isaliteral{5F}{\isacharunderscore}}inject\ A{\isaliteral{5F}{\isacharunderscore}}def\ B{\isaliteral{5F}{\isacharunderscore}}def\ C{\isaliteral{5F}{\isacharunderscore}}def\ three{\isaliteral{5F}{\isacharunderscore}}def{\isaliteral{29}{\isacharparenright}}%
|
17056
|
234 |
\endisatagproof
|
|
235 |
{\isafoldproof}%
|
|
236 |
%
|
|
237 |
\isadelimproof
|
|
238 |
%
|
|
239 |
\endisadelimproof
|
11898
|
240 |
%
|
|
241 |
\begin{isamarkuptext}%
|
|
242 |
\noindent
|
40406
|
243 |
Of course we rely on the simplifier to solve goals like \isa{{\isadigit{0}}\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{1}}}.
|
11898
|
244 |
|
|
245 |
The fact that \isa{A}, \isa{B} and \isa{C} exhaust type \isa{three} is
|
|
246 |
best phrased as a case distinction theorem: if you want to prove \isa{P\ x}
|
|
247 |
(where \isa{x} is of type \isa{three}) it suffices to prove \isa{P\ A},
|
12473
|
248 |
\isa{P\ B} and \isa{P\ C}:%
|
11898
|
249 |
\end{isamarkuptext}%
|
17175
|
250 |
\isamarkuptrue%
|
|
251 |
\isacommand{lemma}\isamarkupfalse%
|
40406
|
252 |
\ three{\isaliteral{5F}{\isacharunderscore}}cases{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}\ P\ A{\isaliteral{3B}{\isacharsemicolon}}\ P\ B{\isaliteral{3B}{\isacharsemicolon}}\ P\ C\ {\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ x{\isaliteral{22}{\isachardoublequoteclose}}%
|
17056
|
253 |
\isadelimproof
|
|
254 |
%
|
|
255 |
\endisadelimproof
|
|
256 |
%
|
|
257 |
\isatagproof
|
16353
|
258 |
%
|
|
259 |
\begin{isamarkuptxt}%
|
27319
|
260 |
\noindent Again this follows easily using the induction principle stemming from the type definition:%
|
16353
|
261 |
\end{isamarkuptxt}%
|
17175
|
262 |
\isamarkuptrue%
|
|
263 |
\isacommand{apply}\isamarkupfalse%
|
40406
|
264 |
{\isaliteral{28}{\isacharparenleft}}induct{\isaliteral{5F}{\isacharunderscore}}tac\ x{\isaliteral{29}{\isacharparenright}}%
|
16353
|
265 |
\begin{isamarkuptxt}%
|
|
266 |
\begin{isabelle}%
|
40406
|
267 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}y{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}P\ A{\isaliteral{3B}{\isacharsemicolon}}\ P\ B{\isaliteral{3B}{\isacharsemicolon}}\ P\ C{\isaliteral{3B}{\isacharsemicolon}}\ y\ {\isaliteral{5C3C696E3E}{\isasymin}}\ three{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ {\isaliteral{28}{\isacharparenleft}}Abs{\isaliteral{5F}{\isacharunderscore}}three\ y{\isaliteral{29}{\isacharparenright}}%
|
16353
|
268 |
\end{isabelle}
|
40406
|
269 |
Simplification with \isa{three{\isaliteral{5F}{\isacharunderscore}}def} leads to the disjunction \isa{y\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ y\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ y\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{2}}} which \isa{auto} separates into three
|
16353
|
270 |
subgoals, each of which is easily solved by simplification:%
|
|
271 |
\end{isamarkuptxt}%
|
17175
|
272 |
\isamarkuptrue%
|
|
273 |
\isacommand{apply}\isamarkupfalse%
|
40406
|
274 |
{\isaliteral{28}{\isacharparenleft}}auto\ simp\ add{\isaliteral{3A}{\isacharcolon}}\ three{\isaliteral{5F}{\isacharunderscore}}def\ A{\isaliteral{5F}{\isacharunderscore}}def\ B{\isaliteral{5F}{\isacharunderscore}}def\ C{\isaliteral{5F}{\isacharunderscore}}def{\isaliteral{29}{\isacharparenright}}\isanewline
|
17175
|
275 |
\isacommand{done}\isamarkupfalse%
|
|
276 |
%
|
17056
|
277 |
\endisatagproof
|
|
278 |
{\isafoldproof}%
|
|
279 |
%
|
|
280 |
\isadelimproof
|
|
281 |
%
|
|
282 |
\endisadelimproof
|
11898
|
283 |
%
|
|
284 |
\begin{isamarkuptext}%
|
|
285 |
\noindent
|
|
286 |
This concludes the derivation of the characteristic theorems for
|
|
287 |
type \isa{three}.
|
|
288 |
|
|
289 |
The attentive reader has realized long ago that the
|
|
290 |
above lengthy definition can be collapsed into one line:%
|
|
291 |
\end{isamarkuptext}%
|
17175
|
292 |
\isamarkuptrue%
|
|
293 |
\isacommand{datatype}\isamarkupfalse%
|
40406
|
294 |
\ better{\isaliteral{5F}{\isacharunderscore}}three\ {\isaliteral{3D}{\isacharequal}}\ A\ {\isaliteral{7C}{\isacharbar}}\ B\ {\isaliteral{7C}{\isacharbar}}\ C%
|
11898
|
295 |
\begin{isamarkuptext}%
|
|
296 |
\noindent
|
|
297 |
In fact, the \isacommand{datatype} command performs internally more or less
|
|
298 |
the same derivations as we did, which gives you some idea what life would be
|
|
299 |
like without \isacommand{datatype}.
|
|
300 |
|
|
301 |
Although \isa{three} could be defined in one line, we have chosen this
|
|
302 |
example to demonstrate \isacommand{typedef} because its simplicity makes the
|
|
303 |
key concepts particularly easy to grasp. If you would like to see a
|
|
304 |
non-trivial example that cannot be defined more directly, we recommend the
|
12473
|
305 |
definition of \emph{finite multisets} in the Library~\cite{HOL-Library}.
|
11898
|
306 |
|
|
307 |
Let us conclude by summarizing the above procedure for defining a new type.
|
|
308 |
Given some abstract axiomatic description $P$ of a type $ty$ in terms of a
|
|
309 |
set of functions $F$, this involves three steps:
|
|
310 |
\begin{enumerate}
|
|
311 |
\item Find an appropriate type $\tau$ and subset $A$ which has the desired
|
|
312 |
properties $P$, and make a type definition based on this representation.
|
|
313 |
\item Define the required functions $F$ on $ty$ by lifting
|
|
314 |
analogous functions on the representation via $Abs_ty$ and $Rep_ty$.
|
|
315 |
\item Prove that $P$ holds for $ty$ by lifting $P$ from the representation.
|
|
316 |
\end{enumerate}
|
|
317 |
You can now forget about the representation and work solely in terms of the
|
|
318 |
abstract functions $F$ and properties $P$.%
|
|
319 |
\index{typedecl@\isacommand {typedef} (command)|)}%
|
|
320 |
\index{types!defining|)}%
|
|
321 |
\end{isamarkuptext}%
|
17175
|
322 |
\isamarkuptrue%
|
17056
|
323 |
%
|
|
324 |
\isadelimtheory
|
|
325 |
%
|
|
326 |
\endisadelimtheory
|
|
327 |
%
|
|
328 |
\isatagtheory
|
|
329 |
%
|
|
330 |
\endisatagtheory
|
|
331 |
{\isafoldtheory}%
|
|
332 |
%
|
|
333 |
\isadelimtheory
|
|
334 |
%
|
|
335 |
\endisadelimtheory
|
11898
|
336 |
\end{isabellebody}%
|
|
337 |
%%% Local Variables:
|
|
338 |
%%% mode: latex
|
|
339 |
%%% TeX-master: "root"
|
|
340 |
%%% End:
|