[Haskell] concurrent haskell, higher-order types and parameterizing by typeclass

2004-04-13 Thread Abraham Egnor
I have a system composed of threads communicating via channels; I'd
like the communicated types to be instances of a particular typeclass
(say Show for purposes of discussion), but not all the same type. 
Since Chan is paramterized by the communicated datatype, the best way
I've found to do this is via a wrapper type:

data Showable = forall a. Show a = Showable a

writer :: Chan Showable - IO ()
writer ch = mapM_ (writeChan ch) [Showable 42, Showable pi, Showable
hello, Showable 'c']

printer :: Chan Showable - IO ()
printer ch = getChanContents ch = mapM_ (\(Showable a) - print a)

However, this solution requires a new wrapper datatype (or at least a
new constructor) to be defined for every typeclass to be used in
Chan-based communication; furthermore, all of the datatypes will be
identical except for the name of the typeclass.  It seems like I
should be able to create a type that's parameterized by typeclass,
i.e. something like:

data Wrapper c = forall a. c a = Wrapper a

writer :: Chan (Wrapper Show) - IO ()
...

Alternatively, are there any ideas as to how I could communicate
heterogeneous instances of a given typeclass in a typesafe manner
(i.e. without escaping to Data.Dynamic)?

Abe
___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] concurrent haskell, higher-order types and parameterizing by typeclass

2004-04-13 Thread Sebastian Sylvan


Abraham Egnor wrote:

However, this solution requires a new wrapper datatype (or at least a
new constructor) to be defined for every typeclass to be used in
Chan-based communication; furthermore, all of the datatypes will be
identical except for the name of the typeclass.  It seems like I
should be able to create a type that's parameterized by typeclass,
i.e. something like:
data Wrapper c = forall a. c a = Wrapper a

writer :: Chan (Wrapper Show) - IO ()
...
Ah yes, well that won't work since c is supposed to be a type and Show 
is a type class. And if types could be refered to by type classes for 
which they have an instance, the problem would be solved!

However I agree that Haskell desparately needs this sort of thing. If I 
want to have a list of Showable values that should be allowed. 
Moreover I don't even think the programmer should have to wrap it up 
inside a Showable data type to use it either. A value such as 
[1,hello,(hello,12)] should automatically be allowed, and the type 
should be inferred by simply taking the intersection of all of the type 
classes for which each element has an instance. Some new syntax might be 
required to group several type classes into one type, perhaps. The 
type of a several type classes could be something like :Show:Eq: and 
:Show:Ord:Num:. That should then be valid to use in the same manner as 
any other type (list of, tuples etc.). One exception though! A 
type-class-type shouldn't be able to be an instance of a type class 
(that could surely lead to some very weird behaviour)!

I'm sure this has been brought up to discussion several times before 
though (although I have missed it).

I really do think this should be a standard feature in Haskell. Yes you 
can get the same functionality by using existential types, but as the 
orignal poster noted it gets tedious very fast, and it's such a common 
feature for programming languages that I don't see why Haskell doesn't 
have it.

/Sebastian Sylvan

--
Clothes make the man. Naked people have little or no influence on society.
- Mark Twain
___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell