Haskell type enforcement

I am trying to bow my head to Haskell type coercion. Value, when you can pass a value to a function without casting and how it works. Here is a specific example, but I'm looking for a more general explanation that I can use to try to understand what is happening:

Prelude> 3 * 20 / 4
15.0
Prelude> let c = 20
Prelude> :t c
c :: Integer
Prelude> 3 * c / 4

<interactive>:94:7:
    No instance for (Fractional Integer)
      arising from a use of `/'
    Possible fix: add an instance declaration for (Fractional Integer)
    In the expression: 3 * c / 4
    In an equation for `it': it = 3 * c / 4

Type (/) - Fractional a => a → a → a. So, I assume that when I do "3 * 20" using literals, Haskell somehow assumes that the result of this expression is Fractional. However, when a variable is used, its type is predefined as an assignment-based Integer.

- . - ? , , , int/float. , ? ?

, . , .

+2
5

, .

. Haskell :

Prelude> :t 3
3 :: Num a => a

(*) Num

Prelude> :t (*)
(*) :: Num a => a -> a -> a

:

Prelude> :t 3 * 20
3 * 20 :: Num a => a

, , Int, Integer, Float, Double, Rational .. . , Fractional Num, , Fractional:

Prelude> :t 3 * 20 / 4
3 * 20 / 4 :: Fractional a => a

c - Integer. , let-binding GHCi , - . : , , , . Integer.

c Integer, Integer:

Prelude> :t 3 * c
3 * c :: Integer

Integer Fractional.

.

  • , .

      Prelude> let c :: Num a => a; c = 20
      Prelude> :t c
      c :: Num a => a
    
  • fromIntegral :

      Prelude> :t fromIntegral
      fromIntegral :: (Integral a, Num b) => a -> b
      Prelude> let c = 20
      Prelude> :t c
      c :: Integer
      Prelude> :t fromIntegral c
      fromIntegral c :: Num b => b
      Prelude> 3 * fromIntegral c / 4
      15.0
    
+11

Haskell , . , , .

, " ", int/float; (.. - Int Float ), .

, :

main = do
    let c = 20
    let it = 3 * c / 4
    print it

, 15.0. .0, GHC , c , - .

c , / , . ​​ div ( x `div` y). , , ? , , , , , , / .

, , , - . GHCi let c = 20 , 3 * c / 4. , , 20 Int, Integer, Float, Double, Rational ..

Haskell ; , - , , . , (, - /). , , c a Integer.

, GHCi 3 * c / 4, . c Integer, 3 * c be, Integer /.

, , let, GHC , . , GHCi, ​​, ; let c = 20.0.

, , , , div, /.

+5

Haskell . , , .

, Num typeclass, fromIntegral . 99% , . :

newtype Foo = Foo Integer
    deriving (Show, Eq)
instance Num Foo where
   fromInteger  _  = Foo 0
   negate          = undefined
   abs             = undefined
   (+)             = undefined 
   (-)             = undefined 
   (*)             = undefined 
   signum          = undefined

, GHCi

*> 0 :: Foo
   Foo 0

*> 1 :: Foo
   Foo 0

, , , GHCi . DSL, .

, Double Integer . .

fromInteger. ?

(Num a) => Integer -> a

(+) Doubles, , Num. .

*> 0 :: Double
    0.0

Double -> Integer. , Hoogle

truncate
floor
round
-- etc ...

.

+3

Haskell (, , ). 20, , Num a => a ( . , ) , , ( , ) ( , , Integer, - ). Num, , . (3* fromIntegral c / 4) .

0

(/) - a = > a → a → a.

, div (/). , div

div :: Integral a => a -> a -> a

, .

, Haskell , , , . Haskell - , ( C, ++, Java ..), , , . , -.

, Haskell . Integer, Float, fromInteger.

0

All Articles