Overview

Chapter 10. Views: extending pattern matching

In type-driven development, functions are usually shaped by the types of their inputs, and ordinary pattern matching only lets you inspect values according to their constructors. This chapter introduces views as a way to extend that style of matching, letting you describe alternative ways of observing the same data. Instead of being limited to matching lists from the front, you can define views that expose the last element first, split a list into halves, or reveal a data structure through an abstract interface while still supporting pattern matching.

The main technique is to pair a view type with a covering function that builds a view value for a given input, then use the with construct to pattern-match on that intermediate result. The chapter first shows a simple view for matching the end of a list, then uses the same idea to define functions like reverse and merge sort. It also explains why some direct formulations fail: Idris cannot always infer the hidden structure behind expressions like concatenation, and it may reject functions as non-total when recursive calls are made through a view that is rebuilt each time.

To address those limits, the chapter develops recursive views, such as a snoc-list representation and a recursive split view, which preserve the decreasing structure needed for totality checking and avoid rebuilding views on each recursive call. These recursive views make it possible to write efficient, total traversals that operate from the end of a list, compare suffixes, or sort by repeatedly splitting and merging. The final part applies the same idea to data abstraction: a store is hidden behind a module interface, while a view exposes its contents in a safe, pattern-matchable form, showing how views can support both efficient computation and clean encapsulation.

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

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

Figure 10.3. Syntax for the with construct

Figure 10.4. Sorting a list using merge sort: split the list into two halves, sort the halves, and then merge the sorted halves back together

Summary

  • A view is a dependent type that describes the possible forms of another data type. Views take advantage of dependent pattern matching to allow you to extend the forms of patterns you can use.
  • A covering function builds a view of a value. By convention, its name is the name of the view with an initial lowercase letter.
  • The with construct allows you to use views directly, without defining an intermediate function.
  • You can use views to define alternative traversals of data structures, such as extracting the last element of a list instead of the first.
  • Recursive views help you write functions that are guaranteed to terminate, by writing recursive functions that pattern-match on the view.
  • Idris provides several views for alternative traversals of List in the Data.List.Views library. Similar libraries exist for Vect, Nat, and String.
  • You can hide the structure of data in a module, while still supporting interactive type-driven programming with that data, by exporting views for traversing data structures.

FAQ

What is a “view” in Idris, and why is it useful?A view is a dependent type that describes an alternative way to observe and pattern-match on data. It is useful because it lets you match on data in forms that are not directly available from the original data constructors, such as matching a list by its last element or by splitting it into halves.
Why can’t Idris directly match on a pattern like (xs ++ [x])?Because ++ is a function, not a data constructor. In general, Idris cannot deduce the inputs to an arbitrary function from its output, so a pattern like (xs ++ [x]) is not a valid direct pattern match.
What problem does the ListLast view solve?ListLast lets you treat a list as either empty or as an initial segment plus a final element. This makes it possible to write functions that process lists from right to left, such as describing or reversing a list by inspecting the last item first.
What is a covering function for a view?A covering function builds a value of the view type for a given input. For example, listLast takes a list and returns a value of type ListLast xs, proving that the list is either empty or can be viewed as (xs ++ [x]).
What does the with construct do?The with construct adds an intermediate value to the left side of a function definition so you can pattern-match on it. It is especially useful for dependent pattern matching, where matching on the intermediate value gives more information about the original input.
How does with differ from a normal case expression?case matches inside the right-hand side of a definition, while with introduces a new pattern on the left-hand side. That difference allows Idris to use dependent pattern matching more directly and refine the types of the original arguments.
Why is the first version of myReverse using ListLast inefficient?It rebuilds the ListLast view on every recursive call, and building that view requires traversing the list again. That means the list is traversed repeatedly rather than just once.
How does the recursive SnocList view improve reverse traversal?SnocList stores the recursive structure of the list itself, so the reverse traversal can reuse the already-built view instead of reconstructing it each time. This makes myReverse run in linear time and also helps Idris verify totality.
Why does Idris sometimes fail to recognize a function as total when it uses views?Idris checks totality by looking for decreasing recursive arguments. If a recursive call is made on a value like (xs ++ [x]), Idris may not see that as structurally smaller, even if the logic is sound, so it may report the function as possibly not total.
How are views used for data abstraction in the data store example?The data store’s internal representation is hidden inside a module, but a StoreView view exposes safe patterns like empty or added entries. Other modules can then traverse or filter the store using the view without knowing the store’s internal structure.

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