首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >"Wither“的Profunctor表示是什么?

"Wither“的Profunctor表示是什么?
EN

Stack Overflow用户
提问于 2020-11-02 00:12:06
回答 1查看 155关注 0票数 5

This article by Chris Penner谈到了“无线光学”;即可以用来从结构中过滤出项目的光学。

本文使用以下"Van Laarhoven“表示法来表示这些光学元件:

代码语言:javascript
运行
复制
type Wither s t a b = forall f. Alternative f => (a -> f b) -> s -> f t

大多数(如果不是所有的) Van Laarhoven光学都有一个等效的profunctor表示。例如镜头:

代码语言:javascript
运行
复制
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t 

等同于:

代码语言:javascript
运行
复制
type Lens s t a b = forall p. Strong p => p a b -> p s t

Wither是否也有Profuctor表示?如果有,那是什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-02 07:06:39

Chris在这里;这是我在profunctor光学表示中的挥杆:

Here's the profunctor class

代码语言:javascript
运行
复制
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

当然也有可能有其他的解释,或者可能有一些更简单的表示,但目前这似乎起到了作用。如果其他人有其他想法,我很乐意看看!

如果你有任何其他的问题,很乐意多聊一聊!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64634005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档