{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
------------------------------------------------------------------------------- |-- Module : Data.Distributive-- Copyright : (C) 2011-2014 Edward Kmett-- License : BSD-style (see the file LICENSE)---- Maintainer : Edward Kmett <ekmett@gmail.com>-- Stability : provisional-- Portability : portable------------------------------------------------------------------------------
module Data.Distributive.Generic
( GDistributive(..)
, genericDistribute
) where
import GHC.Generics
-- | 'distribute' derived from a 'Generic1' type---- This can be used to easily produce a 'Distributive' instance for a-- type with a 'Generic1' instance,---- > data V2 a = V2 a a deriving (Show, Functor, Generic1)-- > instance Distributive V2' where distribute = genericDistributegenericDistribute :: (Functor f, Generic1 g, GDistributive (Rep1 g)) =>f (ga) -> g (fa)
genericDistribute = to1.gdistribute.fmapfrom1-- Can't distribute over,-- * sums (:+:)-- * K1
class GDistributiveg where
gdistribute :: Functor f =>f (ga) -> g (fa)
instance GDistributiveU1 where
gdistribute _ = U1{-# INLINE gdistribute #-}
instance (GDistributive a, GDistributive b) =>GDistributive (a :*: b) where
gdistributef = gdistribute (fmapfstPf) :*:gdistribute (fmapsndPf) where
fstP (l:*: _) = lsndP (_ :*:r) = r{-# INLINE gdistribute #-}
instance (Functor a, Functor b, GDistributive a, GDistributive b) =>GDistributive (a :.: b) where
gdistribute = Comp1.fmapgdistribute.gdistribute.fmapunComp1{-# INLINE gdistribute #-}
instance GDistributivePar1 where
gdistribute = Par1.fmapunPar1{-# INLINE gdistribute #-}
instance GDistributive f =>GDistributive (Rec1f) where
gdistribute = Rec1.gdistribute.fmapunRec1{-# INLINE gdistribute #-}
instance GDistributive f =>GDistributive (M1icf) where
gdistribute = M1.gdistribute.fmapunM1{-# INLINE gdistribute #-}