[Template-haskell] splicing types
Claus Reinke
claus.reinke at talk21.com
Tue Jul 1 03:40:12 EDT 2008
>> data Bar {
>> val1 :: String,
>> val2 :: Int -- omit this in serialization
>> }
..
> which could be generalized to a sort of 'annotation type', e.g.
>
>> type Ann a b = a
>> type Omitted = () -- a particular annotation
> ...
>> val2 :: Ann Int Omitted
>
> you could have multiple annotations, e.g.
>> val2 :: Ann Int (Omitted,Ann1,Ann2) -- an Int with three annotations
try googling for "phantom types"?
..
>> type Omitted a = ()
> ...
>> val2 :: Ann Int (Omitted 1000)
if you bake the default into the type, you can have only one
per type per program, so how about using a class to map
from type to default value?
class Default a where default :: a
valn :: Default a => a
instance Default Bar where default = Bar undefined 1000
or
instance Default Int where default = 1000
then you could omit types that have defaults, or use/infer an
omit annotation for fields that the default for the enclosing
type can fill in. Parsing a slightly extended data syntax might
be easier than overloading the type system, but maybe that
is just me?-)
Claus
ps if you absolutely want to go for more type-level
programming, Oleg has some notes on number-
parameterized types
http://okmij.org/ftp/Haskell/number-parameterized-types.html
More information about the template-haskell
mailing list