Re: [Haskell-cafe] Binary parser combinators and pretty printing

2005-09-15 Thread Einar Karttunen
On 13.09 23:31, Tomasz Zielonka wrote:
 How about all these points together?:
 
 a) Simple monadic interface

I think I already have this - minus packaging and documentation.

 b) Using better combinators

This is lacking.

 c) Using TH to generate code for the simple cases

I have TH for generating code, but that is not yet general 
purpose (the code comes from SerTH).

 d) Using type-classes

As most real-world protocols will need customization I cannot 
see much improvement here. Keeping the types of the serialized
data explicit makes sense. Otherwise changing an innocent Haskell
data declaration would cause on-wire data mismatch rather than
compile-time type errors.

 I've played with such frameworks a couple of times and I feel it's time
 to make a library useful for others. If you're interested, we could
 cooperate.
 

I would be interested in cooperation and getting an usefull library released. 
Currently my parsers just use [FastString] (thus support lazy IO), peek and 
poke.

- Einar Karttunen
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Binary parser combinators and pretty printing

2005-09-15 Thread Bulat Ziganshin
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
EKsid - getWord32BE
EKrid - getWord32BE
EKnmsg- getWord32BE
EKvars- replicateM (fromIntegral nmsg) (getWord32BE = 
getBytes)
EKreturn $ 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,
 Bulatmailto:[EMAIL PROTECTED]



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Binary parser combinators and pretty printing

2005-09-15 Thread Einar Karttunen
On 15.09 21:53, Bulat Ziganshin 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 :: ...

I don't think this is a very good solution. Keeping the on-wire datatypes 
explicit makes sense to me. Also things like endianess will need to be 
taken into account. If the encoding is derived automatically then 
changing the Haskell datatype will change the on-wire representation.
This is not wanted when interfacing with external protocols.

For typeclasses I would rather have:
getWord32BE :: Num a = MyMonad a
than
get :: MyClass a = MyMonad a

Note the difference between the Haskell type determining the on-wire 
type and it being explicit. I already have working TH code for the 
case where I want to derive automatic binary serialization for 
Haskell datatypes (SerTH).

 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

I don't know how generics work in newer versions of GHC, but 
it may be worth investigating.

- Einar Karttunen

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Binary parser combinators and pretty printing

2005-09-13 Thread Malcolm Wallace
Einar Karttunen ekarttun@cs.helsinki.fi writes:

 I am trying to figure out the best interface to binary parser
 and pretty printing combinators for network protocols. 
 
 2) Using better combinators
 
 packet = w32be  w32be  w32be  lengthPrefixList w32be (lengthPrefixList 
 w32be bytes)
 Has anyone used combinators like this before and how did it work?

Yes, the nhc98 Binary library has a  combinator, in very much
the style you outline.  It is only used in pure code, but it permits
some very concise descriptions of the binary layout.  The library is
described here:

ftp://ftp.cs.york.ac.uk/pub/malcolm/ismm98.html

but unfortunately that paper has only the tiniest examples of the
usage of .  However, in my experience the style worked well and
was pleasant to use.

Regards,
Malcolm
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe