Deriving

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Deriving

Daryoush Mehrtash-2
What happens when a type adds driving such as:

newtype
SupplyT s m a = SupplyT (StateT [s] m a)
deriving (Functor, Monad, MonadTrans, MonadIO)

Two questions:

How does the deriving implement the instance?

Is there a way for me to add  my own classes in the deriving?  for example

newtype .....
   deriving( xyz)


Thanks



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Deriving

Martin Huschenbett
If you use a newtype the answer to the second question is yes. Just put

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

in the first line of your module or pass -XGeneralizedNewtypeDeriving to
ghc or ghci.

Daryoush Mehrtash schrieb:

> What happens when a type adds driving such as:
>
> newtype SupplyT s m a = SupplyT (StateT [s] m a)
>
>     deriving (Functor <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor>, Monad <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad>, MonadTrans, MonadIO)
>
>
> Two questions:
>
> How does the deriving implement the instance?
>
> Is there a way for me to add  my own classes in the deriving?  for example
>
> newtype .....
>    deriving( xyz)
>
>
> Thanks
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Deriving

wren ng thornton
In reply to this post by Daryoush Mehrtash-2
Daryoush Mehrtash wrote:
> What happens when a type adds driving such as:
>
> newtype SupplyT s m a = SupplyT (StateT [s] m a)
>     deriving (Functor, Monad, MonadTrans, MonadIO)
>
>
> Two questions:
>
> How does the deriving implement the instance?

With GeneralizedNewtypeDeriving, since newtypes are just a
reinterpretation of the underlying type, the deriving clause just uses
the instance for the underlying type (massaging it with the
wrapping/unwrapping functions for the newtype). The primary use for this
is for defining a newtype on monad transformer stacks, for which it
succeeds excellently. However, if the semantics of your newtype are
different than the underlying type w.r.t. the type class, then it's not
so good.


For the more basic kind of deriving clause, the compiler knows about how
to create generic instances for obvious things, e.g. the compiler can
derive the obvious Show instance by doing introspection on the source
code. However, these derivations are limited to classes with an obvious
implementation based solely on the structure of the type definition.

--
Live well,
~wren
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Deriving

Johan Jeuring
In reply to this post by Daryoush Mehrtash-2
> What happens when a type adds driving such as:
>
> newtype SupplyT s m a = SupplyT (StateT [s] m a)
>
>     deriving (Functor, Monad, MonadTrans, MonadIO)
>
> Two questions:
>
> How does the deriving implement the instance?
>
> Is there a way for me to add  my own classes in the deriving?  for  
> example
>
> newtype .....
>    deriving( xyz)

In general you would have to resort to generic programming techniques
to obtain your own deriving mechanism.

See the generics category on Hackage for libraries:

http://hackage.haskell.org/packages/archive/pkg-list.html

To find out what you can do with the various generic programming
techniques, you can read (shameless plugs):

Alexey Rodriguez Yakushev, Johan Jeuring, Patrik Jansson, Alex Gerdes,
Oleg Kiselyov, Bruno C. d. S. Oliviera. Comparing Libraries for Generic
Programming in Haskell. In Andy Gill, editor, Proceedings of the ACM
SIGPLAN Haskell Symposium. An extended version is available as Technical
report Utrecht University UU-CS-2008-010, 2008.

Ralf Hinze, Johan Jeuring, and Andres Löh. Comparing approaches to  
generic
programming in Haskell. In Roland Backhouse, Jeremy Gibbons, Ralf Hinze,
and Johan Jeuring, editors, Lecture notes of the Spring School on
Datatype-Generic Programming 2006, LNCS 4719, pages 72 - 149, 2007,
Springer-Verlag.

-- Johan Jeuring

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe