Polymorphic functions over string libraries

Hi. I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc). For instance, imagine I have to read a file with text, transform this text in some way and output it to another file, or to the console. If I wanted to use String, I'd just do this: / transform :: String -> String main = readFile "input.txt" >>= writeFile "output.txt" . transform / But if I wanted to use Text instead, I'd have to use this: / import qualified Data.Text.IO as T transform :: Text -> Text main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform / Idem for ByteString. I was wondering if there was a way to create these computations in a generic way, for any kind of string library, something like this: / class StringLibrary s where: sReadFile :: FilePath -> IO s sWriteFile :: FilePath -> s -> IO () ... / So then I'd just have this: / transform :: StringLibrary s => s -> s main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform / Now I can perform the computation I want without being tied down to a specific library. At times when I create some quick scripts, I find myself using one library (for example using String to get it finished more quickly, since I have less experience with the other ones), but find that it's too slow or has some problem that is solved by using one of the other libraries. Yet swapping from one to the other is more cumbersome than expected at times. In the example above, I could easily swap between them, just by forcing the compiler to typecheck to a specific one (for instance by changing the type of "transform"). Or if I wanted to, I could leave it as it is and export it as a library of my own. Is there a way to do something like this in Haskell, with existing libraries? In terms of using the string datatype as some sort of container of characters, I think there are libraries like Lens and mono-traversable that allow you to do stuff like this. But I'm not too familiar with them (at least using them in this way). -- View this message in context: http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libra... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hi Gonzaw,
you could try switching to ClassyPrelude:
{-# LANGUAGE NoImplicitPrelude, OverloadedStrings #-}
import ClassyPrelude
-- | Just change the sig, and all typechecks
transform :: String -> String
--transform :: ByteString -> ByteString
--transform :: Text -> Text
transform s = s
xmain :: IO ()
xmain = readFile "input.txt" >>= writeFile "output.txt" . transform
You must write the signatures so the compiler knows which version to use, if it
can't guess by itself.
The downside is that sometimes the polymorphic signatures and errors on types
can be rather cryptic, but you'll get used to them.
(However, there are some issues when switching. Most of the issues I had were
- about missing partial functions (like head,tail, etc.), so as a quick
workaround you can use unsafeHead, unsafeTail, etc. until you find the time to
make it correct (or headMay, tailMay, etc) and
- switching from Prelude's FilePath to Filesystem's FilePath.)
Best regards,
vlatko
-------- Original Message --------
Subject: [Haskell-cafe] Polymorphic functions over string libraries
From: gonzaw
Hi.
I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc).
For instance, imagine I have to read a file with text, transform this text in some way and output it to another file, or to the console. If I wanted to use String, I'd just do this: / transform :: String -> String main = readFile "input.txt" >>= writeFile "output.txt" . transform / But if I wanted to use Text instead, I'd have to use this: / import qualified Data.Text.IO as T
transform :: Text -> Text main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform / Idem for ByteString.
I was wondering if there was a way to create these computations in a generic way, for any kind of string library, something like this: / class StringLibrary s where: sReadFile :: FilePath -> IO s sWriteFile :: FilePath -> s -> IO () ... / So then I'd just have this: / transform :: StringLibrary s => s -> s main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform / Now I can perform the computation I want without being tied down to a specific library. At times when I create some quick scripts, I find myself using one library (for example using String to get it finished more quickly, since I have less experience with the other ones), but find that it's too slow or has some problem that is solved by using one of the other libraries. Yet swapping from one to the other is more cumbersome than expected at times. In the example above, I could easily swap between them, just by forcing the compiler to typecheck to a specific one (for instance by changing the type of "transform"). Or if I wanted to, I could leave it as it is and export it as a library of my own.
Is there a way to do something like this in Haskell, with existing libraries? In terms of using the string datatype as some sort of container of characters, I think there are libraries like Lens and mono-traversable that allow you to do stuff like this. But I'm not too familiar with them (at least using them in this way).
-- View this message in context: http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libra... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

* Vlatko Basic
Hi Gonzaw,
you could try switching to ClassyPrelude:
{-# LANGUAGE NoImplicitPrelude, OverloadedStrings #-}
Absolutely agree, but I would also mention mono-traversable package,
used by classyPrelude, but probably is lesser dependency.
--
Best regards, Dmitry Bogatov

This is one of the problems that an improved module language would solve such as the one in SML (Signatures and functors provide for this functionality). In Haskell-land, this is an active area of research. You may be interested in Backpack: http://plv.mpi-sws.org/backpack/ gonzaw writes:
Hi.
I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc).
For instance, imagine I have to read a file with text, transform this text in some way and output it to another file, or to the console. If I wanted to use String, I'd just do this: / transform :: String -> String main = readFile "input.txt" >>= writeFile "output.txt" . transform / But if I wanted to use Text instead, I'd have to use this: / import qualified Data.Text.IO as T
transform :: Text -> Text main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform / Idem for ByteString.
I was wondering if there was a way to create these computations in a generic way, for any kind of string library, something like this: / class StringLibrary s where: sReadFile :: FilePath -> IO s sWriteFile :: FilePath -> s -> IO () ... / So then I'd just have this: / transform :: StringLibrary s => s -> s main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform / Now I can perform the computation I want without being tied down to a specific library. At times when I create some quick scripts, I find myself using one library (for example using String to get it finished more quickly, since I have less experience with the other ones), but find that it's too slow or has some problem that is solved by using one of the other libraries. Yet swapping from one to the other is more cumbersome than expected at times. In the example above, I could easily swap between them, just by forcing the compiler to typecheck to a specific one (for instance by changing the type of "transform"). Or if I wanted to, I could leave it as it is and export it as a library of my own.
Is there a way to do something like this in Haskell, with existing libraries? In terms of using the string datatype as some sort of container of characters, I think there are libraries like Lens and mono-traversable that allow you to do stuff like this. But I'm not too familiar with them (at least using them in this way).
-- Kyle Marek-Spartz

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

I
If you want to accept String and Text and ByteString your implicitly state
that you do not make any assumptions about the encoding
I think the most pragmatic (and perhaps even the most appropriate) solution
is to simply to expose one set of functions working on one type.
If you assume UTF-8 you should not accept a ByteString since it's not
encoding aware. If you did you'd have to add documentation saying "this
assumes that your bytestring contains valid UTF-8". Use Text instead since
it's the best unicode-aware library available.
If you don't make any assumptions about encoding, pick ByteString.
On Sun, Oct 26, 2014 at 8:43 PM, John Lato
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.
On Sun, Oct 26, 2014 at 9:18 AM, Kyle Marek-Spartz < kyle.marek.spartz@gmail.com> wrote:
This is one of the problems that an improved module language would solve such as the one in SML (Signatures and functors provide for this functionality). In Haskell-land, this is an active area of research. You may be interested in Backpack:
http://plv.mpi-sws.org/backpack/
gonzaw writes:
Hi.
I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc).
For instance, imagine I have to read a file with text, transform this text in some way and output it to another file, or to the console. If I wanted to use String, I'd just do this: / transform :: String -> String main = readFile "input.txt" >>= writeFile "output.txt" . transform / But if I wanted to use Text instead, I'd have to use this: / import qualified Data.Text.IO as T
transform :: Text -> Text main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform / Idem for ByteString.
I was wondering if there was a way to create these computations in a generic way, for any kind of string library, something like this: / class StringLibrary s where: sReadFile :: FilePath -> IO s sWriteFile :: FilePath -> s -> IO () ... / So then I'd just have this: / transform :: StringLibrary s => s -> s main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform / Now I can perform the computation I want without being tied down to a specific library. At times when I create some quick scripts, I find myself using one library (for example using String to get it finished more quickly, since I have less experience with the other ones), but find that it's too slow or has some problem that is solved by using one of the other libraries. Yet swapping from one to the other is more cumbersome than expected at times. In the example above, I could easily swap between them, just by forcing the compiler to typecheck to a specific one (for instance by changing the type of "transform"). Or if I wanted to, I could leave it as it is and export it as a library of my own.
Is there a way to do something like this in Haskell, with existing libraries? In terms of using the string datatype as some sort of container of characters, I think there are libraries like Lens and mono-traversable that allow you to do stuff like this. But I'm not too familiar with them (at least using them in this way).
-- Kyle Marek-Spartz _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Gonzaw,
Among other alternatives you could try using
http://hackage.haskell.org/package/ListLike package. It provides
similar interface to strings and text, but all functions operate on a
typeclass so they may be used with either Text or Strings. E.g.
import Data.Char
import Data.Text (Text)
import qualified Data.Text as T
import Data.ListLike (ListLike, ListLikeIO)
import qualified Data.ListLike as LL
transform :: String -> String
transform = map toUpper
transform' :: Text -> Text
transform' = T.map toUpper
onFile :: (ListLikeIO s c) => FilePath -> FilePath -> (s -> s) -> IO ()
onFile fileIn fileOut f = LL.readFile fileIn >>= LL.writeFile fileOut . f
main :: IO ()
main = do
writeFile "foo" "hello world\n"
onFile "foo" "bar" transform
onFile "foo" "bar" transform'
On Mon, Oct 27, 2014 at 7:50 PM, Adam Bergmark
I If you want to accept String and Text and ByteString your implicitly state that you do not make any assumptions about the encoding
I think the most pragmatic (and perhaps even the most appropriate) solution is to simply to expose one set of functions working on one type.
If you assume UTF-8 you should not accept a ByteString since it's not encoding aware. If you did you'd have to add documentation saying "this assumes that your bytestring contains valid UTF-8". Use Text instead since it's the best unicode-aware library available. If you don't make any assumptions about encoding, pick ByteString.
On Sun, Oct 26, 2014 at 8:43 PM, John Lato
wrote: 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.
On Sun, Oct 26, 2014 at 9:18 AM, Kyle Marek-Spartz
wrote: This is one of the problems that an improved module language would solve such as the one in SML (Signatures and functors provide for this functionality). In Haskell-land, this is an active area of research. You may be interested in Backpack:
http://plv.mpi-sws.org/backpack/
gonzaw writes:
Hi.
I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc).
For instance, imagine I have to read a file with text, transform this text in some way and output it to another file, or to the console. If I wanted to use String, I'd just do this: / transform :: String -> String main = readFile "input.txt" >>= writeFile "output.txt" . transform / But if I wanted to use Text instead, I'd have to use this: / import qualified Data.Text.IO as T
transform :: Text -> Text main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform / Idem for ByteString.
I was wondering if there was a way to create these computations in a generic way, for any kind of string library, something like this: / class StringLibrary s where: sReadFile :: FilePath -> IO s sWriteFile :: FilePath -> s -> IO () ... / So then I'd just have this: / transform :: StringLibrary s => s -> s main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform / Now I can perform the computation I want without being tied down to a specific library. At times when I create some quick scripts, I find myself using one library (for example using String to get it finished more quickly, since I have less experience with the other ones), but find that it's too slow or has some problem that is solved by using one of the other libraries. Yet swapping from one to the other is more cumbersome than expected at times. In the example above, I could easily swap between them, just by forcing the compiler to typecheck to a specific one (for instance by changing the type of "transform"). Or if I wanted to, I could leave it as it is and export it as a library of my own.
Is there a way to do something like this in Haskell, with existing libraries? In terms of using the string datatype as some sort of container of characters, I think there are libraries like Lens and mono-traversable that allow you to do stuff like this. But I'm not too familiar with them (at least using them in this way).
-- Kyle Marek-Spartz _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yes, I don't explicitly want to make any assumptions about encoding. I want to make it general enough, so that if I encounter a situation where I DO have to make assumptions about the encoding, I can easily use Text (instead of whatever I was using before). If I have to start making assumptions about other things, then I could easily change it to other string libraries. Thanks for the responses, I think switching to classy-prelude might be the easiest one (it also forces good practices). How do those other libraries compose with Prelude, and other modules from base? For instance, does ListLike compose well with Data.Foldable? Same with mono-traversable. Does MonoFunctor compose well with Functor? Is there an easy "glue" code to, for instance, pass any Functor to a function that expects a MonoFunctor and make it work? Idem with other typeclasses. Basically, I think all of these seem great, but I'm concerned about getting "stuck" with them alone, and not being able to use all the other countless great libraries with them because of compatibility issues. -- View this message in context: http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libra... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

ListLike and mono-traversable both provide their own versions of Functor and Foldable (I don't think ListLike has Traversable, exactly, but it could). The problem is that data structures like ByteString and Text simply don't admit instances of these classes. In fact, that's exactly what mono-traversable is: a monomorphic variant of Traversable (and hence the others as well). It's entirely possible to write something like
someFunction :: (ListLike (f el) el, Foldable f) => f el -> a
and it may even be sensible, but it means you can't use e.g. Text as an argument to someFunction. I don't believe mono-traversable provides
instance Functor f => MonoFunctor (f a)
that instance is problematic because it overlaps with many other instances
one could wish to write. However, there are instances for pretty much
every type in base that makes sense, so you can pass any particular Functor
to code that expects a MonoFunctor and it would work.
If you're concerned about getting stuck with a particular library,
classy-prelude seems like exactly the wrong choice, as it makes a lot of
this implicit rather than explicit, which means it would be harder to
migrate away from if you choose to do so later. Besides, it uses
mono-traversable under the hood anyway.
John L.
On Tue, Oct 28, 2014 at 7:05 AM, gonzaw
Yes, I don't explicitly want to make any assumptions about encoding. I want to make it general enough, so that if I encounter a situation where I DO have to make assumptions about the encoding, I can easily use Text (instead of whatever I was using before). If I have to start making assumptions about other things, then I could easily change it to other string libraries.
Thanks for the responses, I think switching to classy-prelude might be the easiest one (it also forces good practices).
How do those other libraries compose with Prelude, and other modules from base? For instance, does ListLike compose well with Data.Foldable? Same with mono-traversable. Does MonoFunctor compose well with Functor? Is there an easy "glue" code to, for instance, pass any Functor to a function that expects a MonoFunctor and make it work? Idem with other typeclasses.
Basically, I think all of these seem great, but I'm concerned about getting "stuck" with them alone, and not being able to use all the other countless great libraries with them because of compatibility issues.
-- View this message in context: http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libra... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I use small string-class library
http://hackage.haskell.org/package/string-class
You have type classes for generic things (I don't use them in practice
much, to be honest, so please check them out by yourself), and also you
have easy toText/toString/fromText/fromString etc. functions, which are
quite handy.
On Sun, Oct 26, 2014 at 3:36 AM, gonzaw
Hi.
I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc).
For instance, imagine I have to read a file with text, transform this text in some way and output it to another file, or to the console. If I wanted to use String, I'd just do this: / transform :: String -> String main = readFile "input.txt" >>= writeFile "output.txt" . transform / But if I wanted to use Text instead, I'd have to use this: / import qualified Data.Text.IO as T
transform :: Text -> Text main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform / Idem for ByteString.
I was wondering if there was a way to create these computations in a generic way, for any kind of string library, something like this: / class StringLibrary s where: sReadFile :: FilePath -> IO s sWriteFile :: FilePath -> s -> IO () ... / So then I'd just have this: / transform :: StringLibrary s => s -> s main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform / Now I can perform the computation I want without being tied down to a specific library. At times when I create some quick scripts, I find myself using one library (for example using String to get it finished more quickly, since I have less experience with the other ones), but find that it's too slow or has some problem that is solved by using one of the other libraries. Yet swapping from one to the other is more cumbersome than expected at times. In the example above, I could easily swap between them, just by forcing the compiler to typecheck to a specific one (for instance by changing the type of "transform"). Or if I wanted to, I could leave it as it is and export it as a library of my own.
Is there a way to do something like this in Haskell, with existing libraries? In terms of using the string datatype as some sort of container of characters, I think there are libraries like Lens and mono-traversable that allow you to do stuff like this. But I'm not too familiar with them (at least using them in this way).
-- View this message in context: http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libra... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 25/10/14 09:36 PM, gonzaw wrote:
Hi.
I was wondering what would be the best way to create a polymorphic function over any possible string library (Text, String, Bytestring, etc).
I'm a bit late to the party, so let me just mention my monoid-subclasses package as well. It does not (yet) provide any generic IO functions, but it covers the in-memory manipulations.
participants (9)
-
Adam Bergmark
-
Dmitry Bogatov
-
gonzaw
-
John Lato
-
Konstantine Rybnikov
-
Kyle Marek-Spartz
-
Mario Blažević
-
Sergey Vinokurov
-
Vlatko Basic