Anatoly Yakovenko | 16 Apr 21:07 2013
Picon

Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

-- ok, something in deriving NFData using Generics in a type that has a Vector in it.


{-# LANGUAGE DeriveGeneric #-}
import Control.DeepSeq
import System.IO
import GHC.Generics (Generic)
import qualified Data.Vector as V
import qualified Data.ByteString.Lazy.Char8 as BL

scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]
scanl' f q ls =  q : (case ls of
                        []   -> []
                        x:xs -> let q' = f q x
                                in q' `deepseq` scanl' f q' xs)

-- this runs without blowing up
-- main = print $ last $ scanl' (+) (0::Int) [0..]

data Simple = Simple (V.Vector Double)
            deriving (Show, Generic)

instance NFData Simple

--this blows up
main = do
   let initial = Simple $ V.fromList (take 100 $ repeat 0)
       sumvs (Simple a) (Simple b) = Simple $ V.zipWith (+) a b
   print $ last $ scanl' sumvs initial $ repeat $ initial


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
José Pedro Magalhães | 16 Apr 21:15 2013
Picon

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

What is the error that you get?


Cheers,
Pedro

On Tue, Apr 16, 2013 at 8:07 PM, Anatoly Yakovenko <aeyakovenko <at> gmail.com> wrote:
-- ok, something in deriving NFData using Generics in a type that has a Vector in it.


{-# LANGUAGE DeriveGeneric #-}
import Control.DeepSeq
import System.IO
import GHC.Generics (Generic)
import qualified Data.Vector as V
import qualified Data.ByteString.Lazy.Char8 as BL

scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]
scanl' f q ls =  q : (case ls of
                        []   -> []
                        x:xs -> let q' = f q x
                                in q' `deepseq` scanl' f q' xs)

-- this runs without blowing up
-- main = print $ last $ scanl' (+) (0::Int) [0..]

data Simple = Simple (V.Vector Double)
            deriving (Show, Generic)

instance NFData Simple

--this blows up
main = do
   let initial = Simple $ V.fromList (take 100 $ repeat 0)
       sumvs (Simple a) (Simple b) = Simple $ V.zipWith (+) a b
   print $ last $ scanl' sumvs initial $ repeat $ initial



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
anatoly yakovenko | 16 Apr 21:36 2013
Picon

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

This compiles but the process runs out of memory, so it seams that NFData derivation isn't doing its job.

On Apr 16, 2013, at 12:15 PM, José Pedro Magalhães <jpm <at> cs.uu.nl> wrote:

> What is the error that you get?
> 
> 
> Cheers,
> Pedro
> 
> On Tue, Apr 16, 2013 at 8:07 PM, Anatoly Yakovenko <aeyakovenko <at> gmail.com> wrote:
> -- ok, something in deriving NFData using Generics in a type that has a Vector in it.
> 
> 
> {-# LANGUAGE DeriveGeneric #-}
> import Control.DeepSeq
> import System.IO
> import GHC.Generics (Generic)
> import qualified Data.Vector as V
> import qualified Data.ByteString.Lazy.Char8 as BL
> 
> scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]
> scanl' f q ls =  q : (case ls of
>                         []   -> []
>                         x:xs -> let q' = f q x
>                                 in q' `deepseq` scanl' f q' xs)
> 
> -- this runs without blowing up
> -- main = print $ last $ scanl' (+) (0::Int) [0..]
> 
> data Simple = Simple (V.Vector Double)
>             deriving (Show, Generic)
> 
> instance NFData Simple
> 
> --this blows up
> main = do
>    let initial = Simple $ V.fromList (take 100 $ repeat 0)
>        sumvs (Simple a) (Simple b) = Simple $ V.zipWith (+) a b
>    print $ last $ scanl' sumvs initial $ repeat $ initial
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 
Anatoly Yakovenko | 17 Apr 02:17 2013
Picon

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

-- + Roman,

-- hey Roman,

-- seems like i cant use deepseq or Generic derive of NFData on data types containing vectors.  The following code tries to use deepseq to force evaluation of a type containing vectors, but when the code is running it seems to not work as expected (blows up in memory).  any ideas?


{-# LANGUAGE DeriveGeneric #-}
import Control.DeepSeq
import System.IO
import GHC.Generics (Generic)
import qualified Data.Vector as V
import qualified Data.ByteString.Lazy.Char8 as BL

scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]
scanl' f q ls =  q : (case ls of
                        []   -> []
                        x:xs -> let q' = f q x
                                in q' `deepseq` scanl' f q' xs)

-- this runs without blowing up
-- main = print $ last $ scanl' (+) (0::Int) [0..]

data Simple = Simple (V.Vector Double)
            deriving (Show, Generic)

instance NFData Simple

--this blows up
main = do
   let initial = Simple $ V.fromList (take 100 $ repeat 0)
       sumvs (Simple a) (Simple b) = Simple $ V.zipWith (+) a b
   print $ last $ scanl' sumvs initial $ repeat $ initial




On Tue, Apr 16, 2013 at 12:36 PM, anatoly yakovenko <aeyakovenko <at> gmail.com> wrote:
This compiles but the process runs out of memory, so it seams that NFData derivation isn't doing its job.


On Apr 16, 2013, at 12:15 PM, José Pedro Magalhães <jpm <at> cs.uu.nl> wrote:

> What is the error that you get?
>
>
> Cheers,
> Pedro
>
> On Tue, Apr 16, 2013 at 8:07 PM, Anatoly Yakovenko <aeyakovenko <at> gmail.com> wrote:
> -- ok, something in deriving NFData using Generics in a type that has a Vector in it.
>
>
> {-# LANGUAGE DeriveGeneric #-}
> import Control.DeepSeq
> import System.IO
> import GHC.Generics (Generic)
> import qualified Data.Vector as V
> import qualified Data.ByteString.Lazy.Char8 as BL
>
> scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]
> scanl' f q ls =  q : (case ls of
>                         []   -> []
>                         x:xs -> let q' = f q x
>                                 in q' `deepseq` scanl' f q' xs)
>
> -- this runs without blowing up
> -- main = print $ last $ scanl' (+) (0::Int) [0..]
>
> data Simple = Simple (V.Vector Double)
>             deriving (Show, Generic)
>
> instance NFData Simple
>
> --this blows up
> main = do
>    let initial = Simple $ V.fromList (take 100 $ repeat 0)
>        sumvs (Simple a) (Simple b) = Simple $ V.zipWith (+) a b
>    print $ last $ scanl' sumvs initial $ repeat $ initial
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Andres Löh | 17 Apr 07:51 2013

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

Hi Anatoly.

I don't think that the normal deepseq package currently provides
generic deriving at all. This doesn't have anything to do with vector.
There's a default implementation for rnf that defines it to be seq,
which is not what you want in this case, of course. There are
additional packages that implement such functionality, though.

By using deepseq-generics, you can import Control.DeepSeq.Generics and
then define

> instance NFData Simple where rnf = genericRnf

Cheers,
  Andres

--

-- 
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
anatoly yakovenko | 17 Apr 16:51 2013
Picon

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

Thanks,  So now i at least get a compiler error

    No instance for (NFData (V.Vector Double))

So how would one define NFData instance for vector?

On Apr 16, 2013, at 10:51 PM, Andres Löh <andres <at> well-typed.com> wrote:

>> NFData Simple where rnf = genericRnf
anatoly yakovenko | 17 Apr 16:56 2013
Picon

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

so this one works

instance NFData (V.Vector a) where rnf a = force a `seq` ()

any reason why something like this isn't part of the vector library?

Thanks,
Anatoly

On Apr 17, 2013, at 7:51 AM, anatoly yakovenko <aeyakovenko <at> gmail.com> wrote:

> Thanks,  So now i at least get a compiler error
> 
>    No instance for (NFData (V.Vector Double))
> 
> So how would one define NFData instance for vector?
> 
> 
> On Apr 16, 2013, at 10:51 PM, Andres Löh <andres <at> well-typed.com> wrote:
> 
>>> NFData Simple where rnf = genericRnf
> 
Andres Löh | 17 Apr 17:05 2013

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)

Hi again.

> instance NFData (V.Vector a) where rnf a = force a `seq` ()
>
>
> any reason why something like this isn't part of the vector library?

Quoting from the Changelog at http://hackage.haskell.org/package/vector :

"
Changes in version 0.10

   *  NFData instances
"

So it's there, and even in the current Haskell Platform.

Cheers,
  Andres

--

-- 
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
anatoly yakovenko | 17 Apr 17:12 2013
Picon

Re: Derving NFData via Generics from a type that has a vector doesn't work. (was trying to understand out of memory exceptions)


On Apr 17, 2013, at 8:05 AM, Andres Löh <andres <at> well-typed.com> wrote:

> Changes in version 0.10
> 
>   *  NFData instances
> "

thanks :).

Anatoly

Gmane