Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: How To Fix Error With "add An Instance Declaration For (Unbox A)" When Using Unboxed Vectors?

Tags:

haskell

I have written some code in which a small part of the code takes a large one dimensional Unboxed.Vector and returns them as a Vector (Vector a).

A part of the code is giving an error. Here is a sample piece of code that is similar to the actual code and gives the same error.

import Data.Vector.Unboxed as D


xs = [0,1,2,3,4,5,6,7,8,9,10,11]
rows = 3
cols = 4

sb = D.fromList xs

takeRows::Int -> Int -> Vector Int -> Vector (Vector Int)
takeRows rows cols x0 = D.map (\x -> D.slice x (fromIntegral cols) x0) starts
                        where
                         starts = D.enumFromStepN 0 cols rows

-- takeRowsList::Int -> Int -> Vector Int -> [Vector Int]                                                         
-- takeRowsList rows cols x0 = Prelude.map (\x -> D.slice x (fromIntegral cols) x0) starts                        
--  where                                                                                  
--  starts = D.toList . D.enumFromStepN 0 cols $ rows                                    

the error is

No instance for (Unbox (Vector Int))
  arising from a use of `D.map'
Possible fix: add an instance declaration for (Unbox (Vector Int))
In the expression:
  D.map (\ x -> slice x (fromIntegral cols) x0) starts
In an equation for `takeRows':
    takeRows rows cols x0
      = D.map (\ x -> slice x (fromIntegral cols) x0) starts
      where
          starts = enumFromStepN 0 cols rows

I have written a similar function takeRowsList, which makes the outer Vector as a List and this doesn't suffer from the same problem. I've also included it above but commented it out, to demonstrate my problem.

I understand that some functions need type definitions, when, I use them with Unboxed Vectors. But in this case I am stumped as to where to put a type definition. I've tried pretty much type defining everything and I keep getting the above error.

Thanks in advance for your help.

like image 974
Manju Avatar asked Nov 19 '11 19:11

Manju


2 Answers

Unboxed vectors need to know the size of their elements, and that size must be constant. Vectors can have different sizes, so they can't be elements of unboxed vectors. They could be elements of boxed vectors, though, so if lists aren't good for what you do, you can make it a boxed vector (import qualified Data.Vector as B and qualify the relevant functions with B instead of D).

like image 144
Daniel Fischer Avatar answered Nov 16 '22 14:11

Daniel Fischer


You cannot have an unboxed vector contain another vector. Only certain primitive datatypes can be unboxed, namely the ones for which Unbox instances are defined. Vectors are not primitive datatypes.

What you can do is have your function return a normal (boxed) vector of unboxed vectors.

like image 2
hammar Avatar answered Nov 16 '22 14:11

hammar