{-# 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

type TypeState = Int

newtype InternaliseTypeM a
  = InternaliseTypeM (StateT TypeState InternaliseM a)
  deriving ((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
<$ :: forall a b. a -> InternaliseTypeM b -> InternaliseTypeM a
$c<$ :: forall a b. a -> InternaliseTypeM b -> InternaliseTypeM a
fmap :: forall a b. (a -> b) -> InternaliseTypeM a -> InternaliseTypeM b
$cfmap :: forall a b. (a -> b) -> InternaliseTypeM a -> InternaliseTypeM b
Functor, Functor InternaliseTypeM
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
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
<* :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a
$c<* :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM a
*> :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
$c*> :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
liftA2 :: forall a b c.
(a -> b -> c)
-> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c
$cliftA2 :: forall a b c.
(a -> b -> c)
-> InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM c
<*> :: forall a b.
InternaliseTypeM (a -> b)
-> InternaliseTypeM a -> InternaliseTypeM b
$c<*> :: forall a b.
InternaliseTypeM (a -> b)
-> InternaliseTypeM a -> InternaliseTypeM b
pure :: forall a. a -> InternaliseTypeM a
$cpure :: forall a. a -> InternaliseTypeM a
Applicative, Applicative InternaliseTypeM
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
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 :: forall a. a -> InternaliseTypeM a
$creturn :: forall a. a -> InternaliseTypeM a
>> :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
$c>> :: forall a b.
InternaliseTypeM a -> InternaliseTypeM b -> InternaliseTypeM b
>>= :: forall a b.
InternaliseTypeM a
-> (a -> InternaliseTypeM b) -> InternaliseTypeM b
$c>>= :: forall a b.
InternaliseTypeM a
-> (a -> InternaliseTypeM b) -> InternaliseTypeM b
Monad, MonadState TypeState)

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

runInternaliseTypeM ::
  InternaliseTypeM a ->
  InternaliseM a
runInternaliseTypeM :: forall a. InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM (InternaliseTypeM StateT Int InternaliseM a
m) =
  StateT Int InternaliseM a -> Int -> InternaliseM a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT Int InternaliseM a
m Int
0

internaliseParamTypes ::
  [E.TypeBase (E.DimDecl VName) ()] ->
  InternaliseM [[I.TypeBase Shape Uniqueness]]
internaliseParamTypes :: [StructType] -> InternaliseM [[TypeBase Shape Uniqueness]]
internaliseParamTypes [StructType]
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
$ (StructType -> InternaliseTypeM [TypeBase Shape Uniqueness])
-> [StructType] -> 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])
-> (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> StructType
-> InternaliseTypeM [TypeBase Shape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM) [StructType]
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]
++ [StructType] -> [Char]
forall a. Pretty a => a -> [Char]
pretty [StructType]
ts

internaliseLoopParamType ::
  E.TypeBase (E.DimDecl VName) () ->
  InternaliseM [I.TypeBase Shape Uniqueness]
internaliseLoopParamType :: StructType -> InternaliseM [TypeBase Shape Uniqueness]
internaliseLoopParamType StructType
et =
  [[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
<$> [StructType] -> InternaliseM [[TypeBase Shape Uniqueness]]
internaliseParamTypes [StructType
et]

internaliseReturnType ::
  E.TypeBase (E.DimDecl VName) () ->
  InternaliseM [I.TypeBase ExtShape Uniqueness]
internaliseReturnType :: StructType -> InternaliseM [TypeBase ExtShape Uniqueness]
internaliseReturnType StructType
et =
  InternaliseTypeM [TypeBase ExtShape Uniqueness]
-> InternaliseM [TypeBase ExtShape Uniqueness]
forall a. InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM StructType
et)

internaliseLambdaReturnType ::
  E.TypeBase (E.DimDecl VName) () ->
  InternaliseM [I.TypeBase Shape NoUniqueness]
internaliseLambdaReturnType :: StructType -> InternaliseM [TypeBase Shape NoUniqueness]
internaliseLambdaReturnType = ([TypeBase Shape Uniqueness] -> [TypeBase Shape NoUniqueness])
-> InternaliseM [TypeBase Shape Uniqueness]
-> InternaliseM [TypeBase Shape NoUniqueness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((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) (InternaliseM [TypeBase Shape Uniqueness]
 -> InternaliseM [TypeBase Shape NoUniqueness])
-> (StructType -> InternaliseM [TypeBase Shape Uniqueness])
-> StructType
-> InternaliseM [TypeBase Shape NoUniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StructType -> InternaliseM [TypeBase Shape Uniqueness]
internaliseLoopParamType

-- | As 'internaliseReturnType', but returns components of a top-level
-- tuple type piecemeal.
internaliseEntryReturnType ::
  E.TypeBase (E.DimDecl VName) () ->
  InternaliseM [[I.TypeBase ExtShape Uniqueness]]
internaliseEntryReturnType :: StructType -> InternaliseM [[TypeBase ExtShape Uniqueness]]
internaliseEntryReturnType StructType
et =
  InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
-> InternaliseM [[TypeBase ExtShape Uniqueness]]
forall a. InternaliseTypeM a -> InternaliseM a
runInternaliseTypeM (InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
 -> InternaliseM [[TypeBase ExtShape Uniqueness]])
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
-> InternaliseM [[TypeBase ExtShape Uniqueness]]
forall a b. (a -> b) -> a -> b
$
    (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [StructType]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM ([StructType] -> InternaliseTypeM [[TypeBase ExtShape Uniqueness]])
-> [StructType]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall a b. (a -> b) -> a -> b
$
      case StructType -> Maybe [StructType]
forall dim as. TypeBase dim as -> Maybe [TypeBase dim as]
E.isTupleRecord StructType
et of
        Just [StructType]
ets | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [StructType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [StructType]
ets -> [StructType]
ets
        Maybe [StructType]
_ -> [StructType
et]

internaliseType ::
  E.TypeBase (E.DimDecl VName) () ->
  InternaliseM [I.TypeBase I.ExtShape Uniqueness]
internaliseType :: StructType -> 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])
-> (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> StructType
-> InternaliseM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM

newId :: InternaliseTypeM Int
newId :: InternaliseTypeM Int
newId = do
  Int
i <- InternaliseTypeM Int
forall s (m :: * -> *). MonadState s m => m s
get
  Int -> InternaliseTypeM ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put (Int -> InternaliseTypeM ()) -> Int -> InternaliseTypeM ()
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
  Int -> InternaliseTypeM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i

internaliseDim ::
  E.DimDecl VName ->
  InternaliseTypeM ExtSize
internaliseDim :: DimDecl VName -> InternaliseTypeM (Ext SubExp)
internaliseDim DimDecl VName
d =
  case DimDecl VName
d of
    DimDecl VName
E.AnyDim -> Int -> Ext SubExp
forall a. Int -> Ext a
Ext (Int -> Ext SubExp)
-> InternaliseTypeM Int -> InternaliseTypeM (Ext SubExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InternaliseTypeM Int
newId
    E.ConstDim Int
n -> Ext SubExp -> InternaliseTypeM (Ext SubExp)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ext SubExp -> InternaliseTypeM (Ext SubExp))
-> Ext SubExp -> InternaliseTypeM (Ext SubExp)
forall a b. (a -> b) -> a -> b
$ SubExp -> Ext SubExp
forall a. a -> Ext a
Free (SubExp -> Ext SubExp) -> SubExp -> Ext SubExp
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 (Ext SubExp)
namedDim QualName VName
name
  where
    namedDim :: QualName VName -> InternaliseTypeM (Ext SubExp)
namedDim (E.QualName [VName]
_ VName
name) = 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] -> Ext SubExp -> InternaliseTypeM (Ext SubExp)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ext SubExp -> InternaliseTypeM (Ext SubExp))
-> Ext SubExp -> InternaliseTypeM (Ext SubExp)
forall a b. (a -> b) -> a -> b
$ SubExp -> Ext SubExp
forall a. a -> Ext a
I.Free SubExp
v
        Maybe [SubExp]
_ -> Ext SubExp -> InternaliseTypeM (Ext SubExp)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ext SubExp -> InternaliseTypeM (Ext SubExp))
-> Ext SubExp -> InternaliseTypeM (Ext SubExp)
forall a b. (a -> b) -> a -> b
$ SubExp -> Ext SubExp
forall a. a -> Ext a
I.Free (SubExp -> Ext SubExp) -> SubExp -> Ext SubExp
forall a b. (a -> b) -> a -> b
$ VName -> SubExp
I.Var VName
name

internaliseTypeM ::
  E.StructType ->
  InternaliseTypeM [I.TypeBase ExtShape Uniqueness]
internaliseTypeM :: StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM StructType
orig_t =
  case StructType
orig_t of
    E.Array ()
_ Uniqueness
u ScalarTypeBase (DimDecl VName) ()
et ShapeDecl (DimDecl VName)
shape -> do
      [Ext SubExp]
dims <- ShapeDecl (DimDecl VName) -> InternaliseTypeM [Ext SubExp]
internaliseShape ShapeDecl (DimDecl VName)
shape
      [TypeBase ExtShape Uniqueness]
ets <- StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall a b. (a -> b) -> a -> b
$ ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
E.Scalar ScalarTypeBase (DimDecl VName) ()
et
      [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (m :: * -> *) a. Monad m => a -> m a
return [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' ([Ext SubExp] -> ExtShape
forall d. [d] -> ShapeBase d
Shape [Ext SubExp]
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 (m :: * -> *) a. Monad m => a -> m a
return [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 StructType
ets)
      -- XXX: we map empty records to bools, because otherwise
      -- arrays of unit will lose their sizes.
      | Map Name StructType -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Map Name StructType
ets -> [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (m :: * -> *) a. Monad m => a -> m a
return [PrimType -> TypeBase ExtShape Uniqueness
forall shape u. PrimType -> TypeBase shape u
I.Prim PrimType
I.Bool]
      | 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, StructType)
 -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [(Name, StructType)]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> ((Name, StructType) -> StructType)
-> (Name, StructType)
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, StructType) -> StructType
forall a b. (a, b) -> b
snd) (Map Name StructType -> [(Name, StructType)]
forall a. Map Name a -> [(Name, a)]
E.sortFields Map Name StructType
ets)
    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]
++ StructType -> [Char]
forall a. Pretty a => a -> [Char]
pretty StructType
orig_t
    E.Scalar (E.Sum Map Name [StructType]
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
<$> ([StructType] -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> Map Name [StructType]
-> 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])
-> ([StructType]
    -> InternaliseTypeM [[TypeBase ExtShape Uniqueness]])
-> [StructType]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [StructType]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM) Map Name [StructType]
cs
      [TypeBase ExtShape Uniqueness]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall (m :: * -> *) a. Monad m => a -> m a
return ([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 [Ext SubExp]
internaliseShape = (DimDecl VName -> InternaliseTypeM (Ext SubExp))
-> [DimDecl VName] -> InternaliseTypeM [Ext SubExp]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM DimDecl VName -> InternaliseTypeM (Ext SubExp)
internaliseDim ([DimDecl VName] -> InternaliseTypeM [Ext SubExp])
-> (ShapeDecl (DimDecl VName) -> [DimDecl VName])
-> ShapeDecl (DimDecl VName)
-> InternaliseTypeM [Ext SubExp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShapeDecl (DimDecl VName) -> [DimDecl VName]
forall dim. ShapeDecl dim -> [dim]
E.shapeDims

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 [StructType]
-> InternaliseM
     ([TypeBase ExtShape Uniqueness], Map Name (Int, [Int]))
internaliseSumType Map Name [StructType]
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
<$> ([StructType] -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> Map Name [StructType]
-> 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])
-> ([StructType]
    -> InternaliseTypeM [[TypeBase ExtShape Uniqueness]])
-> [StructType]
-> InternaliseTypeM [TypeBase ExtShape Uniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness])
-> [StructType]
-> InternaliseTypeM [[TypeBase ExtShape Uniqueness]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM StructType -> InternaliseTypeM [TypeBase ExtShape Uniqueness]
internaliseTypeM) Map Name [StructType]
cs

-- | How many core language values are needed to represent one source
-- language value of the given type?
internalisedTypeSize :: E.TypeBase (E.DimDecl VName) () -> InternaliseM Int
internalisedTypeSize :: StructType -> InternaliseM Int
internalisedTypeSize = ([TypeBase ExtShape Uniqueness] -> Int)
-> InternaliseM [TypeBase ExtShape Uniqueness] -> InternaliseM Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [TypeBase ExtShape Uniqueness] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (InternaliseM [TypeBase ExtShape Uniqueness] -> InternaliseM Int)
-> (StructType -> InternaliseM [TypeBase ExtShape Uniqueness])
-> StructType
-> InternaliseM Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StructType -> InternaliseM [TypeBase ExtShape Uniqueness]
internaliseType

-- | 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