内容简介:问题当然不是重命名,而是绑定到一个新的变量.由于iterateFunc是Rank-2,它需要一个多态参数函数.当然,f是v中的多态,所以可以使用.但是当你写f’= f时,不清楚什么类型f’应该是:与f相同的多态类型或一些单态类型,可能取决于编译器尚未推出的iteratedF中的另一个类型变量的一些关系.编译器默认为单态选项;因为chi说这是单态限制在这里的错误,所以如果你把它关闭你的代码实际编译.尽管如此,即使在RankNTypes代码中没有单态限制,同样的问题也可能会发生,但是它不能完全避免.唯一可靠的
在将代码迭代到正确的版本时,我遇到了以下好奇心:
{-# LANGUAGE RankNTypes #-}
module Foo where
import Data.Vector.Generic.Mutable as M
import Control.Monad.Primitive
-- an in-place vector function with dimension
data DimFun v m r =
DimFun Int (v (PrimState m) r -> m ())
eval :: (PrimMonad m, MVector v r) => DimFun v m r -> v (PrimState m) r -> m ()
eval = error ""
iterateFunc :: (PrimMonad m, MVector v r)
=> (forall v' . (MVector v' r) => DimFun v' m r) -> DimFun v m r
iterateFunc = error ""
f :: (PrimMonad m, MVector v r)
=> DimFun v m r
f = error ""
iteratedF :: (MVector v r, PrimMonad m)
=> v (PrimState m) r -> m ()
iteratedF y =
let f' = f
in eval (iterateFunc f') y
此代码不编译:
Testing/Foo.hs:87:14:
Could not deduce (MVector v0 r) arising from a use of ‘f’
from the context (MVector v r, PrimMonad m)
bound by the type signature for
iteratedF :: (MVector v r, PrimMonad m) =>
v (PrimState m) r -> m ()
at Testing/Foo.hs:(84,14)-(85,39)
The type variable ‘v0’ is ambiguous
Relevant bindings include
f' :: DimFun v0 m r (bound at Testing/Foo.hs:87:9)
y :: v (PrimState m) r (bound at Testing/Foo.hs:86:11)
iteratedF :: v (PrimState m) r -> m ()
(bound at Testing/Foo.hs:86:1)
In the expression: f
In an equation for ‘f'’: f' = f
In the expression: let f' = f in eval (iterateFunc f') y
Testing/Foo.hs:88:26:
Couldn't match type ‘v0’ with ‘v'’
because type variable ‘v'’ would escape its scope
This (rigid, skolem) type variable is bound by
a type expected by the context: MVector v' r => DimFun v' m r
at Testing/Foo.hs:88:14-27
Expected type: DimFun v' m r
Actual type: DimFun v0 m r
Relevant bindings include
f' :: DimFun v0 m r (bound at Testing/Foo.hs:87:9)
In the first argument of ‘iterateFunc’, namely ‘f'’
In the first argument of ‘eval’, namely ‘(iterateFunc f')’
Failed, modules loaded: none.
但是,如果我将iteratedF的定义更改为
iteratedF y = eval (iterateFunc f) y
代码编译GHC 7.8.2.这个问题不是关于奇怪的签名或数据类型,只是这样:为什么重命名f到f’打破代码?这似乎是对我来说是个错误.
问题当然不是重命名,而是绑定到一个新的变量.由于iterateFunc是Rank-2,它需要一个多态参数函数.当然,f是v中的多态,所以可以使用.但是当你写f’= f时,不清楚什么类型f’应该是:与f相同的多态类型或一些单态类型,可能取决于编译器尚未推出的iteratedF中的另一个类型变量的一些关系.
编译器默认为单态选项;因为chi说这是单态限制在这里的错误,所以如果你把它关闭你的代码实际编译.
尽管如此,即使在RankNTypes代码中没有单态限制,同样的问题也可能会发生,但是它不能完全避免.唯一可靠的修复是本地签名,通常需要ScopedTypeVariables.
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/24724738/let-renaming-function-breaks-code
以上所述就是小编给大家介绍的《haskell – 让重命名功能中断代码》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Linux中断一网打尽(2) - IDT及中断处理的实现
- Kernel trace tools(一):中断和软中断关闭时间过长问题追踪
- Java中断机制
- 展开中断或忽略的序号
- Cloudflare 接口服务中断故障复盘与思考
- [译] 使用 PDB 避免 Kubernetes 集群中断
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Algorithms in C (Computer Science Series)
Robert Sedgewick / Addison-Wesley Professional / 1990-01-11 / USD 59.99
This new version of the best-selling book, Algorithms, SecondEdition, provides a comprehensive collection of algorithmsimplemented in C. A variety of algorithms are described in eachofthe following ar......一起来看看 《Algorithms in C (Computer Science Series)》 这本书的介绍吧!