Overview

Chapter 3. Interactive development with types

This chapter introduces a practical way to develop Idris programs by working with types first, then building and refining definitions interactively. The central workflow is “type, define, refine”: start with a type, generate a skeleton function, inspect the types of holes and pattern-matched cases, and then fill in the missing pieces until the program is complete. Idris and the Atom editor work together to support this process with commands for adding definitions, splitting cases, checking types, searching for expressions, and lifting holes into new helper functions.

A major focus is pattern matching over ordinary data types such as lists, booleans, and natural numbers. By examining the constructors of a type, you can write functions one case at a time, and Idris’s interactive tools help reveal exactly what each branch must return. The chapter shows this with examples like inverting booleans, testing whether numbers are even, and defining list-processing functions such as computing word lengths. It also emphasizes totality: a function should cover all valid inputs and make recursive calls that move toward a base case, so Idris can verify that it always terminates for well-typed inputs.

The later part of the chapter increases precision by replacing lists with vectors, whose types record their lengths. This extra information lets Idris rule out incorrect programs and often infer more useful details when filling holes, making the development process both safer and more automated. The same idea is extended to matrix operations, where vector lengths express dimensions and guide the implementation of transposition. The chapter closes by introducing implicit arguments, showing how type-level values like element types and vector lengths can be inferred automatically, brought into scope when needed, and erased at runtime so that the program remains efficient while retaining strong static guarantees.

Figure 3.1. Matching the pattern (x :: xs) for inputs [1] and [2,3,4,5]

Figure 3.2. Calculating the lengths of words in a list. Notice that each input has a corresponding output, so the length of the output vector is always the same as the length of the input vector.

Figure 3.3. Representation of a matrix as two-dimensional vectors. On the left, the matrix is in mathematical notation. The same matrix is represented in Idris on the right.

Figure 3.4. The components of the vector you’re transposing (x and xs), along with the result of transposing xs, and the expected overall result

Summary

  • Functions in Idris are defined by collections of pattern-matching equations.
  • Patterns arise from the constructors of a data type.
  • The Atom text editor provides an interactive editing mode that uses the type to help direct function implementation. Similar modes are available for Emacs and Vim.
  • Interactive editing commands provide natural tools for following the process of type, define, refine.
  • Interactive editing provides a command for searching for a valid expression that satisfies the type of a hole.
  • More-precise types, such as Vect, give more information to the compiler both to help check that a function is correct, and to help constrain expression searches.
  • Matrices are two-dimensional vectors, with the dimensions encoded in the type. Operations on matrices such as addition and multiplication can be given types that precisely describe how the operations affect the dimensions.
  • The type-define-refine process helps you to implement matrix operations with precise types by using the type to direct the implementation of each subexpression and create appropriate helper functions.
  • Type-level variables are implicit arguments to functions, which can be brought into scope and used like any other arguments by enclosing them in braces {}.

FAQ

What is the “type, define, refine” workflow in Idris?

It is a type-driven development process where you first write or inspect a type, then create the structure of the function, and finally refine the definition by filling holes or making the type more precise.

How do holes help with interactive development in Idris?

Holes mark unfinished parts of a function. Idris can show the expected type of a hole and the local variables in scope, helping you decide what code should go there.

What are the main interactive editing commands in Atom for Idris?

Common commands include adding a definition, case splitting, showing documentation, lifting a hole to a top-level definition, matching on intermediate results, reloading and type-checking the buffer, searching for expressions, and checking a name’s type.

How do you define a function by pattern matching in Idris?

You write one or more equations, each matching a possible form of the input. For example, a list can be matched as either [] or (x :: xs), with a separate clause for each case.

Why is pattern matching important for functions over lists and natural numbers?

Because lists and natural numbers are built from constructors, and pattern matching lets you handle each constructor form directly, such as empty versus non-empty lists or zero versus successor natural numbers.

What does totality mean in Idris?

A total function is guaranteed to produce a result for any well-typed input in finite time. Idris checks that all cases are covered and that recursive calls make progress toward a base case.

How does the type of a vector differ from the type of a list?

A vector (Vect) includes its length in the type, so the compiler knows how many elements it contains. A list (List) only records the element type, not the length.

Why is allLengths easier to verify with vectors than with lists?

With vectors, the type can state that the output has the same length as the input. This rules out incorrect implementations that would return too few or too many results.

What are implicit arguments in Idris?

Implicit arguments are values or types that Idris can infer from context, so you do not have to supply them explicitly when calling a function.

How does type precision help Idris infer parts of a program automatically?

More precise types give Idris enough information to fill in some holes or even complete simple expressions automatically, such as the empty vector case or values constrained by the shape of the type.

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