-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter04-type-classes.tex
More file actions
89 lines (61 loc) · 2.72 KB
/
chapter04-type-classes.tex
File metadata and controls
89 lines (61 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
%!TEX root = fp.tex
% Author: Philipp Moers <soziflip funny character gmail dot com>
\chapter{Type Classes} % (fold)
\label{cha:type_classes}
A \textbf{type class} C defines a family of type signatures („methods“) which all \textbf{instances} of C must implement.
\begin{codebox}[haskell]
class C a where
f@$_1$@ :: t@$_1$@
@\dots@
f@$_n$@ :: t@$_n$@
\end{codebox}
The \codeline{t$_i$} \underline{must} mention a.\\
For any \codeline{f$_i$} the class may provide default implementations. \\
We have \codeline{f$_i$ :: C a => t$_i$} \\ (read „if a is instance C then f$_i$ has type t$_i$“).\\
\codeline{C a} is called \textbf{class constraint}.
\textit{Example}:
\begin{codebox}[haskell]
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y)
\end{codebox}
(These are default implementations. To redefine one of them is sufficient.)
% \codefile{haskell}{caption={type-classes.hs}, label=type-classes.hs}{../material/type-classes.hs}
\section{Class Inheritance}
\begin{itemize}
\item Defining \codeline{class (c$_1$ a, c$_2$ a, \dots) => C a where \dots} makes type class C a subclass of the C$_i$.\\
\item \codeline{C a => t} implies \codeline{C$_1$ a, C$_2$ a \dots}.
\end{itemize}
% \vspace{9pt}
% \includegraphics[scale=1.0]{type-class-inheritance.png}
% \textcolor{myorange}{[Here is an image missing]}
% \vspace{9pt}
\section{Class Instances}
If type t implements the methods of class C, t becomes an \textbf{instance of} C:
\begin{codebox}[haskell]
instance C t where
f@$_1$@ = <def of f@$_1$@>
@\dots@
f@$_n$@ = <def of f@$_n$@>
\end{codebox}
(All defs of f$_i$ may be provided, minimal complete definition \underline{must} be provided.)
Class constraint C t is satisfied from now on.
\textit{Example}:
\begin{codebox}[haskell]
instance Eq Bool where
x == y = x && y || (not x && not y)
\end{codebox}
An instance definition for type constructor t may formulate class constraints for its argument types a, b, \dots:\\
\codeline{instance (C$_1$ a, C$_2$ a, \dots) => C t where}
\codefile{haskell}{caption={rock-paper-scissor-inst.hs}, label=rock-paper-scissor-inst.hs}{../material/rock-paper-scissor-inst.hs}
\subsection{Deriving Class Instances}
Automatically make user-defined data types (\codeline{data \dots}) instances of classes C$_i$ $\in$ \{ Eq, Ord, Enum, Bounded, Show, Read \}:
\begin{codebox}[haskell]
data T a@$_1$@ a@$_1$@ @\dots@ a@$_n$@ = @\dots@
| @\dots@
deriving (C@$_1$@, C@$_2$@, @\dots@)
\end{codebox}
\codefile{haskell}{caption={rock-paper-scissor-inst-deriving.hs}, label=rock-paper-scissor-inst-deriving.hs}{../material/rock-paper-scissor-inst-deriving.hs_ASCII}
% chapter type_classes (end)