Hello,<br>I just noticed that the instances for this example look more readable when written with two recently proposed Haskell extensions. Perhaps we should consider implementing these in GHC?<br><div><br></div><div>Using chain instances: (<meta http-equiv="content-type" content="text/html; charset=utf-8"><a href="http://web.cecs.pdx.edu/~mpj/pubs/instancechains.pdf">http://web.cecs.pdx.edu/~mpj/pubs/instancechains.pdf</a>)<br>
<br><font class="Apple-style-span" face="'courier new', monospace">> instance DeepFlat a b => DeepFlat [a] b where dflat = concatMap dflat<br>> else DeepFlat a a where dflat = id<br>
> else fails <br></font><br></div><div>And with the fun. deps. in functional notation: (<a href="http://web.cecs.pdx.edu/~mpj/pubs/fundeps-design.pdf">http://web.cecs.pdx.edu/~mpj/pubs/fundeps-design.pdf</a>)</div>
<div><div><br><div><font class="Apple-style-span" face="'courier new', monospace">> instance DeepFlat [a] (DeepFlat a) where dflat = concatMap dflat<br>> else DeepFlat a a where dflat = id<br>
> else fails<br></font><br></div><div>Happy new year!<br>-Iavor<br><br><br><br><br><br>On Thu, Dec 30, 2010 at 3:52 AM, <<a href="mailto:oleg@okmij.org">oleg@okmij.org</a>> wrote:<br>><br>> William Murphy wrote:<br>
>> I've spent a lot of time trying to write a version of concat, which<br>>> concatenates lists of any "depth":<br>><br>> It is a little bit more involved, but quite possible. The code is not<br>
> much longer than the one you wrote (essentially, three lines: one<br>> class and two instance declarations). Here is the complete code:<br>><br>><br>> {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}<br>
> {-# LANGUAGE TypeFamilies, FlexibleInstances #-}<br>> {-# LANGUAGE UndecidableInstances #-}<br>> {-# LANGUAGE OverlappingInstances #-}<br>><br>> module DeepFlat where<br>><br>><br>> class DeepFlat a b | a -> b where<br>
> dflat :: [a] -> [b]<br>><br>> -- If we flatten a list of lists<br>> instance DeepFlat a b => DeepFlat [a] b where<br>> dflat = concatMap dflat<br>><br>> -- If we are given a list of non-lists<br>
> instance a ~ b => DeepFlat a b where<br>> dflat = id<br>><br>> test1 = dflat "abracadabra"<br>> -- "abracadabra"<br>><br>> test2 = dflat ["abra","cadabra"]<br>
><br>> test3 = dflat [["ab","ra"],["cad","abra"]]<br>> test4 = dflat [[["a","b"],["ra"]],[["cad","abra"]]]<br>><br>><br>
><br>><br>> _______________________________________________<br>> Haskell-Cafe mailing list<br>> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
><br><br></div></div></div>