DSL for data definition (e.g. compiling Haskell type defs into Google's protocol buffers type defs)

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

Just making sure: have you looked at the Data.Data module yet? Edward Excerpts from Karel Gardas's message of Tue Oct 04 12:02:34 -0400 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

An interesting and semi-related project was just presented at ICFP by
Kathleen Fisher. It's called "Forest" and uses template haskell to create
schema's for "FileStores" from Haskell definitions. But they're not
plain-old-haskell type definitions...
-Ryan
On Tue, Oct 4, 2011 at 12:11 PM, Edward Z. Yang
Just making sure: have you looked at the Data.Data module yet?
Edward
Excerpts from Karel Gardas's message of Tue Oct 04 12:02:34 -0400 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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Karel,
You can use SYB's toConstr/dataTypeOf [1] to obtain information about the
name of the constructor and datatype. Alternatively, you can also use the
new generic programming framework of ghc-7.2 [2].
Cheers,
Pedro
[1]
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Data.h...
[2]
http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim-0.2.0.0/GHC-Gener...
On Tue, Oct 4, 2011 at 17:02, Karel Gardas
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@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe

On 4 October 2011 17:02, Karel Gardas
Hello,
[SNIP]
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. ...
This seems reminiscent of ASDL - the Abstract Syntax Definition Language - and ASDLgen (its generator) which used sum and product types (essentially the same a Haskell or ML's algebraic types) to define data for compiler internals. The definitions could be compiled to C++, C, Java, ML, and Haskell. As well as data type definitions ASDLgen generated marshallers for the ASDL binary format. Unfortunately the Zephyr project which sponsored ASDL seems to have had an early demise - so what info there still is available on the web might be patchy.

Hello, 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). Thanks! Karel [1]: http://book.realworldhaskell.org/read/defining-types-streamlining-functions.... 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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Forgotten note: GHC's Generics as described here: http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim-0.2.0.0/GHC-Gener... -- is not yet clear to me, I'm searching for more information about this in the meantime... Karel On 10/ 4/11 08:33 PM, Karel Gardas wrote:
Hello,
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).
Thanks! Karel
[1]: http://book.realworldhaskell.org/read/defining-types-streamlining-functions....
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Oct 4, 2011 at 1:33 PM, Karel Gardas
Hello,
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
You could use Data.Proxy and pass a type proxy as the first argument:
emit_proto (Proxy :: Proxy MyType) 1
It seems like you could then do something like `dataTypeOf (asProxyTypeOf undefined proxy)` to unpack the 'Data' instance of the proxied type. There might be a better way, but this is just what I can think of off the top of my head. The 'Poxy' type is here: http://hackage.haskell.org/packages/archive/tagged/0.2.3.1/doc/html/Data-Pro... Antoine

On Tue, Oct 4, 2011 at 20:33, Karel Gardas
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).
A definition of 'typeOf' is not supposed to use its argument, since the normal way to call it is to pass undefined. The documentation says: "The value of the argument should be ignored by any instance of Typeable, so that it is safe to pass undefined as the argument. " So you should call it like: typeOf (undefined :: PersonType). Erik

On 10/4/11 12: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:
If your main goal is to use protocol buffers in Haskell, then I'd recommend checking out Chris Kuklewicz's packages: hprotoc, protocol-buffers, protocol-buffers-descriptor. If your main goal is actually to have a DSL, well, the other folk's comments about generic programing techniques should help. -- Live well, ~wren
participants (8)
-
Antoine Latter
-
Edward Z. Yang
-
Erik Hesselink
-
José Pedro Magalhães
-
Karel Gardas
-
Ryan Newton
-
Stephen Tetley
-
wren ng thornton