{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# 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,
  BooleanFlag(..) ) where

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

-- ------------------------------------------------------------
-- * 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.
--
data Flag a = Flag a | NoFlag deriving (Flag a -> Flag a -> Bool
forall a. Eq a => Flag a -> Flag a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Flag a -> Flag a -> Bool
$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
Eq, 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
$cto :: forall a x. Rep (Flag a) x -> Flag a
$cfrom :: forall a x. Flag a -> Rep (Flag a) x
Generic, Int -> Flag a -> ShowS
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
showList :: [Flag a] -> ShowS
$cshowList :: forall a. Show a => [Flag a] -> ShowS
show :: Flag a -> String
$cshow :: forall a. Show a => Flag a -> String
showsPrec :: Int -> Flag a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Flag a -> ShowS
Show, ReadPrec [Flag a]
ReadPrec (Flag a)
ReadS [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
readListPrec :: ReadPrec [Flag a]
$creadListPrec :: forall a. Read a => ReadPrec [Flag a]
readPrec :: ReadPrec (Flag a)
$creadPrec :: forall a. Read a => ReadPrec (Flag a)
readList :: ReadS [Flag a]
$creadList :: forall a. Read a => ReadS [Flag a]
readsPrec :: Int -> ReadS (Flag a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Flag a)
Read, Typeable)

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) = forall a. a -> Flag a
Flag (a -> b
f a
x)
  fmap a -> b
_ Flag a
NoFlag  = 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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Flag a
y
  Flag (a -> b)
NoFlag   <*> Flag a
_ = forall a. Flag a
NoFlag
  pure :: forall a. a -> Flag a
pure = forall a. a -> Flag a
Flag

instance Monoid (Flag a) where
  mempty :: Flag a
mempty = forall a. Flag a
NoFlag
  mappend :: Flag a -> Flag a -> Flag a
mappend = 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 = forall a. a -> Flag a
toFlag forall a. Bounded a => a
minBound
  maxBound :: Flag a
maxBound = forall a. a -> Flag a
toFlag forall a. Bounded a => a
maxBound

instance Enum a => Enum (Flag a) where
  fromEnum :: Flag a -> Int
fromEnum = forall a. Enum a => a -> Int
fromEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. WithCallStack (Flag a -> a)
fromFlag
  toEnum :: Int -> Flag a
toEnum   = forall a. a -> Flag a
toFlag   forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => Int -> a
toEnum
  enumFrom :: Flag a -> [Flag a]
enumFrom (Flag a
a) = forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Flag a
toFlag forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> [a]
enumFrom 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) = forall a. a -> Flag a
toFlag forall a b. (a -> b) -> [a] -> [b]
`map` 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) = forall a. a -> Flag a
toFlag forall a b. (a -> b) -> [a] -> [b]
`map` 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) = forall a. a -> Flag a
toFlag forall a b. (a -> b) -> [a] -> [b]
`map` forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo a
a a
b a
c
  enumFromThenTo Flag a
_        Flag a
_        Flag a
_        = []

toFlag :: a -> Flag a
toFlag :: forall a. a -> Flag a
toFlag = forall a. a -> Flag a
Flag

fromFlag :: WithCallStack (Flag a -> a)
fromFlag :: forall a. WithCallStack (Flag a -> a)
fromFlag (Flag a
x) = a
x
fromFlag Flag a
NoFlag   = forall a. HasCallStack => String -> a
error String
"fromFlag NoFlag. Use fromFlagOrDefault"

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

flagToMaybe :: Flag a -> Maybe a
flagToMaybe :: forall a. Flag a -> Maybe a
flagToMaybe (Flag a
x) = forall a. a -> Maybe a
Just a
x
flagToMaybe Flag a
NoFlag   = forall a. Maybe a
Nothing

-- | @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

flagToList :: Flag a -> [a]
flagToList :: forall a. Flag a -> [a]
flagToList (Flag a
x) = [a
x]
flagToList Flag a
NoFlag   = []

allFlags :: [Flag Bool] -> Flag Bool
allFlags :: [Flag Bool] -> Flag Bool
allFlags [Flag Bool]
flags = if forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Flag Bool
f -> forall a. a -> Flag a -> a
fromFlagOrDefault Bool
False Flag Bool
f) [Flag Bool]
flags
                 then forall a. a -> Flag a
Flag Bool
True
                 else forall a. Flag a
NoFlag

maybeToFlag :: Maybe a -> Flag a
maybeToFlag :: forall a. Maybe a -> Flag a
maybeToFlag Maybe a
Nothing  = forall a. Flag a
NoFlag
maybeToFlag (Just a
x) = forall a. a -> Flag a
Flag a
x

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

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