fromString and toString

I know we have class IsString a where fromString :: String -> a But do we have an inverse generic function toString defined anywhere? IsString implies that the type `a` is a subset of String; if all we have is fromString, then IsString defines *supersets* of String. To me, the most logical thing would be that IsString defines bijections to String (i.e. things that are subsets *and* supersets). The reason this came up is that I'm getting pretty sick of type-juggling different string types from different libraries (String, Text, various ByteStrings), especially since I have to tweak all the functions I use if a type changes; discovering fromString was fantastic, but without toString, I can't define something like reString :: (IsString a, IsString b) => a -> b which is great to have if your putStrLn only accepts one kind of string, and you have several distinct ones floating around. Thanks, Ben

04.02.2014, 17:55, "Ben Foppa"
I know we have
class IsString a where
fromString :: String -> a
But do we have an inverse generic function toString defined anywhere? IsString implies that the type `a` is a subset of String; if all we have is fromString, then IsString defines supersets of String. To me, the most logical thing would be that IsString defines bijections to String (i.e. things that are subsets and supersets).
There's more than one way to interpret a ByteString as a String/Text and vice versa. If reString exists, what would you expect it to do when called with a ~ ByteString and b ~ Text?

Ben Foppa
But do we have an inverse generic function toString defined anywhere?
well, we have: class Show a where show :: a -> String
likereString :: (IsString a, IsString b) => a -> b
well you probably don't want to actually convert from a to String (= List of Char) then from there to b, so it would need to be accompanied by some RULES (for specific instances of a, b) for efficiency. This still does not answer the concern whether it is (semantically) wise to have String as the "base" concept here. - J.W.

Isn't toString just show?
On Feb 4, 2014 3:20 PM, "Johannes Waldmann"
Ben Foppa
writes: But do we have an inverse generic function toString defined anywhere?
well, we have: class Show a where show :: a -> String
likereString :: (IsString a, IsString b) => a -> b
well you probably don't want to actually convert from a to String (= List of Char) then from there to b, so it would need to be accompanied by some RULES (for specific instances of a, b) for efficiency.
This still does not answer the concern whether it is (semantically) wise to have String as the "base" concept here.
- J.W.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Show is usually valid Haskell (at least derived instances are), and
has instances for most types. This class would only be a conversion
from types that *are* strings in some sense (like Text) to String.
We have a (very small) package for this called 'isstring' that we use
internally. It has instances for String, Text (x2) and ByteString (x2)
assuming UTF8 encoding. I'd be happy to open source it if people are
interested.
Regards,
Erik
On Wed, Feb 5, 2014 at 9:13 AM, Atze van der Ploeg
Isn't toString just show?
On Feb 4, 2014 3:20 PM, "Johannes Waldmann"
wrote: Ben Foppa
writes: But do we have an inverse generic function toString defined anywhere?
well, we have: class Show a where show :: a -> String
likereString :: (IsString a, IsString b) => a -> b
well you probably don't want to actually convert from a to String (= List of Char) then from there to b, so it would need to be accompanied by some RULES (for specific instances of a, b) for efficiency.
This still does not answer the concern whether it is (semantically) wise to have String as the "base" concept here.
- J.W.
_______________________________________________ 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

On 05/02/14 09:35, Erik Hesselink wrote:
Show is usually valid Haskell (at least derived instances are), and has instances for most types. This class would only be a conversion from types that *are* strings in some sense (like Text) to String.
Totally agree. On 05/02/14 08:13, Atze van der Ploeg wrote:
Isn't toString just show?
No, show is very different: show "hello" == "\"hello\"" While you would expect toString "hello" == "hello" Personally, I would always recommend this: * Make show represent the "programmer version" of your data type, e.g. close to what "deriving Show" would give you, and implement it for every data type: newtype MyString = MyS String instance Show MyString where show (MyS s) = "MyS " ++ show s * Use a different type class (e.g. a ToString) to represent the stringyness of your type: instance ToString MyString where show (MyS s) = s
From my experience, everything else makes programming really nasty, outcomes unpredictable, and debugging very hard. If you want an example, try GHC code / the GHC API, where many things unfortunately do not have a show instance.
We have a (very small) package for this called 'isstring' that we use internally. It has instances for String, Text (x2) and ByteString (x2) assuming UTF8 encoding. I'd be happy to open source it if people are interested.
Yes. And I think a `ToString` typeclass would be useful be in base. Also, this existed once but is now deprecate it: http://hackage.haskell.org/package/to-string-class I speculate that it got deprecated because it is not nice to maintain all the orphan instances - the class in base would fix that.

I agree that exposing the isstring package would be useful. Again, I think the name IsString for the class defining fromString is very misleading, especially if we have an isstring package defining the inverse relationship (i.e. toString). More appropriate would be StringIs, though I'm not recommending changing Data.String.

On 2/4/14, 5:55 AM, Ben Foppa wrote:
The reason this came up is that I'm getting pretty sick of type-juggling different string types from different libraries (String, Text, various ByteStrings), especially since I have to tweak all the functions I use if a type changes; discovering fromString was fantastic, but without toString, I can't define something like
reString :: (IsString a, IsString b) => a -> b
which is great to have if your putStrLn only accepts one kind of string, and you have several distinct ones floating around.
Although it doesn't seem to be used much, I noticed that there's this package on Hackage: http://hackage.haskell.org/package/stringable-0.1.2/docs/Data-Stringable.htm... which seems like it's addressing the same problem you're trying to solve. --Patrick

That's exactly what I want, thank you!
On Feb 20, 2014 2:15 AM, "Patrick Pelletier"
wrote:
On 2/4/14, 5:55 AM, Ben Foppa wrote:
The reason this came up is that I'm getting pretty sick of type-juggling
different string types from different libraries (String, Text, various ByteStrings), especially since I have to tweak all the functions I use if a type changes; discovering fromString was fantastic, but without toString, I can't define something like
reString :: (IsString a, IsString b) => a -> b
which is great to have if your putStrLn only accepts one kind of string, and you have several distinct ones floating around.
Although it doesn't seem to be used much, I noticed that there's this package on Hackage:
http://hackage.haskell.org/package/stringable-0.1.2/docs/ Data-Stringable.html
which seems like it's addressing the same problem you're trying to solve.
--Patrick
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Atze van der Ploeg
-
Ben Foppa
-
Dmitry Dzhus
-
Erik Hesselink
-
Johannes Waldmann
-
Niklas Hambüchen
-
Patrick Pelletier