您好我有以下问题:我正在newtype
通过方法构造参数,并且我不知道如何明确地告诉GHCI
:I want you to instiantiate this newtype using this type parameter
newtype M a = M {fu::a->Int}
var = M (\s-> length (s:"asa")) #tell him i want the type parameter to be Char
b = (fu var) 'c'
我期望得到的是:4
因为length 'c':"aaa"==4
我得到的是:
interactive>:118:5: error:
* Couldn't match expected type `A [Char]'
with actual type `Ghci30.A [Char]'
NB: `Ghci30.A' is defined at <interactive>:100:1-25
`A' is defined at <interactive>:109:1-25
* In the first argument of `fu', namely `b'
In the expression: (fu b) "asa"
In an equation for `it': it = (fu b) "asa"
当您看到类似的名称时Ghci30.A [Char]
,这意味着您已经A
在GHCi中重新定义了类型。如果您使用了正确的.hs
文件并重新加载它,则不会有问题。
考虑以下GHCi会议:
> data A = A Int
> x = A 2
> data A = A Char -- redefinition
> :t x
输出应该是什么?该类型的x
是A
,但它不是同一类型的A
具有Char
内。GHCi会将类型打印为
x :: Ghci0.A
如果您x
在重新定义类型后重新定义,则不会再次收到该错误A
。
如果是您的情况,x
则可能需要重新定义fu
,但仍指的是old A
。进行检查:t fu
:如果提到Ghci30.A
,就是这样。
对于非平凡的定义,我建议使用.hs
文件并重新加载它,以免造成任何麻烦。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句