haskell - 多态函数的引用透明性
问题描述
考虑这个实现on
on f g = curry $ (. bimap g g) $ uncurry f
它是什么类型的?GHC 会说(b -> b -> c) -> (a -> b) -> a -> a -> c
,这是一个很好的猜测。但它错过了实例on (+) toInteger
。让我们尝试解决这个问题(使用、RankNTypes
和KindSignatures
):AllowAmbiguousTypes
ConstraintKinds
on :: forall (co :: * -> Constraint) a1 a2 b c .
(co a1, co a2) =>
(b -> b -> c)
-> (forall a . co a => a -> b)
-> a1 -> a2 -> c
然后进行类型检查。但是当我们尝试
> :t (+) `on` toInteger
我们得到
• Couldn't match type ‘c’ with ‘Integer’
‘c’ is untouchable
inside the constraints: co a
bound by a type expected by the context:
forall a. co a => a -> c
at <interactive>:1:1-18
‘c’ is a rigid type variable bound by
the inferred type of it :: (co a1, co a2, Num c) => a1 -> a2 -> c
at <interactive>:1:1
Possible fix: add a type signature for ‘it’
Expected type: a -> c
Actual type: a -> Integer
• In the second argument of ‘on’, namely ‘toInteger’
In the expression: (+) `on` toInteger
那是什么意思?
解决方案
推荐阅读
- java - 从 JSON 5 天预报中获取有关明天天气的数据
- android - Android:将菜单显示为列表
- excel - 这个 Excel 公式是如何工作的?
- google-chrome - 如何使用 chrome 扩展进行文件 IO?
- java - 如何构造正确的 MultipartEntity 以在 java 中发送多部分/相关请求?
- datetimepicker - 基于另一个datetimepicker限制jquery datetimepicker中的日期
- php - 如何在php中将十进制实体转换为元音?
- ios - 为什么我应该将自定义 dataSource 对象声明为存储属性?
- go - 获取切片中值的指针
- ios - 如何使用 DeviceCheck.DCDevice Xamarin Forms C# 生成令牌代码 iOS DeviceId