{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module BishBosh.Type.Mass(
Base,
CriterionWeight,
CriterionValue,
WeightedMean,
RankValue,
PieceSquareValue(),
) where
#ifdef USE_NEWTYPE_WRAPPERS
import qualified BishBosh.Data.Exception as Data.Exception
import qualified BishBosh.Data.Num as Data.Num
import qualified Control.DeepSeq
import qualified Control.Exception
#endif
#ifdef USE_PRECISION
import BishBosh.Data.Ratio()
#else
import qualified Text.XML.HXT.Arrow.Pickle as HXT
#endif
type Base =
#ifdef USE_PRECISION
Rational
#else /* Floating-point */
# ifdef USE_NARROW_NUMBERS
Float
instance HXT.XmlPickler Float where
xpickle = HXT.xpPrim
# else
Double
instance HXT.XmlPickler Double where
xpickle :: PU Double
xpickle = PU Double
forall a. (Read a, Show a) => PU a
HXT.xpPrim
# endif
#endif
type CriterionWeight = Base
type CriterionValue = CriterionWeight
type WeightedMean = CriterionValue
type RankValue = Base
#ifdef USE_NEWTYPE_WRAPPERS
newtype PieceSquareValue = MkPieceSquareValue Base deriving (Eq, Ord)
instance Show PieceSquareValue where
showsPrec precision (MkPieceSquareValue pieceSquareValue) = showsPrec precision pieceSquareValue
instance Num PieceSquareValue where
MkPieceSquareValue l + MkPieceSquareValue r = mkPieceSquareValue $! l + r
MkPieceSquareValue l * MkPieceSquareValue r = MkPieceSquareValue $! l * r
abs (MkPieceSquareValue pieceSquareValue) = MkPieceSquareValue $! abs pieceSquareValue
signum (MkPieceSquareValue pieceSquareValue) = MkPieceSquareValue $! signum pieceSquareValue
fromInteger = mkPieceSquareValue . fromInteger
negate (MkPieceSquareValue pieceSquareValue) = mkPieceSquareValue $! negate pieceSquareValue
instance Fractional PieceSquareValue where
MkPieceSquareValue l / MkPieceSquareValue r = mkPieceSquareValue $! l / r
fromRational = mkPieceSquareValue . fromRational
instance Real PieceSquareValue where
toRational (MkPieceSquareValue pieceSquareValue) = toRational pieceSquareValue
instance Control.DeepSeq.NFData PieceSquareValue where
rnf (MkPieceSquareValue pieceSquareValue) = Control.DeepSeq.rnf pieceSquareValue
mkPieceSquareValue :: Base -> PieceSquareValue
mkPieceSquareValue pieceSquareValue
| Data.Num.inClosedUnitInterval pieceSquareValue = MkPieceSquareValue pieceSquareValue
| otherwise = Control.Exception.throw . Data.Exception.mkOutOfBounds . showString "BishBosh.Type.Mass.mkPieceSquareValue:\t" $ shows pieceSquareValue " must be within the closed unit-interval [0,1]."
#else
type PieceSquareValue = Base
#endif