I've got a numeric application that does a lot of work with negative logs of probabilities, which (since probabilities range from zero to one) take the values of positive doubles, or negative infinity (if the underlying probability was zero).
I'm using these with a newtype Score
as follows:
newtype Score = Score Double
deriving (Eq, Ord)
-- ^ A "score" is the negated logarithm of a probability
negLogZero :: Score -- ^ Stands in for - log 0
negLogZero = Score 10e1024
negLogOne :: Score -- ^ - log 1
negLogOne = Score 0.0
unScore :: Score -> Double
unScore (Score x) = x
instance Show Score where
show (Score x) = show x
Now, in an implementation of the Viterbi algorithm, I've been using Data.Vector
a lot, and indeed I have some Data.Vector
s of Score
s. While trying to do some performance tuning, I decided to try using Data.Vector.Unboxed
. However, I need to write an instance for Unbox
, which cannot be derived, and I can't quite figure out what I need to do (particularly, what the contract for the Unbox
typeclass is). Since Score
is really a Double
with some useful constructors and semantics, this should be possible, I'd think. As far as I can tell, I need to be able to tell Data.Vector.Unboxed
how big each slot in a vector of Score
s must be, and I guess how to read and write them (but heck, they're a lot like Double
s).
So, what do I do? Thanks!
Could not coerce from ‘Data.Vector.Primitive.Vector Double’ to ‘U.Vector Score’ because ‘Data.Vector.Primitive.Vector Double’ and ‘U.Vector Score’ are different types. arising from the coercion of the method ‘Data.Vector.Generic.Base.basicLength’ from type ‘U.Vector Double -> Int’ to type ‘U.Vector Score -> Int’ Possible fix: use a standalone 'deriving instance' declaration, so you can specify the instance context yourself When deriving the instance for (Vector U.Vector Score)
– Fidelis