liveBook · Manning

With construct

As explained in Type-Driven Development with Idris

The Idris with construct allows a function to pattern-match on an intermediate expression alongside its original inputs.

The Idris with construct lets a function pattern-match on an intermediate expression alongside its original inputs. It is especially useful with views: dependent types that describe alternative ways to inspect a value. A with block provides a concise alternative to defining a separate helper function for matching the value and its view. section 10.0 section 10.1.3

For example, matching a list as an initial portion plus its last element cannot generally be done by writing a pattern with ++ alone: Idris cannot infer an arbitrary function’s inputs from its result. A covering function such as listLast constructs a ListLast view that makes this pattern available. section 10.1 section 10.1.2

Matching the end of a list

With listLast and ListLast defined, write: describeListEnd : List Int -> String describeListEnd input with (listLast input) describeListEnd [] | Empty = "Empty" describeListEnd (xs ++ [x]) | (NonEmpty xs x) = "Non-empty, initial portion = " ++ show xs The expression in parentheses supplies an additional value to match. Each indented clause places its pattern after |. The view pattern refines the original input pattern: NonEmpty xs x describes a list of the form xs ++ [x]. Indentation determines the block’s scope. section 10.1.3 section 10.1.2

To develop a block interactively, start with a function type and a hole, then add with and its intermediate expression. Before splitting, the hole’s context shows both input : List Int and with_pat : ListLast input. Case-splitting with_pat produces the compatible input and view patterns; fill in the resulting holes. section 10.1.3

For a recursive view, calling the top-level function again can rebuild the view and make totality harder to establish. Instead, pass the recursive view directly to the with block using | on the right-hand side. section 10.2.2

myReverse : List a -> List a
myReverse input with (snocList input)
  myReverse [] | Empty = []
  myReverse (xs ++ [x]) | (Snoc rec) = x :: myReverse xs | rec

The reverse example passes rec directly in myReverse xs | rec. This avoids rebuilding the view; the resulting definition is total and runs in linear time. section 10.2.2

With blocks can also be nested. To check whether one list is a suffix of another, match a reverse view of the first list, then match a reverse view of the second within the non-empty case. Compare their last elements and continue with their recursive views when the elements agree. section 10.2.3

The same reuse pattern applies to a split view: merge sort can recursively sort each half using its existing view and merge the results. A totality check and example evaluations help verify the implementation. section 10.2.4

How Type-Driven Development with Idris covers it

How deep it goes
Works through syntax, dependent case splits, nested blocks, recursive view reuse, and totality feedback.
Examples
Develops list-end description, reverse, suffix checking, and merge sort using list views.
What it assumes
Assumes familiarity with function types, lists, and basic case-splitting while developing view-based matching through examples.
What you can do after
Apply it in Idris to match views, reuse recursive views, and check recursive list functions for totality.
How it connects
Connects dependent pattern matching, views, covering functions, recursion, and totality checking.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.