{-# LANGUAGE CPP #-}
module BishBosh.Metric.RankValue(
#ifdef USE_NEWTYPE_WRAPPERS
RankValue(
),
#else
RankValue,
#endif
tag
) where
import qualified BishBosh.Type.Mass as Type.Mass
#ifdef USE_NEWTYPE_WRAPPERS
import qualified BishBosh.Data.Exception as Data.Exception
import qualified BishBosh.Data.Num as Data.Num
import qualified BishBosh.Property.ShowFloat as Property.ShowFloat
import qualified Control.Arrow
import qualified Control.DeepSeq
import qualified Control.Exception
import qualified Data.List.Extra
import qualified Text.XML.HXT.Arrow.Pickle as HXT
#endif
tag :: String
tag :: String
tag = String
"rankValue"
#ifdef USE_NEWTYPE_WRAPPERS
newtype RankValue = MkRankValue {
deconstruct :: Type.Mass.RankValue
} deriving (Eq, Ord)
instance Show RankValue where
showsPrec precision MkRankValue { deconstruct = rankValue } = showsPrec precision rankValue
instance Read RankValue where
readsPrec precision = map (Control.Arrow.first mkRankValue) . readsPrec precision . Data.List.Extra.trimStart
instance Property.ShowFloat.ShowFloat RankValue where
showsFloat fromDouble MkRankValue { deconstruct = rankValue } = fromDouble $! realToFrac rankValue
instance Num RankValue where
MkRankValue { deconstruct = l } + MkRankValue { deconstruct = r } = mkRankValue $! l + r
MkRankValue { deconstruct = l } * MkRankValue { deconstruct = r } = MkRankValue $! l * r
abs MkRankValue { deconstruct = rankValue } = MkRankValue $! abs rankValue
signum MkRankValue { deconstruct = rankValue } = MkRankValue $! signum rankValue
fromInteger = mkRankValue . fromInteger
negate MkRankValue { deconstruct = rankValue } = mkRankValue $! negate rankValue
instance Fractional RankValue where
MkRankValue { deconstruct = l } / MkRankValue { deconstruct = r } = mkRankValue $! l / r
fromRational = mkRankValue . fromRational
instance Real RankValue where
toRational MkRankValue { deconstruct = rankValue } = toRational rankValue
instance Control.DeepSeq.NFData RankValue where
rnf MkRankValue { deconstruct = rankValue } = Control.DeepSeq.rnf rankValue
instance HXT.XmlPickler RankValue where
xpickle = HXT.xpWrap (mkRankValue, deconstruct) $! HXT.xpAttr tag HXT.xpickle
mkRankValue :: Type.Mass.RankValue -> RankValue
mkRankValue rankValue
| Data.Num.inClosedUnitInterval rankValue = MkRankValue rankValue
| otherwise = Control.Exception.throw . Data.Exception.mkOutOfBounds . showString "BishBosh.Metric.RankValue.mkRankValue:\t" $ shows rankValue " must be within the closed unit-interval [0,1]."
#else
type RankValue = Type.Mass.Base
#endif