
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

G'day all. On Thu, Jan 02, 2003 at 05:49:41PM +0100, Ferenc Wagner wrote:
What's the way to express the following: a compound object is generally made up of two components with identical type.
This should work: \begin{code} module Test where class Component b where property :: b -> Int class (Component b) => Compound a b | a -> b where decompose :: a -> (b,b) additive :: a -> Int additive x = property l + property r where (l,r) = decompose x \end{code} Cheers, Andrew Bromage

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've never been very clear on this. As I understand it, Andrew's code specifies a Compound class where the type the compound determines the type (and consequently class) of the value of "decompose c". In Ferenc's example, the Compound class specifies that the class of the value of "decompose c" will be Component. So, Ferenc's example is ambiguous in so far as a Compound type does not determine the Component type, but it does determine the Class and isn't that all we need to know to be able to call the 'property' function? Tom On Fri, 3 Jan 2003 10:18 am, Andrew J Bromage wrote:
G'day all.
On Thu, Jan 02, 2003 at 05:49:41PM +0100, Ferenc Wagner wrote:
What's the way to express the following: a compound object is generally made up of two components with identical type.
This should work:
\begin{code} module Test where
class Component b where property :: b -> Int
class (Component b) => Compound a b | a -> b where decompose :: a -> (b,b) additive :: a -> Int additive x = property l + property r where (l,r) = decompose x \end{code}
Cheers, Andrew Bromage _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE+G8o4Gse0w80KF2gRApvGAKCFRiK2V7ThDlLHcEl8upL6W57sRQCgjCeP CgQJ2uArswCo0z/CS2caEq0= =N/bh -----END PGP SIGNATURE-----
participants (3)
-
Andrew J Bromage
-
Ferenc Wagner
-
Thomas L. Bevan