src/Doc/Tutorial/Types/Axioms.thy
author wenzelm
Fri, 03 May 2024 00:24:57 +0200
changeset 80171 9e88c17a723e
parent 76987 4c275405faae
permissions -rw-r--r--
tuned proofs; tuned whitespace;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72991
d0a0b74f0ad7 avoid multiple uses of the same ML file;
wenzelm
parents: 69597
diff changeset
     1
(*<*)theory Axioms imports Overloading "../Setup" begin(*>*)
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
     2
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
     3
subsection \<open>Axioms\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
     4
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
     5
text \<open>Attaching axioms to our classes lets us reason on the level of
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
     6
classes.  The results will be applicable to all types in a class, just
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
     7
as in axiomatic mathematics.
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
     8
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
     9
\begin{warn}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    10
Proofs in this section use structured \emph{Isar} proofs, which are not
76987
4c275405faae isabelle update -u cite;
wenzelm
parents: 72991
diff changeset
    11
covered in this tutorial; but see \<^cite>\<open>"Nipkow-TYPES02"\<close>.%
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    12
\end{warn}\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    13
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    14
subsubsection \<open>Semigroups\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    15
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    16
text\<open>We specify \emph{semigroups} as subclass of \<^class>\<open>plus\<close>:\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    17
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    18
class semigroup = plus +
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    19
  assumes assoc: "(x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    20
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    21
text \<open>\noindent This @{command class} specification requires that
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    22
all instances of \<^class>\<open>semigroup\<close> obey @{fact "assoc:"}~@{prop
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
    23
[source] "\<And>x y z :: 'a::semigroup. (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"}.
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    24
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    25
We can use this class axiom to derive further abstract theorems
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    26
relative to class \<^class>\<open>semigroup\<close>:\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    27
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    28
lemma assoc_left:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
    29
  fixes x y z :: "'a::semigroup"
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    30
  shows "x \<oplus> (y \<oplus> z) = (x \<oplus> y) \<oplus> z"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    31
  using assoc by (rule sym)
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    32
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    33
text \<open>\noindent The \<^class>\<open>semigroup\<close> constraint on type \<^typ>\<open>'a\<close> restricts instantiations of \<^typ>\<open>'a\<close> to types of class
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    34
\<^class>\<open>semigroup\<close> and during the proof enables us to use the fact
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    35
@{fact assoc} whose type parameter is itself constrained to class
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    36
\<^class>\<open>semigroup\<close>.  The main advantage of classes is that theorems
31682
358cdcdf56d2 corrected some issues
haftmann
parents: 31678
diff changeset
    37
can be proved in the abstract and freely reused for each instance.
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    38
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    39
On instantiation, we have to give a proof that the given operations
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    40
obey the class axioms:\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    41
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    42
instantiation nat :: semigroup
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    43
begin
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    44
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    45
instance proof
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    46
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    47
txt \<open>\noindent The proof opens with a default proof step, which for
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    48
instance judgements invokes method @{method intro_classes}.\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    49
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    50
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    51
  fix m n q :: nat
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    52
  show "(m \<oplus> n) \<oplus> q = m \<oplus> (n \<oplus> q)"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    53
    by (induct m) simp_all
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    54
qed
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    55
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    56
end
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    57
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    58
text \<open>\noindent Again, the interesting things enter the stage with
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    59
parametric types:\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    60
38325
6daf896bca5e * -> prod
haftmann
parents: 31682
diff changeset
    61
instantiation prod :: (semigroup, semigroup) semigroup
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    62
begin
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    63
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    64
instance proof
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
    65
  fix p\<^sub>1 p\<^sub>2 p\<^sub>3 :: "'a::semigroup \<times> 'b::semigroup"
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 48985
diff changeset
    66
  show "p\<^sub>1 \<oplus> p\<^sub>2 \<oplus> p\<^sub>3 = p\<^sub>1 \<oplus> (p\<^sub>2 \<oplus> p\<^sub>3)"
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 48985
diff changeset
    67
    by (cases p\<^sub>1, cases p\<^sub>2, cases p\<^sub>3) (simp add: assoc)
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    68
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    69
txt \<open>\noindent Associativity of product semigroups is established
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    70
using the hypothetical associativity @{fact assoc} of the type
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    71
components, which holds due to the \<^class>\<open>semigroup\<close> constraints
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    72
imposed on the type components by the @{command instance} proposition.
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    73
Indeed, this pattern often occurs with parametric types and type
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    74
classes.\<close>
10420
ef006735bee8 *** empty log message ***
nipkow
parents: 10396
diff changeset
    75
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    76
qed
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    77
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    78
end
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    79
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    80
subsubsection \<open>Monoids\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    81
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
    82
text \<open>We define a subclass \<open>monoidl\<close> (a semigroup with a
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    83
left-hand neutral) by extending \<^class>\<open>semigroup\<close> with one additional
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
    84
parameter \<open>neutral\<close> together with its property:\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    85
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    86
class monoidl = semigroup +
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    87
  fixes neutral :: "'a" ("\<zero>")
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    88
  assumes neutl: "\<zero> \<oplus> x = x"
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    89
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    90
text \<open>\noindent Again, we prove some instances, by providing
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    91
suitable parameter definitions and proofs for the additional
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
    92
specifications.\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    93
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    94
instantiation nat :: monoidl
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    95
begin
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    96
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
    97
definition
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
    98
  neutral_nat_def: "\<zero> = (0::nat)"
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
    99
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   100
instance proof
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   101
  fix n :: nat
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   102
  show "\<zero> \<oplus> n = n"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   103
    unfolding neutral_nat_def by simp
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   104
qed
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   105
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   106
end
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   107
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   108
text \<open>\noindent In contrast to the examples above, we here have both
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   109
specification of class operations and a non-trivial instance proof.
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   110
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   111
This covers products as well:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   112
\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   113
38325
6daf896bca5e * -> prod
haftmann
parents: 31682
diff changeset
   114
instantiation prod :: (monoidl, monoidl) monoidl
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   115
begin
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   116
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   117
definition
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   118
  neutral_prod_def: "\<zero> = (\<zero>, \<zero>)"
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   119
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   120
instance proof
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
   121
  fix p :: "'a::monoidl \<times> 'b::monoidl"
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   122
  show "\<zero> \<oplus> p = p"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   123
    by (cases p) (simp add: neutral_prod_def neutl)
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   124
qed
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   125
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   126
end
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   127
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   128
text \<open>\noindent Fully-fledged monoids are modelled by another
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   129
subclass which does not add new parameters but tightens the
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   130
specification:\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   131
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   132
class monoid = monoidl +
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   133
  assumes neutr: "x \<oplus> \<zero> = x"
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   134
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   135
text \<open>\noindent Corresponding instances for \<^typ>\<open>nat\<close> and products
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   136
are left as an exercise to the reader.\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   137
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   138
subsubsection \<open>Groups\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   139
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   140
text \<open>\noindent To finish our small algebra example, we add a \<open>group\<close> class:\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   141
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   142
class group = monoidl +
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   143
  fixes inv :: "'a \<Rightarrow> 'a" ("\<div> _" [81] 80)
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   144
  assumes invl: "\<div> x \<oplus> x = \<zero>"
11196
bb4ede27fcb7 *** empty log message ***
nipkow
parents: 10885
diff changeset
   145
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   146
text \<open>\noindent We continue with a further example for abstract
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   147
proofs relative to type classes:\<close>
11196
bb4ede27fcb7 *** empty log message ***
nipkow
parents: 10885
diff changeset
   148
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   149
lemma left_cancel:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
   150
  fixes x y z :: "'a::group"
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   151
  shows "x \<oplus> y = x \<oplus> z \<longleftrightarrow> y = z"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   152
proof
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   153
  assume "x \<oplus> y = x \<oplus> z"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   154
  then have "\<div> x \<oplus> (x \<oplus> y) = \<div> x \<oplus> (x \<oplus> z)" by simp
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   155
  then have "(\<div> x \<oplus> x) \<oplus> y = (\<div> x \<oplus> x) \<oplus> z" by (simp add: assoc)
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   156
  then show "y = z" by (simp add: invl neutl)
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   157
next
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   158
  assume "y = z"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   159
  then show "x \<oplus> y = x \<oplus> z" by simp
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   160
qed
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   161
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   162
text \<open>\noindent Any \<open>group\<close> is also a \<open>monoid\<close>; this
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   163
can be made explicit by claiming an additional subclass relation,
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   164
together with a proof of the logical difference:\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   165
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   166
instance group \<subseteq> monoid
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   167
proof
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   168
  fix x
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   169
  from invl have "\<div> x \<oplus> x = \<zero>" .
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   170
  then have "\<div> x \<oplus> (x \<oplus> \<zero>) = \<div> x \<oplus> x"
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   171
    by (simp add: neutl invl assoc [symmetric])
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   172
  then show "x \<oplus> \<zero> = x" by (simp add: left_cancel)
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   173
qed
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10362
diff changeset
   174
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   175
text \<open>\noindent The proof result is propagated to the type system,
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   176
making \<open>group\<close> an instance of \<open>monoid\<close> by adding an
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   177
additional edge to the graph of subclass relation; see also
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10362
diff changeset
   178
Figure~\ref{fig:subclass}.
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10362
diff changeset
   179
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10362
diff changeset
   180
\begin{figure}[htbp]
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   181
 \begin{center}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   182
   \small
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   183
   \unitlength 0.6mm
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   184
   \begin{picture}(40,60)(0,0)
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   185
     \put(20,60){\makebox(0,0){\<open>semigroup\<close>}}
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   186
     \put(20,40){\makebox(0,0){\<open>monoidl\<close>}}
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   187
     \put(00,20){\makebox(0,0){\<open>monoid\<close>}}
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   188
     \put(40,00){\makebox(0,0){\<open>group\<close>}}
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   189
     \put(20,55){\vector(0,-1){10}}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   190
     \put(15,35){\vector(-1,-1){10}}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   191
     \put(25,35){\vector(1,-3){10}}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   192
   \end{picture}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   193
   \hspace{8em}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   194
   \begin{picture}(40,60)(0,0)
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   195
     \put(20,60){\makebox(0,0){\<open>semigroup\<close>}}
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   196
     \put(20,40){\makebox(0,0){\<open>monoidl\<close>}}
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   197
     \put(00,20){\makebox(0,0){\<open>monoid\<close>}}
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   198
     \put(40,00){\makebox(0,0){\<open>group\<close>}}
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   199
     \put(20,55){\vector(0,-1){10}}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   200
     \put(15,35){\vector(-1,-1){10}}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   201
     \put(05,15){\vector(3,-1){30}}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   202
   \end{picture}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   203
   \caption{Subclass relationship of monoids and groups:
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   204
      before and after establishing the relationship
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 68484
diff changeset
   205
      \<open>group \<subseteq> monoid\<close>;  transitive edges are left out.}
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   206
   \label{fig:subclass}
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   207
 \end{center}
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10362
diff changeset
   208
\end{figure}
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   209
\<close>
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10362
diff changeset
   210
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   211
subsubsection \<open>Inconsistencies\<close>
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   212
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   213
text \<open>The reader may be wondering what happens if we attach an
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   214
inconsistent set of axioms to a class. So far we have always avoided
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   215
to add new axioms to HOL for fear of inconsistencies and suddenly it
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   216
seems that we are throwing all caution to the wind. So why is there no
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   217
problem?
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   218
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   219
The point is that by construction, all type variables in the axioms of
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   220
a \isacommand{class} are automatically constrained with the class
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   221
being defined (as shown for axiom @{thm[source]refl} above). These
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   222
constraints are always carried around and Isabelle takes care that
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   223
they are never lost, unless the type variable is instantiated with a
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   224
type that has been shown to belong to that class. Thus you may be able
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   225
to prove \<^prop>\<open>False\<close> from your axioms, but Isabelle will remind you
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   226
that this theorem has the hidden hypothesis that the class is
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   227
non-empty.
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   228
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   229
Even if each individual class is consistent, intersections of
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   230
(unrelated) classes readily become inconsistent in practice. Now we
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   231
know this need not worry us.\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   232
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   233
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   234
subsubsection\<open>Syntactic Classes and Predefined Overloading\<close>
12332
aea72a834c85 *** empty log message ***
nipkow
parents: 11494
diff changeset
   235
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   236
text \<open>In our algebra example, we have started with a \emph{syntactic
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   237
class} \<^class>\<open>plus\<close> which only specifies operations but no axioms; it
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   238
would have been also possible to start immediately with class \<^class>\<open>semigroup\<close>, specifying the \<open>\<oplus>\<close> operation and associativity at
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   239
the same time.
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   240
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   241
Which approach is more appropriate depends.  Usually it is more
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   242
convenient to introduce operations and axioms in the same class: then
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   243
the type checker will automatically insert the corresponding class
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   244
constraints whenever the operations occur, reducing the need of manual
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   245
annotations.  However, when operations are decorated with popular
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   246
syntax, syntactic classes can be an option to re-use the syntax in
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   247
different contexts; this is indeed the way most overloaded constants
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   248
in HOL are introduced, of which the most important are listed in
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   249
Table~\ref{tab:overloading} in the appendix.  Section
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   250
\ref{sec:numeric-classes} covers a range of corresponding classes
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   251
\emph{with} axioms.
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   252
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   253
Further note that classes may contain axioms but \emph{no} operations.
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   254
An example is class \<^class>\<open>finite\<close> from theory \<^theory>\<open>HOL.Finite_Set\<close>
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 58620
diff changeset
   255
which specifies a type to be finite: @{lemma [source] "finite (UNIV :: 'a::finite
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 61076
diff changeset
   256
set)" by (fact finite_UNIV)}.\<close>
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27027
diff changeset
   257
10328
bf33cbd76c05 *** empty log message ***
nipkow
parents:
diff changeset
   258
(*<*)end(*>*)