template haskell for typeclass synonyms
Hello. I'd like to have a template haskell function that take some constraints and a class name and write an empty class from those and relative empty instance to simulate typeclass synonyms. As I've never written TH and couldn't find a easily adaptable code around, I ask here for the code, or some hints on how to arrive there. Thanks paolino
On 1 November 2010 17:53, Paolino <paolo.veronelli@gmail.com> wrote:
I'd like to have a template haskell function that take some constraints and a class name and write an empty class from those and relative empty instance to simulate typeclass synonyms.
As I've never written TH and couldn't find a easily adaptable code around, I ask here for the code, or some hints on how to arrive there.
I took Justin Bailey's haskelldb-th library as a TH example to work from and rewrote one TH function to try my hand at it, it's quite easy to follow with a simple example: http://hpaste.org/paste/41035/demo Maybe this is enough example to get you going. The rest you can find syntax parts from the TH Haddock documentation.
On Mon, Nov 1, 2010 at 6:09 PM, Christopher Done <chrisdone@googlemail.com> wrote:
On 1 November 2010 17:53, Paolino <paolo.veronelli@gmail.com> wrote:
I'd like to have a template haskell function that take some constraints and a class name and write an empty class from those and relative empty instance to simulate typeclass synonyms.
As I've never written TH and couldn't find a easily adaptable code around, I ask here for the code, or some hints on how to arrive there.
I took Justin Bailey's haskelldb-th library as a TH example to work from and rewrote one TH function to try my hand at it, it's quite easy to follow with a simple example:
http://hpaste.org/paste/41035/demo
Maybe this is enough example to get you going. The rest you can find syntax parts from the TH Haddock documentation.
A useful FYI: the API docs are (almost) completely devoid of comments, but if you click to see the source, it does have some additional information in comments there, just not Haddock-formatted.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Work is punishment for failing to procrastinate effectively.
Thanks. I annotated the function http://hpaste.org/paste/41035/test_simpleclasssynonym It seems to produce the right code. How should I use the "Parents" synonym in my functions? This is a noob question I suppose. paolino 2010/11/1 Gábor Lehel <illissius@gmail.com>
On Mon, Nov 1, 2010 at 6:09 PM, Christopher Done <chrisdone@googlemail.com> wrote:
On 1 November 2010 17:53, Paolino <paolo.veronelli@gmail.com> wrote:
I'd like to have a template haskell function that take some constraints and a class name and write an empty class from those and relative empty instance to simulate typeclass synonyms.
As I've never written TH and couldn't find a easily adaptable code around, I ask here for the code, or some hints on how to arrive there.
I took Justin Bailey's haskelldb-th library as a TH example to work from and rewrote one TH function to try my hand at it, it's quite easy to follow with a simple example:
http://hpaste.org/paste/41035/demo
Maybe this is enough example to get you going. The rest you can find syntax parts from the TH Haddock documentation.
A useful FYI: the API docs are (almost) completely devoid of comments, but if you click to see the source, it does have some additional information in comments there, just not Haddock-formatted.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Work is punishment for failing to procrastinate effectively.
I think I've got something nice in the end. http://hpaste.org/41042/classsynonymhs example: class ( ParteDi (Servizio a) s , Read a , Eq a , Show a , Integer `ParteDi` s ) => SClass s a $(classSynonym ''SClass) ghci ":i SClass" command is printing some strange type variables but it compiles paolino 2010/11/1 Gábor Lehel <illissius@gmail.com>
On Mon, Nov 1, 2010 at 6:09 PM, Christopher Done <chrisdone@googlemail.com> wrote:
On 1 November 2010 17:53, Paolino <paolo.veronelli@gmail.com> wrote:
I'd like to have a template haskell function that take some constraints and a class name and write an empty class from those and relative empty instance to simulate typeclass synonyms.
As I've never written TH and couldn't find a easily adaptable code around, I ask here for the code, or some hints on how to arrive there.
I took Justin Bailey's haskelldb-th library as a TH example to work from and rewrote one TH function to try my hand at it, it's quite easy to follow with a simple example:
http://hpaste.org/paste/41035/demo
Maybe this is enough example to get you going. The rest you can find syntax parts from the TH Haddock documentation.
A useful FYI: the API docs are (almost) completely devoid of comments, but if you click to see the source, it does have some additional information in comments there, just not Haddock-formatted.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Work is punishment for failing to procrastinate effectively.
2010/11/1 Paolino <paolo.veronelli@gmail.com>:
I think I've got something nice in the end.
http://hpaste.org/41042/classsynonymhs
example:
class ( ParteDi (Servizio a) s , Read a , Eq a , Show a , Integer `ParteDi` s ) ⇒ SClass s a
$(classSynonym ''SClass)
ghci ":i SClass" command is printing some strange type variables but it compiles
Template Haskell might be overkill for this. In the past, I've done:
class (Eq b, Show b, MyClass b, MyOtherClass b) => MySynonym b instance (Eq b, Show b, MyClass b, MyOtherClass b) => MySynonym b
I think this requires a couple of GHC extensions, but TemplateHaskell is an extension as well. Maybe there are pitfalls with this approach. Antoine
It's first time I use TH. It would be nice to point out the motivations for using it. If everything TH does is doable without it, the point of using it is write less code, eliminating some necessary and automatically computable code. But I guess there is some more ..... paolino 2010/11/2 Antoine Latter <aslatter@gmail.com>
2010/11/1 Paolino <paolo.veronelli@gmail.com>:
I think I've got something nice in the end.
http://hpaste.org/41042/classsynonymhs
example:
class ( ParteDi (Servizio a) s , Read a , Eq a , Show a , Integer `ParteDi` s ) => SClass s a
$(classSynonym ''SClass)
ghci ":i SClass" command is printing some strange type variables but it compiles
Template Haskell might be overkill for this. In the past, I've done:
class (Eq b, Show b, MyClass b, MyOtherClass b) => MySynonym b instance (Eq b, Show b, MyClass b, MyOtherClass b) => MySynonym b
I think this requires a couple of GHC extensions, but TemplateHaskell is an extension as well. Maybe there are pitfalls with this approach.
Antoine
Well, Template Haskell is what you go to when what you want -can't- be reasonably expressed with standard GHC Haskell. It's something of a last resort (at least in my case). Typeclass synonyms can be faked reasonably well with UndecidableInstances, but if you want to, for example, automatically generate nontrivial instances of some class for user-provided types, Template Haskell is frequently what you need to use. 2010/11/2 Paolino <paolo.veronelli@gmail.com>:
It's first time I use TH. It would be nice to point out the motivations for using it. If everything TH does is doable without it, the point of using it is write less code, eliminating some necessary and automatically computable code. But I guess there is some more .....
paolino
2010/11/2 Antoine Latter <aslatter@gmail.com>
2010/11/1 Paolino <paolo.veronelli@gmail.com>:
I think I've got something nice in the end.
http://hpaste.org/41042/classsynonymhs
example:
class ( ParteDi (Servizio a) s , Read a , Eq a , Show a , Integer `ParteDi` s ) => SClass s a
$(classSynonym ''SClass)
ghci ":i SClass" command is printing some strange type variables but it compiles
Template Haskell might be overkill for this. In the past, I've done:
class (Eq b, Show b, MyClass b, MyOtherClass b) => MySynonym b instance (Eq b, Show b, MyClass b, MyOtherClass b) => MySynonym b
I think this requires a couple of GHC extensions, but TemplateHaskell is an extension as well. Maybe there are pitfalls with this approach.
Antoine
-- Work is punishment for failing to procrastinate effectively.
participants (4)
-
Antoine Latter -
Christopher Done -
Gábor Lehel -
Paolino