Chapter 7. Interfaces: using constrained generic types
Interfaces in Idris let you place precise constraints on generic types so functions can be written once and still work safely across many concrete types. The chapter begins by showing how a function like double or a generic list counter can require evidence that a type supports a needed operation, such as equality or arithmetic. This constraint-based style is central to type-driven development: instead of writing overly general code that may fail, you refine the type until Idris knows exactly which operations are available.
The chapter then explains how interfaces are declared and implemented. An interface groups related methods, and implementations provide the concrete behavior for a particular type, such as defining equality for a custom Matter type or ordering for an Album record. It also shows that methods may have default definitions, letting an implementation supply only part of the behavior when the rest can be derived automatically. A key idea is that interfaces themselves can be constrained, creating hierarchies such as Ord depending on Eq, and implementations can also carry constraints when their own behavior depends on the capabilities of contained types, as with equality for trees.
Later, the chapter surveys important Prelude interfaces and what they enable in practice. Show supports readable printing, numeric interfaces such as Num, Neg, Integral, and Fractional define arithmetic behavior and literal conversion, and Cast provides generic conversion between related types. It then moves to interfaces parameterized by type constructors, showing how Functor supports uniform mapping across structures, Foldable reduces structures to a single value, and Applicative and Monad capture common patterns of computation and sequencing. Throughout, the chapter emphasizes that interfaces are the mechanism by which Idris turns generic programming into something both reusable and statically safe.
Summary
- Generic types can be constrained using interfaces, which describe groups of functions that can be applied in a specific context.
- The Eq interface provides functions for comparing values for equality and inequality.
- The Ord interface provides functions for comparing two values to see which is smaller or larger.
- Implementations of interfaces describe how interfaces can be evaluated in specific contexts.
- Interfaces and implementations can themselves have constraints. Constraints say which interfaces must be implemented for a definition to be valid.
- The Prelude provides several standard interfaces, including Show for converting values to a String, Num for arithmetic operations and numeric literals, and Cast for converting between types.
- Interfaces can be parameterized by values of any type. Several in the Prelude are parameterized by values of type Type -> Type.
- An implementation of Functor for a structure allows a uniform action to be applied to each element in the structure.
- An implementation of Foldable allows a structure to be reduced to a single value.
- An implementation of Monad allows you to use do notation to sequence computations on a structure.
FAQ
What does it mean to constrain a generic type with an interface in Idris?
It means you restrict a type variable so it can only stand for types that implement a particular interface. For example,double : Num a => a -> a only works for types that support numeric operations.Why does occurrences need the Eq constraint?
occurrences compares list elements with ==, so Idris needs proof that the element type supports equality. Adding Eq ty => makes that available.How are interfaces related to Haskell type classes?
They are very similar in purpose and usage: both describe behaviors that types can implement. Idris interfaces are more general, since they can be parameterized by values of any type, not just types or type constructors.What are the methods of the Eq interface?
The Prelude's Eq interface provides two methods: (==) for equality and (/=) for inequality.Do I have to implement every method in an interface?
Not always. If a method has a default definition, Idris can use that default when your implementation omits it. ForEq, you can define just (==) and rely on the default for (/=).Why does defining Eq (Tree elem) require an extra constraint on elem?
Because tree equality compares not only the subtrees, but also the stored elements. Idris needs Eq elem to know how to compare those element values.What does the constraint Eq ty => Ord ty mean?
It means any type with an Ord implementation must also have an Eq implementation. This creates an interface hierarchy, where ordering depends on equality.What is the purpose of the Show interface?
Show converts values into human-readable Strings, often for debugging or printing. Functions like print and printLn require Show.How do Functor and Foldable differ?
Functor lets you transform every element in a structure with map. Foldable lets you reduce a structure to a single result with foldr or foldl.What makes Monad and Applicative important in this chapter?
They are examples of interfaces for type constructors of kind Type -> Type. They support generic programming patterns such as sequencing computations with (>>=) and applying functions inside a structure with pure and (<*>).
Type-Driven Development with Idris ebook for free