liveBook · Manning

Covering function

As explained in Type-Driven Development with Idris

A covering function constructs a view corresponding to a value so that the view represents that value's structure.

A covering function converts a value into a view of that value. The view describes its structure in a form that dependent pattern matching can use. By convention, the function takes the view type’s name with an initial lowercase letter. section 10.1.2 section 10.4

total                                                                #1
listLast : (xs : List a) -> ListLast xs
listLast [] = Empty
listLast (x :: xs) = case listLast xs of                             #2
                          Empty => NonEmpty [] x
                          NonEmpty ys y => NonEmpty (x :: ys) y

The listLast implementation recursively builds a ListLast view, exposing a list’s final element. section 10.1.2

Traversing a list from the end

The recursive SnocList view represents a list from its final element backward. Its covering function, snocList, supplies that view to myReverseHelper, which pattern-matches on it to build a reversed list. section 10.2.1

Views can also support data abstraction: a module can hide a store’s representation while exporting a view and its covering function for traversal. section 10.3

How Type-Driven Development with Idris covers it

How deep it goes
Defines the conversion role and naming convention, then works through recursive SnocList construction using equality rewrites.
Examples
Uses ListLast to expose a list’s final element, SnocList to reverse traversal, and StoreView to traverse hidden data.
What you can do after
Build a covering function that converts a list into a matching ListLast or SnocList view.
How it connects
Connects views with dependent types, dependent pattern matching, and alternative list traversals.

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.