I want to store something like an Either type in a Data.Vector.Unboxed.Unbox vector. I assume I should create a Unbox instance for "Either a b" where a and b are Unbox (ignoring the orphan issue for now). What's the best way of doing this? Should I store it the same way as (Bool,a,b)? I'm much more concerned about speed than space.
Asked
Active
Viewed 226 times
4
-
One option is to use three vectors, one of Bools, one of a's and one of b's. Of course, your types `a` and `b` have to some default value. – ErikR Feb 22 '14 at 02:05
-
Right, this is how the Vector code handles (Bool,a,b). Is there an easy way to get a default value for something that's an instance of Unbox? – Ashley Yakeley Feb 22 '14 at 02:10
-
I don't see that `Unbox` requires a default value. To make your code generic you might have to introduce your own `DefaultValue` type class to define the defaults. Or define a data structure like `data EitherVector a b = (a,b,Vector (Bool,a,b))`. – ErikR Feb 22 '14 at 02:30
-
1See my answer [here](http://stackoverflow.com/questions/21896924/creating-custom-instance-of-uarray/21897900#21897900) – crockeea Feb 22 '14 at 04:50
1 Answers
1
To store sum types in unboxed structures you'll need to use the product encoding of them. E.g. as a tuple of the type tag and the pair of values.
So:
Unbox a, Unbox b => Either a b
-->
Unbox (Int, a, b)
With appropriate default values for the "empty" slots for a and b.

Don Stewart
- 137,316
- 36
- 365
- 468