What does :: (double colon) stand for?

FunctionHaskellSyntaxTypes

Function Problem Overview


I see and use the :: symbols everywhere but still don't know what the :: symbol means when programming in Haskell, e.g.

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

What does :: (double colon) stand for in Haskell?

Function Solutions


Solution 1 - Function

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

In Haskell, your programs will often run fine without it (though you will want to use it to hone the specification of any functions you define, and it is good practice).

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

I think you can also use it to "cast" functions to the versions you want; e.g. if a function is "polymorphic" (has a general type signature) and you actually want, say an Integer, then you could do :: Integer on the resulting value perhaps; I'm a bit rusty though.

Solution 2 - Function

You should read:

foo :: a 

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

run :: a -> b 

this means:

  1. You are declaring the name run.

  2. This name will refer to a value that have type a -> b,

The type a -> b is the type of a function which takes a value of type a and returns another value of type b.

You must really learn about types to understand Haskell. The type system is one of the most crucial feature of Haskell, and it's what makes the language so expressive.

Solution 3 - Function

When you have a big scary-looking typechecking error, you can (temporarily) wrap parts of your code in (myexpression :: MyType) to explicitly state to the compiler which type you're expecting myexpression to have. This will often help the compiler give you better error messages.

Solution 4 - Function

Many years later, I ran across this and thought I would point out that there is a subtlety here. :: does indicate a type of something. But it is also used in the description of the kind of something:

ghci> :type []
[] :: [a]

ghci> :kind []
[] :: * -> *

Type and kind are related but different things.

I noticed this from the following in Hackage:

class Applicative m => Monad (m :: Type -> Type) where
   ...

and the Type -> Type is a long form of * -> *.

I think the way to read the above is "For some type 'm', where m has kind * -> *, Monad m extends Applicative m and requires the implementation of ..."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaclunianView Question on Stackoverflow
Solution 1 - FunctionninjageckoView Answer on Stackoverflow
Solution 2 - FunctionRafael S. CalsaveriniView Answer on Stackoverflow
Solution 3 - FunctionhammarView Answer on Stackoverflow
Solution 4 - FunctionmelstonView Answer on Stackoverflow