concept equality in category idris

appears as: equality, equalities
Type-Driven Development with Idris

This is an excerpt from Manning's book Type-Driven Development with Idris.

7.1.1. Testing for equality with Eq

As you saw in chapter 2, Idris provides an operator for testing values for equality, ==, with a constrained generic type:

Idris> :t (==)
(==) : Eq ty => ty -> ty -> Bool

In other words, you can compare two values of some generic type ty for equality, returning a result as a Bool, provided that ty satisfies the Eq constraint. Similarly, there’s an operator for testing for inequality:

In this chapter, we’ll look at a simple property, using types to express guarantees that values are equal. You’ll also see how to express guarantees that values are not equal. Properties such as equality and inequality are sometimes required when you’re defining more-complex functions with dependent types, where the relationship between values might not be immediately obvious to Idris. For example, as you’ll see when we define reverse on vectors, the input and output vector lengths must be the same, so we’ll need to explain to the compiler why the length is preserved.

  • Define— You can now compare m and len for equality using == and inspect the result in a case statement:
    exactLength : (len : Nat) -> (input : Vect m a) -> Maybe (Vect len a)
    exactLength {m} len input = case m == len of
                                     False => ?exactLength_rhs_1
                                     True => ?exactLength_rhs_2
  • With dependent types, you can use types such as EqNat to express additional information about other data, in this case expressing that two Nats are guaranteed to be equal. This is a powerful concept, as you’ll soon see, and can take some time to fully appreciate. We’ll therefore look at representing and checking equalities in some depth.