@@ -27,21 +27,53 @@ import Data.Profunctor.Choice (class Choice)
2727import Data.Profunctor.Closed (class Closed )
2828import Data.Profunctor.Strong (class Strong )
2929
30- -- | Given a type whose "focus element" can always be retrieved ,
31- -- | a lens provides a convenient way to view, get , and transform
30+ -- | Given a type whose "focus element" always exists ,
31+ -- | a lens provides a convenient way to view, set , and transform
3232-- | that element.
3333-- |
34- -- | `_2` is a Tuple -specific `Lens` available from `Data.Lens`, so:
34+ -- | For example, `_2` is a tuple -specific `Lens` available from `Data.Lens`, so:
3535-- | ```purescript
36- -- | > import Data.Lens
37- -- | > over _2 String.length $ Tuple "ignore" "four"
38- -- | (Tuple "ignore" 4)
36+ -- | over _2 String.length $ Tuple "ignore" "four" == Tuple "ignore" 4
3937-- | ```
38+ -- | Note the result has a different type than the original tuple.
39+ -- | That is, the four `Lens` type variables have been narrowed to:
40+ -- |
41+ -- | * `s` is `Tuple String String`
42+ -- | * `t` is `Tuple String Int`
43+ -- | * `a` is `String`
44+ -- | * `b` is `Int`
45+ -- |
46+ -- | See `Data.Lens.Getter` and `Data.Lens.Setter` for functions and operators
47+ -- | frequently used with lenses.
48+
49+
4050type Lens s t a b = forall p . Strong p => Optic p s t a b
4151
42- -- | `Lens` allows `set` to change the type of the focus. Often, a
43- -- | particular lens won't do that. This type alias declares that `set`
44- -- | only changes values, not types.
52+ -- | `Lens'` is a specialization of `Lens`. An optic of type `Lens'`
53+ -- | can change only the value of its focus,
54+ -- | not its type. As an example, consider the `Lens` `_2`, which has this type:
55+ -- |
56+ -- | ```purescript
57+ -- | _2 :: forall s t a b. Lens (Tuple s a) (Tuple t b) a b
58+ -- | ```
59+ -- |
60+ -- | `_2` can produce a `Tuple Int String` from a `Tuple Int Int`:
61+ -- |
62+ -- | ```purescript
63+ -- | set _2 "NEW" (Tuple 1 2) == (Tuple 1 "NEW")
64+ -- | ```
65+ -- |
66+ -- | If we specialize `_2`'s type with `Lens'`, the following will not
67+ -- | type check:
68+ -- |
69+ -- | ```purescript
70+ -- | set (_2 :: Lens' (Tuple Int Int) Int) "NEW" (Tuple 1 2)
71+ -- | ^^^^^^^^^^^^^^^^^^^^^^^^^
72+ -- | ```
73+ -- |
74+ -- | See `Data.Lens.Getter` and `Data.Lens.Setter` for functions and operators
75+ -- | frequently used with lenses.
76+
4577type Lens' s a = Lens s s a a
4678
4779-- | A prism.
0 commit comments