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 \(\mathcal{O}(1)\) rather than \(\mathcal{O}(\log n)\).stimesIdempotent::Integralb=>b->a->astimesIdempotent :: b -> a -> astimesIdempotentbnax|bnb -> b -> Boolforall a. Ord a => a -> a -> Bool<=b0=[Char] -> aforall a. [Char] -> aerrorWithoutStackTrace[Char]"stimesIdempotent: positive multiplier expected"|Boolotherwise=ax-- | 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 \(\mathcal{O}(1)\) rather than \(\mathcal{O}(\log n)\)stimesIdempotentMonoid::(Integralb,Monoida)=>b->a->astimesIdempotentMonoid :: b -> a -> astimesIdempotentMonoidbnax=caseb -> b -> Orderingforall a. Ord a => a -> a -> Orderingcomparebnb0ofOrderingLT->[Char] -> aforall a. [Char] -> aerrorWithoutStackTrace[Char]"stimesIdempotentMonoid: negative multiplier"OrderingEQ->aforall a. Monoid a => amemptyOrderingGT->ax-- | 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->astimesMonoid :: b -> a -> astimesMonoidbnax0=caseb -> b -> Orderingforall a. Ord a => a -> a -> Orderingcomparebnb0ofOrderingLT->[Char] -> aforall a. [Char] -> aerrorWithoutStackTrace[Char]"stimesMonoid: negative multiplier"OrderingEQ->aforall a. Monoid a => amemptyOrderingGT->a -> b -> aforall a a. (Integral a, Monoid a) => a -> a -> afax0bnwheref :: a -> a -> afaxay|a -> Boolforall a. Integral a => a -> Boolevenay=a -> a -> af(axa -> a -> aforall a. Monoid a => a -> a -> a`mappend`ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)|aya -> a -> Boolforall a. Eq a => a -> a -> Bool==a1=ax|Boolotherwise=a -> a -> a -> aforall a a. (Integral a, Monoid a) => a -> a -> a -> ag(axa -> a -> aforall a. Monoid a => a -> a -> a`mappend`ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)ax-- See Note [Half of y - 1]g :: a -> a -> a -> agaxayaz|a -> Boolforall a. Integral a => a -> Boolevenay=a -> a -> a -> ag(axa -> a -> aforall a. Monoid a => a -> a -> a`mappend`ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)az|aya -> a -> Boolforall a. Eq a => a -> a -> Bool==a1=axa -> a -> aforall a. Monoid a => a -> a -> a`mappend`az|Boolotherwise=a -> a -> a -> ag(axa -> a -> aforall a. Monoid a => a -> a -> a`mappend`ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)(axa -> a -> aforall a. Monoid a => a -> a -> a`mappend`az)-- 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->astimesDefault :: b -> a -> astimesDefaultby0ax0|by0b -> b -> Boolforall a. Ord a => a -> a -> Bool<=b0=[Char] -> aforall a. [Char] -> aerrorWithoutStackTrace[Char]"stimes: positive multiplier expected"|Boolotherwise=a -> b -> aforall a a. (Integral a, Semigroup a) => a -> a -> afax0by0wheref :: a -> a -> afaxay|a -> Boolforall a. Integral a => a -> Boolevenay=a -> a -> af(axa -> a -> aforall a. Semigroup a => a -> a -> a<>ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)|aya -> a -> Boolforall a. Eq a => a -> a -> Bool==a1=ax|Boolotherwise=a -> a -> a -> aforall a a. (Integral a, Semigroup a) => a -> a -> a -> ag(axa -> a -> aforall a. Semigroup a => a -> a -> a<>ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)ax-- See Note [Half of y - 1]g :: a -> a -> a -> agaxayaz|a -> Boolforall a. Integral a => a -> Boolevenay=a -> a -> a -> ag(axa -> a -> aforall a. Semigroup a => a -> a -> a<>ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)az|aya -> a -> Boolforall a. Eq a => a -> a -> Bool==a1=axa -> a -> aforall a. Semigroup a => a -> a -> a<>az|Boolotherwise=a -> a -> a -> ag(axa -> a -> aforall a. Semigroup a => a -> a -> a<>ax)(aya -> a -> aforall a. Integral a => a -> a -> a`quot`a2)(axa -> a -> aforall a. Semigroup a => a -> a -> a<>az)-- 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 :: b -> Maybe a -> Maybe astimesMaybeb_Maybe aNothing=Maybe aforall a. Maybe aNothingstimesMaybebn(Justaa)=caseb -> b -> Orderingforall a. Ord a => a -> a -> Orderingcomparebnb0ofOrderingLT->[Char] -> Maybe aforall a. [Char] -> aerrorWithoutStackTrace[Char]"stimes: Maybe, negative multiplier"OrderingEQ->Maybe aforall a. Maybe aNothingOrderingGT->a -> Maybe aforall a. a -> Maybe aJust(b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa)stimesList::Integralb=>b->[a]->[a]stimesList :: b -> [a] -> [a]stimesListbn[a]x|bnb -> b -> Boolforall a. Ord a => a -> a -> Bool<b0=[Char] -> [a]forall a. [Char] -> aerrorWithoutStackTrace[Char]"stimes: [], negative multiplier"|Boolotherwise=b -> [a]forall t. (Eq t, Num t) => t -> [a]repbnwhererep :: t -> [a]rept0=[]repti=[a]x[a] -> [a] -> [a]forall a. [a] -> [a] -> [a]++t -> [a]rep(tit -> t -> tforall a. Num a => a -> a -> a-t1)-- | The dual of a 'Monoid', obtained by swapping the arguments of 'mappend'.---- >>> getDual (mappend (Dual "Hello") (Dual "World"))-- "WorldHello"newtypeDuala=Dual{Dual a -> agetDual::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)whereDualaa<> :: Dual a -> Dual a -> Dual a<>Dualab=a -> Dual aforall a. a -> Dual aDual(aba -> a -> aforall a. Semigroup a => a -> a -> a<>aa)stimes :: b -> Dual a -> Dual astimesbn(Dualaa)=a -> Dual aforall a. a -> Dual aDual(b -> a -> aforall a b. (Semigroup a, Integral b) => b -> a -> astimesbnaa)-- | @since 2.01instanceMonoida=>Monoid(Duala)wheremempty :: Dual amempty=a -> Dual aforall a. a -> Dual aDualaforall a. Monoid a => amempty-- | @since 4.8.0.0instanceFunctorDualwherefmap :: (a -> b) -> Dual a -> Dual bfmap=(a -> b) -> Dual a -> Dual bcoerce-- | @since 4.8.0.0instanceApplicativeDualwherepure :: a -> Dual apure=a -> Dual aforall a. a -> Dual aDual<*> :: Dual (a -> b) -> Dual a -> Dual b(<*>)=Dual (a -> b) -> Dual a -> Dual bcoerce-- | @since 4.8.0.0instanceMonadDualwhereDual am>>= :: Dual a -> (a -> Dual b) -> Dual b>>=a -> Dual bk=a -> Dual bk(Dual a -> aforall a. Dual a -> agetDualDual am)-- | The monoid of endomorphisms under composition.---- >>> let computation = Endo ("Hello, " ++) <> Endo (++ "!")-- >>> appEndo computation "Haskell"-- "Hello, Haskell!"newtypeEndoa=Endo{Endo a -> a -> aappEndo::a->a}deriving(Generic-- ^ @since 4.7.0.0)-- | @since 4.9.0.0instanceSemigroup(Endoa)where<> :: Endo a -> Endo a -> Endo a(<>)=((a -> a) -> (a -> a) -> a -> a) -> Endo a -> Endo a -> Endo acoerce((a -> a) -> (a -> a) -> a -> aforall b c a. (b -> c) -> (a -> b) -> a -> c(.)::(a->a)->(a->a)->(a->a))stimes :: b -> Endo a -> Endo astimes=b -> Endo a -> Endo aforall b a. (Integral b, Monoid a) => b -> a -> astimesMonoid-- | @since 2.01instanceMonoid(Endoa)wheremempty :: Endo amempty=(a -> a) -> Endo aforall a. (a -> a) -> Endo aEndoa -> aforall a. a -> aid-- | 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{All -> BoolgetAll::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<> :: All -> All -> All(<>)=(Bool -> Bool -> Bool) -> All -> All -> AllcoerceBool -> Bool -> Bool(&&)stimes :: b -> All -> Allstimes=b -> All -> Allforall b a. (Integral b, Monoid a) => b -> a -> astimesIdempotentMonoid-- | @since 2.01instanceMonoidAllwheremempty :: Allmempty=Bool -> AllAllBoolTrue-- | 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{Any -> BoolgetAny::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<> :: Any -> Any -> Any(<>)=(Bool -> Bool -> Bool) -> Any -> Any -> AnycoerceBool -> Bool -> Bool(||)stimes :: b -> Any -> Anystimes=b -> Any -> Anyforall b a. (Integral b, Monoid a) => b -> a -> astimesIdempotentMonoid-- | @since 2.01instanceMonoidAnywheremempty :: Anymempty=Bool -> AnyAnyBoolFalse-- | Monoid under addition.---- >>> getSum (Sum 1 <> Sum 2 <> mempty)-- 3newtypeSuma=Sum{Sum a -> agetSum::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<> :: Sum a -> Sum a -> Sum a(<>)=(a -> a -> a) -> Sum a -> Sum a -> Sum acoerce(a -> a -> aforall a. Num a => a -> a -> a(+)::a->a->a)stimes :: b -> Sum a -> Sum astimesbn(Sumaa)=a -> Sum aforall a. a -> Sum aSum(b -> aforall a b. (Integral a, Num b) => a -> bfromIntegralbna -> a -> aforall a. Num a => a -> a -> a*aa)-- | @since 2.01instanceNuma=>Monoid(Suma)wheremempty :: Sum amempty=a -> Sum aforall a. a -> Sum aSuma0-- | @since 4.8.0.0instanceFunctorSumwherefmap :: (a -> b) -> Sum a -> Sum bfmap=(a -> b) -> Sum a -> Sum bcoerce-- | @since 4.8.0.0instanceApplicativeSumwherepure :: a -> Sum apure=a -> Sum aforall a. a -> Sum aSum<*> :: Sum (a -> b) -> Sum a -> Sum b(<*>)=Sum (a -> b) -> Sum a -> Sum bcoerce-- | @since 4.8.0.0instanceMonadSumwhereSum am>>= :: Sum a -> (a -> Sum b) -> Sum b>>=a -> Sum bk=a -> Sum bk(Sum a -> aforall a. Sum a -> agetSumSum am)-- | Monoid under multiplication.---- >>> getProduct (Product 3 <> Product 4 <> mempty)-- 12newtypeProducta=Product{Product a -> agetProduct::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<> :: Product a -> Product a -> Product a(<>)=(a -> a -> a) -> Product a -> Product a -> Product acoerce(a -> a -> aforall a. Num a => a -> a -> a(*)::a->a->a)stimes :: b -> Product a -> Product astimesbn(Productaa)=a -> Product aforall a. a -> Product aProduct(aaa -> b -> aforall a b. (Num a, Integral b) => a -> b -> a^bn)-- | @since 2.01instanceNuma=>Monoid(Producta)wheremempty :: Product amempty=a -> Product aforall a. a -> Product aProducta1-- | @since 4.8.0.0instanceFunctorProductwherefmap :: (a -> b) -> Product a -> Product bfmap=(a -> b) -> Product a -> Product bcoerce-- | @since 4.8.0.0instanceApplicativeProductwherepure :: a -> Product apure=a -> Product aforall a. a -> Product aProduct<*> :: Product (a -> b) -> Product a -> Product b(<*>)=Product (a -> b) -> Product a -> Product bcoerce-- | @since 4.8.0.0instanceMonadProductwhereProduct am>>= :: Product a -> (a -> Product b) -> Product b>>=a -> Product bk=a -> Product bk(Product a -> aforall a. Product a -> agetProductProduct am)-- | Monoid under '<|>'.---- >>> getAlt (Alt (Just 12) <> Alt (Just 24))-- Just 12---- >>> getAlt $ Alt Nothing <> Alt (Just 24)-- Just 24---- @since 4.8.0.0newtypeAltfa=Alt{Alt f a -> f agetAlt::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<> :: Alt f a -> Alt f a -> Alt f a(<>)=(f a -> f a -> f a) -> Alt f a -> Alt f a -> Alt f acoerce(f a -> f a -> f aforall (f :: * -> *) a. Alternative f => f a -> f a -> f a(<|>)::fa->fa->fa)stimes :: b -> Alt f a -> Alt f astimes=b -> Alt f a -> Alt f aforall b a. (Integral b, Monoid a) => b -> a -> astimesMonoid-- | @since 4.8.0.0instanceAlternativef=>Monoid(Altfa)wheremempty :: Alt f amempty=f a -> Alt f aforall k (f :: k -> *) (a :: k). f a -> Alt f aAltf aforall (f :: * -> *) a. Alternative f => f aempty

[8]ページ先頭

©2009-2025 Movatter.jp