@@ -33,14 +33,17 @@ type Traversal a b s t = Optic Wandering a b s t
3333type Traversal' a s = Traversal a a s s
3434
3535swap :: SymmetricMonoidal m u => Iso (a `m ` b ) (c `m ` d ) (b `m ` a ) (d `m ` c )
36- swap = Optical (dimap Bifunctor. swap Bifunctor. swap)
36+ swap = iso Bifunctor. swap Bifunctor. swap
3737
3838assoc :: SymmetricMonoidal m u => Iso ((a `m ` b ) `m ` c ) ((d `m ` e ) `m ` f ) (a `m ` (b `m ` c )) (d `m ` (e `m ` f ))
39- assoc = Optical (dimap Bifunctor. lassoc Bifunctor. rassoc)
39+ assoc = iso Bifunctor. lassoc Bifunctor. rassoc
4040
4141(.>) :: Optic_ arr a b s t -> Optic_ arr x y a b -> Optic_ arr x y s t
4242Optical f .> Optical g = Optical (f P. . g)
4343
44+ prism :: (b ->. t ) -> (s ->. Either t a ) -> Prism a b s t
45+ prism b s = Optical $ \ f -> dimap s (either id id ) (second (rmap b f))
46+
4447_1 :: Lens a b (a ,c ) (b ,c )
4548_1 = Optical first
4649
@@ -53,6 +56,12 @@ _Left = Optical first
5356_Right :: Prism a b (Either c a ) (Either c b )
5457_Right = Optical second
5558
59+ _Just :: Prism a b (Maybe a ) (Maybe b )
60+ _Just = prism Just (maybe (Left Nothing ) Right )
61+
62+ _Nothing :: Prism' () (Maybe a )
63+ _Nothing = prism (\ () -> Nothing ) Left
64+
5665traversed :: Traversable t => Traversal a b (t a ) (t b )
5766traversed = Optical wander
5867
@@ -66,11 +75,29 @@ get :: Optic_ (Kleisli (Const a)) a b s t -> s -> a
6675get l = gets l P. id
6776
6877gets :: Optic_ (Kleisli (Const r )) a b s t -> (a -> r ) -> s -> r
69- gets (Optical l) f s = getConst (runKleisli (l (Kleisli (Const P. . f))) s)
78+ gets (Optical l) f s = getConst' (runKleisli (l (Kleisli (Const P. . f))) s)
7079
7180set :: Optic_ (-> ) a b s t -> b -> s -> t
7281set (Optical l) x = l (const x)
7382
83+ -- get can't return a linear arrow, so `withIso swap const` needs to be used
84+ -- instead of `get swap`
85+ match :: Optic_ (LKleisli (Either a )) a b s t -> s ->. Either t a
86+ match (Optical l) = withIso swap (\ x _ -> x) . runLKleisli (l (LKleisli Left ))
87+
88+ -- will be redundant with multiplicity polymorphism
89+ match' :: Optic_ (Kleisli (Either a )) a b s t -> s -> Either t a
90+ match' (Optical l) = get swap P. . runKleisli (l (Kleisli Left ))
91+
92+ build :: Optic_ (CoLKleisli (Const b )) a b s t -> b ->. t
93+ build (Optical l) x = runCoLKleisli (l (CoLKleisli getConst')) (Const x)
94+
95+ -- XXX: move this to Prelude
96+ -- | Linearly typed patch for the newtype deconstructor. (Temporary until
97+ -- inference will get this from the newtype declaration.)
98+ getConst' :: Const a b ->. a
99+ getConst' (Const x) = x
100+
74101lengthOf :: Num r => Optic_ (Kleisli (Const (Sum r ))) a b s t -> s -> r
75102lengthOf l s = getSum (gets l (const (Sum 1 )) s)
76103
@@ -81,3 +108,11 @@ over' (Optical l) f = l f
81108
82109traverseOf' :: Optic_ (Kleisli f ) a b s t -> (a -> f b ) -> s -> f t
83110traverseOf' (Optical l) f = runKleisli (l (Kleisli f))
111+
112+ iso :: (s ->. a ) -> (b ->. t ) -> Iso a b s t
113+ iso f g = Optical (dimap f g)
114+
115+ withIso :: Optic_ (Exchange a b ) a b s t -> ((s ->. a ) -> (b ->. t ) -> r ) -> r
116+ withIso (Optical l) f = f fro to
117+ where Exchange fro to = l (Exchange id id )
118+
0 commit comments