Skip to content

Commit

Permalink
Merge pull request #5 from bwignall/typeclass
Browse files Browse the repository at this point in the history
s/typeclass/type class
  • Loading branch information
bwignall authored Oct 30, 2019
2 parents 6b5d70a + 5f1c861 commit 50d0850
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 200 deletions.
2 changes: 1 addition & 1 deletion 1-getting-started.org
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ second case, we ask ~ghci~ to print the type of the expression
without actually evaluating it, so it does not have to be so
specific. It answers, in effect, "its type is numeric". We will
see more of this style of type annotation in
[[file:6-using-typeclasses.org][Chapter 6, /Using Typeclasses/]].
[[file:6-using-typeclasses.org][Chapter 6, Using Type Classes]].

** A simple program

Expand Down
6 changes: 3 additions & 3 deletions 10-parsing-a-binary-data-format.org
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ ghci> treeMap (odd . length) tree
Node (Leaf True) (Node (Leaf True) (Leaf False))
#+END_SRC

Haskell provides a well-known typeclass to further generalise
~treeMap~. This typeclass is named ~Functor~, and it defines one
Haskell provides a well-known type class to further generalise
~treeMap~. This type class is named ~Functor~, and it defines one
function, ~fmap~.

#+CAPTION: TreeMap.hs
Expand Down Expand Up @@ -748,7 +748,7 @@ instance Functor Bar where
#+END_SRC

This says that we can only put a type ~a~ into a ~Foo~ if ~a~ is a
member of the ~Eq~ typeclass. However, the constraint renders it
member of the ~Eq~ type class. However, the constraint renders it
impossible to write a ~Functor~ instance for ~Bar~.

#+BEGIN_SRC screen
Expand Down
2 changes: 1 addition & 1 deletion 11-testing-and-quality-assurance.org
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ tedious, and violates the moral code of the efficient functional
programmer: let the machine do the work! To automate this the
QuickCheck library comes with a set of data generators for all the
basic Haskell data types. QuickCheck uses the ~Arbitrary~
typeclass to present a uniform interface to (pseudo-)random data
type class to present a uniform interface to (pseudo-)random data
generation with the type system used to resolve which generator to
use. QuickCheck normally hides the data generation plumbing,
however we can also run the generators by hand to get a sense for
Expand Down
2 changes: 1 addition & 1 deletion 12-barcode-recognition.org
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ luminance (r,g,b) = round (r' * 0.30 + g' * 0.59 + b' * 0.11)
b' = fromIntegral b
#+END_SRC

Haskell arrays are members of the ~Functor~ typeclass, so we can
Haskell arrays are members of the ~Functor~ type class, so we can
simply use ~fmap~ to turn an entire image, or a single scanline,
from colour into greyscale.

Expand Down
14 changes: 7 additions & 7 deletions 13-data-structures.org
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ is. We've shown you a lot of ways to use that power. Here's a
chance to really see that in action.

Back in [[file:6-using-typeclasses.org::*Numeric Types][the section called "Numeric Types"]]
typeclasses that come with Haskell. Let's see what we can do by
defining new types and utilizing the numeric typeclasses to
type classes that come with Haskell. Let's see what we can do by
defining new types and utilizing the numeric type classes to
integrate them with basic mathematics in Haskell.

Let's start by thinking through what we'd like to see out of
Expand Down Expand Up @@ -701,7 +701,7 @@ as valid as a ~SymbolicManip Int~ as it as an ~Int~.
From here, then, our task is simple: extend the ~SymbolicManip~
type to be able to represent all the operations we will want to
perform, implement instances of it for the other numeric
typeclasses, and implement our own instance of ~Show~ for
type classes, and implement our own instance of ~Show~ for
~SymbolicManip~ that renders this tree in a more accessible
fashion.

Expand Down Expand Up @@ -942,7 +942,7 @@ Now it may become clear why we use a ~SymbolicManip~ instead of a
multiplication occur, the unit of measure also changes. For
instance, if we multiply 5 meters by 2 meters, we obtain 10 square
meters. We force the units for addition to match, and implement
subtraction in terms of addition. Let's look at more typeclass
subtraction in terms of addition. Let's look at more type class
instances for ~Units~.

#+CAPTION: num.hs
Expand Down Expand Up @@ -1334,7 +1334,7 @@ binary operator and zero as the identity value, integers form a
monoid. With multiplication as the binary operator and one as the
identity value, integers form a different monoid.

Monoids are ubiquitous in Haskell[fn:3]. The ~Monoid~ typeclass is
Monoids are ubiquitous in Haskell[fn:3]. The ~Monoid~ type class is
defined in the ~Data.Monoid~ module.

#+CAPTION: Monoid.hs
Expand Down Expand Up @@ -1545,7 +1545,7 @@ If we want to create a list from a ~Seq~, we must use the
import qualified Data.Foldable as Foldable
#+END_SRC

This module defines a typeclass, ~Foldable~, which ~Seq~
This module defines a type class, ~Foldable~, which ~Seq~
implements.

#+BEGIN_SRC screen
Expand Down Expand Up @@ -1574,7 +1574,7 @@ well.
** Footnotes

[fn:1] The type we use for the key must be a member of the ~Eq~
typeclass.
type class.

[fn:2] Non-strict evaluation makes the cost calculation
more subtle. We only pay for an append if we actually use the
Expand Down
4 changes: 2 additions & 2 deletions 14-using-parsec.org
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ $ runhaskell csv7.hs < test.csv
** Parsec and MonadPlus

Parsec's ~GenParser~ monad is an instance of the ~MonadPlus~
typeclass that we introduced in
type class that we introduced in
[[file:16-programming-with-monads.org::*Looking for alternatives][the section called "Looking for alternatives"]]
represents a parse failure, while ~mplus~ combines two alternative
parses into one, using ~(<|>)~.
Expand Down Expand Up @@ -786,7 +786,7 @@ effort of coming to grips with the idea.
** Applicative functors for parsing

The standard Haskell libraries include a module named
~Control.Applicative~, which defines a typeclass named
~Control.Applicative~, which defines a type class named
~Applicative~, which represents an /applicative functor/. Because
every applicative functor is also a functor they are represented
as a hierarchy.
Expand Down
28 changes: 14 additions & 14 deletions 15-monads.org
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ programming patterns have a monadic structure: passing around
implicit data, or short-circuiting a chain of evaluations if one
fails, to choose but two.

** The Monad typeclass
** The Monad type class

We can capture the notions of chaining and injection, and the
types that we want them to have, in a Haskell typeclass. The
standard ~Prelude~ already defines just such a typeclass, named
types that we want them to have, in a Haskell type class. The
standard ~Prelude~ already defines just such a type class, named
~Monad~.

#+BEGIN_SRC haskell
Expand Down Expand Up @@ -263,7 +263,7 @@ behavior to its name is that it /returns/ a pure value (of type
~a~) into a monad (of type ~m a~).

While ~(>>=)~ and ~return~ are the core functions of the ~Monad~
typeclass, it also defines two other functions. The first is
type class, it also defines two other functions. The first is
~(>>)~. Like ~(>>=)~, it performs chaining, but it ignores the
value on the left.

Expand Down Expand Up @@ -367,10 +367,10 @@ familiar with. These aren't formal terms, but they're in common
use, so it's helpful to know about them.

- "Monadic" simply means "pertaining to monads". A monadic /type/
is an instance of the ~Monad~ typeclass; a monadic /value/ has a
is an instance of the ~Monad~ type class; a monadic /value/ has a
monadic type.
- When we say that a type "is a monad", this is really a shorthand
way of saying that it's an instance of the ~Monad~ typeclass.
way of saying that it's an instance of the ~Monad~ type class.
Being an instance of ~Monad~ gives us the necessary monadic
triple of type constructor, injection function, and chaining
function.
Expand All @@ -388,7 +388,7 @@ use, so it's helpful to know about them.

In our introduction to monads, we showed how some pre-existing
code was already monadic in form. Now that we are beginning to
grasp what a monad is, and we've seen the ~Monad~ typeclass, let's
grasp what a monad is, and we've seen the ~Monad~ type class, let's
build a monad with foreknowledge of what we're doing. We'll start
out by defining its interface, then we'll put it to use. Once we
have those out of the way, we'll finally build it.
Expand Down Expand Up @@ -461,7 +461,7 @@ runLogger :: Logger a -> (a, Log)

*** Controlled escape

The ~Monad~ typeclass doesn't provide any means for values to
The ~Monad~ type class doesn't provide any means for values to
escape their monadic shackles. We can inject a value into a monad
using ~return~. We can extract a value from a monad using ~(>>=)~
but the function on the right, which can see an unwrapped value,
Expand Down Expand Up @@ -658,7 +658,7 @@ functor, calling the function on it, and rewrapping the result
with the same constructor.

We do exactly the same thing with a monad. Because the ~Monad~
typeclass already provides the ~(>>=)~ and ~return~ functions that
type class already provides the ~(>>=)~ and ~return~ functions that
know how to unwrap and wrap a value, the ~liftM~ function doesn't
need to know any details of a monad's implementation.

Expand All @@ -670,7 +670,7 @@ liftM f m = m >>= \i ->
#+END_SRC

When we declare a type to be an instance of the ~Functor~
typeclass, we have to write our own version of ~fmap~ specially
type class, we have to write our own version of ~fmap~ specially
tailored to that type. By contrast, ~liftM~ doesn't need to know
anything of a monad's internals, because they're abstracted by
~(>>=)~ and ~return~. We only need to write it once, with the
Expand Down Expand Up @@ -849,7 +849,7 @@ flavours, each with different levels of strictness.
Our ~Logger~ monad is a specialised version of the standard
~Writer~ monad, which can be found in the ~Control.Monad.Writer~
module of the ~mtl~ package. We will present a ~Writer~ example in
[[file:16-programming-with-monads.org::*Using typeclasses][the section called "Using typeclasses"]]
[[file:16-programming-with-monads.org::*Using type classes][the section called "Using type classes"]]

** The Maybe monad

Expand Down Expand Up @@ -1631,14 +1631,14 @@ rand = getStdRandom (randomR (0, maxBound))
(The ~randomR~ function takes an inclusive range within which the
generated random value should lie.)

The ~System.Random~ module provides a typeclass, ~RandomGen~, that
The ~System.Random~ module provides a type class, ~RandomGen~, that
lets us define new sources of random ~Int~ values. The type
~StdGen~ is the standard ~RandomGen~ instance. It generates
pseudorandom values. If we had an external source of truly random
data, we could make it an instance of ~RandomGen~ and get truly
random, instead of merely pseudorandom, values.

Another typeclass, ~Random~, indicates how to generate random
Another type class, ~Random~, indicates how to generate random
values of a particular type. The module defines ~Random~ instances
for all of the usual simple types.

Expand Down Expand Up @@ -1882,7 +1882,7 @@ conventionally named ~join~.
If we had definitions of ~join~ and ~fmap~, we wouldn't need to
write a definition of ~(>>=)~ for every monad, because it would be
completely generic. Here's what an alternative definition of the
~Monad~ typeclass might look like, along with a definition of
~Monad~ type class might look like, along with a definition of
~(>>=)~.

#+BEGIN_SRC haskell
Expand Down
Loading

0 comments on commit 50d0850

Please sign in to comment.