{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE Trustworthy #-}

module Futhark.Internalise.TypesValues
  ( -- * Internalising types
    internaliseReturnType,
    internaliseLambdaReturnType,
    internaliseEntryReturnType,
    internaliseType,
    internaliseParamTypes,
    internaliseLoopParamType,
    internalisePrimType,
    internalisedTypeSize,
    internaliseSumType,

    -- * Internalising values
    internalisePrimValue,
  )
where

import Control.Monad.Reader
import Control.Monad.State
import Data.List (delete, find, foldl')
import qualified Data.Map.Strict as M
import Data.Maybe
import Futhark.IR.SOACS as I
import Futhark.Internalise.Monad
import qualified Language.Futhark as E

internaliseUniqueness :: E.Uniqueness -> I.Uniqueness
internaliseUniqueness :: Uniqueness -> Uniqueness
internaliseUniqueness Uniqueness
E.Nonunique = Uniqueness
I.Nonunique
internaliseUniqueness Uniqueness
E.Unique = Uniqueness
I.Unique

newtype TypeState = TypeState {TypeState -> Int
typeCounter :: Int}

newtype InternaliseTypeM a
  = InternaliseTypeM (StateT TypeState InternaliseM a)
  deriving (a -> InternaliseTypeM b -> InternaliseTypeM a
(a -> b) -> InternaliseTypeM a -> InternaliseTypeM b
(forall a b. (a -> b) -> InternaliseTypeM a -> InternaliseTypeM b)
-> (forall a b. a -> InternaliseTypeM b -> InternaliseTypeM a)
-> Functor InternaliseTypeM
forall a b. a -> InternaliseTypeM b -> InternaliseTypeM a
forall a b. (a -> b) -> InternaliseTypeM a -> InternaliseTypeM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> InternaliseTypeM b -> InternaliseTypeM a
$c<$ :: forall a b. a -> InternaliseTypeM b -> InternaliseTypeM a
fmap :: (a -> b) -> InternaliseTypeM a -> InternaliseTypeM b
$cfmap :: forall a b. (a -> b) -> InternaliseTypeM a -> InternaliseTypeM b
Functor, Functor InternaliseTypeM
a -> InternaliseTypeM a
Functor InternaliseTypeM
-> (forall a. a -> InternaliseTypeM a)
-> (forall a b.
    InternaliseTypeM (a -> b)
    -> InternaliseTypeM a -> InternaliseTypeM b)
-> (forall a b c.
    (a -> b -> c)
    -> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c)
-> (forall a b.
    InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b)
-> (forall a b.
    InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a)
-> Applicative InternaliseTypeM
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a
InternaliseTypeM (a -> b)
-> InternaliseTypeM a -> InternaliseTypeM b
(a -> b -> c)
-> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c
forall a. a -> InternaliseTypeM a
forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a
forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
forall a b.
InternaliseTypeM (a -> b)
-> InternaliseTypeM a -> InternaliseTypeM b
forall a b c.
(a -> b -> c)
-> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a
$c<* :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a
*> :: InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
$c*> :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
liftA2 :: (a -> b -> c)
-> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c
$cliftA2 :: forall a b c.
(a -> b -> c)
-> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c
<*> :: InternaliseTypeM (a -> b)
-> InternaliseTypeM a -> InternaliseTypeM b
$c<*> :: forall a b.
InternaliseTypeM (a -> b)
-> InternaliseTypeM a -> InternaliseTypeM b
pure :: a -> InternaliseTypeM a
$cpure :: forall a. a -> InternaliseTypeM a
$cp1Applicative :: Functor InternaliseTypeM
Applicative, Applicative InternaliseTypeM
a -> InternaliseTypeM a
Applicative InternaliseTypeM
-> (forall a b.
    InternaliseTypeM a
    -> (a -> InternaliseTypeM b) -> InternaliseTypeM b)
-> (forall a b.
    InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b)
-> (forall a. a -> InternaliseTypeM a)
-> Monad InternaliseTypeM
InternaliseTypeM a
-> (a -> InternaliseTypeM b) -> InternaliseTypeM b
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
forall a. a -> InternaliseTypeM a
forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
forall a b.
InternaliseTypeM a
-> (a -> InternaliseTypeM b) -> InternaliseTypeM b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> InternaliseTypeM a
$creturn :: forall a. a -> InternaliseTypeM a
>> :: InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
$c>> :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
>>= :: InternaliseTypeM a
-> (a -> InternaliseTypeM b) -> InternaliseTypeM b
$c>>= :: forall a b.
InternaliseTypeM a
-> (a -> InternaliseTypeM b) -> InternaliseTypeM b
$cp1Monad :: Applicative InternaliseTypeM
Monad, MonadState TypeState)

liftInternaliseM :: InternaliseM a -> InternaliseTypeM a
liftInternaliseM :: InternaliseM a -> InternaliseTypeM a
liftInternaliseM = StateT TypeState InternaliseM a -> InternaliseTypeM a
forall a. StateT TypeState InternaliseM a -> InternaliseTypeM a
InternaliseTypeM (StateT TypeState InternaliseM a -> InternaliseTypeM a)
-> (InternaliseM a -> StateT TypeState InternaliseM a)
-> InternaliseM a
-> InternaliseTypeM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InternaliseM a -> StateT TypeState InternaliseM a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

runInternaliseTypeM :: InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM :: InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM = [VName] -> InternaliseTypeM a -> InternaliseM a
forall a. [VName] -> InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM' [VName]
forall a. Monoid a => a
mempty

runInternaliseTypeM' :: [VName] -> InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM' :: [VName] -> InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM' [VName]
exts (InternaliseTypeM StateT TypeState InternaliseM a
m) = StateT TypeState InternaliseM a -> TypeState -> InternaliseM a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT TypeState InternaliseM a
m (TypeState -> InternaliseM a) -> TypeState -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ Int -> TypeState
TypeState ([VName] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [VName]
exts)

internaliseParamTypes ::
  [E.TypeBase (E.DimDecl VName) ()] ->
  InternaliseM [[I.TypeBase Shape Uniqueness]]
internaliseParamTypes :: [TypeBase (DimDecl VName) ()]
-> InternaliseM [[TypeBase Shape Uniqueness]]
internaliseParamTypes [TypeBase (DimDecl VName) ()]
ts =
  InternaliseTypeM [[TypeBase Shape Uniqueness]]
-> InternaliseM [[TypeBase Shape Uniqueness]]
forall a. InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM (InternaliseTypeM [[TypeBase Shape Uniqueness]]
 -> InternaliseM [[TypeBase Shape Uniqueness]])
-> InternaliseTypeM [[TypeBase Shape Uniqueness]]
-> InternaliseM [[TypeBase Shape Uniqueness]]
forall a b. (a -> b) -> a -> b
$ (TypeBase (DimDecl VName) ()
 -> InternaliseTypeM [TypeBase Shape Uniqueness])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM [[TypeBase Shape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (([TypeBase ExtShape Uniqueness] -> [TypeBase Shape Uniqueness])
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase Shape Uniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TypeBase ExtShape Uniqueness -> TypeBase Shape Uniqueness)
-> [TypeBase ExtShape Uniqueness] -> [TypeBase Shape Uniqueness]
forall a b. (a -> b) -> [a] -> [b]
map TypeBase ExtShape Uniqueness -> TypeBase Shape Uniqueness
forall u. TypeBase ExtShape u -> TypeBase Shape u
onType) (InternaliseTypeM [TypeBase ExtShape Uniqueness]
 -> InternaliseTypeM [TypeBase Shape Uniqueness])
-> (TypeBase (DimDecl VName) ()
    -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase Shape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
forall a. Monoid a => a
mempty) [TypeBase (DimDecl VName) ()]
ts
  where
    onType :: TypeBase ExtShape u -> TypeBase Shape u
onType = TypeBase Shape u -> Maybe (TypeBase Shape u) -> TypeBase Shape u
forall a. a -> Maybe a -> a
fromMaybe TypeBase Shape u
forall a. a
bad (Maybe (TypeBase Shape u) -> TypeBase Shape u)
-> (TypeBase ExtShape u -> Maybe (TypeBase Shape u))
-> TypeBase ExtShape u
-> TypeBase Shape u
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeBase ExtShape u -> Maybe (TypeBase Shape u)
forall u. TypeBase ExtShape u -> Maybe (TypeBase Shape u)
hasStaticShape
    bad :: a
bad = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
"internaliseParamTypes: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [TypeBase (DimDecl VName) ()] -> [Char]
forall a. Pretty a => a -> [Char]
pretty [TypeBase (DimDecl VName) ()]
ts

-- We need to fix up the arrays for any Acc return values or loop
-- parameters.  We look at the concrete types for this, since the Acc
-- parameter name in the second list will just be something we made up.
fixupTypes :: [TypeBase shape1 u1] -> [TypeBase shape2 u2] -> [TypeBase shape2 u2]
fixupTypes :: [TypeBase shape1 u1]
-> [TypeBase shape2 u2] -> [TypeBase shape2 u2]
fixupTypes = (TypeBase shape1 u1 -> TypeBase shape2 u2 -> TypeBase shape2 u2)
-> [TypeBase shape1 u1]
-> [TypeBase shape2 u2]
-> [TypeBase shape2 u2]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TypeBase shape1 u1 -> TypeBase shape2 u2 -> TypeBase shape2 u2
forall shape u shape u.
TypeBase shape u -> TypeBase shape u -> TypeBase shape u
fixup
  where
    fixup :: TypeBase shape u -> TypeBase shape u -> TypeBase shape u
fixup (Acc VName
acc Shape
ispace [Type]
ts u
_) (Acc VName
_ Shape
_ [Type]
_ u
u2) = VName -> Shape -> [Type] -> u -> TypeBase shape u
forall shape u. VName -> Shape -> [Type] -> u -> TypeBase shape u
Acc VName
acc Shape
ispace [Type]
ts u
u2
    fixup TypeBase shape u
_ TypeBase shape u
t = TypeBase shape u
t

internaliseLoopParamType ::
  E.TypeBase (E.DimDecl VName) () ->
  [TypeBase shape u] ->
  InternaliseM [I.TypeBase Shape Uniqueness]
internaliseLoopParamType :: TypeBase (DimDecl VName) ()
-> [TypeBase shape u] -> InternaliseM [TypeBase Shape Uniqueness]
internaliseLoopParamType TypeBase (DimDecl VName) ()
et [TypeBase shape u]
ts =
  [TypeBase shape u]
-> [TypeBase Shape Uniqueness] -> [TypeBase Shape Uniqueness]
forall shape1 u1 shape2 u2.
[TypeBase shape1 u1]
-> [TypeBase shape2 u2] -> [TypeBase shape2 u2]
fixupTypes [TypeBase shape u]
ts ([TypeBase Shape Uniqueness] -> [TypeBase Shape Uniqueness])
-> ([[TypeBase Shape Uniqueness]] -> [TypeBase Shape Uniqueness])
-> [[TypeBase Shape Uniqueness]]
-> [TypeBase Shape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[TypeBase Shape Uniqueness]] -> [TypeBase Shape Uniqueness]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[TypeBase Shape Uniqueness]] -> [TypeBase Shape Uniqueness])
-> InternaliseM [[TypeBase Shape Uniqueness]]
-> InternaliseM [TypeBase Shape Uniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [TypeBase (DimDecl VName) ()]
-> InternaliseM [[TypeBase Shape Uniqueness]]
internaliseParamTypes [TypeBase (DimDecl VName) ()
et]

internaliseReturnType ::
  E.StructRetType ->
  [TypeBase shape u] ->
  InternaliseM [I.TypeBase ExtShape Uniqueness]
internaliseReturnType :: StructRetType
-> [TypeBase shape u]
-> InternaliseM [TypeBase ExtShape Uniqueness]
internaliseReturnType (E.RetType [VName]
dims TypeBase (DimDecl VName) ()
et) [TypeBase shape u]
ts =
  [TypeBase shape u]
-> [TypeBase ExtShape Uniqueness] -> [TypeBase ExtShape Uniqueness]
forall shape1 u1 shape2 u2.
[TypeBase shape1 u1]
-> [TypeBase shape2 u2] -> [TypeBase shape2 u2]
fixupTypes [TypeBase shape u]
ts ([TypeBase ExtShape Uniqueness] -> [TypeBase ExtShape Uniqueness])
-> InternaliseM [TypeBase ExtShape Uniqueness]
-> InternaliseM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VName]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
-> InternaliseM [TypeBase ExtShape Uniqueness]
forall a. [VName] -> InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM' [VName]
dims (Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts TypeBase (DimDecl VName) ()
et)
  where
    exts :: Map VName Int
exts = [(VName, Int)] -> Map VName Int
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(VName, Int)] -> Map VName Int)
-> [(VName, Int)] -> Map VName Int
forall a b. (a -> b) -> a -> b
$ [VName] -> [Int] -> [(VName, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VName]
dims [Int
0 ..]

internaliseLambdaReturnType ::
  E.TypeBase (E.DimDecl VName) () ->
  [TypeBase shape u] ->
  InternaliseM [I.TypeBase Shape NoUniqueness]
internaliseLambdaReturnType :: TypeBase (DimDecl VName) ()
-> [TypeBase shape u] -> InternaliseM [Type]
internaliseLambdaReturnType TypeBase (DimDecl VName) ()
et [TypeBase shape u]
ts =
  (TypeBase Shape Uniqueness -> Type)
-> [TypeBase Shape Uniqueness] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map TypeBase Shape Uniqueness -> Type
forall shape.
TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
fromDecl ([TypeBase Shape Uniqueness] -> [Type])
-> InternaliseM [TypeBase Shape Uniqueness] -> InternaliseM [Type]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TypeBase (DimDecl VName) ()
-> [TypeBase shape u] -> InternaliseM [TypeBase Shape Uniqueness]
forall shape u.
TypeBase (DimDecl VName) ()
-> [TypeBase shape u] -> InternaliseM [TypeBase Shape Uniqueness]
internaliseLoopParamType TypeBase (DimDecl VName) ()
et [TypeBase shape u]
ts

-- | As 'internaliseReturnType', but returns components of a top-level
-- tuple type piecemeal.
internaliseEntryReturnType ::
  E.StructRetType ->
  InternaliseM [[I.TypeBase ExtShape Uniqueness]]
internaliseEntryReturnType :: StructRetType -> InternaliseM [[TypeBase ExtShape Uniqueness]]
internaliseEntryReturnType (E.RetType [VName]
dims TypeBase (DimDecl VName) ()
et) =
  [VName]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
-> InternaliseM [[TypeBase ExtShape Uniqueness]]
forall a. [VName] -> InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM' [VName]
dims (InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
 -> InternaliseM [[TypeBase ExtShape Uniqueness]])
-> ([TypeBase (DimDecl VName) ()]
    -> InternaliseTypeM [[TypeBase ExtShape Uniqueness]])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseM [[TypeBase ExtShape Uniqueness]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeBase (DimDecl VName) ()
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts) ([TypeBase (DimDecl VName) ()]
 -> InternaliseM [[TypeBase ExtShape Uniqueness]])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseM [[TypeBase ExtShape Uniqueness]]
forall a b. (a -> b) -> a -> b
$
    case TypeBase (DimDecl VName) () -> Maybe [TypeBase (DimDecl VName) ()]
forall dim as. TypeBase dim as -> Maybe [TypeBase dim as]
E.isTupleRecord TypeBase (DimDecl VName) ()
et of
      Just [TypeBase (DimDecl VName) ()]
ets | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [TypeBase (DimDecl VName) ()] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TypeBase (DimDecl VName) ()]
ets -> [TypeBase (DimDecl VName) ()]
ets
      Maybe [TypeBase (DimDecl VName) ()]
_ -> [TypeBase (DimDecl VName) ()
et]
  where
    exts :: Map VName Int
exts = [(VName, Int)] -> Map VName Int
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(VName, Int)] -> Map VName Int)
-> [(VName, Int)] -> Map VName Int
forall a b. (a -> b) -> a -> b
$ [VName] -> [Int] -> [(VName, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VName]
dims [Int
0 ..]

internaliseType ::
  E.TypeBase (E.DimDecl VName) () ->
  InternaliseM [I.TypeBase I.ExtShape Uniqueness]
internaliseType :: TypeBase (DimDecl VName) ()
-> InternaliseM [TypeBase ExtShape Uniqueness]
internaliseType = InternaliseTypeM [TypeBase ExtShape Uniqueness]
-> InternaliseM [TypeBase ExtShape Uniqueness]
forall a. InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM (InternaliseTypeM [TypeBase ExtShape Uniqueness]
 -> InternaliseM [TypeBase ExtShape Uniqueness])
-> (TypeBase (DimDecl VName) ()
    -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> TypeBase (DimDecl VName) ()
-> InternaliseM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
forall a. Monoid a => a
mempty

newId :: InternaliseTypeM Int
newId :: InternaliseTypeM Int
newId = do
  Int
i <- (TypeState -> Int) -> InternaliseTypeM Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets TypeState -> Int
typeCounter
  (TypeState -> TypeState) -> InternaliseTypeM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((TypeState -> TypeState) -> InternaliseTypeM ())
-> (TypeState -> TypeState) -> InternaliseTypeM ()
forall a b. (a -> b) -> a -> b
$ \TypeState
s -> TypeState
s {typeCounter :: Int
typeCounter = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1}
  Int -> InternaliseTypeM Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
i

internaliseDim ::
  M.Map VName Int ->
  E.DimDecl VName ->
  InternaliseTypeM ExtSize
internaliseDim :: Map VName Int -> DimDecl VName -> InternaliseTypeM ExtSize
internaliseDim Map VName Int
exts DimDecl VName
d =
  case DimDecl VName
d of
    E.AnyDim Maybe VName
_ -> Int -> ExtSize
forall a. Int -> Ext a
Ext (Int -> ExtSize)
-> InternaliseTypeM Int -> InternaliseTypeM ExtSize
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InternaliseTypeM Int
newId
    E.ConstDim Int
n -> ExtSize -> InternaliseTypeM ExtSize
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExtSize -> InternaliseTypeM ExtSize)
-> ExtSize -> InternaliseTypeM ExtSize
forall a b. (a -> b) -> a -> b
$ SubExp -> ExtSize
forall a. a -> Ext a
Free (SubExp -> ExtSize) -> SubExp -> ExtSize
forall a b. (a -> b) -> a -> b
$ IntType -> Integer -> SubExp
intConst IntType
I.Int64 (Integer -> SubExp) -> Integer -> SubExp
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
n
    E.NamedDim QualName VName
name -> QualName VName -> InternaliseTypeM ExtSize
namedDim QualName VName
name
  where
    namedDim :: QualName VName -> InternaliseTypeM ExtSize
namedDim (E.QualName [VName]
_ VName
name)
      | Just Int
x <- VName
name VName -> Map VName Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
`M.lookup` Map VName Int
exts = ExtSize -> InternaliseTypeM ExtSize
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExtSize -> InternaliseTypeM ExtSize)
-> ExtSize -> InternaliseTypeM ExtSize
forall a b. (a -> b) -> a -> b
$ Int -> ExtSize
forall a. Int -> Ext a
I.Ext Int
x
      | Bool
otherwise = do
          Maybe [SubExp]
subst <- InternaliseM (Maybe [SubExp]) -> InternaliseTypeM (Maybe [SubExp])
forall a. InternaliseM a -> InternaliseTypeM a
liftInternaliseM (InternaliseM (Maybe [SubExp])
 -> InternaliseTypeM (Maybe [SubExp]))
-> InternaliseM (Maybe [SubExp])
-> InternaliseTypeM (Maybe [SubExp])
forall a b. (a -> b) -> a -> b
$ VName -> InternaliseM (Maybe [SubExp])
lookupSubst VName
name
          case Maybe [SubExp]
subst of
            Just [SubExp
v] -> ExtSize -> InternaliseTypeM ExtSize
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExtSize -> InternaliseTypeM ExtSize)
-> ExtSize -> InternaliseTypeM ExtSize
forall a b. (a -> b) -> a -> b
$ SubExp -> ExtSize
forall a. a -> Ext a
I.Free SubExp
v
            Maybe [SubExp]
_ -> ExtSize -> InternaliseTypeM ExtSize
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExtSize -> InternaliseTypeM ExtSize)
-> ExtSize -> InternaliseTypeM ExtSize
forall a b. (a -> b) -> a -> b
$ SubExp -> ExtSize
forall a. a -> Ext a
I.Free (SubExp -> ExtSize) -> SubExp -> ExtSize
forall a b. (a -> b) -> a -> b
$ VName -> SubExp
I.Var VName
name

internaliseTypeM ::
  M.Map VName Int ->
  E.StructType ->
  InternaliseTypeM [I.TypeBase ExtShape Uniqueness]
internaliseTypeM :: Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts TypeBase (DimDecl VName) ()
orig_t =
  case TypeBase (DimDecl VName) ()
orig_t of
    E.Array ()
_ Uniqueness
u ScalarTypeBase (DimDecl VName) ()
et ShapeDecl (DimDecl VName)
shape -> do
      [ExtSize]
dims <- ShapeDecl (DimDecl VName) -> InternaliseTypeM [ExtSize]
internaliseShape ShapeDecl (DimDecl VName)
shape
      [TypeBase ExtShape Uniqueness]
ets <- Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts (TypeBase (DimDecl VName) ()
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall a b. (a -> b) -> a -> b
$ ScalarTypeBase (DimDecl VName) () -> TypeBase (DimDecl VName) ()
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
E.Scalar ScalarTypeBase (DimDecl VName) ()
et
      [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [TypeBase ExtShape Uniqueness
-> ExtShape -> Uniqueness -> TypeBase ExtShape Uniqueness
forall shape u_unused u.
ArrayShape shape =>
TypeBase shape u_unused -> shape -> u -> TypeBase shape u
I.arrayOf TypeBase ExtShape Uniqueness
et' ([ExtSize] -> ExtShape
forall d. [d] -> ShapeBase d
Shape [ExtSize]
dims) (Uniqueness -> TypeBase ExtShape Uniqueness)
-> Uniqueness -> TypeBase ExtShape Uniqueness
forall a b. (a -> b) -> a -> b
$ Uniqueness -> Uniqueness
internaliseUniqueness Uniqueness
u | TypeBase ExtShape Uniqueness
et' <- [TypeBase ExtShape Uniqueness]
ets]
    E.Scalar (E.Prim PrimType
bt) ->
      [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [PrimType -> TypeBase ExtShape Uniqueness
forall shape u. PrimType -> TypeBase shape u
I.Prim (PrimType -> TypeBase ExtShape Uniqueness)
-> PrimType -> TypeBase ExtShape Uniqueness
forall a b. (a -> b) -> a -> b
$ PrimType -> PrimType
internalisePrimType PrimType
bt]
    E.Scalar (E.Record Map Name (TypeBase (DimDecl VName) ())
ets)
      -- XXX: we map empty records to units, because otherwise
      -- arrays of unit will lose their sizes.
      | Map Name (TypeBase (DimDecl VName) ()) -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Map Name (TypeBase (DimDecl VName) ())
ets -> [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [PrimType -> TypeBase ExtShape Uniqueness
forall shape u. PrimType -> TypeBase shape u
I.Prim PrimType
I.Unit]
      | Bool
otherwise ->
          [[TypeBase ExtShape Uniqueness]] -> [TypeBase ExtShape Uniqueness]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[TypeBase ExtShape Uniqueness]]
 -> [TypeBase ExtShape Uniqueness])
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Name, TypeBase (DimDecl VName) ())
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [(Name, TypeBase (DimDecl VName) ())]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts (TypeBase (DimDecl VName) ()
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> ((Name, TypeBase (DimDecl VName) ())
    -> TypeBase (DimDecl VName) ())
-> (Name, TypeBase (DimDecl VName) ())
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, TypeBase (DimDecl VName) ()) -> TypeBase (DimDecl VName) ()
forall a b. (a, b) -> b
snd) (Map Name (TypeBase (DimDecl VName) ())
-> [(Name, TypeBase (DimDecl VName) ())]
forall a. Map Name a -> [(Name, a)]
E.sortFields Map Name (TypeBase (DimDecl VName) ())
ets)
    E.Scalar (E.TypeVar ()
_ Uniqueness
u TypeName
tn [E.TypeArgType TypeBase (DimDecl VName) ()
arr_t SrcLoc
_])
      | VName -> Int
baseTag (TypeName -> VName
E.typeLeaf TypeName
tn) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
E.maxIntrinsicTag,
        VName -> [Char]
baseString (TypeName -> VName
E.typeLeaf TypeName
tn) [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"acc" -> do
          [Type]
ts <- (TypeBase ExtShape Uniqueness -> Type)
-> [TypeBase ExtShape Uniqueness] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (TypeBase Shape Uniqueness -> Type
forall shape.
TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
fromDecl (TypeBase Shape Uniqueness -> Type)
-> (TypeBase ExtShape Uniqueness -> TypeBase Shape Uniqueness)
-> TypeBase ExtShape Uniqueness
-> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeBase ExtShape Uniqueness -> TypeBase Shape Uniqueness
forall u. TypeBase ExtShape u -> TypeBase Shape u
onAccType) ([TypeBase ExtShape Uniqueness] -> [Type])
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [Type]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts TypeBase (DimDecl VName) ()
arr_t
          VName
acc_param <- InternaliseM VName -> InternaliseTypeM VName
forall a. InternaliseM a -> InternaliseTypeM a
liftInternaliseM (InternaliseM VName -> InternaliseTypeM VName)
-> InternaliseM VName -> InternaliseTypeM VName
forall a b. (a -> b) -> a -> b
$ [Char] -> InternaliseM VName
forall (m :: * -> *). MonadFreshNames m => [Char] -> m VName
newVName [Char]
"acc_cert"
          let acc_t :: TypeBase shape Uniqueness
acc_t = VName -> Shape -> [Type] -> Uniqueness -> TypeBase shape Uniqueness
forall shape u. VName -> Shape -> [Type] -> u -> TypeBase shape u
Acc VName
acc_param ([SubExp] -> Shape
forall d. [d] -> ShapeBase d
Shape [Int -> [Type] -> SubExp
forall u. Int -> [TypeBase Shape u] -> SubExp
arraysSize Int
0 [Type]
ts]) ((Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
forall u. TypeBase Shape u -> TypeBase Shape u
rowType [Type]
ts) (Uniqueness -> TypeBase shape Uniqueness)
-> Uniqueness -> TypeBase shape Uniqueness
forall a b. (a -> b) -> a -> b
$ Uniqueness -> Uniqueness
internaliseUniqueness Uniqueness
u
          [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [TypeBase ExtShape Uniqueness
forall shape. TypeBase shape Uniqueness
acc_t]
    E.Scalar E.TypeVar {} ->
      [Char] -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall a. HasCallStack => [Char] -> a
error [Char]
"internaliseTypeM: cannot handle type variable."
    E.Scalar E.Arrow {} ->
      [Char] -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall a. HasCallStack => [Char] -> a
error ([Char] -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [Char] -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall a b. (a -> b) -> a -> b
$ [Char]
"internaliseTypeM: cannot handle function type: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ TypeBase (DimDecl VName) () -> [Char]
forall a. Pretty a => a -> [Char]
pretty TypeBase (DimDecl VName) ()
orig_t
    E.Scalar (E.Sum Map Name [TypeBase (DimDecl VName) ()]
cs) -> do
      ([TypeBase ExtShape Uniqueness]
ts, Map Name (Int, [Int])
_) <-
        Map Name [TypeBase ExtShape Uniqueness]
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
internaliseConstructors
          (Map Name [TypeBase ExtShape Uniqueness]
 -> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int])))
-> InternaliseTypeM (Map Name [TypeBase ExtShape Uniqueness])
-> InternaliseTypeM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([TypeBase (DimDecl VName) ()]
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> Map Name [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM (Map Name [TypeBase ExtShape Uniqueness])
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (([[TypeBase ExtShape Uniqueness]]
 -> [TypeBase ExtShape Uniqueness])
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[TypeBase ExtShape Uniqueness]] -> [TypeBase ExtShape Uniqueness]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> ([TypeBase (DimDecl VName) ()]
    -> InternaliseTypeM [[TypeBase ExtShape Uniqueness]])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeBase (DimDecl VName) ()
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
exts)) Map Name [TypeBase (DimDecl VName) ()]
cs
      [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TypeBase ExtShape Uniqueness]
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall a b. (a -> b) -> a -> b
$ PrimType -> TypeBase ExtShape Uniqueness
forall shape u. PrimType -> TypeBase shape u
I.Prim (IntType -> PrimType
I.IntType IntType
I.Int8) TypeBase ExtShape Uniqueness
-> [TypeBase ExtShape Uniqueness] -> [TypeBase ExtShape Uniqueness]
forall a. a -> [a] -> [a]
: [TypeBase ExtShape Uniqueness]
ts
  where
    internaliseShape :: ShapeDecl (DimDecl VName) -> InternaliseTypeM [ExtSize]
internaliseShape = (DimDecl VName -> InternaliseTypeM ExtSize)
-> [DimDecl VName] -> InternaliseTypeM [ExtSize]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map VName Int -> DimDecl VName -> InternaliseTypeM ExtSize
internaliseDim Map VName Int
exts) ([DimDecl VName] -> InternaliseTypeM [ExtSize])
-> (ShapeDecl (DimDecl VName) -> [DimDecl VName])
-> ShapeDecl (DimDecl VName)
-> InternaliseTypeM [ExtSize]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShapeDecl (DimDecl VName) -> [DimDecl VName]
forall dim. ShapeDecl dim -> [dim]
E.shapeDims

    onAccType :: TypeBase ExtShape u -> TypeBase Shape u
onAccType = TypeBase Shape u -> Maybe (TypeBase Shape u) -> TypeBase Shape u
forall a. a -> Maybe a -> a
fromMaybe TypeBase Shape u
forall a. a
bad (Maybe (TypeBase Shape u) -> TypeBase Shape u)
-> (TypeBase ExtShape u -> Maybe (TypeBase Shape u))
-> TypeBase ExtShape u
-> TypeBase Shape u
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeBase ExtShape u -> Maybe (TypeBase Shape u)
forall u. TypeBase ExtShape u -> Maybe (TypeBase Shape u)
hasStaticShape
    bad :: a
bad = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
"internaliseTypeM Acc: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ TypeBase (DimDecl VName) () -> [Char]
forall a. Pretty a => a -> [Char]
pretty TypeBase (DimDecl VName) ()
orig_t

internaliseConstructors ::
  M.Map Name [I.TypeBase ExtShape Uniqueness] ->
  ( [I.TypeBase ExtShape Uniqueness],
    M.Map Name (Int, [Int])
  )
internaliseConstructors :: Map Name [TypeBase ExtShape Uniqueness]
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
internaliseConstructors Map Name [TypeBase ExtShape Uniqueness]
cs =
  (([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
 -> ((Name, [TypeBase ExtShape Uniqueness]), Int)
 -> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int])))
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
-> [((Name, [TypeBase ExtShape Uniqueness]), Int)]
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
-> ((Name, [TypeBase ExtShape Uniqueness]), Int)
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall k (t :: * -> *) shape a.
(Ord k, Foldable t, Eq shape) =>
([TypeBase shape Uniqueness], Map k (a, [Int]))
-> ((k, t (TypeBase shape Uniqueness)), a)
-> ([TypeBase shape Uniqueness], Map k (a, [Int]))
onConstructor ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall a. Monoid a => a
mempty ([((Name, [TypeBase ExtShape Uniqueness]), Int)]
 -> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int])))
-> [((Name, [TypeBase ExtShape Uniqueness]), Int)]
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall a b. (a -> b) -> a -> b
$ [(Name, [TypeBase ExtShape Uniqueness])]
-> [Int] -> [((Name, [TypeBase ExtShape Uniqueness]), Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Map Name [TypeBase ExtShape Uniqueness]
-> [(Name, [TypeBase ExtShape Uniqueness])]
forall a. Map Name a -> [(Name, a)]
E.sortConstrs Map Name [TypeBase ExtShape Uniqueness]
cs) [Int
0 ..]
  where
    onConstructor :: ([TypeBase shape Uniqueness], Map k (a, [Int]))
-> ((k, t (TypeBase shape Uniqueness)), a)
-> ([TypeBase shape Uniqueness], Map k (a, [Int]))
onConstructor ([TypeBase shape Uniqueness]
ts, Map k (a, [Int])
mapping) ((k
c, t (TypeBase shape Uniqueness)
c_ts), a
i) =
      let ([(TypeBase shape NoUniqueness, Int)]
_, [Int]
js, [TypeBase shape Uniqueness]
new_ts) =
            (([(TypeBase shape NoUniqueness, Int)], [Int],
  [TypeBase shape Uniqueness])
 -> TypeBase shape Uniqueness
 -> ([(TypeBase shape NoUniqueness, Int)], [Int],
     [TypeBase shape Uniqueness]))
-> ([(TypeBase shape NoUniqueness, Int)], [Int],
    [TypeBase shape Uniqueness])
-> t (TypeBase shape Uniqueness)
-> ([(TypeBase shape NoUniqueness, Int)], [Int],
    [TypeBase shape Uniqueness])
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ([(TypeBase shape NoUniqueness, Int)], [Int],
 [TypeBase shape Uniqueness])
-> TypeBase shape Uniqueness
-> ([(TypeBase shape NoUniqueness, Int)], [Int],
    [TypeBase shape Uniqueness])
forall shape.
Eq shape =>
([(TypeBase shape NoUniqueness, Int)], [Int],
 [TypeBase shape Uniqueness])
-> TypeBase shape Uniqueness
-> ([(TypeBase shape NoUniqueness, Int)], [Int],
    [TypeBase shape Uniqueness])
f ([TypeBase shape NoUniqueness]
-> [Int] -> [(TypeBase shape NoUniqueness, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip ((TypeBase shape Uniqueness -> TypeBase shape NoUniqueness)
-> [TypeBase shape Uniqueness] -> [TypeBase shape NoUniqueness]
forall a b. (a -> b) -> [a] -> [b]
map TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
forall shape.
TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
fromDecl [TypeBase shape Uniqueness]
ts) [Int
0 ..], [Int]
forall a. Monoid a => a
mempty, [TypeBase shape Uniqueness]
forall a. Monoid a => a
mempty) t (TypeBase shape Uniqueness)
c_ts
       in ([TypeBase shape Uniqueness]
ts [TypeBase shape Uniqueness]
-> [TypeBase shape Uniqueness] -> [TypeBase shape Uniqueness]
forall a. [a] -> [a] -> [a]
++ [TypeBase shape Uniqueness]
new_ts, k -> (a, [Int]) -> Map k (a, [Int]) -> Map k (a, [Int])
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert k
c (a
i, [Int]
js) Map k (a, [Int])
mapping)
      where
        f :: ([(TypeBase shape NoUniqueness, Int)], [Int],
 [TypeBase shape Uniqueness])
-> TypeBase shape Uniqueness
-> ([(TypeBase shape NoUniqueness, Int)], [Int],
    [TypeBase shape Uniqueness])
f ([(TypeBase shape NoUniqueness, Int)]
ts', [Int]
js, [TypeBase shape Uniqueness]
new_ts) TypeBase shape Uniqueness
t
          | Just (TypeBase shape NoUniqueness
_, Int
j) <- ((TypeBase shape NoUniqueness, Int) -> Bool)
-> [(TypeBase shape NoUniqueness, Int)]
-> Maybe (TypeBase shape NoUniqueness, Int)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((TypeBase shape NoUniqueness -> TypeBase shape NoUniqueness -> Bool
forall a. Eq a => a -> a -> Bool
== TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
forall shape.
TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
fromDecl TypeBase shape Uniqueness
t) (TypeBase shape NoUniqueness -> Bool)
-> ((TypeBase shape NoUniqueness, Int)
    -> TypeBase shape NoUniqueness)
-> (TypeBase shape NoUniqueness, Int)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeBase shape NoUniqueness, Int) -> TypeBase shape NoUniqueness
forall a b. (a, b) -> a
fst) [(TypeBase shape NoUniqueness, Int)]
ts' =
              ( (TypeBase shape NoUniqueness, Int)
-> [(TypeBase shape NoUniqueness, Int)]
-> [(TypeBase shape NoUniqueness, Int)]
forall a. Eq a => a -> [a] -> [a]
delete (TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
forall shape.
TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
fromDecl TypeBase shape Uniqueness
t, Int
j) [(TypeBase shape NoUniqueness, Int)]
ts',
                [Int]
js [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int
j],
                [TypeBase shape Uniqueness]
new_ts
              )
          | Bool
otherwise =
              ( [(TypeBase shape NoUniqueness, Int)]
ts',
                [Int]
js [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [[TypeBase shape Uniqueness] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeBase shape Uniqueness]
ts Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [TypeBase shape Uniqueness] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeBase shape Uniqueness]
new_ts],
                [TypeBase shape Uniqueness]
new_ts [TypeBase shape Uniqueness]
-> [TypeBase shape Uniqueness] -> [TypeBase shape Uniqueness]
forall a. [a] -> [a] -> [a]
++ [TypeBase shape Uniqueness
t]
              )

internaliseSumType ::
  M.Map Name [E.StructType] ->
  InternaliseM
    ( [I.TypeBase ExtShape Uniqueness],
      M.Map Name (Int, [Int])
    )
internaliseSumType :: Map Name [TypeBase (DimDecl VName) ()]
-> InternaliseM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
internaliseSumType Map Name [TypeBase (DimDecl VName) ()]
cs =
  InternaliseTypeM
  ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
-> InternaliseM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall a. InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM (InternaliseTypeM
   ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
 -> InternaliseM
      ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int])))
-> InternaliseTypeM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
-> InternaliseM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall a b. (a -> b) -> a -> b
$
    Map Name [TypeBase ExtShape Uniqueness]
-> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
internaliseConstructors
      (Map Name [TypeBase ExtShape Uniqueness]
 -> ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int])))
-> InternaliseTypeM (Map Name [TypeBase ExtShape Uniqueness])
-> InternaliseTypeM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([TypeBase (DimDecl VName) ()]
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> Map Name [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM (Map Name [TypeBase ExtShape Uniqueness])
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (([[TypeBase ExtShape Uniqueness]]
 -> [TypeBase ExtShape Uniqueness])
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[TypeBase ExtShape Uniqueness]] -> [TypeBase ExtShape Uniqueness]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> ([TypeBase (DimDecl VName) ()]
    -> InternaliseTypeM [[TypeBase ExtShape Uniqueness]])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeBase (DimDecl VName) ()
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map VName Int
-> TypeBase (DimDecl VName) ()
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM Map VName Int
forall a. Monoid a => a
mempty)) Map Name [TypeBase (DimDecl VName) ()]
cs

-- | How many core language values are needed to represent one source
-- language value of the given type?
internalisedTypeSize :: E.TypeBase (E.DimDecl VName) als -> InternaliseM Int
-- A few special cases for performance.
internalisedTypeSize :: TypeBase (DimDecl VName) als -> InternaliseM Int
internalisedTypeSize (E.Scalar (E.Prim PrimType
_)) = Int -> InternaliseM Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
internalisedTypeSize (E.Array als
_ Uniqueness
_ (E.Prim PrimType
_) ShapeDecl (DimDecl VName)
_) = Int -> InternaliseM Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
internalisedTypeSize TypeBase (DimDecl VName) als
t = [TypeBase ExtShape Uniqueness] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([TypeBase ExtShape Uniqueness] -> Int)
-> InternaliseM [TypeBase ExtShape Uniqueness] -> InternaliseM Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TypeBase (DimDecl VName) ()
-> InternaliseM [TypeBase ExtShape Uniqueness]
internaliseType (TypeBase (DimDecl VName) als
t TypeBase (DimDecl VName) als -> () -> TypeBase (DimDecl VName) ()
forall dim asf ast. TypeBase dim asf -> ast -> TypeBase dim ast
`E.setAliases` ())

-- | Convert an external primitive to an internal primitive.
internalisePrimType :: E.PrimType -> I.PrimType
internalisePrimType :: PrimType -> PrimType
internalisePrimType (E.Signed IntType
t) = IntType -> PrimType
I.IntType IntType
t
internalisePrimType (E.Unsigned IntType
t) = IntType -> PrimType
I.IntType IntType
t
internalisePrimType (E.FloatType FloatType
t) = FloatType -> PrimType
I.FloatType FloatType
t
internalisePrimType PrimType
E.Bool = PrimType
I.Bool

-- | Convert an external primitive value to an internal primitive value.
internalisePrimValue :: E.PrimValue -> I.PrimValue
internalisePrimValue :: PrimValue -> PrimValue
internalisePrimValue (E.SignedValue IntValue
v) = IntValue -> PrimValue
I.IntValue IntValue
v
internalisePrimValue (E.UnsignedValue IntValue
v) = IntValue -> PrimValue
I.IntValue IntValue
v
internalisePrimValue (E.FloatValue FloatValue
v) = FloatValue -> PrimValue
I.FloatValue FloatValue
v
internalisePrimValue (E.BoolValue Bool
b) = Bool -> PrimValue
I.BoolValue Bool
b