[Haskell-cafe] with -XOverloadedStrings, when is the implied fromString floated out?
Johannes Waldmann
johannes.waldmann at htwk-leipzig.de
Tue Dec 7 12:16:19 UTC 2021
Dear Cafe,
with OverloadedStrings, the compiler inserts `fromString` applications
at String literals. I was wondering - are these floated out?
{-# language OverloadedStrings #-}
import Data.String
import Debug.Trace
data Foo = Foo deriving Show
instance IsString Foo where
fromString "Foo" = trace "**fromString**" Foo
main =
let { f Foo = "Foo" :: Foo; g _ = "Foo" :: Foo }
in print $ map (f . g) [0..1]
With ghc -O1, I get
**fromString**
[Foo,Foo]
That's good - not just floated out, but also interned
(? - both "Foo" are really the same, just one call)
With ghc -00, and in ghci, I get
**fromString**
**fromString**
**fromString**
**fromString**
[Foo,Foo]
no floating here. Is there a way to get this floated in ghci?
The background of my question is that overloaded strings,
and ghci, are heavily used in Tidal Cycles
https://hackage.haskell.org/package/tidal
I guess it's mostly fine, because it's rare (in live
coding performance) that application code contains function definitions.
- J.
More information about the Haskell-Cafe
mailing list