简体   繁体   English

替代类配方的适用法律

[英]Applicative laws for alternative class formulations

A well-known alternative formulation of Applicative (see, eg, Typeclassopedia) is 一种众所周知的Applicative替代配方(参见,例如,Typeclassopedia)是

class Functor f => Monoidal f where
  unit :: f ()
  pair :: f a -> f b -> f (a, b)

This leads to laws that look more like typical identity and associativity laws than what you get from Applicative , but only when you work through pair-reassociating isomorphisms. 这导致法律看起来更像典型的身份和相关性法则,而不是您从Applicative获得的法律,但只有当您通过对重新关联同构时才能工作。 Thinking about this a few weeks ago, I came up with two other formulations that avoid this problem. 几个星期前考虑到这一点,我想出了另外两个避免这个问题的配方。

class Functor f => Fapplicative f where
  funit :: f (a -> a)
  fcomp :: f (b -> c) -> f (a -> b) -> f (a -> c)

class Functor f => Capplicative f where
  cunit :: Category (~>) => f (a ~> a)
  ccomp :: Category (~>) => f (b ~> c) -> f (a ~> b) -> f (a ~> c)

It's easy to implement Capplicative using Applicative , Fapplicative using Capplicative , and Applicative using Fapplicative , so these all have equivalent power. 这很容易实现Capplicative使用ApplicativeFapplicative使用CapplicativeApplicative使用Fapplicative ,所以这些都具有相当的权力。

The identity and associativity laws are entirely obvious. 身份和相关性法则是完全明显的。 But Monoidal needs a naturality law, and these must as well. 但是Monoidal需要一个自然规律,这些也必须如此。 How might I formulate them? 我该如何制定它们? Also: Capplicative seems to suggest an immediate generalization: 另外: Capplicative似乎表明立即概括:

class (Category (~>), Functor f) => Appish (~>) f where
  unit1 :: f (a ~> a)
  comp1 :: f (b ~> c) -> f (a ~> b) -> f (a ~> c)

I am a bit curious about whether this (or something similar) is good for something. 我有点好奇这个(或类似的东西)是否适合某些事情。

This is a really neat idea! 这是一个非常好的主意!

I think the free theorem for fcomp is 我认为fcomp的自由定理是

fcomp (fmap (post .) u) (fmap (. pre) v) = fmap (\f -> post . f . pre) (fcomp u v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM