什么在一个名为?的元组上分配一个仿函数?

时间:2017-09-11 15:44:40

标签: haskell functor

这个操作系列有名称吗?

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 ->)

的背景下

2 个答案:

答案 0 :(得分:8)

在您的具体情况f ~ (x ->)中,我认为可以称之为&#34;权力法律&#34;。

事实上,从理论上讲,将A -> B作为权力B^A是很常见的。对类型(A,B)通常也会写为产品(A*B)

然后你的第一部法律写成

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

并且是经典类型同构。这很容易以明显的方式推广到元组。

在一般情况下,f是一个任意的仿函数,我现在除了&#34;发布&#34;之外别无其他。

答案 1 :(得分:2)

There is Data.Distributive which is the dual of Data.Traversable. It provides the distribute function which can be specialized e.g. as f (Stream a) -> Stream (f a) or distribute :: f (Vec n a) -> Vec n (f a). The latter example is a homogeneous variant of your family of functions.

But we can generalize Data.Distributive a bit just like lenses generalize functors. Enter Colens:

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

Here is the mirror of 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