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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:18:28+00:00 2026-05-17T17:18:28+00:00

I have a polymorphic function like: convert :: (Show a) => a -> String

  • 0

I have a polymorphic function like:

convert :: (Show a) => a -> String
convert = " [label=" ++ (show a) ++ "]"

But sometimes I want to pass it a Data.Map and do some more fancy key value conversion. I know I can’t pattern match here because Data.Map is an abstract data type (according to this similar SO question), but I have been unsuccessful using guards to this end, and I’m not sure if ViewPatterns would help here (and would rather avoid them for portability).

This is more what I want:

import qualified Data.Map as M

convert :: (Show a) => a -> String
convert a 
    | M.size \=0 = processMap2FancyKVString a -- Heres a Data.Map
    | otherwise = " [label=" ++ (show a) ++ "]" -- Probably a string

But this doesn’t work because M.size can’t take anything other than a Data.Map.

Specifically, I am trying to modify the sl utility function in the Functional Graph Library in order to handle coloring and other attributes of edges in GraphViz output.

Update

I wish I could accept all three answers by TomMD, Antal S-Z, and luqui to this question as they all understood what I really was asking. I would say:

  • Antal S-Z gave the most ‘elegant’ solution as applied to the FGL but would also require the most rewriting and rethinking to implement in personal problem.
  • TomMD gave a great answer that lies somewhere between Antal S-Z’s and luqui’s in terms of applicability vs. correctness. It also is direct and to the point which I appreciate greatly and why I chose his answer.
  • luqui gave the best ‘get it working quickly’ answer which I will probably be using in practice (as I’m a grad student, and this is just some throwaway code to test some ideas). The reason I didn’t accept was because TomMD’s answer will probably help other people in more general situations better.

With that said, they are all excellent answers and the above classification is a gross simplification. I’ve also updated the question title to better represent my question (Thanks Thanks again for broadening my horizons everyone!

  • 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-17T17:18:29+00:00Added an answer on May 17, 2026 at 5:18 pm

    What you just explained is you want a function that behaves differently based on the type of the input. While you could use a data wrapper, thus closing the function for all time:

    data Convertable k a = ConvMap (Map k a) | ConvOther a
    convert (ConvMap m) = ...
    convert (ConvOther o) = ...
    

    A better way is to use type classes, thus leaving the convert function open and extensible while preventing users from inputting non-sensical combinations (ex: ConvOther M.empty).

    class (Show a) => Convertable a where
        convert :: a -> String
    
    instance Convertable (M.Map k a) where
        convert m = processMap2FancyKVString m
    
    newtype ConvWrapper a = CW a
    instance Convertable (ConvWrapper a) where
        convert (CW a) = " [label=" ++ (show a) ++ "]"
    

    In this manner you can have the instances you want used for each different data type and every time a new specialization is needed you can extend the definition of convert simply by adding another instance Convertable NewDataType where ....

    Some people might frown at the newtype wrapper and suggest an instance like:

    instance Convertable a where
        convert ...
    

    But this will require the strongly discouraged overlapping and undecidable instances extensions for very little programmer convenience.

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

Sidebar

Related Questions

I have a tags model that I'd like to be polymorphic, but I don't
i have a problem with function overloading. I will show you with some simple
I know that when you want to declare a polymorphic function you have to
I have this pimpl design where the implementation classes are polymorphic but the interfaces
I doubt this is possible but what I would like to do is have
This seems like a silly question but I want to be able to instantiate
This is a little contrived, but say I have a class interface like this:
Do interfaces (polymorphic class solely with pure virtual functions) have a vtable? Since interfaces
How do I make a dynamic variable name/object? I have a polymorphic model for
I have a Discussion (polymorphic resource) that can belong to Project, Task and Subtask.

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.