Skip to content

Commit dd65d13

Browse files
author
Bhavik Mehta
authored
Merge pull request #77 from tweag/better-match
Modify `match` to work for more optics
2 parents d1808d7 + e558ac5 commit dd65d13

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Control/Optics/Linear/Internal.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ module Control.Optics.Linear.Internal
2424
, traversed
2525
-- * Using optics
2626
, get, set, gets
27-
, match, match', build
27+
, match, build
2828
, over, over'
2929
, traverseOf, traverseOf'
3030
, lengthOf
31-
, withIso
31+
, withIso, withPrism
3232
-- * Constructing optics
3333
, iso, prism
3434
)
@@ -106,12 +106,8 @@ gets (Optical l) f s = getConst' (NonLinear.runKleisli (l (NonLinear.Kleisli (Co
106106
set :: Optic_ (->) a b s t -> b -> s -> t
107107
set (Optical l) x = l (const x)
108108

109-
match :: Optic_ (Linear.Kleisli (Either a)) a b s t -> s ->. Either t a
110-
match (Optical l) = withIso swap (\x _ -> x) . Linear.runKleisli (l (Linear.Kleisli Left))
111-
112-
-- will be redundant with multiplicity polymorphism
113-
match' :: Optic_ (NonLinear.Kleisli (Either a)) a b s t -> s -> Either t a
114-
match' (Optical l) = withIso swap (\x _ -> forget x) P.. NonLinear.runKleisli (l (NonLinear.Kleisli Left))
109+
match :: Optic_ (Market a b) a b s t -> s ->. Either t a
110+
match (Optical l) = snd (runMarket (l (Market id Right)))
115111

116112
build :: Optic_ (Linear.CoKleisli (Const b)) a b s t -> b ->. t
117113
build (Optical l) x = Linear.runCoKleisli (l (Linear.CoKleisli getConst')) (Const x)
@@ -139,3 +135,7 @@ iso f g = Optical (dimap f g)
139135
withIso :: Optic_ (Exchange a b) a b s t -> ((s ->. a) -> (b ->. t) -> r) -> r
140136
withIso (Optical l) f = f fro to
141137
where Exchange fro to = l (Exchange id id)
138+
139+
withPrism :: Optic_ (Market a b) a b s t -> ((b ->. t) -> (s ->. Either t a) -> r) -> r
140+
withPrism (Optical l) f = f b m
141+
where Market b m = l (Market id Right)

src/Data/Profunctor/Linear.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Data.Profunctor.Linear
1414
, Wandering(..)
1515
, LinearArrow(..), getLA
1616
, Exchange(..)
17+
, Market(..), runMarket
1718
) where
1819

1920
import qualified Data.Functor.Linear as Data
@@ -101,3 +102,13 @@ instance Prelude.Applicative f => Strong Either Void (Kleisli f) where
101102
first (Kleisli f) = Kleisli $ \case
102103
Left x -> Prelude.fmap Left (f x)
103104
Right y -> Prelude.pure (Right y)
105+
106+
data Market a b s t = Market (b ->. t) (s ->. Either t a)
107+
runMarket :: Market a b s t ->. (b ->. t, s ->. Either t a)
108+
runMarket (Market f g) = (f, g)
109+
110+
instance Profunctor (Market a b) where
111+
dimap f g (Market h k) = Market (g . h) (either (Left . g) Right . k . f)
112+
113+
instance Strong Either Void (Market a b) where
114+
first (Market f g) = Market (Left . f) (either (either (Left . Left) Right . g) (Left . Right))

0 commit comments

Comments
 (0)