Movatterモバイル変換


[0]ホーム

URL:


{-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 modulesSo the rough structure is as follows, in (linearised) dependency orderGHC.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.hiGHC.Base        Classes: Eq, Ord, Functor, Monad                Types:   List, (), Int, Bool, Ordering, Char, StringData.Tuple      Types: tuples, plus instances for GHC.Base classesGHC.Show        Class: Show, plus instances for GHC.Base/GHC.Tup typesGHC.Enum        Class: Enum,  plus instances for GHC.Base/GHC.Tup typesData.Maybe      Type: Maybe, plus instances for GHC.Base classesGHC.List        List functionsGHC.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 NumGHC.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 RealGHC.ST  The ST monad, instances and a few helper functionsIx              Classes: Ix, plus instances for Int, Bool, Char, Integer, Ordering, tuplesGHC.Arr         Types: Array, MutableArray, MutableVar                Arrays are used by a function in GHC.FloatGHC.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.Float.hiOther 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.Base-- Copyright   :  (c) The University of Glasgow, 1992-2002-- License     :  see libraries/base/LICENSE---- Maintainer  :  cvs-ghc@haskell.org-- Stability   :  internal-- Portability :  non-portable (GHC extensions)---- Basic data types and classes.-------------------------------------------------------------------------------#include "MachDeps.h"moduleGHC.Base(moduleGHC.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.ErrmoduleGHC.Prim.PtrEq,-- to avoid lots of people having tomoduleGHC.Err,-- import these modules explicitlymoduleGHC.Maybe)whereimportGHC.TypesimportGHC.ClassesimportGHC.CStringimportGHC.MagicimportGHC.Magic.DictimportGHC.PrimimportGHC.Prim.ExtimportGHC.Prim.PtrEqimportGHC.ErrimportGHC.Maybeimport{-# SOURCE#-}GHC.IO(mkUserError,mplusIO)importGHC.Tuple(Solo(MkSolo))-- Note [Depend on GHC.Tuple]importGHC.Num.Integer()-- Note [Depend on GHC.Num.Integer]-- for 'class Semigroup'import{-# SOURCE#-}GHC.Real(Integral)import{-# SOURCE#-}Data.Semigroup.Internal(stimesDefault,stimesMaybe,stimesList,stimesIdempotentMonoid)-- $setup-- >>> import GHC.Numinfixr9.infixr5++infixl4<$infixl1>>,>>=infixr1=<<infixr0$,$!infixl4<*>,<*,*>,<**>default()-- Double isn't available yet{-Note [Depend on GHC.Num.Integer]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The Integer type is special because GHC.CoreToStg.Prep.mkConvertNumLiterallookups names in ghc-bignum interfaces to construct Integer literal values.Currently it reads the interface file whether or not the current module *has*any Integer literals, so it's important that GHC.Num.Integer is compiled beforeany other module.The danger is that if the build system doesn't know about the implicitdependency on Integer, it'll compile some base module before GHC.Num.Integer,resulting in:  Failed to load interface for ‘GHC.Num.Integer’    There are files missing in the ‘ghc-bignum’ package,To ensure that GHC.Num.Integer is there, we must ensure that there is a visibledependency on GHC.Num.Integer from every module in base.  We make GHC.Basedepend on GHC.Num.Integer; and everything else either depends on GHC.Base,directly on GHC.Num.Integer, or does not have NoImplicitPrelude (and hencedepends on Prelude).The lookup is only disabled for packages ghc-prim and ghc-bignum, which aren'tallowed to contain any Integer literal.Note [Depend on GHC.Tuple]~~~~~~~~~~~~~~~~~~~~~~~~~~Similarly, tuple syntax (or ()) creates an implicit dependency onGHC.Tuple, so we use the same rule as for Integer --- see Note [Depend onGHC.Num.Integer] --- to explain this to the build system.  We make GHC.Basedepend on GHC.Tuple, and everything else depends on GHC.Base or Prelude.-}#if 0-- for use when compiling GHC.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 4.8.0.0dataVoidderiving(Void -> Void -> Bool(Void -> Void -> Bool) -> (Void -> Void -> Bool) -> Eq Voidforall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a$c== :: Void -> Void -> Bool== :: Void -> Void -> Bool$c/= :: Void -> Void -> Bool/= :: Void -> Void -> BoolEq-- ^ @since 4.8.0.0,Eq VoidEq Void =>(Void -> Void -> Ordering)-> (Void -> Void -> Bool)-> (Void -> Void -> Bool)-> (Void -> Void -> Bool)-> (Void -> Void -> Bool)-> (Void -> Void -> Void)-> (Void -> Void -> Void)-> Ord VoidVoid -> Void -> BoolVoid -> Void -> OrderingVoid -> Void -> Voidforall 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 -> Orderingcompare :: 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 -> Voidmax :: Void -> Void -> Void$cmin :: Void -> Void -> Voidmin :: Void -> Void -> VoidOrd-- ^ @since 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 4.8.0.0absurd::Void->aabsurd :: forall a. Void -> aabsurdVoida=caseVoidaof{}-- | 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 4.8.0.0vacuous::Functorf=>fVoid->favacuous :: forall (f :: * -> *) a. Functor f => f Void -> f avacuous=(Void -> a) -> f Void -> f aforall a b. (a -> b) -> f a -> f bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f bfmapVoid -> aforall a. Void -> aabsurdinfixr6<>-- | 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 4.9.0.0classSemigroupawhere-- | 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->aaa<>ab=NonEmpty a -> aforall a. Semigroup a => NonEmpty a -> asconcat(aaa -> [a] -> NonEmpty aforall a. a -> [a] -> NonEmpty a:|[ab])-- | 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::NonEmptya->asconcat(aa:|[a]as)=a -> [a] -> aforall {t}. Semigroup t => t -> [t] -> tgoaa[a]aswherego :: t -> [t] -> tgotb(tc:[t]cs)=tbt -> t -> tforall a. Semigroup a => a -> a -> a<>t -> [t] -> tgotc[t]csgotb[]=tb-- | 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::Integralb=>b->a->astimes=b -> a -> aforall b a. (Integral b, Semigroup a) => b -> a -> astimesDefault{-# MINIMAL(<>)|sconcat#-}-- | 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/.classSemigroupa=>Monoidawhere-- | Identity of 'mappend'---- ==== __Examples__-- >>> "Hello world" <> mempty-- "Hello world"---- >>> mempty <> [1, 2, 3]-- [1,2,3]mempty::amempty=[a] -> aforall a. Monoid a => [a] -> amconcat[]{-# 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->amappend=a -> a -> aforall 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]->amconcat=(a -> a -> a) -> a -> [a] -> aforall a b. (a -> b -> b) -> b -> [a] -> bfoldra -> a -> aforall a. Monoid a => a -> a -> amappendaforall a. Monoid a => amempty{-# INLINEmconcat#-}-- INLINE in the hope of fusion with mconcat's argument (see !4890){-# MINIMALmempty|mconcat#-}-- | @since 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 -> [a] -> [a]forall b a. Integral b => b -> [a] -> [a]stimesList-- | @since 2.01instanceMonoid[a]where{-# INLINEmempty#-}mempty :: [a]mempty=[]{-# INLINEmconcat#-}mconcat :: [[a]] -> [a]mconcat[[a]]xss=[ax|[a]xs<-[[a]]xss,ax<-[a]xs]-- See Note: [List comprehensions and inlining]-- | @since 4.9.0.0instanceSemigroupVoidwhereVoida<> :: Void -> Void -> Void<>Void_=Voidastimes :: forall b. Integral b => b -> Void -> Voidstimesb_Voida=Voida{-Note: [List comprehensions and inlining]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The list monad operations are traditionally described in terms of concatMap:xs >>= f = concatMap f xsSimilarly, mconcat for lists is just concat. Here in Base, however, we don'thave concatMap, and we'll refrain from adding it here so it won't have to behidden in imports. Instead, we use GHC's list comprehension desugaringmechanism to define mconcat and the Applicative and Monad instances for lists.We mark them INLINE because the inliner is not generally too keen to inlinebuild forms such as the ones these desugar to without our insistence.  Definingthese using list comprehensions instead of foldr has an additional potentialbenefit, as described in compiler/GHC/HsToCore/ListComp.hs: if optimizationsneeded to make foldr/build forms efficient are turned off, we'll get reasonablyefficient translations anyway.-}-- | @since 4.9.0.0instanceSemigroup(NonEmptya)where(aa:|[a]as)<> :: NonEmpty a -> NonEmpty a -> NonEmpty a<>~(ab:|[a]bs)=aaa -> [a] -> NonEmpty aforall a. a -> [a] -> NonEmpty a:|([a]as[a] -> [a] -> [a]forall a. [a] -> [a] -> [a]++aba -> [a] -> [a]forall a. a -> [a] -> [a]:[a]bs)-- | @since 4.9.0.0instanceSemigroupb=>Semigroup(a->b)wherea -> bf<> :: (a -> b) -> (a -> b) -> a -> b<>a -> bg=\ax->a -> bfaxb -> b -> bforall a. Semigroup a => a -> a -> a<>a -> bgaxstimes :: forall b. Integral b => b -> (a -> b) -> a -> bstimesbna -> bfae=b -> b -> bforall b. Integral b => b -> b -> bforall a b. (Semigroup a, Integral b) => b -> a -> astimesbn(a -> bfae)-- | @since 2.01instanceMonoidb=>Monoid(a->b)wheremempty :: a -> bmemptya_=bforall a. Monoid a => amempty-- 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 -> bmconcat=\[a -> b]fsax->[b] -> bforall a. Monoid a => [a] -> amconcat([b] -> b) -> [b] -> bforall a b. (a -> b) -> a -> b$((a -> b) -> b) -> [a -> b] -> [b]forall a b. (a -> b) -> [a] -> [b]map(\a -> bf->a -> bfax)[a -> b]fs{-# INLINEmconcat#-}-- | @since 4.9.0.0instanceSemigroup()where()_<> :: () -> () -> ()<>()_=()sconcat :: NonEmpty () -> ()sconcatNonEmpty ()_=()stimes :: forall b. Integral b => b -> () -> ()stimesb_()_=()-- | @since 2.01instanceMonoid()where-- Should it be strict?mempty :: ()mempty=()mconcat :: [()] -> ()mconcat[()]_=()-- | @since 4.15instanceSemigroupa=>Semigroup(Soloa)whereMkSoloaa<> :: Solo a -> Solo a -> Solo a<>MkSoloab=a -> Solo aforall a. a -> Solo aMkSolo(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>ab)stimes :: forall b. Integral b => b -> Solo a -> Solo astimesbn(MkSoloaa)=a -> Solo aforall a. a -> Solo aMkSolo(b -> a -> aforall b. Integral b => b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa)-- | @since 4.15instanceMonoida=>Monoid(Soloa)wheremempty :: Solo amempty=a -> Solo aforall a. a -> Solo aMkSoloaforall a. Monoid a => amempty-- | @since 4.9.0.0instance(Semigroupa,Semigroupb)=>Semigroup(a,b)where(aa,bb)<> :: (a, b) -> (a, b) -> (a, b)<>(aa',bb')=(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>aa',bbb -> b -> bforall a. Semigroup a => a -> a -> a<>bb')stimes :: forall b. Integral b => b -> (a, b) -> (a, b)stimesbn(aa,bb)=(b -> a -> aforall b. Integral b => b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa,b -> b -> bforall b. Integral b => b -> b -> bforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnbb)-- | @since 2.01instance(Monoida,Monoidb)=>Monoid(a,b)wheremempty :: (a, b)mempty=(aforall a. Monoid a => amempty,bforall a. Monoid a => amempty)-- | @since 4.9.0.0instance(Semigroupa,Semigroupb,Semigroupc)=>Semigroup(a,b,c)where(aa,bb,cc)<> :: (a, b, c) -> (a, b, c) -> (a, b, c)<>(aa',bb',cc')=(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>aa',bbb -> b -> bforall a. Semigroup a => a -> a -> a<>bb',ccc -> c -> cforall a. Semigroup a => a -> a -> a<>cc')stimes :: forall b. Integral b => b -> (a, b, c) -> (a, b, c)stimesbn(aa,bb,cc)=(b -> a -> aforall b. Integral b => b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa,b -> b -> bforall b. Integral b => b -> b -> bforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnbb,b -> c -> cforall b. Integral b => b -> c -> cforall a b. (Semigroup a, Integral b) => b -> a -> astimesbncc)-- | @since 2.01instance(Monoida,Monoidb,Monoidc)=>Monoid(a,b,c)wheremempty :: (a, b, c)mempty=(aforall a. Monoid a => amempty,bforall a. Monoid a => amempty,cforall a. Monoid a => amempty)-- | @since 4.9.0.0instance(Semigroupa,Semigroupb,Semigroupc,Semigroupd)=>Semigroup(a,b,c,d)where(aa,bb,cc,dd)<> :: (a, b, c, d) -> (a, b, c, d) -> (a, b, c, d)<>(aa',bb',cc',dd')=(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>aa',bbb -> b -> bforall a. Semigroup a => a -> a -> a<>bb',ccc -> c -> cforall a. Semigroup a => a -> a -> a<>cc',ddd -> d -> dforall a. Semigroup a => a -> a -> a<>dd')stimes :: forall b. Integral b => b -> (a, b, c, d) -> (a, b, c, d)stimesbn(aa,bb,cc,dd)=(b -> a -> aforall b. Integral b => b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa,b -> b -> bforall b. Integral b => b -> b -> bforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnbb,b -> c -> cforall b. Integral b => b -> c -> cforall a b. (Semigroup a, Integral b) => b -> a -> astimesbncc,b -> d -> dforall b. Integral b => b -> d -> dforall a b. (Semigroup a, Integral b) => b -> a -> astimesbndd)-- | @since 2.01instance(Monoida,Monoidb,Monoidc,Monoidd)=>Monoid(a,b,c,d)wheremempty :: (a, b, c, d)mempty=(aforall a. Monoid a => amempty,bforall a. Monoid a => amempty,cforall a. Monoid a => amempty,dforall a. Monoid a => amempty)-- | @since 4.9.0.0instance(Semigroupa,Semigroupb,Semigroupc,Semigroupd,Semigroupe)=>Semigroup(a,b,c,d,e)where(aa,bb,cc,dd,ee)<> :: (a, b, c, d, e) -> (a, b, c, d, e) -> (a, b, c, d, e)<>(aa',bb',cc',dd',ee')=(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>aa',bbb -> b -> bforall a. Semigroup a => a -> a -> a<>bb',ccc -> c -> cforall a. Semigroup a => a -> a -> a<>cc',ddd -> d -> dforall a. Semigroup a => a -> a -> a<>dd',eee -> e -> eforall a. Semigroup a => a -> a -> a<>ee')stimes :: forall b. Integral b => b -> (a, b, c, d, e) -> (a, b, c, d, e)stimesbn(aa,bb,cc,dd,ee)=(b -> a -> aforall b. Integral b => b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa,b -> b -> bforall b. Integral b => b -> b -> bforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnbb,b -> c -> cforall b. Integral b => b -> c -> cforall a b. (Semigroup a, Integral b) => b -> a -> astimesbncc,b -> d -> dforall b. Integral b => b -> d -> dforall a b. (Semigroup a, Integral b) => b -> a -> astimesbndd,b -> e -> eforall b. Integral b => b -> e -> eforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnee)-- | @since 2.01instance(Monoida,Monoidb,Monoidc,Monoidd,Monoide)=>Monoid(a,b,c,d,e)wheremempty :: (a, b, c, d, e)mempty=(aforall a. Monoid a => amempty,bforall a. Monoid a => amempty,cforall a. Monoid a => amempty,dforall a. Monoid a => amempty,eforall a. Monoid a => amempty)-- | @since 4.9.0.0instanceSemigroupOrderingwhereOrderingLT<> :: Ordering -> Ordering -> Ordering<>Ordering_=OrderingLTOrderingEQ<>Orderingy=OrderingyOrderingGT<>Ordering_=OrderingGTstimes :: forall b. Integral b => b -> Ordering -> Orderingstimes=b -> Ordering -> Orderingforall b a. (Integral b, Monoid a) => b -> a -> astimesIdempotentMonoid-- lexicographical ordering-- | @since 2.01instanceMonoidOrderingwheremempty :: Orderingmempty=OrderingEQ-- | @since 4.9.0.0instanceSemigroupa=>Semigroup(Maybea)whereMaybe aNothing<> :: Maybe a -> Maybe a -> Maybe a<>Maybe ab=Maybe abMaybe aa<>Maybe aNothing=Maybe aaJustaa<>Justab=a -> Maybe aforall a. a -> Maybe aJust(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>ab)stimes :: forall b. Integral b => b -> Maybe a -> Maybe astimes=b -> Maybe a -> Maybe aforall b a. (Integral b, Semigroup a) => b -> Maybe a -> Maybe astimesMaybe-- | 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 2.01instanceSemigroupa=>Monoid(Maybea)wheremempty :: Maybe amempty=Maybe aforall a. Maybe aNothing-- | @since 4.15instanceApplicativeSolowherepure :: forall a. a -> Solo apure=a -> Solo aforall a. a -> Solo aMkSolo-- 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--   = rMkSoloa -> bf<*> :: forall a b. Solo (a -> b) -> Solo a -> Solo b<*>MkSoloax=b -> Solo bforall a. a -> Solo aMkSolo(a -> bfax)liftA2 :: forall a b c. (a -> b -> c) -> Solo a -> Solo b -> Solo cliftA2a -> b -> cf(MkSoloax)(MkSoloby)=c -> Solo cforall a. a -> Solo aMkSolo(a -> b -> cfaxby)-- | 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 2.01instanceMonoida=>Applicative((,)a)wherepure :: forall a. a -> (a, a)pureax=(aforall a. Monoid a => amempty,ax)(au,a -> bf)<*> :: forall a b. (a, a -> b) -> (a, a) -> (a, b)<*>(av,ax)=(aua -> a -> aforall a. Semigroup a => a -> a -> a<>av,a -> bfax)liftA2 :: forall a b c. (a -> b -> c) -> (a, a) -> (a, b) -> (a, c)liftA2a -> b -> cf(au,ax)(av,by)=(aua -> a -> aforall a. Semigroup a => a -> a -> a<>av,a -> b -> cfaxby)-- | @since 4.15instanceMonadSolowhereMkSoloax>>= :: forall a b. Solo a -> (a -> Solo b) -> Solo b>>=a -> Solo bf=a -> Solo bfax-- | @since 4.9.0.0instanceMonoida=>Monad((,)a)where(au,aa)>>= :: forall a b. (a, a) -> (a -> (a, b)) -> (a, b)>>=a -> (a, b)k=casea -> (a, b)kaaof(av,bb)->(aua -> a -> aforall a. Semigroup a => a -> a -> a<>av,bb)-- | @since 4.14.0.0instanceFunctor((,,)ab)wherefmap :: forall a b. (a -> b) -> (a, b, a) -> (a, b, b)fmapa -> bf(aa,bb,ac)=(aa,bb,a -> bfac)-- | @since 4.14.0.0instance(Monoida,Monoidb)=>Applicative((,,)ab)wherepure :: forall a. a -> (a, b, a)pureax=(aforall a. Monoid a => amempty,bforall a. Monoid a => amempty,ax)(aa,bb,a -> bf)<*> :: forall a b. (a, b, a -> b) -> (a, b, a) -> (a, b, b)<*>(aa',bb',ax)=(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>aa',bbb -> b -> bforall a. Semigroup a => a -> a -> a<>bb',a -> bfax)-- | @since 4.14.0.0instance(Monoida,Monoidb)=>Monad((,,)ab)where(au,bv,aa)>>= :: forall a b. (a, b, a) -> (a -> (a, b, b)) -> (a, b, b)>>=a -> (a, b, b)k=casea -> (a, b, b)kaaof(au',bv',bb)->(aua -> a -> aforall a. Semigroup a => a -> a -> a<>au',bvb -> b -> bforall a. Semigroup a => a -> a -> a<>bv',bb)-- | @since 4.14.0.0instanceFunctor((,,,)abc)wherefmap :: forall a b. (a -> b) -> (a, b, c, a) -> (a, b, c, b)fmapa -> bf(aa,bb,cc,ad)=(aa,bb,cc,a -> bfad)-- | @since 4.14.0.0instance(Monoida,Monoidb,Monoidc)=>Applicative((,,,)abc)wherepure :: forall a. a -> (a, b, c, a)pureax=(aforall a. Monoid a => amempty,bforall a. Monoid a => amempty,cforall a. Monoid a => amempty,ax)(aa,bb,cc,a -> bf)<*> :: forall a b. (a, b, c, a -> b) -> (a, b, c, a) -> (a, b, c, b)<*>(aa',bb',cc',ax)=(aaa -> a -> aforall a. Semigroup a => a -> a -> a<>aa',bbb -> b -> bforall a. Semigroup a => a -> a -> a<>bb',ccc -> c -> cforall a. Semigroup a => a -> a -> a<>cc',a -> bfax)-- | @since 4.14.0.0instance(Monoida,Monoidb,Monoidc)=>Monad((,,,)abc)where(au,bv,cw,aa)>>= :: 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)kaaof(au',bv',cw',bb)->(aua -> a -> aforall a. Semigroup a => a -> a -> a<>au',bvb -> b -> bforall a. Semigroup a => a -> a -> a<>bv',cwc -> c -> cforall a. Semigroup a => a -> a -> a<>cw',bb)-- | @since 4.18.0.0instanceFunctor((,,,,)abcd)wherefmap :: forall a b. (a -> b) -> (a, b, c, d, a) -> (a, b, c, d, b)fmapa -> bf(aa,bb,cc,dd,ae)=(aa,bb,cc,dd,a -> bfae)-- | @since 4.18.0.0instanceFunctor((,,,,,)abcde)wherefmap :: forall a b. (a -> b) -> (a, b, c, d, e, a) -> (a, b, c, d, e, b)fmapa -> bfun(aa,bb,cc,dd,ee,af)=(aa,bb,cc,dd,ee,a -> bfunaf)-- | @since 4.18.0.0instanceFunctor((,,,,,,)abcdef)wherefmap :: forall a b.(a -> b) -> (a, b, c, d, e, f, a) -> (a, b, c, d, e, f, b)fmapa -> bfun(aa,bb,cc,dd,ee,ff,ag)=(aa,bb,cc,dd,ee,ff,a -> bfunag)-- | @since 4.10.0.0instanceSemigroupa=>Semigroup(IOa)where<> :: IO a -> IO a -> IO a(<>)=(a -> a -> a) -> IO a -> IO a -> IO aforall a b c. (a -> b -> c) -> IO a -> IO b -> IO cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2a -> a -> aforall a. Semigroup a => a -> a -> a(<>)-- | @since 4.9.0.0instanceMonoida=>Monoid(IOa)wheremempty :: IO amempty=a -> IO aforall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apureaforall a. Monoid a => amempty{- | 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 thestructure 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' andthe first law, so you need only check that the former condition holds.See <https://www.schoolofhaskell.com/user/edwardk/snippets/fmap> or<https://github.com/quchen/articles/blob/master/second_functor_law.md>for an explanation.-}classFunctorfwhere-- | '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)->fa->fb-- | 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->fb->fa(<$)=(b -> a) -> f b -> f aforall a b. (a -> b) -> f a -> f bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f bfmap((b -> a) -> f b -> f a) -> (a -> b -> a) -> a -> f b -> f aforall b c a. (b -> c) -> (a -> b) -> a -> c.a -> b -> aforall a b. a -> b -> aconst-- | 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).classFunctorf=>Applicativefwhere{-# MINIMALpure,((<*>)|liftA2)#-}-- | Lift a value.pure::a->fa-- | Sequential application.---- A few functors support an implementation of '<*>' that is more-- efficient than the default one.---- ==== __Example__-- Used in combination with @('<$>')@, @('<*>')@ 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)->fa->fb(<*>)=((a -> b) -> a -> b) -> f (a -> b) -> f a -> f bforall a b c. (a -> b -> c) -> f a -> f b -> f cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2(a -> b) -> a -> bforall a. a -> aid-- | 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::(a->b->c)->fa->fb->fcliftA2a -> b -> cff ax=f (b -> c) -> f b -> f cforall a b. f (a -> b) -> f a -> f bforall (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 bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f bfmapa -> b -> cff ax)-- | 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 Text.ParserCombinators.ReadP-- >>> let p = string "my name is " *> munch1 isAlpha <* eof-- >>> readP_to_S p "my name is Simon"-- [("Simon","")](*>)::fa->fb->fbf aa1*>f ba2=(b -> bforall a. a -> aid(b -> b) -> f a -> f (b -> b)forall a b. a -> f b -> f aforall (f :: * -> *) a b. Functor f => a -> f b -> f a<$f aa1)f (b -> b) -> f b -> f bforall a b. f (a -> b) -> f a -> f bforall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b<*>f ba2-- 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.--(<*)::fa->fb->fa(<*)=(a -> b -> a) -> f a -> f b -> f aforall a b c. (a -> b -> c) -> f a -> f b -> f cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2a -> b -> aforall a b. a -> b -> aconst-- | 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(<**>)::Applicativef=>fa->f(a->b)->fb<**> :: forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b(<**>)=(a -> (a -> b) -> b) -> f a -> f (a -> b) -> f bforall a b c. (a -> b -> c) -> f a -> f b -> f cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2(\aaa -> bf->a -> bfaa)-- | 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::Applicativef=>(a->b)->fa->fbliftA :: forall (f :: * -> *) a b. Applicative f => (a -> b) -> f a -> f bliftAa -> bff aa=(a -> b) -> f (a -> b)forall a. a -> f aforall (f :: * -> *) a. Applicative f => a -> f apurea -> bff (a -> b) -> f a -> f bforall a b. f (a -> b) -> f a -> f bforall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b<*>f aa-- 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::Applicativef=>(a->b->c->d)->fa->fb->fc->fdliftA3 :: forall (f :: * -> *) a b c d.Applicative f =>(a -> b -> c -> d) -> f a -> f b -> f c -> f dliftA3a -> b -> c -> dff aaf bbf cc=(a -> b -> c -> d) -> f a -> f b -> f (c -> d)forall a b c. (a -> b -> c) -> f a -> f b -> f cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2a -> b -> c -> dff aaf bbf (c -> d) -> f c -> f dforall a b. f (a -> b) -> f a -> f bforall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b<*>f cc{-# INLINABLEliftA#-}{-# SPECIALISEliftA::(a1->r)->IOa1->IOr#-}{-# SPECIALISEliftA::(a1->r)->Maybea1->Mayber#-}{-# INLINABLEliftA3#-}{-# SPECIALISEliftA3::(a1->a2->a3->r)->IOa1->IOa2->IOa3->IOr#-}{-# SPECIALISEliftA3::(a1->a2->a3->r)->Maybea1->Maybea2->Maybea3->Mayber#-}-- | 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__---- 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.Conc.atomically' :: STM a -> IO a-- @---- is used to run 'GHC.Conc.STM' transactions atomically. So, by-- specializing the types of 'GHC.Conc.atomically' and 'join' to---- @-- 'GHC.Conc.atomically' :: STM (IO b) -> IO (IO b)-- 'join'       :: IO (IO b)  -> IO b-- @---- we can compose them as---- @-- 'join' . 'GHC.Conc.atomically' :: STM (IO b) -> IO b-- @---- to run an 'GHC.Conc.STM' transaction and the 'IO' action it-- returns.join::(Monadm)=>m(ma)->majoin :: forall (m :: * -> *) a. Monad m => m (m a) -> m ajoinm (m a)x=m (m a)xm (m a) -> (m a -> m a) -> m aforall a b. m a -> (a -> m b) -> m bforall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b>>=m a -> m aforall a. a -> aid{- | 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 tothink of a monad as an /abstract datatype/ of actions.Haskell's @do@ expressions provide a convenient syntax for writingmonadic 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 lists, 'Data.Maybe.Maybe' and 'System.IO.IO'defined in the "Prelude" satisfy these laws.-}classApplicativem=>Monadmwhere-- | 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-- @(>>=)::forallab.ma->(a->mb)->mb-- | 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-- @(>>)::forallab.ma->mb->mbm am>>m bk=m amm a -> (a -> m b) -> m bforall a b. m a -> (a -> m b) -> m bforall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b>>=\a_->m bk-- See Note [Recursive bindings for Applicative/Monad]{-# INLINE(>>)#-}-- | Inject a value into the monadic type.return::a->mareturn=a -> m aforall a. a -> m aforall (f :: * -> *) a. Applicative f => a -> f apure{- Note [Recursive bindings for Applicative/Monad]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The original Applicative/Monad proposal stated that afterimplementation, 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 statedproposal, but you really shouldn't! Why? Because people tend to definesuch instances the /other/ way around: in particular, it is perfectlylegitimate to define an instance of Applicative (*>) in terms of (>>),which would lead to an infinite loop for the default implementation ofMonad! And people do this in the wild.This turned into a nasty bug that was tricky to track down, and ratherthan eliminate it everywhere upstream, it's easier to just retain theoriginal default.-}-- | Same as '>>=', but with the arguments interchanged.{-# SPECIALISE(=<<)::(a->[b])->[a]->[b]#-}(=<<)::Monadm=>(a->mb)->ma->mba -> m bf=<< :: forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b=<<m ax=m axm a -> (a -> m b) -> m bforall a b. m a -> (a -> m b) -> m bforall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b>>=a -> m bf-- | Conditional execution of 'Applicative' expressions. For example,---- > when debug (putStrLn "Debugging")---- will output the string @Debugging@ if the Boolean value @debug@-- is 'True', and otherwise do nothing.when::(Applicativef)=>Bool->f()->f(){-# INLINABLEwhen#-}{-# SPECIALISEwhen::Bool->IO()->IO()#-}{-# SPECIALISEwhen::Bool->Maybe()->Maybe()#-}when :: forall (f :: * -> *). Applicative f => Bool -> f () -> f ()whenBoolpf ()s=ifBoolpthenf ()selse() -> f ()forall a. a -> f aforall (f :: * -> *) a. Applicative f => a -> f apure()-- | Evaluate each action in the sequence from left to right,-- and collect the results.sequence::Monadm=>[ma]->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]mapMm a -> m aforall a. a -> aid-- Note: [sequence and mapM]-- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@.mapM::Monadm=>(a->mb)->[a]->m[b]{-# INLINEmapM#-}mapM :: forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]mapMa -> m bf[a]as=(a -> m [b] -> m [b]) -> m [b] -> [a] -> m [b]forall a b. (a -> b -> b) -> b -> [a] -> bfoldra -> m [b] -> m [b]k([b] -> m [b]forall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn[])[a]aswherek :: a -> m [b] -> m [b]kaam [b]r=do{bx<-a -> m bfaa;[b]xs<-m [b]r;[b] -> m [b]forall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(bxb -> [b] -> [b]forall a. a -> [a] -> [a]:[b]xs)}{-Note: [sequence and mapM]~~~~~~~~~~~~~~~~~~~~~~~~~Originally, we definedmapM f = sequence . map fThis relied on list fusion to produce efficient code for mapM, and led toexcessive allocation in cryptarithm2. Definingsequence = mapM idrelies only on inlining a tiny function (id) and beta reduction, which tends tobe a more reliable aspect of simplification. Indeed, this does not lead tosimilar problems in nofib.-}-- | Promote a function to a monad.liftM::(Monadm)=>(a1->r)->ma1->mrliftM :: forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m rliftMa1 -> rfm a1m1=do{a1x1<-m a1m1;r -> m rforall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(a1 -> rfa1x1)}-- | Promote a function to a monad, scanning the monadic arguments from-- left to right.  For example,---- > liftM2 (+) [0,1] [0,2] = [0,2,1,3]-- > liftM2 (+) (Just 1) Nothing = Nothing--liftM2::(Monadm)=>(a1->a2->r)->ma1->ma2->mrliftM2 :: forall (m :: * -> *) a1 a2 r.Monad m =>(a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2a1 -> a2 -> rfm a1m1m a2m2=do{a1x1<-m a1m1;a2x2<-m a2m2;r -> m rforall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(a1 -> a2 -> rfa1x1a2x2)}-- 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::(Monadm)=>(a1->a2->a3->r)->ma1->ma2->ma3->mrliftM3 :: forall (m :: * -> *) a1 a2 a3 r.Monad m =>(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m rliftM3a1 -> a2 -> a3 -> rfm a1m1m a2m2m a3m3=do{a1x1<-m a1m1;a2x2<-m a2m2;a3x3<-m a3m3;r -> m rforall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(a1 -> a2 -> a3 -> rfa1x1a2x2a3x3)}-- | Promote a function to a monad, scanning the monadic arguments from-- left to right (cf. 'liftM2').liftM4::(Monadm)=>(a1->a2->a3->a4->r)->ma1->ma2->ma3->ma4->mrliftM4 :: forall (m :: * -> *) a1 a2 a3 a4 r.Monad m =>(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m rliftM4a1 -> a2 -> a3 -> a4 -> rfm a1m1m a2m2m a3m3m a4m4=do{a1x1<-m a1m1;a2x2<-m a2m2;a3x3<-m a3m3;a4x4<-m a4m4;r -> m rforall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(a1 -> a2 -> a3 -> a4 -> rfa1x1a2x2a3x3a4x4)}-- | Promote a function to a monad, scanning the monadic arguments from-- left to right (cf. 'liftM2').liftM5::(Monadm)=>(a1->a2->a3->a4->a5->r)->ma1->ma2->ma3->ma4->ma5->mrliftM5 :: 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 rliftM5a1 -> a2 -> a3 -> a4 -> a5 -> rfm a1m1m a2m2m a3m3m a4m4m a5m5=do{a1x1<-m a1m1;a2x2<-m a2m2;a3x3<-m a3m3;a4x4<-m a4m4;a5x5<-m a5m5;r -> m rforall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(a1 -> a2 -> a3 -> a4 -> a5 -> rfa1x1a2x2a3x3a4x4a5x5)}{-# INLINABLEliftM#-}{-# SPECIALISEliftM::(a1->r)->IOa1->IOr#-}{-# SPECIALISEliftM::(a1->r)->Maybea1->Mayber#-}{-# INLINABLEliftM2#-}{-# SPECIALISEliftM2::(a1->a2->r)->IOa1->IOa2->IOr#-}{-# SPECIALISEliftM2::(a1->a2->r)->Maybea1->Maybea2->Mayber#-}{-# INLINABLEliftM3#-}{-# SPECIALISEliftM3::(a1->a2->a3->r)->IOa1->IOa2->IOa3->IOr#-}{-# SPECIALISEliftM3::(a1->a2->a3->r)->Maybea1->Maybea2->Maybea3->Mayber#-}{-# INLINABLEliftM4#-}{-# SPECIALISEliftM4::(a1->a2->a3->a4->r)->IOa1->IOa2->IOa3->IOa4->IOr#-}{-# SPECIALISEliftM4::(a1->a2->a3->a4->r)->Maybea1->Maybea2->Maybea3->Maybea4->Mayber#-}{-# INLINABLEliftM5#-}{-# SPECIALISEliftM5::(a1->a2->a3->a4->a5->r)->IOa1->IOa2->IOa3->IOa4->IOa5->IOr#-}{-# SPECIALISEliftM5::(a1->a2->a3->a4->a5->r)->Maybea1->Maybea2->Maybea3->Maybea4->Maybea5->Mayber#-}{- | In many situations, the 'liftM' operations can be replaced by uses of'ap', which promotes function application.> return f `ap` x1 `ap` ... `ap` xnis equivalent to> liftMn f x1 x2 ... xn-}ap::(Monadm)=>m(a->b)->ma->mbap :: forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m bapm (a -> b)m1m am2=do{a -> bx1<-m (a -> b)m1;ax2<-m am2;b -> m bforall a. a -> m aforall (m :: * -> *) a. Monad m => a -> m areturn(a -> bx1ax2)}-- Since many Applicative instances define (<*>) = ap, we-- cannot define ap = (<*>){-# INLINABLEap#-}{-# SPECIALISEap::IO(a->b)->IOa->IOb#-}{-# SPECIALISEap::Maybe(a->b)->Maybea->Maybeb#-}-- instances for Prelude types-- | @since 2.01instanceFunctor((->)r)wherefmap :: forall a b. (a -> b) -> (r -> a) -> r -> bfmap=(a -> b) -> (r -> a) -> r -> bforall b c a. (b -> c) -> (a -> b) -> a -> c(.)-- | @since 2.01instanceApplicative((->)r)wherepure :: forall a. a -> r -> apure=a -> r -> aforall a b. a -> b -> aconst<*> :: forall a b. (r -> (a -> b)) -> (r -> a) -> r -> b(<*>)r -> (a -> b)fr -> agrx=r -> (a -> b)frx(r -> agrx)liftA2 :: forall a b c. (a -> b -> c) -> (r -> a) -> (r -> b) -> r -> cliftA2a -> b -> cqr -> afr -> bgrx=a -> b -> cq(r -> afrx)(r -> bgrx)-- | @since 2.01instanceMonad((->)r)wherer -> af>>= :: forall a b. (r -> a) -> (a -> r -> b) -> r -> b>>=a -> r -> bk=\rr->a -> r -> bk(r -> afrr)rr-- | @since 4.15instanceFunctorSolowherefmap :: forall a b. (a -> b) -> Solo a -> Solo bfmapa -> bf(MkSoloaa)=b -> Solo bforall a. a -> Solo aMkSolo(a -> bfaa)-- 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.ax<$ :: forall a b. a -> Solo b -> Solo a<$MkSolob_=a -> Solo aforall a. a -> Solo aMkSoloax-- | @since 2.01instanceFunctor((,)a)wherefmap :: forall a b. (a -> b) -> (a, a) -> (a, b)fmapa -> bf(ax,ay)=(ax,a -> bfay)-- | @since 2.01instanceFunctorMaybewherefmap :: forall a b. (a -> b) -> Maybe a -> Maybe bfmapa -> b_Maybe aNothing=Maybe bforall a. Maybe aNothingfmapa -> bf(Justaa)=b -> Maybe bforall a. a -> Maybe aJust(a -> bfaa)-- | @since 2.01instanceApplicativeMaybewherepure :: forall a. a -> Maybe apure=a -> Maybe aforall a. a -> Maybe aJustJusta -> bf<*> :: forall a b. Maybe (a -> b) -> Maybe a -> Maybe b<*>Maybe am=(a -> b) -> Maybe a -> Maybe bforall a b. (a -> b) -> Maybe a -> Maybe bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f bfmapa -> bfMaybe amMaybe (a -> b)Nothing<*>Maybe a_m=Maybe bforall a. Maybe aNothingliftA2 :: forall a b c. (a -> b -> c) -> Maybe a -> Maybe b -> Maybe cliftA2a -> b -> cf(Justax)(Justby)=c -> Maybe cforall a. a -> Maybe aJust(a -> b -> cfaxby)liftA2a -> b -> c_Maybe a_Maybe b_=Maybe cforall a. Maybe aNothingJusta_m1*> :: forall a b. Maybe a -> Maybe b -> Maybe b*>Maybe bm2=Maybe bm2Maybe aNothing*>Maybe b_m2=Maybe bforall a. Maybe aNothing-- | @since 2.01instanceMonadMaybewhere(Justax)>>= :: forall a b. Maybe a -> (a -> Maybe b) -> Maybe b>>=a -> Maybe bk=a -> Maybe bkaxMaybe aNothing>>=a -> Maybe b_=Maybe bforall a. Maybe aNothing>> :: forall a b. Maybe a -> Maybe b -> Maybe b(>>)=Maybe a -> Maybe b -> Maybe bforall a b. Maybe a -> Maybe b -> Maybe bforall (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' []@classApplicativef=>Alternativefwhere-- | The identity of '<|>'empty::fa-- | An associative binary operation(<|>)::fa->fa->fa-- | One or more.some::fa->f[a]somef av=f [a]some_vwheremany_v :: f [a]many_v=f [a]some_vf [a] -> f [a] -> f [a]forall a. f a -> f a -> f aforall (f :: * -> *) a. Alternative f => f a -> f a -> f a<|>[a] -> f [a]forall a. a -> f aforall (f :: * -> *) a. Applicative f => a -> f apure[]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 cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2(:)f avf [a]many_v-- | Zero or more.many::fa->f[a]manyf av=f [a]many_vwheremany_v :: f [a]many_v=f [a]some_vf [a] -> f [a] -> f [a]forall a. f a -> f a -> f aforall (f :: * -> *) a. Alternative f => f a -> f a -> f a<|>[a] -> f [a]forall a. a -> f aforall (f :: * -> *) a. Applicative f => a -> f apure[]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 cforall (f :: * -> *) a b c.Applicative f =>(a -> b -> c) -> f a -> f b -> f cliftA2(:)f avf [a]many_v-- | Picks the leftmost 'Just' value, or, alternatively, 'Nothing'.---- @since 2.01instanceAlternativeMaybewhereempty :: forall a. Maybe aempty=Maybe aforall a. Maybe aNothingMaybe aNothing<|> :: forall a. Maybe a -> Maybe a -> Maybe a<|>Maybe ar=Maybe arMaybe al<|>Maybe a_=Maybe al-- ------------------------------------------------------------------------------- The MonadPlus class definition-- | Monads that also support choice and failure.class(Alternativem,Monadm)=>MonadPlusmwhere-- | The identity of 'mplus'.  It should also satisfy the equations---- > mzero >>= f  =  mzero-- > v >> mzero   =  mzero---- The default definition is---- @-- mzero = 'empty'-- @mzero::mamzero=m aforall a. m aforall (f :: * -> *) a. Alternative f => f aempty-- | An associative operation. The default definition is---- @-- mplus = ('<|>')-- @mplus::ma->ma->mamplus=m a -> m a -> m aforall a. m a -> m a -> m aforall (f :: * -> *) a. Alternative f => f a -> f a -> f a(<|>)-- | Picks the leftmost 'Just' value, or, alternatively, 'Nothing'.---- @since 2.01instanceMonadPlusMaybe----------------------------------------------- The non-empty list typeinfixr5:|-- | Non-empty (and non-strict) list type.---- @since 4.9.0.0dataNonEmptya=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 -> Boolforall 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 -> BoolEq-- ^ @since 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 -> BoolNonEmpty a -> NonEmpty a -> OrderingNonEmpty a -> NonEmpty a -> NonEmpty aforall 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 aforall a. Ord a => Eq (NonEmpty a)forall a. Ord a => NonEmpty a -> NonEmpty a -> Boolforall a. Ord a => NonEmpty a -> NonEmpty a -> Orderingforall a. Ord a => NonEmpty a -> NonEmpty a -> NonEmpty a$ccompare :: forall a. Ord a => NonEmpty a -> NonEmpty a -> Orderingcompare :: 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 amax :: NonEmpty a -> NonEmpty a -> NonEmpty a$cmin :: forall a. Ord a => NonEmpty a -> NonEmpty a -> NonEmpty amin :: NonEmpty a -> NonEmpty a -> NonEmpty aOrd-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceFunctorNonEmptywherefmap :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty bfmapa -> bf~(aa:|[a]as)=a -> bfaab -> [b] -> NonEmpty bforall 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 bfmapa -> bf[a]asab<$ :: forall a b. a -> NonEmpty b -> NonEmpty a<$~(b_:|[b]as)=aba -> [a] -> NonEmpty aforall a. a -> [a] -> NonEmpty a:|(aba -> [b] -> [a]forall a b. a -> [b] -> [a]forall (f :: * -> *) a b. Functor f => a -> f b -> f a<$[b]as)-- | @since 4.9.0.0instanceApplicativeNonEmptywherepure :: forall a. a -> NonEmpty apureaa=aaa -> [a] -> NonEmpty aforall a. a -> [a] -> NonEmpty a:|[]<*> :: forall a b. NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b(<*>)=NonEmpty (a -> b) -> NonEmpty a -> NonEmpty bforall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m bapliftA2 :: forall a b c.(a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty cliftA2=(a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty cforall (m :: * -> *) a1 a2 r.Monad m =>(a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2-- | @since 4.9.0.0instanceMonadNonEmptywhere~(aa:|[a]as)>>= :: forall a b. NonEmpty a -> (a -> NonEmpty b) -> NonEmpty b>>=a -> NonEmpty bf=bbb -> [b] -> NonEmpty bforall a. a -> [a] -> NonEmpty a:|([b]bs[b] -> [b] -> [b]forall a. [a] -> [a] -> [a]++[b]bs')wherebb:|[b]bs=a -> NonEmpty bfaabs' :: [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 bftoList :: NonEmpty a -> [a]toList~(ac:|[a]cs)=aca -> [a] -> [a]forall a. a -> [a] -> [a]:[a]cs------------------------------------------------ The list type-- | @since 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 2.01instanceApplicative[]where{-# INLINEpure#-}pure :: forall a. a -> [a]pureax=[ax]{-# INLINE(<*>)#-}[a -> b]fs<*> :: forall a b. [a -> b] -> [a] -> [b]<*>[a]xs=[a -> bfax|a -> bf<-[a -> b]fs,ax<-[a]xs]{-# INLINEliftA2#-}liftA2 :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]liftA2a -> b -> cf[a]xs[b]ys=[a -> b -> cfaxby|ax<-[a]xs,by<-[b]ys]{-# INLINE(*>)#-}[a]xs*> :: forall a b. [a] -> [b] -> [b]*>[b]ys=[by|a_<-[a]xs,by<-[b]ys]-- See Note: [List comprehensions and inlining]-- | @since 2.01instanceMonad[]where{-# INLINE(>>=)#-}[a]xs>>= :: forall a b. [a] -> (a -> [b]) -> [b]>>=a -> [b]f=[by|ax<-[a]xs,by<-a -> [b]fax]{-# 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 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 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] -> bfoldra -> b -> bkbz=[a] -> bgowherego :: [a] -> bgo[]=bzgo(ay:[a]ys)=aya -> b -> b`k`[a] -> bgo[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]buildforall b. (a -> b -> b) -> b -> bg=(a -> [a] -> [a]) -> [a] -> [a]forall b. (a -> b -> b) -> b -> bg(:)[]-- | 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]augmentforall b. (a -> b -> b) -> b -> bg[a]xs=(a -> [a] -> [a]) -> [a] -> [a]forall b. (a -> b -> b) -> b -> bg(:)[a]xs{-# RULES"fold/build"forallkz(g::forallb.(a->b->b)->b->b).foldrkz(buildg)=gkz"foldr/augment"forallkzxs(g::forallb.(a->b->b)->b->b).foldrkz(augmentgxs)=gk(foldrkzxs)"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"forallkzx.foldrkz[x]=kxz"foldr/nil"forallkz.foldrkz[]=z"foldr/cons/build"forallkzx(g::forallb.(a->b->b)->b->b).foldrkz(x:buildg)=kx(gkz)"augment/build"forall(g::forallb.(a->b->b)->b->b)(h::forallb.(a->b->b)->b->b).augmentg(buildh)=build(\cn->gc(hcn))"augment/nil"forall(g::forallb.(a->b->b)->b->b).augmentg[]=buildg#-}-- 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]mapa -> b_[]=[]mapa -> bf(ax:[a]xs)=a -> bfaxb -> [b] -> [b]forall a. a -> [a] -> [a]:(a -> b) -> [a] -> [b]forall a b. (a -> b) -> [a] -> [b]mapa -> bf[a]xs-- Note eta expandedmapFB::(elt->lst->lst)->(a->elt)->a->lst->lst{-# INLINE[0]mapFB#-}-- See Note [Inline FB functions] in GHC.ListmapFB :: forall elt lst a.(elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lstmapFBelt -> lst -> lstca -> eltf=\axlstys->elt -> lst -> lstc(a -> eltfax)lstys{- 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.List  See also Note [Inline FB functions] in GHC.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]forallfxs.mapfxs=build(\cn->foldr(mapFBcf)nxs)"mapList"[1]forallf.foldr(mapFB(:)f)[]=mapf"mapFB"forallcfg.mapFB(mapFBcf)g=mapFBc(f.g)"mapFB/id"forallc.mapFBc(\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]mapcoerce=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 'Data.List.concat' @=@ 'Data.List.foldr' '(++)' @[]@-- has linear performance, while '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(++)(ax:[a]xs)[a]ys=axa -> [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]forallxsys.xs++ys=augment(\cn->foldrcnxs)ys#-}-- |'otherwise' is defined as the value 'True'.  It helps to make-- guards more readable.  eg.---- >  f x | x < 0     = ...-- >      | otherwise = ...otherwise::Boolotherwise :: Boolotherwise=BoolTrue------------------------------------------------ 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->CharunsafeChr :: Int -> CharunsafeChr(I#Int#i#)=Char# -> CharC#(Int# -> Char#chr#Int#i#)-- | The 'Prelude.fromEnum' method restricted to the type 'Data.Char.Char'.ord::Char->Intord :: Char -> Intord(C#Char#c#)=Int# -> IntI#(Char# -> Int#ord#Char#c#)-- | This 'String' equality predicate is used when desugaring-- pattern-matches against strings.eqString::String->String->BooleqString :: String -> String -> BooleqString[][]=BoolTrueeqString(Charc1:Stringcs1)(Charc2:Stringcs2)=Charc1Char -> Char -> Boolforall a. Eq a => a -> a -> Bool==Charc2Bool -> Bool -> Bool&&Stringcs1String -> String -> Bool`eqString`Stringcs2eqStringString_String_=BoolFalse{-# 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 == 31minInt=I#(-0x40000000#)maxInt=I#0x3FFFFFFF##elif WORD_SIZE_IN_BITS == 32minInt=I#(-0x80000000#)maxInt=I#0x7FFFFFFF##elseminInt :: IntminInt=Int# -> IntI#(Int#-0x8000000000000000#)maxInt :: IntmaxInt=Int# -> IntI#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->aid :: forall a. a -> aidax=ax-- 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->aassert :: forall a. Bool -> a -> aassertBool_predar=arbreakpoint::a->abreakpoint :: forall a. a -> abreakpointar=arbreakpointCond::Bool->a->abreakpointCond :: forall a. Bool -> a -> abreakpointCondBool_ar=ardataOpaque=foralla.Oa-- | @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->aconst :: forall a b. a -> b -> aconstaxb_=ax-- | 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 -> cfa -> bg=\ax->b -> cf(a -> bgax)-- | @'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::(a->b->c)->b->a->cflip :: forall a b c. (a -> b -> c) -> b -> a -> cflipa -> b -> cfbxay=a -> b -> cfaybx-- 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 + 5expr = ((min 5) 1) + 5@@($)@ has precedence 0 (the lowest) and associates to the right, so these are equivalent:@expr = min 5 $ 1 + 5expr = (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" == 98strSum :: 'String' -> 'Int'strSum s = 'sum' ('Data.Maybe.mapMaybe' 'Text.Read.readMaybe' ('words' s))@we can deploy the function application operator:@-- | Sum numbers in a string: strSum "100  5 -7" == 98strSum :: 'String' -> 'Int'strSum s = 'sum' '$' 'Data.Maybe.mapMaybe' '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 -> IntfastMod (I# x) (I# m) = I# $ remInt# x m@-}{-# INLINE($)#-}($)::forallreparepb(a::TYPErepa)(b::TYPErepb).(a->b)->a->b$ :: forall a b. (a -> b) -> a -> b($)a -> bf=a -> bf-- | 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.($!)::forallra(b::TYPEr).(a->b)->a->b{-# INLINE($!)#-}a -> bf$! :: forall a b. (a -> b) -> a -> b$!ax=let!vx :: avx=axina -> bfavx-- see #2273-- | @'until' p f@ yields the result of applying @f@ until @p@ holds.until::(a->Bool)->(a->a)->a->auntil :: forall a. (a -> Bool) -> (a -> a) -> a -> auntila -> Boolpa -> af=a -> agowherego :: a -> agoax|a -> Boolpax=ax|Boolotherwise=a -> ago(a -> afax)-- | '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->aasTypeOf :: forall a. a -> a -> aasTypeOf=a -> a -> aforall a b. a -> b -> aconst------------------------------------------------ Functor/Applicative/Monad instances for IO------------------------------------------------ | @since 2.01instanceFunctorIOwherefmap :: forall a b. (a -> b) -> IO a -> IO bfmapa -> bfIO ax=IO axIO a -> (a -> IO b) -> IO bforall a b. IO a -> (a -> IO b) -> IO bforall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b>>=(b -> IO bforall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apure(b -> IO b) -> (a -> b) -> a -> IO bforall b c a. (b -> c) -> (a -> b) -> a -> c.a -> bf)-- | @since 2.01instanceApplicativeIOwhere{-# INLINEpure#-}{-# INLINE(*>)#-}{-# INLINEliftA2#-}pure :: forall a. a -> IO apure=a -> IO aforall a. a -> IO areturnIO*> :: forall a b. IO a -> IO b -> IO b(*>)=IO a -> IO b -> IO bforall a b. IO a -> IO b -> IO bthenIO<*> :: forall a b. IO (a -> b) -> IO a -> IO b(<*>)=IO (a -> b) -> IO a -> IO bforall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m bapliftA2 :: forall a b c. (a -> b -> c) -> IO a -> IO b -> IO cliftA2=(a -> b -> c) -> IO a -> IO b -> IO cforall (m :: * -> *) a1 a2 r.Monad m =>(a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2-- | @since 2.01instanceMonadIOwhere{-# INLINE(>>)#-}{-# INLINE(>>=)#-}>> :: forall a b. IO a -> IO b -> IO b(>>)=IO a -> IO b -> IO bforall a b. IO a -> IO b -> IO bforall (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 bforall a b. IO a -> (a -> IO b) -> IO bbindIO-- | Takes the first non-throwing 'IO' action\'s result.-- 'empty' throws an exception.---- @since 4.9.0.0instanceAlternativeIOwhereempty :: forall a. IO aempty=String -> IO aforall a. String -> IO afailIOString"mzero"<|> :: forall a. IO a -> IO a -> IO a(<|>)=IO a -> IO a -> IO aforall a. IO a -> IO a -> IO amplusIO-- | Takes the first non-throwing 'IO' action\'s result.-- 'mzero' throws an exception.---- @since 4.9.0.0instanceMonadPlusIOreturnIO::a->IOareturnIO :: forall a. a -> IO areturnIOax=(State# RealWorld -> (# State# RealWorld, a #)) -> IO aforall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO(\State# RealWorlds->(#State# RealWorlds,ax#))bindIO::IOa->(a->IOb)->IObbindIO :: forall a b. IO a -> (a -> IO b) -> IO bbindIO(IOState# RealWorld -> (# State# RealWorld, a #)m)a -> IO bk=(State# RealWorld -> (# State# RealWorld, b #)) -> IO bforall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO(\State# RealWorlds->caseState# RealWorld -> (# State# RealWorld, a #)mState# RealWorldsof(#State# RealWorldnew_s,aa#)->IO b -> State# RealWorld -> (# State# RealWorld, b #)forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)unIO(a -> IO bkaa)State# RealWorldnew_s)thenIO::IOa->IOb->IObthenIO :: forall a b. IO a -> IO b -> IO bthenIO(IOState# RealWorld -> (# State# RealWorld, a #)m)IO bk=(State# RealWorld -> (# State# RealWorld, b #)) -> IO bforall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO(\State# RealWorlds->caseState# RealWorld -> (# State# RealWorld, a #)mState# RealWorldsof(#State# RealWorldnew_s,a_#)->IO b -> State# RealWorld -> (# State# RealWorld, b #)forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)unIOIO bkState# RealWorldnew_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->IOafailIO :: forall a. String -> IO afailIOStrings=(State# RealWorld -> (# State# RealWorld, a #)) -> IO aforall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO(SomeException -> State# RealWorld -> (# State# RealWorld, a #)forall a b. a -> State# RealWorld -> (# State# RealWorld, b #)raiseIO#(String -> SomeExceptionmkUserErrorStrings))unIO::IOa->(State#RealWorld->(#State#RealWorld,a#))unIO :: forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)unIO(IOState# RealWorld -> (# State# RealWorld, a #)a)=State# RealWorld -> (# State# RealWorld, a #)a{- |Returns the tag of a constructor application; this function is usedby the deriving code for Eq, Ord and Enum.-}{-# INLINEgetTag#-}getTag::a->Int#getTag :: forall a. a -> Int#getTagax=a -> Int#forall a. a -> Int#dataToTag#ax------------------------------------------------ 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# -> IntI#(Int#xInt# -> 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# -> IntI#(Int#xInt# -> 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# -> IntI#(Int#xInt# -> 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# -> IntI#(Int#xInt# -> 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#xInt# -> Int# -> (# Int#, Int# #)`quotRemInt#`Int#yof(#Int#q,Int#r#)->(Int# -> IntI#Int#q,Int# -> IntI#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#xInt# -> Int# -> (# Int#, Int# #)`divModInt#`Int#yof(#Int#q,Int#r#)->(Int# -> IntI#Int#q,Int# -> IntI#Int#r){- Note [INLINE division wrappers]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The Int division functions such as 'quotRemInt' and 'divModInt' havebeen manually worker/wrappered, presumably because they construct*nested* products.We intend to preserve the exact worker/wrapper split, hence we markthe wrappers INLINE (#19267). That makes sure the optimiser doesn'taccidentally inline the worker into the wrapper, undoing the manualsplit 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_maskInt#mInt#b=Int# -> Int#negateInt#(Int#bInt# -> Int# -> Int#<#Int#m)-- | Shift the argument left by the specified number of bits-- (which must be non-negative).shiftL#::Word#->Int#->Word#Word#ashiftL# :: Word# -> Int# -> Word#`shiftL#`Int#b=(Word#aWord# -> Int# -> Word#`uncheckedShiftL#`Int#b)Word# -> Word# -> Word#`and#`Int# -> Word#int2Word#(Int# -> Int# -> Int#shift_maskWORD_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#ashiftRL# :: Word# -> Int# -> Word#`shiftRL#`Int#b=(Word#aWord# -> Int# -> Word#`uncheckedShiftRL#`Int#b)Word# -> Word# -> Word#`and#`Int# -> Word#int2Word#(Int# -> Int# -> Int#shift_maskWORD_SIZE_IN_BITS#b)-- | Shift the argument left by the specified number of bits-- (which must be non-negative).iShiftL#::Int#->Int#->Int#Int#aiShiftL# :: Int# -> Int# -> Int#`iShiftL#`Int#b=(Int#aInt# -> Int# -> Int#`uncheckedIShiftL#`Int#b)Int# -> Int# -> Int#`andI#`Int# -> Int# -> Int#shift_maskWORD_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#aiShiftRA# :: Int# -> Int# -> Int#`iShiftRA#`Int#b|Int# -> BoolisTrue#(Int#bInt# -> Int# -> Int#>=#WORD_SIZE_IN_BITS#)=negateInt#(a<#0#)|Boolotherwise=Int#aInt# -> 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#aiShiftRL# :: Int# -> Int# -> Int#`iShiftRL#`Int#b=(Int#aInt# -> Int# -> Int#`uncheckedIShiftRL#`Int#b)Int# -> Int# -> Int#`andI#`Int# -> Int# -> Int#shift_maskWORD_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"forallan.unpackFoldrCString#a(:)n=unpackAppendCString#an"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"forallan.unpackFoldrCStringUtf8#a(:)n=unpackAppendCStringUtf8#an"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#-}

[8]ページ先頭

©2009-2025 Movatter.jp