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

José Pedro Magalhães jpm at cs.uu.nl
Tue Oct 4 18:13:09 CEST 2011


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.html#v:toConstr
[2]
http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Datatype

On Tue, Oct 4, 2011 at 17:02, Karel Gardas <karel.gardas at centrum.cz> 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<http://www.haskell.org/mailman/listinfo/haskell-cafe>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111004/019470cc/attachment.htm>


More information about the Haskell-Cafe mailing list