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 6637691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:20:41+00:00 2026-05-25T23:20:41+00:00

I’ve got the feeling that the answer is yes, and that’s not restricted to

  • 0

I’ve got the feeling that the answer is yes, and that’s not restricted to Haskell. For example, tail-call optimization changes memory requirements from O(n) to O(l), right?

My precise concern is: in the Haskell context, what is expected to understand about compiler optimizations when reasoning about performance and size of a program?

In Scheme, you can take some optimizations for granted, like TCO, given that you are using an interpreter/compiler that conforms to the specification.

  • 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-25T23:20:41+00:00Added an answer on May 25, 2026 at 11:20 pm

    Yes, in particular GHC performs strictness analysis, which can drastically reduce space usage of a program with unintended laziness from O(n) to O(1).

    For example, consider this trivial program:

    $ cat LazySum.hs
    main = print $ sum [1..100000]
    

    Since sum does not assume that the addition operator is strict, (it might be used with a Num instance for which (+) is lazy), it will cause a large number of thunks to be allocated. Without optimizations enabled, strictness analysis is not performed.

    $ ghc --make LazySum.hs -rtsopts -fforce-recomp
    [1 of 1] Compiling Main             ( LazySum.hs, LazySum.o )
    Linking LazySum ...
    $ ./LazySum +RTS -s
    ./LazySum +RTS -s 
    5000050000
          22,047,576 bytes allocated in the heap
          18,365,440 bytes copied during GC
           6,348,584 bytes maximum residency (4 sample(s))
           3,133,528 bytes maximum slop
                  15 MB total memory in use (0 MB lost due to fragmentation)
    
      Generation 0:    23 collections,     0 parallel,  0.04s,  0.03s elapsed
      Generation 1:     4 collections,     0 parallel,  0.01s,  0.02s elapsed
    
      INIT  time    0.00s  (  0.00s elapsed)
      MUT   time    0.01s  (  0.03s elapsed)
      GC    time    0.05s  (  0.04s elapsed)
      EXIT  time    0.00s  (  0.00s elapsed)
      Total time    0.06s  (  0.07s elapsed)
    
      %GC time      83.3%  (58.0% elapsed)
    
      Alloc rate    2,204,757,600 bytes per MUT second
    
      Productivity  16.7% of total user, 13.7% of total elapsed
    

    However, if we compile with optimizations enabled, the strictness analyzer will determine that since we’re using the addition operator for Integer, which is known to be strict, the compiler knows that it is safe to evaluate the thunks ahead of time, and so the program runs in constant space.

    $ ghc --make -O2 LazySum.hs -rtsopts -fforce-recomp
    [1 of 1] Compiling Main             ( LazySum.hs, LazySum.o )
    Linking LazySum ...
    $ ./LazySum +RTS -s
    ./LazySum +RTS -s 
    5000050000
           9,702,512 bytes allocated in the heap
               8,112 bytes copied during GC
              27,792 bytes maximum residency (1 sample(s))
              20,320 bytes maximum slop
                   1 MB total memory in use (0 MB lost due to fragmentation)
    
      Generation 0:    18 collections,     0 parallel,  0.00s,  0.00s elapsed
      Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed
    
      INIT  time    0.00s  (  0.00s elapsed)
      MUT   time    0.01s  (  0.02s elapsed)
      GC    time    0.00s  (  0.00s elapsed)
      EXIT  time    0.00s  (  0.00s elapsed)
      Total time    0.01s  (  0.02s elapsed)
    
      %GC time       0.0%  (2.9% elapsed)
    
      Alloc rate    970,251,200 bytes per MUT second
    
      Productivity 100.0% of total user, 55.0% of total elapsed
    

    Note that we can get constant space even without optimizations, if we add the strictness ourselves:

    $ cat StrictSum.hs 
    import Data.List (foldl')
    main = print $ foldl' (+) 0 [1..100000]
    $ ghc --make StrictSum.hs -rtsopts -fforce-recomp
    [1 of 1] Compiling Main             ( StrictSum.hs, StrictSum.o )
    Linking StrictSum ...
    $ ./StrictSum +RTS -s
    ./StrictSum +RTS -s 
    5000050000
           9,702,664 bytes allocated in the heap
               8,144 bytes copied during GC
              27,808 bytes maximum residency (1 sample(s))
              20,304 bytes maximum slop
                   1 MB total memory in use (0 MB lost due to fragmentation)
    
      Generation 0:    18 collections,     0 parallel,  0.00s,  0.00s elapsed
      Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed
    
      INIT  time    0.00s  (  0.00s elapsed)
      MUT   time    0.00s  (  0.01s elapsed)
      GC    time    0.00s  (  0.00s elapsed)
      EXIT  time    0.00s  (  0.00s elapsed)
      Total time    0.00s  (  0.01s elapsed)
    
      %GC time       0.0%  (2.1% elapsed)
    
      Alloc rate    9,702,664,000,000 bytes per MUT second
    
      Productivity 100.0% of total user, 0.0% of total elapsed
    

    Strictness tends to be a bigger issue than tail calls, which aren’t really a useful concept in Haskell, because of Haskell’s evaluation model.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.