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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:13:19+00:00 2026-05-29T06:13:19+00:00

I started studying Haskell one week ago and have one strange problem. I created

  • 0

I started studying Haskell one week ago and have one strange problem. I created a simple data type and want to show it in a console. I created 2 functions for 2 constructors of my type. The compiler can call function if I use a constructor with 2 arguments. But it can’t call another function which should catch a constructor with 1 argument.

module Main (
    main
) where

data MyContainter a b = FirstVersion a b
                  | SecondVersion a
                  deriving(Show,Eq)

showContainer (FirstVersion a b) = show b
showContainer (SecondVersion a) = show a

--startF = showContainer (FirstVersion 1 2) -- it works
startF = showContainer (SecondVersion 1) -- it doesn't work

main = putStr startF

The compilers tells:

Ambiguous type variable `a0' in the constraint:
  (Show a0) arising from a use of `showMaybe'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: showMaybe (SecondVersion 1)
In an equation for `startF': startF = showMaybe (SecondVersion 1)

Why does it tell that? I created (SecondVersion 1) directly and don’t understand why the compiler doesn’t call showContainer (SecondVersion a).

  • 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-29T06:13:20+00:00Added an answer on May 29, 2026 at 6:13 am

    The problem is that showContainer has the type:

    showContainer :: (Show a, Show b) => MyContainer a b -> String
    

    But when you pass SecondVersion 1, it doesn’t know what b is, because SecondVersion 1 works for any type of b! When you pass a FirstVersion, it works fine, because, as FirstVersion contains both an a and a b, there’s never any ambiguity as to what they should be.

    So, since the compiler has no way of knowing what b you want, and no way of knowing that the choice of b doesn’t affect showContainer (after all, it does affect the behaviour when you pass FirstVersion, since it uses show on a value of type b), it gives up.

    That’s what the error message is saying: the type variable a01 is ambiguous, so please add a type signature to tell me what it is. In this case, it doesn’t matter what it is, so you can just set it to ():

    startF = showContainer (SecondVersion 1 :: MyContainer Integer ())
    

    You probably won’t run into errors like this very often, since the context you use the values in will usually force a specific b to be used.

    1 GHC isn’t the best at picking type variables, unfortunately; if you gave showContainer an explicit type signature like I showed, then it’d use b in the error message too.

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

Sidebar

Related Questions

I have started studying data structures again . I found very few practical uses
I have just started studying Objective - C..and I encountered a problem which I
I just started studying programming about 6 months ago and I have really been
Sorry if this question is too simple or easy. I just started studying Delegates
I have recently started studying Google Web Toolkit. I have went through some walkthroughs,
I have started studying about C++0x. I came across the follow expression somewhere: int
I have just started studying Python . I am referring to the tutorials at
I have just started studying Android and I am still lost. I would just
I have just started studying Android, I have limited java knowledge but am semi
I started to use php a few hours ago. To begin studying I decided

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.