3
votes

I have been developing some code that uses Data.Array to use multidimensional arrays, now I want to put those arrays into a data type so I have something like this

data MyType = MyType { a :: Int, b :: Int, c :: Array }

Data.Array has type:

(Ix i, Num i, Num e) => Array i e

Where "e" can be of any type not just Num.

I am convinced I am missing a concept completely.

How do I accomplish this? What is special about the Data.Array type that is different from Int, Num, String etc?

Thanks for the help!

2
Where are you getting that context for Array? Num i, Num e don't look right to me.C. A. McCann

2 Answers

11
votes

Array is not a type. It's a type constructor. It has kind * -> * -> * which means that you give it two types to get a type back. You can sort of think of it like a function. Types like Int are of kind *. (Num is a type class, which is an entirely different thing).

You're declaring c to be a field of a record, i.e., c is a value. Values have to have a type of kind *. (There are actually a few more kinds for unboxed values but don't worry about that for now).

So you need to provide two type arguments to make a type for c. You can choose two concrete types, or you can add type arguments to MyType to allow the choice to be made elsewhere.

data MyType1 = MyType { a, b :: Int, c :: Array Foo Bar }
data MyType2 i e = MyType { a, b :: Int, c :: Array i e }

References

4
votes

You need to add the type variables i and e to your MyType:

data MyTYpe i e = MyType { a, b :: Int, c :: Array i e }