liveBook · Manning

Mempty

As explained in Learn Haskell by Example

mempty is the Haskell Monoid value that acts as the neutral element for the monoidal operation.

mempty is the neutral value of a Haskell Monoid. Combining it with another value leaves that value unchanged: mappend mempty a == a and mappend a mempty == a. section 7.3.1

Lists and strings

For lists, mempty is []. Thus [1,2,3] <> mempty <> [4,5,6] produces [1,2,3,4,5,6]. Strings behave similarly because they are lists. section 7.3.1

A neutral Csv value

A custom Csv Monoid uses appendCsv for <> and represents mempty with Csv {csvHeader = Nothing, csvColumns = []}: a CSV with no header and no columns. section 7.3.1

Listing 7.10 Instances for Semigroup and Monoid for the Csv type
instance Semigroup Csv where
  (<>) = appendCsv  #1

instance Monoid Csv where
  mempty = Csv {csvHeader = Nothing, csvColumns = []}  #2

Inspect the Csv Semigroup and Monoid instances, including the empty Csv value selected for mempty. section 7.3.1

How Learn Haskell by Example covers it

How deep it goes
Defines identity laws and implements mempty for lists, strings, and Csv, without exploring compiler verification.
Examples
Demonstrates mempty as [] for lists, the empty string-like list identity, and an empty Csv value.
What it assumes
Assumes familiarity with binary operations and type classes while introducing Monoid through Semigroup and concrete instances.
What you can do after
Apply it via Haskell's Monoid interface when defining neutral values for composable data types.
How it connects
Connects mempty to Monoid, Semigroup, mappend, mconcat, the <> operator, and custom Csv combination.

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.