Here's a short transcript from GHCi:
Prelude> :t read
read :: Read a => String -> a
Prelude> :t show
show :: Show a => a -> String
Prelude> :t show.read
show.read :: String -> String
Prelude> (show.read) "whales"
"*** Exception: Prelude.read: no parse
When I compose show
and read
I can only assume that GHC chose some arbitrary type which is both Read
able and Show
able to be the "intermediate" type.
How did it choose this type, and is there any way for me to find out what it is?
The GHCi defaulting rules say that the chosen type is ()
. which is the default type that is chosen if a Show
instance is demanded. GHCi will choose ()
for general constraints, Integer
for numeric or integral constraints, and Double
for fractional/other real constraints. This isn't due to some Haskell intrinsic; it's just how GHCi was implemented so that it can be used easily as a calculator.
If you had actually entered the code in a file and compiled it, the more strict GHC rules would have applied, and you would have gotten an error saying that the intermediate type cannot be resolved.
You can of course instruct GHC to use a different type by giving one of the functions a type, e.g.:
show . (read :: String -> Int)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With