让我们考虑以下代码段:
blah :: a -> b -> a
blah x y = ble x where
ble :: b -> b
ble x = x
Run Code Online (Sandbox Code Playgroud)
这在GHC下编译得很好,这实际上意味着b第3行b与第一行不同.
我的问题很简单:有没有办法以某种方式将类型声明与ble外部上下文中使用的类型相关联,即类型声明blah?
显然,这只是一个示例,而不是类型声明的真实用例.
Haskell似乎没有识别我的类型注释.为什么是这样?
这里Runner是一个函数的包装器,具有c的默认起始值("连续").在rmap中,我希望c有一个默认的"起始"值(例如,如果c是[a],我会让该值为[]).这里当然不方便(也许这是不好的做法,随意提出更好的方法)是需要类型注释,因为rmap的域不涉及类型c.但是,为什么我不能通过类型注释来解决这个问题呢?
data ExitCode = Fail | OK | Success deriving (Eq, Show)
data Runner a b c = Runner {cont ::c
, fun :: (a, c , ExitCode) ->(b, c, ExitCode)}
class Pointed a where
point :: a
rmap:: (Pointed c) => (a->b) -> Runner a b c
rmap f = Runner (point::c) (\(x,y,z) -> (f x,y,z))
Run Code Online (Sandbox Code Playgroud)
错误如下.(似乎将c解释为c1.)
Could not deduce (Pointed c1) arising from a use of `point'
from the context (Pointed c)
bound by the type signature …Run Code Online (Sandbox Code Playgroud)