haskell - 这条适用法则是什么意思?
问题描述
应用程序被声明为
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)
在法律的右手边,
- 有
pure (.)
类型f((b -> c) -> (a -> b) -> (a -> c))
吗? - 有
x
类型f(b->c)
吗? - 有
y
类型f(a->b)
吗? - 有
z
类型f(a)
吗?
在法律的左边,
- 有
x
类型f(a->b->c)
吗? - 有
y
类型f(a)
吗? - 有
z
类型f(b)
吗?
谢谢。
解决方案
在等价的幺半群函子表示中,应用定律更容易理解:
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
推荐阅读
- c++ - 你如何知道 C++ 中的堆栈空间,你能改变它吗?(在 G++ 编译器上,Windows 10)
- windows - Docker build命令在windowsfilter文件夹中创建大量文件夹:目的是什么以及如何控制
- java - Hive UDF - 解析 IP 地址时非常慢
- python - Pyspark 模型的 Python 等效项
- python - 如何根据 Django 中的另一个模型更新模型字段?
- spring-boot - 如何处理 Spring Boot 中服务的可选服务依赖关系?
- php - WysiwygPro 编辑器显示会话过期错误
- azure - 如何获取 Azure DevOps 的访问令牌并将其用于 DevOps 服务 REST API 调用?
- nuxt.js - 删除登录页面时,我仍然在auth js-nuxt版本2.15.7上得到旧的中间件配置
- python - 在 Outlook 邮件中格式化 Pandas DF 标头