[Haskell-cafe] problem with collection (container) class

Leandro Demarco Vedelago leandrodemarco at gmail.com
Thu Feb 7 02:58:10 EST 2008


Hi! I'm pretty (not to say very) new to haskell and i've to create a new
class named container that includes types that hold other values, more
specific, i have "created" this two types:

data Abb a b = Branch a b (Abb a b) (Abb a b) | Leaf
and
data ListAssoc a b = Node a b (ListAssoc a b) | Empty

and implemented some common functions (add, search, delete, empty,etc). So
when I was asked to create this new class I crashed against a big problem,
or at least for me. My first attempt was:

class Container c where
empty :: c

but when I had to implement "add" I tried

add :: c -> a -> b -> c

although I was almost sure that it wouldn't work, so I realized that I had
to give Container not one but three parameters, the container and the types
of values that the container saved so I started searching and I found
similar cases, but the difference iis that in this cases the class was
defined as follows:

class Container c e | c->e where
empty :: c
insert :: c -> e -> c
member :: c -> e -> Bool

but my particular problem here is that the types I designed hold two values
of different type and these holds just one value. Besides I knew nothing
about functional dependency used in here and I keep knowing almost nothing
about them, but I tried something like this:

class Container c a b |c -> a, c -> b  where

empty :: c
add :: c -> a -> c
search :: c -> a -> Maybe b
del :: c -> a -> c
toListPair :: c -> [(a,b)]

instance Container Abb a b where

empty = Leaf

add Leaf x y = Branch x y Leaf Leaf
add arb@(Branch ni nd ri rd) x y    |x == ni = arb
                    |x > ni = Branch ni nd ri (add rd x y)
                    |otherwise = Branch ni nd (add ri x y) rd

search Leaf x  = Nothing
search (Branch ni nd ri rd) x    |x == ni = Just nd
                                            |x > ni = search rd x
                                            |x < ni = search ri x

but when I try to load it in WinHugs I get the following error message:

- Instance is more general than a dependency allows
*** Instance         : Container Abb a b
*** For class        : Container a b c
*** Under dependency : a -> b

and as I have stated above my knowledge about dependencies is almost null,
not to say null, so I don´t even have an idea where the error is. A
suggestion that I've received was to change the type of Abb for

data Abb (a,b) = Branch a b (Abb (a,b)) (Abb (a,b)) | Leaf

and declare container class with just two parameters like I found in all
pages I visited. I have not tried this yet, as I still have hope that what I
intend to do is possible.

Well if you have any suggestions I'd appreciate you send it to me and sorry
for bothering you and my english, but i'm "spanish-speaker".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080207/6f756dd2/attachment.htm


More information about the Haskell-Cafe mailing list