Skip to content

Commit 47ae2a5

Browse files
committed
Edits to Monoids chapter
1 parent 738975d commit 47ae2a5

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

src/pages/fps.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
paper: "us-trade"
1212
)
1313

14-
#set text(font: "EB Garamond 12", size: 12pt)
14+
#set text(font: "EB Garamond", size: 12pt)
1515

1616
#show raw.where(block: true): set block(fill: rgb("F7F7F7"), inset: 8pt, width: 100%)
1717
#show raw.where(block: true): set text(size: 8pt)

src/pages/monoids/applications.typ

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ For example,
4040
one machine may receive an update that other machines did not receive.
4141
We would like to reconcile these different views,
4242
so every machine has the same data if no more updates arrive.
43-
This is called _eventual consistency_.
43+
This is called *eventual consistency*.
4444

4545
A particular class of data types support this reconciliation.
4646
These data types are called conflict-free replicated data types (CRDTs).
@@ -49,6 +49,7 @@ with a result that captures all the information in both instances.
4949
This operation relies on having a monoid instance.
5050
We explore this idea further in the CRDT case study.
5151

52+
5253
=== Monoids in the Small
5354

5455

src/pages/monoids/cats.typ

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ or just
2828
import cats.*
2929
```
3030

31-
#info[
32-
==== Cats Kernel? {-}
33-
34-
31+
#info(title: "Cats Kernel?")[
3532
Cats Kernel is a subproject of Cats
3633
providing a small set of typeclasses
3734
for libraries that don't require the full Cats toolbox.
@@ -61,29 +58,28 @@ For example, if we want the monoid instance for `String`,
6158
and we have the correct given instances in scope,
6259
we can write the following:
6360

64-
```scala mdoc:silent
65-
import cats.Monoid
66-
```
67-
6861
```scala mdoc
62+
import cats.Monoid
6963
Monoid[String].combine("Hi ", "there")
7064
Monoid[String].empty
7165
```
7266

73-
which is equivalent to:
67+
which is equivalent to
7468

7569
```scala mdoc
7670
Monoid.apply[String].combine("Hi ", "there")
7771
Monoid.apply[String].empty
7872
```
7973

8074
As we know, `Monoid` extends `Semigroup`.
81-
If we don't need `empty` we can equivalently write:
75+
If we don't need `empty` we can instead write
8276

8377
```scala mdoc:silent
8478
import cats.Semigroup
8579
```
8680

81+
and then summon instances of `Semigroup` in the usual way:
82+
8783
```scala mdoc
8884
Semigroup[String].combine("Hi ", "there")
8985
```
@@ -106,10 +102,12 @@ we access the syntax by importing from #href("http://typelevel.org/cats/api/cats
106102
import cats.syntax.semigroup.* // for |+|
107103
```
108104

105+
Now we can use `|+|` in place of calling `combine`.
106+
109107
```scala mdoc
110108
val stringResult = "Hi " |+| "there" |+| Monoid[String].empty
111109
112-
val intResult = 1 |+| 2 |+| Monoid[Int].empty
110+
val intResult = 1 |+| 2
113111
```
114112

115113
As always,

src/pages/monoids/index.typ

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ There are instances for `Ints`, `Strings`, `Lists`, `Options`, and many more.
88
Let's start by looking at a few simple types and operations
99
to see what common principles we can extract.
1010

11-
==== Integer addition
12-
13-
1411
Addition of `Ints` is a binary operation that is _closed_,
1512
meaning that adding two `Ints` always produces another `Int`:
1613

@@ -28,8 +25,8 @@ that `a + 0 == 0 + a == a` for any `Int` `a`:
2825
```
2926

3027
There are also other properties of addition.
31-
For instance, it doesn't matter in what order we add elements
32-
because we always get the same result.
28+
For instance, it doesn't matter in where we place brackets when we add elements,
29+
as we always get the same result.
3330
This is a property known as _associativity_:
3431

3532
```scala mdoc
@@ -39,9 +36,6 @@ This is a property known as _associativity_:
3936
```
4037

4138

42-
==== Integer multiplication
43-
44-
4539
The same properties for addition also apply for multiplication,
4640
provided we use `1` as the identity instead of `0`:
4741

@@ -60,9 +54,6 @@ Multiplication, like addition, is associative:
6054
```
6155

6256

63-
==== String and sequence concatenation
64-
65-
6657
We can also add `Strings`,
6758
using string concatenation as our binary operator:
6859

@@ -146,11 +137,12 @@ is not a monoid because subtraction is not associative:
146137

147138
In practice we only need to think about laws
148139
when we are writing our own `Monoid` instances.
149-
Unlawful instances are dangerous because
150-
they can yield unpredictable results
140+
Unlawful instances are dangerous,
141+
not because using them can cause us to end up in jail,
142+
but because they can yield unpredictable results
151143
when used with the rest of Cats' machinery.
152144
Most of the time we can rely on the instances provided by Cats
153-
and assume the library authors know what they're doing.
145+
and assume the library authors knew what they were doing.
154146

155147

156148
== Definition of a Semigroup
@@ -165,6 +157,7 @@ sequence concatenation and integer addition are monoids.
165157
However, if we restrict ourselves
166158
to non-empty sequences and positive integers,
167159
we are no longer able to define a sensible `empty` element.
160+
As a concrete example,
168161
Cats has a #href("http://typelevel.org/cats/api/cats/data/NonEmptyList.html")[`NonEmptyList`] data type
169162
that has an implementation of `Semigroup` but no implementation of `Monoid`.
170163

@@ -214,7 +207,7 @@ object Monoid {
214207

215208
#solution[
216209
There are at least four monoids for `Boolean`!
217-
First, we have _and_ with operator `&&` and identity `true`:
210+
First, we have *and* with operator `&&` and identity `true`:
218211

219212
```scala mdoc:silent
220213
given booleanAndMonoid: Monoid[Boolean] with {
@@ -223,7 +216,7 @@ given booleanAndMonoid: Monoid[Boolean] with {
223216
}
224217
```
225218

226-
Second, we have _or_ with operator `||` and identity `false`:
219+
Second, we have *or* with operator `||` and identity `false`:
227220

228221
```scala mdoc:silent
229222
given booleanOrMonoid: Monoid[Boolean] with {
@@ -232,7 +225,7 @@ given booleanOrMonoid: Monoid[Boolean] with {
232225
}
233226
```
234227

235-
Third, we have _exclusive or_ with identity `false`:
228+
Third, we have *exclusive or* with identity `false`:
236229

237230
```scala mdoc:silent
238231
given booleanEitherMonoid: Monoid[Boolean] with {
@@ -243,7 +236,7 @@ given booleanEitherMonoid: Monoid[Boolean] with {
243236
}
244237
```
245238

246-
Finally, we have _exclusive nor_ (the negation of exclusive or)
239+
Finally, we have *exclusive nor* (the negation of exclusive or)
247240
with identity `true`:
248241

249242
```scala mdoc:silent
@@ -302,7 +295,7 @@ given setIntersectionSemigroup[A]: Semigroup[Set[A]] with {
302295
```
303296

304297
Set complement and set difference are not associative,
305-
so they cannot be considered for either monoids or semigroups.
298+
so they cannot be either monoids or semigroups.
306299
However, symmetric difference (the union less the intersection)
307300
does form a monoid with the empty set:
308301

src/pages/monoids/summary.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ addAll(List(None, Some(1), Some(2)))
6464
They're easy to understand and simple to use.
6565
However, they're just the tip of the iceberg
6666
in terms of the abstractions Cats enables us to make.
67-
In the next chapter we'll look at _functors_,
67+
In the next chapter we'll look at *functors*,
6868
the type class personification of the beloved `map` method.
6969
That's where the fun really begins!

0 commit comments

Comments
 (0)