What happens when I compose โ€œshowsโ€ and โ€œreadโ€ in Haskell?

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 the GHC has chosen some arbitrary type that both read is capable of and show can be an "intermediate" type.

How did he choose this type, and is there any way to find out what it is?

+4
source share
1 answer

The default rules of GHCi say that the selected type is () . which is the default type that is selected if a Show instance is required. GHCi will select () for general constraints, Integer for numerical or integral constraints, and Double for fractional / other real constraints. This is not due to the fact that some kind of Haskell is internal; this is exactly how GHCi was implemented so that it can be easily used as a calculator.

If you really entered the code into a file and compiled it, the stricter GHC rules would apply and you would get an error stating that the intermediate type cannot be resolved.

Of course, you can instruct the GHC to use a different type by specifying one of the functions of the type, for example:

 show . (read :: String -> Int) 
+13
source

Source: https://habr.com/ru/post/1434958/


All Articles