liveBook · Manning

Type variable

As explained in Type-Driven Development with Idris

A type variable stands for a type within a type expression.

A type variable stands for a type within a type expression. In `List elem`, `elem` marks the element type; using the same variable for both inputs and the output of append requires all three lists to share an element type. section 1.2.5

Table 1.3. Appending typed lists, in general. Type variables describe the relationships between the inputs and outputs, even though the exact inputs and outputs are unknown. (view table figure)

 

Input 1 type

Input 2 type

Output type

Simple AnyList AnyList AnyList
Generic List elem List elem List elem
Dependent Vect n elem Vect m elem Vect (n + m) elem

Compare simple, generic, and dependent types for appending lists: the generic signature uses `elem` to preserve the element type, while the dependent signature also tracks list lengths. section 1.2.5

In Idris, type-variable names generally begin with lowercase letters. Some type-level variables, such as `n` and `m` in dependent types, represent ordinary values and can appear in programs as well as types. section 1.2.5

Appending vectors

A signature using `Vect n elem` and `Vect m elem` describes inputs with a shared element type and lengths `n` and `m`; its output, `Vect (n + m) elem`, records both relationships. section 1.2.5

How Type-Driven Development with Idris covers it

How deep it goes
Works through append signatures for lists and vectors, but does not detail type-variable inference or implementation.
Examples
Uses generic list append and vector append with element and length variables.
What it assumes
Assumes basic familiarity with types and lists while explaining type variables through generic function signatures.
What you can do after
Understand how type variables specify shared element types and, with dependent types, relationships between list lengths.
How it connects
Connects generic types and dependent types through `List` and `Vect` function signatures.

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.