[Haskell-cafe] when does ghc(i) expand type synonyms?

Johannes Waldmann johannes.waldmann at htwk-leipzig.de
Wed Oct 25 16:37:18 UTC 2023


Dear cafe,

my students were asking:
why are types printed differently here?

ghci> :set +t

ghci> "foo"
"foo"
it :: String

ghci> reverse "foo"
"oof"
it :: [Char]

... and I add

ghci> fst ("foo", 42)
"foo"
it :: String

compiler docs do not really explain the difference
https://downloads.haskell.org/ghc/latest/docs/users_guide/ghci.html#ghci-cmd-:set%20+t
(it says "display the type of .." while technically it is
"display some representation of the type of ..."?)

I found this explanation of a somewhat related issue
https://gitlab.haskell.org/ghc/ghc/-/issues/9183#note_83743


While playing around with this, I noticed:

ghci> let x = "foo" in (x <> reverse x)
"foooof"
it :: String

ghci> let x = "foo" in (x ++ reverse x)
"foooof"
it :: [Char]


Well, it's just optics: when I extract the type programmatically,
the synonym necessarily does get expanded

ghci> import Data.Data
ghci> typeOf (let x = "foo" in (x <> reverse x))
[Char]
it :: TypeRep

- Johannes



More information about the Haskell-Cafe mailing list