[Haskell-cafe] data Color3 a = Color3 !a !a !a

Ryan Ingram ryani.spam at gmail.com
Tue Jul 8 13:20:31 EDT 2008


A simple example to help understand the difference

> data NoStrict a = NoStrict a deriving Show
> data Strict a = Strict !a deriving Show

> ns1 = NoStrict ()
> ns2 = NoStrict undefined
> ns3 = undefined

> nf1 (NoStrict ()) = "ok"
> nf2 (NoStrict _) = "ok"

> s1 = Strict ()
> s2 = Strict undefined
> s3 = undefined

> f1 (Strict ()) = "ok"
> f2 (Strict _) = "ok"

The difference between these:

*Strict> nf2 ns2
"ok"
*Strict> f2 s2
"*** Exception: Prelude.undefined

s2 and s3 are both the same (undefined), while ns2 and ns3 are different.

As to why you'd want that behavior?  Here are a couple of reasons:
1) You can avoid hiding exceptions in strict data structures; if the
top level evaluates, you know the entire structure is valid.
2) With -funbox-strict-fields, the compiler can remove a level of
indirection; the performance difference between a structure that
contains three machine words and a structure that contains three
pointers to boxed integers is pretty significant.

On the other hand, you lose the benefits of laziness; I find most
"structure-like" data structures work better strict, but that
"list-like" data structures definitely gain an advantage from
laziness.

  -- ryan


2008/7/8 Daryoush Mehrtash <dmehrtash at gmail.com>:
> Can some one explain what the !a does in this:
>
> data Color3 a = Color3 !a !a !a
>
> http://cvs.haskell.org/Hugs/pages/libraries/OpenGL/Graphics-Rendering-OpenGL-GL-VertexSpec.html#t%3AColor3
>
> Thanks,
>
> Daryoush
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list