开发者

Couldn't Match Expected Type Against Inferred Type, Rigid Type Variable Error

开发者 https://www.devze.com 2023-02-03 07:12 出处:网络
What is wrong with this function ? test :: Show s => s test = \"asdasd\" String is an instance of the Show class, so it seems correct.

What is wrong with this function ?

test :: Show s => s
test = "asdasd"

String is an instance of the Show class, so it seems correct.

The error is

src\Main.hs:224:7:
    Couldn't match expected type `s' against inferred type `[Char]'
      `s' is a rigid type variable bound by
          开发者_StackOverflowthe 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 means "for any type which is an instance of Foo, test is a value of that type". So in any place where you can use a value of type X where X is an instance Foo, you can use a value of type Foo a => a.

Something like test :: Num a => a; test = 42 works because 42 can be a value of type Int or Integer or Float or anything else that is an instance of Num.

However "asdasd" can't be an Int or anything else that is an instance of Show - it can only ever be a String. As a consequence it does not match the type Show s => s.


Yes, String is an instance of Show. But that doesn't allow using a string as an abritary Show value. 1 can be Num a => a because there's an 1 :: Integer, an 1 :: Double, an 1 :: Word16, etc. If "asdasd" could be of type Show a => a, there would be "asdasd" :: Bool, "asdasd" :: String, "asdasd" :: Int, etc. There isn't. Therfore, "asdasd" can't be of type Show a => a. The type of a string constant doesn't get much more general than String.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号