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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:23:31+00:00 2026-06-15T15:23:31+00:00

I recently starting to play with haskell and I came across a problem while

  • 0

I recently starting to play with haskell and I came across a problem while using HashMap that can be illustrated by this toy example:

import Data.HashMap as HashMap

foo = HashMap.insert 42 53 HashMap.empty

Here is the error I get when I load my file in the interpreter or compile it:

Prelude List HashMap> :load TypeError.hs
[1 of 1] Compiling Main             ( TypeError.hs, interpreted )

TypeError.hs:3:22:
    Ambiguous type variable `k0' in the constraints:
      (Num k0) arising from the literal `42' at TypeError.hs:3:22-23
      (Ord k0) arising from a use of `insert' at TypeError.hs:3:7-20
      (Data.Hashable.Hashable k0)
        arising from a use of `insert' at TypeError.hs:3:7-20
    Possible cause: the monomorphism restriction applied to the following:
      foo :: Map k0 Integer (bound at TypeError.hs:3:1)
    Probable fix: give these definition(s) an explicit type signature
                  or use -XNoMonomorphismRestriction
    In the first argument of `insert', namely `42'
    In the expression: insert 42 53 empty
    In an equation for `foo': foo = insert 42 53 empty
Failed, modules loaded: none.
Prelude List HashMap>

However if I define the exact same function directly in the interpreter, I get no error:

Prelude List HashMap> let foo = HashMap.insert 42 53 HashMap.empty
Prelude List HashMap>

Does anyone have any clue about this?

Thanks.

  • 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-15T15:23:32+00:00Added an answer on June 15, 2026 at 3:23 pm

    The reason is that ghci uses extended default rules, while the compiler uses (by default) the defaulting rules specified in the language report:

    Ambiguities in the class Num are most common, so Haskell provides another way to resolve them—with a default declaration: default (t1 , … , tn) where n ≥ 0, and each ti must be a type for which Num ti holds. In situations where an ambiguous type is discovered, an ambiguous type variable, v, is defaultable if:

    • v appears only in constraints of the form C v, where C is a class, and
    • at least one of these classes is a numeric class, (that is, Num or a subclass of Num), and
    • all of these classes are defined in the Prelude or a standard library

    The relevant rule is that per the report defaulting is only done if all involved classes are defined in a standard library, but the Hashable constraint on the key involves a class that doesn’t meet that requirement.

    Thus the compiler rejects it, because it can’t resolve the ambiguous type variable arising from the key, while ghci defaults it to Integer, since ghci also defaults if other classes are involved.

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

Sidebar

Related Questions

This work great to delete php, txt and images but I recently starting using
I recently came across Flex builder and it was gr8 to know that it
I've been starting to play around with Node.js recently and I've come across a
I recently stumbled across a problem when starting my application via JNLP. I now
I've recently began using C++ with XCode and I'm starting to miss the integrated
I have a query that is very recently starting to throw: The built-in indices
I've starting using Codeigniter for a project recently (few months ago) but its getting
I have recently starting using the dojo library, and I am currently having some
I was recently asked by a friend of mine who's just starting to play
I read in a post to the Smack forum recently that Starting daemon threads

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.