{-# LANGUAGE CPP                #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFoldable     #-}
{-# LANGUAGE DeriveFunctor      #-}
{-# LANGUAGE DeriveTraversable  #-}

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Monoid.Recommend
-- Copyright   :  (c) 2012-2015 diagrams-core team (see LICENSE)
-- License     :  BSD-style (see LICENSE)
-- Maintainer  :  diagrams-discuss@googlegroups.com
--
-- A type for representing values with an additional bit saying
-- whether the value is \"just a recommendation\" (to be used only if
-- nothing better comes along) or a \"commitment\" (to certainly be
-- used, overriding merely recommended values), along with
-- corresponding @Semigroup@ and @Monoid@ instances.
--
-----------------------------------------------------------------------------

module Data.Monoid.Recommend
       ( Recommend(..)
       , getRecommend
       ) where

#if __GLASGOW_HASKELL__ < 710
import           Data.Foldable
import           Data.Traversable
#endif

import           Data.Data
import           Data.Semigroup

-- | A value of type @Recommend a@ consists of a value of type @a@
--   wrapped up in one of two constructors.  The @Recommend@
--   constructor indicates a \"non-committal recommendation\"---that
--   is, the given value should be used if no other/better values are
--   available.  The @Commit@ constructor indicates a
--   \"commitment\"---a value which should definitely be used,
--   overriding any @Recommend@ed values.
data Recommend a = Recommend a
                 | Commit a
  deriving (Int -> Recommend a -> ShowS
forall a. Show a => Int -> Recommend a -> ShowS
forall a. Show a => [Recommend a] -> ShowS
forall a. Show a => Recommend a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Recommend a] -> ShowS
$cshowList :: forall a. Show a => [Recommend a] -> ShowS
show :: Recommend a -> String
$cshow :: forall a. Show a => Recommend a -> String
showsPrec :: Int -> Recommend a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Recommend a -> ShowS
Show, ReadPrec [Recommend a]
ReadPrec (Recommend a)
ReadS [Recommend a]
forall a. Read a => ReadPrec [Recommend a]
forall a. Read a => ReadPrec (Recommend a)
forall a. Read a => Int -> ReadS (Recommend a)
forall a. Read a => ReadS [Recommend a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Recommend a]
$creadListPrec :: forall a. Read a => ReadPrec [Recommend a]
readPrec :: ReadPrec (Recommend a)
$creadPrec :: forall a. Read a => ReadPrec (Recommend a)
readList :: ReadS [Recommend a]
$creadList :: forall a. Read a => ReadS [Recommend a]
readsPrec :: Int -> ReadS (Recommend a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Recommend a)
Read, forall a b. a -> Recommend b -> Recommend a
forall a b. (a -> b) -> Recommend a -> Recommend b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Recommend b -> Recommend a
$c<$ :: forall a b. a -> Recommend b -> Recommend a
fmap :: forall a b. (a -> b) -> Recommend a -> Recommend b
$cfmap :: forall a b. (a -> b) -> Recommend a -> Recommend b
Functor, Recommend a -> Recommend a -> Bool
forall a. Eq a => Recommend a -> Recommend a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Recommend a -> Recommend a -> Bool
$c/= :: forall a. Eq a => Recommend a -> Recommend a -> Bool
== :: Recommend a -> Recommend a -> Bool
$c== :: forall a. Eq a => Recommend a -> Recommend a -> Bool
Eq, Recommend a -> Recommend a -> Bool
Recommend a -> Recommend a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (Recommend a)
forall a. Ord a => Recommend a -> Recommend a -> Bool
forall a. Ord a => Recommend a -> Recommend a -> Ordering
forall a. Ord a => Recommend a -> Recommend a -> Recommend a
min :: Recommend a -> Recommend a -> Recommend a
$cmin :: forall a. Ord a => Recommend a -> Recommend a -> Recommend a
max :: Recommend a -> Recommend a -> Recommend a
$cmax :: forall a. Ord a => Recommend a -> Recommend a -> Recommend a
>= :: Recommend a -> Recommend a -> Bool
$c>= :: forall a. Ord a => Recommend a -> Recommend a -> Bool
> :: Recommend a -> Recommend a -> Bool
$c> :: forall a. Ord a => Recommend a -> Recommend a -> Bool
<= :: Recommend a -> Recommend a -> Bool
$c<= :: forall a. Ord a => Recommend a -> Recommend a -> Bool
< :: Recommend a -> Recommend a -> Bool
$c< :: forall a. Ord a => Recommend a -> Recommend a -> Bool
compare :: Recommend a -> Recommend a -> Ordering
$ccompare :: forall a. Ord a => Recommend a -> Recommend a -> Ordering
Ord, Typeable, Recommend a -> DataType
Recommend a -> Constr
forall {a}. Data a => Typeable (Recommend a)
forall a. Data a => Recommend a -> DataType
forall a. Data a => Recommend a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> Recommend a -> Recommend a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Recommend a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Recommend a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Recommend a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Recommend a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Recommend a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Recommend a -> c (Recommend a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Recommend a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Recommend a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Recommend a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Recommend a -> c (Recommend a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Recommend a))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Recommend a -> m (Recommend a)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Recommend a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Recommend a -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Recommend a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Recommend a -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Recommend a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Recommend a -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Recommend a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Recommend a -> r
gmapT :: (forall b. Data b => b -> b) -> Recommend a -> Recommend a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Recommend a -> Recommend a
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Recommend a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Recommend a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Recommend a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Recommend a))
dataTypeOf :: Recommend a -> DataType
$cdataTypeOf :: forall a. Data a => Recommend a -> DataType
toConstr :: Recommend a -> Constr
$ctoConstr :: forall a. Data a => Recommend a -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Recommend a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Recommend a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Recommend a -> c (Recommend a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Recommend a -> c (Recommend a)
Data, forall a. Eq a => a -> Recommend a -> Bool
forall a. Num a => Recommend a -> a
forall a. Ord a => Recommend a -> a
forall m. Monoid m => Recommend m -> m
forall a. Recommend a -> Bool
forall a. Recommend a -> Int
forall a. Recommend a -> [a]
forall a. (a -> a -> a) -> Recommend a -> a
forall m a. Monoid m => (a -> m) -> Recommend a -> m
forall b a. (b -> a -> b) -> b -> Recommend a -> b
forall a b. (a -> b -> b) -> b -> Recommend a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Recommend a -> a
$cproduct :: forall a. Num a => Recommend a -> a
sum :: forall a. Num a => Recommend a -> a
$csum :: forall a. Num a => Recommend a -> a
minimum :: forall a. Ord a => Recommend a -> a
$cminimum :: forall a. Ord a => Recommend a -> a
maximum :: forall a. Ord a => Recommend a -> a
$cmaximum :: forall a. Ord a => Recommend a -> a
elem :: forall a. Eq a => a -> Recommend a -> Bool
$celem :: forall a. Eq a => a -> Recommend a -> Bool
length :: forall a. Recommend a -> Int
$clength :: forall a. Recommend a -> Int
null :: forall a. Recommend a -> Bool
$cnull :: forall a. Recommend a -> Bool
toList :: forall a. Recommend a -> [a]
$ctoList :: forall a. Recommend a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Recommend a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Recommend a -> a
foldr1 :: forall a. (a -> a -> a) -> Recommend a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Recommend a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Recommend a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Recommend a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Recommend a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Recommend a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Recommend a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Recommend a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Recommend a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Recommend a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Recommend a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Recommend a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Recommend a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Recommend a -> m
fold :: forall m. Monoid m => Recommend m -> m
$cfold :: forall m. Monoid m => Recommend m -> m
Foldable, Functor Recommend
Foldable Recommend
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
Recommend (m a) -> m (Recommend a)
forall (f :: * -> *) a.
Applicative f =>
Recommend (f a) -> f (Recommend a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Recommend a -> m (Recommend b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Recommend a -> f (Recommend b)
sequence :: forall (m :: * -> *) a.
Monad m =>
Recommend (m a) -> m (Recommend a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
Recommend (m a) -> m (Recommend a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Recommend a -> m (Recommend b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Recommend a -> m (Recommend b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Recommend (f a) -> f (Recommend a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Recommend (f a) -> f (Recommend a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Recommend a -> f (Recommend b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Recommend a -> f (Recommend b)
Traversable)

-- | Extract the value of type @a@ wrapped in @Recommend a@.
getRecommend :: Recommend a -> a
getRecommend :: forall a. Recommend a -> a
getRecommend (Recommend a
a) = a
a
getRecommend (Commit a
a)    = a
a

-- | 'Commit' overrides 'Recommend'. Two values wrapped in the same
--   constructor (both 'Recommend' or both 'Commit') are combined
--   according to the underlying @Semigroup@ instance.
instance Semigroup a => Semigroup (Recommend a) where
  Recommend a
a <> :: Recommend a -> Recommend a -> Recommend a
<> Recommend a
b = forall a. a -> Recommend a
Recommend (a
a forall a. Semigroup a => a -> a -> a
<> a
b)
  Recommend a
_ <> Commit a
b    = forall a. a -> Recommend a
Commit a
b
  Commit a
a    <> Recommend a
_ = forall a. a -> Recommend a
Commit a
a
  Commit a
a    <> Commit a
b    = forall a. a -> Recommend a
Commit (a
a forall a. Semigroup a => a -> a -> a
<> a
b)

  stimes :: forall b. Integral b => b -> Recommend a -> Recommend a
stimes b
n (Recommend a
m) = forall a. a -> Recommend a
Recommend (forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
m)
  stimes b
n (Commit    a
m) = forall a. a -> Recommend a
Commit    (forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
m)

instance (Semigroup a, Monoid a) => Monoid (Recommend a) where
  mappend :: Recommend a -> Recommend a -> Recommend a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
  mempty :: Recommend a
mempty  = forall a. a -> Recommend a
Recommend forall a. Monoid a => a
mempty