开发者

Using Data.Array in a Haskell Data Type

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开发者_StackOverflow 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!


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

  • Kinds for C++ users.
  • Kind (type theory) on Wikipedia.


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 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜