4

Due to the answer to this question, I'd like to be able to construct instances of Foreign.Storable.Storable from instances of Data.Vector.Unboxed.Unbox (and vice versa). Is this possible?

The closest thing I'm aware of is vector-th-unbox, but this only creates an unboxable instance.

Community
  • 1
  • 1
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53

3 Answers3

4

Check the vector package.

Use Data.Vector.convert :: (Vector v a, Vector w a) => v a -> w a.

vivian
  • 1,434
  • 1
  • 10
  • 13
  • I can't seem to find the convert function you mention in the [vector package](http://hackage.haskell.org/packages/archive/vector/0.5/doc/html/Data-Vector.html). Also, based on the type signature I don't see how it would help? – Mike Izbicki May 31 '13 at 07:09
  • Look towards the very end of the `Data.Vector` module/documentation. – vivian May 31 '13 at 08:00
  • All I see is `toList` and `fromList` and a [hoogle search](http://www.haskell.org/hoogle/?hoogle=convert) brings up nothing. – Mike Izbicki May 31 '13 at 15:46
  • 1
    [convert is here](http://hackage.haskell.org/packages/archive/vector/0.10.0.1/doc/html/Data-Vector-Generic.html#v:convert) – nh2 Sep 08 '13 at 10:13
1

As vivian has already answered you can use the convert function that is available in the Data.Vector.Generic module: http://hackage.haskell.org/package/vector/docs/Data-Vector-Generic.html#v:convert

fho
  • 6,787
  • 26
  • 71
0

I can imagine a terribly hacky way to go from unbox to storable, but it is genuinely hacky and inefficient! In fact, having worked through it, I don't actually want to write it out. The notion is that all you can do with an Unbox is work with it in an unboxed vector. Therefore you can initialize an unboxed vector with a singleton element. Given said element, you can create a vector holding it, then pry apart your Vector by evil, and use the bytes directly for your Storable instance. You can equally evilly go the other way.

But I don't know why you'd like to do this. Most data types that are Storable by default are also Unbox and vice versa. And for those that they aren't, deriving Storable is easy enough -- either manually, or with a tool.

sclv
  • 38,665
  • 7
  • 99
  • 204