Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5975945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:09:28+00:00 2026-05-22T21:09:28+00:00

I am starting Haskell and was looking at some libraries where data types are

  • 0

I am starting Haskell and was looking at some libraries where data types are defined with “!”. Example from the bytestring library:

data ByteString = PS {-# UNPACK #-} !(ForeignPtr Word8) -- payload
                     {-# UNPACK #-} !Int                -- offset
                     {-# UNPACK #-} !Int                -- length

Now I saw this question as an explanation of what this means and I guess it is fairly easy to understand. But my question is now: what is the point of using this? Since the expression will be evaluated whenever it is need, why would you force the early evaluation?

In the second answer to this question C.V. Hansen says: “[…] sometimes the overhead of lazyness can be too much or wasteful”. Is that supposed to mean that it is used to save memory (saving the value is cheaper than saving the expression)?

An explanation and an example would be great!

Thanks!

[EDIT] I think I should have chosen an example without {-# UNPACK #-}. So let me make one myself. Would this ever make sense? Is yes, why and in what situation?

data MyType = Const1 !Int
            | Const2 !Double
            | Const3 !SomeOtherDataTypeMaybeMoreComplex
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:09:29+00:00Added an answer on May 22, 2026 at 9:09 pm

    The goal here is not strictness so much as packing these elements into the data structure. Without strictness, any of those three constructor arguments could point either to a heap-allocated value structure or a heap-allocated delayed evaluation thunk. With strictness, it could only point to a heap-allocated value structure. With strictness and packed structures, it’s possible to make those values inline.

    Since each of those three values is a pointer-sized entity and is accessed strictly anyway, forcing a strict and packed structure saves pointer indirections when using this structure.

    In the more general case, a strictness annotation can help reduce space leaks. Consider a case like this:

    data Foo = Foo Int
    
    makeFoo :: ReallyBigDataStructure -> Foo
    makeFoo x = Foo (computeSomething x)
    

    Without the strictness annotation, if you just call makeFoo, it will build a Foo pointing to a thunk pointing to the ReallyBigDataStructure, keeping it around in memory until something forces the thunk to evaluate. If we instead have

    data Foo = Foo !Int
    

    This forces the computeSomething evaluation to proceed immediately (well, as soon as something forces makeFoo itself), which avoids leaving a reference to the ReallyBigDataStructure.

    Note that this is a different use case than the bytestring code; the bytestring code forces its parameters quite frequently so it’s unlikely to lead to a space leak. It’s probably best to interpret the bytestring code as a pure optimization to avoid pointer dereferences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an example Haskell FRP program using the reactive-banana library. I'm only just starting
Starting from this example: http://support.microsoft.com/kb/828736 I have tried to add a test function in
I am starting to test Haskell for linear algebra. Does anyone have any recommendations
Starting from ASP.NET MVC Preview 3, HTML.Button ( and other related HTML controls) are
Starting with the following LINQ query: from a in things where a.Id == b.Id
Starting recently, some of my new web pages (XHTML 1.1) are setup to do
Starting from scratch with very little knowledge of .NET, how much ASP.NET should I
Starting from an Html input like this: <p> <a href=http://www.foo.com>this if foo</a> <a href=http://www.bar.com>this
Starting from an Html input like this: <p> <a href=http://www.foo.com>this if foo</a> <a href=http://www.bar.com>this
Starting from Windows Server 2003, Windows included a new tool which calculates the effective

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.