Existential Type Declarations in Hugs

Critterrathman@aol.com Critterrathman@aol.com
Thu, 3 May 2001 02:54:26 EDT


--part1_da.5caeee1.28225aa2_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

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

--part1_da.5caeee1.28225aa2_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

<HTML><FONT FACE=arial,helvetica><FONT  SIZE=4 FAMILY="SERIF" FACE="Times New Roman" LANG="0">I know just enuf about existential types in Haskell to be dangerous. &nbsp;While 
<BR>trying to learn a little more about how to use them, I keep running into 
<BR>problem. &nbsp;The existential types work great for code that I constructed if the 
<BR>functions take a single argument. &nbsp;However, if the function takes more than 
<BR>one argument, I can't seem to figure out how to get them to work.
<BR>
<BR>In the test code below, everything compiles in Hugs (hugs98-Feb2001) except 
<BR>the last line in the instance declaration (setx). &nbsp;Is there something 
<BR>fundamental that I'm missing about how to create an existential instance 
<BR>and/or data declaration - given the listed class definition?
<BR>
<BR>
<BR>module Test
<BR>
<BR> &nbsp;&nbsp;where
<BR>
<BR> &nbsp;&nbsp;class Shape a where
<BR> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getx :: a -&gt; Int
<BR> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setx :: a -&gt; Int -&gt; a
<BR>
<BR> &nbsp;&nbsp;data ExistentialShape =
<BR> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;forall a. Shape a =&gt; MakeExistentialShape a
<BR>
<BR> &nbsp;&nbsp;instance Shape ExistentialShape where
<BR> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getx (MakeExistentialShape a) = getx a
<BR> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setx (MakeExistentialShape a) newx = setx a newx
<BR>
<BR>
<BR>-- here's the error message I get when I try to :load the module
<BR>ERROR test.hs:14 - Type error in instance member binding
<BR>*** Term &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: setx
<BR>*** Type &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ExistentialShape -&gt; Int -&gt; _12
<BR>*** Does not match : ExistentialShape -&gt; Int -&gt; ExistentialShape
<BR>*** Because &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: cannot instantiate Skolem constant
<BR> 
<BR>
<BR>Thanks,
<BR>Chris Rathman</FONT></HTML>

--part1_da.5caeee1.28225aa2_boundary--