【发布时间】:2015-01-08 11:26:07
【问题描述】:
作为我学习 Haskell 过程的一部分,我喜欢明确键入函数的类型声明。我希望能够对 where 子句中定义的函数执行此操作,但我不知道如何指定 where 子句中的类型变量应该与外部类型声明中的某个类型变量表示相同的类型。比如下面的代码:
foo :: (a -> a) -> a -> a
foo f arg = bar arg
where
bar :: a -> a
bar a = f a
产生此错误:
src\Test.hs:7:14:
Couldn't match expected type `a' against inferred type `a1'
`a' is a rigid type variable bound by
the type signature for `foo' at src\Test.hs:3:8
`a1' is a rigid type variable bound by
the type signature for `bar' at src\Test.hs:6:11
In the first argument of `f', namely `a'
In the expression: f a
In the definition of `bar': bar a = f a
如何表示 bar 的第一个参数应该与 foo 的第二个参数具有相同的类型,以便我可以将 f 应用于它?
谢谢。
【问题讨论】:
标签: haskell