Alson Kemp | 1 Mar 01:26 2006

Help with Type Class

  Although the discussion about Array refactoring died
down quickly on the Haskell' mailing list, I've been
noodling on refactoring the various Collections in
Haskell.  In doing so, I've bumped into a problem with
type classes that I can't resolve.  The issue is as
follows:

  I'm designing a Collections class heirarchy that is
a blend between that of Java and of Haskell. The
problem is that, whereas in OOP it is easy to do so,
Haskell class mechanism seems to make it difficult to
add *new* member variable to subclasses.

  Simple goal: create a top-level Collection with an
*indexed* Array subclass (and various other
subclasses).  The problem I'm running into is
Collection has no need of an "index" variable and I
can't seem to figure out how to add an "index" to
Array when subclassing from Collection  Mock up:

--
  class CollectionClass c e where
    -- every Collection supports toList...
    toList :: c e -> [e]

  class (CollectionClass a e)=> ArrayClass a e where
...

  data Array i e = Array i i 

(Continue reading)

Bulat Ziganshin | 1 Mar 11:53 2006
Picon

Re: Help with Type Class

Hello Alson,

Wednesday, March 1, 2006, 3:26:44 AM, you wrote:

AK>   class CollectionClass c e where

this works with -fglasgow-exts :

class CollectionClass c e where
  toList :: c e -> [e]

data Array i e = Array i i

instance CollectionClass (Array i) e where
  toList a = []

btw, see chapter 7.1.1 in the
http://cvs.haskell.org/Hugs/pages/hugsman/exts.html

--

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin <at> gmail.com
Daniel Fischer | 1 Mar 15:48 2006
Picon

Re: Help with Type Class

Am Mittwoch, 1. März 2006 01:26 schrieb Alson Kemp:
>   Although the discussion about Array refactoring died
> down quickly on the Haskell' mailing list, I've been
> noodling on refactoring the various Collections in
> Haskell.  In doing so, I've bumped into a problem with
> type classes that I can't resolve.  The issue is as
> follows:
>
>   I'm designing a Collections class heirarchy that is
> a blend between that of Java and of Haskell. The
> problem is that, whereas in OOP it is easy to do so,
> Haskell class mechanism seems to make it difficult to
> add *new* member variable to subclasses.
>
>   Simple goal: create a top-level Collection with an
> *indexed* Array subclass (and various other
> subclasses).  The problem I'm running into is
> Collection has no need of an "index" variable and I
> can't seem to figure out how to add an "index" to
> Array when subclassing from Collection  Mock up:
>
> --
>   class CollectionClass c e where
>     -- every Collection supports toList...
>     toList :: c e -> [e]
>
>   class (CollectionClass a e)=> ArrayClass a e where
> ...
>
>   data Array i e = Array i i
(Continue reading)

Alson Kemp | 1 Mar 20:20 2006

Re: Help with Type Class

Bulat,

BZ>AK>   class CollectionClass c e where
BZ>this works with -fglasgow-exts :
Yup.  Figured that out shortly after I e-mailed... 
Thank you for providing the extra detail, though. 
-fglasgow-exts fixed the problem, but I didn't have a
good idea why the problem went away.

Daniel,

> >   data Array i e = Array i i
> shouldn't the element type appear here? 
Typo: data Array i e = Array i i [e] --mockup with
list

> Well, the parameter c of CollectionClass has kind (*
> -> *), Array has kind 
> (* -> * -> *), so it must be
> instance CollectionClass (Array i) e where ...
... which (I think) was breaking until -fglasgow-exts.
 Once that was fixed, this seems to be the preferred
way to implement this function.

> But the element type doesn't really belong in the
> class, wouldn't class Collection c where
>     toList :: c e -> [e]
Hmmm...  For example, implementing Array and UArray as
subclasses of Collection:
  class Collection c where
(Continue reading)

Daniel Fischer | 2 Mar 15:46 2006
Picon

Re: Help with Type Class

Am Mittwoch, 1. März 2006 20:20 schrieben Sie:
> Daniel,
>
> > >   data Array i e = Array i i
> >
> > shouldn't the element type appear here?
>
> Typo: data Array i e = Array i i [e] --mockup with
> list
>
> > Well, the parameter c of CollectionClass has kind (*
> > -> *), Array has kind
> > (* -> * -> *), so it must be
> > instance CollectionClass (Array i) e where ...
>
> ... which (I think) was breaking until -fglasgow-exts.

Well, Haskell98 doesn't allow multiparameter type classes, so extensions are 
required.

>  Once that was fixed, this seems to be the preferred
> way to implement this function.
>
> > But the element type doesn't really belong in the
> > class, wouldn't class Collection c where
> >     toList :: c e -> [e]
>
> Hmmm...  For example, implementing Array and UArray as
> subclasses of Collection:
>   class Collection c where
(Continue reading)


Gmane