Haskell Error: Couldn't match expected type `Integer' against inferred type `Int'

a 夏天 提交于 2019-12-18 08:29:27

问题


I have a haskell function that that calculates the size of the list of finite Ints. I need the output type to be an Integer because the value will actually be larger than the maximum bound of Int (the result will be -1 to be exact if the output type is an Int)

size :: a -> Integer
size a =  (maxBound::Int) - (minBound::Int)

I understand the difference between Ints (bounded) and Integers (unbounded) but I'd like to make an Integer from an Int. I was wondering if there was a function like fromInteger, that will allow me to convert an Int to an Integer type.


回答1:


You'll need to convert the values to Integers, which can be done by the fromIntegral function (numeric casting for Haskell):

fromIntegral :: (Integral a, Num b) => a -> b

It converts any type in the Integral class to any type in the (larger) Num class. E.g.

fromIntegral (maxBound::Int) - fromIntegral (minBound::Int)

However, I would not really trust the approach you're taking -- it seems very fragile. The behaviour in the presence of types that admit wraparound is pretty suspect.

What do you really mean by: "the size of the list of finite Ints". What is the size in this sense, if it isn't the length of the list?




回答2:


I believe you are looking for:

fromIntegral :: (Integral a, Num b) => a -> b

which will convert an Integer to an Int




回答3:


Perhaps you were assuming that Haskell, like many main-stream languages like C and (to a certain extent) Java, has implicit numeric coercions. It doesn't: Int and Integer are totally unrelated types and there is a special function for converting between them: fromIntegral. It belongs to the Num typeclass. Look at the documentation: essentially, fromIntegral does more than that: it is a generic "construct the representation of an arbitrary integral number", t.i. if you're implementing some kind of numbers and instantiating Num, you must provide a way to construct integral numbers of your type. For example, in the Num instance for complex numbers, fromIntegral creates a complex number with a zero imaginary part and an integral real part.

The only sense in which Haskell has implicit numeric coercions is that integer literals are overloaded, and when you write 42, the compiler implicitly interprets it as "fromIntegral (42::Integer)", so you can use integers in whatever contexts where a Num type is required.



来源:https://stackoverflow.com/questions/2301523/haskell-error-couldnt-match-expected-type-integer-against-inferred-type-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!