1 {-# LANGUAGE DeriveFoldable #-}
2 {-# LANGUAGE DeriveGeneric #-}
3 {-# LANGUAGE DeriveTraversable #-}
4 {-# LANGUAGE FlexibleContexts #-}
6 -----------------------------------------------------------------------------
9 -- Module : Distribution.Simple.Flag
10 -- Copyright : Isaac Jones 2003-2004
14 -- Maintainer : cabal-devel@haskell.org
15 -- Portability : portable
17 -- Defines the 'Flag' type and it's 'Monoid' instance, see
18 -- <http://www.haskell.org/pipermail/cabal-devel/2007-December/001509.html>
19 -- for an explanation.
21 -- Split off from "Distribution.Simple.Setup" to break import cycles.
22 module Distribution
.Simple
.Flag
35 import Distribution
.Compat
.Prelude
hiding (get
)
36 import Distribution
.Compat
.Stack
39 -- ------------------------------------------------------------
43 -- ------------------------------------------------------------
45 -- | All flags are monoids, they come in two flavours:
49 -- > --ghc-option=foo --ghc-option=bar
51 -- gives us all the values ["foo", "bar"]
53 -- 2. singular value flags, eg:
55 -- > --enable-foo --disable-foo
57 -- gives us Just False
59 -- So, this 'Flag' type is for the latter singular kind of flag.
60 -- Its monoid instance gives us the behaviour where it starts out as
61 -- 'NoFlag' and later flags override earlier ones.
63 -- Isomorphic to 'Maybe' a.
64 data Flag a
= Flag a | NoFlag
deriving (Eq
, Generic
, Show, Read, Typeable
, Foldable
, Traversable
)
66 instance Binary a
=> Binary
(Flag a
)
67 instance Structured a
=> Structured
(Flag a
)
69 instance Functor Flag
where
70 fmap f
(Flag x
) = Flag
(f x
)
71 fmap _ NoFlag
= NoFlag
73 instance Applicative Flag
where
74 (Flag x
) <*> y
= x
<$> y
78 instance Monoid
(Flag a
) where
82 instance Semigroup
(Flag a
) where
86 instance Bounded a
=> Bounded
(Flag a
) where
87 minBound = toFlag
minBound
88 maxBound = toFlag
maxBound
90 instance Enum a
=> Enum
(Flag a
) where
91 fromEnum = fromEnum . fromFlag
92 toEnum = toFlag
. toEnum
93 enumFrom (Flag a
) = map toFlag
. enumFrom $ a
95 enumFromThen (Flag a
) (Flag b
) = toFlag `
map`
enumFromThen a b
97 enumFromTo (Flag a
) (Flag b
) = toFlag `
map`
enumFromTo a b
99 enumFromThenTo (Flag a
) (Flag b
) (Flag c
) = toFlag `
map`
enumFromThenTo a b c
100 enumFromThenTo _ _ _
= []
102 -- | Wraps a value in 'Flag'.
103 toFlag
:: a
-> Flag a
106 -- | Extracts a value from a 'Flag', and throws an exception on 'NoFlag'.
107 fromFlag
:: WithCallStack
(Flag a
-> a
)
108 fromFlag
(Flag x
) = x
109 fromFlag NoFlag
= error "fromFlag NoFlag. Use fromFlagOrDefault"
111 -- | Extracts a value from a 'Flag', and returns the default value on 'NoFlag'.
112 fromFlagOrDefault
:: a
-> Flag a
-> a
113 fromFlagOrDefault _
(Flag x
) = x
114 fromFlagOrDefault def NoFlag
= def
116 -- | Converts a 'Flag' value to a 'Maybe' value.
117 flagToMaybe
:: Flag a
-> Maybe a
118 flagToMaybe
(Flag x
) = Just x
119 flagToMaybe NoFlag
= Nothing
121 -- | Pushes a function through a 'Flag' value, and returns a default
122 -- if the 'Flag' value is 'NoFlag'.
125 flagElim
:: b
-> (a
-> b
) -> Flag a
-> b
126 flagElim n _ NoFlag
= n
127 flagElim _ f
(Flag x
) = f x
129 -- | Converts a 'Flag' value to a list.
130 flagToList
:: Flag a
-> [a
]
131 flagToList
(Flag x
) = [x
]
132 flagToList NoFlag
= []
134 -- | Returns 'True' only if every 'Flag' 'Bool' value is Flag True, else 'False'.
135 allFlags
:: [Flag
Bool] -> Flag
Bool
137 if all (\f -> fromFlagOrDefault
False f
) flags
141 -- | Converts a 'Maybe' value to a 'Flag' value.
142 maybeToFlag
:: Maybe a
-> Flag a
143 maybeToFlag Nothing
= NoFlag
144 maybeToFlag
(Just x
) = Flag x
146 -- | Types that represent boolean flags.
147 class BooleanFlag a
where
150 instance BooleanFlag
Bool where