[Haskell-cafe] How to support multiple string types in Haskell?

Edward Z. Yang ezyang at mit.edu
Thu Jul 14 01:00:03 UTC 2016


Hello KwangYul,

Supporting this sort of parametricity is one of the goals of the
Backpack project.  You can see some code that demonstrates
how this might be done: https://github.com/yihuang/tagstream-conduit/pull/18
Maybe you'll get to use this feature in GHC 8.2!

A big problem with StringLike type classes is that you must
pre-commit to the set of operations which are supported before
hand. For strings, there are many, many such operations, and it is
difficult to agree a priori what these operations should be.

Edward

Excerpts from KwangYul Seo's message of 2016-07-13 17:40:13 -0700:
> Hi all,
> 
> There are multiple string types in Haskell – String, lazy/strict
> ByteString, lazy/strict Text to name a few. So to make a string handling
> function maximally reusable, it needs to support multiple string types.
> 
> One approach used by TagSoup library is to make a type class StringLike
> which represents the polymorphic string type and uses it where a String
> type is normally needed. For example,
> 
> parseTags :: StringLike str => str -> [Tag str]
> 
> Here parseTags takes a StringLike type instead of a fixed string type.
> Users of TagSoup can pick any of String, lazy/strict ByteString,
> lazy/strict Text because they are all instances of StringLike type class.
> 
> It seems StringLike type class is quite generic but it is used only in the
> TagSoup package. This makes me wonder what is the idiomatic way to support
> multiple string types in Haskell. What other approaches do we have?
> 
> Thanks,
> Kwang Yul Seo


More information about the Haskell-Cafe mailing list