what exactly does "deriving (Functor, Monad, MonadIO)" do?

I was trying to follow the reasoning in Don's article on using haskell for shell scripting http://cgi.cse.unsw.edu.au/~dons/blog/2007/03/10 In the source listing at the end we is newtype Shell a = Shell { runShell :: ErrorT String IO a } deriving (Functor, Monad, MonadIO) and I don't understand it what "deriving" is doing here, nor have I been able to find documentation on it. http://en.wikibooks.org/wiki/Haskell/Class_declarations claims: "You can only use deriving with a limited set of built-in classes. They are: Eq Ord Enum Bounded Show Read " But, here we are deriving classes not in that list. So, is this a recently added feature? Or something that came in from {-# OPTIONS -fglasgow-exts #-} ? I would just like to understand this, and I can't figure out how to begin. Thanks for any help! thomas.

tphyahoo:
I was trying to follow the reasoning in Don's article on using haskell for shell scripting
http://cgi.cse.unsw.edu.au/~dons/blog/2007/03/10
In the source listing at the end we is
newtype Shell a = Shell { runShell :: ErrorT String IO a } deriving (Functor, Monad, MonadIO)
and I don't understand it what "deriving" is doing here, nor have I been able to find documentation on it.
That's 'cunning newtype deriving, my new favourite ghc language extension. http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html... We also use it in xmonad, newtype X a = X (ReaderT XConf (StateT XState IO) a) deriving (Functor, Monad, MonadIO, MonadState XState, MonadReader XConf) :-) -- Don

Thanks Dons.
There's also a short and sweet explanation here.
http://hackage.haskell.org/trac/haskell-prime/wiki/NewtypeDeriving
I am going to try and wrap my head around this, as I am very
interested in solutions for haskell / shell interaction.
Are there are any good examples of code written without this
extension, alongside code condensed by using this extension. That
would be helpful for understanding what's going on.
Thomas.
2007/5/1, Donald Bruce Stewart
tphyahoo:
I was trying to follow the reasoning in Don's article on using haskell for shell scripting
http://cgi.cse.unsw.edu.au/~dons/blog/2007/03/10
In the source listing at the end we is
newtype Shell a = Shell { runShell :: ErrorT String IO a } deriving (Functor, Monad, MonadIO)
and I don't understand it what "deriving" is doing here, nor have I been able to find documentation on it.
That's 'cunning newtype deriving, my new favourite ghc language extension.
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html...
We also use it in xmonad,
newtype X a = X (ReaderT XConf (StateT XState IO) a) deriving (Functor, Monad, MonadIO, MonadState XState, MonadReader XConf)
:-)
-- Don

On May 1, 2007, at 6:05 , Thomas Hartman wrote:
Are there are any good examples of code written without this extension, alongside code condensed by using this extension. That would be helpful for understanding what's going on.
I think all this does is save you from having to write a bunch of wrappers that unwrap the contained value, do something to it, and rewrap the result. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On 01/05/07, Brandon S. Allbery KF8NH
I think all this does is save you from having to write a bunch of wrappers that unwrap the contained value, do something to it, and rewrap the result.
Exactly. Basically what newtype deriving does is if you have a declaration like the following: newtype T = TConstructor M And M instantiates some class (like Monad, Functor etc), you can derive that class for T. For example, here's how the Functor instance would look for the following newtype: newtype MyMaybe a = MM (Maybe a) deriving (Functor) -- The instance looks like this: instance Functor MyMaybe where fmap f (MM a) = MM (fmap f a) The instance just unwraps and rewraps the newtype constructor. -- -David House, dmhouse@gmail.com
participants (4)
-
Brandon S. Allbery KF8NH
-
David House
-
dons@cse.unsw.edu.au
-
Thomas Hartman