[Haskell-cafe] how would this be done? type classes? existentialtypes?

Geest, G. van den G.vandenGeest at students.uu.nl
Thu Mar 16 07:37:36 EST 2006


Try using a GADT:

data Rs where
  Rs :: Resource a => a -> Rs

class Resource a where
    resourceName      :: a -> String

instance Resource String where
    resourceName x = "String"

instance Resource Int where
    resourceName x = "Int"

resName (Rs x) = resourceName x

resNames = map resName

test = resNames [Rs "Hi", Rs (1::Int) ]

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.

We hope this will help you,

Gerrit (and the rest of the ST-lab)




-----Original Message-----
From: haskell-cafe-bounces at haskell.org on behalf of Matthias Fischmann
Sent: Thu 3/16/2006 12:57 PM
To: haskell-cafe at haskell.org
Subject: [Haskell-cafe] how would this be done? type classes? existentialtypes?
 


hi,

this is one of those situations that always make scheme and perl
hackers laugh at me: i have written a piece of code that is
intuitively clear, and now i am trying to turn it into something that
compiles.  and here it goes.

i have a type class that looks something like this:

  class Resource a where
    resourceName      :: a -> String
    resourceAdvance   :: a -> a
    resourceStarved   :: a -> Bool
    resourceSpend     :: a -> Int -> a
    resourceEarn      :: a -> Int -> a

resource types are rice, crude oil, pizza, software code, and so on.
they all have a different internal structure and the same abstract
interface, that's why i have defined this type class.

now i want to create a list of a type similar to

  [r1, r2, r3] :: (Resource a) => [a]

but with r1 being pizza, r2 being crude oil, and so on.  my first idea
was this:

  data Rs = forall a . (Resource a) => Rs a
  unRs (Rs a) = a
  rsName :: Rs -> String
  rsName = resourceName . unRs
  ...

and then export Rs as an abstract data type.  this would allow for
lists of type [Rs], which is exactly what i want.

but what is the type of unRs?  or better: can i make it type at all?
and isn't this solution a little redundant and verbose?  should i do
it like in the example for existentially quantified types in the ghc
manual?

  http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html

but wouldnt't the code become really messy?  or should i do the type
class and instances, and then do Rs the existentially quantified way,
with all class methods arguments to the Rs constructor?  or is there a
completely different way to do this (besides using scheme or perl :-)?


thanks,
matthias




-----Original Message-----
From: haskell-cafe-bounces at haskell.org on behalf of Matthias Fischmann
Sent: Thu 3/16/2006 12:57 PM
To: haskell-cafe at haskell.org
Subject: [Haskell-cafe] how would this be done? type classes? existentialtypes?
 


hi,

this is one of those situations that always make scheme and perl
hackers laugh at me: i have written a piece of code that is
intuitively clear, and now i am trying to turn it into something that
compiles.  and here it goes.

i have a type class that looks something like this:

  class Resource a where
    resourceName      :: a -> String
    resourceAdvance   :: a -> a
    resourceStarved   :: a -> Bool
    resourceSpend     :: a -> Int -> a
    resourceEarn      :: a -> Int -> a

resource types are rice, crude oil, pizza, software code, and so on.
they all have a different internal structure and the same abstract
interface, that's why i have defined this type class.

now i want to create a list of a type similar to

  [r1, r2, r3] :: (Resource a) => [a]

but with r1 being pizza, r2 being crude oil, and so on.  my first idea
was this:

  data Rs = forall a . (Resource a) => Rs a
  unRs (Rs a) = a
  rsName :: Rs -> String
  rsName = resourceName . unRs
  ...

and then export Rs as an abstract data type.  this would allow for
lists of type [Rs], which is exactly what i want.

but what is the type of unRs?  or better: can i make it type at all?
and isn't this solution a little redundant and verbose?  should i do
it like in the example for existentially quantified types in the ghc
manual?

  http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html

but wouldnt't the code become really messy?  or should i do the type
class and instances, and then do Rs the existentially quantified way,
with all class methods arguments to the Rs constructor?  or is there a
completely different way to do this (besides using scheme or perl :-)?


thanks,
matthias

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20060316/236c9fa8/attachment-0001.htm


More information about the Haskell-Cafe mailing list