Existential Type Declarations in Hugs
I know just enuf about existential types in Haskell to be dangerous. While trying to learn a little more about how to use them, I keep running into problem. The existential types work great for code that I constructed if the functions take a single argument. However, if the function takes more than one argument, I can't seem to figure out how to get them to work. In the test code below, everything compiles in Hugs (hugs98-Feb2001) except the last line in the instance declaration (setx). Is there something fundamental that I'm missing about how to create an existential instance and/or data declaration - given the listed class definition? module Test where class Shape a where getx :: a -> Int setx :: a -> Int -> a data ExistentialShape = forall a. Shape a => MakeExistentialShape a instance Shape ExistentialShape where getx (MakeExistentialShape a) = getx a setx (MakeExistentialShape a) newx = setx a newx -- here's the error message I get when I try to :load the module ERROR test.hs:14 - Type error in instance member binding *** Term : setx *** Type : ExistentialShape -> Int -> _12 *** Does not match : ExistentialShape -> Int -> ExistentialShape *** Because : cannot instantiate Skolem constant Thanks, Chris Rathman
Thu, 3 May 2001 02:54:26 EDT, Critterrathman@aol.com <Critterrathman@aol.com> pisze:
class Shape a where getx :: a -> Int setx :: a -> Int -> a
data ExistentialShape = forall a. Shape a => MakeExistentialShape a
instance Shape ExistentialShape where getx (MakeExistentialShape a) = getx a setx (MakeExistentialShape a) newx = setx a newx
Should be: setx (MakeExistentialShape a) newx = MakeExistentialShape (setx a newx) The result of setx must have the same type as its first argument. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (2)
-
Critterrathman@aol.com -
Marcin 'Qrczak' Kowalczyk