liveBook · Manning

Eq

As explained in Learn Haskell by Example

Eq is a Haskell type class defining equality and inequality operations for values that can be compared.

Eq is a Haskell type class for comparing values. It defines equality ((==)) and inequality ((/=)); both take two values of the same type and return Bool. A value can be compared only when its type has an Eq instance. section 5.1.3

ghci> :info Eq
type Eq :: * -> Constraint
class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool
  {-# MINIMAL (==) | (/=) #-}

Inspect Eq in GHCi to see its methods and its minimal implementation requirement: an instance needs to define either (==) or (/=). section 5.1.3

Use Eq constraints when a function compares values:

  • Write Eq a => when a function uses equality on an otherwise arbitrary type a. section 5.1.4
  • Compare only values of the same Eq-enabled type; an Int cannot be compared directly with a Float. section 5.1.3
  • Rely on the missing comparison operation being inferred by negating the supplied one. section 5.1.3
  • Use constrained instances such as Eq a => Eq (Maybe a) and Eq a => Eq [a] to compare containers when their elements are comparable. section 5.1.3

Checking membership in a graph

A membership function uses equality to search an associative list: member :: Eq a => a -> [(a, b)] -> Bool. It returns False for an empty list, compares each key with (==), and can support graph operations such as hasNode and addNode. section 5.1.4

How Learn Haskell by Example covers it

How deep it goes
Defines both methods, instance requirements, type restrictions, and constrained container instances, but does not detail equality algorithms.
Examples
Demonstrates GHCi inspection, associative-list membership, graph node lookup, and Eq instances for primitive and container types.
What it assumes
Assumes basic Haskell types and functions while teaching type classes, constraints, instances, and equality operations explicitly.
What you can do after
Apply it via Eq constraints to write functions that compare values and search associative lists or graph structures.
How it connects
Connects Eq with type classes, type constraints, algebraic data types, Maybe, lists, and higher-order functions.

Related concepts

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.