首页 > 解决方案 > 这条适用法则是什么意思?

问题描述

应用程序被声明为

class   Functor f   =>  Applicative f   where
pure    ::  a   ->  f   a
(<*>)   ::  f   (a  ->  b)  ->  f   a   ->  f   b

应用定律之一是:

x <*> y <*> z = ( pure (.) <*> x <*> y) <*> z

(.)函数之间的组合在哪里 :

(.) ::  (b  ->  c)  ->  (a  ->  b)  ->  (a  ->  c)
f   .   g   =   \x  ->  f   (g  x)

在法律的右手边,

在法律的左边,

谢谢。

标签: haskellapplicative

解决方案


在等价的幺半群函子表示中,应用定律更容易理解:

class Functor f => Monoidal f where
  pureUnit :: f ()
  fzip :: f a -> f b -> f (a,b)
--pure x = fmap (const x) pureUnit
--fs<*>xs = fmap (\(f,x)->f x) $ fzip fs xs
--pureUnit = pure ()
--fzip l r = (,) <$> l <*> r

那么,你要问的法律是这样的:

fzip x (fzip y z) ≅ fzip (fzip x y) z

我的p ≅ q意思是,相当于重新关联元组类型,即显式

fzip x (fzip y z) ≡ fmap (\((a,b),c)->(a,(b,c))) $ fzip (fzip x y) z

所以这实际上只是一个结合律。使用中缀编写时更加明显(⋎) = fzip

x ⋎ (y ⋎ z) ≅ (x ⋎ y) ⋎ z

推荐阅读