[Haskell-beginners] Class definition syntax

Shawn Willden shawn-haskell at willden.org
Sat Oct 31 22:36:26 EDT 2009


On Saturday 31 October 2009 10:50:10 am Daniel Fischer wrote:
> Or perhaps he should look at the class IArray from Data.Array.IArray, maybe
> he can just declare instances of IArray for his datatypes.
> Without more information, I can't tell which way to go.

Looking into the idea of declaring my types as IArray instances, there's one 
immediate problem:  IArray's only method is "bounds".  All of the functions 
that I want as methods of my class are functions in the IArray module (if I'm 
reading it correctly).

So, it seems like what I want to do is to subclass IArray and add the 
additional methods.  Then I can declare instances for my various types and 
define the methods appropriately.

So, I wrote this:

------------------------------------
import Data.Ix (Ix, inRange)
import qualified Data.Array.IArray (IArray, 
                                    Array,
                                    array,
                                    listArray,
                                    range,
                                    bounds,
                                    (!))

listArray   = Data.Array.IArray.listArray
array       = Data.Array.IArray.array              

class (Data.Array.IArray.IArray a e) => MyArray a e where
    bounds :: Ix i => a i e -> (i,i)
    range  :: Ix i => a i e -> [i]
    (!)    :: Ix i => a i e -> i -> e
    (//)   :: Ix i => a i e -> [(i,e)]

type Location = (Int, Int)
newtype Board = Board (Data.Array.IArray.Array Location Int)

instance MyArray Board where
    bounds = Data.Array.IArray.bounds
    (!)    = (Data.Array.IArray.!)
--------------------------------------

However, the instance declaration gives me a "kind mis-match" error.  It says 
that it expects kind '* -> * -> *', but Board has kind '*'.

So, I tried:

instance MyArray (Board Data.Array.IArray.Array Location Int) where

and other variations on that, but they all give me "Board is applied to too 
many type arguments".

How should this be written?

Thanks,

	Shawn.


More information about the Beginners mailing list