Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- universeDef :: (Bounded a, Enum a) => [a]
- interleave :: [[a]] -> [a]
- diagonal :: [[a]] -> [a]
- diagonals :: [[a]] -> [[a]]
- (+++) :: [a] -> [a] -> [a]
- cartesianProduct :: (a -> b -> c) -> [a] -> [b] -> [c]
- (+*+) :: [a] -> [b] -> [(a, b)]
- (<+*+>) :: [a -> b] -> [a] -> [b]
- choices :: [[a]] -> [[a]]
- retagWith :: (a -> b) -> Tagged a x -> Tagged b x
- retag :: forall k1 k2 (s :: k1) b (t :: k2). Tagged s b -> Tagged t b
- newtype Tagged (s :: k) b = Tagged {
- unTagged :: b
- data Natural
- unfairCartesianProduct :: (a -> b -> c) -> [a] -> [b] -> [c]
- unfairChoices :: [[a]] -> [[a]]
Documentation
This module is for functions that are useful for writing instances, but not necessarily for using them (and hence are not exported by the main module to avoid cluttering up the namespace).
Building lists
universeDef :: (Bounded a, Enum a) => [a] Source #
For many types, the universe
should be [minBound .. maxBound]
;
universeDef
makes it easy to make such types an instance of Universe
via
the snippet
instance Universe Foo where universe = universeDef
interleave :: [[a]] -> [a] Source #
Fair n-way interleaving: given a finite number of (possibly infinite)
lists, produce a single list such that whenever v
has finite index in one
of the input lists, v
also has finite index in the output list. No list's
elements occur more frequently (on average) than another's.
diagonal :: [[a]] -> [a] Source #
Unfair n-way interleaving: given a possibly infinite number of (possibly
infinite) lists, produce a single list such that whenever v
has finite
index in an input list at finite index, v
also has finite index in the
output list. Elements from lists at lower index occur more frequently, but
not exponentially so.
diagonals :: [[a]] -> [[a]] Source #
Like diagonal
, but expose a tiny bit more (non-semantic) information:
if you lay out the input list in two dimensions, each list in the result
will be one of the diagonals of the input. In particular, each element of
the output will be a list whose elements are each from a distinct input
list.
cartesianProduct :: (a -> b -> c) -> [a] -> [b] -> [c] Source #
Slightly unfair 2-way Cartesian product: given two (possibly infinite)
lists, produce a single list such that whenever v
and w
have finite
indices in the input lists, (v,w)
has finite index in the output list.
Lower indices occur as the fst
part of the tuple more frequently, but not
exponentially so.
(+*+) :: [a] -> [b] -> [(a, b)] Source #
cartesianProduct
(,)
(<+*+>) :: [a -> b] -> [a] -> [b] Source #
A +*+
with application.
cartesianProduct
($)
choices :: [[a]] -> [[a]] Source #
Slightly unfair n-way Cartesian product: given a finite number of
(possibly infinite) lists, produce a single list such that whenever vi
has
finite index in list i for each i, [v1, ..., vn]
has finite index in the
output list.
Building cardinalities
These functions are handy for inheriting the definition of
cardinality
in a newtype instance. For example,
one might write
newtype Foo = Foo Bar instance Finite Foo where cardinality = retagWith Foo cardinality
A
value is a value Tagged
s bb
with an attached phantom type s
.
This can be used in place of the more traditional but less safe idiom of
passing in an undefined value with the type, because unlike an (s -> b)
,
a
can't try to use the argument Tagged
s bs
as a real value.
Moreover, you don't have to rely on the compiler to inline away the extra argument, because the newtype is "free"
Tagged
has kind k -> * -> *
if the compiler supports PolyKinds
, therefore
there is an extra k
showing in the instance haddocks that may cause confusion.
Instances
Bitraversable (Tagged :: Type -> Type -> Type) | |
Defined in Data.Tagged bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Tagged a b -> f (Tagged c d) # | |
Bifoldable (Tagged :: Type -> Type -> Type) | |
Bifunctor (Tagged :: Type -> Type -> Type) | |
Eq2 (Tagged :: Type -> Type -> Type) | |
Ord2 (Tagged :: Type -> Type -> Type) | |
Defined in Data.Tagged | |
Read2 (Tagged :: Type -> Type -> Type) | |
Defined in Data.Tagged liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Tagged a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Tagged a b] # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Tagged a b) # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Tagged a b] # | |
Show2 (Tagged :: Type -> Type -> Type) | |
Generic1 (Tagged s :: Type -> Type) | |
Monad (Tagged s) | |
Functor (Tagged s) | |
Applicative (Tagged s) | |
Foldable (Tagged s) | |
Defined in Data.Tagged fold :: Monoid m => Tagged s m -> m # foldMap :: Monoid m => (a -> m) -> Tagged s a -> m # foldMap' :: Monoid m => (a -> m) -> Tagged s a -> m # foldr :: (a -> b -> b) -> b -> Tagged s a -> b # foldr' :: (a -> b -> b) -> b -> Tagged s a -> b # foldl :: (b -> a -> b) -> b -> Tagged s a -> b # foldl' :: (b -> a -> b) -> b -> Tagged s a -> b # foldr1 :: (a -> a -> a) -> Tagged s a -> a # foldl1 :: (a -> a -> a) -> Tagged s a -> a # elem :: Eq a => a -> Tagged s a -> Bool # maximum :: Ord a => Tagged s a -> a # minimum :: Ord a => Tagged s a -> a # | |
Traversable (Tagged s) | |
Eq1 (Tagged s) | |
Ord1 (Tagged s) | |
Defined in Data.Tagged | |
Read1 (Tagged s) | |
Defined in Data.Tagged | |
Show1 (Tagged s) | |
Bounded b => Bounded (Tagged s b) | |
Enum a => Enum (Tagged s a) | |
Defined in Data.Tagged succ :: Tagged s a -> Tagged s a # pred :: Tagged s a -> Tagged s a # fromEnum :: Tagged s a -> Int # enumFrom :: Tagged s a -> [Tagged s a] # enumFromThen :: Tagged s a -> Tagged s a -> [Tagged s a] # enumFromTo :: Tagged s a -> Tagged s a -> [Tagged s a] # enumFromThenTo :: Tagged s a -> Tagged s a -> Tagged s a -> [Tagged s a] # | |
Eq b => Eq (Tagged s b) | |
Floating a => Floating (Tagged s a) | |
Defined in Data.Tagged exp :: Tagged s a -> Tagged s a # log :: Tagged s a -> Tagged s a # sqrt :: Tagged s a -> Tagged s a # (**) :: Tagged s a -> Tagged s a -> Tagged s a # logBase :: Tagged s a -> Tagged s a -> Tagged s a # sin :: Tagged s a -> Tagged s a # cos :: Tagged s a -> Tagged s a # tan :: Tagged s a -> Tagged s a # asin :: Tagged s a -> Tagged s a # acos :: Tagged s a -> Tagged s a # atan :: Tagged s a -> Tagged s a # sinh :: Tagged s a -> Tagged s a # cosh :: Tagged s a -> Tagged s a # tanh :: Tagged s a -> Tagged s a # asinh :: Tagged s a -> Tagged s a # acosh :: Tagged s a -> Tagged s a # atanh :: Tagged s a -> Tagged s a # log1p :: Tagged s a -> Tagged s a # expm1 :: Tagged s a -> Tagged s a # | |
Fractional a => Fractional (Tagged s a) | |
Integral a => Integral (Tagged s a) | |
Defined in Data.Tagged quot :: Tagged s a -> Tagged s a -> Tagged s a # rem :: Tagged s a -> Tagged s a -> Tagged s a # div :: Tagged s a -> Tagged s a -> Tagged s a # mod :: Tagged s a -> Tagged s a -> Tagged s a # quotRem :: Tagged s a -> Tagged s a -> (Tagged s a, Tagged s a) # divMod :: Tagged s a -> Tagged s a -> (Tagged s a, Tagged s a) # | |
(Data s, Data b) => Data (Tagged s b) | |
Defined in Data.Tagged gfoldl :: (forall d b0. Data d => c (d -> b0) -> d -> c b0) -> (forall g. g -> c g) -> Tagged s b -> c (Tagged s b) # gunfold :: (forall b0 r. Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Tagged s b) # toConstr :: Tagged s b -> Constr # dataTypeOf :: Tagged s b -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Tagged s b)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tagged s b)) # gmapT :: (forall b0. Data b0 => b0 -> b0) -> Tagged s b -> Tagged s b # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tagged s b -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tagged s b -> r # gmapQ :: (forall d. Data d => d -> u) -> Tagged s b -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Tagged s b -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Tagged s b -> m (Tagged s b) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Tagged s b -> m (Tagged s b) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Tagged s b -> m (Tagged s b) # | |
Num a => Num (Tagged s a) | |
Defined in Data.Tagged | |
Ord b => Ord (Tagged s b) | |
Read b => Read (Tagged s b) | |
Real a => Real (Tagged s a) | |
Defined in Data.Tagged toRational :: Tagged s a -> Rational # | |
RealFloat a => RealFloat (Tagged s a) | |
Defined in Data.Tagged floatRadix :: Tagged s a -> Integer # floatDigits :: Tagged s a -> Int # floatRange :: Tagged s a -> (Int, Int) # decodeFloat :: Tagged s a -> (Integer, Int) # encodeFloat :: Integer -> Int -> Tagged s a # exponent :: Tagged s a -> Int # significand :: Tagged s a -> Tagged s a # scaleFloat :: Int -> Tagged s a -> Tagged s a # isInfinite :: Tagged s a -> Bool # isDenormalized :: Tagged s a -> Bool # isNegativeZero :: Tagged s a -> Bool # | |
RealFrac a => RealFrac (Tagged s a) | |
Show b => Show (Tagged s b) | |
Ix b => Ix (Tagged s b) | |
Defined in Data.Tagged range :: (Tagged s b, Tagged s b) -> [Tagged s b] # index :: (Tagged s b, Tagged s b) -> Tagged s b -> Int # unsafeIndex :: (Tagged s b, Tagged s b) -> Tagged s b -> Int # inRange :: (Tagged s b, Tagged s b) -> Tagged s b -> Bool # rangeSize :: (Tagged s b, Tagged s b) -> Int # unsafeRangeSize :: (Tagged s b, Tagged s b) -> Int # | |
IsString a => IsString (Tagged s a) | |
Defined in Data.Tagged fromString :: String -> Tagged s a # | |
Generic (Tagged s b) | |
Semigroup a => Semigroup (Tagged s a) | |
(Semigroup a, Monoid a) => Monoid (Tagged s a) | |
Storable a => Storable (Tagged s a) | |
Defined in Data.Tagged | |
Bits a => Bits (Tagged s a) | |
Defined in Data.Tagged (.&.) :: Tagged s a -> Tagged s a -> Tagged s a # (.|.) :: Tagged s a -> Tagged s a -> Tagged s a # xor :: Tagged s a -> Tagged s a -> Tagged s a # complement :: Tagged s a -> Tagged s a # shift :: Tagged s a -> Int -> Tagged s a # rotate :: Tagged s a -> Int -> Tagged s a # setBit :: Tagged s a -> Int -> Tagged s a # clearBit :: Tagged s a -> Int -> Tagged s a # complementBit :: Tagged s a -> Int -> Tagged s a # testBit :: Tagged s a -> Int -> Bool # bitSizeMaybe :: Tagged s a -> Maybe Int # bitSize :: Tagged s a -> Int # isSigned :: Tagged s a -> Bool # shiftL :: Tagged s a -> Int -> Tagged s a # unsafeShiftL :: Tagged s a -> Int -> Tagged s a # shiftR :: Tagged s a -> Int -> Tagged s a # unsafeShiftR :: Tagged s a -> Int -> Tagged s a # rotateL :: Tagged s a -> Int -> Tagged s a # | |
FiniteBits a => FiniteBits (Tagged s a) | |
Defined in Data.Tagged finiteBitSize :: Tagged s a -> Int # countLeadingZeros :: Tagged s a -> Int # countTrailingZeros :: Tagged s a -> Int # | |
NFData b => NFData (Tagged s b) | |
Defined in Data.Tagged | |
Finite a => Finite (Tagged b a) Source # | |
Universe a => Universe (Tagged b a) Source # | |
Defined in Data.Universe.Class | |
type Rep1 (Tagged s :: Type -> Type) | |
Defined in Data.Tagged | |
type Rep (Tagged s b) | |
Defined in Data.Tagged |
Type representing arbitrary-precision non-negative integers.
>>>
2^100 :: Natural
1267650600228229401496703205376
Operations whose result would be negative
,throw
(Underflow
:: ArithException
)
>>>
-1 :: Natural
*** Exception: arithmetic underflow
Since: base-4.8.0.0
Instances
Enum Natural | Since: base-4.8.0.0 |
Eq Natural | Since: base-4.8.0.0 |
Integral Natural | Since: base-4.8.0.0 |
Defined in GHC.Real | |
Num Natural | Note that Since: base-4.8.0.0 |
Ord Natural | Since: base-4.8.0.0 |
Read Natural | Since: base-4.8.0.0 |
Real Natural | Since: base-4.8.0.0 |
Defined in GHC.Real toRational :: Natural -> Rational # | |
Show Natural | Since: base-4.8.0.0 |
Universe Natural Source # | |
Defined in Data.Universe.Class |
Debugging
These functions exist primarily as a specification to test against.
unfairCartesianProduct :: (a -> b -> c) -> [a] -> [b] -> [c] Source #
Very unfair 2-way Cartesian product: same guarantee as the slightly unfair
one, except that lower indices may occur as the fst
part of the tuple
exponentially more frequently.
unfairChoices :: [[a]] -> [[a]] Source #
Very unfair n-way Cartesian product: same guarantee as the slightly unfair one, but not as good in the same sense that the very unfair 2-way product is worse than the slightly unfair 2-way product.