Compilation of Overloaded strings and saving constant results

Clinton Mead clintonmead at gmail.com
Mon Jul 25 04:15:49 UTC 2022


Hi All

I'm presuming (correct me if I'm wrong) that when I have an overloaded
string literal in my program, let's say it's overloaded to Text, the
conversion from [Char] -> Text happens at runtime, instead of what would be
ideal being that the compiler somehow does that conversion at compile time
and puts the raw bytes that represent the Text object in the executable?

If it does work like that, that's not a big deal. But let's say I have:

f :: Text -> Text -> Text
f x y = Text.concat ["Hello", x, "World", y]

This is desugared like so:

f :: Text -> Text -> Text
f x y = Text.concat [fromString "Hello", x, fromString "World", y]

Would there be a [Char] -> Text conversion from ['H', 'e', 'l', 'l', 'o']
-> Text "Hello" everytime 'f' is called, or is 'f' rewritten something like
this:

f :: Text -> Text -> Text
f x y = Text.concat [hello, x, world, y]

hello :: Text
hello = fromString "Hello"

world :: Text
world = fromString "World"

So the [Char] -> Text conversion is only done once and memomised?

I guess this brings up the more general question, if I have this:

f x = h x (g (42 :: Int))

Can I rely on GHC rewriting it like this?

f x = h x y
y = g (42 :: Int)

Or is this a transformation I should do explicitly if I want to rely on it?

(I've added ":: Int" because I guess it can't do this when there's
polymorphism sneaking in)

Thanks,
Clinton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/glasgow-haskell-users/attachments/20220725/d5f4502d/attachment.html>


More information about the Glasgow-haskell-users mailing list