<!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>
Rs :: Resource a => a -> Rs<BR>
<BR>
class Resource a where<BR>
resourceName :: a -> String<BR>
<BR>
instance Resource String where<BR>
resourceName x = "String"<BR>
<BR>
instance Resource Int where<BR>
resourceName x = "Int"<BR>
<BR>
resName (Rs x) = resourceName x<BR>
<BR>
resNames = map resName<BR>
<BR>
test = resNames [Rs "Hi", 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. and here it goes.<BR>
<BR>
i have a type class that looks something like this:<BR>
<BR>
class Resource a where<BR>
resourceName :: a -> String<BR>
resourceAdvance :: a -> a<BR>
resourceStarved :: a -> Bool<BR>
resourceSpend :: a -> Int -> a<BR>
resourceEarn :: a -> Int -> 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>
[r1, r2, r3] :: (Resource a) => [a]<BR>
<BR>
but with r1 being pizza, r2 being crude oil, and so on. my first idea<BR>
was this:<BR>
<BR>
data Rs = forall a . (Resource a) => Rs a<BR>
unRs (Rs a) = a<BR>
rsName :: Rs -> String<BR>
rsName = resourceName . unRs<BR>
...<BR>
<BR>
and then export Rs as an abstract data type. this would allow for<BR>
lists of type [Rs], which is exactly what i want.<BR>
<BR>
but what is the type of unRs? or better: can i make it type at all?<BR>
and isn't this solution a little redundant and verbose? should i do<BR>
it like in the example for existentially quantified types in the ghc<BR>
manual?<BR>
<BR>
<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? 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? 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. and here it goes.<BR>
<BR>
i have a type class that looks something like this:<BR>
<BR>
class Resource a where<BR>
resourceName :: a -> String<BR>
resourceAdvance :: a -> a<BR>
resourceStarved :: a -> Bool<BR>
resourceSpend :: a -> Int -> a<BR>
resourceEarn :: a -> Int -> 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>
[r1, r2, r3] :: (Resource a) => [a]<BR>
<BR>
but with r1 being pizza, r2 being crude oil, and so on. my first idea<BR>
was this:<BR>
<BR>
data Rs = forall a . (Resource a) => Rs a<BR>
unRs (Rs a) = a<BR>
rsName :: Rs -> String<BR>
rsName = resourceName . unRs<BR>
...<BR>
<BR>
and then export Rs as an abstract data type. this would allow for<BR>
lists of type [Rs], which is exactly what i want.<BR>
<BR>
but what is the type of unRs? or better: can i make it type at all?<BR>
and isn't this solution a little redundant and verbose? should i do<BR>
it like in the example for existentially quantified types in the ghc<BR>
manual?<BR>
<BR>
<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? 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? 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>