Movatterモバイル変換
[0]ホーム
{-# LANGUAGE CPP #-}{-# LANGUAGE DefaultSignatures #-}{-# LANGUAGE DeriveDataTypeable #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE PolyKinds #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE Trustworthy #-}{-# LANGUAGE TypeOperators #-}------------------------------------------------------------------------------- |-- Module : Data.Semigroup-- Copyright : (C) 2011-2015 Edward Kmett-- License : BSD-style (see the file LICENSE)---- Maintainer : libraries@haskell.org-- Stability : provisional-- Portability : portable---- In mathematics, a semigroup is an algebraic structure consisting of a-- set together with an associative binary operation. A semigroup-- generalizes a monoid in that there might not exist an identity-- element. It also (originally) generalized a group (a monoid with all-- inverses) to a type where every element did not have to have an inverse,-- thus the name semigroup.---- The use of @(\<\>)@ in this module conflicts with an operator with the same-- name that is being exported by Data.Monoid. However, this package-- re-exports (most of) the contents of Data.Monoid, so to use semigroups-- and monoids in the same package just---- > import Data.Semigroup---- @since 4.9.0.0----------------------------------------------------------------------------moduleData.Semigroup(Semigroup(..),stimesMonoid,stimesIdempotent,stimesIdempotentMonoid,mtimesDefault-- * Semigroups,Min(..),Max(..),First(..),Last(..),WrappedMonoid(..)-- * Re-exported monoids from Data.Monoid,Dual(..),Endo(..),All(..),Any(..),Sum(..),Product(..)-- * A better monoid for Maybe,Option(..),option-- * Difference lists of a semigroup,diff,cycle1-- * ArgMin, ArgMax,Arg(..),ArgMin,ArgMax)whereimportPreludehiding(foldr1)importGHC.Base(Semigroup(..))importData.Semigroup.InternalimportControl.ApplicativeimportControl.MonadimportControl.Monad.FiximportData.BifoldableimportData.BifunctorimportData.BitraversableimportData.CoerceimportData.DataimportData.Monoid(All(..),Any(..),Dual(..),Endo(..),Product(..),Sum(..))-- import qualified Data.Monoid as MonoidimportGHC.Generics-- | A generalization of 'Data.List.cycle' to an arbitrary 'Semigroup'.-- May fail to terminate for some values in some semigroups.cycle1::Semigroupm=>m->mcycle1xs=xs'wherexs'=xs<>xs'-- | This lets you use a difference list of a 'Semigroup' as a 'Monoid'.diff::Semigroupm=>m->Endomdiff=Endo.(<>)newtypeMina=Min{getMin::a}deriving(Bounded-- ^ @since 4.9.0.0,Eq-- ^ @since 4.9.0.0,Ord-- ^ @since 4.9.0.0,Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceEnuma=>Enum(Mina)wheresucc(Mina)=Min(succa)pred(Mina)=Min(preda)toEnum=Min.toEnumfromEnum=fromEnum.getMinenumFrom(Mina)=Min<$>enumFromaenumFromThen(Mina)(Minb)=Min<$>enumFromThenabenumFromTo(Mina)(Minb)=Min<$>enumFromToabenumFromThenTo(Mina)(Minb)(Minc)=Min<$>enumFromThenToabc-- | @since 4.9.0.0instanceOrda=>Semigroup(Mina)where(<>)=coerce(min::a->a->a)stimes=stimesIdempotent-- | @since 4.9.0.0instance(Orda,Boundeda)=>Monoid(Mina)wheremempty=maxBound-- | @since 4.9.0.0instanceFunctorMinwherefmapf(Minx)=Min(fx)-- | @since 4.9.0.0instanceFoldableMinwherefoldMapf(Mina)=fa-- | @since 4.9.0.0instanceTraversableMinwheretraversef(Mina)=Min<$>fa-- | @since 4.9.0.0instanceApplicativeMinwherepure=Mina<*_=a_*>a=a(<*>)=coerceliftA2=coerce-- | @since 4.9.0.0instanceMonadMinwhere(>>)=(*>)Mina>>=f=fa-- | @since 4.9.0.0instanceMonadFixMinwheremfixf=fix(f.getMin)-- | @since 4.9.0.0instanceNuma=>Num(Mina)where(Mina)+(Minb)=Min(a+b)(Mina)*(Minb)=Min(a*b)(Mina)-(Minb)=Min(a-b)negate(Mina)=Min(negatea)abs(Mina)=Min(absa)signum(Mina)=Min(signuma)fromInteger=Min.fromIntegernewtypeMaxa=Max{getMax::a}deriving(Bounded-- ^ @since 4.9.0.0,Eq-- ^ @since 4.9.0.0,Ord-- ^ @since 4.9.0.0,Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceEnuma=>Enum(Maxa)wheresucc(Maxa)=Max(succa)pred(Maxa)=Max(preda)toEnum=Max.toEnumfromEnum=fromEnum.getMaxenumFrom(Maxa)=Max<$>enumFromaenumFromThen(Maxa)(Maxb)=Max<$>enumFromThenabenumFromTo(Maxa)(Maxb)=Max<$>enumFromToabenumFromThenTo(Maxa)(Maxb)(Maxc)=Max<$>enumFromThenToabc-- | @since 4.9.0.0instanceOrda=>Semigroup(Maxa)where(<>)=coerce(max::a->a->a)stimes=stimesIdempotent-- | @since 4.9.0.0instance(Orda,Boundeda)=>Monoid(Maxa)wheremempty=minBound-- | @since 4.9.0.0instanceFunctorMaxwherefmapf(Maxx)=Max(fx)-- | @since 4.9.0.0instanceFoldableMaxwherefoldMapf(Maxa)=fa-- | @since 4.9.0.0instanceTraversableMaxwheretraversef(Maxa)=Max<$>fa-- | @since 4.9.0.0instanceApplicativeMaxwherepure=Maxa<*_=a_*>a=a(<*>)=coerceliftA2=coerce-- | @since 4.9.0.0instanceMonadMaxwhere(>>)=(*>)Maxa>>=f=fa-- | @since 4.9.0.0instanceMonadFixMaxwheremfixf=fix(f.getMax)-- | @since 4.9.0.0instanceNuma=>Num(Maxa)where(Maxa)+(Maxb)=Max(a+b)(Maxa)*(Maxb)=Max(a*b)(Maxa)-(Maxb)=Max(a-b)negate(Maxa)=Max(negatea)abs(Maxa)=Max(absa)signum(Maxa)=Max(signuma)fromInteger=Max.fromInteger-- | 'Arg' isn't itself a 'Semigroup' in its own right, but it can be-- placed inside 'Min' and 'Max' to compute an arg min or arg max.dataArgab=Argabderiving(Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)typeArgMinab=Min(Argab)typeArgMaxab=Max(Argab)-- | @since 4.9.0.0instanceFunctor(Arga)wherefmapf(Argxa)=Argx(fa)-- | @since 4.9.0.0instanceFoldable(Arga)wherefoldMapf(Arg_a)=fa-- | @since 4.9.0.0instanceTraversable(Arga)wheretraversef(Argxa)=Argx<$>fa-- | @since 4.9.0.0instanceEqa=>Eq(Argab)whereArga_==Argb_=a==b-- | @since 4.9.0.0instanceOrda=>Ord(Argab)whereArga_`compare`Argb_=compareabminx@(Arga_)y@(Argb_)|a<=b=x|otherwise=ymaxx@(Arga_)y@(Argb_)|a>=b=x|otherwise=y-- | @since 4.9.0.0instanceBifunctorArgwherebimapfg(Argab)=Arg(fa)(gb)-- | @since 4.10.0.0instanceBifoldableArgwherebifoldMapfg(Argab)=fa<>gb-- | @since 4.10.0.0instanceBitraversableArgwherebitraversefg(Argab)=Arg<$>fa<*>gb-- | Use @'Option' ('First' a)@ to get the behavior of-- 'Data.Monoid.First' from "Data.Monoid".newtypeFirsta=First{getFirst::a}deriving(Bounded-- ^ @since 4.9.0.0,Eq-- ^ @since 4.9.0.0,Ord-- ^ @since 4.9.0.0,Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceEnuma=>Enum(Firsta)wheresucc(Firsta)=First(succa)pred(Firsta)=First(preda)toEnum=First.toEnumfromEnum=fromEnum.getFirstenumFrom(Firsta)=First<$>enumFromaenumFromThen(Firsta)(Firstb)=First<$>enumFromThenabenumFromTo(Firsta)(Firstb)=First<$>enumFromToabenumFromThenTo(Firsta)(Firstb)(Firstc)=First<$>enumFromThenToabc-- | @since 4.9.0.0instanceSemigroup(Firsta)wherea<>_=astimes=stimesIdempotent-- | @since 4.9.0.0instanceFunctorFirstwherefmapf(Firstx)=First(fx)-- | @since 4.9.0.0instanceFoldableFirstwherefoldMapf(Firsta)=fa-- | @since 4.9.0.0instanceTraversableFirstwheretraversef(Firsta)=First<$>fa-- | @since 4.9.0.0instanceApplicativeFirstwherepurex=Firstxa<*_=a_*>a=a(<*>)=coerceliftA2=coerce-- | @since 4.9.0.0instanceMonadFirstwhere(>>)=(*>)Firsta>>=f=fa-- | @since 4.9.0.0instanceMonadFixFirstwheremfixf=fix(f.getFirst)-- | Use @'Option' ('Last' a)@ to get the behavior of-- 'Data.Monoid.Last' from "Data.Monoid"newtypeLasta=Last{getLast::a}deriving(Bounded-- ^ @since 4.9.0.0,Eq-- ^ @since 4.9.0.0,Ord-- ^ @since 4.9.0.0,Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceEnuma=>Enum(Lasta)wheresucc(Lasta)=Last(succa)pred(Lasta)=Last(preda)toEnum=Last.toEnumfromEnum=fromEnum.getLastenumFrom(Lasta)=Last<$>enumFromaenumFromThen(Lasta)(Lastb)=Last<$>enumFromThenabenumFromTo(Lasta)(Lastb)=Last<$>enumFromToabenumFromThenTo(Lasta)(Lastb)(Lastc)=Last<$>enumFromThenToabc-- | @since 4.9.0.0instanceSemigroup(Lasta)where_<>b=bstimes=stimesIdempotent-- | @since 4.9.0.0instanceFunctorLastwherefmapf(Lastx)=Last(fx)a<$_=Lasta-- | @since 4.9.0.0instanceFoldableLastwherefoldMapf(Lasta)=fa-- | @since 4.9.0.0instanceTraversableLastwheretraversef(Lasta)=Last<$>fa-- | @since 4.9.0.0instanceApplicativeLastwherepure=Lasta<*_=a_*>a=a(<*>)=coerceliftA2=coerce-- | @since 4.9.0.0instanceMonadLastwhere(>>)=(*>)Lasta>>=f=fa-- | @since 4.9.0.0instanceMonadFixLastwheremfixf=fix(f.getLast)-- | Provide a Semigroup for an arbitrary Monoid.---- __NOTE__: This is not needed anymore since 'Semigroup' became a superclass of-- 'Monoid' in /base-4.11/ and this newtype be deprecated at some point in the future.newtypeWrappedMonoidm=WrapMonoid{unwrapMonoid::m}deriving(Bounded-- ^ @since 4.9.0.0,Eq-- ^ @since 4.9.0.0,Ord-- ^ @since 4.9.0.0,Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceMonoidm=>Semigroup(WrappedMonoidm)where(<>)=coerce(mappend::m->m->m)-- | @since 4.9.0.0instanceMonoidm=>Monoid(WrappedMonoidm)wheremempty=WrapMonoidmempty-- | @since 4.9.0.0instanceEnuma=>Enum(WrappedMonoida)wheresucc(WrapMonoida)=WrapMonoid(succa)pred(WrapMonoida)=WrapMonoid(preda)toEnum=WrapMonoid.toEnumfromEnum=fromEnum.unwrapMonoidenumFrom(WrapMonoida)=WrapMonoid<$>enumFromaenumFromThen(WrapMonoida)(WrapMonoidb)=WrapMonoid<$>enumFromThenabenumFromTo(WrapMonoida)(WrapMonoidb)=WrapMonoid<$>enumFromToabenumFromThenTo(WrapMonoida)(WrapMonoidb)(WrapMonoidc)=WrapMonoid<$>enumFromThenToabc-- | Repeat a value @n@ times.---- > mtimesDefault n a = a <> a <> ... <> a -- using <> (n-1) times---- Implemented using 'stimes' and 'mempty'.---- This is a suitable definition for an 'mtimes' member of 'Monoid'.mtimesDefault::(Integralb,Monoida)=>b->a->amtimesDefaultnx|n==0=mempty|otherwise=unwrapMonoid(stimesn(WrapMonoidx))-- | 'Option' is effectively 'Maybe' with a better instance of-- 'Monoid', built off of an underlying 'Semigroup' instead of an-- underlying 'Monoid'.---- Ideally, this type would not exist at all and we would just fix the-- 'Monoid' instance of 'Maybe'.---- In GHC 8.4 and higher, the 'Monoid' instance for 'Maybe' has been-- corrected to lift a 'Semigroup' instance instead of a 'Monoid'-- instance. Consequently, this type is no longer useful. It will be-- marked deprecated in GHC 8.8 and removed in GHC 8.10.newtypeOptiona=Option{getOption::Maybea}deriving(Eq-- ^ @since 4.9.0.0,Ord-- ^ @since 4.9.0.0,Show-- ^ @since 4.9.0.0,Read-- ^ @since 4.9.0.0,Data-- ^ @since 4.9.0.0,Generic-- ^ @since 4.9.0.0,Generic1-- ^ @since 4.9.0.0)-- | @since 4.9.0.0instanceFunctorOptionwherefmapf(Optiona)=Option(fmapfa)-- | @since 4.9.0.0instanceApplicativeOptionwherepurea=Option(Justa)Optiona<*>Optionb=Option(a<*>b)liftA2f(Optionx)(Optiony)=Option(liftA2fxy)OptionNothing*>_=OptionNothing_*>b=b-- | @since 4.9.0.0instanceMonadOptionwhereOption(Justa)>>=k=ka_>>=_=OptionNothing(>>)=(*>)-- | @since 4.9.0.0instanceAlternativeOptionwhereempty=OptionNothingOptionNothing<|>b=ba<|>_=a-- | @since 4.9.0.0instanceMonadPlusOption-- | @since 4.9.0.0instanceMonadFixOptionwheremfixf=Option(mfix(getOption.f))-- | @since 4.9.0.0instanceFoldableOptionwherefoldMapf(Option(Justm))=fmfoldMap_(OptionNothing)=mempty-- | @since 4.9.0.0instanceTraversableOptionwheretraversef(Option(Justa))=Option.Just<$>fatraverse_(OptionNothing)=pure(OptionNothing)-- | Fold an 'Option' case-wise, just like 'maybe'.option::b->(a->b)->Optiona->boptionnj(Optionm)=maybenjm-- | @since 4.9.0.0instanceSemigroupa=>Semigroup(Optiona)where(<>)=coerce((<>)::Maybea->Maybea->Maybea)#if !defined(__HADDOCK_VERSION__)-- workaround https://github.com/haskell/haddock/issues/680stimes_(OptionNothing)=OptionNothingstimesn(Option(Justa))=casecomparen0ofLT->errorWithoutStackTrace"stimes: Option, negative multiplier"EQ->OptionNothingGT->Option(Just(stimesna))#endif-- | @since 4.9.0.0instanceSemigroupa=>Monoid(Optiona)wheremempty=OptionNothing
[8]ページ先頭