module Language.Fortran.Repr.Type.Array where

import Language.Fortran.Repr.Type.Scalar
import Language.Fortran.Repr.Compat.Natural

import GHC.Generics ( Generic )
import Data.Data ( Data )

-- | A Fortran array type.
--
-- An array type is defined by a scalar type together with a shape.
data FArrayType = FArrayType
  { FArrayType -> FScalarType
fatScalar :: FScalarType
  , FArrayType -> Shape
fatShape  :: Shape
  } deriving stock ((forall x. FArrayType -> Rep FArrayType x)
-> (forall x. Rep FArrayType x -> FArrayType) -> Generic FArrayType
forall x. Rep FArrayType x -> FArrayType
forall x. FArrayType -> Rep FArrayType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FArrayType -> Rep FArrayType x
from :: forall x. FArrayType -> Rep FArrayType x
$cto :: forall x. Rep FArrayType x -> FArrayType
to :: forall x. Rep FArrayType x -> FArrayType
Generic, Typeable FArrayType
Typeable FArrayType =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> FArrayType -> c FArrayType)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c FArrayType)
-> (FArrayType -> Constr)
-> (FArrayType -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c FArrayType))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c FArrayType))
-> ((forall b. Data b => b -> b) -> FArrayType -> FArrayType)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> FArrayType -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> FArrayType -> r)
-> (forall u. (forall d. Data d => d -> u) -> FArrayType -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> FArrayType -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> FArrayType -> m FArrayType)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> FArrayType -> m FArrayType)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> FArrayType -> m FArrayType)
-> Data FArrayType
FArrayType -> Constr
FArrayType -> DataType
(forall b. Data b => b -> b) -> FArrayType -> FArrayType
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> FArrayType -> u
forall u. (forall d. Data d => d -> u) -> FArrayType -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FArrayType
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FArrayType -> c FArrayType
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FArrayType)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FArrayType)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FArrayType -> c FArrayType
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FArrayType -> c FArrayType
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FArrayType
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FArrayType
$ctoConstr :: FArrayType -> Constr
toConstr :: FArrayType -> Constr
$cdataTypeOf :: FArrayType -> DataType
dataTypeOf :: FArrayType -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FArrayType)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FArrayType)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FArrayType)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FArrayType)
$cgmapT :: (forall b. Data b => b -> b) -> FArrayType -> FArrayType
gmapT :: (forall b. Data b => b -> b) -> FArrayType -> FArrayType
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FArrayType -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> FArrayType -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FArrayType -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FArrayType -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
Data, Int -> FArrayType -> ShowS
[FArrayType] -> ShowS
FArrayType -> String
(Int -> FArrayType -> ShowS)
-> (FArrayType -> String)
-> ([FArrayType] -> ShowS)
-> Show FArrayType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FArrayType -> ShowS
showsPrec :: Int -> FArrayType -> ShowS
$cshow :: FArrayType -> String
show :: FArrayType -> String
$cshowList :: [FArrayType] -> ShowS
showList :: [FArrayType] -> ShowS
Show, FArrayType -> FArrayType -> Bool
(FArrayType -> FArrayType -> Bool)
-> (FArrayType -> FArrayType -> Bool) -> Eq FArrayType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FArrayType -> FArrayType -> Bool
== :: FArrayType -> FArrayType -> Bool
$c/= :: FArrayType -> FArrayType -> Bool
/= :: FArrayType -> FArrayType -> Bool
Eq, Eq FArrayType
Eq FArrayType =>
(FArrayType -> FArrayType -> Ordering)
-> (FArrayType -> FArrayType -> Bool)
-> (FArrayType -> FArrayType -> Bool)
-> (FArrayType -> FArrayType -> Bool)
-> (FArrayType -> FArrayType -> Bool)
-> (FArrayType -> FArrayType -> FArrayType)
-> (FArrayType -> FArrayType -> FArrayType)
-> Ord FArrayType
FArrayType -> FArrayType -> Bool
FArrayType -> FArrayType -> Ordering
FArrayType -> FArrayType -> FArrayType
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: FArrayType -> FArrayType -> Ordering
compare :: FArrayType -> FArrayType -> Ordering
$c< :: FArrayType -> FArrayType -> Bool
< :: FArrayType -> FArrayType -> Bool
$c<= :: FArrayType -> FArrayType -> Bool
<= :: FArrayType -> FArrayType -> Bool
$c> :: FArrayType -> FArrayType -> Bool
> :: FArrayType -> FArrayType -> Bool
$c>= :: FArrayType -> FArrayType -> Bool
>= :: FArrayType -> FArrayType -> Bool
$cmax :: FArrayType -> FArrayType -> FArrayType
max :: FArrayType -> FArrayType -> FArrayType
$cmin :: FArrayType -> FArrayType -> FArrayType
min :: FArrayType -> FArrayType -> FArrayType
Ord)

-- | The shape of a Fortran array is a list of extents. (The rank of the array
--   is length of the list.)
--
-- Note that the F90 standard limits maximum array rank to 7 (R512).
--
-- TODO
--   * An empty list here feels nonsensical. Perhaps this should be NonEmpty.
--   * List type is inefficient here, since we don't care about pushing/popping,
--     and list length is important. Use a vector type instead.
newtype Shape = Shape { Shape -> [Natural]
getShape :: [Natural] }
    deriving stock ((forall x. Shape -> Rep Shape x)
-> (forall x. Rep Shape x -> Shape) -> Generic Shape
forall x. Rep Shape x -> Shape
forall x. Shape -> Rep Shape x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Shape -> Rep Shape x
from :: forall x. Shape -> Rep Shape x
$cto :: forall x. Rep Shape x -> Shape
to :: forall x. Rep Shape x -> Shape
Generic, Typeable Shape
Typeable Shape =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Shape -> c Shape)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Shape)
-> (Shape -> Constr)
-> (Shape -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Shape))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Shape))
-> ((forall b. Data b => b -> b) -> Shape -> Shape)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r)
-> (forall u. (forall d. Data d => d -> u) -> Shape -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Shape -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Shape -> m Shape)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Shape -> m Shape)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Shape -> m Shape)
-> Data Shape
Shape -> Constr
Shape -> DataType
(forall b. Data b => b -> b) -> Shape -> Shape
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Shape -> u
forall u. (forall d. Data d => d -> u) -> Shape -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Shape
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shape -> c Shape
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Shape)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Shape)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shape -> c Shape
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shape -> c Shape
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Shape
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Shape
$ctoConstr :: Shape -> Constr
toConstr :: Shape -> Constr
$cdataTypeOf :: Shape -> DataType
dataTypeOf :: Shape -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Shape)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Shape)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Shape)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Shape)
$cgmapT :: (forall b. Data b => b -> b) -> Shape -> Shape
gmapT :: (forall b. Data b => b -> b) -> Shape -> Shape
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Shape -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Shape -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Shape -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Shape -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
Data, Int -> Shape -> ShowS
[Shape] -> ShowS
Shape -> String
(Int -> Shape -> ShowS)
-> (Shape -> String) -> ([Shape] -> ShowS) -> Show Shape
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Shape -> ShowS
showsPrec :: Int -> Shape -> ShowS
$cshow :: Shape -> String
show :: Shape -> String
$cshowList :: [Shape] -> ShowS
showList :: [Shape] -> ShowS
Show, Shape -> Shape -> Bool
(Shape -> Shape -> Bool) -> (Shape -> Shape -> Bool) -> Eq Shape
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Shape -> Shape -> Bool
== :: Shape -> Shape -> Bool
$c/= :: Shape -> Shape -> Bool
/= :: Shape -> Shape -> Bool
Eq, Eq Shape
Eq Shape =>
(Shape -> Shape -> Ordering)
-> (Shape -> Shape -> Bool)
-> (Shape -> Shape -> Bool)
-> (Shape -> Shape -> Bool)
-> (Shape -> Shape -> Bool)
-> (Shape -> Shape -> Shape)
-> (Shape -> Shape -> Shape)
-> Ord Shape
Shape -> Shape -> Bool
Shape -> Shape -> Ordering
Shape -> Shape -> Shape
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Shape -> Shape -> Ordering
compare :: Shape -> Shape -> Ordering
$c< :: Shape -> Shape -> Bool
< :: Shape -> Shape -> Bool
$c<= :: Shape -> Shape -> Bool
<= :: Shape -> Shape -> Bool
$c> :: Shape -> Shape -> Bool
> :: Shape -> Shape -> Bool
$c>= :: Shape -> Shape -> Bool
>= :: Shape -> Shape -> Bool
$cmax :: Shape -> Shape -> Shape
max :: Shape -> Shape -> Shape
$cmin :: Shape -> Shape -> Shape
min :: Shape -> Shape -> Shape
Ord)

fatSize :: FArrayType -> Natural
fatSize :: FArrayType -> Natural
fatSize = [Natural] -> Natural
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Natural] -> Natural)
-> (FArrayType -> [Natural]) -> FArrayType -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Shape -> [Natural]
getShape (Shape -> [Natural])
-> (FArrayType -> Shape) -> FArrayType -> [Natural]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FArrayType -> Shape
fatShape