[Haskell-cafe] with -XOverloadedStrings, when is the implied fromString floated out?

Kai-Oliver Prott kai.prott at hotmail.de
Tue Dec 7 13:16:57 UTC 2021


Hey

By default, GHCi uses bytecode, which interacts badly with some 
optimizations according to the GHC manual.
However, you can force GHCi to use object-code via `-fobject-code`. 
There are some small disadvantages, but at least it works.

{-# language OverloadedStrings #-}
{-# OPTIONS_GHC -fobject-code -O1 #-}
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]

ghci> main
[**fromString**
Foo,Foo]

- Kai

On 07.12.21 13:16, Johannes Waldmann wrote:
> 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.
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list