What does :: (double colon) mean in Haskell?

I see and use :: symbols everywhere, but still don’t know what the :: symbol means when programming in Haskell, for example

 run :: Int -> Int -> Int -- ?? 

What does :: (double colon) mean in Haskell?

+34
function syntax types haskell
May 08 '11 at 10:14
source share
3 answers

You can google for haskell "double colon" or similar things; this, unfortunately, is a bit difficult for Google to syntax, but in this case you can name it.

In Haskell, your programs will work fine without it (although you will want to use it to hone the specification of any functions that you define, which is good practice).

The idea is that you can insert :: ... anywhere (even in the middle of an expression) to say "by the way, Mr. Compiler, this expression must be of type ... ". Then the compiler will throw an error if it can be proven that it may not be so.

I think you can also use it to "throw" functions into the versions you need; for example, if a function is "polymorphic" (has a common type signature), and you really want, say, Integer , then you could do :: Integer by the resulting value; I'm a little rusty though.

+30
May 08 '11 at 10:25
source share

You should read:

 foo :: a 

as "the name foo is a value of type a ". When you write:

 run :: a -> b 

it means:

  • You declare the name run .

  • This name will refer to a value of type a -> b ,

Type a -> b is a type of function that takes a value of type a and returns another value of type b .

You must really learn about types in order to understand Haskell. The type system is one of the most important features of Haskell, and this makes the language so expressive.

+11
May 8 '11 at
source share

If you have a big scary type checking error, you can (temporarily) wrap parts of your code in (myexpression :: MyType) to explicitly tell the compiler whose type you expect myexpression . This often helps the compiler give you better error messages.

+6
May 08 '11 at 2:00
source share



All Articles