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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:39:49+00:00 2026-05-24T15:39:49+00:00

I’d like to define a function which can show values of any type, with

  • 0

I’d like to define a function which can “show” values of any type, with special behavior for types which actually do define a Show instance:

magicShowCast :: ?

debugShow :: a -> String
debugShow x = case magicShowCast x of
    Just x' -> show x'
    Nothing -> "<unprintable>"

This would be used to add more detailed information to error messages when something goes wrong:

-- needs to work on non-Showable types
assertEq :: Eq a => a -> a -> IO ()
assertEq x y = when (x /= y)
    (throwIO (AssertionFailed (debugShow x) (debugShow y)))

data CanShow = CanShow1
             | CanShow 2
    deriving (Eq, Show)

data NoShow = NoShow1
            | NoShow2
    deriving (Eq)

-- AssertionFailed "CanShow1" "CanShow2"
assertEq CanShow1 CanShow2

-- AssertionFailed "<unprintable>" "<unprintable>"
assertEq NoShow1 NoShow2

Is there any way to do this? I tried using various combinations of GADTs, existential types, and template haskell, but either these aren’t enough or I can’t figure out how to apply them properly.

  • 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-24T15:39:50+00:00Added an answer on May 24, 2026 at 3:39 pm

    The real answer: You can’t. Haskell intentionally doesn’t define a generic “serialize to string” function, and being able to do so without some type class constraint would violate parametricity all over town. Dreadful, just dreadful.

    If you don’t see why this poses a problem, consider the following type signature:

    something :: (a, a) -> b -> a
    

    How would you implement this function? The generic type means it has to be either const . fst or const . snd, right? Hmm.

    something (x,y) z = if debugShow z == debugShow y then y else x
    
    > something ('a', 'b') ()
    'a'
    > something ('a', 'b') 'b'
    'b'
    

    Oooooooops! So much for being able to reason about your program in any sane way. That’s it, show‘s over, go home, it was fun while it lasted.


    The terrible, no good, unwise answer: Sure, if you don’t mind shamelessly cheating. Did I mention that example above was an actual GHCi session? Ha, ha.

    import Control.Exception
    import Control.Monad
    import GHC.Vacuum
    
    debugShow :: a -> String
    debugShow = show . nameGraph . vacuumLazy
    
    assertEq :: Eq a => a -> a -> IO ()
    assertEq x y = when (x /= y) . throwIO . AssertionFailed $ 
        unlines ["assertEq failed:", '\t':debugShow x, "=/=", '\t':debugShow y]
    
    data NoShow = NoShow1
                | NoShow2
        deriving (Eq)
    
    > assertEq NoShow1 NoShow2
    *** Exception: assertEq failed:
        [("|0",["NoShow1|1"]),("NoShow1|1",[])]
    =/=
        [("|0",["NoShow2|1"]),("NoShow2|1",[])]
    

    Oh. Ok. That looks like a fantastic idea, doesn’t it.

    Anyway, this doesn’t quite give you what you want, since there’s no obvious way to fall back to a proper Show instance when available. On the other hand, this lets you do a lot more than show can do, so perhaps it’s a wash.

    Seriously, though. Don’t do this in actual, working code. Ok, error reporting, debugging, logging… that makes sense. But otherwise, it’s probably very ill-advised.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.