Reputation: 115
Currently I'm reading PureScript by example
book and have question about one of exercises:
Chapter 6, exercise 5 at the end of the chapter.
Write a Foldable instance for NonEmpty. Hint: resuse the foldable instance for arrays.
data NonEmpty a = NonEmpty a (Array a)
My initial thought was to implement it like this:
instance foldableNonEmpty :: Foldable (Array (a)) => Foldable (NonEmpty a) where
foldr f b (NonEmpty a ar) = NonEmpty a (foldr f b ar)
foldl f b (NonEmpty a ar) = NonEmpty a (foldl f b ar)
foldMap m (NonEmpty a ar) = NonEmpty a (foldMap m ar)
But this doesn't work and currently I can't figure out why (compiler error can't help me)
Could not match kind
Type -> Type
with kind
Type
while checking the kind of Foldable (Array a) => Foldable (NonEmpty a)
in value declaration foldableNonEmpty
Can someone point out in which direction should I dig to find an answer?
Upvotes: 1
Views: 234
Reputation: 10487
The error message is saying that Foldable
expected to receive a type constructor (Type -> Type
), but you are giving it a concrete type (Type
). For example: Maybe
and Array
are type constructors; Maybe a
and Array a
are concrete types. In your case, NonEmpty a
is the concrete type. I believe now you get the idea how to fix it.
Moreover, your implementation is also wrong. The type of foldr
is
foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
You should return a value with type b
, but you are returning a value with f a
, which is NonEmpty a
in your case.
I left my answer in this link in case you still can not figure it out.
Upvotes: 2