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表示?如果有,那是什么呢?
发布于 2020-11-02 07:06:39
Chris在这里;这是我在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
当然也有可能有其他的解释,或者可能有一些更简单的表示,但目前这似乎起到了作用。如果其他人有其他想法,我很乐意看看!
如果你有任何其他的问题,很乐意多聊一聊!
https://stackoverflow.com/questions/64634005
复制相似问题