[Haskell-beginners] Re: problem with System.Directory.Tree

Stephen Tetley stephen.tetley at gmail.com
Mon Jun 28 03:53:04 EDT 2010


Hi Anand

MD5Digest is an abstract type (the constructor is not exported from
its module) but it is an instance of Binary.
ClockTime (from System.Time) is not an instance of Binary but it does
export its constructor.
Neither are instances of Data.Data.

So I would hand-craft an instance of Binary for the Props datatype
rather than try to first make them instances of Data.

The code will be something like this, as I don't have MD5 installed it
is unchecked:

class Binary Props where
  put (Prop md5 tim name) =
      do { putWord8 0     -- number each constructor
         ; put md5        -- MD5Digest has a Binary instance
         ; putTOD tim
         ; put name
         }

  put Blank               =
      do { putWord8 1 }   -- number each constructor

  get = do { typ <- getWord8 -- get the constructor tag...
           ; case typ of
              0 -> getProp
              1 -> return Blank
           }

getProp :: Get Props
getProp = do
    { md5 <- get
    ; tim <- getTOD
    ; name <- get
    ; return (Prop md5 tim name)
    }


-- ClockTime doesn't have a binary instance
-- but a it has a single constructor
--
-- > TOD Integer Integer -
--
-- and Integer has a Binary instance, so I
-- would make auxillaris for put and get:

putTOD :: ClockTime -> Put ()
putTOD (TOD a b) = do { put a ; put b }

getTOD :: Get ClockTime
getTOD = do { a <- get; b <- get; return (TOD a b) }


More information about the Beginners mailing list