Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the :: operator syntax work in the context of bounded typeclass?

Tags:

haskell

I'm learning Haskell and trying to understand the reasoning behind it's syntax design at the same time. Most of the syntax is beautiful.

But since :: normally is like a type annotation, How is it that this works:

Input: minBound::Int

Output: -2147483648
like image 648
Thomas Avatar asked Dec 06 '25 08:12

Thomas


1 Answers

There is no separate operator: :: is a type annotation in that example. Perhaps the best way to understand this is to consider this code:

main = print (f minBound)

f :: Int -> Int
f = id

This also prints -2147483648. The use of minBound is inferred to be an Int because it is the parameter to f. Once the type has been inferred, the value for that type is known.

Now, back to:

main = print (minBound :: Int)

This works in the same way, except that minBound is known to be an Int because of the type annotation, rather than for some more complex reason. The :: isn't some binary operation; it just directs the compiler that the expression minBound has the type Int. Once again, since the type is known, the value can be determined from the type class.

like image 153
Chris Smith Avatar answered Dec 08 '25 21:12

Chris Smith