overlapping instances in 6.6 candidate

Simon Peyton-Jones simonpj at microsoft.com
Mon Sep 4 09:00:12 EDT 2006


You can see the rules here
http://www.haskell.org/ghc/dist/current/docs/users_guide/type-extensions
.html#instance-overlap

GHC 6.6's story is that an instance declaration can only be overlapped
if you compile that module with -fallow-overlapping-instances.  Since
the list instance for Show was not compiled in this way, you can't
overlap it.

You may think this is annoying -- but it does mean that you can look at
an instance declaration and see whether it might be overlapped, just by
look at the flags for that module.  Currently there is no workaround;
you have to compile the instance decl that you want to overlap with the
-fallow-overlapping-instances flag.

It would be possible to relax this restriction, so that instances can
overlapped if *either* you use -fallow-overlapping-instances when you
compile the instance declaration, *or* you use that flag at the usage
site.  (But the exact usage site can be hard to pin down.)  I'd be
interested in feedback on this. 

Simon

| -----Original Message-----
| From: glasgow-haskell-users-bounces at haskell.org
[mailto:glasgow-haskell-users-bounces at haskell.org]
| On Behalf Of Serge D. Mechveliani
| Sent: 02 September 2006 12:30
| To: glasgow-haskell-users at haskell.org
| Subject: overlapping instances in 6.6 candidate
| 
| Dear GHC developers,
| 
| Can you, please, explain what has happened in  6.5.20060831
| with treating of  overlapping instances?
| 
| It seems to understand them differently than  ghc-6.4.1.
| For example, I compile the module
| 
| ------------------------
| class C a where c :: a -> Bool
| 
| instance C Int where c n = n > 0
| 
| instance C a => C [a] where  c xs =  and $ map c xs
| 
| instance C [Int] where  c xs =  c $ head xs
| 
| main = putStr (shows (c [1, (-2 :: Int)]) "\n")
| 
| instance Show [Bool] where  showsPrec _ xs =  shows (head xs)
| ------------------------
| 
| with the options
| $dmCpOpt =
|   -fglasgow-exts -fallow-undecidable-instances
|   -fallow-overlapping-instances -fno-warn-overlapping-patterns
|   -fwarn-unused-binds -fwarn-unused-matches -fwarn-unused-imports
| :
| > ghc $dmCpOpt --make Main
| 
| It reports
| 
| --------------------------------------------------------
| [1 of 1] Compiling Main             ( Main.hs, Main.o )
| 
| Main.hs:11:0:
|     Overlapping instances for Show [Bool]
|       arising from use of `GHC.Show.$dmshowList' at Main.hs:11:0
|     Matching instances:
|       instance (Show a) => Show [a] -- Defined in GHC.Show
|       instance [overlap ok] Show [Bool] -- Defined at Main.hs:11:0
|     In the expression: GHC.Show.$dmshowList
|     In the definition of `showList': showList = GHC.Show.$dmshowList
|     In the definition for method `showList'
| --------------------------------------------
| 
| And it takes as all right the overlaps for the class  C.
| 
| [Bool]  is a substitutional instance of  [a],  and according to the
GHC
| specification, the Show instance for [Bool] must override the more
generic
| one for  Show a => [a].
| 
| Thank you in advance for explanation.
| 
| -----------------
| Serge Mechveliani
| mechvel at botik.ru
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users at haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


More information about the Glasgow-haskell-users mailing list