Abstract

Functional programmers have an established tradition of using traversals as a design pattern to work with recursive data structures. The technique is so prolific that a whole host of libraries have been designed to help in the task of automatically providing traversals by analysing the generic structure of data types. More recently, lenses have entered the functional scene and have proved themselves to be a simple and versatile mechanism for working with product types. They make it easy to focus on the salient parts of a data structure in a composable and reusable manner. This paper uses the combination of lenses and traversals to give rise to a library with unprecedented expressivity and flexibility for querying and modifying complex data structures. Furthermore, since lenses and traversals are based on the generic shape of data, this information is used to generate code that is as efficient as hand-optimised versions. The technique leverages the structure of data to produce generic abstractions that are then eliminated by the standard workhorses of modern functional compilers: inlining and specialisation.

Highlights

  • Traversals are a ubiquitous way of querying and manipulating data

  • Implementing param poses a number of new challenges: Locating parameters The traversal must distinguish between values that correspond to the queried type parameter, and values that were monomorphically defined

  • (gl) generic-lens The library which we describe in this paper. geniplate-0.7.6 [Augustsson 2018] A library which provides a similar interface to the uniplate library below but uses Template Haskell in order to generate traversals. uniplate-1.6.12 [Mitchell and Runciman 2007] A library which provides an interface for traversing data

Read more

Summary

INTRODUCTION

Traversals are a ubiquitous way of querying and manipulating data They provide a reliable interface for working with data types in a structured and predictable manner. 2003], treats this problem by performing run-time type tests to decide which part of the tree to traverse. In this paper we present generic-lens, a Haskell library that provides a suite of traversals that is both faster and richer than SYB. To have a taste of the generic-lens library, consider a data type of weighted trees. The generic-lens library provides a traversal for this data type called param which takes a type-level integer as an argument. This allows a traversal to specify which parameter it wants to focus on.

TYPE-DIRECTED QUERIES
BACKGROUND
GENERIC TRAVERSALS WITH TYPES
GENERIC TRAVERSALS WITH PARAMETERS
Locating Parameters
Type Inference
GENERIC TRAVERSALS WITH CLASS
PERFORMANCE
Evidence Generation
Inlining
Specialisation
Inspection Testing
BENCHMARKS
10 RELATED WORK
11 CONCLUSION

Talk to us

Join us for a 30 min session where you can share your feedback and ask us any queries you have

Schedule a call

Disclaimer: All third-party content on this website/platform is and will remain the property of their respective owners and is provided on "as is" basis without any warranties, express or implied. Use of third-party content does not indicate any affiliation, sponsorship with or endorsement by them. Any references to third-party content is to identify the corresponding services and shall be considered fair use under The CopyrightLaw.