{-

Copyright (c) 2013 Lukas Mai

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of his contributors
  may be used to endorse or promote products derived from this software
  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY LUKAS MAI AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-}

{-# LANGUAGE CPP #-}

#define HAVE_GHC_GENERICS (__GLASGOW_HASKELL__ >= 704)

#if HAVE_GHC_GENERICS
{-# LANGUAGE DefaultSignatures, TypeOperators, FlexibleContexts #-}
#endif

module Data.Default.Class (
-- | This module defines a class for types with a default value.
-- It also defines 'Default' instances for the types 'Int', 'Int8',
-- 'Int16', 'Int32', 'Int64', 'Word', 'Word8', 'Word16', 'Word32', 'Word64',
-- 'Integer', 'Float', 'Double', 'Ratio', 'Complex', 'CShort', 'CUShort',
-- 'CInt', 'CUInt', 'CLong', 'CULong', 'CLLong', 'CULLong', 'CPtrdiff',
-- 'CSize', 'CSigAtomic', 'CIntPtr', 'CUIntPtr', 'CIntMax', 'CUIntMax',
-- 'CClock', 'CTime', 'CUSeconds', 'CSUSeconds', 'CFloat', 'CDouble', '(->)',
-- 'IO', 'Maybe', '()', '[]', 'Ordering', 'Any', 'All', 'Last', 'First', 'Sum',
-- 'Product', 'Endo', 'Dual', and tuples.
    Default(..)
) where

import Data.Int
import Data.Word
import Data.Monoid
import Data.Ratio
import Data.Complex
import Foreign.C.Types

#if HAVE_GHC_GENERICS
import GHC.Generics

class GDefault f where
    gdef :: f a

instance GDefault U1 where
    gdef :: U1 a
gdef = U1 a
forall k (p :: k). U1 p
U1

instance (Default a) => GDefault (K1 i a) where
    gdef :: K1 i a a
gdef = a -> K1 i a a
forall k i c (p :: k). c -> K1 i c p
K1 a
forall a. Default a => a
def

instance (GDefault a, GDefault b) => GDefault (a :*: b) where
    gdef :: (:*:) a b a
gdef = a a
forall (f :: * -> *) a. GDefault f => f a
gdef a a -> b a -> (:*:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: b a
forall (f :: * -> *) a. GDefault f => f a
gdef

instance (GDefault a) => GDefault (M1 i c a) where
    gdef :: M1 i c a a
gdef = a a -> M1 i c a a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 a a
forall (f :: * -> *) a. GDefault f => f a
gdef
#endif

-- | A class for types with a default value.
class Default a where
    -- | The default value for this type.
    def :: a

#if HAVE_GHC_GENERICS
    default def :: (Generic a, GDefault (Rep a)) => a
    def = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
to Rep a Any
forall (f :: * -> *) a. GDefault f => f a
gdef
#endif

instance Default Int where def :: Int
def = 0
instance Default Int8 where def :: Int8
def = 0
instance Default Int16 where def :: Int16
def = 0
instance Default Int32 where def :: Int32
def = 0
instance Default Int64 where def :: Int64
def = 0
instance Default Word where def :: Word
def = 0
instance Default Word8 where def :: Word8
def = 0
instance Default Word16 where def :: Word16
def = 0
instance Default Word32 where def :: Word32
def = 0
instance Default Word64 where def :: Word64
def = 0
instance Default Integer where def :: Integer
def = 0
instance Default Float where def :: Float
def = 0
instance Default Double where def :: Double
def = 0
instance (Integral a) => Default (Ratio a) where def :: Ratio a
def = 0
instance (Default a, RealFloat a) => Default (Complex a) where def :: Complex a
def = a
forall a. Default a => a
def a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. Default a => a
def

instance Default CShort     where def :: CShort
def = 0
instance Default CUShort    where def :: CUShort
def = 0
instance Default CInt       where def :: CInt
def = 0
instance Default CUInt      where def :: CUInt
def = 0
instance Default CLong      where def :: CLong
def = 0
instance Default CULong     where def :: CULong
def = 0
instance Default CLLong     where def :: CLLong
def = 0
instance Default CULLong    where def :: CULLong
def = 0
instance Default CPtrdiff   where def :: CPtrdiff
def = 0
instance Default CSize      where def :: CSize
def = 0
instance Default CSigAtomic where def :: CSigAtomic
def = 0
instance Default CIntPtr    where def :: CIntPtr
def = 0
instance Default CUIntPtr   where def :: CUIntPtr
def = 0
instance Default CIntMax    where def :: CIntMax
def = 0
instance Default CUIntMax   where def :: CUIntMax
def = 0
instance Default CClock     where def :: CClock
def = 0
instance Default CTime      where def :: CTime
def = 0
#if MIN_VERSION_base(4, 4, 0)
instance Default CUSeconds  where def :: CUSeconds
def = 0
instance Default CSUSeconds where def :: CSUSeconds
def = 0
#endif
instance Default CFloat     where def :: CFloat
def = 0
instance Default CDouble    where def :: CDouble
def = 0

instance (Default r) => Default (e -> r) where def :: e -> r
def = r -> e -> r
forall a b. a -> b -> a
const r
forall a. Default a => a
def
instance (Default a) => Default (IO a) where def :: IO a
def = a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. Default a => a
def

instance Default (Maybe a) where def :: Maybe a
def = Maybe a
forall a. Maybe a
Nothing

instance Default () where def :: ()
def = ()
forall a. Monoid a => a
mempty
instance Default [a] where def :: [a]
def = [a]
forall a. Monoid a => a
mempty
instance Default Ordering where def :: Ordering
def = Ordering
forall a. Monoid a => a
mempty
instance Default Any where def :: Any
def = Any
forall a. Monoid a => a
mempty
instance Default All where def :: All
def = All
forall a. Monoid a => a
mempty
instance Default (Last a) where def :: Last a
def = Last a
forall a. Monoid a => a
mempty
instance Default (First a) where def :: First a
def = First a
forall a. Monoid a => a
mempty
instance (Num a) => Default (Sum a) where def :: Sum a
def = Sum a
forall a. Monoid a => a
mempty
instance (Num a) => Default (Product a) where def :: Product a
def = Product a
forall a. Monoid a => a
mempty
instance Default (Endo a) where def :: Endo a
def = Endo a
forall a. Monoid a => a
mempty

instance (Default a) => Default (Dual a) where def :: Dual a
def = a -> Dual a
forall a. a -> Dual a
Dual a
forall a. Default a => a
def
instance (Default a, Default b) => Default (a, b) where def :: (a, b)
def = (a
forall a. Default a => a
def, b
forall a. Default a => a
def)
instance (Default a, Default b, Default c) => Default (a, b, c) where def :: (a, b, c)
def = (a
forall a. Default a => a
def, b
forall a. Default a => a
def, c
forall a. Default a => a
def)
instance (Default a, Default b, Default c, Default d) => Default (a, b, c, d) where def :: (a, b, c, d)
def = (a
forall a. Default a => a
def, b
forall a. Default a => a
def, c
forall a. Default a => a
def, d
forall a. Default a => a
def)
instance (Default a, Default b, Default c, Default d, Default e) => Default (a, b, c, d, e) where def :: (a, b, c, d, e)
def = (a
forall a. Default a => a
def, b
forall a. Default a => a
def, c
forall a. Default a => a
def, d
forall a. Default a => a
def, e
forall a. Default a => a
def)
instance (Default a, Default b, Default c, Default d, Default e, Default f) => Default (a, b, c, d, e, f) where def :: (a, b, c, d, e, f)
def = (a
forall a. Default a => a
def, b
forall a. Default a => a
def, c
forall a. Default a => a
def, d
forall a. Default a => a
def, e
forall a. Default a => a
def, f
forall a. Default a => a
def)
instance (Default a, Default b, Default c, Default d, Default e, Default f, Default g) => Default (a, b, c, d, e, f, g) where def :: (a, b, c, d, e, f, g)
def = (a
forall a. Default a => a
def, b
forall a. Default a => a
def, c
forall a. Default a => a
def, d
forall a. Default a => a
def, e
forall a. Default a => a
def, f
forall a. Default a => a
def, g
forall a. Default a => a
def)