| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Options.Generic
Contents
Description
This library auto-generates command-line parsers for data types using Haskell's built-in support for generic programming. The best way to understand how this library works is to walk through a few examples.
For example, suppose that you want to parse a record with named fields like this:
-- Example.hs
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
import Options.Generic
data Example = Example { foo :: Int, bar :: Double }
deriving (Generic, Show)
instance ParseRecord Example
main = do
x <- getRecord "Test program"
print (x :: Example)Named fields translate to flags which you can provide in any order:
$ stack build optparse-generic
$ stack runghc Example.hs -- --bar 2.5 --foo 1
Example {foo = 1, bar = 2.5}This also auto-generates --help output:
$ stack runghc Example.hs -- --help Test program Usage: Example.hs --foo INT --bar DOUBLE Available options: -h,--help Show this help text
You can also add help descriptions to each field, like this:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
import Options.Generic
data Example = Example
{ foo :: Int <?> "Documentation for the foo flag"
, bar :: Double <?> "Documentation for the bar flag"
} deriving (Generic, Show)
instance ParseRecord Example
main = do
x <- getRecord "Test program"
print (x :: Example)... which produces the following --help output:
$ stack runghc Example.hs -- --help Test program Usage: Example.hs --foo INT --bar DOUBLE Available options: -h,--help Show this help text --foo INT Documentation for the foo flag --bar DOUBLE Documentation for the bar flag
However, any fields you document will be wrapped in the Helpful
constructor:
$ stack runghc Example.hs -- --foo 1 --bar 2.5
Example {foo = Helpful {unHelpful = 1}, bar = Helpful {unHelpful = 2.5}}To avoid this, while still being able to document your fields, you may
generalize the definition of your record with a parameter w, and use
unwrapRecord.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-} -- One more extension.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-} -- To derive Show
{-# LANGUAGE TypeOperators #-}
import Options.Generic
data Example w = Example
{ foo :: w ::: Int <?> "Documentation for the foo flag"
, bar :: w ::: Double <?> "Documentation for the bar flag"
} deriving (Generic)
instance ParseRecord (Example Wrapped)
deriving instance Show (Example Unwrapped)
main = do
x <- unwrapRecord "Test program"
print (x :: Example Unwrapped)Example Unwrapped is equivalent to a record type with simple fields:
$ stack runghc Example.hs -- --foo 1 --bar 2.5
Example {foo = 1, bar = 2.5}You can also add default values to each Readable field, like this:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
import Options.Generic
data Example = Example
{ foo :: Int <!> "1"
, bar :: String <!> "hello"
} deriving (Generic, Show)
instance ParseRecord Example
main = do
x <- getRecord "Test program"
print (x :: Example)Default values will work alongside help descriptions and unwrapping.
For the following examples I encourage you to test what --help output they
generate.
This library will also do the right thing if the fields have no labels:
data Example = Example Int Double deriving (Generic, Show)
Fields without labels translate into positional command-line arguments:
$ stack runghc Example.hs -- 1 2.5 Example 1 2.5
Certain types of fields are given special treatment, such as in this example:
data Example = Example
{ switch :: Bool
, list :: [Int]
, optional :: Maybe Int
, first :: First Int
, last :: Last Int
, sum :: Sum Int
, product :: Product Int
} deriving (Generic, Show)This gives the following behavior:
$ stack runghc Example.hs --
--switch
--optional 1
--list 1 --list 2
--first 1 --first 2
--last 1 --last 2
--sum 1 --sum 2
--product 1 --product 2
Example {switch = True, list = [1,2], optional = Just 1, first = First
{getFirst = Just 1}, last = Last {getLast = Just 2}, sum = Sum {getSum =
3}, product = Product {getProduct = 2}}
$ stack runghc Example.hs
Example {switch = False, list = [], optional = Nothing, first = First
{getFirst = Nothing}, second = Last {getLast = Nothing}, sum = Sum {getSum
= 0}, product = Product {getProduct = 1}}If a datatype has multiple constructors:
data Example
= Create { name :: Text, duration :: Maybe Int }
| Kill { name :: Text }
deriving (Generic, Show)... then they will translate into subcommands named after each constructor:
$ stack runghc Example.hs -- create --name foo --duration=60
Create {name = "foo", duration = Just 60}
$ stack runghc Example.hs -- kill --name foo
Kill {name = "foo"}This library also provides out-of-the-box support for many existing types,
like tuples and Either.
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
import Options.Generic
main = do
x <- getRecord "Test program"
print (x :: Either Double Int)$ stack runghc Example.hs -- left 1.0 Left 1.0 $ stack runghc Example.hs -- right 2 Right 2
main = do
x <- getRecord "Test program"
print (x :: (Double, Int))$ stack runghc Example.hs -- 1.0 2 (1.0,2)
... and you can also just parse a single value:
main = do
x <- getRecord "Test program"
print (x :: Int)$ stack runghc Example.hs -- 2 2
However, there are some types that this library cannot generate sensible command-line parsers for, such as:
recursive types:
data Example = Example { foo :: Example }records whose fields are other records
data Outer = Outer { foo :: Inner } deriving (Show, Generic) data Inner = Inner { bar :: Int } deriving (Show, Generic)record fields with nested
Maybes or nested listsdata Example = Example { foo :: Maybe (Maybe Int) } data Example = Example { foo :: [[Int]] }
If you try to auto-generate a parser for these types you will get an error at compile time that will look something like this:
No instance for (ParseFields TheTypeOfYourField)
arising from a use of ‘Options.Generic.$gdmparseRecord’
In the expression: Options.Generic.$gdmparseRecord
In an equation for ‘parseRecord’:
parseRecord = Options.Generic.$gdmparseRecord
In the instance declaration for ‘ParseRecord TheTypeOfYourRecord’You can customize the library's default behavior using the
parseRecordWithModifiers utility, like this:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
import Options.Generic
data Example = Example { foo :: Int, bar :: Double }
deriving (Generic, Show)
modifiers :: Modifiers
modifiers = defaultModifiers
{ shortNameModifier = firstLetter
}
instance ParseRecord Example where
parseRecord = parseRecordWithModifiers modifiers
main = do
x <- getRecord "Test program"
print (x :: Example)Synopsis
- getRecord :: (MonadIO io, ParseRecord a) => Text -> io a
- getRecordWith :: (MonadIO io, ParseRecord a) => InfoMod a -> PrefsMod -> io a
- getWithHelpWith :: (MonadIO io, ParseRecord a) => Text -> PrefsMod -> io (a, io ())
- getWithHelp :: (MonadIO io, ParseRecord a) => Text -> io (a, io ())
- getRecordPure :: ParseRecord a => [Text] -> Maybe a
- getRecordPureWith :: ParseRecord a => [Text] -> InfoMod a -> PrefsMod -> Maybe a
- unwrapRecord :: (Functor io, MonadIO io, ParseRecord (f Wrapped), Unwrappable f) => Text -> io (f Unwrapped)
- unwrapWithHelp :: (MonadIO io, ParseRecord (f Wrapped), Unwrappable f) => Text -> io (f Unwrapped, io ())
- unwrapRecordPure :: (ParseRecord (f Wrapped), Unwrappable f) => [Text] -> Maybe (f Unwrapped)
- unwrap :: forall f. Unwrappable f => f Wrapped -> f Unwrapped
- class ParseRecord a where
- parseRecord :: Parser a
- class ParseRecord a => ParseFields a where
- class ParseField a where
- newtype Only a = Only {
- fromOnly :: a
- getOnly :: Only a -> a
- readIntegralBounded :: forall a. (Integral a, Bounded a, Typeable a, ParseField a) => ReadM a
- data Modifiers = Modifiers {
- fieldNameModifier :: String -> String
- constructorNameModifier :: String -> String
- shortNameModifier :: String -> Maybe Char
- parseRecordWithModifiers :: (Generic a, GenericParseRecord (Rep a)) => Modifiers -> Parser a
- defaultModifiers :: Modifiers
- lispCaseModifiers :: Modifiers
- firstLetter :: String -> Maybe Char
- class GenericParseRecord f where
- genericParseRecord :: Modifiers -> Parser (f p)
- newtype (field :: *) <?> (help :: Symbol) = Helpful {
- unHelpful :: field
- newtype (field :: *) <!> (value :: Symbol) = DefValue {
- unDefValue :: field
- newtype (field :: *) <#> (value :: Symbol) = ShortName {
- unShortName :: field
- type family wrap ::: wrapped
- data Wrapped
- data Unwrapped
- type Unwrappable f = (Generic (f Wrapped), Generic (f Unwrapped), GenericUnwrappable (Rep (f Wrapped)) (Rep (f Unwrapped)))
- class Generic a
- data Text
- newtype All = All {}
- newtype Any = Any {}
- newtype First a = First {}
- newtype Last a = Last {}
- newtype Sum a = Sum {
- getSum :: a
- newtype Product a = Product {
- getProduct :: a
Parsers
Arguments
| :: (MonadIO io, ParseRecord a) | |
| => Text | Program description |
| -> io a |
Marshal any value that implements ParseRecord from the command line
If you need to modify the top-level ParserInfo or ParserPrefs
use the getRecordWith function.
Arguments
| :: (MonadIO io, ParseRecord a) | |
| => InfoMod a |
|
| -> PrefsMod |
|
| -> io a |
Marshal any value that implements ParseRecord from the command line
This is the lower-level sibling of 'getRecord and lets you modify
the ParserInfo and ParserPrefs records.
Arguments
| :: (MonadIO io, ParseRecord a) | |
| => Text | Program description |
| -> PrefsMod |
|
| -> io (a, io ()) | (options, io action to print help message) |
Marshal any value that implements ParseRecord from the commmand line
alongside an io action that prints the help message.
Arguments
| :: (MonadIO io, ParseRecord a) | |
| => Text | Program description |
| -> io (a, io ()) | (options, io action to print help message) |
Marshal any value that implements ParseRecord from the commmand line
alongside an io action that prints the help message.
Arguments
| :: ParseRecord a | |
| => [Text] | Command-line arguments |
| -> Maybe a |
Pure version of getRecord
If you need to modify the parser's ParserInfo or ParserPrefs, use
getRecordPureWith.
>>>:set -XOverloadedStrings>>>getRecordPure ["1"] :: Maybe IntJust 1>>>getRecordPure ["1", "2"] :: Maybe [Int]Just [1,2]>>>getRecordPure ["Foo"] :: Maybe IntNothing
Arguments
| :: ParseRecord a | |
| => [Text] | Command-line arguments |
| -> InfoMod a |
|
| -> PrefsMod |
|
| -> Maybe a |
Pure version of getRecordWith
Like getRecordWith, this is a sibling of 'getRecordPure and
exposes the monoidal modifier structures for ParserInfo and
ParserPrefs to you.
>>>:set -XOverloadedStrings>>>getRecordPureWith ["1"] mempty mempty :: Maybe IntJust 1>>>getRecordPureWith ["1", "2"] mempty mempty :: Maybe [Int]Just [1,2]>>>getRecordPureWith ["Foo"] mempty mempty :: Maybe IntNothing
unwrapRecord :: (Functor io, MonadIO io, ParseRecord (f Wrapped), Unwrappable f) => Text -> io (f Unwrapped) Source #
Marshal any value that implements ParseRecord from the command line
and unwrap its fields
Arguments
| :: (MonadIO io, ParseRecord (f Wrapped), Unwrappable f) | |
| => Text | Program description |
| -> io (f Unwrapped, io ()) | (options, io action to print help message) |
Marshal any value that implements ParseRecord from the command line
and unwrap its fields alongside an io action to print the help message
Arguments
| :: (ParseRecord (f Wrapped), Unwrappable f) | |
| => [Text] | Command-line arguments |
| -> Maybe (f Unwrapped) |
Pure version of unwrapRecord
unwrap :: forall f. Unwrappable f => f Wrapped -> f Unwrapped Source #
Unwrap the fields of a constructor
class ParseRecord a where Source #
A class for types that can be parsed from the command line
This class has a default implementation for any type that implements
Generic and you can derive Generic for many types by enabling the
DeriveGeneric language extension
You can also use getOnly to create a ParseRecord instance from a
ParseFields instance:
instance ParseRecord MyType where
parseRecord = fmap getOnly parseRecordMinimal complete definition
Nothing
Methods
parseRecord :: Parser a Source #
default parseRecord :: (Generic a, GenericParseRecord (Rep a)) => Parser a Source #
Instances
class ParseRecord a => ParseFields a where Source #
A class for all types that can be parsed from zero or more arguments/options on the command line
parseFields has a default implementation for any type that implements
ParseField
Minimal complete definition
Nothing
Methods
Instances
class ParseField a where Source #
A class for all record fields that can be parsed from exactly one option or argument on the command line
parseField has a default implementation for any type that implements
Read and Typeable. You can derive Read for many types and you can
derive Typeable for any type if you enable the DeriveDataTypeable
language extension
Minimal complete definition
Nothing
Methods
Arguments
| :: Maybe Text | Help message |
| -> Maybe Text | Field label |
| -> Maybe Char | Short name |
| -> Maybe String | Default value |
| -> Parser a |
Arguments
| :: Maybe Text | Help message |
| -> Maybe Text | Field label |
| -> Maybe Char | Short name |
| -> Maybe String | Default value |
| -> Parser [a] |
The only reason for this method is to provide a special case for
handling Strings. All other instances should just fall back on the
default implementation for parseListOfField
Instances
The 1-tuple type or single-value "collection".
This type is structurally equivalent to the
Identity type, but its intent is more
about serving as the anonymous 1-tuple type missing from Haskell for attaching
typeclass instances.
Parameter usage example:
encodeSomething (Only (42::Int))Result usage example:
xs <- decodeSomething
forM_ xs $ \(Only id) -> {- ... -}Instances
| Functor Only | |
| Data a => Data (Only a) | |
Defined in Data.Tuple.Only Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Only a -> c (Only a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Only a) # toConstr :: Only a -> Constr # dataTypeOf :: Only a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Only a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Only a)) # gmapT :: (forall b. Data b => b -> b) -> Only a -> Only a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Only a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Only a -> r # gmapQ :: (forall d. Data d => d -> u) -> Only a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Only a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Only a -> m (Only a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Only a -> m (Only a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Only a -> m (Only a) # | |
| Generic (Only a) | |
| Read a => Read (Only a) | |
| Show a => Show (Only a) | |
| NFData a => NFData (Only a) | |
Defined in Data.Tuple.Only | |
| Eq a => Eq (Only a) | |
| Ord a => Ord (Only a) | |
| ParseFields a => ParseRecord (Only a) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (Only a) Source # | |
| type Rep (Only a) | |
Defined in Data.Tuple.Only | |
getOnly :: Only a -> a Source #
This is a convenience function that you can use if you want to create a
ParseRecord instance that just defers to the ParseFields instance for
the same type:
instance ParseRecord MyType where
parseRecord = fmap getOnly parseRecordreadIntegralBounded :: forall a. (Integral a, Bounded a, Typeable a, ParseField a) => ReadM a Source #
Options for customizing derived ParseRecord implementations for Generic
types
You can either create the Modifiers record directly:
modifiers :: Modifiers
modifiers = Modifiers
{ fieldNameModifier = ...
, constructorNameModifier = ...
, shortNameModifier = ...
}... or you can tweak the defaultModifiers:
modifiers :: Modifiers
modifiers = defaultModifiers { fieldNameModifier = ... }... or you can use/tweak a predefined Modifier, like lispCaseModifiers
The parseRecordWithModifiers function uses this Modifiers record when
generating a Generic implementation of ParseRecord
Constructors
| Modifiers | |
Fields
| |
parseRecordWithModifiers :: (Generic a, GenericParseRecord (Rep a)) => Modifiers -> Parser a Source #
Use parseRecordWithModifiers when you want to tweak the behavior of a
derived ParseRecord implementation, like this:
myModifiers :: Modifiers
myModifiers = defaultModifiers { constructorNameModifier = id }
instance ParseRecord MyType where
parseRecord = parseRecordWithModifiers myModifiersThis will still require that you derive Generic for your type to automate
most of the implementation, but the Modifiers that you pass will change
how the implementation generates the command line interface
defaultModifiers :: Modifiers Source #
These are the default modifiers used if you derive a Generic
implementation. You can customize this and pass the result to
parseRecordWithModifiers if you would like to modify the derived
implementation:
myModifiers :: Modifiers
myModifiers = defaultModifiers { constructorNameModifier = id }
instance ParseRecord MyType where
parseRecord = parseRecordWithModifiers myModifierslispCaseModifiers :: Modifiers Source #
Convert field and constructor names from CamelCase to lisp-case.
Leading underscores are dropped, allowing one to use option names which are Haskell keywords or otherwise conflicting identifiers.
BuildCommand -> build-command someFlag -> --some-flag _type -> --type _splitAt -> --split-at
firstLetter :: String -> Maybe Char Source #
Use this for the shortNameModifier field of the Modifiers record if
you want to use the first letter of each option as the short name
class GenericParseRecord f where Source #
Methods
genericParseRecord :: Modifiers -> Parser (f p) Source #
Instances
Help
newtype (field :: *) <?> (help :: Symbol) Source #
Use this to annotate a field with a type-level string (i.e. a Symbol)
representing the help description for that field:
data Example = Example
{ foo :: Int <?> "Documentation for the foo flag"
, bar :: Double <?> "Documentation for the bar flag"
} deriving (Generic, Show)Instances
| (KnownSymbol help, Data field) => Data (field <?> help) Source # | |
Defined in Options.Generic Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> (field <?> help) -> c (field <?> help) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (field <?> help) # toConstr :: (field <?> help) -> Constr # dataTypeOf :: (field <?> help) -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (field <?> help)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (field <?> help)) # gmapT :: (forall b. Data b => b -> b) -> (field <?> help) -> field <?> help # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> (field <?> help) -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> (field <?> help) -> r # gmapQ :: (forall d. Data d => d -> u) -> (field <?> help) -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> (field <?> help) -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> (field <?> help) -> m (field <?> help) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> (field <?> help) -> m (field <?> help) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> (field <?> help) -> m (field <?> help) # | |
| Generic (field <?> help) Source # | |
| Show field => Show (field <?> help) Source # | |
| (ParseField a, KnownSymbol h) => ParseField (a <?> h) Source # | |
Defined in Options.Generic | |
| (ParseFields a, KnownSymbol h) => ParseFields (a <?> h) Source # | |
| (ParseFields a, KnownSymbol h) => ParseRecord (a <?> h) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (a <?> h) Source # | |
| type Rep (field <?> help) Source # | |
Defined in Options.Generic | |
newtype (field :: *) <!> (value :: Symbol) Source #
Use this to annotate a field with a type-level string (i.e. a Symbol)
representing the default value for that field:
data Example = Example
{ foo :: Int <!> "1"
, bar :: Double <!> "0.5"
} deriving (Generic, Show)Constructors
| DefValue | |
Fields
| |
Instances
| (KnownSymbol value, Data field) => Data (field <!> value) Source # | |
Defined in Options.Generic Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> (field <!> value) -> c (field <!> value) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (field <!> value) # toConstr :: (field <!> value) -> Constr # dataTypeOf :: (field <!> value) -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (field <!> value)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (field <!> value)) # gmapT :: (forall b. Data b => b -> b) -> (field <!> value) -> field <!> value # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> (field <!> value) -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> (field <!> value) -> r # gmapQ :: (forall d. Data d => d -> u) -> (field <!> value) -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> (field <!> value) -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> (field <!> value) -> m (field <!> value) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> (field <!> value) -> m (field <!> value) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> (field <!> value) -> m (field <!> value) # | |
| Generic (field <!> value) Source # | |
| Show field => Show (field <!> value) Source # | |
| (ParseField a, KnownSymbol d) => ParseField (a <!> d) Source # | |
Defined in Options.Generic | |
| (ParseFields a, KnownSymbol d) => ParseFields (a <!> d) Source # | |
| (ParseFields a, KnownSymbol h) => ParseRecord (a <!> h) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (a <!> h) Source # | |
| type Rep (field <!> value) Source # | |
Defined in Options.Generic | |
newtype (field :: *) <#> (value :: Symbol) Source #
Use this to annotate a field with a type-level char (i.e. a Symbol)
representing the short name of the field (only the first character of the
symbol is used):
data Example = Example
{ foo :: Int <#> "f"
, bar :: Double <#> "b"
} deriving (Generic, Show)Constructors
| ShortName | |
Fields
| |
Instances
| (KnownSymbol value, Data field) => Data (field <#> value) Source # | |
Defined in Options.Generic Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> (field <#> value) -> c (field <#> value) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (field <#> value) # toConstr :: (field <#> value) -> Constr # dataTypeOf :: (field <#> value) -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (field <#> value)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (field <#> value)) # gmapT :: (forall b. Data b => b -> b) -> (field <#> value) -> field <#> value # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> (field <#> value) -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> (field <#> value) -> r # gmapQ :: (forall d. Data d => d -> u) -> (field <#> value) -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> (field <#> value) -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> (field <#> value) -> m (field <#> value) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> (field <#> value) -> m (field <#> value) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> (field <#> value) -> m (field <#> value) # | |
| Generic (field <#> value) Source # | |
| Show field => Show (field <#> value) Source # | |
| (ParseField a, KnownSymbol c) => ParseField (a <#> c) Source # | |
Defined in Options.Generic | |
| (ParseFields a, KnownSymbol c) => ParseFields (a <#> c) Source # | |
| (ParseFields a, KnownSymbol h) => ParseRecord (a <#> h) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (a <#> h) Source # | |
| type Rep (field <#> value) Source # | |
Defined in Options.Generic | |
Flag to keep fields wrapped
Flag to unwrap fields annotated using (<?>)
Instances
| Data Unwrapped Source # | |
Defined in Options.Generic Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Unwrapped -> c Unwrapped # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Unwrapped # toConstr :: Unwrapped -> Constr # dataTypeOf :: Unwrapped -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Unwrapped) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Unwrapped) # gmapT :: (forall b. Data b => b -> b) -> Unwrapped -> Unwrapped # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Unwrapped -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Unwrapped -> r # gmapQ :: (forall d. Data d => d -> u) -> Unwrapped -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Unwrapped -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Unwrapped -> m Unwrapped # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Unwrapped -> m Unwrapped # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Unwrapped -> m Unwrapped # | |
| type Unwrapped ::: wrapped Source # | |
Defined in Options.Generic | |
type Unwrappable f = (Generic (f Wrapped), Generic (f Unwrapped), GenericUnwrappable (Rep (f Wrapped)) (Rep (f Unwrapped))) Source #
Constraint for types whose fields can be unwrapped
Re-exports
Representable types of kind *.
This class is derivable in GHC with the DeriveGeneric flag on.
A Generic instance must satisfy the following laws:
from.to≡idto.from≡id
Instances
A space efficient, packed, unboxed Unicode text type.
Instances
| ParseField Text Source # | |
| ParseFields Text Source # | |
| ParseRecord Text Source # | |
Defined in Options.Generic Methods parseRecord :: Parser Text Source # | |
| type Item Text | |
Boolean monoid under conjunction (&&).
>>>getAll (All True <> mempty <> All False)False
>>>getAll (mconcat (map (\x -> All (even x)) [2,4,6,7,8]))False
Instances
| Data All | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> All -> c All # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c All # dataTypeOf :: All -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c All) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c All) # gmapT :: (forall b. Data b => b -> b) -> All -> All # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> All -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> All -> r # gmapQ :: (forall d. Data d => d -> u) -> All -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> All -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> All -> m All # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> All -> m All # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> All -> m All # | |
| Monoid All | Since: base-2.1 |
| Semigroup All | Since: base-4.9.0.0 |
| Bounded All | Since: base-2.1 |
| Generic All | |
| Read All | Since: base-2.1 |
| Show All | Since: base-2.1 |
| Eq All | Since: base-2.1 |
| Ord All | Since: base-2.1 |
| ParseField All Source # | |
| ParseFields All Source # | |
| ParseRecord All Source # | |
Defined in Options.Generic Methods parseRecord :: Parser All Source # | |
| type Rep All | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |
Boolean monoid under disjunction (||).
>>>getAny (Any True <> mempty <> Any False)True
>>>getAny (mconcat (map (\x -> Any (even x)) [2,4,6,7,8]))True
Instances
| Data Any | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Any -> c Any # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Any # dataTypeOf :: Any -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Any) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Any) # gmapT :: (forall b. Data b => b -> b) -> Any -> Any # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Any -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Any -> r # gmapQ :: (forall d. Data d => d -> u) -> Any -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Any -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Any -> m Any # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Any -> m Any # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Any -> m Any # | |
| Monoid Any | Since: base-2.1 |
| Semigroup Any | Since: base-4.9.0.0 |
| Bounded Any | Since: base-2.1 |
| Generic Any | |
| Read Any | Since: base-2.1 |
| Show Any | Since: base-2.1 |
| Eq Any | Since: base-2.1 |
| Ord Any | Since: base-2.1 |
| ParseField Any Source # | |
| ParseFields Any Source # | |
| ParseRecord Any Source # | |
Defined in Options.Generic Methods parseRecord :: Parser Any Source # | |
| type Rep Any | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |
Maybe monoid returning the leftmost non-Nothing value.
is isomorphic to First a, but precedes it
historically.Alt Maybe a
>>>getFirst (First (Just "hello") <> First Nothing <> First (Just "world"))Just "hello"
Instances
| Foldable First | Since: base-4.8.0.0 |
Defined in Data.Foldable Methods fold :: Monoid m => First m -> m # foldMap :: Monoid m => (a -> m) -> First a -> m # foldMap' :: Monoid m => (a -> m) -> First a -> m # foldr :: (a -> b -> b) -> b -> First a -> b # foldr' :: (a -> b -> b) -> b -> First a -> b # foldl :: (b -> a -> b) -> b -> First a -> b # foldl' :: (b -> a -> b) -> b -> First a -> b # foldr1 :: (a -> a -> a) -> First a -> a # foldl1 :: (a -> a -> a) -> First a -> a # elem :: Eq a => a -> First a -> Bool # maximum :: Ord a => First a -> a # minimum :: Ord a => First a -> a # | |
| Traversable First | Since: base-4.8.0.0 |
| Applicative First | Since: base-4.8.0.0 |
| Functor First | Since: base-4.8.0.0 |
| Monad First | Since: base-4.8.0.0 |
| Data a => Data (First a) | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> First a -> c (First a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (First a) # toConstr :: First a -> Constr # dataTypeOf :: First a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (First a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (First a)) # gmapT :: (forall b. Data b => b -> b) -> First a -> First a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> First a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> First a -> r # gmapQ :: (forall d. Data d => d -> u) -> First a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> First a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> First a -> m (First a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> First a -> m (First a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> First a -> m (First a) # | |
| Monoid (First a) | Since: base-2.1 |
| Semigroup (First a) | Since: base-4.9.0.0 |
| Generic (First a) | |
| Read a => Read (First a) | Since: base-2.1 |
| Show a => Show (First a) | Since: base-2.1 |
| Eq a => Eq (First a) | Since: base-2.1 |
| Ord a => Ord (First a) | Since: base-2.1 |
| ParseField a => ParseFields (First a) Source # | |
| ParseField a => ParseRecord (First a) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (First a) Source # | |
| Generic1 First | |
| type Rep (First a) | Since: base-4.7.0.0 |
Defined in Data.Monoid | |
| type Rep1 First | Since: base-4.7.0.0 |
Defined in Data.Monoid | |
Maybe monoid returning the rightmost non-Nothing value.
is isomorphic to Last a, and thus to
Dual (First a)Dual (Alt Maybe a)
>>>getLast (Last (Just "hello") <> Last Nothing <> Last (Just "world"))Just "world"
Instances
| Foldable Last | Since: base-4.8.0.0 |
Defined in Data.Foldable Methods fold :: Monoid m => Last m -> m # foldMap :: Monoid m => (a -> m) -> Last a -> m # foldMap' :: Monoid m => (a -> m) -> Last a -> m # foldr :: (a -> b -> b) -> b -> Last a -> b # foldr' :: (a -> b -> b) -> b -> Last a -> b # foldl :: (b -> a -> b) -> b -> Last a -> b # foldl' :: (b -> a -> b) -> b -> Last a -> b # foldr1 :: (a -> a -> a) -> Last a -> a # foldl1 :: (a -> a -> a) -> Last a -> a # elem :: Eq a => a -> Last a -> Bool # maximum :: Ord a => Last a -> a # | |
| Traversable Last | Since: base-4.8.0.0 |
| Applicative Last | Since: base-4.8.0.0 |
| Functor Last | Since: base-4.8.0.0 |
| Monad Last | Since: base-4.8.0.0 |
| Data a => Data (Last a) | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Last a -> c (Last a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Last a) # toConstr :: Last a -> Constr # dataTypeOf :: Last a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Last a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Last a)) # gmapT :: (forall b. Data b => b -> b) -> Last a -> Last a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Last a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Last a -> r # gmapQ :: (forall d. Data d => d -> u) -> Last a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Last a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Last a -> m (Last a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Last a -> m (Last a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Last a -> m (Last a) # | |
| Monoid (Last a) | Since: base-2.1 |
| Semigroup (Last a) | Since: base-4.9.0.0 |
| Generic (Last a) | |
| Read a => Read (Last a) | Since: base-2.1 |
| Show a => Show (Last a) | Since: base-2.1 |
| Eq a => Eq (Last a) | Since: base-2.1 |
| Ord a => Ord (Last a) | Since: base-2.1 |
| ParseField a => ParseFields (Last a) Source # | |
| ParseField a => ParseRecord (Last a) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (Last a) Source # | |
| Generic1 Last | |
| type Rep (Last a) | Since: base-4.7.0.0 |
Defined in Data.Monoid | |
| type Rep1 Last | Since: base-4.7.0.0 |
Defined in Data.Monoid | |
Monoid under addition.
>>>getSum (Sum 1 <> Sum 2 <> mempty)3
Instances
| Foldable Sum | Since: base-4.8.0.0 |
Defined in Data.Foldable Methods fold :: Monoid m => Sum m -> m # foldMap :: Monoid m => (a -> m) -> Sum a -> m # foldMap' :: Monoid m => (a -> m) -> Sum a -> m # foldr :: (a -> b -> b) -> b -> Sum a -> b # foldr' :: (a -> b -> b) -> b -> Sum a -> b # foldl :: (b -> a -> b) -> b -> Sum a -> b # foldl' :: (b -> a -> b) -> b -> Sum a -> b # foldr1 :: (a -> a -> a) -> Sum a -> a # foldl1 :: (a -> a -> a) -> Sum a -> a # elem :: Eq a => a -> Sum a -> Bool # maximum :: Ord a => Sum a -> a # | |
| Traversable Sum | Since: base-4.8.0.0 |
| Applicative Sum | Since: base-4.8.0.0 |
| Functor Sum | Since: base-4.8.0.0 |
| Monad Sum | Since: base-4.8.0.0 |
| Data a => Data (Sum a) | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Sum a -> c (Sum a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Sum a) # dataTypeOf :: Sum a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Sum a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Sum a)) # gmapT :: (forall b. Data b => b -> b) -> Sum a -> Sum a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Sum a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Sum a -> r # gmapQ :: (forall d. Data d => d -> u) -> Sum a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Sum a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Sum a -> m (Sum a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Sum a -> m (Sum a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Sum a -> m (Sum a) # | |
| Num a => Monoid (Sum a) | Since: base-2.1 |
| Num a => Semigroup (Sum a) | Since: base-4.9.0.0 |
| Bounded a => Bounded (Sum a) | Since: base-2.1 |
| Generic (Sum a) | |
| Num a => Num (Sum a) | Since: base-4.7.0.0 |
| Read a => Read (Sum a) | Since: base-2.1 |
| Show a => Show (Sum a) | Since: base-2.1 |
| Eq a => Eq (Sum a) | Since: base-2.1 |
| Ord a => Ord (Sum a) | Since: base-2.1 |
| (Num a, ParseField a) => ParseFields (Sum a) Source # | |
| (Num a, ParseField a) => ParseRecord (Sum a) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (Sum a) Source # | |
| Generic1 Sum | |
| type Rep (Sum a) | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |
| type Rep1 Sum | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |
Monoid under multiplication.
>>>getProduct (Product 3 <> Product 4 <> mempty)12
Constructors
| Product | |
Fields
| |
Instances
| Foldable Product | Since: base-4.8.0.0 |
Defined in Data.Foldable Methods fold :: Monoid m => Product m -> m # foldMap :: Monoid m => (a -> m) -> Product a -> m # foldMap' :: Monoid m => (a -> m) -> Product a -> m # foldr :: (a -> b -> b) -> b -> Product a -> b # foldr' :: (a -> b -> b) -> b -> Product a -> b # foldl :: (b -> a -> b) -> b -> Product a -> b # foldl' :: (b -> a -> b) -> b -> Product a -> b # foldr1 :: (a -> a -> a) -> Product a -> a # foldl1 :: (a -> a -> a) -> Product a -> a # elem :: Eq a => a -> Product a -> Bool # maximum :: Ord a => Product a -> a # minimum :: Ord a => Product a -> a # | |
| Traversable Product | Since: base-4.8.0.0 |
| Applicative Product | Since: base-4.8.0.0 |
| Functor Product | Since: base-4.8.0.0 |
| Monad Product | Since: base-4.8.0.0 |
| Data a => Data (Product a) | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Product a -> c (Product a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Product a) # toConstr :: Product a -> Constr # dataTypeOf :: Product a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Product a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Product a)) # gmapT :: (forall b. Data b => b -> b) -> Product a -> Product a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Product a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Product a -> r # gmapQ :: (forall d. Data d => d -> u) -> Product a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Product a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Product a -> m (Product a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Product a -> m (Product a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Product a -> m (Product a) # | |
| Num a => Monoid (Product a) | Since: base-2.1 |
| Num a => Semigroup (Product a) | Since: base-4.9.0.0 |
| Bounded a => Bounded (Product a) | Since: base-2.1 |
| Generic (Product a) | |
| Num a => Num (Product a) | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |
| Read a => Read (Product a) | Since: base-2.1 |
| Show a => Show (Product a) | Since: base-2.1 |
| Eq a => Eq (Product a) | Since: base-2.1 |
| Ord a => Ord (Product a) | Since: base-2.1 |
| (Num a, ParseField a) => ParseFields (Product a) Source # | |
| (Num a, ParseField a) => ParseRecord (Product a) Source # | |
Defined in Options.Generic Methods parseRecord :: Parser (Product a) Source # | |
| Generic1 Product | |
| type Rep (Product a) | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |
| type Rep1 Product | Since: base-4.7.0.0 |
Defined in Data.Semigroup.Internal | |