Size argument supplied to all three functions unfoldrN, unfoldrNM and fromListN serves as hint for upper bound for the length of a vector. The problem is that regardless of how big a vector would have been, the supplied upper bound memory is allocated, even if it too big, which can result in an application being killed with an asynchronous exception HeapOverflow
fromListN.hs:
importqualifiedData.Vector.PrimitiveasV
main =dolet xs = [1, 2, 3, 4, 5] :: [Int]
print$V.fromListN (maxBound`div`8) xs
$ ghc fromListN.hs && ./fromListN
[1 of 1] Compiling Main ( fromListN.hs, fromListN.o )
fromListN: Out of memory
unfoldrN.hs
importqualifiedData.Vector.PrimitiveasV
main =print (V.unfoldrN (maxBound`div`8) (constNothing) ()::V.VectorInt)
$ ghc unfoldrN.hs && ./unfoldrN
[1 of 1] Compiling Main ( unfoldrN.hs, unfoldrN.o )
Linking unfoldrN ...
unfoldrN: Out of memory
unfoldrNM.hs
importControl.ExceptionimportqualifiedData.Vector.PrimitiveasV
main =do
eRes <- try (V.unfoldrNM (maxBound`div`8) (const$pureNothing) ()::IO (V.VectorInt))
print (eRes ::EitherSomeException (V.VectorInt))
$ stack exec -- ghc unfoldrNM.hs -O2 && ./unfoldrNM
[1 of 1] Compiling Main ( unfoldrNM.hs, unfoldrNM.o )
Linking unfoldrNM ...
Left heap overflow
Size argument supplied to all three functions
unfoldrN,unfoldrNMandfromListNserves as hint for upper bound for the length of a vector. The problem is that regardless of how big a vector would have been, the supplied upper bound memory is allocated, even if it too big, which can result in an application being killed with an asynchronous exceptionHeapOverflowfromListN.hs:$ ghc fromListN.hs && ./fromListN [1 of 1] Compiling Main ( fromListN.hs, fromListN.o ) fromListN: Out of memoryunfoldrN.hs$ ghc unfoldrN.hs && ./unfoldrN [1 of 1] Compiling Main ( unfoldrN.hs, unfoldrN.o ) Linking unfoldrN ... unfoldrN: Out of memoryunfoldrNM.hs