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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:18:43+00:00 2026-05-22T12:18:43+00:00

When programming in Haskell (and especially when solving Project Euler problems, where suboptimal solutions

  • 0

When programming in Haskell (and especially when solving Project Euler problems, where suboptimal solutions tend to stress the CPU or memory needs) I’m often puzzled why the program behaves the way it is. I look at profiles, try to introduce some strictness, chose another data structure, … but mostly it’s groping in the dark, because I lack a good intuition.

Also, while I know how Lisp, Prolog and imperative languages are typically implemented, I have no idea about implementing a lazy language. I’m a bit curious too.

Hence I would like to know more about the whole chain from program source to execution model.

Things I wonder about:

  • what typical optimizations are applied?

  • what is the execution order when there are multiple candidates for evaluation (while I know it’s driven from the needed outputs, there may still be big performance differences between first evaluating A and then B, or evaluating B first to detect that you don’t need A at all)

  • how are thunks represented?

  • how are the stack and the heap used?

  • what is a CAF? (profiling indicates sometimes that the hotspot is there, but I have no clue)

  • 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-22T12:18:44+00:00Added an answer on May 22, 2026 at 12:18 pm

    The majority of the technical information about the architecture and approach of the GHC system is in their wiki. I’ll link to the key pieces, and some related papers that people may not know about.

    What typical optimizations are applied?

    The key paper on this is: A transformation-based optimiser for Haskell,
    SL Peyton Jones and A Santos, 1998, which describes the model GHC uses of applying type-preserving transformations (refactorings) of a core Haskell-like language to improve time and memory use. This process is called “simplification”.

    Typical things that are done in a Haskell compiler include:

    • Inlining;
    • Beta reduction;
    • Dead code elimination;
    • Transformation of conditions: case-of-case, case elimiation.
    • Unboxing;
    • Constructed product return;
    • Full laziness transformation;
    • Specialization;
    • Eta expansion;
    • Lambda lifting;
    • Strictness analysis.

    And sometimes:

    • The static argument transformation;
    • Build/foldr or stream fusion;
    • Common sub-expression elimination;
    • Constructor specialization.

    The above-mentioned paper is the key place to start to understand most of these optimizations. Some of the simpler ones are given in the earlier book, Implementing Functional Languages, Simon Peyton Jones and David Lester.

    What is the execution order when there are multiple candidates for evaluation

    Assuming you’re on a uni-processor, then the answer is “some order that the compiler picks statically based on heuristics, and the demand pattern of the program”. If you’re using speculative evaluation via sparks, then “some non-deterministic, out-of-order execution pattern”.

    In general, to see what the execution order is, look at the core, with, e.g. the ghc-core tool. An introduction to Core is in the RWH chapter on optimizations.

    How are thunks represented?

    Thunks are represented as heap-allocated data with a code pointer.

    Heap object

    See the layout of heap objects.
    Specifically, see how thunks are represented.

    How are the stack and the heap used?

    As determined by the design of the Spineless Tagless G-machine, specifically, with many modifications since that paper was released. Broadly, the execution model:

    • (boxed) objects are allocated on the global heap;
    • every thread object has a stack, consisting of frames with the same layout as heap objects;
    • when you make a function call, you push values onto the stack and jump to the function;
    • if the code needs to allocate e.g. a constructor, that data is placed on the heap.

    To deeply understand the stack use model, see “Push/Enter versus Eval/Apply”.

    What is a CAF?

    A “Constant Applicative Form”. E.g. a top level constant in your program allocated for the lifetime of your program’s execution. Since they’re allocated statically, they have to be treated specially by the garbage collector.


    References and further reading:

    • The GHC Commentary
    • The Spinless Tagless G-machine
    • Compilation via Transformation
    • Push/Enter vs Eval/Apply
    • Unboxed Values as First-Class Citizens
    • Secrets of the Inliner
    • Runtime Support for Multicore Haskell
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are some good links for diving into Haskell and functional programming in general?
I'm currently learning Haskell, Which language (F# or Haskell) do you prefer for programming
I am new to functional programming, and now learn Haskell. As an exercise I
Programming Student here...trying to work on a project but I'm stuck. The project is
Programming in vim I often go search for something, yank it, then go back
I'm new in Haskell programming. Now i'm learn lambda function and lambda abstractions. And
Programming languages had several (r)evolutionary steps in their history. Some people argue that model-driven
Programming PHP in Eclipse PDT is predominately a joy: code completion, templates, method jumping,
Programming is learned by writing programs. But code reading is said to be another
Some programming languages such as Java and C# include encryption packages in their standard

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.