Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Num vs Integral

Tags:

This function:

hola :: (Integral a) => a -> String hola 1 = "OK" hola _ = "asdf" 

works fine. But this one:

hola :: (Num a) => a -> String hola 1 = "OK" hola _ = "asdf" 

can't be compiled: "Could not deduce (Eq a) arising from the literal `1'"

I really don't get it. I am reading a tutorial where it is said

"Integral is also a numeric typeclass. Num includes all numbers, including real numbers and integral numbers, Integral includes only integral (whole) numbers. In this typeclass are Int and Integer." http://learnyouahaskell.com/types-and-typeclasses

Why can't I use Num?

like image 300
user1726613 Avatar asked Oct 07 '12 11:10

user1726613


People also ask

What does integral mean in Haskell?

Integral types Int is the type of limited-precision integers; this means that there is a smallest integer of type Int , namely minBound , and a greatest integer of type Int , namely maxBound .

What is integral type in Python?

int (signed integers) − They are often called just integers or ints. They are positive or negative whole numbers with no decimal point. Integers in Python 3 are of unlimited size. Python 2 has two integer types - int and long. There is no 'long integer' in Python 3 anymore.

What is NUM Haskell?

Num is a typeclass — a group of types — which includes all types which are regarded as numbers. The (Num a) => part of the signature restricts a to number types – or, in Haskell terminology, instances of Num .

What is the difference between int and Integer in Haskell?

What's the difference between Integer and Int ? Integer can represent arbitrarily large integers, up to using all of the storage on your machine. Int can only represent integers in a finite range.


1 Answers

It's a recent change proposed and accepted in September/October last year, in the latest version of the base package, Eq and Show are no longer superclasses of Num. Since that change, no new version of the language report has been published, so it's not yet in the report. And since it's recent, it has also not made it yet into many tutorials or books.

"Pattern matching" against a numeric literal is an implicit application of (==), so an Eq instance is required for it to work. That instance can now no longer be inferred from the Num constraint, so the (quite new :D) compiler rejects the code with only a Num constraint.

But Integral is a subclass of Real, which has Ord (and hence Eq) as a superclass, hence that works.

like image 50
Daniel Fischer Avatar answered Oct 14 '22 09:10

Daniel Fischer