-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/

-- | Lazy infinite streams with O(1) indexing and applications for memoization
--   
--   There are plenty of memoizing libraries on Hackage, but they usually
--   fall into two categories:
--   
--   
--   
- Store cache as a flat array, enabling us to obtain cached values
--   in O(1) time, which is nice. The drawback is that one must specify the
--   size of the array beforehand, limiting an interval of inputs, and
--   actually allocate it at once.
--   
- Store cache as a lazy binary tree. Thanks to laziness, one can
--   freely use the full range of inputs. The drawback is that obtaining
--   values from a tree takes logarithmic time and is unfriendly to CPU
--   cache, which kinda defeats the purpose.
--   
--   
--   This package intends to tackle both issues, providing a data type
--   Chimera for lazy infinite compact streams with cache-friendly
--   O(1) indexing.
--   
--   Additional features include:
--   
--   
--   
- memoization of recursive functions and recurrent sequences,
--   
- memoization of functions of several, possibly signed
--   arguments,
--   
- efficient memoization of boolean predicates.
--   
@package chimera
@version 0.3.4.0

-- | Helpers for continuous mappings, useful to memoize functions on
--   Int (instead of Word only) and functions over two and
--   three arguments.
--   
--   Example 1
--   
--   Imagine writing a program to simulate Rule 90. This is a
--   cellular automaton, which consists of an infinite one-dimensional line
--   of cells, each being either dead (False) or alive
--   (True). If two neighbours of a cell are equal, it becomes dead
--   on the next step, otherwise alive.
--   
--   Usually cellular automata are modelled by a finite vector. This is a
--   bit suboptimal, because cellular automata may grow in different
--   directions over time, but with a finite vector one has to define a
--   bounding segment well beforehand. Moreover, what if we are interested
--   to explore an evolution of an essentially infinite initial
--   configuration?
--   
--   It would be natural to encode an initial configuration as a function
--   Int -> Bool, which takes a coordinate and
--   returns the status of the corresponding cell. Define a function, which
--   translates the automaton to the next step:
--   
--   
--   step :: (Int -> Bool) -> (Int -> Bool)
--   step current = \n -> current (n - 1) /= current (n + 1)
--   
--   
--   Unfortunately, iterating step would be extremely slow because
--   of branching recursion. One could suggest to introduce a caching
--   layer:
--   
--   
--   step :: (Int -> Bool) -> (Int -> Bool)
--   step current = \n -> current' (n - 1) /= current' (n + 1)
--     where
--       current' = memoize (current . fromIntegral) . fromIntegral
--   
--   
--   Unfortunately, it would not work well, because fromIntegral
--   :: Int -> Word maps -1 to
--   maxBound and it would take ages to memoize everything up to
--   maxBound. But continuous mappings intToWord and
--   wordToInt avoid this issue:
--   
--   
--   step :: (Int -> Bool) -> (Int -> Bool)
--   step current = \n -> current' (n - 1) /= current' (n + 1)
--     where
--       current' = memoize (current . wordToInt) . intToWord
--   
--   
--   Example 2
--   
--   What about another famous cellular automaton: Conway's Game of
--   Life? It is two-dimensional, so its state can be represented as a
--   function Int -> Int ->
--   Bool. Following the approach above, we would like to memoize
--   such functions. Namely, cast the state to Word ->
--   Bool, ready for memoization:
--   
--   
--   cast :: (Int -> Int -> Bool) -> (Word -> Bool)
--   cast f = \n -> let (x, y) = fromZCurve n in
--    f (wordToInt x) (wordToInt y)
--   
--   
--   and then back:
--   
--   
--   uncast :: (Word -> Bool) -> (Int -> Int -> Bool)
--   uncast g = \x y -> g (toZCurve (intToWord x) (intToWord y))
--   
module Data.Chimera.ContinuousMapping

-- | Total map, which satisfies
--   
--   
--   abs (intToWord x - intToWord y) <= 2 * abs (x - y)
--   
--   
--   Note that usual fromIntegral :: Int
--   -> Word does not satisfy this inequality, because
--   it has a discontinuity between −1 and 0.
--   
--   
--   >>> map intToWord [-5..5]
--   [9,7,5,3,1,0,2,4,6,8,10]
--   
intToWord :: Int -> Word

-- | Inverse for intToWord.
--   
--   
--   >>> map wordToInt [0..10]
--   [0,-1,1,-2,2,-3,3,-4,4,-5,5]
--   
wordToInt :: Word -> Int

-- | Total map from plain to line, continuous almost everywhere. See
--   Z-order curve.
--   
--   Only lower halfs of bits of arguments are used (32 bits on 64-bit
--   architecture).
--   
--   
--   >>> [ toZCurve x y | x <- [0..3], y <- [0..3] ]
--   [0,2,8,10,1,3,9,11,4,6,12,14,5,7,13,15]
--   
toZCurve :: Word -> Word -> Word

-- | Inverse for toZCurve. See Z-order curve.
--   
--   
--   >>> map fromZCurve [0..15]
--   [(0,0),(1,0),(0,1),(1,1),(2,0),(3,0),(2,1),(3,1),(0,2),(1,2),(0,3),(1,3),(2,2),(3,2),(2,3),(3,3)]
--   
fromZCurve :: Word -> (Word, Word)

-- | Total map from space to line, continuous almost everywhere. See
--   Z-order curve.
--   
--   Only lower thirds of bits of arguments are used (21 bits on 64-bit
--   architecture).
--   
--   
--   >>> [ toZCurve3 x y z | x <- [0..3], y <- [0..3], z <- [0..3] ]
--   [0,4,32,36,2,6,34,38,16,20,48,52,18,22,50,54,1,5,33,37,3,7,35,39,17,21,49,53,19,23,51,55,
--   8,12,40,44,10,14,42,46,24,28,56,60,26,30,58,62,9,13,41,45,11,15,43,47,25,29,57,61,27,31,59,63]
--   
toZCurve3 :: Word -> Word -> Word -> Word

-- | Inverse for toZCurve3. See Z-order curve.
--   
--   
--   >>> map fromZCurve3 [0..63]
--   [(0,0,0),(1,0,0),(0,1,0),(1,1,0),(0,0,1),(1,0,1),(0,1,1),(1,1,1),(2,0,0),(3,0,0),(2,1,0),(3,1,0),(2,0,1),(3,0,1),(2,1,1),(3,1,1),
--    (0,2,0),(1,2,0),(0,3,0),(1,3,0),(0,2,1),(1,2,1),(0,3,1),(1,3,1),(2,2,0),(3,2,0),(2,3,0),(3,3,0),(2,2,1),(3,2,1),(2,3,1),(3,3,1),
--    (0,0,2),(1,0,2),(0,1,2),(1,1,2),(0,0,3),(1,0,3),(0,1,3),(1,1,3),(2,0,2),(3,0,2),(2,1,2),(3,1,2),(2,0,3),(3,0,3),(2,1,3),(3,1,3),
--    (0,2,2),(1,2,2),(0,3,2),(1,3,2),(0,2,3),(1,2,3),(0,3,3),(1,3,3),(2,2,2),(3,2,2),(2,3,2),(3,3,2),(2,2,3),(3,2,3),(2,3,3),(3,3,3)]
--   
fromZCurve3 :: Word -> (Word, Word, Word)

-- | Lazy infinite streams with O(1) indexing.
module Data.Chimera

-- | Memoize a function: repeating calls to memoize f
--   n would compute f n only once and cache the
--   result in VChimera. This is just a shortcut for index
--   . tabulate. When a is Unbox, it is
--   faster to use index (tabulate f ::
--   UChimera a).
--   
--   
--   memoize f n = f n
--   
memoize :: (Word -> a) -> Word -> a

-- | For a given f memoize a recursive function fix
--   f, caching results in VChimera. This is just a
--   shortcut for index . tabulateFix. When a
--   is Unbox, it is faster to use index (tabulateFix
--   f :: UChimera a).
--   
--   
--   memoizeFix f n = fix f n
--   
--   
--   For example, imagine that we want to memoize Fibonacci numbers:
--   
--   
--   >>> fibo n = if n < 2 then toInteger n else fibo (n - 1) + fibo (n - 2)
--   
--   
--   Can we find fiboF such that fibo = fix
--   fiboF? Just replace all recursive calls to fibo with
--   f:
--   
--   
--   >>> fiboF f n = if n < 2 then toInteger n else f (n - 1) + f (n - 2)
--   
--   
--   Now we are ready to use memoizeFix:
--   
--   
--   >>> memoizeFix fiboF 10
--   55
--   
--   >>> memoizeFix fiboF 100
--   354224848179261915075
--   
--   
--   This function can be used even when arguments of recursive calls are
--   not strictly decreasing, but they might not get memoized. If this is
--   not desired use tabulateFix' instead. For example, here is a
--   routine to measure the length of Collatz sequence:
--   
--   
--   >>> collatzF f n = if n <= 1 then 0 else 1 + f (if even n then n `quot` 2 else 3 * n + 1)
--   
--   >>> memoizeFix collatzF 27
--   111
--   
memoizeFix :: ((Word -> a) -> Word -> a) -> Word -> a

-- | Lazy infinite streams with elements from a, backed by a
--   Vector v (boxed, unboxed, storable, etc.). Use
--   tabulate, tabulateFix, etc. to create a stream and
--   index to access its arbitrary elements in constant time.
data Chimera v a

-- | Streams backed by boxed vectors.
type VChimera = Chimera Vector

-- | Streams backed by unboxed vectors.
type UChimera = Chimera Vector

-- | Create a stream of values of a given function. Once created it can be
--   accessed via index or toList.
--   
--   
--   >>> ch = tabulate (^ 2) :: UChimera Word
--   
--   >>> index ch 9
--   81
--   
--   >>> take 10 (toList ch)
--   [0,1,4,9,16,25,36,49,64,81]
--   
tabulate :: Vector v a => (Word -> a) -> Chimera v a

-- | For a given f create a stream of values of a recursive
--   function fix f. Once created it can be accessed via
--   index or toList.
--   
--   For example, imagine that we want to tabulate Catalan numbers:
--   
--   
--   >>> catalan n = if n == 0 then 1 else sum [ catalan i * catalan (n - 1 - i) | i <- [0 .. n - 1] ]
--   
--   
--   Can we find catalanF such that catalan = fix
--   catalanF? Just replace all recursive calls to
--   catalan with f:
--   
--   
--   >>> catalanF f n = if n == 0 then 1 else sum [ f i * f (n - 1 - i) | i <- [0 .. n - 1] ]
--   
--   
--   Now we are ready to use tabulateFix:
--   
--   
--   >>> ch = tabulateFix catalanF :: VChimera Integer
--   
--   >>> index ch 9
--   4862
--   
--   >>> take 10 (toList ch)
--   [1,1,2,5,14,42,132,429,1430,4862]
--   
--   
--   Note: Only recursive function calls with decreasing arguments
--   are memoized. If full memoization is desired, use tabulateFix'
--   instead.
tabulateFix :: Vector v a => ((Word -> a) -> Word -> a) -> Chimera v a

-- | Fully memoizing version of tabulateFix. This function will
--   tabulate every recursive call, but might allocate a lot of memory in
--   doing so. For example, the following piece of code calculates the
--   highest number reached by the Collatz sequence of a given
--   number, but also allocates tens of gigabytes of memory, because the
--   Collatz sequence will spike to very high numbers.
--   
--   
--   >>> collatzF :: (Word -> Word) -> (Word -> Word)
--   
--   >>> collatzF _ 0 = 0
--   
--   >>> collatzF f n = if n <= 2 then 4 else n `max` f (if even n then n `quot` 2 else 3 * n + 1)
--   
--   >>> 
--   
--   >>> maximumBy (comparing $ index $ tabulateFix' collatzF) [0..1000000]
--   ...
--   
--   
--   Using memoizeFix instead fixes the problem:
--   
--   
--   >>> maximumBy (comparing $ memoizeFix collatzF) [0..1000000]
--   56991483520
--   
tabulateFix' :: Vector v a => ((Word -> a) -> Word -> a) -> Chimera v a

-- | iterate f x returns an infinite stream,
--   generated by repeated applications of f to x.
--   
--   It holds that index (iterate f x) 0 is
--   equal to x.
--   
--   
--   >>> ch = iterate (+ 1) 0 :: UChimera Int
--   
--   >>> take 10 (toList ch)
--   [0,1,2,3,4,5,6,7,8,9]
--   
iterate :: Vector v a => (a -> a) -> a -> Chimera v a

-- | iterateWithIndex f x returns an infinite
--   stream, generated by applications of f to a current index and
--   previous value, starting from x.
--   
--   It holds that index (iterateWithIndex f
--   x) 0 is equal to x.
--   
--   
--   >>> ch = iterateWithIndex (+) 100 :: UChimera Word
--   
--   >>> take 10 (toList ch)
--   [100,101,103,106,110,115,121,128,136,145]
--   
iterateWithIndex :: Vector v a => (Word -> a -> a) -> a -> Chimera v a

-- | unfoldr f x returns an infinite stream,
--   generated by repeated applications of f to x,
--   similar to unfoldr.
--   
--   
--   >>> ch = unfoldr (\acc -> (acc * acc, acc + 1)) 0 :: UChimera Int
--   
--   >>> take 10 (toList ch)
--   [0,1,4,9,16,25,36,49,64,81]
--   
unfoldr :: Vector v b => (a -> (b, a)) -> a -> Chimera v b

-- | Return an infinite repetition of a given vector. Throw an error on an
--   empty vector.
--   
--   
--   >>> ch = cycle (Data.Vector.fromList [4, 2]) :: VChimera Int
--   
--   >>> take 10 (toList ch)
--   [4,2,4,2,4,2,4,2,4,2]
--   
cycle :: Vector v a => v a -> Chimera v a

-- | Create a stream of values from a given prefix, followed by default
--   value afterwards.
fromListWithDef :: Vector v a => a -> [a] -> Chimera v a

-- | Create a stream of values from a given prefix, followed by default
--   value afterwards.
fromVectorWithDef :: Vector v a => a -> v a -> Chimera v a

-- | Create a stream of values from a given infinite list.
fromInfinite :: Vector v a => Infinite a -> Chimera v a

-- | Intertleave two streams, sourcing even elements from the first one and
--   odd elements from the second one.
--   
--   
--   >>> ch = interleave (tabulate id) (tabulate (+ 100)) :: UChimera Word
--   
--   >>> take 10 (toList ch)
--   [0,100,1,101,2,102,3,103,4,104]
--   
interleave :: Vector v a => Chimera v a -> Chimera v a -> Chimera v a

-- | Index a stream in a constant time.
--   
--   
--   >>> ch = tabulate (^ 2) :: UChimera Word
--   
--   >>> index ch 9
--   81
--   
index :: Vector v a => Chimera v a -> Word -> a

-- | Right-associative fold, necessarily lazy in the accumulator. Any
--   unconditional attempt to force the accumulator even to WHNF will hang
--   the computation. E. g., the following definition isn't productive:
--   
--   
--   import Data.List.NonEmpty (NonEmpty(..))
--   toNonEmpty = foldr (\a (x :| xs) -> a :| x : xs) :: VChimera a -> NonEmpty a
--   
--   
--   One should use lazy patterns, e. g.,
--   
--   
--   toNonEmpty = foldr (\a ~(x :| xs) -> a :| x : xs)
--   
foldr :: Vector v a => (a -> b -> b) -> Chimera v a -> b

-- | Convert a stream to an infinite list.
--   
--   
--   >>> ch = tabulate (^ 2) :: UChimera Word
--   
--   >>> take 10 (toList ch)
--   [0,1,4,9,16,25,36,49,64,81]
--   
toList :: Vector v a => Chimera v a -> [a]

-- | Convert a stream to a proper infinite list.
toInfinite :: Vector v a => Chimera v a -> Infinite a

-- | Monadic version of tabulate.
tabulateM :: (Monad m, Vector v a) => (Word -> m a) -> m (Chimera v a)

-- | Monadic version of tabulateFix. There are no particular
--   guarantees about the order of recursive calls: they may be executed
--   more than once or executed in different order. That said, monadic
--   effects must be idempotent and commutative.
tabulateFixM :: (Monad m, Vector v a) => ((Word -> m a) -> Word -> m a) -> m (Chimera v a)

-- | Monadic version of tabulateFix'.
tabulateFixM' :: forall m v a. (Monad m, Vector v a) => ((Word -> m a) -> Word -> m a) -> m (Chimera v a)

-- | Monadic version of iterate.
iterateM :: (Monad m, Vector v a) => (a -> m a) -> a -> m (Chimera v a)

-- | Monadic version of iterateWithIndex.
iterateWithIndexM :: (Monad m, Vector v a) => (Word -> a -> m a) -> a -> m (Chimera v a)

-- | Monadic version of unfoldr.
unfoldrM :: (Monad m, Vector v b) => (a -> m (b, a)) -> a -> m (Chimera v b)

-- | Map subvectors of a stream, using a given length-preserving function.
mapSubvectors :: (Vector u a, Vector v b) => (u a -> v b) -> Chimera u a -> Chimera v b

-- | Traverse subvectors of a stream, using a given length-preserving
--   function.
--   
--   Be careful, because similar to tabulateM, only lazy monadic
--   effects can be executed in a finite time: lazy state monad is fine,
--   but strict one is not.
traverseSubvectors :: (Vector u a, Vector v b, Applicative m) => (u a -> m (v b)) -> Chimera u a -> m (Chimera v b)

-- | Zip subvectors from two streams, using a given length-preserving
--   function.
zipWithSubvectors :: (Vector u a, Vector v b, Vector w c) => (u a -> v b -> w c) -> Chimera u a -> Chimera v b -> Chimera w c

-- | Zip subvectors from two streams, using a given monadic
--   length-preserving function. Caveats for tabulateM and
--   traverseSubvectors apply.
zipWithMSubvectors :: (Vector u a, Vector v b, Vector w c, Applicative m) => (u a -> v b -> m (w c)) -> Chimera u a -> Chimera v b -> m (Chimera w c)

-- | Take a slice of Chimera, represented as a list on consecutive
--   subvectors.
sliceSubvectors :: Vector v a => Int -> Int -> Chimera v a -> [v a]
instance Data.Traversable.Traversable v => Data.Traversable.Traversable (Data.Chimera.Chimera v)
instance Data.Foldable.Foldable v => Data.Foldable.Foldable (Data.Chimera.Chimera v)
instance GHC.Base.Functor v => GHC.Base.Functor (Data.Chimera.Chimera v)
instance GHC.Base.Applicative (Data.Chimera.Chimera Data.Vector.Vector)
instance GHC.Base.Monad (Data.Chimera.Chimera Data.Vector.Vector)
instance Control.Monad.Fix.MonadFix (Data.Chimera.Chimera Data.Vector.Vector)
instance Control.Monad.Zip.MonadZip (Data.Chimera.Chimera Data.Vector.Vector)
instance Control.Monad.Reader.Class.MonadReader GHC.Types.Word (Data.Chimera.Chimera Data.Vector.Vector)
instance Data.Distributive.Distributive (Data.Chimera.Chimera Data.Vector.Vector)
instance Data.Functor.Rep.Representable (Data.Chimera.Chimera Data.Vector.Vector)

-- | 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 write
--   
--   
--   isPrimeCache6 :: 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)
module Data.Chimera.WheelMapping

-- | fromWheel2 n is the (n+1)-th positive odd number. Sequence
--   A005408.
--   
--   
--   map fromWheel2 [0..] == [ n | n <- [0..], n `gcd` 2 == 1 ]
--   
--   
--   
--   >>> map fromWheel2 [0..9]
--   [1,3,5,7,9,11,13,15,17,19]
--   
fromWheel2 :: Word -> Word

-- | Left inverse for fromWheel2. Monotonically non-decreasing
--   function.
--   
--   
--   toWheel2 . fromWheel2 == id
--   
toWheel2 :: Word -> Word

-- | fromWheel6 n is the (n+1)-th positive number, not divisible by
--   2 or 3. Sequence A007310.
--   
--   
--   map fromWheel6 [0..] == [ n | n <- [0..], n `gcd` 6 == 1 ]
--   
--   
--   
--   >>> map fromWheel6 [0..9]
--   [1,5,7,11,13,17,19,23,25,29]
--   
fromWheel6 :: Word -> Word

-- | Left inverse for fromWheel6. Monotonically non-decreasing
--   function.
--   
--   
--   toWheel6 . fromWheel6 == id
--   
toWheel6 :: Word -> Word

-- | fromWheel30 n is the (n+1)-th positive number, not divisible by
--   2, 3 or 5. Sequence A007775.
--   
--   
--   map fromWheel30 [0..] == [ n | n <- [0..], n `gcd` 30 == 1 ]
--   
--   
--   
--   >>> map fromWheel30 [0..9]
--   [1,7,11,13,17,19,23,29,31,37]
--   
fromWheel30 :: Word -> Word

-- | Left inverse for fromWheel30. Monotonically non-decreasing
--   function.
--   
--   
--   toWheel30 . fromWheel30 == id
--   
toWheel30 :: Word -> Word

-- | fromWheel210 n is the (n+1)-th positive number, not divisible
--   by 2, 3, 5 or 7. Sequence A008364.
--   
--   
--   map fromWheel210 [0..] == [ n | n <- [0..], n `gcd` 210 == 1 ]
--   
--   
--   
--   >>> map fromWheel210 [0..9]
--   [1,11,13,17,19,23,29,31,37,41]
--   
fromWheel210 :: Word -> Word

-- | Left inverse for fromWheel210. Monotonically non-decreasing
--   function.
--   
--   
--   toWheel210 . fromWheel210 == id
--   
toWheel210 :: Word -> Word
