How to save a recursive data type using Data.Binary

Data.Binaryfine. There is only one question that I have. Imagine that I have this type of data:

import Data.Binary

data Ref = Ref {
    refName :: String,
    refRefs :: [(String, Ref)]
}

instance Binary Ref where
    put a = put (refName a) >> put (refRefs a)
    get = liftM2 Ref get get

It's easy to see that this is a recursive data type that works because Haskell is lazy. Since Haskell as a language does not use either references or pointers, but presents the data as is, I am not sure how this will be saved. I have a strong indication that this naive reproach will lead to an infinite bytestring ...

So how can you safely save this type?

+5
source share
1 answer

If your data has no loops, everything will be fine. But a loop, for example

r = Ref "a" [("b", r)]

. - , .

+6

All Articles