liveBook · Manning

Dependent pair

As explained in Type-Driven Development with Idris

A dependent pair is a pair whose second component's type can depend on the value of its first component.

A dependent pair combines a value with another value whose type is determined by the first. Unlike an ordinary tuple, it can express a relationship such as “this vector has length n.” This is useful when the length is not known until input arrives. section 5.3.3 section 5.4

anyVect : (n : Nat ** Vect n String)
anyVect = (3 ** ["Rod", "Jane", "Freddy"])

Here, the first component supplies the length required by the vector type in the second component. section 5.3.3

For console input, return the length together with the vector: `IO (len ** Vect len String)`. Pattern matching on the result exposes both values, so vectors of different lengths can be checked before calling `zip`, which requires equal lengths. section 5.3.3 section 5.3.4

How Type-Driven Development with Idris covers it

How deep it goes
Defines pair syntax, demonstrates type inference with holes, and implements input returning a length-indexed vector.
Examples
Uses a string vector of length three and console input terminated by a blank line.
What it assumes
Assumes familiarity with tuples and vector types, and introduces dependent-pair notation through examples.
What you can do after
Apply dependent pairs to return a runtime-sized vector with its length, then check lengths before zipping vectors.
How it connects
Connects tuples, dependent types, vectors, and interactive input/output.

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.