Chapter 10: Chapter 10. Views: extending pattern matching
This section introduces views as dependent types that offer alternative ways to pattern-match and observe data.
After this chapter you will be able to
- Views as Alternative Observations. Explain what a view is and describe alternative observations it can provide, including an example involving lists.
- Forward and backward list matching. Describe what the pattern (x :: xs) binds and contrast it with the desired decomposition into an initial segment and a last element.
- Function patterns and views. Explain why (xs ++ [x]) cannot be used directly as the desired pattern and state how views address the limitation at a high level.
- ListLast view of lists. Explain how ListLast provides an alternative view of a list and what is needed to use it on an input list.
- Dependent case split on form. Trace how describeHelper uses a case split on its ListLast argument to determine the form of its input.
- Covering function for ListLast. Explain how listLast converts a list into a ListLast view and what that view exposes.
- With block syntax. Describe how a with block adds an intermediate result as a pattern-matching argument, using describeListEnd as an example.
- Dependent view matching. Explain how matching the listLast result refines the input pattern in describeListEnd, and distinguish this use of with from a case block.
- Valid and forced patterns. Explain why Idris restricts patterns and identify the constructor and forced-pattern cases described in this section.
- List reversal with ListLast. Describe how myReverse uses the ListLast view to reverse a list from its last element.
- Repeated view construction cost. Explain why this implementation of myReverse is inefficient and contrast it with the traversal needed for a linear-time reversal.
- Decreasing argument recognition. Explain why Idris cannot establish totality for myReverse from its recursive call on xs.
- Merge sort with SplitList. Describe how mergeSort uses splitList in a with block to sort lists.
- Concatenation Pattern Ambiguity. Explain why a view is needed to match a list as a concatenation of two parts.
- SplitList Type Guarantees. Distinguish what the SplitList type guarantees from what its implementation must ensure.
- View-based pattern matching. Explain how input data, a view, a covering function, and the with construct work together to provide informative patterns for the original data.
- Recursive SnocList view. Explain how a SnocList represents a list for reverse traversal and how its recursive view relates to the equivalent List.
- Reversal with recursive views. Trace myReverseHelper through its empty and nonempty cases, and explain how myReverse supplies the view and why the helper is total.
- Covering function construction. Describe how snocList constructs the view and relate its single traversal to the complexity of myReverse.
- View Based Reversal Cases. Describe how case splitting on the SnocList view produces the cases for myReverse and how each case constructs its result.
- Recursive View Reuse. Explain why myReverse calls the with block with rec rather than calling myReverse xs alone, and relate this choice to totality and running time.
- Nested view pattern matching. Explain how nested with blocks let isSuffix match both input lists using snocList.
- Reverse suffix checking. Describe the cases and recursive comparison used to decide whether the first list is a suffix of the second.
- Merge sort with SplitRec. Describe how mergeSort uses SplitRec to traverse and sort a list recursively as a total function.
- Alternative List Views. Describe how SnocList and SplitRec present alternative ways to inspect a list without directly examining its constructors.
- Views and Data Abstraction. Explain how a view can support interactive development of functions while a module hides its data representation.
- Module export modifiers. Distinguish private, export, and public export, including their effects on data constructors and the default visibility.
- Abstract module interfaces. Describe how exporting a type and selected functions while hiding constructors makes the type abstract to importing modules.
- Store Module Abstraction. Describe what other modules can see and use from the exported store module, and what remains hidden.
- Abstract Store Traversal Cases. Explain why a view is useful for traversing the abstract store and describe the two cases it should support.
- Abstract store traversal. Explain how StoreView and storeView let clients traverse a store while preserving its abstraction.
- Filtering store keys. Trace how filterKeys uses the store view to collect keys whose associated values pass a test.
What it builds on
- Defining functions by pattern matching (chapter 3) · Defining functions by pattern matching
- Defining dependent data types (chapter 4) · Dependent data types
- Data types and patterns (chapter 3) · List constructors and patterns.
- Partial and total functions (chapter 1) · The distinction between partial and total functions
- The rewrite construct: rewriting a type using equality (chapter 8) · The rewrite construct changes a type using equality.
- Delegating proofs and rewriting to holes (chapter 8) · Holes can help with proofs and rewriting.
- Type synonyms: giving informative names to complex types (chapter 6) · Type synonyms
- Type-level functions with pattern matching (chapter 6) · Type-level functions
Section by section
Introduction
This section introduces views as dependent types that offer alternative ways to pattern-match and observe data.
- Constructor Based Pattern Matching: Explain how case splits use a type’s constructors to structure a function, using list patterns as an example.
Why it matters: This explains the ordinary pattern-matching structure that views extend. - Views as Alternative Observations: Explain what a view is and describe alternative observations it can provide, including an example involving lists.
Why it matters: Views let you choose how to observe data rather than being limited to its ordinary constructor patterns.
Key terms: case split, data constructors, pattern matching, List, views, dependent types, abstract interface
10.1 Defining and using views
This section motivates views by contrasting ordinary list patterns with the desired ability to match a list’s final element.
- Forward and backward list matching: Describe what the pattern (x :: xs) binds and contrast it with the desired decomposition into an initial segment and a last element.
Why it matters: This distinction explains why the usual list pattern is inconvenient for processing a list from the end. - Function patterns and views: Explain why (xs ++ [x]) cannot be used directly as the desired pattern and state how views address the limitation at a high level.
Why it matters: This explains both why the intuitive end-of-list pattern fails and why views are useful.
Key terms: (x :: xs), tail, initial segment, last element, ++, pattern-match, views, informative data types
10.1.1 Matching the last item in a list
This section uses the ListLast view and dependent pattern matching to describe a list by its final element.
- ListLast view of lists: Explain how ListLast provides an alternative view of a list and what is needed to use it on an input list.
Why it matters: This view makes it possible to work with a list in terms of its end rather than only its usual structure. - Dependent case split on form: Trace how describeHelper uses a case split on its ListLast argument to determine the form of its input.
Why it matters: Dependent pattern matching lets the helper describe the end of a list even though that expression cannot be matched directly.
Key terms: ListLast, view, initial portion, final element, dependent pattern matching, case-split, describeHelper, Empty, NonEmpty
10.1.2 Building views: covering functions
This section introduces covering functions through a view that exposes the last element of a list.
- Covering function for ListLast: Explain how listLast converts a list into a ListLast view and what that view exposes.
Why it matters: A covering function lets you work with a value through a useful alternative view. - Covering function naming convention: State the naming convention for a view's covering function.
Why it matters: The convention makes covering functions easier to recognize alongside their view types.
Key terms: covering function, ListLast, view, view type
10.1.3 with blocks: syntax for extended pattern matching
This section shows how with blocks support concise dependent pattern matching on intermediate results and explains which patterns Idris permits.
- With block syntax: Describe how a with block adds an intermediate result as a pattern-matching argument, using describeListEnd as an example.
Why it matters: This notation lets you match an intermediate view result without writing a separate helper. - Dependent view matching: Explain how matching the listLast result refines the input pattern in describeListEnd, and distinguish this use of with from a case block.
Why it matters: A view can reveal the shape of an input while you match an intermediate result. - Valid and forced patterns: Explain why Idris restricts patterns and identify the constructor and forced-pattern cases described in this section.
Why it matters: These restrictions explain why the list-end expression can appear as a pattern without making arbitrary expressions valid patterns.
Key terms: with construct, with block, with_pat, listLast, dependent pattern matching, Empty, NonEmpty, case block, data constructor, valid pattern
10.1.4 Example: reversing a list using a view
This section implements list reversal using the ListLast view and explains its efficiency and totality-checking limitations.
- List reversal with ListLast: Describe how myReverse uses the ListLast view to reverse a list from its last element.
Why it matters: This example shows how a view supports a traversal that differs from the list's usual left-to-right structure. - Repeated view construction cost: Explain why this implementation of myReverse is inefficient and contrast it with the traversal needed for a linear-time reversal.
Why it matters: A direct view-based definition can conceal repeated traversals of the input. - Decreasing argument recognition: Explain why Idris cannot establish totality for myReverse from its recursive call on xs.
Why it matters: Understanding the check explains why an intuitively shrinking recursion can still be reported as possibly not total. - Consequences of unverified totality: Distinguish the guarantee provided by a function Idris can determine to be total from the guarantee when it cannot, including the effect on callers.
Why it matters: The totality result affects how strongly the type describes a function's behavior and what Idris can establish about its callers.
Key terms: ListLast, with block, Empty, NonEmpty, myReverse, recursive call, linear time, totality, decreasing argument, data constructor
10.1.5 Example: merge sort
This section develops merge sort using a list-splitting view and examines its guarantees and termination limitations.
- Merge sort with SplitList: Describe how mergeSort uses splitList in a with block to sort lists.
Why it matters: The view lets the implementation follow the algorithm’s natural cases. - Concatenation Pattern Ambiguity: Explain why a view is needed to match a list as a concatenation of two parts.
Why it matters: This explains why the direct-looking merge sort definition needs a view. - SplitList Type Guarantees: Distinguish what the SplitList type guarantees from what its implementation must ensure.
Why it matters: A valid split and a balanced split are different guarantees. - Balanced Splitting Counter: Describe how splitListHelp uses a second reference to the input as a counter to locate the halfway point.
Why it matters: The counter explains how the covering function obtains nearly equal halves. - Merge Sort Totality Limitation: Explain why Idris cannot establish that this mergeSort definition is total.
Why it matters: A working splitting strategy does not automatically make its termination evident to Idris.
Key terms: mergeSort, SplitList, SplitNil, SplitOne, SplitPair, merge, pattern, concatenation, covering function, total
10.2 Recursive views: termination and efficiency
This section recaps how views extend pattern matching and identifies totality and efficiency problems in earlier examples.
- View-based pattern matching: Explain how input data, a view, a covering function, and the with construct work together to provide informative patterns for the original data.
Why it matters: This explains how a view provides another way to match the data a function receives. - Earlier view implementation limitations: Describe the totality and efficiency problems observed in the earlier list-view examples.
Why it matters: These limitations motivate improving the way traversals are represented.
Key terms: view, covering function, with construct, dependent pattern matching, total, recursive call
10.2.1 “Snoc” lists: traversing a list in reverse
This section uses a recursive SnocList view to reverse a list and explains its termination and linear-time construction.
- Recursive SnocList view: Explain how a SnocList represents a list for reverse traversal and how its recursive view relates to the equivalent List.
Why it matters: The recursive view exposes a list from its end while preserving its relationship to the original list. - Reversal with recursive views: Trace myReverseHelper through its empty and nonempty cases, and explain how myReverse supplies the view and why the helper is total.
Why it matters: The example shows how a recursive view supplies both the decomposition and the decreasing argument needed for reversal. - Covering function construction: Describe how snocList constructs the view and relate its single traversal to the complexity of myReverse.
Why it matters: Building the view once makes it reusable and enables the reversal to run in linear time. - Rewrite and hole inspection: Describe the role of rewrite in snocList and how replacing expressions with holes helps investigate its types.
Why it matters: Inspecting holes makes the type adjustments in a tricky view construction easier to understand.
Key terms: SnocList, recursive view, Snoc, List, myReverseHelper, myReverse, Empty, total, covering function, accumulator
10.2.2 Recursive views and the with construct
This section rewrites list reversal using a recursive SnocList view and the with construct, preserving totality and linear-time traversal.
- View Based Reversal Cases: Describe how case splitting on the SnocList view produces the cases for myReverse and how each case constructs its result.
Why it matters: These cases show how a view lets you match a list from its end rather than its beginning. - Recursive View Reuse: Explain why myReverse calls the with block with rec rather than calling myReverse xs alone, and relate this choice to totality and running time.
Why it matters: Reusing the recursive view makes the concise definition both checkably total and efficient. - Generated With Block Helper: Explain how Idris represents the with block using a generated helper and how the direct recursive call corresponds to the earlier manual helper call.
Why it matters: The helper-function equivalence clarifies what a direct recursive call to a with block does.
Key terms: with construct, SnocList, Empty, Snoc, case splitting, recursive view, with block, total, linear time, myReverseHelper
10.2.3 Traversing multiple arguments: nested with blocks
This section uses a suffix test to show how nested with blocks traverse multiple lists using recursive views.
- Nested view pattern matching: Explain how nested with blocks let isSuffix match both input lists using snocList.
Why it matters: Nesting lets a definition make decisions based on views of more than one input. - Reverse suffix checking: Describe the cases and recursive comparison used to decide whether the first list is a suffix of the second.
Why it matters: The example shows how reverse traversal makes suffix checking a comparison of corresponding final elements. - Recursive view argument reuse: Explain why the recursive isSuffix call passes both recursive view arguments.
Why it matters: Reusing the views avoids unnecessary work during the recursive traversal.
Key terms: with blocks, snocList, Empty, Snoc, isSuffix, suffix, recursive view arguments, xsrec, ysrec
10.2.4 More traversals: Data.List.Views
This section introduces library views for traversing lists and uses SplitRec to implement a total merge sort.
- Merge sort with SplitRec: Describe how mergeSort uses SplitRec to traverse and sort a list recursively as a total function.
Why it matters: This shows how a recursive view can support a total implementation of merge sort. - Lazy computation annotation: Explain what Lazy indicates and how Idris handles Delay and Force when type-checking.
Why it matters: This helps you read library views that use deferred computations without having to insert Delay and Force yourself. - VList palindrome traversal: Describe how the VList view can traverse a list for a palindrome check.
Why it matters: This provides a traversal shape suited to comparing the two ends of a list.
Key terms: SplitRec, SplitRecNil, SplitRecOne, SplitRecPair, with block, mergeSort, Lazy, Delay, Force, VList
10.3 Data abstraction: hiding the structure of data using views
This section explains how views provide alternative ways to inspect data and support programming against hidden representations.
- Alternative List Views: Describe how SnocList and SplitRec present alternative ways to inspect a list without directly examining its constructors.
Why it matters: Views let you choose a way of inspecting data that suits the function you are writing. - Views and Data Abstraction: Explain how a view can support interactive development of functions while a module hides its data representation.
Why it matters: A view lets users work with data without tying their functions to its hidden representation.
Key terms: views, SnocList, SplitRec, pattern matching, data abstraction, module, case splitting, type-driven development
10.3.1 Digression: modules in Idris
This section explains Idris module export modifiers and how they control visibility and data abstraction.
- Module export modifiers: Distinguish private, export, and public export, including their effects on data constructors and the default visibility.
Why it matters: These modifiers determine what other modules can see and use. - Function definitions in types: Explain why a function definition may need public export when its behavior is used in a type.
Why it matters: A function's signature alone does not expose the behavior needed for this use in types. - Abstract module interfaces: Describe how exporting a type and selected functions while hiding constructors makes the type abstract to importing modules.
Why it matters: An abstract interface lets a module offer useful operations without exposing its data structure.
Key terms: private, export, public export, data constructors, type constructor, type synonyms, type-level functions, abstract data type, export modifiers, pattern-match
10.3.2 The data store, revisited
This section revisits the data store as an abstract module with a fixed schema and motivates a view for traversing its contents.
- Schema Parameterized Data Store: Explain why the DataStore record is parameterized by its schema rather than storing the schema as a field.
Why it matters: The schema parameter expresses how this version of the store treats its schema as fixed. - Store Module Abstraction: Describe what other modules can see and use from the exported store module, and what remains hidden.
Why it matters: The module lets clients construct stores without exposing the record’s internal accessors or function definitions. - Abstract Store Traversal Cases: Explain why a view is useful for traversing the abstract store and describe the two cases it should support.
Why it matters: These cases connect the store’s abstract construction interface to a useful way to inspect its contents.
Key terms: DataStore, schema, parameterized record, export, size, items, empty, addToStore, view
10.3.3 Traversing the store’s contents with a view
This section uses a view to traverse and filter an abstract data store without exposing its internal representation.
- Abstract store traversal: Explain how StoreView and storeView let clients traverse a store while preserving its abstraction.
Why it matters: This lets you write store traversals that are insulated from changes to the internal representation. - Filtering store keys: Trace how filterKeys uses the store view to collect keys whose associated values pass a test.
Why it matters: This example shows how to use an abstract recursive view to implement a useful selection operation. - Infix application operator: Describe how the $ operator reduces brackets in nested function applications, using the section's example.
Why it matters: This notation makes deeply nested store construction easier to read.
Key terms: StoreView, covering function, empty, addToStore, internal representation, filterKeys, SchemaType, SString, val_schema, SNil
10.4 Summary
Where this is used later
- An arithmetic quiz using streams of random numbers (chapter 11) · Views provide an alternative way to analyze data.
- (chapter intro) (chapter 11) · Termination when consuming data, aided by views.
- Digression: what does it mean for a function to be total? (chapter 11) · The earlier discussion of recursive termination.
- Generating infinite structures using Lazy types (chapter 11) · Further develops the use of Lazy types for generating infinite structures.
- Sequencing Commands with do notation (chapter 11) · Modules and namespaces in Idris
- Defining a module for Process (chapter 15) · Module and export concepts
Concepts explained in this chapter
- viewAn alternative representation of a value that exposes its structure for pattern matching without requiring access to the value’s original constructors.
- with constructThe Idris with construct allows a function to pattern-match on an intermediate expression alongside its original inputs.
- merge sortMerge sort is a sorting algorithm that recursively divides a list into smaller parts and merges the sorted parts.
- covering functionA covering function constructs a view corresponding to a value so that the view represents that value's structure.
- SnocList viewSnocList is an example list view that exposes a list as empty or as a prefix followed by its final element.
- recursive viewA recursive view is a view whose structure includes a recursively smaller view of part of the value it represents.
- dependent pattern matchingDependent pattern matching is pattern matching in which matching a value can reveal information about the types or forms of related values.
- snoc listA snoc list is a list representation in which each added element follows the list containing the preceding elements.
- exportThe Idris export modifier makes a declaration's name and type available to importing modules while hiding its definition or data constructors.
- LazyLazy is an Idris type constructor marking a computation whose result is evaluated only when required.
- projection functionA function generated for a record field that retrieves that field’s value from a record.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
