Profunctor optics: Modular data accessors

<p>Data accessors allow one to read and write components of a data structure, such as the fields of a record, the variants of a union, or the elements of a container. These data accessors are collectively known as optics; they are fundamental to programs that manipulate complex data. Individua...

Full description

Bibliographic Details
Main Authors: Pickering, M, Gibbons, J, Wu, N
Format: Journal article
Published: Aspect-Oriented Software Association 2017
_version_ 1797083993587318784
author Pickering, M
Gibbons, J
Wu, N
author_facet Pickering, M
Gibbons, J
Wu, N
author_sort Pickering, M
collection OXFORD
description <p>Data accessors allow one to read and write components of a data structure, such as the fields of a record, the variants of a union, or the elements of a container. These data accessors are collectively known as optics; they are fundamental to programs that manipulate complex data. Individual data accessors for simple data structures are easy to write, for example as pairs of ‘getter’ and ‘setter’ methods. However, it is not obvious how to combine data accessors, in such a way that data accessors for a compound data structure are composed out of smaller data accessors for the parts of that structure. Generally, one has to write a sequence of statements or declarations that navigate step by step through the data structure, accessing one level at a time—which is to say, data accessors are traditionally not first-class citizens, combinable in their own right.</p> <br/> <p>We present a framework for modular data access, in which individual data accessors for simple data structures may be freely combined to obtain more complex data accessors for compound data structures. Data accessors become first-class citizens. The framework is based around the notion of profunctors, a flexible generalization of functions. The language features required are higher-order functions (‘lambdas’ or ‘closures’), parametrized types (‘generics’ or ‘abstract types’) of higher kind, and some mechanism for separating interfaces from implementations (‘abstract classes’ or ‘modules’). We use Haskell as a vehicle in which to present our constructions, but other languages such as Scala that provide the necessary features should work just as well. We provide implementations of all our constructions, in the form of a literate program: the manuscript file for the paper is also the source code for the program, and the extracted code is available separately for evaluation. We also prove the essential properties, demonstrating that our profunctor-based representations are precisely equivalent to the more familiar concrete representations. Our results should pave the way to simpler ways of writing programs that access the components of compound data structures.</p>
first_indexed 2024-03-07T01:49:22Z
format Journal article
id oxford-uuid:9989be57-a045-4504-b9d7-dc93fd508365
institution University of Oxford
last_indexed 2024-03-07T01:49:22Z
publishDate 2017
publisher Aspect-Oriented Software Association
record_format dspace
spelling oxford-uuid:9989be57-a045-4504-b9d7-dc93fd5083652022-03-27T00:15:04ZProfunctor optics: Modular data accessorsJournal articlehttp://purl.org/coar/resource_type/c_dcae04bcuuid:9989be57-a045-4504-b9d7-dc93fd508365Symplectic Elements at OxfordAspect-Oriented Software Association2017Pickering, MGibbons, JWu, N<p>Data accessors allow one to read and write components of a data structure, such as the fields of a record, the variants of a union, or the elements of a container. These data accessors are collectively known as optics; they are fundamental to programs that manipulate complex data. Individual data accessors for simple data structures are easy to write, for example as pairs of ‘getter’ and ‘setter’ methods. However, it is not obvious how to combine data accessors, in such a way that data accessors for a compound data structure are composed out of smaller data accessors for the parts of that structure. Generally, one has to write a sequence of statements or declarations that navigate step by step through the data structure, accessing one level at a time—which is to say, data accessors are traditionally not first-class citizens, combinable in their own right.</p> <br/> <p>We present a framework for modular data access, in which individual data accessors for simple data structures may be freely combined to obtain more complex data accessors for compound data structures. Data accessors become first-class citizens. The framework is based around the notion of profunctors, a flexible generalization of functions. The language features required are higher-order functions (‘lambdas’ or ‘closures’), parametrized types (‘generics’ or ‘abstract types’) of higher kind, and some mechanism for separating interfaces from implementations (‘abstract classes’ or ‘modules’). We use Haskell as a vehicle in which to present our constructions, but other languages such as Scala that provide the necessary features should work just as well. We provide implementations of all our constructions, in the form of a literate program: the manuscript file for the paper is also the source code for the program, and the extracted code is available separately for evaluation. We also prove the essential properties, demonstrating that our profunctor-based representations are precisely equivalent to the more familiar concrete representations. Our results should pave the way to simpler ways of writing programs that access the components of compound data structures.</p>
spellingShingle Pickering, M
Gibbons, J
Wu, N
Profunctor optics: Modular data accessors
title Profunctor optics: Modular data accessors
title_full Profunctor optics: Modular data accessors
title_fullStr Profunctor optics: Modular data accessors
title_full_unstemmed Profunctor optics: Modular data accessors
title_short Profunctor optics: Modular data accessors
title_sort profunctor optics modular data accessors
work_keys_str_mv AT pickeringm profunctoropticsmodulardataaccessors
AT gibbonsj profunctoropticsmodulardataaccessors
AT wun profunctoropticsmodulardataaccessors