Commits

Michael Snoyman committed df95bcc

Support for binary 0.5

Comments (0)

Files changed (20)

Statistics/Distribution/Beta.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  Statistics.Distribution.Beta
   incompleteBeta, invIncompleteBeta, logBeta, digamma)
 import Numeric.MathFunctions.Constants (m_NaN)
 import qualified Statistics.Distribution as D
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 -- | The beta distribution
 data BetaDistribution = BD
    -- ^ Beta shape parameter
  } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary BetaDistribution
+instance Binary BetaDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (BD x y) = put x >> put y
+    get = BD <$> get <*> get
+#endif
 
 -- | Create beta distribution. Both shape parameters must be positive.
 betaDistr :: Double             -- ^ Shape parameter alpha

Statistics/Distribution/Binomial.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Binomial
 -- Copyright : (c) 2009 Bryan O'Sullivan
 import qualified Statistics.Distribution.Poisson.Internal as I
 import Numeric.SpecFunctions (choose,incompleteBeta)
 import Numeric.MathFunctions.Constants (m_epsilon)
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 
 -- | The binomial distribution.
     -- ^ Probability.
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary BinomialDistribution
+instance Binary BinomialDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (BD x y) = put x >> put y
+    get = BD <$> get <*> get
+#endif
 
 instance D.Distribution BinomialDistribution where
     cumulative = cumulative

Statistics/Distribution/CauchyLorentz.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.CauchyLorentz
 -- Copyright : (c) 2011 Aleksey Khudyakov
 import Data.Data (Data, Typeable)
 import GHC.Generics (Generic)
 import qualified Statistics.Distribution as D
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 -- | Cauchy-Lorentz distribution.
 data CauchyDistribution = CD {
   }
   deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance Binary CauchyDistribution
+instance Binary CauchyDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (CD x y) = put x >> put y
+    get = CD <$> get <*> get
+#endif
 
 -- | Cauchy distribution
 cauchyDistribution :: Double    -- ^ Central point

Statistics/Distribution/ChiSquared.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.ChiSquared
 -- Copyright : (c) 2010 Alexey Khudyakov
 
 import qualified Statistics.Distribution         as D
 import qualified System.Random.MWC.Distributions as MWC
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+#endif
 
 
 -- | Chi-squared distribution
 newtype ChiSquared = ChiSquared Int
                      deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary ChiSquared
+instance Binary ChiSquared where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = fmap ChiSquared get
+    put (ChiSquared x) = put x
+#endif
 
 -- | Get number of degrees of freedom
 chiSquaredNDF :: ChiSquared -> Int

Statistics/Distribution/Exponential.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Exponential
 -- Copyright : (c) 2009 Bryan O'Sullivan
 import qualified Statistics.Sample               as S
 import qualified System.Random.MWC.Distributions as MWC
 import Statistics.Types (Sample)
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+#endif
 
 
 newtype ExponentialDistribution = ED {
       edLambda :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary ExponentialDistribution
+instance Binary ExponentialDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put = put . edLambda
+    get = fmap ED get
+#endif
 
 instance D.Distribution ExponentialDistribution where
     cumulative      = cumulative

Statistics/Distribution/FDistribution.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.FDistribution
 -- Copyright : (c) 2011 Aleksey Khudyakov
 import qualified Statistics.Distribution as D
 import Numeric.SpecFunctions (
   logBeta, incompleteBeta, invIncompleteBeta, digamma)
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 
 
                        }
                    deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance Binary FDistribution
+instance Binary FDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = F <$> get <*> get <*> get
+    put (F x y z) = put x >> put y >> put z
+#endif
 
 fDistribution :: Int -> Int -> FDistribution
 fDistribution n m

Statistics/Distribution/Gamma.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Gamma
 -- Copyright : (c) 2009, 2011 Bryan O'Sullivan
 import Statistics.Distribution.Poisson.Internal  as Poisson
 import qualified Statistics.Distribution         as D
 import qualified System.Random.MWC.Distributions as MWC
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 -- | The gamma distribution.
 data GammaDistribution = GD {
     , gdScale :: {-# UNPACK #-} !Double -- ^ Scale parameter, &#977;.
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary GammaDistribution
+instance Binary GammaDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (GD x y) = put x >> put y
+    get = GD <$> get <*> get
+#endif
 
 -- | Create gamma distribution. Both shape and scale parameters must
 -- be positive.

Statistics/Distribution/Geometric.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Geometric
 -- Copyright : (c) 2009 Bryan O'Sullivan
 import Numeric.MathFunctions.Constants(m_pos_inf,m_neg_inf)
 import qualified Statistics.Distribution         as D
 import qualified System.Random.MWC.Distributions as MWC
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>))
+#endif
 
 ----------------------------------------------------------------
 -- Distribution over [1..]
       gdSuccess :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary GeometricDistribution
+instance Binary GeometricDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = GD <$> get
+    put (GD x) = put x
+#endif
 
 instance D.Distribution GeometricDistribution where
     cumulative = cumulative
       gdSuccess0 :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary GeometricDistribution0
+instance Binary GeometricDistribution0 where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = GD0 <$> get
+    put (GD0 x) = put x
+#endif
 
 instance D.Distribution GeometricDistribution0 where
     cumulative (GD0 s) x = cumulative (GD s) (x + 1)

Statistics/Distribution/Hypergeometric.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Hypergeometric
 -- Copyright : (c) 2009 Bryan O'Sullivan
 import Numeric.MathFunctions.Constants (m_epsilon)
 import Numeric.SpecFunctions (choose)
 import qualified Statistics.Distribution as D
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 data HypergeometricDistribution = HD {
       hdM :: {-# UNPACK #-} !Int
     , hdK :: {-# UNPACK #-} !Int
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary HypergeometricDistribution
+instance Binary HypergeometricDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = HD <$> get <*> get <*> get
+    put (HD x y z) = put x >> put y >> put z
+#endif
 
 instance D.Distribution HypergeometricDistribution where
     cumulative = cumulative

Statistics/Distribution/Normal.hs

-{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Normal
 -- Copyright : (c) 2009 Bryan O'Sullivan
 import qualified Statistics.Distribution as D
 import qualified Statistics.Sample       as S
 import qualified System.Random.MWC.Distributions as MWC
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 
 
     , ndCdfDenom :: {-# UNPACK #-} !Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary NormalDistribution
+instance Binary NormalDistribution where
+    put (ND w x y z) = put w >> put x >> put y >> put z
+    get = ND <$> get <*> get <*> get <*> get
 
 instance D.Distribution NormalDistribution where
     cumulative      = cumulative

Statistics/Distribution/Poisson.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Poisson
 -- Copyright : (c) 2009, 2011 Bryan O'Sullivan
 import qualified Statistics.Distribution.Poisson.Internal as I
 import Numeric.SpecFunctions (incompleteGamma,logFactorial)
 import Numeric.MathFunctions.Constants (m_neg_inf)
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+#endif
 
 
 newtype PoissonDistribution = PD {
       poissonLambda :: Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary PoissonDistribution
+instance Binary PoissonDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = fmap PD get
+    put = put . poissonLambda
+#endif
 
 instance D.Distribution PoissonDistribution where
     cumulative (PD lambda) x
 poisson :: Double -> PoissonDistribution
 poisson l
   | l >=  0   = PD l
-  | otherwise = error $ "Statistics.Distribution.Poisson.poisson:\
-                        \ lambda must be non-negative. Got " ++ show l
+  | otherwise = error $
+    "Statistics.Distribution.Poisson.poisson: lambda must be non-negative. Got "
+    ++ show l
 {-# INLINE poisson #-}
 
 -- $references

Statistics/Distribution/StudentT.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.StudentT
 -- Copyright : (c) 2011 Aleksey Khudyakov
 import Statistics.Distribution.Transform (LinearTransform (..))
 import Numeric.SpecFunctions (
   logBeta, incompleteBeta, invIncompleteBeta, digamma)
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+#endif
 
 -- | Student-T distribution
 newtype StudentT = StudentT { studentTndf :: Double }
                    deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance Binary StudentT
+instance Binary StudentT where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put = put . studentTndf
+    get = fmap StudentT get
+#endif
 
 -- | Create Student-T distribution. Number of parameters must be positive.
 studentT :: Double -> StudentT

Statistics/Distribution/Transform.hs

 {-# LANGUAGE DeriveDataTypeable, DeriveGeneric, FlexibleContexts,
-    FlexibleInstances, UndecidableInstances #-}
+    FlexibleInstances, UndecidableInstances, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Transform
 -- Copyright : (c) 2013 John McDonnell;
 import GHC.Generics (Generic)
 import Data.Functor          ((<$>))
 import qualified Statistics.Distribution as D
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<*>))
+#endif
 
 -- | Linear transformation applied to distribution.
 --
     -- ^ Distribution being transformed.
   } deriving (Eq, Show, Read, Typeable, Data, Generic)
 
-instance (Binary d) => Binary (LinearTransform d)
+instance (Binary d) => Binary (LinearTransform d) where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = LinearTransform <$> get <*> get <*> get
+    put (LinearTransform x y z) = put x >> put y >> put z
+#endif
 
 -- | Apply linear transformation to distribution.
 scaleAround :: Double           -- ^ Fixed point

Statistics/Distribution/Uniform.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, CPP #-}
 -- |
 -- Module    : Statistics.Distribution.Uniform
 -- Copyright : (c) 2011 Aleksey Khudyakov
 import GHC.Generics (Generic)
 import qualified Statistics.Distribution as D
 import qualified System.Random.MWC       as MWC
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 
 -- | Uniform distribution from A to B
     , uniformB :: {-# UNPACK #-} !Double -- ^ Upper boundary of distribution
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary UniformDistribution
+instance Binary UniformDistribution where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (UniformDistribution x y) = put x >> put y
+    get = UniformDistribution <$> get <*> get
+#endif
 
 -- | Create uniform distribution.
 uniformDistr :: Double -> Double -> UniformDistribution

Statistics/Math/RootFinding.hs

-{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric, CPP #-}
 
 -- |
 -- Module    : Statistics.Math.RootFinding
 import Control.Monad       (MonadPlus(..), ap)
 import Data.Data (Data, Typeable)
 import GHC.Generics (Generic)
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Data.Binary.Put (putWord8)
+import Data.Binary.Get (getWord8)
+#endif
 
 
 -- | The result of searching for a root of a mathematical function.
             -- ^ A root was successfully found.
               deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance (Binary a) => Binary (Root a)
+instance (Binary a) => Binary (Root a) where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put NotBracketed = putWord8 0
+    put SearchFailed = putWord8 1
+    put (Root a) = putWord8 2 >> put a
+
+    get = do
+        i <- getWord8
+        case i of
+            0 -> return NotBracketed
+            1 -> return SearchFailed
+            2 -> fmap Root get
+            _ -> fail $ "Root.get: Invalid value: " ++ show i
+#endif
 
 instance Functor Root where
     fmap _ NotBracketed = NotBracketed

Statistics/Resampling.hs

-{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric #-}
+{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric, CPP #-}
 
 -- |
 -- Module    : Statistics.Resampling
       fromResample :: U.Vector Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Resample
+instance Binary Resample where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put = put . fromResample
+    get = fmap Resample get
+#endif
 
 -- | /O(e*r*s)/ Resample a data set repeatedly, with replacement,
 -- computing each estimate over the resampled data.

Statistics/Resampling/Bootstrap.hs

 {-# LANGUAGE DeriveDataTypeable, DeriveGeneric, OverloadedStrings,
-    RecordWildCards #-}
+    RecordWildCards, CPP #-}
 
 -- |
 -- Module    : Statistics.Resampling.Bootstrap
 import Statistics.Sample (mean)
 import Statistics.Types (Estimator, Sample)
 import qualified Data.Vector.Unboxed as U
+#if !MIN_VERSION_binary(0, 6, 0)
+import Data.Binary (put, get)
+import Control.Applicative ((<$>), (<*>))
+#endif
 
 -- | A point and interval estimate computed via an 'Estimator'.
 data Estimate = Estimate {
     -- ^ Confidence level of the confidence intervals.
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Estimate
+instance Binary Estimate where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (Estimate w x y z) = put w >> put x >> put y >> put z
+    get = Estimate <$> get <*> get <*> get <*> get
+#endif
 instance NFData Estimate
 
 -- | Multiply the point, lower bound, and upper bound in an 'Estimate'

Statistics/Sample/KernelDensity/Simple.hs

-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, FlexibleContexts #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, FlexibleContexts, CPP #-}
 -- |
 -- Module    : Statistics.Sample.KernelDensity.Simple
 -- Copyright : (c) 2009 Bryan O'Sullivan
       fromPoints :: U.Vector Double
     } deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Points
+instance Binary Points where
+#if !MIN_VERSION_binary(0, 6, 0)
+    get = fmap Points get
+    put = put . fromPoints
+#endif
 
 -- | Bandwidth estimator for an Epanechnikov kernel.
 epanechnikovBW :: Double -> Bandwidth

Statistics/Sample/Powers.hs

 {-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric,
-    FlexibleContexts #-}
+    FlexibleContexts, CPP #-}
 -- |
 -- Module    : Statistics.Sample.Powers
 -- Copyright : (c) 2009, 2010 Bryan O'Sullivan
 newtype Powers = Powers (U.Vector Double)
     deriving (Eq, Read, Show, Typeable, Data, Generic)
 
-instance Binary Powers
+instance Binary Powers where
+#if !MIN_VERSION_binary(0, 6, 0)
+    put (Powers v) = put v
+    get = fmap Powers get
+#endif
 
 -- | O(/n/) Collect the /n/ simple powers of a sample.
 --
     Statistics.Test.Internal
   build-depends:
     base < 5,
-    binary >= 0.6.3.0,
+    binary >= 0.5.1.0,
     deepseq >= 1.1.0.2,
     erf,
     monad-par         >= 0.3.4,