James Strieter
James Strieter

Reputation: 819

What Does Kind Constraint mean in Haskell typeclass instance?

When type variables are constrained by classes in Haskell, I understand what that means.

For example, in a function declaration,

myFunction :: Foldable f => f a -> b

means that f is a type with an instance of Foldable which wraps some other type a.

But what does it mean when a type variable is constrained by a kind?

Consider for instance this definition for Foldable:

class Foldable (t :: * -> *) where

Also, does the fact that 1 example is from a function definition and the other example is from a class definition make any difference in what the constraint means?

Upvotes: 0

Views: 72

Answers (1)

chi
chi

Reputation: 116139

t :: * -> * is not a constraint, it is a kind annotation. In this case, it is used to remark that Foldable can take as arguments type constructors such as Maybe, Identity, [], or even partially applied ones like Either Bool and (,) String. By contrast Foldable Int and Foldable [Bool] would be kind errors.

t :: * -> * can be read as "t maps types to types".

The point is, when we have Foldable f we then use f as in f a, applying f to one argument. If we allow f = Maybe we get Maybe a which makes sense. If we allowed f = Int, we would get Int a which is meaningless.

Upvotes: 2

Related Questions