From hpacheco at gmail.com Sun Jul 8 12:01:21 2007 From: hpacheco at gmail.com (Hugo Pacheco) Date: Sun Jul 8 11:54:59 2007 Subject: [Template-haskell] Newbie question - class dependencies Message-ID: <7b92c2840707080901g3158e75cq49a2c87376265a9e@mail.gmail.com> Hi, I'm wondering if I can (ab)use from TH in order to generalize a context-dependant function into a generic function without that context, as long as I now that the arguments I'm applying the function to belong to the class. I guess this would work similarly to an automatic instance generation example. My example would be: module Main where myprint :: a -> String myprint = show main = do putStr $ myprint 1 putStr $ myprint '2' My ideia would be to stage the computation of *myprint* until it is called, where the *Show* instance can be satisfied. hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/template-haskell/attachments/20070708/3c946db4/attachment.htm From igloo at earth.li Sun Jul 8 12:24:15 2007 From: igloo at earth.li (Ian Lynagh) Date: Sun Jul 8 12:17:52 2007 Subject: [Template-haskell] Newbie question - class dependencies In-Reply-To: <7b92c2840707080901g3158e75cq49a2c87376265a9e@mail.gmail.com> References: <7b92c2840707080901g3158e75cq49a2c87376265a9e@mail.gmail.com> Message-ID: <20070708162415.GA30332@matrix.chaos.earth.li> On Sun, Jul 08, 2007 at 05:01:21PM +0100, Hugo Pacheco wrote: > > module Main where > > myprint :: a -> String > myprint = show > > main = do > putStr $ myprint 1 > putStr $ myprint '2' > > My ideia would be to stage the computation of *myprint* until it is called, > where the *Show* instance can be satisfied. Do you mean that you want to have myprint :: Integer -> String myprint = show generated when GHC notices that myprint is called with an Integer type? Is so, then no, TH, can't do that. What you can do is to write a function mkMyPrint :: Type -> Q [Dec] which you could then call like so: $( mkMyPrint ''Integer ) but you have to do so by hand. Thanks Ian From hpacheco at gmail.com Sun Jul 8 13:10:43 2007 From: hpacheco at gmail.com (Hugo Pacheco) Date: Sun Jul 8 13:04:21 2007 Subject: [Template-haskell] Newbie question - class dependencies In-Reply-To: <20070708162415.GA30332@matrix.chaos.earth.li> References: <7b92c2840707080901g3158e75cq49a2c87376265a9e@mail.gmail.com> <20070708162415.GA30332@matrix.chaos.earth.li> Message-ID: <7b92c2840707081010i32287f55w2cba9f313b47d9e0@mail.gmail.com> What I do need is to delay the evaluation of myprint, so I can replace it with the specialized version, but you said it isn't possible. Thanks anyway, hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/template-haskell/attachments/20070708/c257ab8d/attachment.htm