Friday, 15 May 2015

haskell - how to write a derivable class? -



haskell - how to write a derivable class? -

i have this

data = integer deriving (myclass, show) class myclass hello :: myclass => -> instance myclass integer hello = + 1 main = print . hello $ 3

but myclass isn't derivable. why?

ghc cannot magically derive instances arbitrary info types. however, can create utilize of fact newtype declarations create new name same underlying type derive instances using generalizednewtypederiving extension. so, this:

{-# language generalizednewtypederiving #-} newtype = integer deriving (myclass, show) class myclass hello :: myclass => -> instance myclass integer hello = + 1 main = print . hello $ 3

the reason ghc cannot derive new instance not know instance should be. if info type has 1 field, may not same field. ability derive instances newtypes convenient, since used provide different behaviours typeclasses or way utilize type scheme separate things have same type different uses in code.

haskell

No comments:

Post a Comment