gpt4 book ai didi

haskell - "Wither"的 Profunctor 表示是什么?

转载 作者:行者123 更新时间:2023-12-03 16:58:38 24 4
gpt4 key购买 nike

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 光学表示中的摇摆:
Here's the profunctor class :

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 :
绝对有可能有其他解释或一些更简单的表示,但目前这似乎可以解决问题。如果其他人有其他想法,我很乐意看到他们!
如果您有任何其他问题,很高兴更多地讨论它!

关于haskell - "Wither"的 Profunctor 表示是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64634005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com