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. 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
$cto :: forall x. Rep FArrayType x -> FArrayType
$cfrom :: forall x. FArrayType -> Rep FArrayType x
Generic, Typeable FArrayType
FArrayType -> DataType
FArrayType -> Constr
(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)
gmapMo :: 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
gmapMp :: forall (m :: * -> *).
MonadPlus 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
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FArrayType -> m FArrayType
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FArrayType -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FArrayType -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> FArrayType -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FArrayType -> [u]
gmapQr :: 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
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FArrayType -> r
gmapT :: (forall b. Data b => b -> b) -> FArrayType -> FArrayType
$cgmapT :: (forall b. Data b => b -> b) -> FArrayType -> FArrayType
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FArrayType)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FArrayType)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FArrayType)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FArrayType)
dataTypeOf :: FArrayType -> DataType
$cdataTypeOf :: FArrayType -> DataType
toConstr :: FArrayType -> Constr
$ctoConstr :: FArrayType -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FArrayType
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FArrayType
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FArrayType -> c FArrayType
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FArrayType -> c FArrayType
Data, Int -> FArrayType -> ShowS
[FArrayType] -> ShowS
FArrayType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FArrayType] -> ShowS
$cshowList :: [FArrayType] -> ShowS
show :: FArrayType -> String
$cshow :: FArrayType -> String
showsPrec :: Int -> FArrayType -> ShowS
$cshowsPrec :: Int -> FArrayType -> ShowS
Show, FArrayType -> FArrayType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FArrayType -> FArrayType -> Bool
$c/= :: FArrayType -> FArrayType -> Bool
== :: FArrayType -> FArrayType -> Bool
$c== :: FArrayType -> FArrayType -> Bool
Eq, Eq 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
min :: FArrayType -> FArrayType -> FArrayType
$cmin :: FArrayType -> FArrayType -> FArrayType
max :: FArrayType -> FArrayType -> FArrayType
$cmax :: FArrayType -> FArrayType -> FArrayType
>= :: FArrayType -> FArrayType -> Bool
$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
compare :: FArrayType -> FArrayType -> Ordering
$ccompare :: FArrayType -> FArrayType -> Ordering
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. 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
$cto :: forall x. Rep Shape x -> Shape
$cfrom :: forall x. Shape -> Rep Shape x
Generic, Typeable Shape
Shape -> DataType
Shape -> Constr
(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)
gmapMo :: 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
gmapMp :: forall (m :: * -> *).
MonadPlus 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
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shape -> m Shape
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Shape -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Shape -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Shape -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Shape -> [u]
gmapQr :: 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
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Shape -> r
gmapT :: (forall b. Data b => b -> b) -> Shape -> Shape
$cgmapT :: (forall b. Data b => b -> b) -> Shape -> Shape
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Shape)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Shape)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Shape)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Shape)
dataTypeOf :: Shape -> DataType
$cdataTypeOf :: Shape -> DataType
toConstr :: Shape -> Constr
$ctoConstr :: Shape -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Shape
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Shape
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shape -> c Shape
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shape -> c Shape
Data, Int -> Shape -> ShowS
[Shape] -> ShowS
Shape -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Shape] -> ShowS
$cshowList :: [Shape] -> ShowS
show :: Shape -> String
$cshow :: Shape -> String
showsPrec :: Int -> Shape -> ShowS
$cshowsPrec :: Int -> Shape -> ShowS
Show, Shape -> Shape -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Shape -> Shape -> Bool
$c/= :: Shape -> Shape -> Bool
== :: Shape -> Shape -> Bool
$c== :: Shape -> Shape -> Bool
Eq, Eq 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
min :: Shape -> Shape -> Shape
$cmin :: Shape -> Shape -> Shape
max :: Shape -> Shape -> Shape
$cmax :: Shape -> Shape -> Shape
>= :: Shape -> Shape -> Bool
$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
compare :: Shape -> Shape -> Ordering
$ccompare :: Shape -> Shape -> Ordering
Ord)

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