doc-src/IsarAdvanced/Classes/Thy/Classes.thy
author haftmann
Fri, 10 Oct 2008 15:52:45 +0200
changeset 28565 519b17118926
parent 28540 541366e3c1b3
child 28566 be2a72b421ae
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
     1
theory Classes
28565
haftmann
parents: 28540
diff changeset
     2
imports Main Setup
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
     3
begin
75b56e51fade initial draft
haftmann
parents:
diff changeset
     4
75b56e51fade initial draft
haftmann
parents:
diff changeset
     5
chapter {* Haskell-style classes with Isabelle/Isar *}
75b56e51fade initial draft
haftmann
parents:
diff changeset
     6
75b56e51fade initial draft
haftmann
parents:
diff changeset
     7
section {* Introduction *}
75b56e51fade initial draft
haftmann
parents:
diff changeset
     8
75b56e51fade initial draft
haftmann
parents:
diff changeset
     9
text {*
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    10
  Type classes were introduces by Wadler and Blott \cite{wadler89how}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    11
  into the Haskell language, to allow for a reasonable implementation
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    12
  of overloading\footnote{throughout this tutorial, we are referring
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    13
  to classical Haskell 1.0 type classes, not considering
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    14
  later additions in expressiveness}.
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    15
  As a canonical example, a polymorphic equality function
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    16
  @{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} which is overloaded on different
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    17
  types for @{text "\<alpha>"}, which is achieved by splitting introduction
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    18
  of the @{text eq} function from its overloaded definitions by means
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    19
  of @{text class} and @{text instance} declarations:
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    20
28565
haftmann
parents: 28540
diff changeset
    21
  \begin{quote}
haftmann
parents: 28540
diff changeset
    22
haftmann
parents: 28540
diff changeset
    23
  \noindent@{text "class eq where"}\footnote{syntax here is a kind of isabellized Haskell} \\
haftmann
parents: 28540
diff changeset
    24
  \hspace*{2ex}@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    25
28565
haftmann
parents: 28540
diff changeset
    26
  \medskip\noindent@{text "instance nat \<Colon> eq where"} \\
haftmann
parents: 28540
diff changeset
    27
  \hspace*{2ex}@{text "eq 0 0 = True"} \\
haftmann
parents: 28540
diff changeset
    28
  \hspace*{2ex}@{text "eq 0 _ = False"} \\
haftmann
parents: 28540
diff changeset
    29
  \hspace*{2ex}@{text "eq _ 0 = False"} \\
haftmann
parents: 28540
diff changeset
    30
  \hspace*{2ex}@{text "eq (Suc n) (Suc m) = eq n m"}
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    31
28565
haftmann
parents: 28540
diff changeset
    32
  \medskip\noindent\@{text "instance (\<alpha>\<Colon>eq, \<beta>\<Colon>eq) pair \<Colon> eq where"} \\
haftmann
parents: 28540
diff changeset
    33
  \hspace*{2ex}@{text "eq (x1, y1) (x2, y2) = eq x1 x2 \<and> eq y1 y2"}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    34
28565
haftmann
parents: 28540
diff changeset
    35
  \medskip\noindent@{text "class ord extends eq where"} \\
haftmann
parents: 28540
diff changeset
    36
  \hspace*{2ex}@{text "less_eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} \\
haftmann
parents: 28540
diff changeset
    37
  \hspace*{2ex}@{text "less \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"}
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    38
28565
haftmann
parents: 28540
diff changeset
    39
  \end{quote}
haftmann
parents: 28540
diff changeset
    40
haftmann
parents: 28540
diff changeset
    41
  \noindent Type variables are annotated with (finitely many) classes;
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    42
  these annotations are assertions that a particular polymorphic type
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    43
  provides definitions for overloaded functions.
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    44
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    45
  Indeed, type classes not only allow for simple overloading
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    46
  but form a generic calculus, an instance of order-sorted
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
    47
  algebra \cite{Nipkow-Prehofer:1993,nipkow-sorts93,Wenzel:1997:TPHOL}.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    48
28540
haftmann
parents: 27505
diff changeset
    49
  From a software engeneering point of view, type classes
haftmann
parents: 27505
diff changeset
    50
  roughly correspond to interfaces in object-oriented languages like Java;
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    51
  so, it is naturally desirable that type classes do not only
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
    52
  provide functions (class parameters) but also state specifications
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    53
  implementations must obey.  For example, the @{text "class eq"}
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    54
  above could be given the following specification, demanding that
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    55
  @{text "class eq"} is an equivalence relation obeying reflexivity,
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    56
  symmetry and transitivity:
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    57
28565
haftmann
parents: 28540
diff changeset
    58
  \begin{quote}
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    59
28565
haftmann
parents: 28540
diff changeset
    60
  \noindent@{text "class eq where"} \\
haftmann
parents: 28540
diff changeset
    61
  \hspace*{2ex}@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} \\
haftmann
parents: 28540
diff changeset
    62
  @{text "satisfying"} \\
haftmann
parents: 28540
diff changeset
    63
  \hspace*{2ex}@{text "refl: eq x x"} \\
haftmann
parents: 28540
diff changeset
    64
  \hspace*{2ex}@{text "sym: eq x y \<longleftrightarrow> eq x y"} \\
haftmann
parents: 28540
diff changeset
    65
  \hspace*{2ex}@{text "trans: eq x y \<and> eq y z \<longrightarrow> eq x z"}
haftmann
parents: 28540
diff changeset
    66
haftmann
parents: 28540
diff changeset
    67
  \end{quote}
haftmann
parents: 28540
diff changeset
    68
haftmann
parents: 28540
diff changeset
    69
  \noindent From a theoretic point of view, type classes are lightweight
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
    70
  modules; Haskell type classes may be emulated by
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    71
  SML functors \cite{classes_modules}. 
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    72
  Isabelle/Isar offers a discipline of type classes which brings
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    73
  all those aspects together:
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    74
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    75
  \begin{enumerate}
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
    76
    \item specifying abstract parameters together with
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    77
       corresponding specifications,
28540
haftmann
parents: 27505
diff changeset
    78
    \item instantiating those abstract parameters by a particular
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    79
       type
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    80
    \item in connection with a ``less ad-hoc'' approach to overloading,
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
    81
    \item with a direct link to the Isabelle module system
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
    82
      (aka locales \cite{kammueller-locales}).
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    83
  \end{enumerate}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    84
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    85
  \noindent Isar type classes also directly support code generation
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    86
  in a Haskell like fashion.
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    87
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    88
  This tutorial demonstrates common elements of structured specifications
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    89
  and abstract reasoning with type classes by the algebraic hierarchy of
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    90
  semigroups, monoids and groups.  Our background theory is that of
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
    91
  Isabelle/HOL \cite{isa-tutorial}, for which some
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    92
  familiarity is assumed.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    93
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    94
  Here we merely present the look-and-feel for end users.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    95
  Internally, those are mapped to more primitive Isabelle concepts.
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
    96
  See \cite{Haftmann-Wenzel:2006:classes} for more detail.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
    97
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
    98
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
    99
section {* A simple algebra example \label{sec:example} *}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   100
75b56e51fade initial draft
haftmann
parents:
diff changeset
   101
subsection {* Class definition *}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   102
75b56e51fade initial draft
haftmann
parents:
diff changeset
   103
text {*
75b56e51fade initial draft
haftmann
parents:
diff changeset
   104
  Depending on an arbitrary type @{text "\<alpha>"}, class @{text
28565
haftmann
parents: 28540
diff changeset
   105
  "semigroup"} introduces a binary operator @{text "(\<otimes>)"} that is
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   106
  assumed to be associative:
75b56e51fade initial draft
haftmann
parents:
diff changeset
   107
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   108
28565
haftmann
parents: 28540
diff changeset
   109
class %quote semigroup = type +
haftmann
parents: 28540
diff changeset
   110
  fixes mult :: "\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"    (infixl "\<otimes>" 70)
haftmann
parents: 28540
diff changeset
   111
  assumes assoc: "(x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   112
75b56e51fade initial draft
haftmann
parents:
diff changeset
   113
text {*
28565
haftmann
parents: 28540
diff changeset
   114
  \noindent This @{command class} specification consists of two
haftmann
parents: 28540
diff changeset
   115
  parts: the \qn{operational} part names the class parameter
haftmann
parents: 28540
diff changeset
   116
  (@{element "fixes"}), the \qn{logical} part specifies properties on them
haftmann
parents: 28540
diff changeset
   117
  (@{element "assumes"}).  The local @{element "fixes"} and
haftmann
parents: 28540
diff changeset
   118
  @{element "assumes"} are lifted to the theory toplevel,
haftmann
parents: 28540
diff changeset
   119
  yielding the global
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   120
  parameter @{term [source] "mult \<Colon> \<alpha>\<Colon>semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"} and the
28565
haftmann
parents: 28540
diff changeset
   121
  global theorem @{fact "semigroup.assoc:"}~@{prop [source] "\<And>x y
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   122
  z \<Colon> \<alpha>\<Colon>semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"}.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   123
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   124
75b56e51fade initial draft
haftmann
parents:
diff changeset
   125
75b56e51fade initial draft
haftmann
parents:
diff changeset
   126
subsection {* Class instantiation \label{sec:class_inst} *}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   127
75b56e51fade initial draft
haftmann
parents:
diff changeset
   128
text {*
28565
haftmann
parents: 28540
diff changeset
   129
  The concrete type @{typ int} is made a @{class semigroup}
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   130
  instance by providing a suitable definition for the class parameter
28565
haftmann
parents: 28540
diff changeset
   131
  @{text "(\<otimes>)"} and a proof for the specification of @{fact assoc}.
haftmann
parents: 28540
diff changeset
   132
  This is accomplished by the @{command instantiation} target:
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   133
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   134
28565
haftmann
parents: 28540
diff changeset
   135
instantiation %quote int :: semigroup
haftmann
parents: 28540
diff changeset
   136
begin
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   137
28565
haftmann
parents: 28540
diff changeset
   138
definition %quote
haftmann
parents: 28540
diff changeset
   139
  mult_int_def: "i \<otimes> j = i + (j\<Colon>int)"
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   140
28565
haftmann
parents: 28540
diff changeset
   141
instance %quote proof
haftmann
parents: 28540
diff changeset
   142
  fix i j k :: int have "(i + j) + k = i + (j + k)" by simp
haftmann
parents: 28540
diff changeset
   143
  then show "(i \<otimes> j) \<otimes> k = i \<otimes> (j \<otimes> k)"
haftmann
parents: 28540
diff changeset
   144
  unfolding mult_int_def .
haftmann
parents: 28540
diff changeset
   145
qed
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   146
28565
haftmann
parents: 28540
diff changeset
   147
end %quote
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   148
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   149
text {*
28565
haftmann
parents: 28540
diff changeset
   150
  \noindent @{command instantiation} allows to define class parameters
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   151
  at a particular instance using common specification tools (here,
28565
haftmann
parents: 28540
diff changeset
   152
  @{command definition}).  The concluding @{command instance}
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   153
  opens a proof that the given parameters actually conform
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   154
  to the class specification.  Note that the first proof step
28565
haftmann
parents: 28540
diff changeset
   155
  is the @{method default} method,
haftmann
parents: 28540
diff changeset
   156
  which for such instance proofs maps to the @{method intro_classes} method.
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   157
  This boils down an instance judgement to the relevant primitive
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   158
  proof goals and should conveniently always be the first method applied
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   159
  in an instantiation proof.
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   160
28565
haftmann
parents: 28540
diff changeset
   161
  From now on, the type-checker will consider @{typ int}
haftmann
parents: 28540
diff changeset
   162
  as a @{class semigroup} automatically, i.e.\ any general results
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   163
  are immediately available on concrete instances.
28565
haftmann
parents: 28540
diff changeset
   164
haftmann
parents: 28540
diff changeset
   165
  \medskip Another instance of @{class semigroup} are the natural numbers:
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   166
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   167
28565
haftmann
parents: 28540
diff changeset
   168
instantiation %quote nat :: semigroup
haftmann
parents: 28540
diff changeset
   169
begin
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   170
28565
haftmann
parents: 28540
diff changeset
   171
primrec %quote mult_nat where
haftmann
parents: 28540
diff changeset
   172
  "(0\<Colon>nat) \<otimes> n = n"
haftmann
parents: 28540
diff changeset
   173
  | "Suc m \<otimes> n = Suc (m \<otimes> n)"
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   174
28565
haftmann
parents: 28540
diff changeset
   175
instance %quote proof
haftmann
parents: 28540
diff changeset
   176
  fix m n q :: nat 
haftmann
parents: 28540
diff changeset
   177
  show "m \<otimes> n \<otimes> q = m \<otimes> (n \<otimes> q)"
haftmann
parents: 28540
diff changeset
   178
    by (induct m) auto
haftmann
parents: 28540
diff changeset
   179
qed
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   180
28565
haftmann
parents: 28540
diff changeset
   181
end %quote
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   182
25871
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   183
text {*
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   184
  \noindent Note the occurence of the name @{text mult_nat}
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   185
  in the primrec declaration;  by default, the local name of
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   186
  a class operation @{text f} to instantiate on type constructor
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   187
  @{text \<kappa>} are mangled as @{text f_\<kappa>}.  In case of uncertainty,
28565
haftmann
parents: 28540
diff changeset
   188
  these names may be inspected using the @{command "print_context"} command
25871
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   189
  or the corresponding ProofGeneral button.
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   190
*}
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   191
25247
haftmann
parents: 25200
diff changeset
   192
subsection {* Lifting and parametric types *}
haftmann
parents: 25200
diff changeset
   193
haftmann
parents: 25200
diff changeset
   194
text {*
haftmann
parents: 25200
diff changeset
   195
  Overloaded definitions giving on class instantiation
haftmann
parents: 25200
diff changeset
   196
  may include recursion over the syntactic structure of types.
haftmann
parents: 25200
diff changeset
   197
  As a canonical example, we model product semigroups
haftmann
parents: 25200
diff changeset
   198
  using our simple algebra:
haftmann
parents: 25200
diff changeset
   199
*}
haftmann
parents: 25200
diff changeset
   200
28565
haftmann
parents: 28540
diff changeset
   201
instantiation %quote * :: (semigroup, semigroup) semigroup
haftmann
parents: 28540
diff changeset
   202
begin
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   203
28565
haftmann
parents: 28540
diff changeset
   204
definition %quote
haftmann
parents: 28540
diff changeset
   205
  mult_prod_def: "p\<^isub>1 \<otimes> p\<^isub>2 = (fst p\<^isub>1 \<otimes> fst p\<^isub>2, snd p\<^isub>1 \<otimes> snd p\<^isub>2)"
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   206
28565
haftmann
parents: 28540
diff changeset
   207
instance %quote proof
haftmann
parents: 28540
diff changeset
   208
  fix p\<^isub>1 p\<^isub>2 p\<^isub>3 :: "\<alpha>\<Colon>semigroup \<times> \<beta>\<Colon>semigroup"
haftmann
parents: 28540
diff changeset
   209
  show "p\<^isub>1 \<otimes> p\<^isub>2 \<otimes> p\<^isub>3 = p\<^isub>1 \<otimes> (p\<^isub>2 \<otimes> p\<^isub>3)"
haftmann
parents: 28540
diff changeset
   210
  unfolding mult_prod_def by (simp add: assoc)
haftmann
parents: 28540
diff changeset
   211
qed      
25247
haftmann
parents: 25200
diff changeset
   212
28565
haftmann
parents: 28540
diff changeset
   213
end %quote
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   214
25247
haftmann
parents: 25200
diff changeset
   215
text {*
haftmann
parents: 25200
diff changeset
   216
  \noindent Associativity from product semigroups is
haftmann
parents: 25200
diff changeset
   217
  established using
28565
haftmann
parents: 28540
diff changeset
   218
  the definition of @{text "(\<otimes>)"} on products and the hypothetical
27505
ddd1e71adbfc fix more typos
huffman
parents: 27504
diff changeset
   219
  associativity of the type components;  these hypotheses
28565
haftmann
parents: 28540
diff changeset
   220
  are facts due to the @{class semigroup} constraints imposed
haftmann
parents: 28540
diff changeset
   221
  on the type components by the @{command instance} proposition.
25247
haftmann
parents: 25200
diff changeset
   222
  Indeed, this pattern often occurs with parametric types
haftmann
parents: 25200
diff changeset
   223
  and type classes.
haftmann
parents: 25200
diff changeset
   224
*}
haftmann
parents: 25200
diff changeset
   225
haftmann
parents: 25200
diff changeset
   226
haftmann
parents: 25200
diff changeset
   227
subsection {* Subclassing *}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   228
75b56e51fade initial draft
haftmann
parents:
diff changeset
   229
text {*
28565
haftmann
parents: 28540
diff changeset
   230
  We define a subclass @{text monoidl} (a semigroup with a left-hand neutral)
haftmann
parents: 28540
diff changeset
   231
  by extending @{class semigroup}
haftmann
parents: 28540
diff changeset
   232
  with one additional parameter @{text neutral} together
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   233
  with its property:
75b56e51fade initial draft
haftmann
parents:
diff changeset
   234
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   235
28565
haftmann
parents: 28540
diff changeset
   236
class %quote monoidl = semigroup +
haftmann
parents: 28540
diff changeset
   237
  fixes neutral :: "\<alpha>" ("\<one>")
haftmann
parents: 28540
diff changeset
   238
  assumes neutl: "\<one> \<otimes> x = x"
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   239
75b56e51fade initial draft
haftmann
parents:
diff changeset
   240
text {*
25247
haftmann
parents: 25200
diff changeset
   241
  \noindent Again, we prove some instances, by
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   242
  providing suitable parameter definitions and proofs for the
27505
ddd1e71adbfc fix more typos
huffman
parents: 27504
diff changeset
   243
  additional specifications.  Observe that instantiations
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   244
  for types with the same arity may be simultaneous:
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   245
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   246
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   247
    instantiation nat and int :: monoidl
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   248
    begin
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   249
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   250
    definition
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   251
      neutral_nat_def: "\<one> = (0\<Colon>nat)"
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   252
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   253
    definition
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   254
      neutral_int_def: "\<one> = (0\<Colon>int)"
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   255
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   256
    instance proof
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   257
      fix n :: nat
25247
haftmann
parents: 25200
diff changeset
   258
      show "\<one> \<otimes> n = n"
26967
27f60aaa5a7b simplified proof
haftmann
parents: 25871
diff changeset
   259
	unfolding neutral_nat_def by simp
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   260
    next
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   261
      fix k :: int
25247
haftmann
parents: 25200
diff changeset
   262
      show "\<one> \<otimes> k = k"
haftmann
parents: 25200
diff changeset
   263
	unfolding neutral_int_def mult_int_def by simp
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   264
    qed
75b56e51fade initial draft
haftmann
parents:
diff changeset
   265
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   266
    end
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   267
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   268
    instantiation * :: (monoidl, monoidl) monoidl
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   269
    begin
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   270
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   271
    definition
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   272
      neutral_prod_def: "\<one> = (\<one>, \<one>)"
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   273
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   274
    instance proof
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   275
      fix p :: "\<alpha>\<Colon>monoidl \<times> \<beta>\<Colon>monoidl"
25247
haftmann
parents: 25200
diff changeset
   276
      show "\<one> \<otimes> p = p"
haftmann
parents: 25200
diff changeset
   277
	unfolding neutral_prod_def mult_prod_def by (simp add: neutl)
haftmann
parents: 25200
diff changeset
   278
    qed
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   279
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   280
   end
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   281
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   282
text {*
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   283
  \noindent Fully-fledged monoids are modelled by another subclass
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   284
  which does not add new parameters but tightens the specification:
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   285
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   286
75b56e51fade initial draft
haftmann
parents:
diff changeset
   287
    class monoid = monoidl +
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   288
      assumes neutr: "x \<otimes> \<one> = x"
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   289
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   290
    instantiation nat and int :: monoid 
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   291
    begin
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   292
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   293
    instance proof
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   294
      fix n :: nat
25247
haftmann
parents: 25200
diff changeset
   295
      show "n \<otimes> \<one> = n"
25871
45753d56d935 some more primrec
haftmann
parents: 25868
diff changeset
   296
	unfolding neutral_nat_def by (induct n) simp_all
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   297
    next
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   298
      fix k :: int
25247
haftmann
parents: 25200
diff changeset
   299
      show "k \<otimes> \<one> = k"
haftmann
parents: 25200
diff changeset
   300
	unfolding neutral_int_def mult_int_def by simp
haftmann
parents: 25200
diff changeset
   301
    qed
haftmann
parents: 25200
diff changeset
   302
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   303
    end
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   304
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   305
    instantiation * :: (monoid, monoid) monoid
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   306
    begin
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   307
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   308
    instance proof 
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   309
      fix p :: "\<alpha>\<Colon>monoid \<times> \<beta>\<Colon>monoid"
25247
haftmann
parents: 25200
diff changeset
   310
      show "p \<otimes> \<one> = p"
haftmann
parents: 25200
diff changeset
   311
	unfolding neutral_prod_def mult_prod_def by (simp add: neutr)
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   312
    qed
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   313
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   314
    end
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   315
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   316
text {*
28565
haftmann
parents: 28540
diff changeset
   317
  \noindent To finish our small algebra example, we add a @{text group} class
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   318
  with a corresponding instance:
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   319
*}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   320
75b56e51fade initial draft
haftmann
parents:
diff changeset
   321
    class group = monoidl +
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   322
      fixes inverse :: "\<alpha> \<Rightarrow> \<alpha>"    ("(_\<div>)" [1000] 999)
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   323
      assumes invl: "x\<div> \<otimes> x = \<one>"
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   324
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   325
    instantiation int :: group
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   326
    begin
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   327
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   328
    definition
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   329
      inverse_int_def: "i\<div> = - (i\<Colon>int)"
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   330
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   331
    instance proof
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   332
      fix i :: int
75b56e51fade initial draft
haftmann
parents:
diff changeset
   333
      have "-i + i = 0" by simp
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   334
      then show "i\<div> \<otimes> i = \<one>"
25247
haftmann
parents: 25200
diff changeset
   335
	unfolding mult_int_def neutral_int_def inverse_int_def .
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   336
    qed
75b56e51fade initial draft
haftmann
parents:
diff changeset
   337
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   338
    end
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   339
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   340
section {* Type classes as locales *}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   341
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   342
subsection {* A look behind the scene *}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   343
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   344
text {*
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   345
  The example above gives an impression how Isar type classes work
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   346
  in practice.  As stated in the introduction, classes also provide
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   347
  a link to Isar's locale system.  Indeed, the logical core of a class
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   348
  is nothing else than a locale:
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   349
*}
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   350
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22347
diff changeset
   351
class idem = type +
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   352
  fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   353
  assumes idem: "f (f x) = f x"
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   354
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   355
text {*
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   356
  \noindent essentially introduces the locale
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   357
*}
28565
haftmann
parents: 28540
diff changeset
   358
haftmann
parents: 28540
diff changeset
   359
setup %invisible {* Sign.add_path "foo" *}
haftmann
parents: 28540
diff changeset
   360
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   361
locale idem =
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   362
  fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   363
  assumes idem: "f (f x) = f x"
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   364
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   365
text {* \noindent together with corresponding constant(s): *}
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   366
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   367
consts f :: "\<alpha> \<Rightarrow> \<alpha>"
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   368
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   369
text {*
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   370
  \noindent The connection to the type system is done by means
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   371
  of a primitive axclass
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   372
*}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   373
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   374
axclass idem < type
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   375
  idem: "f (f x) = f x"
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   376
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   377
text {* \noindent together with a corresponding interpretation: *}
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   378
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   379
interpretation idem_class:
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   380
  idem ["f \<Colon> (\<alpha>\<Colon>idem) \<Rightarrow> \<alpha>"]
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   381
by unfold_locales (rule idem)
28565
haftmann
parents: 28540
diff changeset
   382
haftmann
parents: 28540
diff changeset
   383
setup %invisible {* Sign.parent_path *}
haftmann
parents: 28540
diff changeset
   384
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   385
text {*
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   386
  This give you at hand the full power of the Isabelle module system;
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   387
  conclusions in locale @{text idem} are implicitly propagated
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   388
  to class @{text idem}.
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   389
*}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   390
75b56e51fade initial draft
haftmann
parents:
diff changeset
   391
subsection {* Abstract reasoning *}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   392
75b56e51fade initial draft
haftmann
parents:
diff changeset
   393
text {*
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   394
  Isabelle locales enable reasoning at a general level, while results
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   395
  are implicitly transferred to all instances.  For example, we can
75b56e51fade initial draft
haftmann
parents:
diff changeset
   396
  now establish the @{text "left_cancel"} lemma for groups, which
25247
haftmann
parents: 25200
diff changeset
   397
  states that the function @{text "(x \<otimes>)"} is injective:
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   398
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   399
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   400
    lemma (in group) left_cancel: "x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   401
    proof
25247
haftmann
parents: 25200
diff changeset
   402
      assume "x \<otimes> y = x \<otimes> z"
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   403
      then have "x\<div> \<otimes> (x \<otimes> y) = x\<div> \<otimes> (x \<otimes> z)" by simp
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   404
      then have "(x\<div> \<otimes> x) \<otimes> y = (x\<div> \<otimes> x) \<otimes> z" using assoc by simp
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   405
      then show "y = z" using neutl and invl by simp
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   406
    next
25247
haftmann
parents: 25200
diff changeset
   407
      assume "y = z"
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   408
      then show "x \<otimes> y = x \<otimes> z" by simp
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   409
    qed
75b56e51fade initial draft
haftmann
parents:
diff changeset
   410
75b56e51fade initial draft
haftmann
parents:
diff changeset
   411
text {*
28565
haftmann
parents: 28540
diff changeset
   412
  \noindent Here the \qt{@{keyword "in"} @{class group}} target specification
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   413
  indicates that the result is recorded within that context for later
28565
haftmann
parents: 28540
diff changeset
   414
  use.  This local theorem is also lifted to the global one @{fact
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   415
  "group.left_cancel:"} @{prop [source] "\<And>x y z \<Colon> \<alpha>\<Colon>group. x \<otimes> y = x \<otimes>
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   416
  z \<longleftrightarrow> y = z"}.  Since type @{text "int"} has been made an instance of
75b56e51fade initial draft
haftmann
parents:
diff changeset
   417
  @{text "group"} before, we may refer to that fact as well: @{prop
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   418
  [source] "\<And>x y z \<Colon> int. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"}.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   419
*}
75b56e51fade initial draft
haftmann
parents:
diff changeset
   420
75b56e51fade initial draft
haftmann
parents:
diff changeset
   421
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   422
subsection {* Derived definitions *}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   423
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   424
text {*
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   425
  Isabelle locales support a concept of local definitions
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   426
  in locales:
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   427
*}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   428
28540
haftmann
parents: 27505
diff changeset
   429
    primrec (in monoid) pow_nat :: "nat \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   430
      "pow_nat 0 x = \<one>"
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   431
      | "pow_nat (Suc n) x = x \<otimes> pow_nat n x"
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   432
75b56e51fade initial draft
haftmann
parents:
diff changeset
   433
text {*
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   434
  \noindent If the locale @{text group} is also a class, this local
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   435
  definition is propagated onto a global definition of
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   436
  @{term [source] "pow_nat \<Colon> nat \<Rightarrow> \<alpha>\<Colon>monoid \<Rightarrow> \<alpha>\<Colon>monoid"}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   437
  with corresponding theorems
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   438
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   439
  @{thm pow_nat.simps [no_vars]}.
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   440
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   441
  \noindent As you can see from this example, for local
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   442
  definitions you may use any specification tool
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   443
  which works together with locales (e.g. \cite{krauss2006}).
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   444
*}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   445
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   446
25247
haftmann
parents: 25200
diff changeset
   447
subsection {* A functor analogy *}
haftmann
parents: 25200
diff changeset
   448
haftmann
parents: 25200
diff changeset
   449
text {*
haftmann
parents: 25200
diff changeset
   450
  We introduced Isar classes by analogy to type classes
haftmann
parents: 25200
diff changeset
   451
  functional programming;  if we reconsider this in the
haftmann
parents: 25200
diff changeset
   452
  context of what has been said about type classes and locales,
haftmann
parents: 25200
diff changeset
   453
  we can drive this analogy further by stating that type
haftmann
parents: 25200
diff changeset
   454
  classes essentially correspond to functors which have
haftmann
parents: 25200
diff changeset
   455
  a canonical interpretation as type classes.
haftmann
parents: 25200
diff changeset
   456
  Anyway, there is also the possibility of other interpretations.
28565
haftmann
parents: 28540
diff changeset
   457
  For example, also @{text list}s form a monoid with
haftmann
parents: 28540
diff changeset
   458
  @{text append} and @{term "[]"} as operations, but it
25247
haftmann
parents: 25200
diff changeset
   459
  seems inappropriate to apply to lists
27505
ddd1e71adbfc fix more typos
huffman
parents: 27504
diff changeset
   460
  the same operations as for genuinely algebraic types.
25247
haftmann
parents: 25200
diff changeset
   461
  In such a case, we simply can do a particular interpretation
haftmann
parents: 25200
diff changeset
   462
  of monoids for lists:
haftmann
parents: 25200
diff changeset
   463
*}
haftmann
parents: 25200
diff changeset
   464
28565
haftmann
parents: 28540
diff changeset
   465
    interpretation list_monoid: monoid [append "[]"]
25247
haftmann
parents: 25200
diff changeset
   466
      by unfold_locales auto
haftmann
parents: 25200
diff changeset
   467
haftmann
parents: 25200
diff changeset
   468
text {*
haftmann
parents: 25200
diff changeset
   469
  \noindent This enables us to apply facts on monoids
haftmann
parents: 25200
diff changeset
   470
  to lists, e.g. @{thm list_monoid.neutl [no_vars]}.
haftmann
parents: 25200
diff changeset
   471
haftmann
parents: 25200
diff changeset
   472
  When using this interpretation pattern, it may also
haftmann
parents: 25200
diff changeset
   473
  be appropriate to map derived definitions accordingly:
haftmann
parents: 25200
diff changeset
   474
*}
haftmann
parents: 25200
diff changeset
   475
28540
haftmann
parents: 27505
diff changeset
   476
    fun replicate :: "nat \<Rightarrow> \<alpha> list \<Rightarrow> \<alpha> list" where
25247
haftmann
parents: 25200
diff changeset
   477
      "replicate 0 _ = []"
haftmann
parents: 25200
diff changeset
   478
      | "replicate (Suc n) xs = xs @ replicate n xs"
haftmann
parents: 25200
diff changeset
   479
28565
haftmann
parents: 28540
diff changeset
   480
    interpretation list_monoid: monoid [append "[]"] where
haftmann
parents: 28540
diff changeset
   481
      "monoid.pow_nat append [] = replicate"
28540
haftmann
parents: 27505
diff changeset
   482
    proof -
28565
haftmann
parents: 28540
diff changeset
   483
      interpret monoid [append "[]"] ..
haftmann
parents: 28540
diff changeset
   484
      show "monoid.pow_nat append [] = replicate"
28540
haftmann
parents: 27505
diff changeset
   485
      proof
haftmann
parents: 27505
diff changeset
   486
        fix n
28565
haftmann
parents: 28540
diff changeset
   487
        show "monoid.pow_nat append [] n = replicate n"
28540
haftmann
parents: 27505
diff changeset
   488
          by (induct n) auto
haftmann
parents: 27505
diff changeset
   489
      qed
haftmann
parents: 27505
diff changeset
   490
    qed intro_locales
25247
haftmann
parents: 25200
diff changeset
   491
haftmann
parents: 25200
diff changeset
   492
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   493
subsection {* Additional subclass relations *}
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   494
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   495
text {*
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   496
  Any @{text "group"} is also a @{text "monoid"};  this
25247
haftmann
parents: 25200
diff changeset
   497
  can be made explicit by claiming an additional
haftmann
parents: 25200
diff changeset
   498
  subclass relation,
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   499
  together with a proof of the logical difference:
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   500
*}
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   501
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   502
    subclass (in group) monoid
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   503
    proof unfold_locales
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   504
      fix x
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   505
      from invl have "x\<div> \<otimes> x = \<one>" by simp
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   506
      with assoc [symmetric] neutl invl have "x\<div> \<otimes> (x \<otimes> \<one>) = x\<div> \<otimes> x" by simp
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   507
      with left_cancel show "x \<otimes> \<one> = x" by simp
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   508
    qed
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   509
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   510
text {*
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   511
  \noindent The logical proof is carried out on the locale level
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   512
  and thus conveniently is opened using the @{text unfold_locales}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   513
  method which only leaves the logical differences still
25200
f1d2e106f2fe adjusted
haftmann
parents: 24991
diff changeset
   514
  open to proof to the user.  Afterwards it is propagated
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   515
  to the type system, making @{text group} an instance of
25247
haftmann
parents: 25200
diff changeset
   516
  @{text monoid} by adding an additional edge
haftmann
parents: 25200
diff changeset
   517
  to the graph of subclass relations
haftmann
parents: 25200
diff changeset
   518
  (cf.\ \figref{fig:subclass}).
haftmann
parents: 25200
diff changeset
   519
haftmann
parents: 25200
diff changeset
   520
  \begin{figure}[htbp]
haftmann
parents: 25200
diff changeset
   521
   \begin{center}
haftmann
parents: 25200
diff changeset
   522
     \small
haftmann
parents: 25200
diff changeset
   523
     \unitlength 0.6mm
haftmann
parents: 25200
diff changeset
   524
     \begin{picture}(40,60)(0,0)
haftmann
parents: 25200
diff changeset
   525
       \put(20,60){\makebox(0,0){@{text semigroup}}}
haftmann
parents: 25200
diff changeset
   526
       \put(20,40){\makebox(0,0){@{text monoidl}}}
haftmann
parents: 25200
diff changeset
   527
       \put(00,20){\makebox(0,0){@{text monoid}}}
haftmann
parents: 25200
diff changeset
   528
       \put(40,00){\makebox(0,0){@{text group}}}
haftmann
parents: 25200
diff changeset
   529
       \put(20,55){\vector(0,-1){10}}
haftmann
parents: 25200
diff changeset
   530
       \put(15,35){\vector(-1,-1){10}}
haftmann
parents: 25200
diff changeset
   531
       \put(25,35){\vector(1,-3){10}}
haftmann
parents: 25200
diff changeset
   532
     \end{picture}
haftmann
parents: 25200
diff changeset
   533
     \hspace{8em}
haftmann
parents: 25200
diff changeset
   534
     \begin{picture}(40,60)(0,0)
haftmann
parents: 25200
diff changeset
   535
       \put(20,60){\makebox(0,0){@{text semigroup}}}
haftmann
parents: 25200
diff changeset
   536
       \put(20,40){\makebox(0,0){@{text monoidl}}}
haftmann
parents: 25200
diff changeset
   537
       \put(00,20){\makebox(0,0){@{text monoid}}}
haftmann
parents: 25200
diff changeset
   538
       \put(40,00){\makebox(0,0){@{text group}}}
haftmann
parents: 25200
diff changeset
   539
       \put(20,55){\vector(0,-1){10}}
haftmann
parents: 25200
diff changeset
   540
       \put(15,35){\vector(-1,-1){10}}
haftmann
parents: 25200
diff changeset
   541
       \put(05,15){\vector(3,-1){30}}
haftmann
parents: 25200
diff changeset
   542
     \end{picture}
haftmann
parents: 25200
diff changeset
   543
     \caption{Subclass relationship of monoids and groups:
haftmann
parents: 25200
diff changeset
   544
        before and after establishing the relationship
haftmann
parents: 25200
diff changeset
   545
        @{text "group \<subseteq> monoid"};  transitive edges left out.}
haftmann
parents: 25200
diff changeset
   546
     \label{fig:subclass}
haftmann
parents: 25200
diff changeset
   547
   \end{center}
haftmann
parents: 25200
diff changeset
   548
  \end{figure}
haftmann
parents: 25200
diff changeset
   549
haftmann
parents: 25200
diff changeset
   550
  For illustration, a derived definition
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   551
  in @{text group} which uses @{text pow_nat}:
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   552
*}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   553
28565
haftmann
parents: 28540
diff changeset
   554
definition %quote (in group) pow_int :: "int \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
haftmann
parents: 28540
diff changeset
   555
  "pow_int k x = (if k >= 0
haftmann
parents: 28540
diff changeset
   556
    then pow_nat (nat k) x
haftmann
parents: 28540
diff changeset
   557
    else (pow_nat (nat (- k)) x)\<div>)"
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   558
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   559
text {*
25247
haftmann
parents: 25200
diff changeset
   560
  \noindent yields the global definition of
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   561
  @{term [source] "pow_int \<Colon> int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group"}
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   562
  with the corresponding theorem @{thm pow_int_def [no_vars]}.
24991
c6f5cc939c29 added subclass command
haftmann
parents: 24628
diff changeset
   563
*}
23956
48494ccfabaf updated
haftmann
parents: 22845
diff changeset
   564
25868
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   565
subsection {* A note on syntax *}
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   566
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   567
text {*
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   568
  As a commodity, class context syntax allows to refer
27505
ddd1e71adbfc fix more typos
huffman
parents: 27504
diff changeset
   569
  to local class operations and their global counterparts
25868
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   570
  uniformly;  type inference resolves ambiguities.  For example:
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   571
*}
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   572
28565
haftmann
parents: 28540
diff changeset
   573
context %quote semigroup
25868
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   574
begin
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   575
28565
haftmann
parents: 28540
diff changeset
   576
term %quote "x \<otimes> y" -- {* example 1 *}
haftmann
parents: 28540
diff changeset
   577
term %quote "(x\<Colon>nat) \<otimes> y" -- {* example 2 *}
25868
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   578
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   579
end
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   580
28565
haftmann
parents: 28540
diff changeset
   581
term %quote "x \<otimes> y" -- {* example 3 *}
25868
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   582
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   583
text {*
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   584
  \noindent Here in example 1, the term refers to the local class operation
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   585
  @{text "mult [\<alpha>]"}, whereas in example 2 the type constraint
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   586
  enforces the global class operation @{text "mult [nat]"}.
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   587
  In the global context in example 3, the reference is
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   588
  to the polymorphic global class operation @{text "mult [?\<alpha> \<Colon> semigroup]"}.
97c6787099bc a note on syntax
haftmann
parents: 25533
diff changeset
   589
*}
22347
ddbf185a3be0 continued
haftmann
parents: 22317
diff changeset
   590
25247
haftmann
parents: 25200
diff changeset
   591
section {* Type classes and code generation *}
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   592
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   593
text {*
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   594
  Turning back to the first motivation for type classes,
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   595
  namely overloading, it is obvious that overloading
28565
haftmann
parents: 28540
diff changeset
   596
  stemming from @{command class} statements and
haftmann
parents: 28540
diff changeset
   597
  @{command instantiation}
25533
0140cc7b26ad added something about instantiation target
haftmann
parents: 25369
diff changeset
   598
  targets naturally maps to Haskell type classes.
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   599
  The code generator framework \cite{isabelle-codegen} 
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   600
  takes this into account.  Concerning target languages
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   601
  lacking type classes (e.g.~SML), type classes
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   602
  are implemented by explicit dictionary construction.
28540
haftmann
parents: 27505
diff changeset
   603
  As example, let's go back to the power function:
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   604
*}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   605
28565
haftmann
parents: 28540
diff changeset
   606
definition %quote example :: int where
haftmann
parents: 28540
diff changeset
   607
  "example = pow_int 10 (-2)"
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   608
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   609
text {*
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   610
  \noindent This maps to Haskell as:
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   611
*}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   612
28565
haftmann
parents: 28540
diff changeset
   613
text %quote {*@{code_stmts example (Haskell)}*}
22317
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   614
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   615
text {*
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   616
  \noindent The whole code in SML with explicit dictionary passing:
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   617
*}
b550d2c6ca90 continued class tutorial
haftmann
parents: 20946
diff changeset
   618
28565
haftmann
parents: 28540
diff changeset
   619
text %quote {*@{code_stmts example (SML)}*}
20946
75b56e51fade initial draft
haftmann
parents:
diff changeset
   620
75b56e51fade initial draft
haftmann
parents:
diff changeset
   621
end