Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does !( ) means in data constructor?

Tags:

haskell

map

I am reading the source code of Data.Map, and I find that !() is used in the data constructor of data Map k a.

data Map k a  = Tip 
              | Bin {-# UNPACK #-} !Size !k a !(Map k a) !(Map k a) 

I find that the !( ) does not affect how the patten matching against the data. In the function of mapWithKey, the patten matching is still for 5 things. So I do not consider it as a operator.

mapWithKey f (Bin sx kx x l r) 

After googleing, I found that the !( ) may be related to -XBangPatterns which is used for lazy evalution. Am I right ? Or is it for other purpose?

like image 615
code4j Avatar asked Jun 27 '13 07:06

code4j


2 Answers

! in data type declarations is used to indicate strictness. If we search for it on Hoogle, we see a link to keyword !, which explains the behavior. The Report defines the exact behavior.

In data Foo = Foo ... !T ..., the constructor Foo forces its argument, i.e. Foo ... x ... = x `seq` RealFoo ... x .... where RealFoo is the constructor you would get without the !.

{-# UNPACK #-} is a GHC extension that means the Size (i.e. Int) is stored unboxed, directly as part of the data type.

like image 127
shachaf Avatar answered Sep 18 '22 13:09

shachaf


In a data constructor ! indicates the arguments are evaluated as the type is constructed. This forces functions to be evaluated and helps you control the space requirements of your application.

The high-performance Haskell slides cover this in more detail.

like image 21
Jeff Foster Avatar answered Sep 20 '22 13:09

Jeff Foster