module Data.Monoid.HT (cycle, (<>), when, power) where
import Data.Monoid (Monoid, mappend, mempty, )
import Data.Function (fix, )
import Prelude (Integer, Bool, Ordering(..), compare, divMod, error)
cycle :: Monoid m => m -> m
cycle :: forall m. Monoid m => m -> m
cycle m
x =
forall a. (a -> a) -> a
fix (forall a. Monoid a => a -> a -> a
mappend m
x)
infixr 6 <>
(<>) :: Monoid m => m -> m -> m
<> :: forall a. Monoid a => a -> a -> a
(<>) = forall a. Monoid a => a -> a -> a
mappend
when :: Monoid m => Bool -> m -> m
when :: forall m. Monoid m => Bool -> m -> m
when Bool
b m
m = if Bool
b then m
m else forall a. Monoid a => a
mempty
power :: Monoid m => Integer -> m -> m
power :: forall m. Monoid m => Integer -> m -> m
power Integer
k m
m =
case forall a. Ord a => a -> a -> Ordering
compare Integer
k Integer
0 of
Ordering
LT -> forall a. HasCallStack => [Char] -> a
error [Char]
"Monoid.power: negative exponent"
Ordering
EQ -> forall a. Monoid a => a
mempty
Ordering
GT ->
let (Integer
k2,Integer
r) = forall a. Integral a => a -> a -> (a, a)
divMod Integer
k Integer
2
p :: m
p = forall m. Monoid m => Integer -> m -> m
power Integer
k2 m
m
p2 :: m
p2 = m
pforall a. Monoid a => a -> a -> a
<>m
p
in case Integer
r of
Integer
0 -> m
p2
Integer
_ -> m
mforall a. Monoid a => a -> a -> a
<>m
p2