10328
|
1 |
(*<*)theory Axioms = Overloading:(*>*)
|
|
2 |
|
|
3 |
subsection{*Axioms*}
|
|
4 |
|
|
5 |
|
|
6 |
text{*Now we want to attach axioms to our classes. Then we can reason on the
|
|
7 |
level of classes and the results will be applicable to all types in a class,
|
|
8 |
just as in axiomatic mathematics. These ideas are demonstrated by means of
|
|
9 |
our above ordering relations.
|
|
10 |
*}
|
|
11 |
|
|
12 |
subsubsection{*Partial orders*}
|
|
13 |
|
|
14 |
text{*
|
|
15 |
A \emph{partial order} is a subclass of @{text ordrel}
|
|
16 |
where certain axioms need to hold:
|
|
17 |
*}
|
|
18 |
|
|
19 |
axclass parord < ordrel
|
|
20 |
refl: "x <<= x"
|
|
21 |
trans: "\<lbrakk> x <<= y; y <<= z \<rbrakk> \<Longrightarrow> x <<= z"
|
|
22 |
antisym: "\<lbrakk> x <<= y; y <<= x \<rbrakk> \<Longrightarrow> x = y"
|
|
23 |
less_le: "x << y = (x <<= y \<and> x \<noteq> y)"
|
|
24 |
|
|
25 |
text{*\noindent
|
|
26 |
The first three axioms are the familiar ones, and the final one
|
|
27 |
requires that @{text"<<"} and @{text"<<="} are related as expected.
|
|
28 |
Note that behind the scenes, Isabelle has restricted the axioms to class
|
|
29 |
@{text parord}. For example, this is what @{thm[source]refl} really looks like:
|
|
30 |
@{thm[show_types]refl}.
|
|
31 |
|
|
32 |
We can now prove simple theorems in this abstract setting, for example
|
|
33 |
that @{text"<<"} is not symmetric:
|
|
34 |
*}
|
|
35 |
|
|
36 |
lemma [simp]: "(x::'a::parord) << y \<Longrightarrow> (\<not> y << x) = True";
|
|
37 |
|
|
38 |
txt{*\noindent
|
|
39 |
The conclusion is not simply @{prop"\<not> y << x"} because the preprocessor
|
|
40 |
of the simplifier would turn this into @{prop"y << x = False"}, thus yielding
|
|
41 |
a nonterminating rewrite rule. In the above form it is a generally useful
|
|
42 |
rule.
|
|
43 |
The type constraint is necessary because otherwise Isabelle would only assume
|
|
44 |
@{text"'a::ordrel"} (as required in the type of @{text"<<"}), in
|
|
45 |
which case the proposition is not a theorem. The proof is easy:
|
|
46 |
*}
|
|
47 |
|
|
48 |
by(simp add:less_le antisym);
|
|
49 |
|
|
50 |
text{* We could now continue in this vein and develop a whole theory of
|
|
51 |
results about partial orders. Eventually we will want to apply these results
|
|
52 |
to concrete types, namely the instances of the class. Thus we first need to
|
|
53 |
prove that the types in question, for example @{typ bool}, are indeed
|
|
54 |
instances of @{text parord}:*}
|
|
55 |
|
|
56 |
instance bool :: parord
|
|
57 |
apply intro_classes;
|
|
58 |
|
|
59 |
txt{*\noindent
|
|
60 |
This time @{text intro_classes} leaves us with the four axioms,
|
|
61 |
specialized to type @{typ bool}, as subgoals:
|
|
62 |
@{goals[display,show_types,indent=0]}
|
|
63 |
Fortunately, the proof is easy for blast, once we have unfolded the definitions
|
|
64 |
of @{text"<<"} and @{text"<<="} at @{typ bool}:
|
|
65 |
*}
|
|
66 |
|
|
67 |
apply(simp_all (no_asm_use) only: le_bool_def less_bool_def);
|
|
68 |
by(blast, blast, blast, blast);
|
|
69 |
|
|
70 |
text{*\noindent
|
|
71 |
Can you figure out why we have to include @{text"(no_asm_use)"}?
|
|
72 |
|
|
73 |
We can now apply our single lemma above in the context of booleans:
|
|
74 |
*}
|
|
75 |
|
|
76 |
lemma "(P::bool) << Q \<Longrightarrow> \<not>(Q << P)";
|
|
77 |
by simp;
|
|
78 |
|
|
79 |
text{*\noindent
|
|
80 |
The effect is not stunning but demonstrates the principle. It also shows
|
|
81 |
that tools like the simplifier can deal with generic rules as well. Moreover,
|
|
82 |
it should be clear that the main advantage of the axiomatic method is that
|
|
83 |
theorems can be proved in the abstract and one does not need to repeat the
|
|
84 |
proof for each instance.
|
|
85 |
*}
|
|
86 |
|
|
87 |
subsubsection{*Linear orders*}
|
|
88 |
|
|
89 |
text{* If any two elements of a partial order are comparable it is a
|
|
90 |
\emph{linear} or \emph{total} order: *}
|
|
91 |
|
|
92 |
axclass linord < parord
|
|
93 |
total: "x <<= y \<or> y <<= x"
|
|
94 |
|
|
95 |
text{*\noindent
|
|
96 |
By construction, @{text linord} inherits all axioms from @{text parord}.
|
|
97 |
Therefore we can show that totality can be expressed in terms of @{text"<<"}
|
|
98 |
as follows:
|
|
99 |
*}
|
|
100 |
|
|
101 |
lemma "\<And>x::'a::linord. x<<y \<or> x=y \<or> y<<x";
|
|
102 |
apply(simp add: less_le);
|
|
103 |
apply(insert total);
|
|
104 |
apply blast;
|
|
105 |
done
|
|
106 |
|
|
107 |
text{*
|
|
108 |
Linear orders are an example of subclassing by construction, which is the most
|
|
109 |
common case. It is also possible to prove additional subclass relationships
|
|
110 |
later on, i.e.\ subclassing by proof. This is the topic of the following
|
|
111 |
section.
|
|
112 |
*}
|
|
113 |
|
|
114 |
subsubsection{*Strict orders*}
|
|
115 |
|
|
116 |
text{*
|
|
117 |
An alternative axiomatization of partial orders takes @{text"<<"} rather than
|
|
118 |
@{text"<<="} as the primary concept. The result is a \emph{strict} order:
|
|
119 |
*}
|
|
120 |
|
|
121 |
axclass strord < ordrel
|
|
122 |
irrefl: "\<not> x << x"
|
|
123 |
less_trans: "\<lbrakk> x << y; y << z \<rbrakk> \<Longrightarrow> x << z"
|
|
124 |
le_less: "x <<= y = (x << y \<or> x = y)"
|
|
125 |
|
|
126 |
text{*\noindent
|
|
127 |
It is well known that partial orders are the same as strict orders. Let us
|
|
128 |
prove one direction, namely that partial orders are a subclass of strict
|
|
129 |
orders. The proof follows the ususal pattern: *}
|
|
130 |
|
|
131 |
instance parord < strord
|
|
132 |
apply intro_classes;
|
|
133 |
apply(simp_all (no_asm_use) add:less_le);
|
|
134 |
apply(blast intro: trans antisym);
|
|
135 |
apply(blast intro: refl);
|
|
136 |
done
|
|
137 |
|
|
138 |
text{*\noindent
|
|
139 |
The result is the following subclass diagram:
|
|
140 |
\[
|
|
141 |
\begin{array}{c}
|
|
142 |
\isa{term}\\
|
|
143 |
|\\
|
|
144 |
\isa{ordrel}\\
|
|
145 |
|\\
|
|
146 |
\isa{strord}\\
|
|
147 |
|\\
|
|
148 |
\isa{parord}
|
|
149 |
\end{array}
|
|
150 |
\]
|
|
151 |
|
|
152 |
In general, the subclass diagram must be acyclic. Therefore Isabelle will
|
|
153 |
complain if you also prove the relationship @{text"strord < parord"}.
|
|
154 |
Multiple inheritance is however permitted.
|
|
155 |
|
|
156 |
This finishes our demonstration of type classes based on orderings. We
|
|
157 |
remind our readers that \isa{Main} contains a much more developed theory of
|
|
158 |
orderings phrased in terms of the usual @{text"\<le>"} and @{text"<"}.
|
|
159 |
It is recommended that, if possible,
|
|
160 |
you base your own ordering relations on this theory.
|
|
161 |
*}
|
|
162 |
|
|
163 |
(*
|
|
164 |
instance strord < parord
|
|
165 |
apply intro_classes;
|
|
166 |
apply(simp_all (no_asm_use) add:le_less);
|
|
167 |
apply(blast intro: less_trans);
|
|
168 |
apply(erule disjE);
|
|
169 |
apply(erule disjE);
|
|
170 |
apply(rule irrefl[THEN notE]);
|
|
171 |
apply(rule less_trans, assumption, assumption);
|
|
172 |
apply blast;apply blast;
|
|
173 |
apply(blast intro: irrefl[THEN notE]);
|
|
174 |
done
|
|
175 |
*)
|
|
176 |
|
|
177 |
subsubsection{*Inconsistencies*}
|
|
178 |
|
|
179 |
text{* The reader may be wondering what happens if we, maybe accidentally,
|
|
180 |
attach an inconsistent set of axioms to a class. So far we have always
|
|
181 |
avoided to add new axioms to HOL for fear of inconsistencies and suddenly it
|
|
182 |
seems that we are throwing all caution to the wind. So why is there no
|
|
183 |
problem?
|
|
184 |
|
|
185 |
The point is that by construction, all type variables in the axioms of an
|
|
186 |
\isacommand{axclass} are automatically constrained with the class being
|
|
187 |
defined (as shown for axiom @{thm[source]refl} above). These constraints are
|
|
188 |
always carried around and Isabelle takes care that they are never lost,
|
|
189 |
unless the type variable is instantiated with a type that has been shown to
|
|
190 |
belong to that class. Thus you may be able to prove @{prop False}
|
|
191 |
from your axioms, but Isabelle will remind you that this
|
|
192 |
theorem has the hidden hypothesis that the class is non-empty.
|
|
193 |
*}
|
|
194 |
|
|
195 |
(*
|
|
196 |
axclass false<"term"
|
|
197 |
false: "x \<noteq> x";
|
|
198 |
|
|
199 |
lemma "False";
|
|
200 |
by(rule notE[OF false HOL.refl]);
|
|
201 |
*)
|
|
202 |
(*<*)end(*>*)
|