Annotations and Template Haskell

Hello, I'm wondering whether annotations [1] could be made available to Template Haskell users as a ghc specific extension. Would that be feasible? Would it be useful? An example use would be annotating a type with a representation of a default value that could be used in generating instances for various type classes (like parsers that take the default value into account). The alternative would be to pass that representation explicitely to every single Template Haskell invocation that uses it. Regards, Bertram [1] http://hackage.haskell.org/trac/ghc/wiki/Annotations

I'm not sure what you intend here. Can you be more concrete? Simon | -----Original Message----- | From: glasgow-haskell-users-bounces@haskell.org [mailto:glasgow-haskell-users- | bounces@haskell.org] On Behalf Of Bertram Felgenhauer | Sent: 08 March 2010 10:28 | To: glasgow-haskell-users@haskell.org | Subject: Annotations and Template Haskell | | Hello, | | I'm wondering whether annotations [1] could be made available to | Template Haskell users as a ghc specific extension. | | Would that be feasible? Would it be useful? | | An example use would be annotating a type with a representation of a | default value that could be used in generating instances for various | type classes (like parsers that take the default value into account). | | The alternative would be to pass that representation explicitely to | every single Template Haskell invocation that uses it. | | Regards, | | Bertram | | [1] http://hackage.haskell.org/trac/ghc/wiki/Annotations | _______________________________________________ | Glasgow-haskell-users mailing list | Glasgow-haskell-users@haskell.org | http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

Hello Simon,
I'm not sure what you intend here. Can you be more concrete?
A concrete example could look like this, {-# ANN T $(defaultValue [| T { foo = 1 } |]) #-} data T = T { foo :: Int, baz :: Int } where defaultValue :: Q Exp -> Q Exp would encode the value in a suitable form. The intention is to specify default values for some fields of the record. Then, later, I want to generate a parser for T, that takes the default value into consideration, in order to make the foo field optional and the baz field mandatory. $(deriveParser ''T) Inside deriveParser, I want to access the annotation of T. That requires a function like getAnnotations :: (Typeable a, Binary a) => Name -> Q [a] which does not exist. So the first question is, Would adding this function be feasible? If yes, what sort of stage restrictions would be required to make it work? Continuing the example above, I can achieve the same effect by passing the default value in some form as an argument to deriveParser. But I'd prefer using an annotation because then I only have to specify that value once even if there are several chunks of generated code that rely on a default value. A second advantage is that an unused annotation does not add any clutter to the code at all -- it can be completely optional. So in my eyes this feature would be ideal for specifying exceptional handling of a few data types (or functions or constructors) from Template Haskell code. Of course there are disadvantages as well, foremost that it'd be yet another ghc specific extension. So the second question is, is the idea really useful enough to justify being implemented? regards, Bertram [annotations] http://hackage.haskell.org/trac/ghc/wiki/Annotations
participants (2)
-
Bertram Felgenhauer
-
Simon Peyton-Jones