<div dir="ltr"><div class="gmail_default" style="font-size:small">Wow, I had no idea that existed!</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 19, 2016 at 4:23 PM, Edward Z. Yang <span dir="ltr"><<a href="mailto:ezyang@mit.edu" target="_blank">ezyang@mit.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">A RULE could work. Assuming fromListLike is inlined, a rule can<br>
detect when (fromList . toList :: a -> a) and then replace it with id.<br>
<br>
edward<br>
<br>
Excerpts from David Fox's message of 2016-06-19 16:01:01 -0700:<br>
<div class="HOEnZb"><div class="h5">> Suppose you have a class ListLike (which you do have, actually) and it has<br>
> methods for operations on types that are like lists, and in particular it<br>
> has a method fromListLike which converts any ListLike value to any other:<br>
><br>
> fromListLike :: ListLike full' item => full' -> full<br>
><br>
> the default implementation of this is simply<br>
><br>
> fromListLike = fromList . toList<br>
><br>
> but this means that if you happen to apply fromListLike to a value which is<br>
> already the desired type, two unnecessary and possibly expensive<br>
> conversions take place. My question is, how can one write code that<br>
> implements fromListLike in such a way that when the two type parameters are<br>
> the same type it just uses<br>
><br>
> fromListLike = id<br>
><br>
> I've thought about using a class Convert a b class and writing an instance<br>
> for every pair of ListLike instances. I haven't convinced myself this<br>
> would work, and if it does it means that adding a new instance involves<br>
> writing a bunch of Convert instances. Maybe one could somehow have a<br>
> default implementation (fromList . toList) but then you run into<br>
> overlapping instances. Anyone have better ideas about any of this?<br>
> Hopefully I'm missing something dead simple...<br>
</div></div></blockquote></div><br></div>