Why is MVector not Foldable?

Viewed 85

May I understand why the MVector type(from vector package) doesn't instance the Foldable type class? I suppose people would often need to iterate over vectors. I can freeze it to Data.Vector and then iterate. But I hate transforming these things, as if we don't do it enough with String, Text, Bytestrings etc.

In my case I'd like to print each element.

1 Answers

Reading from a mutable vector requires doing effects in the associated monad -- IO or ST s. But Foldable requires you to be able to produce a pure answer; if there were an instance, we would have to be able to at least implement

foldr :: (a -> b -> b) -> b -> MVector s a -> b

even when the caller chooses a b that doesn't mention IO or ST. Whoops!

Related