Hoogle Search
Within LTS Haskell 24.68 (ghc-9.10.3)
Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.
module Data.Chimera.
WheelMapping Helpers for mapping to rough numbers and back. This has various applications in number theory. Example Let isPrime be an expensive predicate, which checks whether its argument is a prime number. We can memoize it as usual:
isPrimeCache1 :: UChimera Bool isPrimeCache1 = tabulate isPrime isPrime1 :: Word -> Bool isPrime1 = index isPrimeCache1
But one may argue that since the only even prime number is 2, it is quite wasteful to cache isPrime for even arguments. So we can save half the space by memoizing it for odd numbers only:isPrimeCache2 :: UChimera Bool isPrimeCache2 = tabulate (isPrime . (\n -> 2 * n + 1)) isPrime2 :: Word -> Bool isPrime2 n | n == 2 = True | even n = False | otherwise = index isPrimeCache2 ((n - 1) `quot` 2)
Here \n -> 2 * n + 1 maps n to the (n+1)-th odd number, and \n -> (n - 1) `quot` 2 takes it back. These functions are available below as fromWheel2 and toWheel2. Odd numbers are the simplest example of numbers, lacking small prime factors (so called rough numbers). Removing numbers, having small prime factors, is sometimes called wheel sieving. One can go further and exclude not only even numbers, but also integers, divisible by 3. To do this we need a function which maps n to the (n+1)-th number coprime with 2 and 3 (thus, with 6) and its inverse: namely, fromWheel6 and toWheel6. Then writeisPrimeCache6 :: UChimera Bool isPrimeCache6 = tabulate (isPrime . fromWheel6) isPrime6 :: Word -> Bool isPrime6 n | n `elem` [2, 3] = True | n `gcd` 6 /= 1 = False | otherwise = index isPrimeCache6 (toWheel6 n)
Thus, the wheel of 6 saves more space, improving memory locality. (If you need to reduce memory consumption even further, consider using Bit wrapper, which provides an instance of unboxed vector, packing one boolean per bit instead of one boolean per byte for Bool)bitCoerceMap :: (BitPack a, BitPack b, BitSize a ~ BitSize b) => (a -> a) -> b -> bclash-prelude Clash.Class.BitPack Map a value by first coercing to another type through its bit representation.
>>> pack (-5 :: Signed 32) 0b1111_1111_1111_1111_1111_1111_1111_1011 >>> bitCoerceMap @(Vec 4 (BitVector 8)) (replace 1 0) (-5 :: Signed 32) -16711685 >>> pack (-16711685 :: Signed 32) 0b1111_1111_0000_0000_1111_1111_1111_1011
bitCoerceMap :: (BitPack a, BitPack b, BitSize a ~ BitSize b) => (a -> a) -> b -> bclash-prelude Clash.Class.BitPack.Internal Map a value by first coercing to another type through its bit representation.
>>> pack (-5 :: Signed 32) 0b1111_1111_1111_1111_1111_1111_1111_1011 >>> bitCoerceMap @(Vec 4 (BitVector 8)) (replace 1 0) (-5 :: Signed 32) -16711685 >>> pack (-16711685 :: Signed 32) 0b1111_1111_0000_0000_1111_1111_1111_1011
concatMap :: forall a (m :: Nat) b (n :: Nat) . (a -> Vec m b) -> Vec n a -> Vec (n * m) bclash-prelude Clash.Explicit.Prelude Map a function over all the elements of a vector and concatentate the resulting vectors.
>>> concatMap (replicate d3) (1:>2:>3:>Nil) 1 :> 1 :> 1 :> 2 :> 2 :> 2 :> 3 :> 3 :> 3 :> Nil
imap :: forall (n :: Nat) a b . KnownNat n => (Index n -> a -> b) -> Vec n a -> Vec n bclash-prelude Clash.Explicit.Prelude Apply a function of every element of a vector and its index.
>>> :t imap (+) (2 :> 2 :> 2 :> 2 :> Nil) imap (+) (2 :> 2 :> 2 :> 2 :> Nil) :: Vec 4 (Index 4) >>> imap (+) (2 :> 2 :> 2 :> 2 :> Nil) 2 :> 3 :> *** Exception: X: Clash.Sized.Index: result 4 is out of bounds: [0..3] ... >>> imap (\i a -> extend (bitCoerce i) + a) (2 :> 2 :> 2 :> 2 :> Nil) :: Vec 4 (Unsigned 8) 2 :> 3 :> 4 :> 5 :> Nil
"imap f xs" corresponds to the following circuit layout:-
clash-prelude Clash.Explicit.Prelude Apply a function to every element of a vector and the element's position (as an SNat value) in the vector.
>>> let rotateMatrix = smap (flip rotateRightS) >>> let xss = (1:>2:>3:>Nil):>(1:>2:>3:>Nil):>(1:>2:>3:>Nil):>Nil >>> xss (1 :> 2 :> 3 :> Nil) :> (1 :> 2 :> 3 :> Nil) :> (1 :> 2 :> 3 :> Nil) :> Nil >>> rotateMatrix xss (1 :> 2 :> 3 :> Nil) :> (3 :> 1 :> 2 :> Nil) :> (2 :> 3 :> 1 :> Nil) :> Nil
concatMap :: forall a (m :: Nat) b (n :: Nat) . (a -> Vec m b) -> Vec n a -> Vec (n * m) bclash-prelude Clash.Explicit.Prelude.Safe Map a function over all the elements of a vector and concatentate the resulting vectors.
>>> concatMap (replicate d3) (1:>2:>3:>Nil) 1 :> 1 :> 1 :> 2 :> 2 :> 2 :> 3 :> 3 :> 3 :> Nil
imap :: forall (n :: Nat) a b . KnownNat n => (Index n -> a -> b) -> Vec n a -> Vec n bclash-prelude Clash.Explicit.Prelude.Safe Apply a function of every element of a vector and its index.
>>> :t imap (+) (2 :> 2 :> 2 :> 2 :> Nil) imap (+) (2 :> 2 :> 2 :> 2 :> Nil) :: Vec 4 (Index 4) >>> imap (+) (2 :> 2 :> 2 :> 2 :> Nil) 2 :> 3 :> *** Exception: X: Clash.Sized.Index: result 4 is out of bounds: [0..3] ... >>> imap (\i a -> extend (bitCoerce i) + a) (2 :> 2 :> 2 :> 2 :> Nil) :: Vec 4 (Unsigned 8) 2 :> 3 :> 4 :> 5 :> Nil
"imap f xs" corresponds to the following circuit layout:-
clash-prelude Clash.Explicit.Prelude.Safe Apply a function to every element of a vector and the element's position (as an SNat value) in the vector.
>>> let rotateMatrix = smap (flip rotateRightS) >>> let xss = (1:>2:>3:>Nil):>(1:>2:>3:>Nil):>(1:>2:>3:>Nil):>Nil >>> xss (1 :> 2 :> 3 :> Nil) :> (1 :> 2 :> 3 :> Nil) :> (1 :> 2 :> 3 :> Nil) :> Nil >>> rotateMatrix xss (1 :> 2 :> 3 :> Nil) :> (3 :> 1 :> 2 :> Nil) :> (2 :> 3 :> 1 :> Nil) :> Nil
fmap :: Functor f => (a -> b) -> f a -> f bclash-prelude Clash.HaskellPrelude fmap is used to apply a function of type (a -> b) to a value of type f a, where f is a functor, to produce a value of type f b. Note that for any type constructor with more than one parameter (e.g., Either), only the last type parameter can be modified with fmap (e.g., b in `Either a b`). Some type constructors with two parameters or more have a Bifunctor instance that allows both the last and the penultimate parameters to be mapped over.
Examples
Convert from a Maybe Int to a Maybe String using show:>>> fmap show Nothing Nothing >>> fmap show (Just 3) Just "3"
Convert from an Either Int Int to an Either Int String using show:>>> fmap show (Left 17) Left 17 >>> fmap show (Right 17) Right "17"
Double each element of a list:>>> fmap (*2) [1,2,3] [2,4,6]
Apply even to the second element of a pair:>>> fmap even (2,2) (2,True)
It may seem surprising that the function is only applied to the last element of the tuple compared to the list example above which applies it to every element in the list. To understand, remember that tuples are type constructors with multiple type parameters: a tuple of 3 elements (a,b,c) can also be written (,,) a b c and its Functor instance is defined for Functor ((,,) a b) (i.e., only the third parameter is free to be mapped over with fmap). It explains why fmap can be used with tuples containing values of different types as in the following example:>>> fmap even ("hello", 1.0, 4) ("hello",1.0,True)