"Wither" 的 Profunctor 表示是什么?
What's the Profunctor Representation of "Wither"?
This article by Chris Penner 谈论“凋零光学”;可用于从结构中过滤项目的光学器件。
本文对这些光学器件使用以下“Van Laarhoven”表示法:
type Wither s t a b = forall f. Alternative f => (a -> f b) -> s -> f t
大多数(如果不是全部)Van Laarhoven 光学器件具有等效的profunctor 表示。例如镜头:
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
相当于:
type Lens s t a b = forall p. Strong p => p a b -> p s t
Wither
是否也有 Profuctor 代表?如果是,那是什么?
这里是克里斯;这是我对 profunctor 光学表示的看法:
import Data.Profunctor
import Data.Profunctor.Traversing
import Control.Applicative
class (Traversing p) => Withering p where
cull :: (forall f. Alternative f => (a -> f b) -> (s -> f t)) -> p a b -> p s t
instance Alternative f => Withering (Star f) where
cull f (Star amb) = Star (f amb)
instance Monoid m => Withering (Forget m) where
cull f (Forget h) = Forget (getAnnihilation . f (AltConst . Just . h))
where
getAnnihilation (AltConst Nothing) = mempty
getAnnihilation (AltConst (Just m)) = m
newtype AltConst a b = AltConst (Maybe a)
deriving stock (Eq, Ord, Show, Functor)
instance Monoid a => Applicative (AltConst a) where
pure _ = (AltConst (Just mempty))
(AltConst Nothing) <*> _ = (AltConst Nothing)
_ <*> (AltConst Nothing) = (AltConst Nothing)
(AltConst (Just a)) <*> (AltConst (Just b)) = AltConst (Just (a <> b))
instance (Semigroup a) => Semigroup (AltConst a x) where
(AltConst Nothing) <> _ = (AltConst Nothing)
_ <> (AltConst Nothing) = (AltConst Nothing)
(AltConst (Just a)) <> (AltConst (Just b)) = AltConst (Just (a <> b))
instance (Monoid a) => Monoid (AltConst a x) where
mempty = (AltConst (Just mempty))
instance Monoid m => Alternative (AltConst m) where
empty = (AltConst Nothing)
(AltConst Nothing) <|> a = a
a <|> (AltConst Nothing) = a
(AltConst (Just a)) <|> (AltConst (Just b)) = (AltConst (Just (a <> b)))
如果您对出现的某些光学器件感兴趣,我已经实现了其中的一些 here:
绝对有可能存在其他解释或更简单的表示,但目前这似乎可以解决问题。如果其他人有其他想法,我很乐意看到他们!
如果您有任何其他问题,很高兴再讨论它!
This article by Chris Penner 谈论“凋零光学”;可用于从结构中过滤项目的光学器件。
本文对这些光学器件使用以下“Van Laarhoven”表示法:
type Wither s t a b = forall f. Alternative f => (a -> f b) -> s -> f t
大多数(如果不是全部)Van Laarhoven 光学器件具有等效的profunctor 表示。例如镜头:
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
相当于:
type Lens s t a b = forall p. Strong p => p a b -> p s t
Wither
是否也有 Profuctor 代表?如果是,那是什么?
这里是克里斯;这是我对 profunctor 光学表示的看法:
import Data.Profunctor
import Data.Profunctor.Traversing
import Control.Applicative
class (Traversing p) => Withering p where
cull :: (forall f. Alternative f => (a -> f b) -> (s -> f t)) -> p a b -> p s t
instance Alternative f => Withering (Star f) where
cull f (Star amb) = Star (f amb)
instance Monoid m => Withering (Forget m) where
cull f (Forget h) = Forget (getAnnihilation . f (AltConst . Just . h))
where
getAnnihilation (AltConst Nothing) = mempty
getAnnihilation (AltConst (Just m)) = m
newtype AltConst a b = AltConst (Maybe a)
deriving stock (Eq, Ord, Show, Functor)
instance Monoid a => Applicative (AltConst a) where
pure _ = (AltConst (Just mempty))
(AltConst Nothing) <*> _ = (AltConst Nothing)
_ <*> (AltConst Nothing) = (AltConst Nothing)
(AltConst (Just a)) <*> (AltConst (Just b)) = AltConst (Just (a <> b))
instance (Semigroup a) => Semigroup (AltConst a x) where
(AltConst Nothing) <> _ = (AltConst Nothing)
_ <> (AltConst Nothing) = (AltConst Nothing)
(AltConst (Just a)) <> (AltConst (Just b)) = AltConst (Just (a <> b))
instance (Monoid a) => Monoid (AltConst a x) where
mempty = (AltConst (Just mempty))
instance Monoid m => Alternative (AltConst m) where
empty = (AltConst Nothing)
(AltConst Nothing) <|> a = a
a <|> (AltConst Nothing) = a
(AltConst (Just a)) <|> (AltConst (Just b)) = (AltConst (Just (a <> b)))
如果您对出现的某些光学器件感兴趣,我已经实现了其中的一些 here:
绝对有可能存在其他解释或更简单的表示,但目前这似乎可以解决问题。如果其他人有其他想法,我很乐意看到他们!
如果您有任何其他问题,很高兴再讨论它!