Chapter 6. Programming with first-class types
Idris treats types as first-class values, so the chapter shows how to compute types from other data and use that ability to make programs more precise. It begins with type synonyms as a simple form of type-level function, using them to give clearer names to complex types such as positions and polygons. From there, it moves to genuine type-level computation, where the result type of a function can depend on a value like a Boolean, enabling dependent pattern matching and letting the compiler refine both arguments and return types as definitions are worked out.
The next major theme is functions with varying numbers of arguments, built by calculating function types from an initial input. A small adder example demonstrates the general pattern: a type-level function maps a natural number to a chain of function arrows, and the implementation follows the same structure by recursion. This idea is then scaled up to a type-safe formatted output function, where a format description determines the exact sequence and types of arguments required, and an intermediate format datatype makes parsing and type calculation easier to manage.
The chapter finishes by applying these techniques to a larger interactive data store. The store is refined so that its contents are governed by a user-defined schema, and that schema is translated into a concrete Idris type by a type-level function. Records are used to package the schema, size, and items together while automatically generating projection functions, and holes guide the repair of the rest of the program as the types change. The parser and command language are then extended to handle schema-aware input, display values according to their schema, and safely reject invalid updates, with a final note that monadic sequencing with Maybe can be written more concisely using do notation.
Summary
- Type synonyms are alternative names for existing types, which allow you to give more-descriptive names to types.
- Type-level functions are functions that can be used anywhere that Idris is expecting a Type. Type synonyms are a special case of type-level functions.
- Type-level functions can be used to compute types, and they allow you to write functions with varying numbers of arguments, such as printf.
- You can represent a schema for a data store as a type, and then use type-level functions to calculate the types of operations, such as parsing and displaying entries in the data store from the schema description.
- A record is a data type with only one constructor, and with automatically generated functions for projecting fields from the record.
- After refining a data type, you can use holes to correct compilation errors temporarily.
- Using do notation, you can sequence computations that use Maybe to capture the possibility of errors.
FAQ
What is the main idea of programming with first-class types in Idris?
Types are treated like ordinary values: they can be passed to functions, returned from functions, stored, and computed. This lets Idris programs calculate types from other values, making APIs more precise and type-safe.What is a type synonym in Idris?
A type synonym is an ordinary function that provides an alternative name for a type. For example,Position = (Double, Double) makes a coordinate pair more readable and self-documenting.Why would you use a type-level function instead of writing the type directly?
You use a type-level function when the resulting type depends on some input value. This is useful for describing things like variable-length arguments, schemas, or return types that depend on a flag or format string.How does dependent pattern matching help when writing type-level functions?
When you case-split on a value, Idris refines related types automatically. This means the type of later arguments or results can change based on the value you matched, making it possible to write functions that are both flexible and type-safe.What does the StringOrInt example demonstrate?
It shows a type-level function that returns String for False and Int for True. It demonstrates how a function’s input value can determine the type of its output and other arguments.How can types be used to define a function with a variable number of arguments?
You define a type-level function that computes a function type from a number, such asAdderType. Then the main function, like adder, uses that computed type so the number of arguments is reflected in the type system.Why is printf a good example of first-class types?
printf can calculate its argument types from a format description. In Idris, this means the format string or format data can determine exactly what arguments are required, preventing mismatched calls at compile time.Why does the chapter introduce an intermediate Format type for printf?
The intermediate Format type separates parsing from processing and makes the structure of the format explicit. It also makes interactive editing and case splitting easier than working directly with raw strings.How does the refined data store use schemas and type-level functions?
The data store stores a schema description and uses a type-level function,SchemaType, to compute the concrete type of its contents. This lets the store support different entry types while preserving type safety.
Type-Driven Development with Idris ebook for free