Haskell 不能';t将预期类型与推断类型匹配,刚性类型变量错误

Haskell 不能';t将预期类型与推断类型匹配,刚性类型变量错误,haskell,Haskell,这个函数有什么问题 test :: Show s => s test = "asdasd" 字符串是Show类的一个实例,因此它看起来是正确的 错误是 src\Main.hs:224:7: Couldn't match expected type `s' against inferred type `[Char]' `s' is a rigid type variable bound by the type signature for `test'

这个函数有什么问题

test :: Show s => s
test = "asdasd"
字符串是
Show
类的一个实例,因此它看起来是正确的

错误是

src\Main.hs:224:7:
    Couldn't match expected type `s' against inferred type `[Char]'
      `s' is a rigid type variable bound by
          the type signature for `test' at src\Main.hs:223:13
    In the expression: "asdasd"
    In the definition of `test': test = "asdasd"

test::Foo a=>a
表示“对于作为
Foo
实例的任何类型,
test
是该类型的值”。因此,在任何可以使用
X
类型的值的地方,
X
是实例
Foo
,都可以使用
fooa=>a
类型的值

类似于
test::Num a=>a;test=42之所以有效,是因为42可以是
Int
Integer
Float
类型的值,或者是
Num
的任何其他实例


但是
“asdasd”
不能是
Int
或任何其他
Show
的实例-它只能是
字符串。因此,它与类型
Show s=>s
Yes不匹配,
String
Show
的一个实例。但这不允许使用字符串作为基本
Show
1
可以是
Num a=>a
,因为有
1::Integer
1::Double
1::Word16
等。如果
“asdasd”
可以是
Show a=>a
类型,则会有
“asdasd”::Bool
“asdasd”::String
“asdasd::Int
等。没有。因此,
“asdasd”
不能是
Show a=>a
类型。字符串常量的类型没有比GHC中的
string

更一般,GHC使用
重载字符串
语言扩展名并导入
数据。字符串
,您还可以使用签名
测试::IsString s⇒ s
test::Num=>a
是否与
test::Num
?@mcb有任何不同,这取决于您如何定义“不同”。它们都是错误,所以在这个意义上它们是一样的。不管你怎么说,它们是不同的,因为它们可能是不同错误的结果。也就是说,
test::Num=>a
可能是简单的打字错误造成的(忘记了
=>
之前的
a
),而
test::Num
大概是代码作者认为
Num
是一个类型而不是一个类型类的结果。有没有其他方法可以解决这个问题,或者用这种方法解决这个问题根本是错误的?这里是我的例子,我有类型class
IDP
和函数
fromString::idpa=>String->maybea
意思是将字符串转换为已知的
IDP
,然后我可以像
fetchValue::idpa=>maybea->IO()