Function return type not applicable

I would be very grateful if someone sick enough would explain the situation to me below. It seems to me that Haskell was ready to do some integer type coercion when returning a value from a function. On the other hand, I read that Haskell never converts a type implicitly.

If I type GHCi:

> import Data.Word
> let t :: (Integer, Word32); 
      t = let { y = fromIntegral (-1)     -- line breaks added for readability
              ; y' :: Integer 
              ; y' = fromIntegral y } in (y', y)

GHCi later informs me that t = (-1,4294967295). But if I restrict the local type yspecifically for Word32:

> let t :: (Integer, Word32); 
      t = let { y :: Word32
              ; y = fromIntegral (-1)     -- line breaks added for readability
              ; y' :: Integer
              ; y' = fromIntegral y } in (y', y)

GHCi will tell me that t = (4294967295,4294967295).

I thought that if the type is tindicated as (Integer, Word32), GHCi will conclude that y' :: Integerand y :: Word32, since the result of the function (y', y). Then a type definition y :: Word32would be completely unnecessary.

, "" Integral - . Int → Word32. Just 1 1 Nothing -1.

SO .

+4
2

, t (Integer, Word32), GHCi , y' :: Integer y :: Word32, (y', y).

, y' y. , . 1:

x = -1

y :: Integer
y = x

y' :: Word32
y' = x

x? Integer, Word32. :

x :: Num a => a
x = fromInteger (-1 :: Integer)

Num a => a Integer y, Word32 y'. , x, x , .


, - :

t = let y = fromIntegral (-1)
        y' = (fromIntegral y) :: Integer
    in (y', y) :: (Integer, Word32)

t = ( (fromIntegral (fromIntegral (-1))) :: Integer -- nothing says Word32 here
    , fromIntegral (-1)
    ) :: (Integer, Word32)

t = let y = (fromIntegral (-1)) :: Word32
        y' = (fromIntegral y) :: Integer
     in (y', y) :: (Integer, Word32)

t = ( (fromIntegral ( (fromIntegral (-1)) :: Word32 )) :: Integer
    , (fromIntegral (-1)) :: Word32
    ) :: (Integer, Word32)

1: , . - , x ( )?

+7

-X[No]MonomorphismRestriction. -XNoMonomorphismRestriction GHCi, GHCi y :: Num a => a. fromIntegral y Integer ( Num), (y', y) Word32. y, y Word32 .

:set -XMonomorphismRestriction, .

+3

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


All Articles