author | wenzelm |
Sat, 01 Dec 2001 18:52:32 +0100 | |
changeset 12338 | de0f4a63baa5 |
parent 11494 | 23a118849801 |
child 12815 | 1f073030b97a |
permissions | -rw-r--r-- |
10305 | 1 |
(*<*)theory Overloading1 = Main:(*>*) |
2 |
||
10885 | 3 |
subsubsection{*Controlled Overloading with Type Classes*} |
10305 | 4 |
|
5 |
text{* |
|
11494 | 6 |
We now start with the theory of ordering relations, which we shall phrase |
11277 | 7 |
in terms of the two binary symbols @{text"<<"} and @{text"<<="} |
8 |
to avoid clashes with @{text"<"} and @{text"\<le>"} in theory @{text |
|
10305 | 9 |
Main}. To restrict the application of @{text"<<"} and @{text"<<="} we |
10 |
introduce the class @{text ordrel}: |
|
11 |
*} |
|
12 |
||
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11494
diff
changeset
|
13 |
axclass ordrel < type |
10305 | 14 |
|
15 |
text{*\noindent |
|
10328 | 16 |
This introduces a new class @{text ordrel} and makes it a subclass of |
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11494
diff
changeset
|
17 |
the predefined class @{text type}, which |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11494
diff
changeset
|
18 |
is the class of all HOL types. |
10305 | 19 |
This is a degenerate form of axiomatic type class without any axioms. |
20 |
Its sole purpose is to restrict the use of overloaded constants to meaningful |
|
21 |
instances: |
|
22 |
*} |
|
23 |
||
10328 | 24 |
consts less :: "('a::ordrel) \<Rightarrow> 'a \<Rightarrow> bool" (infixl "<<" 50) |
25 |
le :: "('a::ordrel) \<Rightarrow> 'a \<Rightarrow> bool" (infixl "<<=" 50) |
|
26 |
||
27 |
text{*\noindent |
|
10396 | 28 |
Note that only one occurrence of a type variable in a type needs to be |
29 |
constrained with a class; the constraint is propagated to the other |
|
30 |
occurrences automatically. |
|
31 |
||
11494 | 32 |
So far there are no types of class @{text ordrel}. To breathe life |
10328 | 33 |
into @{text ordrel} we need to declare a type to be an \bfindex{instance} of |
34 |
@{text ordrel}: |
|
35 |
*} |
|
10305 | 36 |
|
37 |
instance bool :: ordrel |
|
10328 | 38 |
|
39 |
txt{*\noindent |
|
40 |
Command \isacommand{instance} actually starts a proof, namely that |
|
41 |
@{typ bool} satisfies all axioms of @{text ordrel}. |
|
42 |
There are none, but we still need to finish that proof, which we do |
|
11494 | 43 |
by invoking the \methdx{intro_classes} method: |
10328 | 44 |
*} |
45 |
||
10305 | 46 |
by intro_classes |
47 |
||
10328 | 48 |
text{*\noindent |
49 |
More interesting \isacommand{instance} proofs will arise below |
|
50 |
in the context of proper axiomatic type classes. |
|
51 |
||
11161 | 52 |
Although terms like @{prop"False <<= P"} are now legal, we still need to say |
10328 | 53 |
what the relation symbols actually mean at type @{typ bool}: |
54 |
*} |
|
55 |
||
10305 | 56 |
defs (overloaded) |
57 |
le_bool_def: "P <<= Q \<equiv> P \<longrightarrow> Q" |
|
58 |
less_bool_def: "P << Q \<equiv> \<not>P \<and> Q" |
|
59 |
||
10328 | 60 |
text{*\noindent |
11494 | 61 |
Now @{prop"False <<= P"} is provable: |
10305 | 62 |
*} |
63 |
||
10328 | 64 |
lemma "False <<= P" |
10305 | 65 |
by(simp add: le_bool_def) |
66 |
||
67 |
text{*\noindent |
|
11494 | 68 |
At this point, @{text"[] <<= []"} is not even well-typed. |
69 |
To make it well-typed, |
|
10305 | 70 |
we need to make lists a type of class @{text ordrel}:*}(*<*)end(*>*) |