{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}

-----------------------------------------------------------------------------

-- |
-- Module      :  Distribution.Simple.Flag
-- Copyright   :  Isaac Jones 2003-2004
--                Duncan Coutts 2007
-- License     :  BSD3
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Defines the 'Flag' type and it's 'Monoid' instance,  see
-- <http://www.haskell.org/pipermail/cabal-devel/2007-December/001509.html>
-- for an explanation.
--
-- Split off from "Distribution.Simple.Setup" to break import cycles.
module Distribution.Simple.Flag
  ( Flag (..)
  , allFlags
  , toFlag
  , fromFlag
  , fromFlagOrDefault
  , flagElim
  , flagToMaybe
  , flagToList
  , maybeToFlag
  , mergeListFlag
  , BooleanFlag (..)
  ) where

import Distribution.Compat.Prelude hiding (get)
import Distribution.Compat.Stack
import Prelude ()

-- ------------------------------------------------------------

-- * Flag type

-- ------------------------------------------------------------

-- | All flags are monoids, they come in two flavours:
--
-- 1. list flags eg
--
--   > --ghc-option=foo --ghc-option=bar
--
--   gives us all the values ["foo", "bar"]
--
-- 2. singular value flags, eg:
--
--   > --enable-foo --disable-foo
--
--   gives us Just False
--
-- So, this 'Flag' type is for the latter singular kind of flag.
-- Its monoid instance gives us the behaviour where it starts out as
-- 'NoFlag' and later flags override earlier ones.
--
-- Isomorphic to 'Maybe' a.
data Flag a = Flag a | NoFlag deriving (Flag a -> Flag a -> Bool
(Flag a -> Flag a -> Bool)
-> (Flag a -> Flag a -> Bool) -> Eq (Flag a)
forall a. Eq a => Flag a -> Flag a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Flag a -> Flag a -> Bool
== :: Flag a -> Flag a -> Bool
$c/= :: forall a. Eq a => Flag a -> Flag a -> Bool
/= :: Flag a -> Flag a -> Bool
Eq, (forall x. Flag a -> Rep (Flag a) x)
-> (forall x. Rep (Flag a) x -> Flag a) -> Generic (Flag a)
forall x. Rep (Flag a) x -> Flag a
forall x. Flag a -> Rep (Flag a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Flag a) x -> Flag a
forall a x. Flag a -> Rep (Flag a) x
$cfrom :: forall a x. Flag a -> Rep (Flag a) x
from :: forall x. Flag a -> Rep (Flag a) x
$cto :: forall a x. Rep (Flag a) x -> Flag a
to :: forall x. Rep (Flag a) x -> Flag a
Generic, Int -> Flag a -> ShowS
[Flag a] -> ShowS
Flag a -> String
(Int -> Flag a -> ShowS)
-> (Flag a -> String) -> ([Flag a] -> ShowS) -> Show (Flag a)
forall a. Show a => Int -> Flag a -> ShowS
forall a. Show a => [Flag a] -> ShowS
forall a. Show a => Flag a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Flag a -> ShowS
showsPrec :: Int -> Flag a -> ShowS
$cshow :: forall a. Show a => Flag a -> String
show :: Flag a -> String
$cshowList :: forall a. Show a => [Flag a] -> ShowS
showList :: [Flag a] -> ShowS
Show, ReadPrec [Flag a]
ReadPrec (Flag a)
Int -> ReadS (Flag a)
ReadS [Flag a]
(Int -> ReadS (Flag a))
-> ReadS [Flag a]
-> ReadPrec (Flag a)
-> ReadPrec [Flag a]
-> Read (Flag a)
forall a. Read a => ReadPrec [Flag a]
forall a. Read a => ReadPrec (Flag a)
forall a. Read a => Int -> ReadS (Flag a)
forall a. Read a => ReadS [Flag a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Flag a)
readsPrec :: Int -> ReadS (Flag a)
$creadList :: forall a. Read a => ReadS [Flag a]
readList :: ReadS [Flag a]
$creadPrec :: forall a. Read a => ReadPrec (Flag a)
readPrec :: ReadPrec (Flag a)
$creadListPrec :: forall a. Read a => ReadPrec [Flag a]
readListPrec :: ReadPrec [Flag a]
Read, Typeable, (forall m. Monoid m => Flag m -> m)
-> (forall m a. Monoid m => (a -> m) -> Flag a -> m)
-> (forall m a. Monoid m => (a -> m) -> Flag a -> m)
-> (forall a b. (a -> b -> b) -> b -> Flag a -> b)
-> (forall a b. (a -> b -> b) -> b -> Flag a -> b)
-> (forall b a. (b -> a -> b) -> b -> Flag a -> b)
-> (forall b a. (b -> a -> b) -> b -> Flag a -> b)
-> (forall a. (a -> a -> a) -> Flag a -> a)
-> (forall a. (a -> a -> a) -> Flag a -> a)
-> (forall a. Flag a -> [a])
-> (forall a. Flag a -> Bool)
-> (forall a. Flag a -> Int)
-> (forall a. Eq a => a -> Flag a -> Bool)
-> (forall a. Ord a => Flag a -> a)
-> (forall a. Ord a => Flag a -> a)
-> (forall a. Num a => Flag a -> a)
-> (forall a. Num a => Flag a -> a)
-> Foldable Flag
forall a. Eq a => a -> Flag a -> Bool
forall a. Num a => Flag a -> a
forall a. Ord a => Flag a -> a
forall m. Monoid m => Flag m -> m
forall a. Flag a -> Bool
forall a. Flag a -> Int
forall a. Flag a -> [a]
forall a. (a -> a -> a) -> Flag a -> a
forall m a. Monoid m => (a -> m) -> Flag a -> m
forall b a. (b -> a -> b) -> b -> Flag a -> b
forall a b. (a -> b -> b) -> b -> Flag 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
$cfold :: forall m. Monoid m => Flag m -> m
fold :: forall m. Monoid m => Flag m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Flag a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Flag a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Flag a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Flag a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Flag a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Flag a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Flag a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Flag a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Flag a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Flag a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Flag a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Flag a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Flag a -> a
foldr1 :: forall a. (a -> a -> a) -> Flag a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Flag a -> a
foldl1 :: forall a. (a -> a -> a) -> Flag a -> a
$ctoList :: forall a. Flag a -> [a]
toList :: forall a. Flag a -> [a]
$cnull :: forall a. Flag a -> Bool
null :: forall a. Flag a -> Bool
$clength :: forall a. Flag a -> Int
length :: forall a. Flag a -> Int
$celem :: forall a. Eq a => a -> Flag a -> Bool
elem :: forall a. Eq a => a -> Flag a -> Bool
$cmaximum :: forall a. Ord a => Flag a -> a
maximum :: forall a. Ord a => Flag a -> a
$cminimum :: forall a. Ord a => Flag a -> a
minimum :: forall a. Ord a => Flag a -> a
$csum :: forall a. Num a => Flag a -> a
sum :: forall a. Num a => Flag a -> a
$cproduct :: forall a. Num a => Flag a -> a
product :: forall a. Num a => Flag a -> a
Foldable, Functor Flag
Foldable Flag
(Functor Flag, Foldable Flag) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Flag a -> f (Flag b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Flag (f a) -> f (Flag a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Flag a -> m (Flag b))
-> (forall (m :: * -> *) a. Monad m => Flag (m a) -> m (Flag a))
-> Traversable Flag
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 => Flag (m a) -> m (Flag a)
forall (f :: * -> *) a. Applicative f => Flag (f a) -> f (Flag a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Flag a -> m (Flag b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Flag a -> f (Flag b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Flag a -> f (Flag b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Flag a -> f (Flag b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Flag (f a) -> f (Flag a)
sequenceA :: forall (f :: * -> *) a. Applicative f => Flag (f a) -> f (Flag a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Flag a -> m (Flag b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Flag a -> m (Flag b)
$csequence :: forall (m :: * -> *) a. Monad m => Flag (m a) -> m (Flag a)
sequence :: forall (m :: * -> *) a. Monad m => Flag (m a) -> m (Flag a)
Traversable)

instance Binary a => Binary (Flag a)
instance Structured a => Structured (Flag a)

instance Functor Flag where
  fmap :: forall a b. (a -> b) -> Flag a -> Flag b
fmap a -> b
f (Flag a
x) = b -> Flag b
forall a. a -> Flag a
Flag (a -> b
f a
x)
  fmap a -> b
_ Flag a
NoFlag = Flag b
forall a. Flag a
NoFlag

instance Applicative Flag where
  (Flag a -> b
x) <*> :: forall a b. Flag (a -> b) -> Flag a -> Flag b
<*> Flag a
y = a -> b
x (a -> b) -> Flag a -> Flag b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Flag a
y
  Flag (a -> b)
NoFlag <*> Flag a
_ = Flag b
forall a. Flag a
NoFlag
  pure :: forall a. a -> Flag a
pure = a -> Flag a
forall a. a -> Flag a
Flag

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

instance Semigroup (Flag a) where
  Flag a
_ <> :: Flag a -> Flag a -> Flag a
<> f :: Flag a
f@(Flag a
_) = Flag a
f
  Flag a
f <> Flag a
NoFlag = Flag a
f

instance Bounded a => Bounded (Flag a) where
  minBound :: Flag a
minBound = a -> Flag a
forall a. a -> Flag a
toFlag a
forall a. Bounded a => a
minBound
  maxBound :: Flag a
maxBound = a -> Flag a
forall a. a -> Flag a
toFlag a
forall a. Bounded a => a
maxBound

instance Enum a => Enum (Flag a) where
  fromEnum :: Flag a -> Int
fromEnum = a -> Int
forall a. Enum a => a -> Int
fromEnum (a -> Int) -> (Flag a -> a) -> Flag a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Flag a -> a
forall a. WithCallStack (Flag a -> a)
fromFlag
  toEnum :: Int -> Flag a
toEnum = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> (Int -> a) -> Int -> Flag a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
forall a. Enum a => Int -> a
toEnum
  enumFrom :: Flag a -> [Flag a]
enumFrom (Flag a
a) = (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Flag a
forall a. a -> Flag a
toFlag ([a] -> [Flag a]) -> (a -> [a]) -> a -> [Flag a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Enum a => a -> [a]
enumFrom (a -> [Flag a]) -> a -> [Flag a]
forall a b. (a -> b) -> a -> b
$ a
a
  enumFrom Flag a
_ = []
  enumFromThen :: Flag a -> Flag a -> [Flag a]
enumFromThen (Flag a
a) (Flag a
b) = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
`map` a -> a -> [a]
forall a. Enum a => a -> a -> [a]
enumFromThen a
a a
b
  enumFromThen Flag a
_ Flag a
_ = []
  enumFromTo :: Flag a -> Flag a -> [Flag a]
enumFromTo (Flag a
a) (Flag a
b) = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
`map` a -> a -> [a]
forall a. Enum a => a -> a -> [a]
enumFromTo a
a a
b
  enumFromTo Flag a
_ Flag a
_ = []
  enumFromThenTo :: Flag a -> Flag a -> Flag a -> [Flag a]
enumFromThenTo (Flag a
a) (Flag a
b) (Flag a
c) = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
`map` a -> a -> a -> [a]
forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo a
a a
b a
c
  enumFromThenTo Flag a
_ Flag a
_ Flag a
_ = []

-- | Wraps a value in 'Flag'.
toFlag :: a -> Flag a
toFlag :: forall a. a -> Flag a
toFlag = a -> Flag a
forall a. a -> Flag a
Flag

-- | Extracts a value from a 'Flag', and throws an exception on 'NoFlag'.
fromFlag :: WithCallStack (Flag a -> a)
fromFlag :: forall a. WithCallStack (Flag a -> a)
fromFlag (Flag a
x) = a
x
fromFlag Flag a
NoFlag = String -> a
forall a. HasCallStack => String -> a
error String
"fromFlag NoFlag. Use fromFlagOrDefault"

-- | Extracts a value from a 'Flag', and returns the default value on 'NoFlag'.
fromFlagOrDefault :: a -> Flag a -> a
fromFlagOrDefault :: forall a. a -> Flag a -> a
fromFlagOrDefault a
_ (Flag a
x) = a
x
fromFlagOrDefault a
def Flag a
NoFlag = a
def

-- | Converts a 'Flag' value to a 'Maybe' value.
flagToMaybe :: Flag a -> Maybe a
flagToMaybe :: forall a. Flag a -> Maybe a
flagToMaybe (Flag a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
flagToMaybe Flag a
NoFlag = Maybe a
forall a. Maybe a
Nothing

-- | Pushes a function through a 'Flag' value, and returns a default
-- if the 'Flag' value is 'NoFlag'.
--
-- @since 3.4.0.0
flagElim :: b -> (a -> b) -> Flag a -> b
flagElim :: forall b a. b -> (a -> b) -> Flag a -> b
flagElim b
n a -> b
_ Flag a
NoFlag = b
n
flagElim b
_ a -> b
f (Flag a
x) = a -> b
f a
x

-- | Converts a 'Flag' value to a list.
flagToList :: Flag a -> [a]
flagToList :: forall a. Flag a -> [a]
flagToList (Flag a
x) = [a
x]
flagToList Flag a
NoFlag = []

-- | Returns 'True' only if every 'Flag' 'Bool' value is Flag True, else 'False'.
allFlags :: [Flag Bool] -> Flag Bool
allFlags :: [Flag Bool] -> Flag Bool
allFlags [Flag Bool]
flags =
  if (Flag Bool -> Bool) -> [Flag Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Flag Bool
f -> Bool -> Flag Bool -> Bool
forall a. a -> Flag a -> a
fromFlagOrDefault Bool
False Flag Bool
f) [Flag Bool]
flags
    then Bool -> Flag Bool
forall a. a -> Flag a
Flag Bool
True
    else Flag Bool
forall a. Flag a
NoFlag

-- | Converts a 'Maybe' value to a 'Flag' value.
maybeToFlag :: Maybe a -> Flag a
maybeToFlag :: forall a. Maybe a -> Flag a
maybeToFlag Maybe a
Nothing = Flag a
forall a. Flag a
NoFlag
maybeToFlag (Just a
x) = a -> Flag a
forall a. a -> Flag a
Flag a
x

-- | Merge the elements of a list 'Flag' with another list 'Flag'.
mergeListFlag :: Flag [a] -> Flag [a] -> Flag [a]
mergeListFlag :: forall a. Flag [a] -> Flag [a] -> Flag [a]
mergeListFlag Flag [a]
currentFlags Flag [a]
v =
  [a] -> Flag [a]
forall a. a -> Flag a
Flag ([a] -> Flag [a]) -> [a] -> Flag [a]
forall a b. (a -> b) -> a -> b
$ [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Flag [a] -> [[a]]
forall a. Flag a -> [a]
flagToList Flag [a]
currentFlags [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
++ Flag [a] -> [[a]]
forall a. Flag a -> [a]
flagToList Flag [a]
v)

-- | Types that represent boolean flags.
class BooleanFlag a where
  asBool :: a -> Bool

instance BooleanFlag Bool where
  asBool :: Bool -> Bool
asBool = Bool -> Bool
forall a. a -> a
id