{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | This module provides pull arrays.
--
-- These are part of a larger framework for controlling when memory is
-- allocated for an array. See @Data.Array.Polarized@.
--
module Data.Array.Polarized.Pull
  ( Array
    -- * Construction
  , fromFunction
  , fromVector
  , make
  , singleton
    -- * Consumption
  , toVector
  , asList
    -- * Operations
  , zip
  , zipWith
  , append
  , foldr
  , foldMap
  , findLength
  , split
  , reverse
  , index
  )
  where

import Data.Array.Polarized.Pull.Internal
-- XXX: the data constructor Pull.Array could be used unsafely, so we don't
-- export it, instead exporting a collection of functions to manipulate
-- PullArrays
-- (eg one could use an element multiple times, if the constructor was
-- available)
-- TODO: the current collection is almost certainly not complete: it would be
-- nice if there was one (or a small number) of functions which characterise
-- PullArrays, but I'm not sure what they are
-- In particular, PullArrays are incredibly unfriendly in returned-value
-- position at the moment, moreso than they should be
import qualified Data.Functor.Linear as Data
import Prelude.Linear hiding (zip, zipWith, foldr, foldMap, reverse)
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import qualified Unsafe.Linear as Unsafe

-- | Convert a pull array into a list.
asList :: Array a %1-> [a]
asList :: forall a. Array a %1 -> [a]
asList = (a %1 -> [a] %1 -> [a]) -> [a] %1 -> Array a %1 -> [a]
forall a b. (a %1 -> b %1 -> b) -> b %1 -> Array a %1 -> b
foldr (\a
x [a]
xs -> a
xa %1 -> [a] %1 -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) []

-- | @zipWith f [x1,x2,...,xn] [y1,y2,...,yn] = [f x1 y1, ..., f xn yn]@
-- __Partial:__ `zipWith f [x1,x2,...,xn] [y1,y2,...,yp]` is an error
-- if @n ≠ p@.
zipWith :: (a %1-> b %1-> c) -> Array a %1-> Array b %1-> Array c
zipWith :: forall a b c.
(a %1 -> b %1 -> c) -> Array a %1 -> Array b %1 -> Array c
zipWith a %1 -> b %1 -> c
f Array a
x Array b
y = ((a, b) %1 -> c) -> Array (a, b) %1 -> Array c
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap ((a %1 -> b %1 -> c) %1 -> (a, b) %1 -> c
forall a b c. (a %1 -> b %1 -> c) %1 -> (a, b) %1 -> c
uncurry a %1 -> b %1 -> c
f) (Array a %1 -> Array b %1 -> Array (a, b)
forall a b. Array a %1 -> Array b %1 -> Array (a, b)
zip Array a
x Array b
y)

-- | Fold a pull array using a monoid.
foldMap :: Monoid m => (a %1-> m) -> Array a %1-> m
foldMap :: forall m a. Monoid m => (a %1 -> m) -> Array a %1 -> m
foldMap a %1 -> m
f = (a %1 -> m %1 -> m) -> m %1 -> Array a %1 -> m
forall a b. (a %1 -> b %1 -> b) -> b %1 -> Array a %1 -> b
foldr (m %1 -> m %1 -> m
forall a. Semigroup a => a %1 -> a %1 -> a
(<>) (m %1 -> m %1 -> m) %1 -> (a %1 -> m) %1 -> a %1 -> m %1 -> m
forall b c a. (b %1 -> c) %1 -> (a %1 -> b) %1 -> a %1 -> c
. a %1 -> m
f) m
forall a. Monoid a => a
mempty

-- I'm fairly sure this can be used safely
-- | Convert a Vector to a pull array.
fromVector :: Vector a %1-> Array a
fromVector :: forall a. Vector a %1 -> Array a
fromVector = (Vector a -> Array a) %1 -> Vector a %1 -> Array a
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear ((Vector a -> Array a) %1 -> Vector a %1 -> Array a)
%1 -> (Vector a -> Array a) %1 -> Vector a %1 -> Array a
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ \Vector a
v -> (Int -> a) -> Int -> Array a
forall a. (Int -> a) -> Int -> Array a
fromFunction (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
Vector.!) (Vector a -> Int
forall a. Vector a -> Int
Vector.length Vector a
v)