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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:56:57+00:00 2026-06-13T17:56:57+00:00

I have a type class Foo with an associated type: {-# LANGUAGE TypeFamilies #-}

  • 0

I have a type class Foo with an associated type:

{-# LANGUAGE TypeFamilies #-}

class Foo a where
    type Bar a
    foo :: a -> Bar a

Now I want to define a data type that holds one of the associated types, and derive a Show instance for it:

data Baz a = Baz (Bar a) deriving (Show)

This doesn’t compile, though, because you can’t guarantee that there is a Show instance for Bar a

No instance for (Show (Bar a))
  arising from the 'deriving' clause of a data type declaration

I can fix the problem by turning on FlexibleContexts and UndecidableInstances and writing a manual Show instance as follows

{-# LANGUAGE FlexibleContexts, UndecidableInstances #-}

data Baz a = Bar a

instance (Show a, Show (Bar a)) => Show (Baz a) where
    showsPrec _ (Baz x) = showString "Baz " . shows x

But that’s not particularly satisfactory, especially when Baz is a more complicated than a simple wrapper around one value, or when I also want to derive instances of other type classes. Is there a way out?

  • 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-13T17:56:59+00:00Added an answer on June 13, 2026 at 5:56 pm

    You can use StandaloneDeriving to ask GHC to generate the same Show instance as ever, but with a different context:

    {-# LANGUAGE FlexibleContexts, StandaloneDeriving, TypeFamilies, UndecidableInstances #-}
    
    class Foo a where
        type Bar a
        foo :: a -> Bar a
    
    data Baz a = Baz (Bar a)
    deriving instance Show (Bar a) => Show (Baz a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a type like this: class Foo { public: int bar[3]; /*Possibly some
I have a generic class foo . I have a Type object called bar
Suppose you have a class Foo with private member of type Bar. You don't
I have a nested type that I want to partially specify using associated type
I have a class called Foo that defines a list of objects of type
I have a Class with generic type: class Foo<T> { protected boolean validateType(Object obj){
I have following test.pyx cdef public class Foo[object Foo, type FooType]: cdef public char*
I have generic type that looks like: public class GenericClass<T, U> where T :
If have a type hierarchy that starts out with a generic type class A<T>
If I have a class with a constructor that takes a parametized generic type:

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.