{-
The overall structure of the GHC Prelude is a bit tricky.
 a) We want to avoid "orphan modules", i.e. ones with instance
 decls that don't belong either to a tycon or a class
 defined in the same module
 b) We want to avoid giant modules
So the rough structure is as follows, in (linearised) dependency order
GHC.Prim Has no implementation. It defines built-in things, and
 by importing it you bring them into scope.
 The source file is GHC.Prim.hi-boot, which is just
 copied to make GHC.Prim.hi
GHC.Internal.Base Classes: Eq, Ord, Functor, Monad
 Types: List, (), Int, Bool, Ordering, Char, String
GHC.Internal.Data.Tuple Types: tuples, plus instances for GHC.Internal.Base classes
GHC.Internal.Show Class: Show, plus instances for GHC.Base/GHC.Tup types
GHC.Internal.Enum Class: Enum, plus instances for GHC.Base/GHC.Tup types
GHC.Internal.Data.Maybe Type: Maybe, plus instances for GHC.Internal.Base classes
GHC.Internal.List List functions
GHC.Internal.Num Class: Num, plus instances for Int
 Type: Integer, plus instances for all classes so far (Eq, Ord, Num, Show)
 Integer is needed here because it is mentioned in the signature
 of 'fromInteger' in class Num
GHC.Internal.Real Classes: Real, Integral, Fractional, RealFrac
 plus instances for Int, Integer
 Types: Ratio, Rational
 plus instances for classes so far
 Rational is needed here because it is mentioned in the signature
 of 'toRational' in class Real
GHC.Internal.ST The ST monad, instances and a few helper functions
Ix Classes: Ix, plus instances for Int, Bool, Char, Integer, Ordering, tuples
GHC.Internal.Arr Types: Array, MutableArray, MutableVar
 Arrays are used by a function in GHC.Internal.Float
GHC.Internal.Float Classes: Floating, RealFloat
 Types: Float, Double, plus instances of all classes so far
 This module contains everything to do with floating point.
 It is a big module (900 lines)
 With a bit of luck, many modules can be compiled without ever reading GHC.Internal.Float.hi
Other Prelude modules are much easier with fewer complex dependencies.
-}{-# LANGUAGE BangPatterns #-}{-# LANGUAGE CPP #-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE EmptyCase #-}{-# LANGUAGE EmptyDataDeriving #-}{-# LANGUAGE ExistentialQuantification #-}{-# LANGUAGE MagicHash #-}{-# LANGUAGE NoImplicitPrelude #-}{-# LANGUAGE PolyKinds #-}{-# LANGUAGE RankNTypes #-}{-# LANGUAGE UnboxedTuples #-}{-# LANGUAGE Unsafe #-}-- -Wno-orphans is needed for things like:-- Orphan rule: "x# -# x#" ALWAYS forall x# :: Int# -# x# x# = 0{-# OPTIONS_GHC -Wno-orphans #-}{-# OPTIONS_HADDOCK not-home #-}------------------------------------------------------------------------------- |-- Module : GHC.Internal.Base-- Copyright : (c) The University of Glasgow, 1992-2002-- License : see libraries/base/LICENSE---- Maintainer : ghc-devs@haskell.org-- Stability : internal-- Portability : non-portable (GHC extensions)---- Basic data types and classes.-------------------------------------------------------------------------------
#include "MachDeps.h"
moduleGHC.Internal.Base (moduleGHC.Internal.Base ,moduleGHC.Classes ,moduleGHC.CString ,moduleGHC.Magic ,moduleGHC.Magic.Dict ,moduleGHC.Types ,moduleGHC.Prim ,-- Re-export GHC.Prim, GHC.Prim.Ext,moduleGHC.Prim.Ext ,-- GHC.Prim.PtrEq and [boot] GHC.Internal.ErrmoduleGHC.Prim.PtrEq ,-- to avoid lots of people having tomoduleGHC.Internal.Err ,-- import these modules explicitlymoduleGHC.Internal.Maybe )whereimportGHC.Types hiding(Unit# ,Solo# ,Tuple0# ,Tuple1# ,Tuple2# ,Tuple3# ,Tuple4# ,Tuple5# ,Tuple6# ,Tuple7# ,Tuple8# ,Tuple9# ,Tuple10# ,Tuple11# ,Tuple12# ,Tuple13# ,Tuple14# ,Tuple15# ,Tuple16# ,Tuple17# ,Tuple18# ,Tuple19# ,Tuple20# ,Tuple21# ,Tuple22# ,Tuple23# ,Tuple24# ,Tuple25# ,Tuple26# ,Tuple27# ,Tuple28# ,Tuple29# ,Tuple30# ,Tuple31# ,Tuple32# ,Tuple33# ,Tuple34# ,Tuple35# ,Tuple36# ,Tuple37# ,Tuple38# ,Tuple39# ,Tuple40# ,Tuple41# ,Tuple42# ,Tuple43# ,Tuple44# ,Tuple45# ,Tuple46# ,Tuple47# ,Tuple48# ,Tuple49# ,Tuple50# ,Tuple51# ,Tuple52# ,Tuple53# ,Tuple54# ,Tuple55# ,Tuple56# ,Tuple57# ,Tuple58# ,Tuple59# ,Tuple60# ,Tuple61# ,Tuple62# ,Tuple63# ,Tuple64# ,Sum2# ,Sum3# ,Sum4# ,Sum5# ,Sum6# ,Sum7# ,Sum8# ,Sum9# ,Sum10# ,Sum11# ,Sum12# ,Sum13# ,Sum14# ,Sum15# ,Sum16# ,Sum17# ,Sum18# ,Sum19# ,Sum20# ,Sum21# ,Sum22# ,Sum23# ,Sum24# ,Sum25# ,Sum26# ,Sum27# ,Sum28# ,Sum29# ,Sum30# ,Sum31# ,Sum32# ,Sum33# ,Sum34# ,Sum35# ,Sum36# ,Sum37# ,Sum38# ,Sum39# ,Sum40# ,Sum41# ,Sum42# ,Sum43# ,Sum44# ,Sum45# ,Sum46# ,Sum47# ,Sum48# ,Sum49# ,Sum50# ,Sum51# ,Sum52# ,Sum53# ,Sum54# ,Sum55# ,Sum56# ,Sum57# ,Sum58# ,Sum59# ,Sum60# ,Sum61# ,Sum62# ,Sum63# ,)importGHC.Classes hiding(CUnit ,CSolo ,CTuple0 ,CTuple1 ,CTuple2 ,CTuple3 ,CTuple4 ,CTuple5 ,CTuple6 ,CTuple7 ,CTuple8 ,CTuple9 ,CTuple10 ,CTuple11 ,CTuple12 ,CTuple13 ,CTuple14 ,CTuple15 ,CTuple16 ,CTuple17 ,CTuple18 ,CTuple19 ,CTuple20 ,CTuple21 ,CTuple22 ,CTuple23 ,CTuple24 ,CTuple25 ,CTuple26 ,CTuple27 ,CTuple28 ,CTuple29 ,CTuple30 ,CTuple31 ,CTuple32 ,CTuple33 ,CTuple34 ,CTuple35 ,CTuple36 ,CTuple37 ,CTuple38 ,CTuple39 ,CTuple40 ,CTuple41 ,CTuple42 ,CTuple43 ,CTuple44 ,CTuple45 ,CTuple46 ,CTuple47 ,CTuple48 ,CTuple49 ,CTuple50 ,CTuple51 ,CTuple52 ,CTuple53 ,CTuple54 ,CTuple55 ,CTuple56 ,CTuple57 ,CTuple58 ,CTuple59 ,CTuple60 ,CTuple61 ,CTuple62 ,CTuple63 ,CTuple64 ,)importGHC.CString importGHC.Magic importGHC.Magic.Dict importGHC.Prim hiding(dataToTagSmall# ,dataToTagLarge# ,whereFrom# )-- Hide dataToTag# ops because they are expected to break for-- GHC-internal reasons in the near future, and shouldn't-- be exposed from base (not even GHC.Exts)importGHC.Prim.Ext importGHC.Prim.PtrEq importGHC.Internal.Err importGHC.Internal.Maybe import{-# SOURCE#-}GHC.Internal.IO (mkUserError ,mplusIO )importGHC.Tuple (Solo (MkSolo ))-- See Note [Semigroup stimes cycle]import{-# SOURCE#-}GHC.Internal.Num (Num (..))import{-# SOURCE#-}GHC.Internal.Real (Integral (..))-- $setup-- >>> import GHC.Internal.Numinfixr9. infixr5++ infixl4<$ infixl1>> ,>>= infixr1=<< infixr0$ ,$! infixl4<*> ,<* ,*> ,<**> default()-- Double isn't available yet{-
Note [Tracking dependencies on primitives]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When desugaring certain program constructs, GHC will insert references
to the primitives used to implement those constructs even if those
primitives have not actually been imported. For example, the function
``fst (x,_) = x`` will be desugared using references to the 2-tuple
data constructor, which lives in GHC.Tuple.
When bootstrapping GHC, it is important that we do not attempt to
compile any such reference to GHC.Tuple before GHC.Tuple itself has
been built, otherwise compilation will fail with an error like this one:
 Failed to load interface for ‘GHC.Tuple’.
 There are files missing in the ‘ghc-prim-0.10.0’ package,
 try running 'ghc-pkg check'.
 Use -v to see a list of the files searched for.
To prevent such errors, we insist that if any boot library module X
implicitly depends on primitives in module Y, then the transitive
imports of X must include Y.
Such implicit dependencies can be introduced in at least the following ways:
W1:
 Common awkward dependencies:
 * TypeRep metadata introduces references to GHC.Types in EVERY module.
 * A String literal introduces a reference to GHC.CString, for either
 unpackCString# or unpackCStringUtf8# depending on its contents.
 * Tuple-notation introduces references to GHC.Tuple.
 * Constraint tuples introduce references to GHC.Classes.
 * Short lists like [3,8,2] produce references to GHC.Internal.Base.build
 A module can transitively depend on all of these by importing any of
 GHC.Internal.Base, GHC.Base, or Prelude. The latter in particular
 means that an explicit import for this reason is only necessary when
 ImplicitPrelude is disabled, so this primarily comes up in the
 dependencies of base and in the compiler itself.
 * Most modules in ghc-internal import GHC.Internal.Base.
 * Most modules in compiler/ import GHC.Prelude, which imports Prelude.
 * Most hs-boot files that would otherwise have no imports can get
 away with just importing GHC.Types.
 Unfortunately, the requirement to transitively import these modules
 when they are implicitly used is obscure and causes only /intermittent/
 build failures, so enforcement of that requirement has historically
 been pretty spotty, causing issues like #23942.
 Improving this situation is discussed at #24520.
W2:
 Non-exhaustive pattern matches, incomplete record selectors,
 missing record fields, and missing class instance methods all
 introduce references to GHC.Internal.Control.Exception.Base.
 These constructs are therefore not allowed in ghc-prim or ghc-bignum.
 But since they generally have bad code smell and are avoided by
 developers anyway, this restriction has not been very burdensome.
W3:
 Various "overloaded" bits of syntax:
 * Overloaded integer literals introduce references to GHC.Internal.Num.
 * Likewise overloaded fractional literals to GHC.Internal.Real
 * Likewise overloaded string literals to GHC.Internal.Data.String
 * Likewise overloaded list literals to GHC.Internal.IsList
 * Overloaded labels introduce references to GHC.Internal.OverloadedLabels
 * Uses of OverloadedRecordDot introduce references to GHC.Internal.Records.
 * Do-notation introduces references to GHC.Internal.Base for Monad stuff.
 * Likewise arrow-notation to GHC.Internal.Control.Arrow
 * Likewise RecursiveDo stuff to GHC.Internal.Control.Monad.Fix
 * TemplateHaskell quotes introduce references to GHC.Internal.TH.Lib.
 These are not problematic in practice, because we do not make use of
 overloaded notation during bootstrap of GHC. Yet! If in the future we
 we decide to use TemplateHaskell in GHC or `ghc-internal`, we need to
 add explicit imports. To demonstrate that these errors can occur, consider
 e n = [| True |]
 which compiles with -XTemplateHaskell *without* requiring the user to
 import GHC.Internal.TH.Lib.
W4:
 Stock derived instances introduce references to various things.
 Derived Eq instances can reference GHC.Magic.dataToTag#, for example.
 But since any module containing a derived Eq instance must import Eq,
 as long as the module which defines Eq imports GHC.Magic this cannot
 cause trouble.
 A similar solution concerns the deriving of Lift instances with
 -XTemplateHaskell: A derived Lift instance must (transitively)
 import GHC.Internal.TH.Lift, where the Lift class is defined.
 The derived Lift instance references various identifiers in
 GHC.Internal.TH.Lib, so it is an import of GHC.Internal.TH.Lift.
W5:
 If no explicit "default" declaration is present, the assumed
 "default (Integer, Double)" creates a dependency on GHC.Num.Integer
 for the Integer type if defaulting is ever attempted during
 type-checking. (This doesn't apply to hs-boot files, which can't
 be given "default" declarations anyway.)
W6:
 In the wasm backend, JSFFI imports and exports pull in a bunch of stuff;
 see Note [Desugaring JSFFI static export] and Note [Desugaring JSFFI import]
 in GHC.HsToCore.Foreign.Wasm.
A complete list could probably be made by going through the known-key
names in GHC.Builtin.Names and GHC.Builtin.Names.TH. To test whether
the transitive imports are sufficient for any single module, instruct
the build system to build /only/ that module in stage 2. For example,
a command to check whether the transitive imports for GHC.Internal.Maybe
are sufficient is:
 hadrian/build -o_checkDeps _checkDeps/stage1/libraries/ghc-internal/build/GHC/Internal/Maybe.o
Use the ".o-boot" suffix instead of ".o" to check an hs-boot file's
transitive imports.
Note [Semigroup stimes cycle]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Semigroup is defined in this module, GHC.Base, with the method
stimes :: (Semigroup a, Integral b) => b -> a -> a
This presents a problem.
* We use Integral methods (quot, rem) and Num methods (-) in stimes definitions
 in this module. Num is a superclass of Integral.
* Num is defined in GHC.Internal.Num, which imports GHC.Internal.Base.
* Enum is defined in GHC.Internal.Enum, which imports
 GHC.Internal.Base and GHC.Internal.Num. Enum is a superclass of
 Integral. We don't use any Enum methods here, but it is relevant
 (read on).
* Integral is defined in GHC.Internal.Real, which imports
 GHC.Internal.Base, GHC.Internal.Num, and GHC.Internal.Enum.
We resolve this web of dependencies with hs-boot files. The rules
https://ghc.gitlab.haskell.org/ghc/doc/users_guide/separate_compilation.html#how-to-compile-mutually-recursive-modules
require us to put either the full declarations or only the instance head for
classes in a hs-boot file.
So we put the full class decls for Num and Integral in Num.hs-boot and
Real.hs-boot respectively. This also forces us to have an Enum.hs-boot.
An obvious alternative is to move the class decls for Num, Enum, Real, and
Integral here. We don't do that because we would then need to move all the
instances (for Int, Word, Integer, etc.) here as well, or leave those instances
as orphans, which is generally bad.
We previously resolved this problem in a different way, with an hs-boot for
Semigroup.Internal that provided stimes implementations. This made them
impossible to inline or specialize when used in this module. We no longer have
that problem because we only import classes and not implementations.
-}
#if 0
-- for use when compiling GHC.Internal.Base itself doesn't workdataBool=False|TruedataOrdering=LT|EQ|GTdataChar=C#Char#typeString=[Char]dataInt=I#Int#data()=()data[]a=MkNilnotTrue=False(&&)TrueTrue=Trueotherwise=Truebuild=errorWithoutStackTrace"urk"foldr=errorWithoutStackTrace"urk"
#endif
-- | Uninhabited data type---- @since base-4.8.0.0dataVoid deriving(Void -> Void -> Bool
(Void -> Void -> Bool) -> (Void -> Void -> Bool) -> Eq Void
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Void -> Void -> Bool
== :: Void -> Void -> Bool
$c/= :: Void -> Void -> Bool
/= :: Void -> Void -> Bool
Eq -- ^ @since base-4.8.0.0,Eq Void
Eq Void =>
(Void -> Void -> Ordering)
-> (Void -> Void -> Bool)
-> (Void -> Void -> Bool)
-> (Void -> Void -> Bool)
-> (Void -> Void -> Bool)
-> (Void -> Void -> Void)
-> (Void -> Void -> Void)
-> Ord Void
Void -> Void -> Bool
Void -> Void -> Ordering
Void -> Void -> Void
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Void -> Void -> Ordering
compare :: Void -> Void -> Ordering
$c< :: Void -> Void -> Bool
< :: Void -> Void -> Bool
$c<= :: Void -> Void -> Bool
<= :: Void -> Void -> Bool
$c> :: Void -> Void -> Bool
> :: Void -> Void -> Bool
$c>= :: Void -> Void -> Bool
>= :: Void -> Void -> Bool
$cmax :: Void -> Void -> Void
max :: Void -> Void -> Void
$cmin :: Void -> Void -> Void
min :: Void -> Void -> Void
Ord -- ^ @since base-4.8.0.0)-- | Since 'Void' values logically don't exist, this witnesses the-- logical reasoning tool of \"ex falso quodlibet\".---- >>> let x :: Either Void Int; x = Right 5-- >>> :{-- case x of-- Right r -> r-- Left l -> absurd l-- :}-- 5---- @since base-4.8.0.0absurd ::Void ->a absurd :: forall a. Void -> a
absurd Void
a =caseVoid
a of{}-- | If 'Void' is uninhabited then any 'Functor' that holds only-- values of type 'Void' is holding no values.-- It is implemented in terms of @fmap absurd@.---- @since base-4.8.0.0vacuous ::Functor f =>f Void ->f a vacuous :: forall (f :: * -> *) a. Functor f => f Void -> f a
vacuous =(Void -> a) -> f Void -> f a
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Void -> a
forall a. Void -> a
absurd infixr6<> -- | The class of semigroups (types with an associative binary operation).---- Instances should satisfy the following:---- [Associativity] @x '<>' (y '<>' z) = (x '<>' y) '<>' z@---- You can alternatively define `sconcat` instead of (`<>`), in which case the-- laws are:---- [Unit]: @'sconcat' ('pure' x) = x@-- [Multiplication]: @'sconcat' ('join' xss) = 'sconcat' ('fmap' 'sconcat' xss)@---- @since base-4.9.0.0classSemigroup a where-- | An associative operation.---- ==== __Examples__---- >>> [1,2,3] <> [4,5,6]-- [1,2,3,4,5,6]---- >>> Just [1, 2, 3] <> Just [4, 5, 6]-- Just [1,2,3,4,5,6]---- >>> putStr "Hello, " <> putStrLn "World!"-- Hello, World!(<>) ::a ->a ->a a
a <> a
b =NonEmpty a -> a
forall a. Semigroup a => NonEmpty a -> a
sconcat (a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a
b ])-- | Reduce a non-empty list with '<>'---- The default definition should be sufficient, but this can be-- overridden for efficiency.---- ==== __Examples__---- For the following examples, we will assume that we have:---- >>> import Data.List.NonEmpty (NonEmpty (..))---- >>> sconcat $ "Hello" :| [" ", "Haskell", "!"]-- "Hello Haskell!"---- >>> sconcat $ Just [1, 2, 3] :| [Nothing, Just [4, 5, 6]]-- Just [1,2,3,4,5,6]---- >>> sconcat $ Left 1 :| [Right 2, Left 3, Right 4]-- Right 2sconcat ::NonEmpty a ->a sconcat (a
a :| [a]
as )=a -> [a] -> a
forall {t}. Semigroup t => t -> [t] -> t
go a
a [a]
as wherego :: t -> [t] -> t
go t
b (t
c : [t]
cs )=t
b t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t -> [t] -> t
go t
c [t]
cs go t
b []=t
b -- | Repeat a value @n@ times.---- The default definition will raise an exception for a multiplier that is @<= 0@.-- This may be overridden with an implementation that is total. For monoids-- it is preferred to use 'stimesMonoid'.---- By making this a member of the class, idempotent semigroups-- and monoids can upgrade this to execute in \(\mathcal{O}(1)\) by-- picking @stimes = 'Data.Semigroup.stimesIdempotent'@ or @stimes =-- 'Data.Semigroup.stimesIdempotentMonoid'@ respectively.---- ==== __Examples__---- >>> stimes 4 [1]-- [1,1,1,1]---- >>> stimes 5 (putStr "hi!")-- hi!hi!hi!hi!hi!---- >>> stimes 3 (Right ":)")-- Right ":)"stimes ::Integral b =>b ->a ->a stimes b
y0 a
x0 |b
y0 b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0=[Char] -> a
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"stimes: positive multiplier expected"|Bool
otherwise =a -> b -> a
forall {a} {t}. (Integral a, Semigroup t) => t -> a -> t
f a
x0 b
y0 wheref :: t -> a -> t
f t
x a
y |a
y a -> a -> a
forall a. Integral a => a -> a -> a
`rem` a
2a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0=t -> a -> t
f (t
x t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
x )(a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2)|a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
1=t
x |Bool
otherwise =t -> a -> t -> t
forall {a} {t}. (Integral a, Semigroup t) => t -> a -> t -> t
g (t
x t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
x )(a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2)t
x -- See Note [Half of y - 1]g :: t -> a -> t -> t
g t
x a
y t
z |a
y a -> a -> a
forall a. Integral a => a -> a -> a
`rem` a
2a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0=t -> a -> t -> t
g (t
x t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
x )(a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2)t
z |a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
1=t
x t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
z |Bool
otherwise =t -> a -> t -> t
g (t
x t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
x )(a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2)(t
x t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
z )-- See Note [Half of y - 1]{-# MINIMAL(<>)|sconcat #-}{- Note [Half of y - 1]
 ~~~~~~~~~~~~~~~~~~~~~
 Since y is guaranteed to be odd and positive here,
 half of y - 1 can be computed as y `quot` 2, optimising subtraction away.
-}-- | The class of monoids (types with an associative binary operation that-- has an identity). Instances should satisfy the following:---- [Right identity] @x '<>' 'mempty' = x@-- [Left identity] @'mempty' '<>' x = x@-- [Associativity] @x '<>' (y '<>' z) = (x '<>' y) '<>' z@ ('Semigroup' law)-- [Concatenation] @'mconcat' = 'foldr' ('<>') 'mempty'@---- You can alternatively define `mconcat` instead of `mempty`, in which case the-- laws are:---- [Unit]: @'mconcat' ('pure' x) = x@-- [Multiplication]: @'mconcat' ('join' xss) = 'mconcat' ('fmap' 'mconcat' xss)@-- [Subclass]: @'mconcat' ('toList' xs) = 'sconcat' xs@---- The method names refer to the monoid of lists under concatenation,-- but there are many other instances.---- Some types can be viewed as a monoid in more than one way,-- e.g. both addition and multiplication on numbers.-- In such cases we often define @newtype@s and make those instances-- of 'Monoid', e.g. 'Data.Semigroup.Sum' and 'Data.Semigroup.Product'.---- __NOTE__: 'Semigroup' is a superclass of 'Monoid' since /base-4.11.0.0/.classSemigroup a =>Monoid a where-- | Identity of 'mappend'---- ==== __Examples__-- >>> "Hello world" <> mempty-- "Hello world"---- >>> mempty <> [1, 2, 3]-- [1,2,3]mempty ::a mempty =[a] -> a
forall a. Monoid a => [a] -> a
mconcat []{-# INLINEmempty #-}-- | An associative operation---- __NOTE__: This method is redundant and has the default-- implementation @'mappend' = ('<>')@ since /base-4.11.0.0/.-- Should it be implemented manually, since 'mappend' is a synonym for-- ('<>'), it is expected that the two functions are defined the same-- way. In a future GHC release 'mappend' will be removed from 'Monoid'.mappend ::a ->a ->a mappend =a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) {-# INLINEmappend #-}-- | Fold a list using the monoid.---- For most types, the default definition for 'mconcat' will be-- used, but the function is included in the class definition so-- that an optimized version can be provided for specific types.---- >>> mconcat ["Hello", " ", "Haskell", "!"]-- "Hello Haskell!"mconcat ::[a ]->a mconcat =(a -> a -> a) -> a -> [a] -> a
forall a b. (a -> b -> b) -> b -> [a] -> b
foldr a -> a -> a
forall a. Monoid a => a -> a -> a
mappend a
forall a. Monoid a => a
mempty {-# INLINEmconcat #-}-- INLINE in the hope of fusion with mconcat's argument (see !4890){-# MINIMALmempty |mconcat #-}-- | @since base-4.9.0.0instanceSemigroup [a ]where<> :: [a] -> [a] -> [a]
(<>) =[a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
(++) {-# INLINE(<>)#-}stimes :: forall b. Integral b => b -> [a] -> [a]
stimes b
n [a]
x |b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
0=[Char] -> [a]
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"stimes: [], negative multiplier"|Bool
otherwise =b -> [a]
forall {t}. (Eq t, Num t) => t -> [a]
rep b
n whererep :: t -> [a]
rep t
0=[]rep t
i =[a]
x [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ t -> [a]
rep (t
i t -> t -> t
forall a. Num a => a -> a -> a
- t
1)-- | @since base-2.01instanceMonoid [a ]where{-# INLINEmempty #-}mempty :: [a]
mempty =[]{-# INLINEmconcat #-}mconcat :: [[a]] -> [a]
mconcat [[a]]
xss =[a
x |[a]
xs <-[[a]]
xss ,a
x <-[a]
xs ]-- See Note: [List comprehensions and inlining]-- | @since base-4.9.0.0instanceSemigroup Void whereVoid
a <> :: Void -> Void -> Void
<> Void
_=Void
a stimes :: forall b. Integral b => b -> Void -> Void
stimes b
_Void
a =Void
a {-
Note: [List comprehensions and inlining]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The list monad operations are traditionally described in terms of concatMap:
xs >>= f = concatMap f xs
Similarly, mconcat for lists is just concat. Here in Base, however, we don't
have concatMap, and we'll refrain from adding it here so it won't have to be
hidden in imports. Instead, we use GHC's list comprehension desugaring
mechanism to define mconcat and the Applicative and Monad instances for lists.
We mark them INLINE because the inliner is not generally too keen to inline
build forms such as the ones these desugar to without our insistence. Defining
these using list comprehensions instead of foldr has an additional potential
benefit, as described in compiler/GHC/HsToCore/ListComp.hs: if optimizations
needed to make foldr/build forms efficient are turned off, we'll get reasonably
efficient translations anyway.
-}-- | @since base-4.9.0.0instanceSemigroup (NonEmpty a )where(a
a :| [a]
as )<> :: NonEmpty a -> NonEmpty a -> NonEmpty a
<> ~(a
b :| [a]
bs )=a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| ([a]
as [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
bs )-- | @since base-4.9.0.0instanceSemigroup b =>Semigroup (a ->b )wherea -> b
f <> :: (a -> b) -> (a -> b) -> a -> b
<> a -> b
g =\a
x ->a -> b
f a
x b -> b -> b
forall a. Semigroup a => a -> a -> a
<> a -> b
g a
x stimes :: forall b. Integral b => b -> (a -> b) -> a -> b
stimes b
n a -> b
f a
e =b -> b -> b
forall b. Integral b => b -> b -> b
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n (a -> b
f a
e )-- | @since base-2.01instanceMonoid b =>Monoid (a ->b )wheremempty :: a -> b
mempty a
_=b
forall a. Monoid a => a
mempty -- If `b` has a specialised mconcat, use that, rather than the default-- mconcat, which can be much less efficient. Inline in the hope that-- it may result in list fusion.mconcat :: [a -> b] -> a -> b
mconcat =\[a -> b]
fs a
x ->[b] -> b
forall a. Monoid a => [a] -> a
mconcat ([b] -> b) -> [b] -> b
forall a b. (a -> b) -> a -> b
$ ((a -> b) -> b) -> [a -> b] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map (\a -> b
f ->a -> b
f a
x )[a -> b]
fs {-# INLINEmconcat #-}-- | @since base-4.9.0.0instanceSemigroup ()where()
_<> :: () -> () -> ()
<> ()
_=()sconcat :: NonEmpty () -> ()
sconcat NonEmpty ()
_=()stimes :: forall b. Integral b => b -> () -> ()
stimes b
_()
_=()-- | @since base-2.01instanceMonoid ()where-- Should it be strict?mempty :: ()
mempty =()mconcat :: [()] -> ()
mconcat [()]
_=()-- | @since base-4.15instanceSemigroup a =>Semigroup (Solo a )whereMkSolo a
a <> :: Solo a -> Solo a -> Solo a
<> MkSolo a
b =a -> Solo a
forall a. a -> Solo a
MkSolo (a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
b )stimes :: forall b. Integral b => b -> Solo a -> Solo a
stimes b
n (MkSolo a
a )=a -> Solo a
forall a. a -> Solo a
MkSolo (b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
a )-- | @since base-4.15instanceMonoid a =>Monoid (Solo a )wheremempty :: Solo a
mempty =a -> Solo a
forall a. a -> Solo a
MkSolo a
forall a. Monoid a => a
mempty -- | @since base-4.9.0.0instance(Semigroup a ,Semigroup b )=>Semigroup (a ,b )where(a
a ,b
b )<> :: (a, b) -> (a, b) -> (a, b)
<> (a
a' ,b
b' )=(a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a' ,b
b b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
b' )stimes :: forall b. Integral b => b -> (a, b) -> (a, b)
stimes b
n (a
a ,b
b )=(b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
a ,b -> b -> b
forall b. Integral b => b -> b -> b
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n b
b )-- | @since base-2.01instance(Monoid a ,Monoid b )=>Monoid (a ,b )wheremempty :: (a, b)
mempty =(a
forall a. Monoid a => a
mempty ,b
forall a. Monoid a => a
mempty )-- | @since base-4.9.0.0instance(Semigroup a ,Semigroup b ,Semigroup c )=>Semigroup (a ,b ,c )where(a
a ,b
b ,c
c )<> :: (a, b, c) -> (a, b, c) -> (a, b, c)
<> (a
a' ,b
b' ,c
c' )=(a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a' ,b
b b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
b' ,c
c c -> c -> c
forall a. Semigroup a => a -> a -> a
<> c
c' )stimes :: forall b. Integral b => b -> (a, b, c) -> (a, b, c)
stimes b
n (a
a ,b
b ,c
c )=(b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
a ,b -> b -> b
forall b. Integral b => b -> b -> b
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n b
b ,b -> c -> c
forall b. Integral b => b -> c -> c
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n c
c )-- | @since base-2.01instance(Monoid a ,Monoid b ,Monoid c )=>Monoid (a ,b ,c )wheremempty :: (a, b, c)
mempty =(a
forall a. Monoid a => a
mempty ,b
forall a. Monoid a => a
mempty ,c
forall a. Monoid a => a
mempty )-- | @since base-4.9.0.0instance(Semigroup a ,Semigroup b ,Semigroup c ,Semigroup d )=>Semigroup (a ,b ,c ,d )where(a
a ,b
b ,c
c ,d
d )<> :: (a, b, c, d) -> (a, b, c, d) -> (a, b, c, d)
<> (a
a' ,b
b' ,c
c' ,d
d' )=(a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a' ,b
b b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
b' ,c
c c -> c -> c
forall a. Semigroup a => a -> a -> a
<> c
c' ,d
d d -> d -> d
forall a. Semigroup a => a -> a -> a
<> d
d' )stimes :: forall b. Integral b => b -> (a, b, c, d) -> (a, b, c, d)
stimes b
n (a
a ,b
b ,c
c ,d
d )=(b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
a ,b -> b -> b
forall b. Integral b => b -> b -> b
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n b
b ,b -> c -> c
forall b. Integral b => b -> c -> c
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n c
c ,b -> d -> d
forall b. Integral b => b -> d -> d
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n d
d )-- | @since base-2.01instance(Monoid a ,Monoid b ,Monoid c ,Monoid d )=>Monoid (a ,b ,c ,d )wheremempty :: (a, b, c, d)
mempty =(a
forall a. Monoid a => a
mempty ,b
forall a. Monoid a => a
mempty ,c
forall a. Monoid a => a
mempty ,d
forall a. Monoid a => a
mempty )-- | @since base-4.9.0.0instance(Semigroup a ,Semigroup b ,Semigroup c ,Semigroup d ,Semigroup e )=>Semigroup (a ,b ,c ,d ,e )where(a
a ,b
b ,c
c ,d
d ,e
e )<> :: (a, b, c, d, e) -> (a, b, c, d, e) -> (a, b, c, d, e)
<> (a
a' ,b
b' ,c
c' ,d
d' ,e
e' )=(a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a' ,b
b b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
b' ,c
c c -> c -> c
forall a. Semigroup a => a -> a -> a
<> c
c' ,d
d d -> d -> d
forall a. Semigroup a => a -> a -> a
<> d
d' ,e
e e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e' )stimes :: forall b. Integral b => b -> (a, b, c, d, e) -> (a, b, c, d, e)
stimes b
n (a
a ,b
b ,c
c ,d
d ,e
e )=(b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
a ,b -> b -> b
forall b. Integral b => b -> b -> b
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n b
b ,b -> c -> c
forall b. Integral b => b -> c -> c
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n c
c ,b -> d -> d
forall b. Integral b => b -> d -> d
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n d
d ,b -> e -> e
forall b. Integral b => b -> e -> e
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n e
e )-- | @since base-2.01instance(Monoid a ,Monoid b ,Monoid c ,Monoid d ,Monoid e )=>Monoid (a ,b ,c ,d ,e )wheremempty :: (a, b, c, d, e)
mempty =(a
forall a. Monoid a => a
mempty ,b
forall a. Monoid a => a
mempty ,c
forall a. Monoid a => a
mempty ,d
forall a. Monoid a => a
mempty ,e
forall a. Monoid a => a
mempty )-- | @since base-4.9.0.0instanceSemigroup Ordering whereOrdering
LT <> :: Ordering -> Ordering -> Ordering
<> Ordering
_=Ordering
LT Ordering
EQ <> Ordering
y =Ordering
y Ordering
GT <> Ordering
_=Ordering
GT stimes :: forall b. Integral b => b -> Ordering -> Ordering
stimes b
n Ordering
x =caseb -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare b
n b
0ofOrdering
LT ->[Char] -> Ordering
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"stimes: Ordering, negative multiplier"Ordering
EQ ->Ordering
EQ Ordering
GT ->Ordering
x -- lexicographical ordering-- | @since base-2.01instanceMonoid Ordering wheremempty :: Ordering
mempty =Ordering
EQ -- | @since base-4.9.0.0instanceSemigroup a =>Semigroup (Maybe a )whereMaybe a
Nothing <> :: Maybe a -> Maybe a -> Maybe a
<> Maybe a
b =Maybe a
b Maybe a
a <> Maybe a
Nothing =Maybe a
a Just a
a <> Just a
b =a -> Maybe a
forall a. a -> Maybe a
Just (a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
b )stimes :: forall b. Integral b => b -> Maybe a -> Maybe a
stimes b
_Maybe a
Nothing =Maybe a
forall a. Maybe a
Nothing stimes b
n (Just a
a )=caseb -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare b
n b
0ofOrdering
LT ->[Char] -> Maybe a
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"stimes: Maybe, negative multiplier"Ordering
EQ ->Maybe a
forall a. Maybe a
Nothing Ordering
GT ->a -> Maybe a
forall a. a -> Maybe a
Just (b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n a
a )-- | Lift a semigroup into 'Maybe' forming a 'Monoid' according to-- <http://en.wikipedia.org/wiki/Monoid>: \"Any semigroup @S@ may be-- turned into a monoid simply by adjoining an element @e@ not in @S@-- and defining @e*e = e@ and @e*s = s = s*e@ for all @s ∈ S@.\"---- /Since 4.11.0/: constraint on inner @a@ value generalised from-- 'Monoid' to 'Semigroup'.---- @since base-2.01instanceSemigroup a =>Monoid (Maybe a )wheremempty :: Maybe a
mempty =Maybe a
forall a. Maybe a
Nothing -- | @since base-4.15instanceApplicative Solo wherepure :: forall a. a -> Solo a
pure =a -> Solo a
forall a. a -> Solo a
MkSolo -- Note: we really want to match strictly here. This lets us write,-- for example,---- forceSpine :: Foldable f => f a -> ()-- forceSpine xs-- | MkSolo r <- traverse_ MkSolo xs-- = rMkSolo a -> b
f <*> :: forall a b. Solo (a -> b) -> Solo a -> Solo b
<*> MkSolo a
x =b -> Solo b
forall a. a -> Solo a
MkSolo (a -> b
f a
x )liftA2 :: forall a b c. (a -> b -> c) -> Solo a -> Solo b -> Solo c
liftA2 a -> b -> c
f (MkSolo a
x )(MkSolo b
y )=c -> Solo c
forall a. a -> Solo a
MkSolo (a -> b -> c
f a
x b
y )-- | For tuples, the 'Monoid' constraint on @a@ determines-- how the first values merge.-- For example, 'String's concatenate:---- > ("hello ", (+15)) <*> ("world!", 2002)-- > ("hello world!",2017)---- @since base-2.01instanceMonoid a =>Applicative ((,)a )wherepure :: forall a. a -> (a, a)
pure a
x =(a
forall a. Monoid a => a
mempty ,a
x )(a
u ,a -> b
f )<*> :: forall a b. (a, a -> b) -> (a, a) -> (a, b)
<*> (a
v ,a
x )=(a
u a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
v ,a -> b
f a
x )liftA2 :: forall a b c. (a -> b -> c) -> (a, a) -> (a, b) -> (a, c)
liftA2 a -> b -> c
f (a
u ,a
x )(a
v ,b
y )=(a
u a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
v ,a -> b -> c
f a
x b
y )-- | @since base-4.15instanceMonad Solo whereMkSolo a
x >>= :: forall a b. Solo a -> (a -> Solo b) -> Solo b
>>= a -> Solo b
f =a -> Solo b
f a
x -- | @since base-4.9.0.0instanceMonoid a =>Monad ((,)a )where(a
u ,a
a )>>= :: forall a b. (a, a) -> (a -> (a, b)) -> (a, b)
>>= a -> (a, b)
k =casea -> (a, b)
k a
a of(a
v ,b
b )->(a
u a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
v ,b
b )-- | @since base-4.14.0.0instanceFunctor ((,,)a b )wherefmap :: forall a b. (a -> b) -> (a, b, a) -> (a, b, b)
fmap a -> b
f (a
a ,b
b ,a
c )=(a
a ,b
b ,a -> b
f a
c )-- | @since base-4.14.0.0instance(Monoid a ,Monoid b )=>Applicative ((,,)a b )wherepure :: forall a. a -> (a, b, a)
pure a
x =(a
forall a. Monoid a => a
mempty ,b
forall a. Monoid a => a
mempty ,a
x )(a
a ,b
b ,a -> b
f )<*> :: forall a b. (a, b, a -> b) -> (a, b, a) -> (a, b, b)
<*> (a
a' ,b
b' ,a
x )=(a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a' ,b
b b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
b' ,a -> b
f a
x )-- | @since base-4.14.0.0instance(Monoid a ,Monoid b )=>Monad ((,,)a b )where(a
u ,b
v ,a
a )>>= :: forall a b. (a, b, a) -> (a -> (a, b, b)) -> (a, b, b)
>>= a -> (a, b, b)
k =casea -> (a, b, b)
k a
a of(a
u' ,b
v' ,b
b )->(a
u a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
u' ,b
v b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
v' ,b
b )-- | @since base-4.14.0.0instanceFunctor ((,,,)a b c )wherefmap :: forall a b. (a -> b) -> (a, b, c, a) -> (a, b, c, b)
fmap a -> b
f (a
a ,b
b ,c
c ,a
d )=(a
a ,b
b ,c
c ,a -> b
f a
d )-- | @since base-4.14.0.0instance(Monoid a ,Monoid b ,Monoid c )=>Applicative ((,,,)a b c )wherepure :: forall a. a -> (a, b, c, a)
pure a
x =(a
forall a. Monoid a => a
mempty ,b
forall a. Monoid a => a
mempty ,c
forall a. Monoid a => a
mempty ,a
x )(a
a ,b
b ,c
c ,a -> b
f )<*> :: forall a b. (a, b, c, a -> b) -> (a, b, c, a) -> (a, b, c, b)
<*> (a
a' ,b
b' ,c
c' ,a
x )=(a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a' ,b
b b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
b' ,c
c c -> c -> c
forall a. Semigroup a => a -> a -> a
<> c
c' ,a -> b
f a
x )-- | @since base-4.14.0.0instance(Monoid a ,Monoid b ,Monoid c )=>Monad ((,,,)a b c )where(a
u ,b
v ,c
w ,a
a )>>= :: forall a b. (a, b, c, a) -> (a -> (a, b, c, b)) -> (a, b, c, b)
>>= a -> (a, b, c, b)
k =casea -> (a, b, c, b)
k a
a of(a
u' ,b
v' ,c
w' ,b
b )->(a
u a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
u' ,b
v b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
v' ,c
w c -> c -> c
forall a. Semigroup a => a -> a -> a
<> c
w' ,b
b )-- | @since base-4.18.0.0instanceFunctor ((,,,,)a b c d )wherefmap :: forall a b. (a -> b) -> (a, b, c, d, a) -> (a, b, c, d, b)
fmap a -> b
f (a
a ,b
b ,c
c ,d
d ,a
e )=(a
a ,b
b ,c
c ,d
d ,a -> b
f a
e )-- | @since base-4.18.0.0instanceFunctor ((,,,,,)a b c d e )wherefmap :: forall a b. (a -> b) -> (a, b, c, d, e, a) -> (a, b, c, d, e, b)
fmap a -> b
fun (a
a ,b
b ,c
c ,d
d ,e
e ,a
f )=(a
a ,b
b ,c
c ,d
d ,e
e ,a -> b
fun a
f )-- | @since base-4.18.0.0instanceFunctor ((,,,,,,)a b c d e f )wherefmap :: forall a b.
(a -> b) -> (a, b, c, d, e, f, a) -> (a, b, c, d, e, f, b)
fmap a -> b
fun (a
a ,b
b ,c
c ,d
d ,e
e ,f
f ,a
g )=(a
a ,b
b ,c
c ,d
d ,e
e ,f
f ,a -> b
fun a
g )-- | @since base-4.10.0.0instanceSemigroup a =>Semigroup (IO a )where<> :: IO a -> IO a -> IO a
(<>) =(a -> a -> a) -> IO a -> IO a -> IO a
forall a b c. (a -> b -> c) -> IO a -> IO b -> IO c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) -- | @since base-4.9.0.0instanceMonoid a =>Monoid (IO a )wheremempty :: IO a
mempty =a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty {- | A type @f@ is a Functor if it provides a function @fmap@ which, given any types @a@ and @b@
lets you apply any function from @(a -> b)@ to turn an @f a@ into an @f b@, preserving the
structure of @f@. Furthermore @f@ needs to adhere to the following:
[Identity] @'fmap' 'id' == 'id'@
[Composition] @'fmap' (f . g) == 'fmap' f . 'fmap' g@
Note, that the second law follows from the free theorem of the type 'fmap' and
the first law, so you need only check that the former condition holds.
See these articles by <https://www.schoolofhaskell.com/user/edwardk/snippets/fmap School of Haskell> or
<https://github.com/quchen/articles/blob/master/second_functor_law.md David Luposchainsky>
for an explanation.
-}classFunctor f where-- | 'fmap' is used to apply a function of type @(a -> b)@ to a value of type @f a@,-- where f is a functor, to produce a value of type @f b@.-- Note that for any type constructor with more than one parameter (e.g., 'Either'),-- only the last type parameter can be modified with `fmap` (e.g., `b` in `Either a b`).---- Some type constructors with two parameters or more have a @'Data.Bifunctor'@ instance that allows-- both the last and the penultimate parameters to be mapped over.---- ==== __Examples__---- Convert from a @'Data.Maybe.Maybe' Int@ to a @Maybe String@-- using 'Prelude.show':---- >>> fmap show Nothing-- Nothing-- >>> fmap show (Just 3)-- Just "3"---- Convert from an @'Data.Either.Either' Int Int@ to an-- @Either Int String@ using 'Prelude.show':---- >>> fmap show (Left 17)-- Left 17-- >>> fmap show (Right 17)-- Right "17"---- Double each element of a list:---- >>> fmap (*2) [1,2,3]-- [2,4,6]---- Apply 'Prelude.even' to the second element of a pair:---- >>> fmap even (2,2)-- (2,True)---- It may seem surprising that the function is only applied to the last element of the tuple-- compared to the list example above which applies it to every element in the list.-- To understand, remember that tuples are type constructors with multiple type parameters:-- a tuple of 3 elements @(a,b,c)@ can also be written @(,,) a b c@ and its @Functor@ instance-- is defined for @Functor ((,,) a b)@ (i.e., only the third parameter is free to be mapped over-- with @fmap@).---- It explains why @fmap@ can be used with tuples containing values of different types as in the-- following example:---- >>> fmap even ("hello", 1.0, 4)-- ("hello",1.0,True)fmap ::(a ->b )->f a ->f b -- | Replace all locations in the input with the same value.-- The default definition is @'fmap' . 'const'@, but this may be-- overridden with a more efficient version.---- ==== __Examples__---- Perform a computation with 'Maybe' and replace the result with a-- constant value if it is 'Just':---- >>> 'a' <$ Just 2-- Just 'a'-- >>> 'a' <$ Nothing-- Nothing(<$) ::a ->f b ->f a (<$) =(b -> a) -> f b -> f a
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> a) -> f b -> f a) -> (a -> b -> a) -> a -> f b -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> a
forall a b. a -> b -> a
const -- | A functor with application, providing operations to---- * embed pure expressions ('pure'), and---- * sequence computations and combine their results ('<*>' and 'liftA2').---- A minimal complete definition must include implementations of 'pure'-- and of either '<*>' or 'liftA2'. If it defines both, then they must behave-- the same as their default definitions:---- @('<*>') = 'liftA2' 'id'@---- @'liftA2' f x y = f 'Prelude.<$>' x '<*>' y@---- Further, any definition must satisfy the following:---- [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 = ('id' '<$' u) '<*>' v@---- * @u '<*' v = 'liftA2' 'const' u v@---- As a consequence of these laws, the 'Functor' instance for @f@ will satisfy---- * @'fmap' f x = 'pure' f '<*>' x@------ It may be useful to note that supposing---- @forall x y. p (q x y) = f x . g y@---- it follows from the above that---- @'liftA2' p ('liftA2' q u v) = 'liftA2' f u . 'liftA2' g v@------ If @f@ is also a 'Monad', it should satisfy---- * @'pure' = 'return'@---- * @m1 '<*>' m2 = m1 '>>=' (\\x1 -> m2 '>>=' (\\x2 -> 'return' (x1 x2)))@---- * @('*>') = ('>>')@---- (which implies that 'pure' and '<*>' satisfy the applicative functor laws).classFunctor f =>Applicative f where{-# MINIMALpure ,((<*>)|liftA2 )#-}-- | Lift a value into the Structure.---- ==== __Examples__---- >>> pure 1 :: Maybe Int-- Just 1---- >>> pure 'z' :: [Char]-- "z"---- >>> pure (pure ":D") :: Maybe [String]-- Just [":D"]pure ::a ->f a -- | Sequential application.---- A few functors support an implementation of '<*>' that is more-- efficient than the default one.---- ==== __Example__-- Used in combination with @'(Data.Functor.<$>)'@, @'(<*>)'@ can be used to build a record.---- >>> data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}---- >>> produceFoo :: Applicative f => f Foo-- >>> produceBar :: Applicative f => f Bar-- >>> produceBaz :: Applicative f => f Baz---- >>> mkState :: Applicative f => f MyState-- >>> mkState = MyState <$> produceFoo <*> produceBar <*> produceBaz(<*>) ::f (a ->b )->f a ->f b (<*>) =((a -> b) -> a -> b) -> f (a -> b) -> f a -> f b
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (a -> b) -> a -> b
forall a. a -> a
id -- | Lift a binary function to actions.---- Some functors support an implementation of 'liftA2' that is more-- efficient than the default one. In particular, if 'fmap' is an-- expensive operation, it is likely better to use 'liftA2' than to-- 'fmap' over the structure and then use '<*>'.---- This became a typeclass method in 4.10.0.0. Prior to that, it was-- a function defined in terms of '<*>' and 'fmap'.---- ==== __Example__---- >>> liftA2 (,) (Just 3) (Just 5)-- Just (3,5)---- >>> liftA2 (+) [1, 2, 3] [4, 5, 6]-- [5,6,7,6,7,8,7,8,9]liftA2 ::(a ->b ->c )->f a ->f b ->f c liftA2 a -> b -> c
f f a
x =f (b -> c) -> f b -> f c
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) ((a -> b -> c) -> f a -> f (b -> c)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b -> c
f f a
x )-- | Sequence actions, discarding the value of the first argument.---- ==== __Examples__-- If used in conjunction with the Applicative instance for 'Maybe',-- you can chain Maybe computations, with a possible "early return"-- in case of 'Nothing'.---- >>> Just 2 *> Just 3-- Just 3---- >>> Nothing *> Just 3-- Nothing---- Of course a more interesting use case would be to have effectful-- computations instead of just returning pure values.---- >>> import Data.Char-- >>> import GHC.Internal.Text.ParserCombinators.ReadP-- >>> let p = string "my name is " *> munch1 isAlpha <* eof-- >>> readP_to_S p "my name is Simon"-- [("Simon","")](*>) ::f a ->f b ->f b f a
a1 *> f b
a2 =(b -> b
forall a. a -> a
id (b -> b) -> f a -> f (b -> b)
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f a
a1 )f (b -> b) -> f b -> f b
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f b
a2 -- This is essentially the same as liftA2 (flip const), but if the-- Functor instance has an optimized (<$), it may be better to use-- that instead. Before liftA2 became a method, this definition-- was strictly better, but now it depends on the functor. For a-- functor supporting a sharing-enhancing (<$), this definition-- may reduce allocation by preventing a1 from ever being fully-- realized. In an implementation with a boring (<$) but an optimizing-- liftA2, it would likely be better to define (*>) using liftA2.-- | Sequence actions, discarding the value of the second argument.--(<*) ::f a ->f b ->f a (<*) =(a -> b -> a) -> f a -> f b -> f a
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> a
forall a b. a -> b -> a
const -- | A variant of '<*>' with the types of the arguments reversed. It differs from-- @`flip` `(<*>)`@ in that the effects are resolved in the order the arguments are-- presented.---- ==== __Examples__-- >>> (<**>) (print 1) (id <$ print 2)-- 1-- 2---- >>> flip (<*>) (print 1) (id <$ print 2)-- 2-- 1---- >>> ZipList [4, 5, 6] <**> ZipList [(+1), (*2), (/3)]-- ZipList {getZipList = [5.0,10.0,2.0]}(<**>) ::Applicative f =>f a ->f (a ->b )->f b <**> :: forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
(<**>) =(a -> (a -> b) -> b) -> f a -> f (a -> b) -> f b
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a
a a -> b
f ->a -> b
f a
a )-- | Lift a function to actions.-- Equivalent to Functor's `fmap` but implemented using only `Applicative`'s methods:-- @'liftA' f a = 'pure' f '<*>' a@---- As such this function may be used to implement a `Functor` instance from an `Applicative` one.---- ==== __Examples__-- Using the Applicative instance for Lists:---- >>> liftA (+1) [1, 2]-- [2,3]---- Or the Applicative instance for 'Maybe'---- >>> liftA (+1) (Just 3)-- Just 4liftA ::Applicative f =>(a ->b )->f a ->f b liftA :: forall (f :: * -> *) a b. Applicative f => (a -> b) -> f a -> f b
liftA a -> b
f f a
a =(a -> b) -> f (a -> b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a -> b
f f (a -> b) -> f a -> f b
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
a -- Caution: since this may be used for `fmap`, we can't use the obvious-- definition of liftA = fmap.-- | Lift a ternary function to actions.liftA3 ::Applicative f =>(a ->b ->c ->d )->f a ->f b ->f c ->f d liftA3 :: forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 a -> b -> c -> d
f f a
a f b
b f c
c =(a -> b -> c -> d) -> f a -> f b -> f (c -> d)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c -> d
f f a
a f b
b f (c -> d) -> f c -> f d
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f c
c {-# INLINABLEliftA #-}{-# SPECIALISEliftA ::(a1 ->r )->IO a1 ->IO r #-}{-# SPECIALISEliftA ::(a1 ->r )->Maybe a1 ->Maybe r #-}{-# INLINABLEliftA3 #-}{-# SPECIALISEliftA3 ::(a1 ->a2 ->a3 ->r )->IO a1 ->IO a2 ->IO a3 ->IO r #-}{-# SPECIALISEliftA3 ::(a1 ->a2 ->a3 ->r )->Maybe a1 ->Maybe a2 ->Maybe a3 ->Maybe r #-}-- | The 'join' function is the conventional monad join operator. It-- is used to remove one level of monadic structure, projecting its-- bound argument into the outer level.------ \'@'join' bss@\' can be understood as the @do@ expression---- @-- do bs <- bss-- bs-- @---- ==== __Examples__---- >>> join [[1, 2, 3], [4, 5, 6], [7, 8, 9]]-- [1,2,3,4,5,6,7,8,9]---- >>> join (Just (Just 3))-- Just 3---- A common use of 'join' is to run an 'IO' computation returned from-- an 'GHC.Conc.STM' transaction, since 'GHC.Conc.STM' transactions-- can't perform 'IO' directly. Recall that---- @-- 'GHC.Internal.Conc.atomically' :: STM a -> IO a-- @---- is used to run 'GHC.Conc.STM' transactions atomically. So, by-- specializing the types of 'GHC.Internal.Conc.atomically' and 'join' to---- @-- 'GHC.Internal.Conc.atomically' :: STM (IO b) -> IO (IO b)-- 'join' :: IO (IO b) -> IO b-- @---- we can compose them as---- @-- 'join' . 'GHC.Internal.Conc.atomically' :: STM (IO b) -> IO b-- @---- to run an 'GHC.Conc.STM' transaction and the 'IO' action it-- returns.join ::(Monad m )=>m (m a )->m a join :: forall (m :: * -> *) a. Monad m => m (m a) -> m a
join m (m a)
x =m (m a)
x m (m a) -> (m a -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m a -> m a
forall a. a -> a
id {- | The 'Monad' class defines the basic operations over a /monad/,
a concept from a branch of mathematics known as /category theory/.
From the perspective of a Haskell programmer, however, it is best to
think of a monad as an /abstract datatype/ of actions.
Haskell's @do@ expressions provide a convenient syntax for writing
monadic expressions.
Instances of 'Monad' should satisfy the following:
[Left identity] @'return' a '>>=' k = k a@
[Right identity] @m '>>=' 'return' = m@
[Associativity] @m '>>=' (\\x -> k x '>>=' h) = (m '>>=' k) '>>=' h@
Furthermore, the 'Monad' and 'Applicative' operations should relate as follows:
* @'pure' = 'return'@
* @m1 '<*>' m2 = m1 '>>=' (\\x1 -> m2 '>>=' (\\x2 -> 'return' (x1 x2)))@
The above laws imply:
* @'fmap' f xs = xs '>>=' 'return' . f@
* @('>>') = ('*>')@
and that 'pure' and ('<*>') satisfy the applicative functor laws.
The instances of 'Monad' for 'GHC.List.List', 'Data.Maybe.Maybe' and 'System.IO.IO'
defined in the "Prelude" satisfy these laws.
-}classApplicative m =>Monad m where-- | Sequentially compose two actions, passing any value produced-- by the first as an argument to the second.---- \'@as '>>=' bs@\' can be understood as the @do@ expression---- @-- do a <- as-- bs a-- @---- An alternative name for this function is \'bind\', but some people-- may refer to it as \'flatMap\', which results from it being equivalent-- to---- @\\x f -> 'join' ('fmap' f x) :: Monad m => m a -> (a -> m b) -> m b@---- which can be seen as mapping a value with-- @Monad m => m a -> m (m b)@ and then \'flattening\' @m (m b)@ to @m b@ using 'join'.(>>=) ::foralla b .m a ->(a ->m b )->m b -- | Sequentially compose two actions, discarding any value produced-- by the first, like sequencing operators (such as the semicolon)-- in imperative languages.---- \'@as '>>' bs@\' can be understood as the @do@ expression---- @-- do as-- bs-- @---- or in terms of @'(>>=)'@ as---- > as >>= const bs(>>) ::foralla b .m a ->m b ->m b m a
m >> m b
k =m a
m m a -> (a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
_->m b
k -- See Note [Recursive bindings for Applicative/Monad]{-# INLINE(>>)#-}-- | Inject a value into the monadic type.-- This function should /not/ be different from its default implementation-- as 'pure'. The justification for the existence of this function is-- merely historic.return ::a ->m a return =a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure {- Note [Recursive bindings for Applicative/Monad]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The original Applicative/Monad proposal stated that after
implementation, the designated implementation of (>>) would become
 (>>) :: forall a b. m a -> m b -> m b
 (>>) = (*>)
by default. You might be inclined to change this to reflect the stated
proposal, but you really shouldn't! Why? Because people tend to define
such instances the /other/ way around: in particular, it is perfectly
legitimate to define an instance of Applicative (*>) in terms of (>>),
which would lead to an infinite loop for the default implementation of
Monad! And people do this in the wild.
This turned into a nasty bug that was tricky to track down, and rather
than eliminate it everywhere upstream, it's easier to just retain the
original default.
-}-- | Same as '>>=', but with the arguments interchanged.---- > as >>= f == f =<< as{-# SPECIALISE(=<<)::(a ->[b ])->[a ]->[b ]#-}(=<<) ::Monad m =>(a ->m b )->m a ->m b a -> m b
f =<< :: forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m a
x =m a
x m a -> (a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
f -- | Conditional execution of 'Applicative' expressions. For example,---- ==== __Examples__---- > when debug (putStrLn "Debugging")---- will output the string @Debugging@ if the Boolean value @debug@-- is 'True', and otherwise do nothing.---- >>> putStr "pi:" >> when False (print 3.14159)-- pi:when ::(Applicative f )=>Bool ->f ()->f (){-# INLINABLEwhen #-}{-# SPECIALISEwhen ::Bool ->IO ()->IO ()#-}{-# SPECIALISEwhen ::Bool ->Maybe ()->Maybe ()#-}when :: forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
p f ()
s =ifBool
p thenf ()
s else() -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()-- | Evaluate each action in the sequence from left to right,-- and collect the results.sequence ::Monad m =>[m a ]->m [a ]{-# INLINEsequence #-}sequence :: forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence =(m a -> m a) -> [m a] -> m [a]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM m a -> m a
forall a. a -> a
id -- Note: [sequence and mapM]-- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@.mapM ::Monad m =>(a ->m b )->[a ]->m [b ]{-# INLINEmapM #-}mapM :: forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM a -> m b
f [a]
as =(a -> m [b] -> m [b]) -> m [b] -> [a] -> m [b]
forall a b. (a -> b -> b) -> b -> [a] -> b
foldr a -> m [b] -> m [b]
k ([b] -> m [b]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [])[a]
as wherek :: a -> m [b] -> m [b]
k a
a m [b]
r =do{x <-a -> m b
f a
a ;xs <-r ;return (x : xs )}{-
Note: [sequence and mapM]
~~~~~~~~~~~~~~~~~~~~~~~~~
Originally, we defined
mapM f = sequence . map f
This relied on list fusion to produce efficient code for mapM, and led to
excessive allocation in cryptarithm2. Defining
sequence = mapM id
relies only on inlining a tiny function (id) and beta reduction, which tends to
be a more reliable aspect of simplification. Indeed, this does not lead to
similar problems in nofib.
-}-- | Promote a function to a monad.-- This is equivalent to 'fmap' but specialised to Monads.liftM ::(Monad m )=>(a1 ->r )->m a1 ->m r liftM :: forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a1 -> r
f m a1
m1 =do{x1 <-m a1
m1 ;return (f x1 )}-- | Promote a function to a monad, scanning the monadic arguments from-- left to right.---- ==== __Examples__---- >>> liftM2 (+) [0,1] [0,2]-- [0,2,1,3]---- >>> liftM2 (+) (Just 1) Nothing-- Nothing---- >>> liftM2 (+) (+ 3) (* 2) 5-- 18liftM2 ::(Monad m )=>(a1 ->a2 ->r )->m a1 ->m a2 ->m r liftM2 :: forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a1 -> a2 -> r
f m a1
m1 m a2
m2 =do{x1 <-m a1
m1 ;x2 <-m2 ;return (f x1 x2 )}-- Caution: since this may be used for `liftA2`, we can't use the obvious-- definition of liftM2 = liftA2.-- | Promote a function to a monad, scanning the monadic arguments from-- left to right (cf. 'liftM2').liftM3 ::(Monad m )=>(a1 ->a2 ->a3 ->r )->m a1 ->m a2 ->m a3 ->m r liftM3 :: forall (m :: * -> *) a1 a2 a3 r.
Monad m =>
(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
liftM3 a1 -> a2 -> a3 -> r
f m a1
m1 m a2
m2 m a3
m3 =do{x1 <-m a1
m1 ;x2 <-m2 ;x3 <-m3 ;return (f x1 x2 x3 )}-- | Promote a function to a monad, scanning the monadic arguments from-- left to right (cf. 'liftM2').liftM4 ::(Monad m )=>(a1 ->a2 ->a3 ->a4 ->r )->m a1 ->m a2 ->m a3 ->m a4 ->m r liftM4 :: forall (m :: * -> *) a1 a2 a3 a4 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
liftM4 a1 -> a2 -> a3 -> a4 -> r
f m a1
m1 m a2
m2 m a3
m3 m a4
m4 =do{x1 <-m a1
m1 ;x2 <-m2 ;x3 <-m3 ;x4 <-m4 ;return (f x1 x2 x3 x4 )}-- | Promote a function to a monad, scanning the monadic arguments from-- left to right (cf. 'liftM2').liftM5 ::(Monad m )=>(a1 ->a2 ->a3 ->a4 ->a5 ->r )->m a1 ->m a2 ->m a3 ->m a4 ->m a5 ->m r liftM5 :: forall (m :: * -> *) a1 a2 a3 a4 a5 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> a5 -> r)
-> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
liftM5 a1 -> a2 -> a3 -> a4 -> a5 -> r
f m a1
m1 m a2
m2 m a3
m3 m a4
m4 m a5
m5 =do{x1 <-m a1
m1 ;x2 <-m2 ;x3 <-m3 ;x4 <-m4 ;x5 <-m5 ;return (f x1 x2 x3 x4 x5 )}{-# INLINABLEliftM #-}{-# SPECIALISEliftM ::(a1 ->r )->IO a1 ->IO r #-}{-# SPECIALISEliftM ::(a1 ->r )->Maybe a1 ->Maybe r #-}{-# INLINABLEliftM2 #-}{-# SPECIALISEliftM2 ::(a1 ->a2 ->r )->IO a1 ->IO a2 ->IO r #-}{-# SPECIALISEliftM2 ::(a1 ->a2 ->r )->Maybe a1 ->Maybe a2 ->Maybe r #-}{-# INLINABLEliftM3 #-}{-# SPECIALISEliftM3 ::(a1 ->a2 ->a3 ->r )->IO a1 ->IO a2 ->IO a3 ->IO r #-}{-# SPECIALISEliftM3 ::(a1 ->a2 ->a3 ->r )->Maybe a1 ->Maybe a2 ->Maybe a3 ->Maybe r #-}{-# INLINABLEliftM4 #-}{-# SPECIALISEliftM4 ::(a1 ->a2 ->a3 ->a4 ->r )->IO a1 ->IO a2 ->IO a3 ->IO a4 ->IO r #-}{-# SPECIALISEliftM4 ::(a1 ->a2 ->a3 ->a4 ->r )->Maybe a1 ->Maybe a2 ->Maybe a3 ->Maybe a4 ->Maybe r #-}{-# INLINABLEliftM5 #-}{-# SPECIALISEliftM5 ::(a1 ->a2 ->a3 ->a4 ->a5 ->r )->IO a1 ->IO a2 ->IO a3 ->IO a4 ->IO a5 ->IO r #-}{-# SPECIALISEliftM5 ::(a1 ->a2 ->a3 ->a4 ->a5 ->r )->Maybe a1 ->Maybe a2 ->Maybe a3 ->Maybe a4 ->Maybe a5 ->Maybe r #-}{- | In many situations, the 'liftM' operations can be replaced by uses of
'ap', which promotes function application.
> return f `ap` x1 `ap` ... `ap` xn
is equivalent to
> liftM<n> f x1 x2 ... xn
==== __Examples__
>>> pure (\x y z -> x + y * z) `ap` Just 1 `ap` Just 5 `ap` Just 10
Just 51
-}ap ::(Monad m )=>m (a ->b )->m a ->m b ap :: forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap m (a -> b)
m1 m a
m2 =do{x1 <-m (a -> b)
m1 ;x2 <-m2 ;return (x1 x2 )}-- Since many Applicative instances define (<*>) = ap, we-- cannot define ap = (<*>){-# INLINABLEap #-}{-# SPECIALISEap ::IO (a ->b )->IO a ->IO b #-}{-# SPECIALISEap ::Maybe (a ->b )->Maybe a ->Maybe b #-}-- instances for Prelude types-- | @since base-2.01instanceFunctor ((->)r )wherefmap :: forall a b. (a -> b) -> (r -> a) -> r -> b
fmap =(a -> b) -> (r -> a) -> r -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) -- | @since base-2.01instanceApplicative ((->)r )wherepure :: forall a. a -> r -> a
pure =a -> r -> a
forall a b. a -> b -> a
const <*> :: forall a b. (r -> (a -> b)) -> (r -> a) -> r -> b
(<*>) r -> (a -> b)
f r -> a
g r
x =r -> (a -> b)
f r
x (r -> a
g r
x )liftA2 :: forall a b c. (a -> b -> c) -> (r -> a) -> (r -> b) -> r -> c
liftA2 a -> b -> c
q r -> a
f r -> b
g r
x =a -> b -> c
q (r -> a
f r
x )(r -> b
g r
x )-- | @since base-2.01instanceMonad ((->)r )wherer -> a
f >>= :: forall a b. (r -> a) -> (a -> r -> b) -> r -> b
>>= a -> r -> b
k =\r
r ->a -> r -> b
k (r -> a
f r
r )r
r -- | @since base-4.15instanceFunctor Solo wherefmap :: forall a b. (a -> b) -> Solo a -> Solo b
fmap a -> b
f (MkSolo a
a )=b -> Solo b
forall a. a -> Solo a
MkSolo (a -> b
f a
a )-- Being strict in the `Solo` argument here seems most consistent-- with the concept behind `Solo`: always strict in the wrapper and lazy-- in the contents.a
x <$ :: forall a b. a -> Solo b -> Solo a
<$ MkSolo b
_=a -> Solo a
forall a. a -> Solo a
MkSolo a
x -- | @since base-2.01instanceFunctor ((,)a )wherefmap :: forall a b. (a -> b) -> (a, a) -> (a, b)
fmap a -> b
f (a
x ,a
y )=(a
x ,a -> b
f a
y )-- | @since base-2.01instanceFunctor Maybe wherefmap :: forall a b. (a -> b) -> Maybe a -> Maybe b
fmap a -> b
_Maybe a
Nothing =Maybe b
forall a. Maybe a
Nothing fmap a -> b
f (Just a
a )=b -> Maybe b
forall a. a -> Maybe a
Just (a -> b
f a
a )-- | @since base-2.01instanceApplicative Maybe wherepure :: forall a. a -> Maybe a
pure =a -> Maybe a
forall a. a -> Maybe a
Just Just a -> b
f <*> :: forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
<*> Maybe a
m =(a -> b) -> Maybe a -> Maybe b
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Maybe a
m Maybe (a -> b)
Nothing <*> Maybe a
_m =Maybe b
forall a. Maybe a
Nothing liftA2 :: forall a b c. (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
liftA2 a -> b -> c
f (Just a
x )(Just b
y )=c -> Maybe c
forall a. a -> Maybe a
Just (a -> b -> c
f a
x b
y )liftA2 a -> b -> c
_Maybe a
_Maybe b
_=Maybe c
forall a. Maybe a
Nothing Just a
_m1 *> :: forall a b. Maybe a -> Maybe b -> Maybe b
*> Maybe b
m2 =Maybe b
m2 Maybe a
Nothing *> Maybe b
_m2 =Maybe b
forall a. Maybe a
Nothing -- | @since base-2.01instanceMonad Maybe where(Just a
x )>>= :: forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
>>= a -> Maybe b
k =a -> Maybe b
k a
x Maybe a
Nothing >>= a -> Maybe b
_=Maybe b
forall a. Maybe a
Nothing >> :: forall a b. Maybe a -> Maybe b -> Maybe b
(>>) =Maybe a -> Maybe b -> Maybe b
forall a b. Maybe a -> Maybe b -> Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) -- ------------------------------------------------------------------------------- The Alternative class definitioninfixl3<|> -- | A monoid on applicative functors.---- If defined, 'some' and 'many' should be the least solutions-- of the equations:---- * @'some' v = (:) 'Prelude.<$>' v '<*>' 'many' v@---- * @'many' v = 'some' v '<|>' 'pure' []@---- ==== __Examples__---- >>> Nothing <|> Just 42-- Just 42---- >>> [1, 2] <|> [3, 4]-- [1,2,3,4]---- >>> empty <|> print (2^15)-- 32768classApplicative f =>Alternative f where-- | The identity of '<|>'---- > empty <|> a == a-- > a <|> empty == aempty ::f a -- | An associative binary operation(<|>) ::f a ->f a ->f a -- | One or more.---- ==== __Examples__---- >>> some (putStr "la")-- lalalalalalalalala... * goes on forever *---- >>> some Nothing-- nothing---- >>> take 5 <$> some (Just 1)-- * hangs forever *---- Note that this function can be used with Parsers based on-- Applicatives. In that case @some parser@ will attempt to-- parse @parser@ one or more times until it fails.some ::f a ->f [a ]some f a
v =f [a]
some_v wheremany_v :: f [a]
many_v =f [a]
some_v f [a] -> f [a] -> f [a]
forall a. f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> f [a]
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []some_v :: f [a]
some_v =(a -> [a] -> [a]) -> f a -> f [a] -> f [a]
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:)f a
v f [a]
many_v -- | Zero or more.---- ==== __Examples__---- >>> many (putStr "la")-- lalalalalalalalala... * goes on forever *---- >>> many Nothing-- Just []---- >>> take 5 <$> many (Just 1)-- * hangs forever *---- Note that this function can be used with Parsers based on-- Applicatives. In that case @many parser@ will attempt to-- parse @parser@ zero or more times until it fails.many ::f a ->f [a ]many f a
v =f [a]
many_v wheremany_v :: f [a]
many_v =f [a]
some_v f [a] -> f [a] -> f [a]
forall a. f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> f [a]
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []some_v :: f [a]
some_v =(a -> [a] -> [a]) -> f a -> f [a] -> f [a]
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:)f a
v f [a]
many_v -- | Picks the leftmost 'Just' value, or, alternatively, 'Nothing'.---- @since base-2.01instanceAlternative Maybe whereempty :: forall a. Maybe a
empty =Maybe a
forall a. Maybe a
Nothing Maybe a
Nothing <|> :: forall a. Maybe a -> Maybe a -> Maybe a
<|> Maybe a
r =Maybe a
r Maybe a
l <|> Maybe a
_=Maybe a
l -- ------------------------------------------------------------------------------- The MonadPlus class definition-- | Monads that also support choice and failure.class(Alternative m ,Monad m )=>MonadPlus m where-- | The identity of 'mplus'. It should also satisfy the equations---- > mzero >>= f = mzero-- > v >> mzero = mzero---- The default definition is---- @-- mzero = 'empty'-- @mzero ::m a mzero =m a
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty -- | An associative operation. The default definition is---- @-- mplus = ('<|>')-- @mplus ::m a ->m a ->m a mplus =m a -> m a -> m a
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) -- | Picks the leftmost 'Just' value, or, alternatively, 'Nothing'.---- @since base-2.01instanceMonadPlus Maybe ----------------------------------------------- The non-empty list typeinfixr5:| -- | Non-empty (and non-strict) list type.---- @since base-4.9.0.0dataNonEmpty a =a :| [a ]deriving(NonEmpty a -> NonEmpty a -> Bool
(NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> Bool) -> Eq (NonEmpty a)
forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
== :: NonEmpty a -> NonEmpty a -> Bool
$c/= :: forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
/= :: NonEmpty a -> NonEmpty a -> Bool
Eq -- ^ @since base-4.9.0.0,Eq (NonEmpty a)
Eq (NonEmpty a) =>
(NonEmpty a -> NonEmpty a -> Ordering)
-> (NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> NonEmpty a)
-> (NonEmpty a -> NonEmpty a -> NonEmpty a)
-> Ord (NonEmpty a)
NonEmpty a -> NonEmpty a -> Bool
NonEmpty a -> NonEmpty a -> Ordering
NonEmpty a -> NonEmpty a -> NonEmpty a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (NonEmpty a)
forall a. Ord a => NonEmpty a -> NonEmpty a -> Bool
forall a. Ord a => NonEmpty a -> NonEmpty a -> Ordering
forall a. Ord a => NonEmpty a -> NonEmpty a -> NonEmpty a
$ccompare :: forall a. Ord a => NonEmpty a -> NonEmpty a -> Ordering
compare :: NonEmpty a -> NonEmpty a -> Ordering
$c< :: forall a. Ord a => NonEmpty a -> NonEmpty a -> Bool
< :: NonEmpty a -> NonEmpty a -> Bool
$c<= :: forall a. Ord a => NonEmpty a -> NonEmpty a -> Bool
<= :: NonEmpty a -> NonEmpty a -> Bool
$c> :: forall a. Ord a => NonEmpty a -> NonEmpty a -> Bool
> :: NonEmpty a -> NonEmpty a -> Bool
$c>= :: forall a. Ord a => NonEmpty a -> NonEmpty a -> Bool
>= :: NonEmpty a -> NonEmpty a -> Bool
$cmax :: forall a. Ord a => NonEmpty a -> NonEmpty a -> NonEmpty a
max :: NonEmpty a -> NonEmpty a -> NonEmpty a
$cmin :: forall a. Ord a => NonEmpty a -> NonEmpty a -> NonEmpty a
min :: NonEmpty a -> NonEmpty a -> NonEmpty a
Ord -- ^ @since base-4.9.0.0)-- | @since base-4.9.0.0instanceFunctor NonEmpty wherefmap :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
fmap a -> b
f ~(a
a :| [a]
as )=a -> b
f a
a b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f [a]
as a
b <$ :: forall a b. a -> NonEmpty b -> NonEmpty a
<$ ~(b
_:| [b]
as )=a
b a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| (a
b a -> [b] -> [a]
forall a b. a -> [b] -> [a]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [b]
as )-- | @since base-4.9.0.0instanceApplicative NonEmpty wherepure :: forall a. a -> NonEmpty a
pure a
a =a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []<*> :: forall a b. NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
(<*>) =NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap liftA2 :: forall a b c.
(a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
liftA2 =(a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 -- | @since base-4.9.0.0instanceMonad NonEmpty where~(a
a :| [a]
as )>>= :: forall a b. NonEmpty a -> (a -> NonEmpty b) -> NonEmpty b
>>= a -> NonEmpty b
f =b
b b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| ([b]
bs [b] -> [b] -> [b]
forall a. [a] -> [a] -> [a]
++ [b]
bs' )whereb
b :| [b]
bs =a -> NonEmpty b
f a
a bs' :: [b]
bs' =[a]
as [a] -> (a -> [b]) -> [b]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NonEmpty b -> [b]
forall {a}. NonEmpty a -> [a]
toList (NonEmpty b -> [b]) -> (a -> NonEmpty b) -> a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> NonEmpty b
f toList :: NonEmpty a -> [a]
toList ~(a
c :| [a]
cs )=a
c a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
cs ------------------------------------------------ The list type-- | @since base-2.01instanceFunctor []where{-# INLINEfmap #-}fmap :: forall a b. (a -> b) -> [a] -> [b]
fmap =(a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map -- See Note: [List comprehensions and inlining]-- | @since base-2.01instanceApplicative []where{-# INLINEpure #-}pure :: forall a. a -> [a]
pure a
x =[a
x ]{-# INLINE(<*>)#-}[a -> b]
fs <*> :: forall a b. [a -> b] -> [a] -> [b]
<*> [a]
xs =[a -> b
f a
x |a -> b
f <-[a -> b]
fs ,a
x <-[a]
xs ]{-# INLINEliftA2 #-}liftA2 :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
liftA2 a -> b -> c
f [a]
xs [b]
ys =[a -> b -> c
f a
x b
y |a
x <-[a]
xs ,b
y <-[b]
ys ]{-# INLINE(*>)#-}[a]
xs *> :: forall a b. [a] -> [b] -> [b]
*> [b]
ys =[b
y |a
_<-[a]
xs ,b
y <-[b]
ys ]-- See Note: [List comprehensions and inlining]-- | @since base-2.01instanceMonad []where{-# INLINE(>>=)#-}[a]
xs >>= :: forall a b. [a] -> (a -> [b]) -> [b]
>>= a -> [b]
f =[b
y |a
x <-[a]
xs ,b
y <-a -> [b]
f a
x ]{-# INLINE(>>)#-}>> :: forall a b. [a] -> [b] -> [b]
(>>) =[a] -> [b] -> [b]
forall a b. [a] -> [b] -> [b]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) -- | Combines lists by concatenation, starting from the empty list.---- @since base-2.01instanceAlternative []whereempty :: forall a. [a]
empty =[]<|> :: forall a. [a] -> [a] -> [a]
(<|>) =[a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
(++) -- | Combines lists by concatenation, starting from the empty list.---- @since base-2.01instanceMonadPlus []{-
A few list functions that appear here because they are used here.
The rest of the prelude list functions are in GHC.List.
-}------------------------------------------------ foldr/build/augment------------------------------------------------ | 'foldr', applied to a binary operator, a starting value (typically-- the right-identity of the operator), and a list, reduces the list-- using the binary operator, from right to left:---- > foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)foldr ::(a ->b ->b )->b ->[a ]->b -- foldr _ z [] = z-- foldr f z (x:xs) = f x (foldr f z xs){-# INLINE[0]foldr #-}-- Inline only in the final stage, after the foldr/cons rule has had a chance-- Also note that we inline it when it has *two* parameters, which are the-- ones we are keen about specialising!foldr :: forall a b. (a -> b -> b) -> b -> [a] -> b
foldr a -> b -> b
k b
z =[a] -> b
go wherego :: [a] -> b
go []=b
z go (a
y : [a]
ys )=a
y a -> b -> b
`k` [a] -> b
go [a]
ys -- | A list producer that can be fused with 'foldr'.-- This function is merely---- > build g = g (:) []---- but GHC's simplifier will transform an expression of the form-- @'foldr' k z ('build' g)@, which may arise after inlining, to @g k z@,-- which avoids producing an intermediate list.build ::foralla .(forallb .(a ->b ->b )->b ->b )->[a ]{-# INLINE[1]build #-}-- The INLINE is important, even though build is tiny,-- because it prevents [] getting inlined in the version that-- appears in the interface file. If [] *is* inlined, it-- won't match with [] appearing in rules in an importing module.---- The "1" says to inline in phase 1build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build forall b. (a -> b -> b) -> b -> b
g =(a -> [a] -> [a]) -> [a] -> [a]
forall b. (a -> b -> b) -> b -> b
g (:)[]-- | A list producer that can be fused with 'foldr'.-- This function is merely---- > augment g xs = g (:) xs---- but GHC's simplifier will transform an expression of the form-- @'foldr' k z ('augment' g xs)@, which may arise after inlining, to-- @g k ('foldr' k z xs)@, which avoids producing an intermediate list.augment ::foralla .(forallb .(a ->b ->b )->b ->b )->[a ]->[a ]{-# INLINE[1]augment #-}augment :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a] -> [a]
augment forall b. (a -> b -> b) -> b -> b
g [a]
xs =(a -> [a] -> [a]) -> [a] -> [a]
forall b. (a -> b -> b) -> b -> b
g (:)[a]
xs {-# RULES"fold/build"forallk z (g ::forallb .(a ->b ->b )->b ->b ).foldr k z (build g )=g k z "foldr/augment"forallk z xs (g ::forallb .(a ->b ->b )->b ->b ).foldr k z (augment g xs )=g k (foldr k z xs )"foldr/id"foldr (:)[]=\x ->x "foldr/app"[1]forallys .foldr (:)ys =\xs ->xs ++ ys -- Only activate this from phase 1, because that's-- when we disable the rule that expands (++) into foldr-- The foldr/cons rule looks nice, but it can give disastrously-- bloated code when compiling-- array (a,b) [(1,2), (2,2), (3,2), ...very long list... ]-- i.e. when there are very very long literal lists-- So I've disabled it for now. We could have special cases-- for short lists, I suppose.-- "foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)"foldr/single"forallk z x .foldr k z [x ]=k x z "foldr/nil"forallk z .foldr k z []=z "foldr/cons/build"forallk z x (g ::forallb .(a ->b ->b )->b ->b ).foldr k z (x : build g )=k x (g k z )"augment/build"forall(g ::forallb .(a ->b ->b )->b ->b )(h ::forallb .(a ->b ->b )->b ->b ).augment g (build h )=build (\c n ->g c (h c n ))"augment/nil"forall(g ::forallb .(a ->b ->b )->b ->b ).augment g []=build g #-}-- This rule is true, but not (I think) useful:-- augment g (augment h t) = augment (\cn -> g c (h c n)) t------------------------------------------------ map------------------------------------------------ | \(\mathcal{O}(n)\). 'map' @f xs@ is the list obtained by applying @f@ to-- each element of @xs@, i.e.,---- > map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]-- > map f [x1, x2, ...] == [f x1, f x2, ...]---- this means that @map id == id@---- ==== __Examples__---- >>> map (+1) [1, 2, 3]-- [2,3,4]---- >>> map id [1, 2, 3]-- [1,2,3]---- >>> map (\n -> 3 * n + 1) [1, 2, 3]-- [4,7,10]map ::(a ->b )->[a ]->[b ]{-# NOINLINE[0]map #-}-- We want the RULEs "map" and "map/coerce" to fire first.-- map is recursive, so won't inline anyway,-- but saying so is more explicit, and silences warningsmap :: forall a b. (a -> b) -> [a] -> [b]
map a -> b
_[]=[]map a -> b
f (a
x : [a]
xs )=a -> b
f a
x b -> [b] -> [b]
forall a. a -> [a] -> [a]
: (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
xs -- Note eta expandedmapFB ::(elt ->lst ->lst )->(a ->elt )->a ->lst ->lst {-# INLINE[0]mapFB #-}-- See Note [Inline FB functions] in GHC.Internal.ListmapFB :: forall elt lst a.
(elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lst
mapFB elt -> lst -> lst
c a -> elt
f =\a
x lst
ys ->elt -> lst -> lst
c (a -> elt
f a
x )lst
ys {- Note [The rules for map]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The rules for map work like this.
* Up to (but not including) phase 1, we use the "map" rule to
 rewrite all saturated applications of map with its build/fold
 form, hoping for fusion to happen.
 In phase 1 and 0, we switch off that rule, inline build, and
 switch on the "mapList" rule, which rewrites the foldr/mapFB
 thing back into plain map.
 It's important that these two rules aren't both active at once
 (along with build's unfolding) else we'd get an infinite loop
 in the rules. Hence the activation control below.
* This same pattern is followed by many other functions:
 e.g. append, filter, iterate, repeat, etc. in GHC.Internal.List
 See also Note [Inline FB functions] in GHC.Internal.List
* The "mapFB" rule optimises compositions of map
* The "mapFB/id" rule gets rid of 'map id' calls.
 You might think that (mapFB c id) will turn into c simply
 when mapFB is inlined; but before that happens the "mapList"
 rule turns
 (foldr (mapFB (:) id) [] a
 back into
 map id
 Which is not very clever.
* Any similarity to the Functor laws for [] is expected.
-}{-# RULES"map"[~1]forallf xs .map f xs =build (\c n ->foldr (mapFB c f )n xs )"mapList"[1]forallf .foldr (mapFB (:)f )[]=map f "mapFB"forallc f g .mapFB (mapFB c f )g =mapFB c (f . g )"mapFB/id"forallc .mapFB c (\x ->x )=c #-}-- See Breitner, Eisenberg, Peyton Jones, and Weirich, "Safe Zero-cost-- Coercions for Haskell", section 6.5:-- http://research.microsoft.com/en-us/um/people/simonpj/papers/ext-f/coercible.pdf{-# RULES"map/coerce"[1]map coerce =coerce #-}-- See Note [Getting the map/coerce RULE to work] in GHC.Core.SimpleOpt------------------------------------------------ append------------------------------------------------ | '(++)' appends two lists, i.e.,---- > [x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn]-- > [x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]---- If the first list is not finite, the result is the first list.---- ==== __Performance considerations__---- This function takes linear time in the number of elements of the-- __first__ list. Thus it is better to associate repeated-- applications of '(++)' to the right (which is the default behaviour):-- @xs ++ (ys ++ zs)@ or simply @xs ++ ys ++ zs@, but not @(xs ++ ys) ++ zs@.-- For the same reason 'GHC.Internal.Data.List.concat' @=@ 'GHC.Internal.Data.List.foldr' '(++)' @[]@-- has linear performance, while 'GHC.Internal.Data.List.foldl' '(++)' @[]@ is prone-- to quadratic slowdown---- ==== __Examples__---- >>> [1, 2, 3] ++ [4, 5, 6]-- [1,2,3,4,5,6]---- >>> [] ++ [1, 2, 3]-- [1,2,3]---- >>> [3, 2, 1] ++ []-- [3,2,1](++) ::[a ]->[a ]->[a ]{-# NOINLINE[2](++)#-}-- Give time for the RULEs for (++) to fire in InitialPhase-- It's recursive, so won't inline anyway,-- but saying so is more explicit++ :: forall a. [a] -> [a] -> [a]
(++) [][a]
ys =[a]
ys (++) (a
x : [a]
xs )[a]
ys =a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
ys {-# RULES"++/literal"forallx .(++)(unpackCString# x )=unpackAppendCString# x "++/literal_utf8"forallx .(++)(unpackCStringUtf8# x )=unpackAppendCStringUtf8# x #-}{-# RULES"++"[~1]forallxs ys .xs ++ ys =augment (\c n ->foldr c n xs )ys #-}-- |'otherwise' is defined as the value 'True'. It helps to make-- guards more readable. eg.---- > f x | x < 0 = ...-- > | otherwise = ...otherwise ::Bool otherwise :: Bool
otherwise =Bool
True ------------------------------------------------ Type Char and String------------------------------------------------ | 'String' is an alias for a list of characters.---- String constants in Haskell are values of type 'String'.-- That means if you write a string literal like @"hello world"@,-- it will have the type @[Char]@, which is the same as @String@.---- __Note:__ You can ask the compiler to automatically infer different types-- with the @-XOverloadedStrings@ language extension, for example-- @"hello world" :: Text@. See t'Data.String.IsString' for more information.---- Because @String@ is just a list of characters, you can use normal list functions-- to do basic string manipulation. See "Data.List" for operations on lists.---- === __Performance considerations__---- @[Char]@ is a relatively memory-inefficient type.-- It is a linked list of boxed word-size characters, internally it looks something like:---- > ╭─────┬───┬──╮ ╭─────┬───┬──╮ ╭─────┬───┬──╮ ╭────╮-- > │ (:) │ │ ─┼─>│ (:) │ │ ─┼─>│ (:) │ │ ─┼─>│ [] │-- > ╰─────┴─┼─┴──╯ ╰─────┴─┼─┴──╯ ╰─────┴─┼─┴──╯ ╰────╯-- > v v v-- > 'a' 'b' 'c'---- The @String@ "abc" will use @5*3+1 = 16@ (in general @5n+1@)-- words of space in memory.---- Furthermore, operations like '(++)' (string concatenation) are @O(n)@-- (in the left argument).---- For historical reasons, the @base@ library uses @String@ in a lot of places-- for the conceptual simplicity, but library code dealing with user-data-- should use the [text](https://hackage.haskell.org/package/text)-- package for Unicode text, or the the-- [bytestring](https://hackage.haskell.org/package/bytestring) package-- for binary data.typeString =[Char ]unsafeChr ::Int ->Char unsafeChr :: Int -> Char
unsafeChr (I# Int#
i# )=Char# -> Char
C# (Int# -> Char#
chr# Int#
i# )-- | The 'Prelude.fromEnum' method restricted to the type 'Data.Char.Char'.ord ::Char ->Int ord :: Char -> Int
ord (C# Char#
c# )=Int# -> Int
I# (Char# -> Int#
ord# Char#
c# )-- | This 'String' equality predicate is used when desugaring-- pattern-matches against strings.eqString ::String ->String ->Bool eqString :: [Char] -> [Char] -> Bool
eqString [][]=Bool
True eqString (Char
c1 : [Char]
cs1 )(Char
c2 : [Char]
cs2 )=Char
c1 Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c2 Bool -> Bool -> Bool
&& [Char]
cs1 [Char] -> [Char] -> Bool
`eqString` [Char]
cs2 eqString [Char]
_[Char]
_=Bool
False {-# RULES"eqString"(==)=eqString #-}-- eqString also has a BuiltInRule in GHC.Core.Opt.ConstantFold:-- eqString (unpackCString# (Lit s1)) (unpackCString# (Lit s2)) = s1==s2------------------------------------------------ 'Int' related definitions----------------------------------------------maxInt ,minInt ::Int {- Seems clumsy. Should perhaps put minInt and MaxInt directly into MachDeps.h -}
#if WORD_SIZE_IN_BITS == 31
minInt=I#(-0x40000000#)maxInt=I#0x3FFFFFFF#
#elif WORD_SIZE_IN_BITS == 32
minInt=I#(-0x80000000#)maxInt=I#0x7FFFFFFF#
#else
minInt :: Int
minInt =Int# -> Int
I# (Int#
-0x8000000000000000#)maxInt :: Int
maxInt =Int# -> Int
I# Int#
0x7FFFFFFFFFFFFFFF#
#endif
------------------------------------------------ The function type------------------------------------------------ | Identity function.---- > id x = x---- This function might seem useless at first glance, but it can be very useful-- in a higher order context.---- ==== __Examples__---- >>> length $ filter id [True, True, False, True]-- 3---- >>> Just (Just 3) >>= id-- Just 3---- >>> foldr id 0 [(^3), (*5), (+2)]-- 1000id ::a ->a id :: forall a. a -> a
id a
x =a
x -- Assertion function. This simply ignores its boolean argument.-- The compiler may rewrite it to @('assertError' line)@.-- | If the first argument evaluates to 'True', then the result is the-- second argument. Otherwise an 'Control.Exception.AssertionFailed' exception-- is raised, containing a 'String' with the source file and line number of the-- call to 'assert'.---- Assertions can normally be turned on or off with a compiler flag-- (for GHC, assertions are normally on unless optimisation is turned on-- with @-O@ or the @-fignore-asserts@-- option is given). When assertions are turned off, the first-- argument to 'assert' is ignored, and the second argument is-- returned as the result.-- SLPJ: in 5.04 etc 'assert' is in GHC.Prim,-- but from Template Haskell onwards it's simply-- defined here in Base.hsassert ::Bool ->a ->a assert :: forall a. Bool -> a -> a
assert Bool
_pred a
r =a
r breakpoint ::a ->a breakpoint :: forall a. a -> a
breakpoint a
r =a
r breakpointCond ::Bool ->a ->a breakpointCond :: forall a. Bool -> a -> a
breakpointCond Bool
_a
r =a
r dataOpaque =foralla .O a -- | @const x y@ always evaluates to @x@, ignoring its second argument.---- > const x = \_ -> x---- This function might seem useless at first glance, but it can be very useful-- in a higher order context.---- ==== __Examples__---- >>> const 42 "hello"-- 42---- >>> map (const 42) [0..3]-- [42,42,42,42]const ::a ->b ->a const :: forall a b. a -> b -> a
const a
x b
_=a
x -- | Right to left function composition.---- prop> (f . g) x = f (g x)---- prop> f . id = f = id . f---- ==== __Examples__---- >>> map ((*2) . length) [[], [0, 1, 2], [0]]-- [0,6,2]---- >>> foldr (.) id [(+1), (*3), (^3)] 2-- 25---- >>> let (...) = (.).(.) in ((*2)...(+)) 5 10-- 30{-# INLINE(.)#-}-- Make sure it has TWO args only on the left, so that it inlines-- when applied to two functions, even if there is no final argument(.) ::(b ->c )->(a ->b )->a ->c . :: forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) b -> c
f a -> b
g =\a
x ->b -> c
f (a -> b
g a
x )-- | @'flip' f@ takes its (first) two arguments in the reverse order of @f@.---- prop> flip f x y = f y x---- prop> flip . flip = id---- ==== __Examples__---- >>> flip (++) "hello" "world"-- "worldhello"---- >>> let (.>) = flip (.) in (+1) .> show $ 5-- "6"flip ::forallrepc a b (c ::TYPE repc ).(a ->b ->c )->b ->a ->c flip :: forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> c
f b
x a
y =a -> b -> c
f a
y b
x -- Note: Before base-4.19, ($) was not representation polymorphic-- in both type parameters but only in the return type.-- The generalization forced a change to the implementation,-- changing its laziness, affecting expressions like (($) undefined): before-- base-4.19 the expression (($) undefined) `seq` () was equivalent to-- (\x -> undefined x) `seq` () and thus would just evaluate to (), but now-- it is equivalent to undefined `seq` () which diverges.{- | @'($)'@ is the __function application__ operator.
Applying @'($)'@ to a function @f@ and an argument @x@ gives the same result as applying @f@ to @x@ directly. The definition is akin to this:
@
($) :: (a -> b) -> a -> b
($) f x = f x
@
This is @'id'@ specialized from @a -> a@ to @(a -> b) -> (a -> b)@ which by the associativity of @(->)@
is the same as @(a -> b) -> a -> b@.
On the face of it, this may appear pointless! But it's actually one of the most useful and important operators in Haskell.
The order of operations is very different between @($)@ and normal function application. Normal function application has precedence 10 - higher than any operator - and associates to the left. So these two definitions are equivalent:
@
expr = min 5 1 + 5
expr = ((min 5) 1) + 5
@
@($)@ has precedence 0 (the lowest) and associates to the right, so these are equivalent:
@
expr = min 5 $ 1 + 5
expr = (min 5) (1 + 5)
@
==== __Examples__
A common use cases of @($)@ is to avoid parentheses in complex expressions.
For example, instead of using nested parentheses in the following
 Haskell function:
@
-- | Sum numbers in a string: strSum "100 5 -7" == 98
strSum :: 'String' -> 'Int'
strSum s = 'sum' ('GHC.Internal.Data.Maybe.mapMaybe' 'GHC.Internal.Text.Read.readMaybe' ('words' s))
@
we can deploy the function application operator:
@
-- | Sum numbers in a string: strSum "100 5 -7" == 98
strSum :: 'String' -> 'Int'
strSum s = 'sum' '$' 'GHC.Internal.Data.Maybe.mapMaybe' 'GHC.Internal.Text.Read.readMaybe' '$' 'words' s
@
@($)@ is also used as a section (a partially applied operator), in order to indicate that we wish to apply some yet-unspecified function to a given value. For example, to apply the argument @5@ to a list of functions:
@
applyFive :: [Int]
applyFive = map ($ 5) [(+1), (2^)]
>>> [6, 32]
@
==== __Technical Remark (Representation Polymorphism)__
@($)@ is fully representation-polymorphic. This allows it to also be used with arguments of unlifted and even unboxed kinds, such as unboxed integers:
@
fastMod :: Int -> Int -> Int
fastMod (I# x) (I# m) = I# $ remInt# x m
@
-}{-# INLINE($)#-}($) ::forallrepa repb (a ::TYPE repa )(b ::TYPE repb ).(a ->b )->a ->b $ :: forall a b. (a -> b) -> a -> b
($) a -> b
f =a -> b
f -- | Strict (call-by-value) application operator. It takes a function and an-- argument, evaluates the argument to weak head normal form (WHNF), then calls-- the function with that value.($!) ::forallr a (b ::TYPE r ).(a ->b )->a ->b {-# INLINE($!)#-}a -> b
f $! :: forall a b. (a -> b) -> a -> b
$! a
x =let!vx :: a
vx =a
x ina -> b
f a
vx -- see #2273-- | @'until' p f@ yields the result of applying @f@ until @p@ holds.until ::(a ->Bool )->(a ->a )->a ->a until :: forall a. (a -> Bool) -> (a -> a) -> a -> a
until a -> Bool
p a -> a
f =a -> a
go wherego :: a -> a
go a
x |a -> Bool
p a
x =a
x |Bool
otherwise =a -> a
go (a -> a
f a
x )-- | 'asTypeOf' is a type-restricted version of 'const'. It is usually-- used as an infix operator, and its typing forces its first argument-- (which is usually overloaded) to have the same type as the second.asTypeOf ::a ->a ->a asTypeOf :: forall a. a -> a -> a
asTypeOf =a -> a -> a
forall a b. a -> b -> a
const ------------------------------------------------ Functor/Applicative/Monad instances for IO------------------------------------------------ | @since base-2.01instanceFunctor IO wherefmap :: forall a b. (a -> b) -> IO a -> IO b
fmap a -> b
f IO a
x =IO a
x IO a -> (a -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (b -> IO b
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> IO b) -> (a -> b) -> a -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f )-- | @since base-2.01instanceApplicative IO where{-# INLINEpure #-}{-# INLINE(*>)#-}{-# INLINEliftA2 #-}pure :: forall a. a -> IO a
pure =a -> IO a
forall a. a -> IO a
returnIO *> :: forall a b. IO a -> IO b -> IO b
(*>) =IO a -> IO b -> IO b
forall a b. IO a -> IO b -> IO b
thenIO <*> :: forall a b. IO (a -> b) -> IO a -> IO b
(<*>) =IO (a -> b) -> IO a -> IO b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap liftA2 :: forall a b c. (a -> b -> c) -> IO a -> IO b -> IO c
liftA2 =(a -> b -> c) -> IO a -> IO b -> IO c
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 -- | @since base-2.01instanceMonad IO where{-# INLINE(>>)#-}{-# INLINE(>>=)#-}>> :: forall a b. IO a -> IO b -> IO b
(>>) =IO a -> IO b -> IO b
forall a b. IO a -> IO b -> IO b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) >>= :: forall a b. IO a -> (a -> IO b) -> IO b
(>>=) =IO a -> (a -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
bindIO -- | Takes the first non-throwing 'IO' action\'s result.-- 'empty' throws an exception.---- @since base-4.9.0.0instanceAlternative IO whereempty :: forall a. IO a
empty =[Char] -> IO a
forall a. [Char] -> IO a
failIO [Char]
"mzero"<|> :: forall a. IO a -> IO a -> IO a
(<|>) =IO a -> IO a -> IO a
forall a. IO a -> IO a -> IO a
mplusIO -- | Takes the first non-throwing 'IO' action\'s result.-- 'mzero' throws an exception.---- @since base-4.9.0.0instanceMonadPlus IO returnIO ::a ->IO a returnIO :: forall a. a -> IO a
returnIO a
x =(State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO (\State# RealWorld
s ->(#State# RealWorld
s ,a
x #))bindIO ::IO a ->(a ->IO b )->IO b bindIO :: forall a b. IO a -> (a -> IO b) -> IO b
bindIO (IO State# RealWorld -> (# State# RealWorld, a #)
m )a -> IO b
k =(State# RealWorld -> (# State# RealWorld, b #)) -> IO b
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO (\State# RealWorld
s ->caseState# RealWorld -> (# State# RealWorld, a #)
m State# RealWorld
s of(#State# RealWorld
new_s ,a
a #)->IO b -> State# RealWorld -> (# State# RealWorld, b #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (a -> IO b
k a
a )State# RealWorld
new_s )thenIO ::IO a ->IO b ->IO b thenIO :: forall a b. IO a -> IO b -> IO b
thenIO (IO State# RealWorld -> (# State# RealWorld, a #)
m )IO b
k =(State# RealWorld -> (# State# RealWorld, b #)) -> IO b
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO (\State# RealWorld
s ->caseState# RealWorld -> (# State# RealWorld, a #)
m State# RealWorld
s of(#State# RealWorld
new_s ,a
_#)->IO b -> State# RealWorld -> (# State# RealWorld, b #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO IO b
k State# RealWorld
new_s )-- Note that it is import that we do not SOURCE import this as-- its demand signature encodes knowledge of its bottoming-- behavior, which can expose useful simplifications. See-- #16588.failIO ::String ->IO a failIO :: forall a. [Char] -> IO a
failIO [Char]
s =(State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO (SomeException -> State# RealWorld -> (# State# RealWorld, a #)
forall a b. a -> State# RealWorld -> (# State# RealWorld, b #)
raiseIO# ([Char] -> SomeException
mkUserError [Char]
s ))unIO ::IO a ->(State# RealWorld ->(#State# RealWorld ,a #))unIO :: forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (IO State# RealWorld -> (# State# RealWorld, a #)
a )=State# RealWorld -> (# State# RealWorld, a #)
a {- |
Returns the tag of a constructor application; this function was once used
by the deriving code for Eq, Ord and Enum.
-}{-# INLINEgetTag #-}getTag ::forall{lev ::Levity }(a ::TYPE (BoxedRep lev )).DataToTag a =>a ->Int# getTag :: forall a. DataToTag a => a -> Int#
getTag =a -> Int#
forall a. DataToTag a => a -> Int#
dataToTag# ------------------------------------------------ Numeric primops------------------------------------------------ Definitions of the boxed PrimOps; these will be-- used in the case of partial applications, etc.-- See Note [INLINE division wrappers]{-# INLINEquotInt #-}{-# INLINEremInt #-}{-# INLINEdivInt #-}{-# INLINEmodInt #-}{-# INLINEquotRemInt #-}{-# INLINEdivModInt #-}-- | Used to implement `quot` for the `Integral` typeclass.-- This performs integer division on its two parameters, truncated towards zero.---- ==== __Example__-- >>> quotInt 10 2-- 5---- >>> quot 10 2-- 5quotInt ::Int ->Int ->Int (I# Int#
x )quotInt :: Int -> Int -> Int
`quotInt` (I# Int#
y )=Int# -> Int
I# (Int#
x Int# -> Int# -> Int#
`quotInt#` Int#
y )-- | Used to implement `rem` for the `Integral` typeclass.-- This gives the remainder after integer division of its two parameters, satisfying---- > ((x `quot` y) * y) + (x `rem` y) == x---- ==== __Example__-- >>> remInt 3 2-- 1---- >>> rem 3 2-- 1remInt ::Int ->Int ->Int (I# Int#
x )remInt :: Int -> Int -> Int
`remInt` (I# Int#
y )=Int# -> Int
I# (Int#
x Int# -> Int# -> Int#
`remInt#` Int#
y )-- | Used to implement `div` for the `Integral` typeclass.-- This performs integer division on its two parameters, truncated towards negative infinity.---- ==== __Example__-- >>> 10 `divInt` 2-- 5---- >>> 10 `div` 2-- 5divInt ::Int ->Int ->Int (I# Int#
x )divInt :: Int -> Int -> Int
`divInt` (I# Int#
y )=Int# -> Int
I# (Int#
x Int# -> Int# -> Int#
`divInt#` Int#
y )-- | Used to implement `mod` for the `Integral` typeclass.-- This performs the modulo operation, satisfying---- > ((x `div` y) * y) + (x `mod` y) == x---- ==== __Example__-- >>> 7 `modInt` 3-- 1---- >>> 7 `mod` 3-- 1modInt ::Int ->Int ->Int (I# Int#
x )modInt :: Int -> Int -> Int
`modInt` (I# Int#
y )=Int# -> Int
I# (Int#
x Int# -> Int# -> Int#
`modInt#` Int#
y )-- | Used to implement `quotRem` for the `Integral` typeclass.-- This gives a tuple equivalent to---- > (quot x y, mod x y)---- ==== __Example__-- >>> quotRemInt 10 2-- (5,0)---- >>> quotRem 10 2-- (5,0)quotRemInt ::Int ->Int ->(Int ,Int )(I# Int#
x )quotRemInt :: Int -> Int -> (Int, Int)
`quotRemInt` (I# Int#
y )=caseInt#
x Int# -> Int# -> (# Int#, Int# #)
`quotRemInt#` Int#
y of(#Int#
q ,Int#
r #)->(Int# -> Int
I# Int#
q ,Int# -> Int
I# Int#
r )-- | Used to implement `divMod` for the `Integral` typeclass.-- This gives a tuple equivalent to---- > (div x y, mod x y)---- ==== __Example__-- >>> divModInt 10 2-- (5,0)---- >>> divMod 10 2-- (5,0)divModInt ::Int ->Int ->(Int ,Int )(I# Int#
x )divModInt :: Int -> Int -> (Int, Int)
`divModInt` (I# Int#
y )=caseInt#
x Int# -> Int# -> (# Int#, Int# #)
`divModInt#` Int#
y of(#Int#
q ,Int#
r #)->(Int# -> Int
I# Int#
q ,Int# -> Int
I# Int#
r ){- Note [INLINE division wrappers]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Int division functions such as 'quotRemInt' and 'divModInt' have
been manually worker/wrappered, presumably because they construct
*nested* products.
We intend to preserve the exact worker/wrapper split, hence we mark
the wrappers INLINE (#19267). That makes sure the optimiser doesn't
accidentally inline the worker into the wrapper, undoing the manual
split again.
-}-- Wrappers for the shift operations. The uncheckedShift# family are-- undefined when the amount being shifted by is greater than the size-- in bits of Int#, so these wrappers perform a check and return-- either zero or -1 appropriately.---- Note that these wrappers still produce undefined results when the-- second argument (the shift amount) is negative.-- | This function is used to implement branchless shifts. If the number of bits-- to shift is greater than or equal to the type size in bits, then the shift-- must return 0. Instead of doing a test, we use a mask obtained via this-- function which is branchless too.---- shift_mask m b-- | b < m = 0xFF..FF-- | otherwise = 0--shift_mask ::Int# ->Int# ->Int# {-# INLINEshift_mask #-}shift_mask :: Int# -> Int# -> Int#
shift_mask Int#
m Int#
b =Int# -> Int#
negateInt# (Int#
b Int# -> Int# -> Int#
<# Int#
m )-- | Shift the argument left by the specified number of bits-- (which must be non-negative).shiftL# ::Word# ->Int# ->Word# Word#
a shiftL# :: Word# -> Int# -> Word#
`shiftL#` Int#
b =(Word#
a Word# -> Int# -> Word#
`uncheckedShiftL#` Int#
b )Word# -> Word# -> Word#
`and#` Int# -> Word#
int2Word# (Int# -> Int# -> Int#
shift_mask WORD_SIZE_IN_BITS#b)-- | Shift the argument right by the specified number of bits-- (which must be non-negative).-- The "RL" means "right, logical" (as opposed to RA for arithmetic)-- (although an arithmetic right shift wouldn't make sense for Word#)shiftRL# ::Word# ->Int# ->Word# Word#
a shiftRL# :: Word# -> Int# -> Word#
`shiftRL#` Int#
b =(Word#
a Word# -> Int# -> Word#
`uncheckedShiftRL#` Int#
b )Word# -> Word# -> Word#
`and#` Int# -> Word#
int2Word# (Int# -> Int# -> Int#
shift_mask WORD_SIZE_IN_BITS#b)-- | Shift the argument left by the specified number of bits-- (which must be non-negative).iShiftL# ::Int# ->Int# ->Int# Int#
a iShiftL# :: Int# -> Int# -> Int#
`iShiftL#` Int#
b =(Int#
a Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
b )Int# -> Int# -> Int#
`andI#` Int# -> Int# -> Int#
shift_mask WORD_SIZE_IN_BITS#b-- | Shift the argument right (signed) by the specified number of bits-- (which must be non-negative).-- The "RA" means "right, arithmetic" (as opposed to RL for logical)iShiftRA# ::Int# ->Int# ->Int# Int#
a iShiftRA# :: Int# -> Int# -> Int#
`iShiftRA#` Int#
b |Int# -> Bool
isTrue# (Int#
b Int# -> Int# -> Int#
>=# WORD_SIZE_IN_BITS#)=negateInt#(a<#0#)|Bool
otherwise =Int#
a Int# -> Int# -> Int#
`uncheckedIShiftRA#` Int#
b -- | Shift the argument right (unsigned) by the specified number of bits-- (which must be non-negative).-- The "RL" means "right, logical" (as opposed to RA for arithmetic)iShiftRL# ::Int# ->Int# ->Int# Int#
a iShiftRL# :: Int# -> Int# -> Int#
`iShiftRL#` Int#
b =(Int#
a Int# -> Int# -> Int#
`uncheckedIShiftRL#` Int#
b )Int# -> Int# -> Int#
`andI#` Int# -> Int# -> Int#
shift_mask WORD_SIZE_IN_BITS#b-- Rules for C strings (the functions themselves are now in GHC.CString){-# RULES"unpack"[~1]foralla .unpackCString# a =build (unpackFoldrCString# a )"unpack-list"[1]foralla .unpackFoldrCString# a (:)[]=unpackCString# a "unpack-append"foralla n .unpackFoldrCString# a (:)n =unpackAppendCString# a n "unpack-append-nil"foralla .unpackAppendCString# a []=unpackCString# a "unpack-utf8"[~1]foralla .unpackCStringUtf8# a =build (unpackFoldrCStringUtf8# a )"unpack-list-utf8"[1]foralla .unpackFoldrCStringUtf8# a (:)[]=unpackCStringUtf8# a "unpack-append-utf8"foralla n .unpackFoldrCStringUtf8# a (:)n =unpackAppendCStringUtf8# a n "unpack-append-nil-utf8"foralla .unpackAppendCStringUtf8# a []=unpackCStringUtf8# a -- There's a built-in rule (in GHC.Core.Op.ConstantFold) for-- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n-- See also the Note [String literals in GHC] in CString.hs#-}

AltStyle によって変換されたページ (->オリジナル) /