Question

While trying to define some mathematical objects using the Numeric prelude I've run into a problem. The Additive typeclass defines an instance

instance Additive.C v => Additive.C [v]

Which I read "if v is Additive, [v] is too" (apparently I was wrong here). It's implemented something like

(+) x y = map (\(a,b) -> a + b) $ zip x y

So that [1,2,3] + [4,5,6] = [5,7,9] which is useless for what I want to do. I assumed I wouldn't have a problem as my v type isn't Additive. Unfortunately I still got an overlapping instances error which I found very confusing. I've did a little reading and I now understand that for some reason, Haskell ignores everything before the "=>" bit so I should have read the default instance as "any list is potentially additive in the sense of the default instance". I've tried using OverlappingInstances despite the fact that this extension has the reputation of being "dangerous", but even that doesn't seem to help.

Here is my testcase.

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE MultiParamTypeClasses,FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-} --This doesn't seem to help
import NumericPrelude

import qualified Algebra.Additive    as Additive

data Test = Red | Green | Blue deriving Show

instance Additive.C [Test] where
   zero = undefined
   (+) =  undefined
   negate = undefined

test = [Red] + [Green] + [Blue]

Produces the error (Update: this appears to only happen in older versions of GHC. version 7.2.2 seems to accept it):

Overlapping instances for Additive.C [Test]
  arising from a use of `+'
Matching instances:
  instance Additive.C v => Additive.C [v]
    -- Defined in Algebra.Additive
  instance [overlap ok] Additive.C [Test]
    -- Defined at Testcase.hs:10:10-26
In the first argument of `(+)', namely `[Red] + [Green]'
In the expression: [Red] + [Green] + [Blue]
In an equation for `test': test = [Red] + [Green] + [Blue]

Does this mean I can't use lists because I don't want to default instance of Additive? What I really want to do is tell ghc to just forget that default instance, it that possible? If not, I'm not sure where to go from here other than dropping lists.

Was it helpful?

Solution

Edit: As @kosmikus mentioned, your example works well for me, too. I am using ghc 7.4.1.

You cannot make the compiler forget the instance as it is imported as soon as you import the module, the instance is defined in. Note that OverlappingInstances does not tell the compiler to forget an instance but to take the most specify instance that is available.

In order to prevent overlapping instances you can use a type wrapper that is used to distinguish arbitrary lists from the lists you are using. For example, you can define

data TestList = TestList [Test]

Then, you can define a custom instance of the type class for TestList. In most cases, people use record syntax to define an accessor for the list as you have to wrap and unwrap the lists.

data TestList = TestList { list :: [Test] }

In order to reduce the cost of the additional constructor you can use a newtype instead of a data.

newtype TestList = TestList { list :: [Test] }

A newtype may only have a single argument and the compiler basically handles it as if it wasn't there but uses the type information, which the constructor provides, to distinguish your lists from arbitrary lists when choosing the correct instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top