3

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!

1
  • 2
    Where are you getting that context for Array? Num i, Num e don't look right to me. Commented Jul 30, 2011 at 16:34

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

Comments

4

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 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.