-
Notifications
You must be signed in to change notification settings - Fork 2
Lenses
Vlad Patryshev edited this page Feb 5, 2022
·
1 revision
This table of lenses is inspired by the following article. All categories are regular small categories, so things are simpler than in the original paper.
- C and D are categories, a, b, etc are objects.
- a∈C, s∈C - objects, where
a
represents a smaller type, and s represents a larger type. - b∈D, t∈D - objects, where b represents a smaller type, and t represents a larger type.
- We could think of a as a "member" of type s, and of b as a member of type t - meaning that it's rather actually an arrow, s→a
- (•): C × D → D is a monoidal action of C on D
access : s -> Either[t, (a, b => t)]
Name | Formula | Meaning | Code Sample |
---|---|---|---|
Adapter | C(s, a) × D(b, t) | Two components, one extracts an a from an s, another is a factory that produces a t from b | |
Lens | C(s,a)×D(s•b,t) | Two components, one exracts an a from an s, another is a factory that, given an s and a b, produces a t (here C acts on D via (•)) |
view: s => a
build: s => b => t |
Monoidal Lens | CCom(s,a)×C(Us×b,t) | CCom is the category of cocommutative comonoids; U is the forgetful functor | |
Algebraic Lens | C(s,a) × D(Ms • b, t) | A pair, an extractor of a from s, and a producer of t from Ms • b. M is a monad. Same as regular lense, but the factory takes Ms • b.
When M is When M is |
view: s => a
build: M[s] => b => t The case of M is view: s => a
update: s => b => t
create: b => t |
Monadic Lens | C(s,a) × C(s×b, Mt) | Same as the regular lens, but M is a monad, and so we have a side effect when updating |
view: s => a
build: s => b => M[t] |
Linear Lens | C(s, tb • a) | Here tb is an internal Hom in C (the right adjoint to ×). |
putget: s => (b => t, a) |
Prism | C(s, t•a) × D(b, t) | a prism from (S,T) to (A,B) is a lens from (T,S) to (B,A) in the opposite categories Dop and Cop. However, they can also be seen as optics from (A, B) to (S, T). |
decompose: s => (t, a)
create: b => t |
Coalg. Prism | C(s, Θt • a) × D(b, t) |
cMatch: s => Either[(c, t), a]
build: b => t |
|
Grate | D(as • b, t) | Grates create a new structure provided a way of creating a new focus from a view function. |
grate: ((s => a) => b) => t |
Glass | C(s × bas, t) | In functional programming, glasses can be implemented by a single function glass that takes a way of transforming views into new foci and uses it to update the data structure. |
glass: ((s => a) => b) => (s => t) |
Affine Traversal | C(s, t + a × tb) | ||
Traversal | C(s, ∫n∈ℕan×tbn) |
extract: s => (List[a], List[b] => t) |
|
Kaleidoscope | ∫n∈ℕ V((bn)an, tsn) | Kaleidoscopes are optics for the action of applicative functors. Known in Haskell programming as the FunList applicative |
aggregate: (List[a] => b) => List[s] => t |
Getter | C(s, a) |
get: s => a |
|
Review | D(b, t) |
review: b => t |
|
Setter | C(ba, ts) |
over: (a => b) => s => t |
|
Fold | V(s,La) | The category of foldable functors is the slice category on the list functor L: V → V, also known as the free monoid functor. Folds are degenerate optics for the action of foldable functors |