[Haskell-cafe] Binary parser combinators and pretty printing

Bulat Ziganshin bulatz at HotPOP.com
Thu Sep 15 13:53:30 EDT 2005

Hello Einar,

Tuesday, September 13, 2005, 7:03:00 PM, you wrote:

EK> data Packet = Packet Word32 Word32 Word32 [FastString]

well. you can see my own BinaryStream package at http://freearc.narod.ru

class BinaryData a where
  read :: ...
  write :: ...

instance BinaryData Word32 where
  read = ...
  write = ...

instance BinaryData FastString where
  read = ...
  write = ...

instance (BinaryData a, BinaryData b, BinaryData c, BinaryData d) => BinaryData (a,b,c,d) where
  read = ...
  write = ...

instance (BinaryData a) => BinaryData [a] where
  read = ...
  write = ...

EK> 1) Simple monadic interface

EK> getPacket = do mid <- getWord32BE
EK>                sid <- getWord32BE
EK>                rid <- getWord32BE
EK>                nmsg<- getWord32BE
EK>                vars<- replicateM (fromIntegral nmsg) (getWord32BE >>= getBytes)
EK>                return $ Packet mid sid rid nmsg vars

turns into:

  (a,b,c,d) <- read
  return $ Packet a b c d

EK> Maybe even the tuple could be eliminated by using a little of TH.

it may be eliminated even without TH! :+: and :*: should work,
although i don't tried this

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list