[Haskell-cafe] repa Shape help

Ben Lippmeier benl at ouroborus.net
Mon May 9 12:06:08 CEST 2011


On 09/05/2011, at 15:31 , <briand at aracnet.com> <briand at aracnet.com> wrote:

> main = do
>  let x = A.fromList (AS.shapeOfList [2, 2]) ([1.0, 2.0, 3.0, 4.0]::[Double])
>  putStrLn $ show x
> 
> test_repa.hs:10:13:
>    Ambiguous type variable `sh' in the constraint:
>      `Shape sh' arising from a use of `show' at test_repa.hs:10:13-18
>    Probable fix: add a type signature that fixes these type variable(s)
> Failed, modules loaded: none.
> 
> After much staring at the type signatures I finally figured out that adding a type annotation to x of :
> 
>  :: Array DIM2 Double
> 
> would fix the problem, but I'm not completely clear as to why.

Because the GHC type system doesn't (yet) know that applying shapeOfList to a two element array should yield a DIM2.


> after all fromList is typed:
> 
> (Shape sh, Elt a) => sh -> [a] -> Array sh a
> 
> Since it knows [a] is [Double] and sh must be - well I'm not really clear on what sh is supposed to be.  therein lies my problem.  Although it does seem that sh can be darn near anything, which is probably why it was ambiguous.

Shape is dimensionally of the array. The only valid choices for sh are DIM1, DIM2, DIM3 etc for 1 dimensional, 2 dimensional etc arrays.


> At one point I had tried something like (2 :. 2) and got a whole host of errors for that too, except that DIM2 is defined in exactly that way, so it's not at all obvious why that didn't work.

Try (Z :. 2 :. 3). This is basically a list containing the column and row lengths. Similar to (3 : 2 : []), except that the list extends to the left instead of the right. The Z constructor is equivalent to [].


Cheers,
Ben.





More information about the Haskell-Cafe mailing list