<div dir="ltr">This could be solved by SML-style modules, but as you mention, that's an active area of research.  Class-based solutions, such as ListLike or the newer mono-traversable, have been available for years.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Oct 26, 2014 at 9:18 AM, Kyle Marek-Spartz <span dir="ltr"><<a href="mailto:kyle.marek.spartz@gmail.com" target="_blank">kyle.marek.spartz@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is one of the problems that an improved module language would solve<br>
such as the one in SML (Signatures and functors provide for this<br>
functionality). In Haskell-land, this is an active area of research. You<br>
may be interested in Backpack:<br>
<br>
<a href="http://plv.mpi-sws.org/backpack/" target="_blank">http://plv.mpi-sws.org/backpack/</a><br>
<div class="HOEnZb"><div class="h5"><br>
<br>
gonzaw writes:<br>
<br>
> Hi.<br>
><br>
> I was wondering what would be the best way to create a polymorphic function<br>
> over any possible string library (Text, String, Bytestring, etc).<br>
><br>
> For instance, imagine I have to read a file with text, transform this text<br>
> in some way and output it to another file, or to the console.<br>
> If I wanted to use String, I'd just do this:<br>
> /<br>
> transform :: String -> String<br>
> main = readFile "input.txt" >>= writeFile "output.txt" . transform<br>
> /<br>
> But if I wanted to use Text instead, I'd have to use this:<br>
> /<br>
> import qualified <a href="http://Data.Text.IO" target="_blank">Data.Text.IO</a> as T<br>
><br>
> transform :: Text -> Text<br>
> main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform<br>
> /<br>
> Idem for ByteString.<br>
><br>
> I was wondering if there was a way to create these computations in a generic<br>
> way, for any kind of string library, something like this:<br>
> /<br>
> class StringLibrary s where:<br>
> sReadFile :: FilePath -> IO s<br>
> sWriteFile :: FilePath -> s -> IO ()<br>
> ...<br>
> /<br>
> So then I'd just have this:<br>
> /<br>
> transform :: StringLibrary s => s -> s<br>
> main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform<br>
> /<br>
> Now I can perform the computation I want without being tied down to a<br>
> specific library. At times when I create some quick scripts, I find myself<br>
> using one library (for example using String to get it finished more quickly,<br>
> since I have less experience with the other ones), but find that it's too<br>
> slow or has some problem that is solved by using one of the other libraries.<br>
> Yet swapping from one to the other is more cumbersome than expected at<br>
> times.<br>
> In the example above, I could easily swap between them, just by forcing the<br>
> compiler to typecheck to a specific one (for instance by changing the type<br>
> of "transform"). Or if I wanted to, I could leave it as it is and export it<br>
> as a library of my own.<br>
><br>
> Is there a way to do something like this in Haskell, with existing<br>
> libraries?<br>
> In terms of using the string datatype as some sort of container of<br>
> characters, I think there are libraries like Lens and mono-traversable that<br>
> allow you to do stuff like this. But I'm not too familiar with them (at<br>
> least using them in this way).<br>
<br>
--<br>
</div></div><span class="HOEnZb"><font color="#888888">Kyle Marek-Spartz<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<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" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br></div>