Skip to content

Commit abe2ed0

Browse files
committed
re function that reverses (at least) Isos and
`Prism`s.
1 parent 361ca29 commit abe2ed0

File tree

8 files changed

+107
-9
lines changed

8 files changed

+107
-9
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"purescript-profunctor": "~0.3.0",
2424
"purescript-sets": "~0.5.1",
2525
"purescript-unsafe-coerce": "~0.1.0",
26-
"purescript-transformers": "~0.7.1"
26+
"purescript-transformers": "~0.8.1"
2727
}
2828
}

src/Data/Lens/Fold.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Prelude
1212

1313
import Control.Apply ((*>))
1414

15-
import Data.Const (Const(..), getConst)
1615
import Data.Either (Either(..), either)
1716
import Data.Foldable (Foldable, foldr)
1817
import Data.Functor.Contravariant (Contravariant)
@@ -32,6 +31,7 @@ import Data.Profunctor.Star (Star(..), runStar)
3231
import Data.Tuple (Tuple(..))
3332

3433
import Data.Lens.Internal.Void (coerce)
34+
import Data.Lens.Internal.Forget (Forget (..), runForget)
3535
import Data.Lens.Types (Fold(), FoldP()) as ExportTypes
3636
import Data.Lens.Types (Optic(), OpticP(), Fold())
3737

@@ -48,11 +48,11 @@ preview p = runFirst <<< foldMapOf p (First <<< Just)
4848

4949
-- | Folds all foci of a `Fold` to one. Note that this is the same as `view`.
5050
foldOf :: forall s t a b. Fold a s t a b -> s -> a
51-
foldOf p = getConst <<< runStar (p (Star Const))
51+
foldOf p = runForget (p (Forget id))
5252

5353
-- | Maps and then folds all foci of a `Fold`.
5454
foldMapOf :: forall s t a b r. Fold r s t a b -> (a -> r) -> s -> r
55-
foldMapOf p f = getConst <<< runStar (p (Star (Const <<< f)))
55+
foldMapOf p f = runForget (p (Forget f))
5656

5757
-- | Right fold over a `Fold`.
5858
foldrOf :: forall s t a b r. Fold (Endo r) s t a b -> (a -> r -> r) -> r -> s -> r

src/Data/Lens/Getter.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ module Data.Lens.Getter
66
, module Data.Lens.Types
77
) where
88

9-
import Prelude ((<<<))
9+
import Prelude (id, (<<<))
1010

1111
import Data.Const (Const(..), getConst)
1212
import Data.Functor.Contravariant (Contravariant, cmap)
1313
import Data.Profunctor.Star (Star(..), runStar)
1414
import Control.Monad.State.Class (MonadState, gets)
1515

16+
import Data.Lens.Internal.Forget (Forget (..), runForget)
1617
import Data.Lens.Types (Getter(), Optic())
1718

1819
infixl 8 ^.
1920

2021
-- | View the focus of a `Getter`.
2122
view :: forall s t a b. Getter s t a b -> s -> a
22-
view l s = getConst (runStar (l (Star Const)) s)
23+
view l = runForget (l (Forget id))
2324

2425
-- | Synonym for `view`, flipped.
2526
(^.) :: forall s t a b. s -> Getter s t a b -> a

src/Data/Lens/Internal/Forget.purs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Data.Lens.Internal.Forget where
2+
3+
import Prelude
4+
5+
import Data.Tuple (Tuple (..), fst, snd)
6+
import Data.Either (Either (..), either)
7+
import Data.Monoid (Monoid, mempty)
8+
import Data.Const (Const (..), getConst)
9+
import Data.Profunctor (Profunctor)
10+
import Data.Profunctor.Strong (Strong)
11+
import Data.Profunctor.Choice (Choice)
12+
import Data.Profunctor.Cochoice (Cochoice)
13+
14+
import Data.Lens.Internal.Wander (Wander)
15+
16+
-- | Profunctor that forgets the `b` value and returns (and accumulates) a
17+
-- | value of type `r`.
18+
-- |
19+
-- | `Forget r` is isomorphic to `Star (Const r)`, but can be given a `Cochoice`
20+
-- | instance.
21+
newtype Forget r a b = Forget (a -> r)
22+
23+
-- | Unwrap a value of type `Forget`.
24+
runForget :: forall r a b. Forget r a b -> a -> r
25+
runForget (Forget z) = z
26+
27+
instance profunctorForget :: Profunctor (Forget r) where
28+
dimap f _ (Forget z) = Forget (z <<< f)
29+
30+
instance choiceForget :: (Monoid r) => Choice (Forget r) where
31+
left (Forget z) = Forget (either z mempty)
32+
right (Forget z) = Forget (either mempty z)
33+
34+
instance strongForget :: Strong (Forget r) where
35+
first (Forget z) = Forget (z <<< fst)
36+
second (Forget z) = Forget (z <<< snd)
37+
38+
instance cochoiceForget :: Cochoice (Forget r) where
39+
unleft (Forget z) = Forget (z <<< Left)
40+
unright (Forget z) = Forget (z <<< Right)
41+
42+
instance wanderForget :: (Monoid r) => Wander (Forget r) where
43+
wander f (Forget r) = Forget \s -> getConst (f (Const <<< r) s)
44+
45+
-- forall s t a b. (forall f. (Applicative f) => (a -> f b) -> s -> f t)
46+
-- -> p a b -> p s t

src/Data/Lens/Internal/Re.purs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- | This module defines the `Re` profunctor
2+
3+
module Data.Lens.Internal.Re where
4+
5+
import Prelude
6+
7+
import Data.Profunctor
8+
import Data.Profunctor.Strong
9+
import Data.Profunctor.Choice
10+
import Data.Profunctor.Cochoice
11+
import Data.Profunctor.Costrong
12+
13+
--
14+
newtype Re p s t a b = Re (p b a -> p t s)
15+
16+
runRe :: forall p s t a b. Re p s t a b -> p b a -> p t s
17+
runRe (Re r) = r
18+
19+
instance profunctorRe :: (Profunctor p) => Profunctor (Re p s t) where
20+
dimap f g (Re r) = Re (r <<< dimap g f)
21+
22+
instance choiceRe :: (Choice p) => Cochoice (Re p s t) where
23+
unleft (Re r) = Re (r <<< left)
24+
unright (Re r) = Re (r <<< right)
25+
26+
instance cochoiceRe :: (Cochoice p) => Choice (Re p s t) where
27+
left (Re r) = Re (r <<< unleft)
28+
right (Re r) = Re (r <<< unright)
29+
30+
instance strongRe :: (Strong p) => Costrong (Re p s t) where
31+
unfirst (Re r) = Re (r <<< first)
32+
unsecond (Re r) = Re (r <<< second)
33+
34+
instance costrongRe :: (Costrong p) => Strong (Re p s t) where
35+
first (Re r) = Re (r <<< unfirst)
36+
second (Re r) = Re (r <<< unsecond)

src/Data/Lens/Internal/Tagged.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module Data.Lens.Internal.Tagged where
44

55
import Data.Profunctor (Profunctor)
66
import Data.Profunctor.Choice (Choice)
7+
import Data.Profunctor.Costrong (Costrong)
78
import Data.Either (Either(..))
9+
import Data.Tuple (Tuple(..))
810

911
newtype Tagged a b = Tagged b
1012

@@ -15,5 +17,9 @@ instance taggedChoice :: Choice Tagged where
1517
left (Tagged x) = Tagged (Left x)
1618
right (Tagged x) = Tagged (Right x)
1719

20+
instance taggedCostrong :: Costrong Tagged where
21+
unfirst (Tagged (Tuple b _)) = Tagged b
22+
unsecond (Tagged (Tuple _ c)) = Tagged c
23+
1824
unTagged :: forall a b. Tagged a b -> b
1925
unTagged (Tagged x) = x

src/Data/Lens/Iso.purs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- | This module defines functions for working with isomorphisms.
22

33
module Data.Lens.Iso
4-
( iso, withIso, cloneIso, au, auf, under, curried, uncurried, flipped
4+
( iso, withIso, cloneIso, re, au, auf, under, curried, uncurried, flipped
55
, module Data.Lens.Types
66
) where
77

@@ -10,7 +10,7 @@ import Prelude ((<<<), flip, id)
1010
import Data.Profunctor (Profunctor, dimap, rmap)
1111
import Data.Tuple (Tuple(), curry, uncurry)
1212

13-
import Data.Lens.Types (Iso(), IsoP(), AnIso(), AnIsoP(), Exchange(..))
13+
import Data.Lens.Types (Iso(), IsoP(), AnIso(), AnIsoP(), Optic(), Exchange(..), Re(..), runRe)
1414

1515
-- | Create an `Iso` from a pair of morphisms.
1616
iso :: forall s t a b. (s -> a) -> (b -> t) -> Iso s t a b
@@ -25,6 +25,10 @@ withIso l f = case l (Exchange id id) of
2525
cloneIso :: forall s t a b. AnIso s t a b -> Iso s t a b
2626
cloneIso l = withIso l \x y p -> iso x y p
2727

28+
-- | Reverses an optic.
29+
re :: forall p s t a b. Optic (Re p a b) s t a b -> Optic p b a t s
30+
re t = runRe (t (Re id))
31+
2832
au :: forall s t a b e. AnIso s t a b -> ((b -> t) -> e -> s) -> e -> a
2933
au l = withIso l \sa bt f e -> sa (f bt e)
3034

src/Data/Lens/Types.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ module Data.Lens.Types
66
, module Data.Lens.Internal.Market
77
, module Data.Lens.Internal.Shop
88
, module Data.Lens.Internal.Tagged
9+
, module Data.Lens.Internal.Forget
910
, module Data.Lens.Internal.Wander
11+
, module Data.Lens.Internal.Re
1012
) where
1113

1214
import Data.Const (Const())
1315
import Data.Profunctor (Profunctor)
1416
import Data.Profunctor.Choice (Choice)
1517
import Data.Profunctor.Star (Star())
1618
import Data.Profunctor.Strong (Strong)
19+
import Data.Profunctor.Closed (Closed)
1720

1821
import Data.Lens.Internal.Exchange (Exchange(..))
1922
import Data.Lens.Internal.Market (Market(..))
2023
import Data.Lens.Internal.Shop (Shop(..))
2124
import Data.Lens.Internal.Tagged (Tagged(..), unTagged)
25+
import Data.Lens.Internal.Forget (Forget(..), runForget)
2226
import Data.Lens.Internal.Wander (Wander, wander)
27+
import Data.Lens.Internal.Re (Re(..), runRe)
2328

2429
-- | A general-purpose Data.Lens.
2530
type Optic p s t a b = p a b -> p s t
@@ -63,5 +68,5 @@ type Review s t a b = Optic Tagged s t a b
6368
type ReviewP s a = Review s s a a
6469

6570
-- | A fold.
66-
type Fold r s t a b = Optic (Star (Const r)) s t a b
71+
type Fold r s t a b = Optic (Forget r) s t a b
6772
type FoldP r s a = Fold r s s a a

0 commit comments

Comments
 (0)