Commit c83bec78 authored by Herbert Valerio Riedel's avatar Herbert Valerio Riedel 🕺

Fold base.git into ghc.git (re #8545)

At the time of merge, base.git was at
[52c0b09036c36f1ed928663abb2f295fd36a88bb/base]
Signed-off-by: Herbert Valerio Riedel's avatarHerbert Valerio Riedel <hvr@gnu.org>
parents a6e68af1 d8072fa0
Simon Marlow <marlowsd@gmail.com>, simonmar, simonmar@microsoft.com
Ross Paterson <ross@soi.city.ac.uk>, ross
Sven Panne <sven.panne@aedion.de>, panne
Malcolm Wallace <Malcolm.Wallace@cs.york.ac.uk>, malcolm
Simon Peyton Jones <simonpj@microsoft.com>, simonpj
Don Stewart <dons@galois.com>, dons
Tim Harris <tharris@microsoft.com>, tharris
Lennart Augustsson <lennart@augustsson.net>, lennart.augustsson@credit-suisse.com
Duncan Coutts <duncan@haskell.org>, duncan.coutts@worc.ox.ac.uk, duncan@well-typed.com
Ben Lippmeier <benl@ouroborus.net>, benl@cse.unsw.edu.au, Ben.Lippmeier@anu.edu.au
Manuel M T Chakravarty <chak@cse.unsw.edu.au>, chak
Jose Pedro Magalhaes <jpm@cs.uu.nl>, jpm@cs.uu.nl
*.o
*.aux
*.hi
*.comp.stderr
*.comp.stdout
*.interp.stderr
*.interp.stdout
*.run.stderr
*.run.stdout
*.eventlog
*.genscript
# Backup files
*~
# Specific generated files
/GNUmakefile
/autom4te.cache/
/base.buildinfo
/config.log
/config.status
/configure
/dist-install/
/ghc.mk
/include/EventConfig.h
/include/HsBaseConfig.h
/include/HsBaseConfig.h.in
tests/.hpc/
tests/4006
tests/CPUTime001
tests/Concurrent/.hpc/
tests/Concurrent/4876
tests/Concurrent/ThreadDelay001
tests/IO/.hpc/
tests/IO/2122
tests/IO/2122-test
tests/IO/3307
tests/IO/4808
tests/IO/4808.test
tests/IO/4855
tests/IO/4895
tests/IO/IOError001
tests/IO/IOError002
tests/IO/T4144
tests/IO/chinese-file-*
tests/IO/chinese-name
tests/IO/concio002
tests/IO/countReaders001
tests/IO/countReaders001.txt
tests/IO/decodingerror001
tests/IO/decodingerror002
tests/IO/encoding001
tests/IO/encoding001.utf16
tests/IO/encoding001.utf16.utf16be
tests/IO/encoding001.utf16.utf16le
tests/IO/encoding001.utf16.utf32
tests/IO/encoding001.utf16.utf32be
tests/IO/encoding001.utf16.utf32le
tests/IO/encoding001.utf16.utf8
tests/IO/encoding001.utf16.utf8_bom
tests/IO/encoding001.utf16be
tests/IO/encoding001.utf16be.utf16
tests/IO/encoding001.utf16be.utf16le
tests/IO/encoding001.utf16be.utf32
tests/IO/encoding001.utf16be.utf32be
tests/IO/encoding001.utf16be.utf32le
tests/IO/encoding001.utf16be.utf8
tests/IO/encoding001.utf16be.utf8_bom
tests/IO/encoding001.utf16le
tests/IO/encoding001.utf16le.utf16
tests/IO/encoding001.utf16le.utf16be
tests/IO/encoding001.utf16le.utf32
tests/IO/encoding001.utf16le.utf32be
tests/IO/encoding001.utf16le.utf32le
tests/IO/encoding001.utf16le.utf8
tests/IO/encoding001.utf16le.utf8_bom
tests/IO/encoding001.utf32
tests/IO/encoding001.utf32.utf16
tests/IO/encoding001.utf32.utf16be
tests/IO/encoding001.utf32.utf16le
tests/IO/encoding001.utf32.utf32be
tests/IO/encoding001.utf32.utf32le
tests/IO/encoding001.utf32.utf8
tests/IO/encoding001.utf32.utf8_bom
tests/IO/encoding001.utf32be
tests/IO/encoding001.utf32be.utf16
tests/IO/encoding001.utf32be.utf16be
tests/IO/encoding001.utf32be.utf16le
tests/IO/encoding001.utf32be.utf32
tests/IO/encoding001.utf32be.utf32le
tests/IO/encoding001.utf32be.utf8
tests/IO/encoding001.utf32be.utf8_bom
tests/IO/encoding001.utf32le
tests/IO/encoding001.utf32le.utf16
tests/IO/encoding001.utf32le.utf16be
tests/IO/encoding001.utf32le.utf16le
tests/IO/encoding001.utf32le.utf32
tests/IO/encoding001.utf32le.utf32be
tests/IO/encoding001.utf32le.utf8
tests/IO/encoding001.utf32le.utf8_bom
tests/IO/encoding001.utf8
tests/IO/encoding001.utf8.utf16
tests/IO/encoding001.utf8.utf16be
tests/IO/encoding001.utf8.utf16le
tests/IO/encoding001.utf8.utf32
tests/IO/encoding001.utf8.utf32be
tests/IO/encoding001.utf8.utf32le
tests/IO/encoding001.utf8.utf8_bom
tests/IO/encoding001.utf8_bom
tests/IO/encoding001.utf8_bom.utf16
tests/IO/encoding001.utf8_bom.utf16be
tests/IO/encoding001.utf8_bom.utf16le
tests/IO/encoding001.utf8_bom.utf32
tests/IO/encoding001.utf8_bom.utf32be
tests/IO/encoding001.utf8_bom.utf32le
tests/IO/encoding001.utf8_bom.utf8
tests/IO/encoding002
tests/IO/encodingerror001
tests/IO/environment001
tests/IO/finalization001
tests/IO/hClose001
tests/IO/hClose001.tmp
tests/IO/hClose002
tests/IO/hClose002.tmp
tests/IO/hClose003
tests/IO/hDuplicateTo001
tests/IO/hFileSize001
tests/IO/hFileSize002
tests/IO/hFileSize002.out
tests/IO/hFlush001
tests/IO/hFlush001.out
tests/IO/hGetBuf001
tests/IO/hGetBuffering001
tests/IO/hGetChar001
tests/IO/hGetLine001
tests/IO/hGetLine002
tests/IO/hGetLine003
tests/IO/hGetPosn001
tests/IO/hGetPosn001.out
tests/IO/hIsEOF001
tests/IO/hIsEOF002
tests/IO/hIsEOF002.out
tests/IO/hReady001
tests/IO/hReady002
tests/IO/hSeek001
tests/IO/hSeek002
tests/IO/hSeek003
tests/IO/hSeek004
tests/IO/hSeek004.out
tests/IO/hSetBuffering002
tests/IO/hSetBuffering003
tests/IO/hSetBuffering004
tests/IO/hSetEncoding001
tests/IO/ioeGetErrorString001
tests/IO/ioeGetFileName001
tests/IO/ioeGetHandle001
tests/IO/isEOF001
tests/IO/misc001
tests/IO/misc001.out
tests/IO/newline001
tests/IO/newline001.out
tests/IO/openFile001
tests/IO/openFile002
tests/IO/openFile003
tests/IO/openFile004
tests/IO/openFile004.out
tests/IO/openFile005
tests/IO/openFile005.out1
tests/IO/openFile005.out2
tests/IO/openFile006
tests/IO/openFile006.out
tests/IO/openFile007
tests/IO/openFile007.out
tests/IO/openFile008
tests/IO/openTempFile001
tests/IO/putStr001
tests/IO/readFile001
tests/IO/readFile001.out
tests/IO/readwrite001
tests/IO/readwrite001.inout
tests/IO/readwrite002
tests/IO/readwrite002.inout
tests/IO/readwrite003
tests/IO/readwrite003.txt
tests/IO/tmp
tests/Numeric/.hpc/
tests/Numeric/num001
tests/Numeric/num002
tests/Numeric/num003
tests/Numeric/num004
tests/Numeric/num005
tests/Numeric/num006
tests/Numeric/num007
tests/Numeric/num008
tests/Numeric/num009
tests/Numeric/num010
tests/System/.hpc/
tests/System/exitWith001
tests/System/getArgs001
tests/System/getEnv001
tests/System/system001
tests/Text.Printf/.hpc/
tests/Text.Printf/1548
tests/addr001
tests/assert
tests/char001
tests/char002
tests/cstring001
tests/data-fixed-show-read
tests/dynamic001
tests/dynamic002
tests/dynamic003
tests/dynamic004
tests/dynamic005
tests/echo001
tests/enum01
tests/enum02
tests/enum03
tests/enum04
tests/enumDouble
tests/enumRatio
tests/exceptionsrun001
tests/exceptionsrun002
tests/fixed
tests/genericNegative001
tests/hGetBuf002
tests/hGetBuf003
tests/hPutBuf001
tests/hPutBuf002
tests/hPutBuf002.out
tests/hTell001
tests/hTell002
tests/hash001
tests/ioref001
tests/ix001
tests/length001
tests/lex001
tests/list001
tests/list002
tests/list003
tests/memo001
tests/memo002
tests/performGC001
tests/quotOverflow
tests/rand001
tests/ratio001
tests/readDouble001
tests/readFloat
tests/readInteger001
tests/readLitChar
tests/reads001
tests/show001
tests/showDouble
tests/stableptr001
tests/stableptr003
tests/stableptr004
tests/stableptr005
tests/take001
tests/tempfiles
tests/text001
tests/trace001
tests/tup001
tests/unicode001
tests/unicode002
tests/weak001
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE AutoDeriveTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
-----------------------------------------------------------------------------
-- |
-- Module : Control.Applicative
-- Copyright : Conor McBride and Ross Paterson 2005
-- License : BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer : libraries@haskell.org
-- Stability : experimental
-- Portability : portable
--
-- This module describes a structure intermediate between a functor and
-- a monad (technically, a strong lax monoidal functor). Compared with
-- monads, this interface lacks the full power of the binding operation
-- '>>=', but
--
-- * it has more instances.
--
-- * it is sufficient for many uses, e.g. context-free parsing, or the
-- 'Data.Traversable.Traversable' class.
--
-- * instances can perform analysis of computations before they are
-- executed, and thus produce shared optimizations.
--
-- This interface was introduced for parsers by Niklas R&#xF6;jemo, because
-- it admits more sharing than the monadic interface. The names here are
-- mostly based on parsing work by Doaitse Swierstra.
--
-- For more details, see
-- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative Programming with Effects>,
-- by Conor McBride and Ross Paterson.
module Control.Applicative (
-- * Applicative functors
Applicative(..),
-- * Alternatives
Alternative(..),
-- * Instances
Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
-- * Utility functions
(<$>), (<$), (<**>),
liftA, liftA2, liftA3,
optional,
) where
import Prelude hiding (id,(.))
import Control.Category
import Control.Arrow
import Control.Monad (liftM, ap, MonadPlus(..))
import Control.Monad.ST.Safe (ST)
import qualified Control.Monad.ST.Lazy.Safe as Lazy (ST)
import Data.Functor ((<$>), (<$))
import Data.Monoid (Monoid(..))
import Data.Proxy
import Text.ParserCombinators.ReadP (ReadP)
import Text.ParserCombinators.ReadPrec (ReadPrec)
import GHC.Conc (STM, retry, orElse)
import GHC.Generics
infixl 3 <|>
infixl 4 <*>, <*, *>, <**>
-- | A functor with application, providing operations to
--
-- * embed pure expressions ('pure'), and
--
-- * sequence computations and combine their results ('<*>').
--
-- A minimal complete definition must include implementations of these
-- functions satisfying the following laws:
--
-- [/identity/]
--
-- @'pure' 'id' '<*>' v = v@
--
-- [/composition/]
--
-- @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
--
-- [/homomorphism/]
--
-- @'pure' f '<*>' 'pure' x = 'pure' (f x)@
--
-- [/interchange/]
--
-- @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
--
-- The other methods have the following default definitions, which may
-- be overridden with equivalent specialized implementations:
--
-- * @u '*>' v = 'pure' ('const' 'id') '<*>' u '<*>' v@
--
-- * @u '<*' v = 'pure' 'const' '<*>' u '<*>' v@
--
-- As a consequence of these laws, the 'Functor' instance for @f@ will satisfy
--
-- * @'fmap' f x = 'pure' f '<*>' x@
--
-- If @f@ is also a 'Monad', it should satisfy
--
-- * @'pure' = 'return'@
--
-- * @('<*>') = 'ap'@
--
-- (which implies that 'pure' and '<*>' satisfy the applicative functor laws).
class Functor f => Applicative f where
-- | Lift a value.
pure :: a -> f a
-- | Sequential application.
(<*>) :: f (a -> b) -> f a -> f b
-- | Sequence actions, discarding the value of the first argument.
(*>) :: f a -> f b -> f b
(*>) = liftA2 (const id)
-- | Sequence actions, discarding the value of the second argument.
(<*) :: f a -> f b -> f a
(<*) = liftA2 const
-- | A monoid on applicative functors.
--
-- Minimal complete definition: 'empty' and '<|>'.
--
-- If defined, 'some' and 'many' should be the least solutions
-- of the equations:
--
-- * @some v = (:) '<$>' v '<*>' many v@
--
-- * @many v = some v '<|>' 'pure' []@
class Applicative f => Alternative f where
-- | The identity of '<|>'
empty :: f a
-- | An associative binary operation
(<|>) :: f a -> f a -> f a
-- | One or more.