<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jun 26, 2015 at 9:55 AM, Matt Williams <span dir="ltr"><<a href="mailto:matt.williams45.mw@gmail.com" target="_blank">matt.williams45.mw@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>I am trying to produce a Map, where the (tricky) idea is that the key is a pair, (t1, t2), and the key is considered identical under ordering. Thus:<br></div><br></div>(t1, t2) is the same as (t2, t1) but<br></div>(t1, t3) is not the same as (t1,t2).<br><br></div>This LOOKS like a equality definition. However, the Map key typeclass is defined as Ord, which requires me to define compare:<br></div></blockquote><div><br></div><div>You need more than Eq to get a collection which can be searched efficiently. Map uses Ord; Hashmap (in unordered-containers) uses Hashable, and might be more appropriate for this type. You will still have to deal with the pair, however.</div><div><br></div><div>Ord (or Hashable) is only used internally for searching, so you can define an instance which does not necessarily do anything semantically meaningful. For example, one way to define a `compare` for this is to sort the values in the pairs and then apply compare between them:</div><div><br></div><div><font face="monospace, monospace">    -- assumes s, t are themselves known by compiler to be Ord</font></div><div><font face="monospace, monospace">    instance Ord Edge where</font></div><div><font face="monospace, monospace">        (Edge s1 t1) `compare` (Edge s2 t2) = let arb s t = if s < t then (s,t) else (t,s)</font></div><div><font face="monospace, monospace">                                               in arb s1 t1 `compare` arb s2 t2</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">A similar trick could be used to get a Hashable instance.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">This would end up being slow for large maps or many lookups. In that case, you might consider a wrapper which applies the above "arb" operation to the key on insert or lookup (the "normalized" key is stored in the map), rather than having to compute it for every node traversed during lookup.</font></div></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>brandon s allbery kf8nh                               sine nomine associates</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>                                  <a href="mailto:ballbery@sinenomine.net" target="_blank">ballbery@sinenomine.net</a></div><div>unix, openafs, kerberos, infrastructure, xmonad        <a href="http://sinenomine.net" target="_blank">http://sinenomine.net</a></div></div></div>
</div></div>