[Template-haskell] TH question

Tim Newsham newsham at lava.net
Thu Jan 19 13:04:23 EST 2006


> imho, it's a sort of problem which easier to solve without TH - just
> generate text file with all code you need

Writing this as a text preprocessor is pretty easy, and I have
written a prototype generator using python.  However, I was hoping
to get some tighter integration with the language.

Here's a good example of why thats desirable.  It should be possible
to generate marshallers for records with any field types, so long as
every field is also of ByteContainer type.  To generate the marshaller,
the size of each field must be known.  ByteContainer's have a method
for returning their size (a constant).  If a text preprocessor is used
to generate marshallers, then it must be smart enough to cross reference
with any previous generation that is used as a record field in order
to compute the size.

Which brings me to a question I have.... I have most of the code for
a TH generator done.  The only bit I haven't done is something that
returns the proper size of each field.  I have a record name and a
field name (and a field type name), all as strings.  I need some
way to get the result "containerLength (fieldname x)" for a record
instance "x".  How can I evaluate such a call at template compute
time (ie. compile time)?

> why you don't use some serialization library?

Because I haven't yet seen one thats any good :(

My goals are to have a serialization library that allows for the
specification of big and little endian fields, and that specifies
the layout of each type without specifying a process.  It is
important that it is specified in a way that allows for lazy
evaluation.  What I have so far is a method that lets me declare
an item as a ByteContainer, and convert data back and forth
between different ByteContainers (ie. an array of bytes could be
a byte container and an IP header could be another, making it
possible to convert IP headers into arrays and vice versa).  Because
lazy evaluation is allowed, it should be possible to extract
parts of a component even if there is not enough room for an
entire component (ie. I can convert a 4-byte array to an IP
record, and extract the first three fields of the record without
causing an exception).  Here's what this looks like so far:

     http://lava.net/~newsham/x/Pkts5.lhs.txt

If anyone has comments or suggestions on this, I'd love to hear them.
(Though perhaps offtopic for this list, feel free to mail me directly).

> Bulat                            mailto:bulatz at HotPOP.com

Tim Newsham
http://www.lava.net/~newsham/


More information about the template-haskell mailing list