<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7226.0">
<TITLE>RE: [Haskell-cafe] how would this be done? type classes? existentialtypes?</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>Try using a GADT:<BR>
<BR>
data Rs where<BR>
&nbsp; Rs :: Resource a =&gt; a -&gt; Rs<BR>
<BR>
class Resource a where<BR>
&nbsp;&nbsp;&nbsp; resourceName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; String<BR>
<BR>
instance Resource String where<BR>
&nbsp;&nbsp;&nbsp; resourceName x = &quot;String&quot;<BR>
<BR>
instance Resource Int where<BR>
&nbsp;&nbsp;&nbsp; resourceName x = &quot;Int&quot;<BR>
<BR>
resName (Rs x) = resourceName x<BR>
<BR>
resNames = map resName<BR>
<BR>
test = resNames [Rs &quot;Hi&quot;, Rs (1::Int) ]<BR>
<BR>
The most important observations is that when pattern matching on (Rs x) we cannot make any assumptions about x, except using the class members of Resource.<BR>
<BR>
We hope this will help you,<BR>
<BR>
Gerrit (and the rest of the ST-lab)<BR>
<BR>
<BR>
<BR>
<BR>
-----Original Message-----<BR>
From: haskell-cafe-bounces@haskell.org on behalf of Matthias Fischmann<BR>
Sent: Thu 3/16/2006 12:57 PM<BR>
To: haskell-cafe@haskell.org<BR>
Subject: [Haskell-cafe] how would this be done? type classes? existentialtypes?<BR>
<BR>
<BR>
<BR>
hi,<BR>
<BR>
this is one of those situations that always make scheme and perl<BR>
hackers laugh at me: i have written a piece of code that is<BR>
intuitively clear, and now i am trying to turn it into something that<BR>
compiles.&nbsp; and here it goes.<BR>
<BR>
i have a type class that looks something like this:<BR>
<BR>
&nbsp; class Resource a where<BR>
&nbsp;&nbsp;&nbsp; resourceName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; String<BR>
&nbsp;&nbsp;&nbsp; resourceAdvance&nbsp;&nbsp; :: a -&gt; a<BR>
&nbsp;&nbsp;&nbsp; resourceStarved&nbsp;&nbsp; :: a -&gt; Bool<BR>
&nbsp;&nbsp;&nbsp; resourceSpend&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; Int -&gt; a<BR>
&nbsp;&nbsp;&nbsp; resourceEarn&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; Int -&gt; a<BR>
<BR>
resource types are rice, crude oil, pizza, software code, and so on.<BR>
they all have a different internal structure and the same abstract<BR>
interface, that's why i have defined this type class.<BR>
<BR>
now i want to create a list of a type similar to<BR>
<BR>
&nbsp; [r1, r2, r3] :: (Resource a) =&gt; [a]<BR>
<BR>
but with r1 being pizza, r2 being crude oil, and so on.&nbsp; my first idea<BR>
was this:<BR>
<BR>
&nbsp; data Rs = forall a . (Resource a) =&gt; Rs a<BR>
&nbsp; unRs (Rs a) = a<BR>
&nbsp; rsName :: Rs -&gt; String<BR>
&nbsp; rsName = resourceName . unRs<BR>
&nbsp; ...<BR>
<BR>
and then export Rs as an abstract data type.&nbsp; this would allow for<BR>
lists of type [Rs], which is exactly what i want.<BR>
<BR>
but what is the type of unRs?&nbsp; or better: can i make it type at all?<BR>
and isn't this solution a little redundant and verbose?&nbsp; should i do<BR>
it like in the example for existentially quantified types in the ghc<BR>
manual?<BR>
<BR>
&nbsp; <A HREF="http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html">http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html</A><BR>
<BR>
but wouldnt't the code become really messy?&nbsp; or should i do the type<BR>
class and instances, and then do Rs the existentially quantified way,<BR>
with all class methods arguments to the Rs constructor?&nbsp; or is there a<BR>
completely different way to do this (besides using scheme or perl :-)?<BR>
<BR>
<BR>
thanks,<BR>
matthias<BR>
<BR>
<BR>
<BR>
<BR>
-----Original Message-----<BR>
From: haskell-cafe-bounces@haskell.org on behalf of Matthias Fischmann<BR>
Sent: Thu 3/16/2006 12:57 PM<BR>
To: haskell-cafe@haskell.org<BR>
Subject: [Haskell-cafe] how would this be done? type classes? existentialtypes?<BR>
<BR>
<BR>
<BR>
hi,<BR>
<BR>
this is one of those situations that always make scheme and perl<BR>
hackers laugh at me: i have written a piece of code that is<BR>
intuitively clear, and now i am trying to turn it into something that<BR>
compiles.&nbsp; and here it goes.<BR>
<BR>
i have a type class that looks something like this:<BR>
<BR>
&nbsp; class Resource a where<BR>
&nbsp;&nbsp;&nbsp; resourceName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; String<BR>
&nbsp;&nbsp;&nbsp; resourceAdvance&nbsp;&nbsp; :: a -&gt; a<BR>
&nbsp;&nbsp;&nbsp; resourceStarved&nbsp;&nbsp; :: a -&gt; Bool<BR>
&nbsp;&nbsp;&nbsp; resourceSpend&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; Int -&gt; a<BR>
&nbsp;&nbsp;&nbsp; resourceEarn&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :: a -&gt; Int -&gt; a<BR>
<BR>
resource types are rice, crude oil, pizza, software code, and so on.<BR>
they all have a different internal structure and the same abstract<BR>
interface, that's why i have defined this type class.<BR>
<BR>
now i want to create a list of a type similar to<BR>
<BR>
&nbsp; [r1, r2, r3] :: (Resource a) =&gt; [a]<BR>
<BR>
but with r1 being pizza, r2 being crude oil, and so on.&nbsp; my first idea<BR>
was this:<BR>
<BR>
&nbsp; data Rs = forall a . (Resource a) =&gt; Rs a<BR>
&nbsp; unRs (Rs a) = a<BR>
&nbsp; rsName :: Rs -&gt; String<BR>
&nbsp; rsName = resourceName . unRs<BR>
&nbsp; ...<BR>
<BR>
and then export Rs as an abstract data type.&nbsp; this would allow for<BR>
lists of type [Rs], which is exactly what i want.<BR>
<BR>
but what is the type of unRs?&nbsp; or better: can i make it type at all?<BR>
and isn't this solution a little redundant and verbose?&nbsp; should i do<BR>
it like in the example for existentially quantified types in the ghc<BR>
manual?<BR>
<BR>
&nbsp; <A HREF="http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html">http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html</A><BR>
<BR>
but wouldnt't the code become really messy?&nbsp; or should i do the type<BR>
class and instances, and then do Rs the existentially quantified way,<BR>
with all class methods arguments to the Rs constructor?&nbsp; or is there a<BR>
completely different way to do this (besides using scheme or perl :-)?<BR>
<BR>
<BR>
thanks,<BR>
matthias<BR>
<BR>
</FONT>
</P>

</BODY>
</HTML>