Bidirectional transformation

In computer programming, bidirectional transformations (bx) are programs in which a single piece of code can be run in several ways, such that the same data are sometimes considered as input, and sometimes as output.

For example, a bx run in the forward direction might transform input I into output O, while the same bx run backward would take as input versions of I and O and produce a new version of I as its output.

More general is a lens language, in which there is a distinguished forward direction ("get") that takes a concrete input to an abstract output, discarding some information in the process: the concrete state includes all the information that is in the abstract state, and usually some more.

Lenses are required to obey certain conditions to ensure sensible behaviour.

The most general case is that of symmetric bidirectional transformations.

Unlike a lens, a prism may not always give a view; also unlike a lens, given a prism, a view is sufficient to construct a source.

If lenses allow "focusing" (viewing, updating) on a part of a product type, prisms allow focusing (possible viewing, building) on a part of a sum type.

Both lenses and prisms, as well as other constructions such as traversals, are more general notion of bidirectional transformations known as optics.