J'essaie de terminer l'exercice 5 de la section 6.7 du livre Pure Phil de Phil Freeman. L'exercice veut que j'écrive une instance Foldable
pour le type suivant. J'ai écrit cette instance en implémentant foldMap
. J'ai écrit cette instance en implémentant .Comment utiliser foldrDefault et foldlDefault avec mon foldMap?
instance foldableNonEmpty :: Foldable a => Foldable NonEmpty where
foldMap :: forall a m. Monoid m => (a -> m) -> NonEmpty a -> m
foldMap f (NonEmpty x xs) = (f x) <> (foldMap f xs)
foldr :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
foldr f = foldrDefault f
foldl f = foldlDefault f
Mais cela génère l'erreur suivante.
Error found:
in module Data.Hashable
at src/Data/Hashable.purs line 110, column 11 - line 110, column 23
No type class instance was found for
Data.Foldable.Foldable t2
The instance head contains unknown type variables. Consider adding a type annotation.
while checking that type forall f a b. Foldable f => (a -> b -> b) -> b -> f a -> b
is at least as general as type (a0 -> b1 -> b1) -> b1 -> NonEmpty a0 -> b1
while checking that expression foldrDefault
has type (a0 -> b1 -> b1) -> b1 -> NonEmpty a0 -> b1
in value declaration foldableNonEmpty
where b1 is a rigid type variable
bound at line 110, column 11 - line 110, column 23
a0 is a rigid type variable
bound at line 110, column 11 - line 110, column 23
t2 is an unknown type
Je pense que je reçois l'erreur car foldr = foldrDefault
implique au compilateur que NonEmpty
est déjà pliable quand c'est ce que je suis en train de cas, mais je ne sais pas comment utiliser les implémentations de pliage par défaut. Toute aide serait grandement appréciée.