Difference in Haskell Function Argument Type Declaration

I am new to Haskell. I'm just confused by the difference between argument type declarations here:

someFunction :: (Integral a) => a -> Bool -> a 

This will compile.

someFunction :: (Integral a, Bool b) => a -> b -> a 

However, given this, the compiler will complain:

• Expecting one fewer arguments to ‘Bool’
  Expected kind ‘* -> Constraint’, but ‘Bool’ has kind ‘*’
• In the type signature:
    someFunction :: (Integral a, Bool b) => a -> b -> a

What is the difference between the two ads? I tried to do a Google search, but there seems to be no direct answer to this question.

Thank!


Thanks for the quick answers to the guys. And they lead me to the following question: what are the thoughts associated with limiting arguments in different places of the declaration? (vs C-like languages ​​where constraints, abstract or specific, are mixed together in a declaration)

+4
source share
2 answers

Integralnot a type: it is a class. It describes a type restriction.

So signature:

someFunction :: (Integral a) => a -> Bool -> a 

, " a Integral, ".

(Integral a, Bool b) => a -> b -> a , Bool - , .

Integral? , , - - - , Int:

instance Integral Int where
    -- (declarations here)

: Sizeable:

class Sizeable s where
    size :: s -> Int

, , size. , :

instance Sizeable [a] where
    size = length

instance Sizeable (a,b) where
    size _ = 2

instance Sizeable (a,b,c) where
    size _ = 3

, :

sizePlusOne s = (size s) + 1

:

sizePlusOne :: (Sizeable s) => s -> Int
+4

=> . Haskell . , Java ..

, Haskell Eq:

class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool

Int "" Num typeclass, instance of Num ** , class. , , () Eq, :

data Peano = Zero | Succ Peano

Eq:

instance Eq Peano where
    Zero == Zero = True
    s(x) == s(y) = x == y
    _ == _ = False

    Zero /= Zero = False
    s(x) /= s(y) = x /= y
    _ /= _ = True

, Peano ==, Peano Eq. , - :

someFunction :: Eq a => a -> a -> a -> Bool
someFunction x y z = x == y && y == z

a Peano, , Eq Peano ().

, (, == /=), , .

Bool, , , . Bool b. , (.. Eq Peano), Bool - ).

+2

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


All Articles