liveBook · Manning

Applicative style

As explained in Learn Haskell by Example

Applicative style is a functional programming style that combines independent computations using an applicative interface.

Applicative style combines independent computations through an applicative interface. For command-line programs, it builds a typed result by applying a constructor to several argument parsers. section 15.3.1 section 15.3.4

A command-line parser commonly combines:

  • strOption for string arguments, option auto for readable types such as integers, switch for Boolean flags, and optional for Maybe-valued arguments. section 15.3.1
  • Applicative operators: <$> applies a constructor to the first parser, while <*> supplies subsequent parsed values. section 15.3.1
  • Parser descriptions combined with <>; their order is irrelevant because the option metadata forms a monoid-style composition. section 15.3.1
  • ParserInfo adds help, version information, and program descriptions before execParser runs the parser. section 15.3.1
Listing 15.13 An example of an argument parser
import Options.Applicative

...

argsParser :: Parser Args
argsParser = Args <$> strArg <*> intArg <*>
 boolArg <*> maybeArg         #1
  where
    strArg =           #2
      strOption $
        long "string"
          <> short 's'
          <> metavar "STRING"
          <> help "An example string"
    intArg =                        #3
      option auto $
        long "integer"
          <> short 'i'
          <> metavar "INTEGER"
          <> help "An example integer"
    boolArg =                           #4
      switch $
        long "boolean"
          <> short 'b'
          <> help "An example boolean switch"
    maybeArg =                              #5
      optional $
        strOption $
          long "optional"
            <> short 'o'
            <> metavar "OPTIONAL"
            <> help "An example optional string"

The complete parser constructs an Args value from required, Boolean, and optional command-line fields. section 15.3.1

Parsing a synchronization tool's configuration

A CLI parser can nest applicative construction: CliArgs <$> config <*> isVerbose, with config itself built from path, file-type, recursion, and file-count parsers. Running the tool then executes execParser, passes the configuration to the application, and conditionally prints its log. section 15.3.4

How Learn Haskell by Example covers it

How deep it goes
Demonstrates parser primitives, applicative operators, metadata composition, execution, generated help, and nested configuration parsing.
Examples
Uses string, integer, Boolean, optional, synchronization-path, and verbosity arguments in executable CLI examples.
What you can do after
Apply it via Options.Applicative to build typed CLI parsers with validation, required-argument checks, help text, and version information.
How it connects
Connects command-line parsing with parser combinators, monoids, monads, typed records, and IO execution.

Also explained in

Compare how 2 books explain applicative style →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.