Ambiguous defaults

Ferenc Wagner wferi@afavant.elte.hu
Thu, 02 Jan 2003 17:49:41 +0100


               Hello,

What's the way to express the following: a compound object
is generally made up of two components with identical type.
For this common case I'd like to provide some default
methods, which take the object apart, operate on the parts
and put the results back together.  In other cases this
default method should be overridden, eg. when the compound
object isn't made of two identical components.  My first try
(below) doesn't work, can you recommend a solution?

                                     Thanks: Feri.

\begin{code}
module Test where

class Component b where
    property :: b -> Int

class Compound a where
    decompose :: Component b => a -> (b,b)
    additive :: a -> Int
    additive x = property l + property r
        where (l,r) = decompose x
\end{code}

ghci: test.hs:9:
    Ambiguous type variable(s) `b' in the constraint `Component b'
    arising from use of `property' at test.hs:9

hugs: ERROR test.hs:9 - Cannot justify constraints in default member binding
*** Expression    : additive
*** Type          : Compound a => a -> Int
*** Given context : Compound a
*** Constraints   : Component b