implicit or shorthand class instances?

There is a lot of code in my project like so: code: -------- data SomeThing = Something { center :: ... velocity :: ... ... } instance Locatable SomeThing where center = Something.center instance Moving SomeThing where velocity = Something.velocity instance ... -------- Is there any special extension or syntax trick that would allow me to reduce the amount of this repetitious code? Someway to implicitly state that the functions in the data type are equal to the functions in the class instance, rather than having to spell it out? -- frigidcode.com

On Thu, Nov 29, 2012 at 12:52:38PM -0900, Christopher Howard wrote:
Is there any special extension or syntax trick that would allow me to reduce the amount of this repetitious code?
I am not aware of any (which doesn't mean there is none). I would hack a few lines of Template Haskell for this. Should be pretty straight forward. Use the thee quotation operator like so ---8<--- [| {- code -} |] --->8--- to get the Haskell AST of this code snippet and pretty print it. Then you have a good starting point for what your TH function should return. Note, that if the TH function returns 'Q [Dec]' you don't need a splice operator for top-level declarations. Instead, you can simply write ---8<--- data SomeThing = .... instantiate 'SomeThing --->8--- where instantiate is your TH function. Pretty handy, IMHO. See [1] for a starting point. HTH Alex [1] http://www.haskell.org/ghc/docs/7.6.1/html/users_guide/template-haskell.html
participants (2)
-
Alexander Bernauer
-
Christopher Howard