【问题标题】:What's distributing a functor over a tuple called?什么是在一个被调用的元组上分配一个仿函数?
【发布时间】:2017-09-11 15:44:40
【问题描述】:

这一系列操作有名称吗?

Functor f => f (a, b) -> (f a, f b)
Functor f => f (a, b, c) -> (f a, f b, f c)
...
Functor f => f (a, b, ..., z)  -> (f a, f b, ..., f z)

它们很容易实现,只是想弄清楚该怎么称呼它。

\fab -> (fst <$> fab, snd <$> fab)

对我来说,它是在 f ~ (x -&gt;) 的上下文中出现的。

【问题讨论】:

标签: haskell functor


【解决方案1】:

在您的特定上下文f ~ (x -&gt;) 中,我认为它们可以称为“幂律”。

确实,理论上,将A -&gt; B 写成幂B^A 是很常见的。对类型(A,B)也常写成产品(A*B)

然后你的第一条定律写成

(A*B)^C = A^C * B^C

and 是一个经典的类型同构。这可以很容易地以明显的方式推广到元组。

在一般情况下,f 是一个任意函子,我现在除了“分布”之外别无他法。

【讨论】:

【解决方案2】:

Data.DistributiveData.Traversable 的对偶。它提供了 distribute 函数,可以专门化,例如如f (Stream a) -&gt; Stream (f a)distribute :: f (Vec n a) -&gt; Vec n (f a)。后一个示例是您的函数族的同质变体。

但是我们可以概括Data.Distributive 有点像镜头概括函子一样。输入Colens

type Colens s t a b = forall f. Functor f => (f a -> b) -> f s -> t

这是Control.Lens.Each的镜子:

class Coeach s t a b | s -> a, t -> b, s b -> t, t a -> s where
  coeach :: Colens s t a b

instance (a~a', b~b') => Coeach (a,a') (b,b') a b where
  coeach f p = (f $ fst <$> p, f $ snd <$> p)

instance (a~a2, a~a3, b~b2, b~b3) => Coeach (a,a2,a3) (b,b2,b3) a b where
  coeach f p = ...

...

就像each 一样,我们可以遍历元组

each_id1 :: Applicative f => (f a, f a) -> f (a, a)
each_id1 = each id

each_id2 :: Applicative f => (f a, f a, f a) -> f (a, a, a)
each_id2 = each id

使用coeach,我们可以遍历元组:

coeach_id1 :: Functor f => f (a, a) -> (f a, f a)
coeach_id1 = coeach id

coeach_id2 :: Functor f => f (a, a, a) -> (f a, f a, f a)
coeach_id2 = coeach id

不过,这仍然是同质的。我对lens了解不多,所以不能说是否存在异类each和对应的coeach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多