📘
HPM Education - Haskell
  • Introduction to Haskell
  • Introduction
    • Functions
    • Functional Programming vs Imperative Programming
    • Installing Haskell
    • Haskell Modules
    • Loading Modules into GHCi
    • Expressions
    • Laziness
    • Immutability
  • Types in Haskell
    • Introduction
    • Basic Types
    • Static Type Check
    • Polymorphic and Overloaded Types
    • Data Structure Types
      • Lists
        • List Functions
      • Tuples
    • Function Types
      • Curried Functions
      • Partial Application
  • Defining Functions / Working with Functions
    • The Layout Rule
    • Local Definitions
    • The Infix Operator
    • Conditionals
      • If-then-else Statements
      • MultiWayIf
      • Guarded Equations
      • Case-of Statements
    • Pattern Matching
      • Tuple Patterns
      • List Patterns
    • Lambda functions
    • Function Operators
  • List Comprehensions
    • List Comprehensions
  • Higher-order Functions
    • Introduction
    • The map Function
    • The filter Function
  • Recursion
    • Introduction
    • 4 Steps to Defining Recursive Functions
    • Recursion Practice
    • Folds
      • Fold Right (foldr)
      • Fold Left (foldl)
  • Cutom Types
    • Declaring Types
      • Type Synonyms
      • Data Declarations
      • Newtype declarations
  • Type Classes
    • Introduction
    • Basic Classes
      • Eq – Equality Types
      • Ord – ordered types
      • Show – Showable Types
      • Read – readable types
      • Num – Numeric Types
      • Integral – Integral Types
      • Fractional – Fractional Types
      • Enum – Enumeration Types
    • Derived Instances
    • Exercise – Making a Card Deck Type
  • Interactive Programming
    • Introduction
    • Input / Output Actions
    • Sequencing Actions
    • Exercise - Numbers Guessing Game
  • Functors, Applicatives and Monads
    • Introduction
    • Functors
    • Applicative Functors
    • Monads
      • Maybe Monad
      • List Monad
      • Monad Laws
  • References / Further Reading
Powered by GitBook
On this page

Was this helpful?

  1. Type Classes
  2. Basic Classes

Eq – Equality Types

The Eq class supports the comparison methods (equality and inequality), so any two values of a type that is an instance of the Eq class can be compared using the functions:

(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool

We have already used these functions many times, and we were able to because all the basic types (Char, Bool, Int....) are instances of the Eq class. Classes are defined using the class keyword followed by the class name, the type specification and the where keyword, followed by the default definitions of the class methods:

class Eq a where
  (==), (/=) :: a -> a-> Bool
  
    -- Minimal complete definition:
    -- (==) or (/=)
    
  x /= y = not (x == y)
  x == y = not (x /= y)

We see the two default methods above defined in terms of each other, which means we need to define one of them in a clear way to have a complete definition for an instance. For example, the Bool type can be made an instance of the Eq class with:

instance Eq Bool where
  False == True = False
  True == False = False
  _ == _ = True
  
-- or using the (/=) method
    
instance Eq Bool where
  False /= True = True
  True /= False = True
  _ /= _ = False

Keep in mind that the definitions of the required functions can be as simple or as complicated as you like, and default definitions can be overwritten when declaring instances.

PreviousBasic ClassesNextOrd – ordered types

Last updated 2 years ago

Was this helpful?