liveBook · Manning

Idris interface

As explained in Type-Driven Development with Idris

An Idris interface specifies generic operations that can have implementations for different types.

An Idris interface groups generic operations that can have different implementations for different types. An interface constraint in a function’s type states which operations must be available for that function to work. section 7.0

For example, doubling a value requires addition. The type ty -> ty alone does not say that ty supports arithmetic, so Idris rejects a definition using x + x. Adding the constraint Num ty supplies that requirement: the function can then be used with numeric types such as Int, Nat, and Integer. section 2.2.4 section 7.0

double : Num ty => ty -> ty                      #1
double x = x + x

Read Num ty => as “under the constraint that ty is numeric.” section 2.2.4

An interface declaration gives types to related methods. The Eq interface declares equality and inequality; its parameter ty defaults to type Type and appears in each method declaration. Declaring the interface also introduces top-level functions whose types carry the Eq ty constraint. section 7.1.2

interface Eq ty where              #1
    (==) : ty -> ty -> Bool        #2
    (/=) : ty -> ty -> Bool        #2

Eq specifies the operations without choosing how any particular type compares its values. section 7.1.2

An implementation supplies method definitions for a specific context. For Matter, equality can compare matching constructors, while inequality can be defined using equality. Once Eq Matter is available, a generic function constrained by Eq can compare Matter values. section 7.1.2

Eq Matter where                         #1
    (==) Solid Solid = True             #2
    (==) Liquid Liquid = True
    (==) Gas Gas = True
    (==) _ _ = False
    (/=) x y = not (x == y)             #3

This implementation defines both Eq methods for Matter. section 7.1.2

Interface parameters need not be types themselves. A parameter can have type Type -> Type and stand for a parameterized type such as List or IO. Functor, Applicative, and Monad use such parameters to provide generic operations including map, pure, and >>=. section 7.2 section 7.3

map : Functor f => (a -> b) -> f a -> f b
pure : Applicative f => a -> f a
(>>=) : Monad m => m a -> (a -> m b) -> m b

Each constraint names the interface that supplies the corresponding generic operation. section 7.3

Other Prelude interfaces include Ord for ordering, Show for converting values to String, and Cast for conversions between types. Interfaces and implementations can themselves have constraints. Idris also permits multiple implementations of an interface, although their details require further study. section 7.4 section 7.0

How Type-Driven Development with Idris covers it

How deep it goes
Develops declarations, implementations, and Prelude interfaces, but defers details of multiple implementations.
Examples
Uses generic numeric doubling, an Eq implementation for Matter, and constrained types for map, pure, and >>=.
What you can do after
Build interfaces and implementations from scratch and apply their constraints to generic functions.
How it connects
Connects constrained generic types to arithmetic, equality, ordering, functors, and monads.

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.