While learning more about Haskell's type system, I encountered this toy problem. When I compile the following code:
data SingleData a = SingleData a Int String -- etc.
data DataFail a = DataFail [SingleData a]
instance Functor DataFail where
fmap f (DataFail xs) = DataFail (map f xs)
I get the dreaded "Occurs check: cannot construct the infinite type: b = SingleData b" message on the definition of fmap. Strangely, the same definition of fmap will work if it is outside of the instance declaration:
fmapWorks f (DataFail xs) = DataFail (map f xs)
Even stranger, implementing Monoid compiles and works just fine:
instance Monoid (DataFail a) where
mempty = DataFail []
mappend (DataFail xs) (DataFail ys) = DataFail (xs ++ ys)
I believe that this is somehow a result of using SingleData inside of DataFail, because this works just fine as well:
data DataSuccess a = DataSuccess [a]
instance Functor DataSuccess where
fmap f (DataSuccess xs) = DataSuccess (map f xs)
Why is Haskell complaining about DataFail's fmap function, and what can I do to fix it?
fmapWorks
you'll see that it's not the type needed forfmap
. – augustss