liveBook · Manning

Ord

As explained in Type-Driven Development with Idris

An Idris interface for types whose values can be compared in an ordering.

Ord lets a type support comparisons such as < and >=. It extends Eq, so an Ord implementation must also define equality; compare provides the ordering used by several default methods. section 7.1 section 7.1.5

interface Eq ty => Ord ty where                   #1
    compare : ty -> ty -> Ordering                #2
    (<) : ty -> ty -> Bool
    (>) : ty -> ty -> Bool
    (<=) : ty -> ty -> Bool
    (>=) : ty -> ty -> Bool
    max : ty -> ty -> ty                          #3
    min : ty -> ty -> ty                          #4

The interface signature shows compare returning Ordering alongside comparison operators, max, and min. section 7.1.5

Ordering albums

An Album record can be ordered by artist, then release year, then title when the earlier fields match. After defining Eq and Ord for Album, sort can order a list of albums; without that implementation, Idris reports that it cannot find Ord Album. section 7.1.5

How Type-Driven Development with Idris covers it

How deep it goes
Works from the Eq constraint through a custom ordering implementation and sorting, without exploring implementation internals.
Examples
Orders Album records by artist, year, and title, then sorts a collection to list its titles.
What it assumes
Assumes familiarity with Idris types and equality; constrained generic types and operator constraints are introduced from simpler examples.
What you can do after
Apply it by implementing Eq and Ord for a record type, then sorting its values with sort.
How it connects
Connects Ord to Eq, Ordering, comparison operators, and sort.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.