Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `Num a => a` mean in Haskell type system?

Tags:

haskell

If I force Haskell to infer the type of a number I'll get Num a => a. For example:

Prelude> :t 1
1 :: Num a => a

But what does a => a mean?

like image 651
Finkelson Avatar asked Jan 24 '16 10:01

Finkelson


People also ask

What is type NUM A in 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 .

How do you read type in Haskell?

In Haskell read function is used to deal with strings, if you want to parse any string to any particular type than we can use read function from the Haskell programming. In Haskell read function is the in built function, that means we do not require to include or install any dependency for it, we can use it directly.

What is Haskell function type?

Haskell is a functional language and it is strictly typed, which means the data type used in the entire application will be known to the compiler at compile time.

How do you define a data type in Haskell?

The Data Keyword and Constructors In general, we define a new data type by using the data keyword, followed by the name of the type we're defining. The type has to begin with a capital letter to distinguish it from normal expression names. To start defining our type, we must provide a constructor.


1 Answers

1 :: Num a => a means that 1 has some type a, where a is an instance of the Num typeclass. Note that Num is not a type, but a typeclass, which describes common properties of various types. The Num typeclass, for example, describes types that are numeric, and so support basic arithmetic. The native machine integer type Int is an instance of Num, as is the arbitrary-sized Integer, the floating point type Double, and even the rational number type Rational.

like image 153
Sebastian Redl Avatar answered Sep 20 '22 17:09

Sebastian Redl