[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 20:33:12 CEST 2011


thanks a lot to Edward, Jose, Ryan and Stephen for fast reply in this 
thread. I see I've not been that precise in specification of what I need 
exactly so I need to add this: I've changed a little bit definition of 
person to:

data PersonType = Person {
         id :: Int
         , name :: String
         , email :: Maybe String
         deriving (Show, Data, Typeable)

so I have `PersonType' as type constructor and Person as value 
constructor (or data constructor) -- speaking using terms from Real 
World Haskell, Chapter 3[1]. And now I see that none of 
typeOf/dataTypeOf/toContr is applicable to *type constructor* but all 
are applicable to *value/data constructor*. Ditto happen when testing 
Color versus RED, GREEN, BLUE. At least GHCi complains this way:

*Main> typeOf Color

<interactive>:0:8: Not in scope: data constructor `Color'
*Main> typeOf PersonType

<interactive>:0:8: Not in scope: data constructor `PersonType'

But, I'd like to start processing of data definition from the *type 
constructor*. So:

emit_proto PersonType 1
emit_proto Color 1

Is that possible at all? I mean in the scope/context of GHC's 
Data/Data.Data/Data.Typeable etc. modules. (w/o considering TH now).



On 10/ 4/11 06:02 PM, Karel Gardas wrote:
> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list