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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:14:25+00:00 2026-06-12T18:14:25+00:00

I don’t understand why Haskell can’t figure out the type for line 8 in

  • 0

I don’t understand why Haskell can’t figure out the type for line 8 in the following code. Doesn’t the type signature of the expressMaybe function establish that the result type is the same as the type of the two input parameters?

{-# LANGUAGE MultiParamTypeClasses #-}

class Gene g n where
  express :: g -> g -> g
  -- there will be other functions that use the "n" type parameter

expressMaybe :: Gene g n => Maybe g -> Maybe g -> Maybe g
expressMaybe (Just a) (Just b) = Just (express a b) -- line 8
expressMaybe (Just a) Nothing  = Just a
expressMaybe Nothing (Just b)  = Just b
expressMaybe Nothing Nothing   = Nothing

The error I get is:

Amy20.hs:8:40:
    Ambiguous type variable `n0' in the constraint:
      (Gene g n0) arising from a use of `express'
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `Just', namely `(express a b)'
    In the expression: Just (express a b)
    In an equation for `expressMaybe':
        expressMaybe (Just a) (Just b) = Just (express a b)
Failed, modules loaded: none.

I tried playing around with RankNTypes and ScopedTypeVariables, but I couldn’t figure out how to make the error go away.

Thank you in advance for your help!

Edit: Now that I understand the problem, I used fundeps, because I’m familiar with them, and for my application it doesn’t make much sense to have more than one “alphabet” for encoding genes. I’ve never used type families before, though, so I’ll look into that too.

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}

class Gene g n | g -> n where
  express :: g -> g -> g
  -- there will be other functions that use the "n" type parameter

expressMaybe :: Gene g n => Maybe g -> Maybe g -> Maybe g
expressMaybe (Just a) (Just b) = Just (express a b) -- line 8
expressMaybe (Just a) Nothing  = Just a
expressMaybe Nothing (Just b)  = Just b
expressMaybe Nothing Nothing   = Nothing
  • 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-12T18:14:26+00:00Added an answer on June 12, 2026 at 6:14 pm

    Consider this:

    instance Gene String Int where
      express _ _ = "INT"
    
    instance Gene String Double where
      express _ _ = "DOUBLE"
    
    expressMaybe (Just "") (Just "")
    

    Should that code produce (Just "INT") or (Just "DOUBLE")? Yes, Haskell knows that the result of expressMaybe will have the same type as the arguments. But that doesn’t mean it knows which instance to use here since there can be multiple instances for the same type g with different n.

    If in your case there will only ever be one type n for each type g, you might consider using extensions like type families or functional dependencies with which you can express that fact to the type system.

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

Sidebar

Related Questions

Don't understand this simple code: def main(): print (This program illustrates a chaotic function)
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't ask me how--I haven't the slightest. The following code crashes my terminal and
Don't be scared of the extensive code. The problem is general. I just provided
Don't ask me why but I need to do the following: string ClassName =
I don't know why, but this code worked for me a month ago... maybe
Don't think that I'm mad, I understand how php works! That being said. I
Don't know if anyone can help me with this or if it's even possible.

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.