[Template-haskell] Stuck... is it even possible?

Bulat Ziganshin bulatz at HotPOP.com
Fri Jan 20 15:46:15 EST 2006


Hello Tim,

Friday, January 20, 2006, 8:35:03 PM, you wrote:

TN> Thank you.  This makes a lot of sense.  My only problem with this is
TN> the declarations of the base types in the generator:
TN>     flen t | t == 'Word8 = return 1
TN> This precludes someone from adding other base types without modifying
TN> the generator.

as i initially understand, you are planned to use this lib only in
your own app?

anyway, this depends on how other things will be arranged. adding new
type require some other support besides of modifying `flen`. how your
imagine the scenario of this process?

on the other side, sizes for enumerations can be calculated. number of
other simple types are limited. all other types are complex, i.e.
their sizes is a function of sized of types they contain

on the third side :)  you can try to rewrite instantiation so that it
will not need to know these sizes at compile-time:

>data IP = IP Word16 Word32 Word64
>
>instance ByteContainer IP
>       containerLength (IP x1 x2 x3) = (containerLength x1)+
>                                       (containerLength x2)+
>                                       (containerLength x3)
>
>       makeContainer b = IP
>               (makeContainer (Slice b 0))
>               (makeContainer (Slice b offset1))
>               (makeContainer (Slice b offset2))
>           where offset1 = containerLength newWord16
>                 offset2 = offset1+containerLength newWord32
>
>       getByteAt (IP x0 x1 x2) n
>           | n >= offset2  =  getByteAt x2 (n-offset2)
>           | n >= offset1  =  getByteAt x1 (n-offset1)
>           | otherwise     =  getByteAt x0 n
>
>           where offset1 = containerLength newWord16
>                 offset2 = offset1+containerLength newWord32
>

? the only problem is what TH can't generate INLINE pragmas, so this
code can work rather slow


btw, newXX operations may be better to include in the class
definition:

> class ByteContainer b where
>       new :: b
>       .....


-- 
Best regards,
 Bulat                            mailto:bulatz at HotPOP.com





More information about the template-haskell mailing list