Boxed foreign prim

Edward Kmett ekmett at gmail.com
Fri Mar 9 05:12:37 CET 2012


I'm currently working with a lot of very short arrays of fixed length and
as a thought experiment I thought I would try to play with fast numeric
field accessors

In particular, I'd like to use something like foreign prim to do something
like

> foreign import prim "cmm_getField" unsafeField# :: a -> Int# -> b

> unsafeField :: a -> Int -> b
> unsafeField a (I# i) = a' `pseq` unsafeField# a' i -- the pseq could be
moved into the prim, I suppose.
>   where a' = unsafeCoerce a

> fst :: (a,b) -> a
> fst = unsafeField 0

> snd :: (a,b) -> b
> snd = unsafeField 1

This becomes more reasonable to consider when you are forced to make
something like

> data V4 a = V4 a a a a

using

> unsafeIndex (V4 a _ _ _) 0 = a
> unsafeIndex (V4 _ b _ _) 1 = b
> unsafeIndex (V4 _ _ c _) 2 = c
> unsafeIndex (V4 _ _ _ d) 3 = d

rather than

> unsafeIndex :: V4 a -> Int -> a
> unsafeIndex = unsafeField

But I can only pass unboxed types to foreign prim.

Is this an intrinsic limitation or just an artifact of the use cases that
have presented themselves to date?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20120308/3e2c19dd/attachment.htm>


More information about the Glasgow-haskell-users mailing list