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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:16:59+00:00 2026-05-30T12:16:59+00:00

How should one reason about function evaluation in examples like the following in Haskell:

  • 0

How should one reason about function evaluation in examples like the following in Haskell:

let f x = ...
    x = ...
in map (g (f x)) xs

In GHC, sometimes (f x) is evaluated only once, and sometimes once for each element in xs, depending on what exactly f and g are. This can be important when f x is an expensive computation. It has just tripped a Haskell beginner I was helping and I didn’t know what to tell him other than that it is up to the compiler. Is there a better story?

Update

In the following example (f x) will be evaluated 4 times:

let f x = trace "!" $ zip x x
    x = "abc"
in map (\i -> lookup i (f x)) "abcd" 
  • 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-30T12:17:00+00:00Added an answer on May 30, 2026 at 12:17 pm

    With language extensions, we can create situations where f x must be evaluated repeatedly:

    {-# LANGUAGE GADTs, Rank2Types #-}
    module MultiEvG where
    
    data BI where
        B :: (Bounded b, Integral b) => b -> BI
    
    foo :: [BI] -> [Integer]
    foo xs = let f :: (Integral c, Bounded c) => c -> c
                 f x = maxBound - x
                 g :: (forall a. (Integral a, Bounded a) => a) -> BI -> Integer
                 g m (B y) = toInteger (m + y)
                 x :: (Integral i) => i
                 x = 3
             in map (g (f x)) xs
    

    The crux is to have f x polymorphic even as the argument of g, and we must create a situation where the type(s) at which it is needed can’t be predicted (my first stab used an Either a b instead of BI, but when optimising, that of course led to only two evaluations of f x at most).

    A polymorphic expression must be evaluated at least once for each type it is used at. That’s one reason for the monomorphism restriction. However, when the range of types it can be needed at is restricted, it is possible to memoise the values at each type, and in some circumstances GHC does that (needs optimising, and I expect the number of types involved mustn’t be too large). Here we confront it with what is basically an inhomogeneous list, so in each invocation of g (f x), it can be needed at an arbitrary type satisfying the constraints, so the computation cannot be lifted outside the map (technically, the compiler could still build a cache of the values at each used type, so it would be evaluated only once per type, but GHC doesn’t, in all likelihood it wouldn’t be worth the trouble).

    • Monomorphic expressions need only be evaluated once, they can be shared. Whether they are is up to the implementation; by purity, it doesn’t change the semantics of the programme. If the expression is bound to a name, in practice you can rely on it being shared, since it’s easy and obviously what the programmer wants. If it isn’t bound to a name, it’s a question of optimisation. With the bytecode generator or without optimisations, the expression will often be evaluated repeatedly, but with optimisations repeated evaluation would indicate a compiler bug.
    • Polymorphic expressions must be evaluated at least once for every type they’re used at, but with optimisations, when GHC can see that it may be used multiple times at the same type, it will (usually) still be shared for that type during a larger computation.

    Bottom line: Always compile with optimisations, help the compiler by binding expressions you want shared to a name, and give monomorphic type signatures where possible.

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

Sidebar

Related Questions

When should one use a scripting language over a more verbose, compiled language like
I apologize if you know nothing about Python, however, the following snippet should be
Should one create unit tests involving IO? Ie, testing a class method for serializing/deserializing
Where should one go to be kept updated on all of the security flaws/problems
How should one ensure correctness when multiple processes access one single SQLite database file?
How should one split up the controllers in an ASP.NET MVC site? For example,
Why should one go for Windows Installer XML (WiX) when we have in built
How much should one DataSet represent? Using the example of an ordering system: While
In what situations should one catch java.lang.Error on an application?
What return type should one return from a method that is getting rows from

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.