module Data.RAList.Internal where

import Data.Word
-- cribbed from indexed-traversable, modified
-- originally in  'Control.Lens.Indexed.indexed'.
newtype Indexing f a = Indexing { Indexing f a -> Word64 -> (Word64, f a)
runIndexing :: Word64 -> (Word64, f a) }

instance Functor f => Functor (Indexing f) where
  fmap :: (a -> b) -> Indexing f a -> Indexing f b
fmap a -> b
f (Indexing Word64 -> (Word64, f a)
m) = (Word64 -> (Word64, f b)) -> Indexing f b
forall (f :: * -> *) a. (Word64 -> (Word64, f a)) -> Indexing f a
Indexing ((Word64 -> (Word64, f b)) -> Indexing f b)
-> (Word64 -> (Word64, f b)) -> Indexing f b
forall a b. (a -> b) -> a -> b
$ \Word64
i -> case Word64 -> (Word64, f a)
m Word64
i of
    (Word64
j, f a
x) -> (Word64
j, (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f f a
x)
  {-# INLINE fmap #-}

instance Applicative f => Applicative (Indexing f) where
  pure :: a -> Indexing f a
pure a
x = (Word64 -> (Word64, f a)) -> Indexing f a
forall (f :: * -> *) a. (Word64 -> (Word64, f a)) -> Indexing f a
Indexing ((Word64 -> (Word64, f a)) -> Indexing f a)
-> (Word64 -> (Word64, f a)) -> Indexing f a
forall a b. (a -> b) -> a -> b
$ \Word64
i -> (Word64
i, a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
  {-# INLINE pure #-}
  Indexing Word64 -> (Word64, f (a -> b))
mf <*> :: Indexing f (a -> b) -> Indexing f a -> Indexing f b
<*> Indexing Word64 -> (Word64, f a)
ma = (Word64 -> (Word64, f b)) -> Indexing f b
forall (f :: * -> *) a. (Word64 -> (Word64, f a)) -> Indexing f a
Indexing ((Word64 -> (Word64, f b)) -> Indexing f b)
-> (Word64 -> (Word64, f b)) -> Indexing f b
forall a b. (a -> b) -> a -> b
$ \Word64
i -> case Word64 -> (Word64, f (a -> b))
mf Word64
i of
    (Word64
j, f (a -> b)
ff) -> case Word64 -> (Word64, f a)
ma Word64
j of
                  (Word64
k, f a
fa) -> (Word64
k, f (a -> b)
ff f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
fa)
  {-# INLINE (<*>) #-}