[Haskell-cafe] DSL for data definition (e.g. compiling Haskell type defs into Google's protocol buffers type defs)
Karel Gardas
karel.gardas at centrum.cz
Tue Oct 4 18:02:34 CEST 2011
Hello,
I'm trying to find out if it's possible to use Haskell data type
definition capability to define types and compile defined types into
other languages, for example into Google's protocol buffers data
definition language. So basically speaking I'm thinking about using
Haskell sub-set as a data-definition DSL together with some functions
which will generate some code based on supplied defined data types. My
idea is:
data Person = Person {
id :: Int
, name :: String
, email :: Maybe String
}
deriving (Show, Data, Typeable)
emit_proto Person 1
where emit_proto is function which will translate Person data type
definition into Google's proto language (the 1 is index from which start
to index type's fields) by traversing data type definition and
translating all its children plus do some header/footer generation etc:
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
I've looked for something like that and found SYB papers which works on
top of data instance (i.e. actual data, not data type). I also found
JSON lib which again works on top of data and not data type. I've tried
to look into Data.Typetable etc, but have not found function which will
print data type's field name and field type name (although JSON lib
seems to use field name for JSON generation so I'll need to investigate
this more). I've tested `typeOf' function and it's quite useful, but its
limitation is that it's not working on ADT name:
data Color = RED|GREEN|BLUE
*Main> typeOf Color
<interactive>:1:8: Not in scope: data constructor `Color'
*Main> typeOf RED
Main.Color
and I would need that in order to translate Color defined above into
enum like:
enum Color {
RED = 0;
GREEN = 1;
BLUE = 2;
}
My question is: do you think I'm looking into good direction (i.e.
Data/Typeable) or do you think I'll need to use something different for
data definition DSL (Template Haskell?, or impossible in Haskell so
write my own language with full parser? etc?)
Thanks for any idea or opinion on this!
Karel
More information about the Haskell-Cafe
mailing list