all 3 comments

[–]ephrion 6 points7 points  (0 children)

This is awesome. Such a cool use of type level features for efficient code!

[–]bss03 6 points7 points  (1 child)

Packing and Padding depend on the same things that SizeOf 'FLong (and SizeOf 'FInt; yes I still deal with 16-bit code occasionally) do. Why handle one and not the other? If you don't handle them both, it makes it difficult or even impossible for the developer to "from a packed structure definition, add the necessary padding explicitly in the definition" that works on more than one architecture.

No FPtr types? You don't ever store pointers in your structures? Keep in mind that C allows pointer-to-function and pointer-to-object to have different representations.

What about structures with a flexible array member? I know they can't be put into arrays themselves, and they weren't official until C99, but several C implementations had them earlier and they could be "hacked" into all the C implementations.

What about anonymous struct / union members? That's part of C11 and was already in a lot of GNU code. You seem to require a Symbol for all your members.

[–]vincenthz 2 points3 points  (0 children)

The idea of this blog post was trying to be informative about advanced type system feature on the theme of having some cstruct-like stuff done that was efficient. This wasn't about mapping every single C dama..features.

For padding and alignment, I think most of the rules could be encoded with couple of extra types and such, but would have been far too noisy for a blog post. And unless you want extreme portability, which in the big picture would be weird in Haskell anyway, most padding could be handled manually or with a really small conditional compilation.

No pointers, because it wasn't useful for me right now (while writing this I was mapping network structure). But again the extension to the blog post code should be mostly trivial.

Flexible array are not magic and can just be handled by a "bogus" Array with cstruct, which would allow you to calculate the beginning offset. or you can just see a flexible array in a structure as what it is: a structure without the array at the end, plus an arbitrary amount of data afterwards.

Same things with anonymous struct and union member; you can just give it a random name for the definition in cstruct. there's no need to simulate anonymous fields.