Overview

Chapter 4. User-defined data types

Type-driven development in Idris is presented as a discipline of shaping programs by first being precise about the form of the data they manipulate. The chapter explains that many useful structures are ordinary user-defined data types, built from a type constructor and one or more data constructors, and that careful type design gives the compiler more information to guide implementation. It shows how to define new types in the compact and explicit forms, how to document constructors, and how the interactive edit-refine workflow helps build functions from their types and the patterns of their inputs.

The discussion then classifies common user-defined types into enumerations, union types, recursive types, and generic types. Enumerations describe a fixed set of values, union types attach data to different alternatives, recursive types describe data that contains smaller values of the same kind, and generic types capture reusable patterns with type parameters. Examples such as directions, shapes, pictures, and trees demonstrate how pattern matching and recursion naturally follow from the data definition, while naming directives and named patterns improve readability. The chapter also emphasizes the trade-off between precision and reuse: more precise types can rule out invalid cases, but may make a structure less general.

In the final part, the chapter moves from ordinary data types to dependent data types, where types are indexed by values such as a power source or a vector length. This allows the type system to distinguish meaningful subsets of a family of types and to prevent invalid operations, such as refueling a pedal-powered vehicle or indexing outside a vector’s bounds. The data store example ties everything together by building an interactive program whose state, commands, and lookup operations are all guided by types, including safe conversion from arbitrary integers to bounded indices. The result is a larger application built in a type-driven style, where precise representations and total functions help ensure correctness from the start.

Figure 4.1. Defining a Direction data type (Direction.idr)

Figure 4.2. Example shapes represented as a union type. Triangle takes two Doubles, for base and height; Rectangle takes two Doubles, for width and height; Circle takes a Double for radius.

Figure 4.3. An example picture combining three shapes translated to different positions

Figure 4.4. Defining the Either data type

Figure 4.5. Pairing corresponding elements of [1,2,3,4] and ["one","two","three","four"] using zip

Summary

  • Data types are defined in terms of a type constructor and data constructors.
  • Enumeration types are defined by listing the data constructors of the type.
  • Union types are defined by listing the data constructors of the type, each of which may carry additional information.
  • Generic types are parameterized over some other type. In a generic type definition, variables stand in place of concrete types.
  • Dependent types can be indexed over any other value.
  • Using dependent types, you can classify a larger family of types (such as vehicles) into smaller subgroups (such as vehicles powered by petrol and those powered by pedal) in the same declaration.
  • Dependent types allow safety checks to be guaranteed at compile time, such as guaranteeing that all vector accesses are within the bounds of the vector.
  • You can write larger programs in the type-driven style, creating new data types where appropriate to help describe components of the system.
  • Interactive programs that involve state can be written using the replWith function.

FAQ

What is the basic idea behind type-driven development in this chapter?Type-driven development here means designing data types carefully so they capture the structure of the problem as precisely as possible. Once the types are right, they guide the implementation of functions, reduce errors, and make interactive refinement easier.
What is the difference between a type constructor and a data constructor?A type constructor defines the type itself, while data constructors define the possible values of that type. For example, List is the type constructor, and Nil and (::) are its data constructors.
What is an enumerated type in Idris?An enumerated type is a type whose valid values are listed directly by its constructors. A simple example is Bool, which has the constructors False and True.
How is a union type different from an enumerated type?A union type is like an enumerated type, but each constructor can carry additional data. For example, Shape can have constructors such as Triangle Double Double or Circle Double, where the constructors store shape-specific information.
What makes a recursive data type recursive?A recursive data type is defined in terms of itself. In this chapter, Nat and Picture are examples: Nat uses S Nat, and Picture can contain smaller Picture values inside its constructors.
Why do recursive types need a non-recursive constructor?They need at least one constructor that does not refer back to the type itself, otherwise you could never build a finite value of the type. For example, Nat has Z as a base case, and Picture has Primitive.
What is a generic data type?A generic data type is parameterized over another type. Maybe and List are examples: Maybe a can store a value of any type a, and List elem works for any element type.
Why is List not itself a complete type?List alone is a type constructor, not a concrete type. It becomes a real type only when applied to an argument, such as List Int or List String.
What is the benefit of storing the length in the type of Vect?Putting the length in the type lets Idris enforce size-related properties at compile time. This makes operations like append, zip, and safe indexing more precise and helps prevent out-of-bounds errors.
Why does the chapter use Maybe in functions like safeDivide and tryIndex?Maybe is used to model possible failure safely. Instead of using an invalid sentinel value or crashing, the function returns Nothing when the operation cannot be completed, such as dividing by zero or indexing out of range.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Type-Driven Development with Idris Type-Driven Development with Idris ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Type-Driven Development with Idris Type-Driven Development with Idris ebook for free