Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to implement ad-hoc polymorphism in Haskell?
    primarykey
    data
    text
    <p>I have a polymorphic function like: </p> <pre><code>convert :: (Show a) =&gt; a -&gt; String convert = " [label=" ++ (show a) ++ "]" </code></pre> <p>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 <a href="https://stackoverflow.com/questions/3327532/haskell-pattern-matching-on-the-empty-set">this similar SO question</a>), 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). </p> <p>This is more what I want:</p> <pre><code>import qualified Data.Map as M convert :: (Show a) =&gt; a -&gt; String convert a | M.size \=0 = processMap2FancyKVString a -- Heres a Data.Map | otherwise = " [label=" ++ (show a) ++ "]" -- Probably a string </code></pre> <p>But this doesn't work because M.size can't take anything other than a Data.Map.</p> <p>Specifically, I am trying to modify the <a href="http://hackage.haskell.org/packages/archive/fgl/5.4.2.3/doc/html/src/Data-Graph-Inductive-Graphviz.html#graphviz%27" rel="nofollow noreferrer">sl utility function in the Functional Graph Library</a> in order to handle coloring and other attributes of edges in GraphViz output. </p> <p><strong>Update</strong></p> <p>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:</p> <ul> <li>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.</li> <li>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.</li> <li>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.</li> </ul> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload