KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to wrap the Data.Binary.Put monad into another so that later I can ask it questions like "how many bytes it's going to write" or "what is the current position in file". But even very trivial wraps like: data Writer1M a = Writer1M { write :: P.PutM a } or data Writer2M a = Writer2M { write :: (a, P.Put) } create a huge space leak and the program usually crashes (after taking up 4GB of RAM). Here is what I've tried so far: -- This works well and consumes almost no memory. type Writer = P.Put writer :: P.Put -> Writer writer put = put writeToFile :: String -> Writer -> IO () writeToFile path writer = BL.writeFile path (P.runPut writer) -- This one will cause memory leak. data Writer1M a = Writer1M { write :: P.PutM a } instance Monad Writer1M where return a = Writer1M $ return a ma >>= f = Writer1M $ (write ma) >>= \a -> write $ f a type WriterM = Writer1M type Writer = WriterM () writer :: P.Put -> Writer writer put = Writer1M $ put writeToFile :: String -> Writer -> IO () writeToFile path writer = BL.writeFile path (P.runPut $ write writer) -- This one will crash as well with exactly the -- same memory foot print as Writer1M data Writer2M a = Writer2M { write :: (a, P.Put) } instance Monad Writer2M where return a = Writer2M $ (a, return ()) ma >>= f = Writer2M $ (b, p >> p') where (a,p) = write ma (b,p') = write $ f a type WriterM = Writer2M type Writer = WriterM () writer :: P.Put -> Writer writer put = Writer2M $ ((), put) writeToFile :: String -> Writer -> IO () writeToFile path writer = BL.writeFile path (P.runPut $ snd $ write writer) I'm new to Haskell and this makes no sence to me, but the wrapper monads seem very trivial so I'm guessing there is something obvious I'm missing. Thanks for looking. UPDATE: Here is a sample code that demonst
Tags (comma-separated)
Save Edits
Cancel