logo       

RE: can't derive Monad: msg#00084

lang.haskell.glasgow.bugs

Subject: RE: can't derive Monad

| > You'd get
| >
| > instance Eq [T] => Eq T
| >
| > which will make the type checker loop for sure.
|
| Actually not.
|
| swan(102)% cat Deriving.hs
| newtype N = N [N] deriving Eq

What it's doing is *first* trying the new newtype-deriving stuff,
failing (because recursive) and then trying old vanilla deriving,
getting
instance Eq N where
(==) (N a) (N b) = a == b

You can't do that when the class isn't a standard class that GHC knows
about. The point about the new newtype-deriving stuff is that it works
for ANY class. From
newtype Foo = Foo T deriving( C )
we generate
instance C T => C Foo
and use the (C T) dictionary for (C Foo). But there has to be some way
to stop instance deduction looping. Usually that's by making instance
decls have only type vars to the left of the =>, but here it's by making
sure the newtype is non-recursive.

Simon

|
| n1 = N []
| n2 = N []
| n3 = N [n1]
| n4 = N [n1,n2]
|
| main = mapM_ print [ n1 == n2, n2 == n3, n3 == n4 ]
| swan(103)% ghci Deriving
| ___ ___ _
| / _ \ /\ /\/ __(_)
| / /_\// /_/ / / | | GHC Interactive, version 5.04.3, for
Haskell 98.
|
| / /_\\/ __ / /___| | http://www.haskell.org/ghc/
| \____/\/ /_/\____/|_| Type :? for help.
|
| Loading package base ... linking ... done.
| Loading package haskell98 ... linking ... done.
| Compiling Main ( Deriving.hs, interpreted )
| Ok, modules loaded: Main.
| *Main> main
| True
| False
| False
| *Main>
|
| > I'm not sure what a
| > safe approximation might be.
|
| Would one need to approximate? Could one try to type check and bail
out if
| that fails?
|
| > But I'll put your example in a comment in
| > the source code as an example of a safe one that's rejected!
| >
| > Simon
| >
| > | -----Original Message-----
| > | From: glasgow-haskell-bugs-admin@xxxxxxxxxxx
| > [mailto:glasgow-haskell-bugs-admin@xxxxxxxxxxx] On
| > | Behalf Of Dean Herington
| > | Sent: 28 July 2003 23:01
| > | To: glasgow-haskell-bugs@xxxxxxxxxxx
| > | Subject: can't derive Monad
| > |
| > | The following may not be a bug, but it surprised me. Why does the
| > | circularity cause GHC fits?
| > |
| > |
| > | swan(106)% cat NestStateT.hs
| > | {-# OPTIONS -fglasgow-exts #-}
| > |
| > | import Control.Monad.State
| > |
| > | newtype S1 = S1 [T1 ()]
| > | newtype T1 a = T1 (StateT S1 IO a )
| > | deriving Monad
| > |
| > | main = undefined
| > | swan(105)% ghci NestStateT.hs
| > | ___ ___ _
| > | / _ \ /\ /\/ __(_)
| > | / /_\// /_/ / / | | GHC Interactive, version 5.04.3, for
| > Haskell
| > | 98.
| > | / /_\\/ __ / /___| | http://www.haskell.org/ghc/
| > | \____/\/ /_/\____/|_| Type :? for help.
| > |
| > | Loading package base ... linking ... done.
| > | Loading package haskell98 ... linking ... done.
| > | Compiling Main ( NestStateT.hs, interpreted )
| > |
| > | NestStateT.hs:6:
| > | Can't make a derived instance of `Monad T1'
| > | (too hard for cunning newtype deriving)
| > | When deriving instances for type `T1'
| > | Failed, modules loaded: none.
| > | Prelude>
|


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise