SkillAgentSearch skills...

Vitrea

Optics via the profunctor representation theorem

Install / Use

/learn @mroman42/Vitrea
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

  • vitrea

Pure profunctor lenses and optics via the /profunctor representation theorem/. You can read more about this representation in the following articles.

  • [[https://bartoszmilewski.com/2017/07/07/profunctor-optics-the-categorical-view/]["Profunctor optics: the categorical view"]], Milewski (2017).
  • [[https://www.cs.ox.ac.uk/jeremy.gibbons/publications/proyo.pdf]["What you needa know about Yoneda"]], Boisseau, Gibbons (2018).
  • [[https://arxiv.org/abs/1809.00738]["Categories of optics"]], Riley (2018).
  • [[https://arxiv.org/abs/2001.07488]["Profunctor optics: a categorical update"]], Clarke, Elkins, Gibbons, Loregian, Milewski, Pillmore, Román (2019).

All the optics on this library are particular cases of a single unified definition. All the combinators on this library are instantiations of the profunctor optic to a particular Tambara module.

The usual notions of lawfulness (as in [[https://arxiv.org/abs/1809.00738]["Categories of optics"]]) apply to non-mixed optics (lenses, prisms, traversals...). However, there is not a consensus on what the natural notion of laws for a mixed optic should be, apart from specific cases like monadic lenses.

** Examples

The library follows, where possible, the style of combinators of the [[https://github.com/ekmett/lens/wiki/operators][lens library]]. Example code from [[https://arxiv.org/abs/2001.07488]["Profunctor optics: a categorical update"]] works directly under the library.

Example 1. (Lenses and prisms) The composition of a prism (~address~) and a lens (~street~) is used to parse a string and then access and modify one of its subfields.

#+begin_src haskell let home = "221b Baker St, London, UK" address :: Prism Address String street :: Lens String Address

home?. address.street Just "221b Baker St" home & address.street. ̃ "4 Marylebone Rd" "4 Marylebone Rd, London, UK" #+end_src

Example 2. (Monadic lenses) A polymorphic family of type-changing monadic lenses for a logging monad is used to track each time a data holder (~Box~) is accessed.

#+begin_src haskell box :: (Show b) => MonadicLens IO a b (Box a) (Box b)

return (Box 42) >>= mupdate box "hello" >>= mupdate box "world" [box]: contents changed to "hello". [box]: contents changed to "world". Box{"world"} #+end_src

Example 3. (Algebraic lenses) A classifying lens (~measure~) is used both for accessing a the measurements of a point in the iris dataset and to classify new measurements into a species (~Versicolor~).

#+begin_src haskell let iris = [ Iris Setosa 4.9 3.0 1.4, 0.2 , Iris Setosa 4.7 3.2 1.3 0.2 , ... , Iris Virginica 5.9 3.0 5.1 1.8 ] measure :: AlgLens [] Measurements Flower

iris!!4 ˆ. measure (5.0, 3.6, 1.4, 0.2) iris & measure .? Measurements (4.8, 3.2, 3.5, 2.1) Iris Versicolor (4.8, 3.1, 1.5, 0.1) #+end_src

Example 4. (Traversals) The composition of a traversal (~each~) with a prism (~address~) and a lens (~city~) is used to parse a collection of strings and modify one of their subfields.

#+begin_src haskell let mail = [ "43 Adlington Rd, Wilmslow, United Kingdom" , "26 Westcott Rd, Princeton, USA" , "St James's Square, London, United Kingdom" ] each :: Traversal String [String] address :: Prism Address String city :: Lens String Address

mail & each.address.city % ̃ uppercase [ "43 Adlington Rd, WILMSLOW, United Kingdom" , "26 Westcott Rd, PRINCETON, USA" , "St James's Square, LONDON, United Kingdom" ] #+end_src haskell

Example 5. (Kaleidoscopes) Following the previous Example 3, a kaleidoscope (~aggregate~) is composed with an algebraic lens to create a new point on the dataset by aggregating measurements with some function (~mean~, ~maximum~) and then classifying it.

#+begin_src haskell measure :: AlgebraicLens [] Measurements Flower aggregate :: Kaleidoscope Float Measurements

iris & measure.aggregate >- mean Iris Versicolor; Sepal (5.843, 3.054); Petal (3.758, 1.198) #+end_src haskell

** About /This is a complete rewriting from the first prototype of [[https://github.com/mroman42/vitrea-prototype-1][vitrea]], maintained by @emilypi and @mroman42./

View on GitHub
GitHub Stars24
CategoryDevelopment
Updated1y ago
Forks3

Languages

Haskell

Security Score

75/100

Audited on Feb 11, 2025

No findings