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

  • Home
  • SEARCH
  • 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 8152835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:49:46+00:00 2026-06-06T15:49:46+00:00

I was trying to answer another question about polymorphism vs sharing when I stumbled

  • 0

I was trying to answer another question about polymorphism vs sharing when I stumbled upon this strange behaviour.

In GHCi, when I explicitly define a polymorphic constant, it does not get any sharing, which is understandable:

> let fib :: Num a => [a]; fib = 1 : 1 : zipWith (+) fib (tail fib)
> fib !! 30
1346269
(5.63 secs, 604992600 bytes)

On the other hand, if I try to achieve the same by omitting the type signature and disabling the monomorphism restriction, my constant suddenly gets shared!

> :set -XNoMonomorphismRestriction
> let fib = 1 : 1 : zipWith (+) fib (tail fib)
> :t fib
fib :: Num a => [a]
> fib !! 50
20365011074
(0.00 secs, 2110136 bytes)

Why?!

Ugh… When compiled with optimisations, it is fast even with monomorphism restriction disabled.

  • 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-06-06T15:49:47+00:00Added an answer on June 6, 2026 at 3:49 pm

    By giving explicit type signature, you prevent GHC from making certain assumptions about your code. I’ll show an example (taken from this question):

    foo (x:y:_) = x == y
    foo [_]     = foo []
    foo []      = False
    

    According to GHCi, the type of this function is Eq a => [a] -> Bool, as you’d expect. However, if you declare foo with this signature, you’ll get “ambiguous type variable” error.

    The reason why this function works only without a type signature is because of how typechecking works in GHC. When you omit a type signature, foo is assumed to have monotype [a] -> Bool for some fixed type a. Once you finish typing the binding group, you generalize the types. That’s where you get the forall a. ....

    On the other hand, when you declare a polymorphic type signature, you explicitly state that foo is polymorphic (and thus the type of [] doesn’t have to match the type of first argument) and boom, you get ambiguous type variable.

    Now, knowing this, let’s compare the core:

    fib = 0:1:zipWith (+) fib (tail fib)
    -----
    fib :: forall a. Num a => [a]
    [GblId, Arity=1]
    fib =
      \ (@ a) ($dNum :: Num a) ->
        letrec {
          fib1 [Occ=LoopBreaker] :: [a]
          [LclId]
          fib1 =
            break<3>()
            : @ a
              (fromInteger @ a $dNum (__integer 0))
              (break<2>()
               : @ a
                 (fromInteger @ a $dNum (__integer 1))
                 (break<1>()
                  zipWith
                    @ a @ a @ a (+ @ a $dNum) fib1 (break<0>() tail @ a fib1))); } in
        fib1
    

    And for the second one:

    fib :: Num a => [a]
    fib = 0:1:zipWith (+) fib (tail fib)
    -----
    Rec {
    fib [Occ=LoopBreaker] :: forall a. Num a => [a]
    [GblId, Arity=1]
    fib =
      \ (@ a) ($dNum :: Num a) ->
        break<3>()
        : @ a
          (fromInteger @ a $dNum (__integer 0))
          (break<2>()
           : @ a
             (fromInteger @ a $dNum (__integer 1))
             (break<1>()
              zipWith
                @ a
                @ a
                @ a
                (+ @ a $dNum)
                (fib @ a $dNum)
                (break<0>() tail @ a (fib @ a $dNum))))
    end Rec }
    

    With explicit type signature, as with foo above, GHC has to treat fib as potentially polymorphically recursive value. We could pass some different Num dictionary to fib in zipWith (+) fib ... and at this point we would have to throw most of the list away, since different Num means different (+). Of course, once you compile with optimizations, GHC notices that Num dictionary never changes during “recursive calls” and optimizes it away.

    In the core above, you can see that GHC indeed gives fib a Num dictionary (named $dNum) again and again.

    Because fib without type signature was assumed to be monomorphic before the generalization of entire binding group was finished, the fib subparts were given exactly the same type as the whole fib. Thanks to this, fib looks like:

    {-# LANGUAGE ScopedTypeVariables #-}
    fib :: forall a. Num a => [a]
    fib = fib'
      where
        fib' :: [a]
        fib' = 0:1:zipWith (+) fib' (tail fib')
    

    And because the type stays fixed, you can use just the one dictionary given at start.

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

Sidebar

Related Questions

There was a question about regex and trying to answer I found another strange
I was trying to answer another question about the == operator and I created
While trying to write an answer for another SO question something really peculiar happened.
While trying to answer this question I found that the code int* p =
While trying to answer this question I found without () (which invokes C++ most
I have a job interview tomorrow and I'm trying to answer this question: There
(Came up with this question in the course of trying to answer this other
I have been trying to follow the instructions in the answer to this question
While trying to answer another question, I was serializing a C# object to an
I saw this question on SO about casting, and the answer specifically mentioned numeric

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.