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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:15:59+00:00 2026-06-08T17:15:59+00:00

Consider the following code example: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} —

  • 0

Consider the following code example:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-} -- Is there a way to avoid this?

-- A generic class with a generic function.
class Foo a where
  foo :: a -> a

-- A specific class with specific functions.
class Bar a where
  bar :: a -> a
  baz :: a -> a

-- Given the specific class functions, we can implement the generic class function.
instance Bar a => Foo a where
  foo = bar . baz

-- So if a type belongs to the specific class...
instance Bar String where
  bar = id
  baz = id

-- We can invoke the generic function on it.
main :: IO ()
main =
  putStrLn (foo "bar")

(My actual code is way more elaborate; this is a minimal boiled-down case to demonstrate the pattern.)

It isn’t clear to me why UndecidableInstances are needed here – the type parameter a appears once in both sides of the Bar a => Foo a, so I expected things to “just work”. I’m obviously missing something here. But at any rate, is there a way to do this without using UndecidableInstances?

  • 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-08T17:16:01+00:00Added an answer on June 8, 2026 at 5:16 pm

    There are a few approaches you can take; I don’t think you’ve provided enough context to determine which would be the most appropriate. If you’re using GHC-7.4, you might want to try the DefaultSignatures extension.

    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE DefaultSignatures #-}
    
    -- A generic class with a generic function.
    class Foo a where
      foo :: a -> a
      default foo :: Bar a => a -> a
      foo = bar . baz
    
    -- A specific class with specific functions.
    class Bar a where
      bar :: a -> a
      baz :: a -> a
    
    instance Bar String where
      bar = id
      baz = id
    
    instance Foo String
    
    main :: IO ()
    main =
      putStrLn (foo "bar")
    

    You still need to declare that a type is an instance of Foo, but you don’t need to repeat the method declaration because the default implementation will be used.

    Another fairly lightweight approach is to use a newtype. If you have functions that need a Foo instance, you can wrap a Bar instance in the newtype.

    newtype FooBar a = FooBar { unFooBar :: a }
    
    instance Bar a => Foo (FooBar a) where
        foo = FooBar . bar . baz . unFooBar
    
    -- imported from a library or something...
    needsFoo :: Foo a => a -> b
    
    myFunc = needsFoo (FooBar someBar)
    

    Alternatively, you may be able to get by with replacing foo with a normal function, or making a specialized version for Bar instances:

    -- if every `Foo` is also a `Bar`, you can just do this.  No need for `Foo` at all!
    foo :: Bar a => a -> a
    foo = bar . baz
    
    -- if most `Foo`s aren't `Bar`s, you may be able to use this function when you have a `Bar`
    fooBar :: Bar a => a -> a
    foo = bar . baz
    

    These are probably the best solutions if they work for your situation.

    Another option is to declare every Foo instance manually. Although there may be a lot of different conceivable instances, it’s fairly common for codebases to only have a handful of instances that are actually used. If that’s true here, it’s probably less work to just write out the 3 or 4 instances you need rather than try to implement a more general solution.

    As a very last resort, you can use something like your original code, but you’ll also need OverlappingInstances to make it work (if you don’t need OverlappingInstances, then you don’t need a Foo class). This is the extension that allows GHC to choose the “most specific instance” when there are multiple available matches. This will more or less work, although you may not get what you expect.

    class Foo a where
      foo :: a -> a
    
    class Bar a where
      bar :: a -> a
      baz :: a -> a
    
    instance Bar String where
      bar = id
      baz = id
    
    instance Bar a => Foo a where
      foo = bar . baz
    
    instance Foo [a] where
      foo _ = []
    
    main :: IO ()
    main =
      print (foo "foo")
    

    Now main prints an empty string. There are two Foo instances, for a and [a]. The latter is more specific, so it gets chosen for foo "foo" since a string has type [Char], although you probably wanted the former. So now you’d also need to write

    instance Foo String where
      foo = bar . baz
    

    at which point you may as well leave out the Bar a => Foo a instance entirely.

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

Sidebar

Related Questions

Consider the following code example: TempList.ForEach(Function(obj) obj.Deleted = True End Function) And this one:
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following code: (live example here ) $(function() { var wrapper = $(<div
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
For example, consider the following code (in a model): scope :popular, where(views >= 250
I would like different nested onmousedown events. For example, consider the following code: <div

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.