liveBook · Manning

Type family

As explained in Type-Driven Development with Idris

A type family is a collection of types whose specific type is determined by one or more indices.

A type family is a set of related types selected by an index. For example, a vehicle’s power source selects whether its type is Vehicle Pedal or Vehicle Petrol. section 4.2.1

data PowerSource = Petrol | Pedal                      #1
data Vehicle : PowerSource -> Type where               #2
     Bicycle : Vehicle Pedal                           #3
     Car : (fuel : Nat) -> Vehicle Petrol              #4
     Bus : (fuel : Nat) -> Vehicle Petrol              #4

The constructors show how the power-source index distinguishes bicycles from cars and buses. section 4.2.1

Vect is another family: its index records length, while its element type is a parameter. This lets function types express requirements such as equal input lengths for zip. section 4.2.2

Appending vectors

The type append : Vect n elem -> Vect m elem -> Vect (n + m) elem states that the result length is the sum of the input lengths. Case splitting on the first vector gives a definition that preserves this guarantee. section 4.2.2

How Type-Driven Development with Idris covers it

How deep it goes
Works through indexed declarations and vector functions, including case splitting, but does not develop the underlying type theory.
Examples
Uses vehicles indexed by power source and vectors indexed by length, with append and zip as operations.
What you can do after
Build indexed data types and write functions whose types constrain valid inputs and results.
How it connects
Connects type families to dependent types, indices, parameters, and vectors.

Also explained in

Compare how 2 books explain type family →

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.