Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE NoImplicitPrelude #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE PolyKinds #-}{-# LANGUAGE ScopedTypeVariables #-}-- | Auxilary definitions for 'Semigroup'---- This module provides some @newtype@ wrappers and helpers which are-- reexported from the "Data.Semigroup" module or imported directly-- by some other modules.---- This module also provides internal definitions related to the-- 'Semigroup' class some.---- This module exists mostly to simplify or workaround import-graph-- issues; there is also a .hs-boot file to allow "GHC.Base" and other-- modules to import method default implementations for 'stimes'---- @since 4.11.0.0moduleData.Semigroup.InternalwhereimportGHC.Basehiding(Any)importGHC.EnumimportGHC.NumimportGHC.ReadimportGHC.ShowimportGHC.GenericsimportGHC.Real-- | This is a valid definition of 'stimes' for an idempotent 'Semigroup'.---- When @x <> x = x@, this definition should be preferred, because it-- works in /O(1)/ rather than /O(log n)/.stimesIdempotent::Integralb=>b->a->astimesIdempotentnx|n<=0=errorWithoutStackTrace"stimesIdempotent: positive multiplier expected"|otherwise=x-- | This is a valid definition of 'stimes' for an idempotent 'Monoid'.---- When @mappend x x = x@, this definition should be preferred, because it-- works in /O(1)/ rather than /O(log n)/stimesIdempotentMonoid::(Integralb,Monoida)=>b->a->astimesIdempotentMonoidnx=casecomparen0ofLT->errorWithoutStackTrace"stimesIdempotentMonoid: negative multiplier"EQ->memptyGT->x-- | This is a valid definition of 'stimes' for a 'Monoid'.---- Unlike the default definition of 'stimes', it is defined for 0-- and so it should be preferred where possible.stimesMonoid::(Integralb,Monoida)=>b->a->astimesMonoidnx0=casecomparen0ofLT->errorWithoutStackTrace"stimesMonoid: negative multiplier"EQ->memptyGT->fx0nwherefxy|eveny=f(x`mappend`x)(y`quot`2)|y==1=x|otherwise=g(x`mappend`x)(y`quot`2)x-- See Note [Half of y - 1]gxyz|eveny=g(x`mappend`x)(y`quot`2)z|y==1=x`mappend`z|otherwise=g(x`mappend`x)(y`quot`2)(x`mappend`z)-- See Note [Half of y - 1]-- this is used by the class definitionin GHC.Base;-- it lives here to avoid cyclesstimesDefault::(Integralb,Semigroupa)=>b->a->astimesDefaulty0x0|y0<=0=errorWithoutStackTrace"stimes: positive multiplier expected"|otherwise=fx0y0wherefxy|eveny=f(x<>x)(y`quot`2)|y==1=x|otherwise=g(x<>x)(y`quot`2)x-- See Note [Half of y - 1]gxyz|eveny=g(x<>x)(y`quot`2)z|y==1=x<>z|otherwise=g(x<>x)(y`quot`2)(x<>z)-- See Note [Half of y - 1]{- Note [Half of y - 1]   ~~~~~~~~~~~~~~~~~~~~~   Since y is guaranteed to be odd and positive here,   half of y - 1 can be computed as y `quot` 2, optimising subtraction away.-}stimesMaybe::(Integralb,Semigroupa)=>b->Maybea->MaybeastimesMaybe_Nothing=NothingstimesMayben(Justa)=casecomparen0ofLT->errorWithoutStackTrace"stimes: Maybe, negative multiplier"EQ->NothingGT->Just(stimesna)stimesList::Integralb=>b->[a]->[a]stimesListnx|n<0=errorWithoutStackTrace"stimes: [], negative multiplier"|otherwise=repnwhererep0=[]repi=x++rep(i-1)-- | The dual of a 'Monoid', obtained by swapping the arguments of 'mappend'.---- >>> getDual (mappend (Dual "Hello") (Dual "World"))-- "WorldHello"newtypeDuala=Dual{getDual::a}deriving(Eq-- ^ @since 2.01,Ord-- ^ @since 2.01,Read-- ^ @since 2.01,Show-- ^ @since 2.01,Bounded-- ^ @since 2.01,Generic-- ^ @since 4.7.0.0,Generic1-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceSemigroupa=>Semigroup(Duala)whereDuala<>Dualb=Dual(b<>a)stimesn(Duala)=Dual(stimesna)-- | @since 2.01instanceMonoida=>Monoid(Duala)wheremempty=Dualmempty-- | @since 4.8.0.0instanceFunctorDualwherefmap=coerce-- | @since 4.8.0.0instanceApplicativeDualwherepure=Dual(<*>)=coerce-- | @since 4.8.0.0instanceMonadDualwherem>>=k=k(getDualm)-- | The monoid of endomorphisms under composition.---- >>> let computation = Endo ("Hello, " ++) <> Endo (++ "!")-- >>> appEndo computation "Haskell"-- "Hello, Haskell!"newtypeEndoa=Endo{appEndo::a->a}deriving(Generic-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceSemigroup(Endoa)where(<>)=coerce((.)::(a->a)->(a->a)->(a->a))stimes=stimesMonoid-- | @since 2.01instanceMonoid(Endoa)wheremempty=Endoid-- | Boolean monoid under conjunction ('&&').---- >>> getAll (All True <> mempty <> All False)-- False---- >>> getAll (mconcat (map (\x -> All (even x)) [2,4,6,7,8]))-- FalsenewtypeAll=All{getAll::Bool}deriving(Eq-- ^ @since 2.01,Ord-- ^ @since 2.01,Read-- ^ @since 2.01,Show-- ^ @since 2.01,Bounded-- ^ @since 2.01,Generic-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceSemigroupAllwhere(<>)=coerce(&&)stimes=stimesIdempotentMonoid-- | @since 2.01instanceMonoidAllwheremempty=AllTrue-- | Boolean monoid under disjunction ('||').---- >>> getAny (Any True <> mempty <> Any False)-- True---- >>> getAny (mconcat (map (\x -> Any (even x)) [2,4,6,7,8]))-- TruenewtypeAny=Any{getAny::Bool}deriving(Eq-- ^ @since 2.01,Ord-- ^ @since 2.01,Read-- ^ @since 2.01,Show-- ^ @since 2.01,Bounded-- ^ @since 2.01,Generic-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceSemigroupAnywhere(<>)=coerce(||)stimes=stimesIdempotentMonoid-- | @since 2.01instanceMonoidAnywheremempty=AnyFalse-- | Monoid under addition.---- >>> getSum (Sum 1 <> Sum 2 <> mempty)-- 3newtypeSuma=Sum{getSum::a}deriving(Eq-- ^ @since 2.01,Ord-- ^ @since 2.01,Read-- ^ @since 2.01,Show-- ^ @since 2.01,Bounded-- ^ @since 2.01,Generic-- ^ @since 4.7.0.0,Generic1-- ^ @since 4.7.0.0,Num-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceNuma=>Semigroup(Suma)where(<>)=coerce((+)::a->a->a)stimesn(Suma)=Sum(fromIntegraln*a)-- | @since 2.01instanceNuma=>Monoid(Suma)wheremempty=Sum0-- | @since 4.8.0.0instanceFunctorSumwherefmap=coerce-- | @since 4.8.0.0instanceApplicativeSumwherepure=Sum(<*>)=coerce-- | @since 4.8.0.0instanceMonadSumwherem>>=k=k(getSumm)-- | Monoid under multiplication.---- >>> getProduct (Product 3 <> Product 4 <> mempty)-- 12newtypeProducta=Product{getProduct::a}deriving(Eq-- ^ @since 2.01,Ord-- ^ @since 2.01,Read-- ^ @since 2.01,Show-- ^ @since 2.01,Bounded-- ^ @since 2.01,Generic-- ^ @since 4.7.0.0,Generic1-- ^ @since 4.7.0.0,Num-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceNuma=>Semigroup(Producta)where(<>)=coerce((*)::a->a->a)stimesn(Producta)=Product(a^n)-- | @since 2.01instanceNuma=>Monoid(Producta)wheremempty=Product1-- | @since 4.8.0.0instanceFunctorProductwherefmap=coerce-- | @since 4.8.0.0instanceApplicativeProductwherepure=Product(<*>)=coerce-- | @since 4.8.0.0instanceMonadProductwherem>>=k=k(getProductm)-- | Monoid under '<|>'.---- @since 4.8.0.0newtypeAltfa=Alt{getAlt::fa}deriving(Generic-- ^ @since 4.8.0.0,Generic1-- ^ @since 4.8.0.0,Read-- ^ @since 4.8.0.0,Show-- ^ @since 4.8.0.0,Eq-- ^ @since 4.8.0.0,Ord-- ^ @since 4.8.0.0,Num-- ^ @since 4.8.0.0,Enum-- ^ @since 4.8.0.0,Monad-- ^ @since 4.8.0.0,MonadPlus-- ^ @since 4.8.0.0,Applicative-- ^ @since 4.8.0.0,Alternative-- ^ @since 4.8.0.0,Functor-- ^ @since 4.8.0.0)-- | @since 4.9.0.0instanceAlternativef=>Semigroup(Altfa)where(<>)=coerce((<|>)::fa->fa->fa)stimes=stimesMonoid-- | @since 4.8.0.0instanceAlternativef=>Monoid(Altfa)wheremempty=Altempty

[8]ページ先頭

©2009-2025 Movatter.jp