{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Strict #-}

-- | Internalising bindings.
module Futhark.Internalise.Bindings
  ( internaliseAttrs,
    internaliseAttr,
    bindingFParams,
    bindingLoopParams,
    bindingLambdaParams,
    stmPat,
  )
where

import Control.Monad.Reader hiding (mapM)
import Data.Bifunctor
import qualified Data.Map.Strict as M
import Data.Maybe
import qualified Futhark.IR.SOACS as I
import Futhark.Internalise.Monad
import Futhark.Internalise.TypesValues
import Futhark.Util
import Language.Futhark as E hiding (matchDims)

internaliseAttr :: E.AttrInfo VName -> InternaliseM I.Attr
internaliseAttr :: AttrInfo VName -> InternaliseM Attr
internaliseAttr (E.AttrAtom (E.AtomName Name
v) SrcLoc
_) =
  Attr -> InternaliseM Attr
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Attr -> InternaliseM Attr) -> Attr -> InternaliseM Attr
forall a b. (a -> b) -> a -> b
$ Name -> Attr
I.AttrName Name
v
internaliseAttr (E.AttrAtom (E.AtomInt Integer
x) SrcLoc
_) =
  Attr -> InternaliseM Attr
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Attr -> InternaliseM Attr) -> Attr -> InternaliseM Attr
forall a b. (a -> b) -> a -> b
$ Integer -> Attr
I.AttrInt Integer
x
internaliseAttr (E.AttrComp Name
f [AttrInfo VName]
attrs SrcLoc
_) =
  Name -> [Attr] -> Attr
I.AttrComp Name
f ([Attr] -> Attr) -> InternaliseM [Attr] -> InternaliseM Attr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (AttrInfo VName -> InternaliseM Attr)
-> [AttrInfo VName] -> InternaliseM [Attr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM AttrInfo VName -> InternaliseM Attr
internaliseAttr [AttrInfo VName]
attrs

internaliseAttrs :: [E.AttrInfo VName] -> InternaliseM I.Attrs
internaliseAttrs :: [AttrInfo VName] -> InternaliseM Attrs
internaliseAttrs = ([Attr] -> Attrs) -> InternaliseM [Attr] -> InternaliseM Attrs
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Attrs] -> Attrs
forall a. Monoid a => [a] -> a
mconcat ([Attrs] -> Attrs) -> ([Attr] -> [Attrs]) -> [Attr] -> Attrs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Attr -> Attrs) -> [Attr] -> [Attrs]
forall a b. (a -> b) -> [a] -> [b]
map Attr -> Attrs
I.oneAttr) (InternaliseM [Attr] -> InternaliseM Attrs)
-> ([AttrInfo VName] -> InternaliseM [Attr])
-> [AttrInfo VName]
-> InternaliseM Attrs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AttrInfo VName -> InternaliseM Attr)
-> [AttrInfo VName] -> InternaliseM [Attr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM AttrInfo VName -> InternaliseM Attr
internaliseAttr

bindingFParams ::
  [E.TypeParam] ->
  [E.Pat] ->
  ([I.FParam] -> [[I.FParam]] -> InternaliseM a) ->
  InternaliseM a
bindingFParams :: [TypeParam]
-> [Pat]
-> ([FParam] -> [[FParam]] -> InternaliseM a)
-> InternaliseM a
bindingFParams [TypeParam]
tparams [Pat]
params [FParam] -> [[FParam]] -> InternaliseM a
m = do
  [[(Ident, [AttrInfo VName])]]
flattened_params <- (Pat -> InternaliseM [(Ident, [AttrInfo VName])])
-> [Pat] -> InternaliseM [[(Ident, [AttrInfo VName])]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pat -> InternaliseM [(Ident, [AttrInfo VName])]
forall (m :: * -> *).
MonadFreshNames m =>
Pat -> m [(Ident, [AttrInfo VName])]
flattenPat [Pat]
params
  let params_idents :: [(Ident, [AttrInfo VName])]
params_idents = [[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Ident, [AttrInfo VName])]]
flattened_params
  [[TypeBase Shape Uniqueness]]
params_ts <-
    [TypeBase (DimDecl VName) ()]
-> InternaliseM [[TypeBase Shape Uniqueness]]
internaliseParamTypes ([TypeBase (DimDecl VName) ()]
 -> InternaliseM [[TypeBase Shape Uniqueness]])
-> [TypeBase (DimDecl VName) ()]
-> InternaliseM [[TypeBase Shape Uniqueness]]
forall a b. (a -> b) -> a -> b
$
      ((Ident, [AttrInfo VName]) -> TypeBase (DimDecl VName) ())
-> [(Ident, [AttrInfo VName])] -> [TypeBase (DimDecl VName) ()]
forall a b. (a -> b) -> [a] -> [b]
map ((TypeBase (DimDecl VName) Aliasing
 -> () -> TypeBase (DimDecl VName) ())
-> ()
-> TypeBase (DimDecl VName) Aliasing
-> TypeBase (DimDecl VName) ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip TypeBase (DimDecl VName) Aliasing
-> () -> TypeBase (DimDecl VName) ()
forall dim asf ast. TypeBase dim asf -> ast -> TypeBase dim ast
E.setAliases () (TypeBase (DimDecl VName) Aliasing -> TypeBase (DimDecl VName) ())
-> ((Ident, [AttrInfo VName]) -> TypeBase (DimDecl VName) Aliasing)
-> (Ident, [AttrInfo VName])
-> TypeBase (DimDecl VName) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Info (TypeBase (DimDecl VName) Aliasing)
-> TypeBase (DimDecl VName) Aliasing
forall a. Info a -> a
E.unInfo (Info (TypeBase (DimDecl VName) Aliasing)
 -> TypeBase (DimDecl VName) Aliasing)
-> ((Ident, [AttrInfo VName])
    -> Info (TypeBase (DimDecl VName) Aliasing))
-> (Ident, [AttrInfo VName])
-> TypeBase (DimDecl VName) Aliasing
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ident -> Info (TypeBase (DimDecl VName) Aliasing)
forall (f :: * -> *) vn.
IdentBase f vn -> f (TypeBase (DimDecl VName) Aliasing)
E.identType (Ident -> Info (TypeBase (DimDecl VName) Aliasing))
-> ((Ident, [AttrInfo VName]) -> Ident)
-> (Ident, [AttrInfo VName])
-> Info (TypeBase (DimDecl VName) Aliasing)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ident, [AttrInfo VName]) -> Ident
forall a b. (a, b) -> a
fst) [(Ident, [AttrInfo VName])]
params_idents
  let num_param_idents :: [Int]
num_param_idents = ([(Ident, [AttrInfo VName])] -> Int)
-> [[(Ident, [AttrInfo VName])]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [(Ident, [AttrInfo VName])] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[(Ident, [AttrInfo VName])]]
flattened_params
      num_param_ts :: [Int]
num_param_ts = ([[TypeBase Shape Uniqueness]] -> Int)
-> [[[TypeBase Shape Uniqueness]]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int)
-> ([[TypeBase Shape Uniqueness]] -> [Int])
-> [[TypeBase Shape Uniqueness]]
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([TypeBase Shape Uniqueness] -> Int)
-> [[TypeBase Shape Uniqueness]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [TypeBase Shape Uniqueness] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length) ([[[TypeBase Shape Uniqueness]]] -> [Int])
-> [[[TypeBase Shape Uniqueness]]] -> [Int]
forall a b. (a -> b) -> a -> b
$ [Int]
-> [[TypeBase Shape Uniqueness]] -> [[[TypeBase Shape Uniqueness]]]
forall a. [Int] -> [a] -> [[a]]
chunks [Int]
num_param_idents [[TypeBase Shape Uniqueness]]
params_ts

  let shape_params :: [Param (TypeBase shape u)]
shape_params = [Attrs -> VName -> TypeBase shape u -> Param (TypeBase shape u)
forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
forall a. Monoid a => a
mempty VName
v (TypeBase shape u -> Param (TypeBase shape u))
-> TypeBase shape u -> Param (TypeBase shape u)
forall a b. (a -> b) -> a -> b
$ PrimType -> TypeBase shape u
forall shape u. PrimType -> TypeBase shape u
I.Prim PrimType
I.int64 | E.TypeParamDim VName
v SrcLoc
_ <- [TypeParam]
tparams]
      shape_subst :: Map VName [SubExp]
shape_subst = [(VName, [SubExp])] -> Map VName [SubExp]
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Param (TypeBase Any Any) -> VName
forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p, [VName -> SubExp
I.Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ Param (TypeBase Any Any) -> VName
forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p]) | Param (TypeBase Any Any)
p <- [Param (TypeBase Any Any)]
forall shape u. [Param (TypeBase shape u)]
shape_params]
  [(Ident, [AttrInfo VName])]
-> [TypeBase Shape Uniqueness]
-> ([[Param (TypeBase Shape Uniqueness)]] -> InternaliseM a)
-> InternaliseM a
forall t a.
Show t =>
[(Ident, [AttrInfo VName])]
-> [t] -> ([[Param t]] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident, [AttrInfo VName])]
params_idents ([[TypeBase Shape Uniqueness]] -> [TypeBase Shape Uniqueness]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[TypeBase Shape Uniqueness]]
params_ts) (([[Param (TypeBase Shape Uniqueness)]] -> InternaliseM a)
 -> InternaliseM a)
-> ([[Param (TypeBase Shape Uniqueness)]] -> InternaliseM a)
-> InternaliseM a
forall a b. (a -> b) -> a -> b
$ \[[Param (TypeBase Shape Uniqueness)]]
valueparams -> do
    let ([Maybe (Param (TypeBase shape u))]
certparams, [Param (TypeBase Shape Uniqueness)]
valueparams') = [(Maybe (Param (TypeBase shape u)),
  Param (TypeBase Shape Uniqueness))]
-> ([Maybe (Param (TypeBase shape u))],
    [Param (TypeBase Shape Uniqueness)])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Maybe (Param (TypeBase shape u)),
   Param (TypeBase Shape Uniqueness))]
 -> ([Maybe (Param (TypeBase shape u))],
     [Param (TypeBase Shape Uniqueness)]))
-> [(Maybe (Param (TypeBase shape u)),
     Param (TypeBase Shape Uniqueness))]
-> ([Maybe (Param (TypeBase shape u))],
    [Param (TypeBase Shape Uniqueness)])
forall a b. (a -> b) -> a -> b
$ (Param (TypeBase Shape Uniqueness)
 -> (Maybe (Param (TypeBase shape u)),
     Param (TypeBase Shape Uniqueness)))
-> [Param (TypeBase Shape Uniqueness)]
-> [(Maybe (Param (TypeBase shape u)),
     Param (TypeBase Shape Uniqueness))]
forall a b. (a -> b) -> [a] -> [b]
map Param (TypeBase Shape Uniqueness)
-> (Maybe (Param (TypeBase shape u)),
    Param (TypeBase Shape Uniqueness))
forall shape u shape u.
Param (TypeBase shape u)
-> (Maybe (Param (TypeBase shape u)), Param (TypeBase shape u))
fixAccParam ([[Param (TypeBase Shape Uniqueness)]]
-> [Param (TypeBase Shape Uniqueness)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Param (TypeBase Shape Uniqueness)]]
valueparams)
    Scope SOACS -> InternaliseM a -> InternaliseM a
forall rep (m :: * -> *) a.
LocalScope rep m =>
Scope rep -> m a -> m a
I.localScope ([Param (TypeBase Shape Uniqueness)] -> Scope SOACS
forall rep dec. (FParamInfo rep ~ dec) => [Param dec] -> Scope rep
I.scopeOfFParams ([Param (TypeBase Shape Uniqueness)] -> Scope SOACS)
-> [Param (TypeBase Shape Uniqueness)] -> Scope SOACS
forall a b. (a -> b) -> a -> b
$ [Maybe (Param (TypeBase Shape Uniqueness))]
-> [Param (TypeBase Shape Uniqueness)]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (Param (TypeBase Shape Uniqueness))]
forall shape u. [Maybe (Param (TypeBase shape u))]
certparams [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
forall a. [a] -> [a] -> [a]
++ [Param (TypeBase Shape Uniqueness)]
forall shape u. [Param (TypeBase shape u)]
shape_params [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
forall a. [a] -> [a] -> [a]
++ [Param (TypeBase Shape Uniqueness)]
valueparams') (InternaliseM a -> InternaliseM a)
-> InternaliseM a -> InternaliseM a
forall a b. (a -> b) -> a -> b
$
      Map VName [SubExp] -> InternaliseM a -> InternaliseM a
forall a. Map VName [SubExp] -> InternaliseM a -> InternaliseM a
substitutingVars Map VName [SubExp]
shape_subst (InternaliseM a -> InternaliseM a)
-> InternaliseM a -> InternaliseM a
forall a b. (a -> b) -> a -> b
$
        [FParam] -> [[FParam]] -> InternaliseM a
m ([Maybe (Param (TypeBase Shape Uniqueness))]
-> [Param (TypeBase Shape Uniqueness)]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (Param (TypeBase Shape Uniqueness))]
forall shape u. [Maybe (Param (TypeBase shape u))]
certparams [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
forall a. [a] -> [a] -> [a]
++ [Param (TypeBase Shape Uniqueness)]
forall shape u. [Param (TypeBase shape u)]
shape_params) ([[FParam]] -> InternaliseM a) -> [[FParam]] -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ [Int]
-> [Param (TypeBase Shape Uniqueness)]
-> [[Param (TypeBase Shape Uniqueness)]]
forall a. [Int] -> [a] -> [[a]]
chunks [Int]
num_param_ts [Param (TypeBase Shape Uniqueness)]
valueparams'
  where
    fixAccParam :: Param (TypeBase shape u)
-> (Maybe (Param (TypeBase shape u)), Param (TypeBase shape u))
fixAccParam (I.Param Attrs
attrs VName
pv (I.Acc VName
acc Shape
ispace [Type]
ts u
u)) =
      ( Param (TypeBase shape u) -> Maybe (Param (TypeBase shape u))
forall a. a -> Maybe a
Just (Attrs -> VName -> TypeBase shape u -> Param (TypeBase shape u)
forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
attrs VName
acc (TypeBase shape u -> Param (TypeBase shape u))
-> TypeBase shape u -> Param (TypeBase shape u)
forall a b. (a -> b) -> a -> b
$ PrimType -> TypeBase shape u
forall shape u. PrimType -> TypeBase shape u
I.Prim PrimType
I.Unit),
        Attrs -> VName -> TypeBase shape u -> Param (TypeBase shape u)
forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
attrs VName
pv (VName -> Shape -> [Type] -> u -> TypeBase shape u
forall shape u. VName -> Shape -> [Type] -> u -> TypeBase shape u
I.Acc VName
acc Shape
ispace [Type]
ts u
u)
      )
    fixAccParam Param (TypeBase shape u)
p = (Maybe (Param (TypeBase shape u))
forall a. Maybe a
Nothing, Param (TypeBase shape u)
p)

bindingLoopParams ::
  [E.TypeParam] ->
  E.Pat ->
  [I.Type] ->
  ([I.FParam] -> [I.FParam] -> InternaliseM a) ->
  InternaliseM a
bindingLoopParams :: [TypeParam]
-> Pat
-> [Type]
-> ([FParam] -> [FParam] -> InternaliseM a)
-> InternaliseM a
bindingLoopParams [TypeParam]
tparams Pat
pat [Type]
ts [FParam] -> [FParam] -> InternaliseM a
m = do
  [(Ident, [AttrInfo VName])]
pat_idents <- Pat -> InternaliseM [(Ident, [AttrInfo VName])]
forall (m :: * -> *).
MonadFreshNames m =>
Pat -> m [(Ident, [AttrInfo VName])]
flattenPat Pat
pat
  [TypeBase Shape Uniqueness]
pat_ts <- TypeBase (DimDecl VName) ()
-> [Type] -> InternaliseM [TypeBase Shape Uniqueness]
forall shape u.
TypeBase (DimDecl VName) ()
-> [TypeBase shape u] -> InternaliseM [TypeBase Shape Uniqueness]
internaliseLoopParamType (Pat -> TypeBase (DimDecl VName) ()
E.patternStructType Pat
pat) [Type]
ts

  let shape_params :: [Param (TypeBase shape u)]
shape_params = [Attrs -> VName -> TypeBase shape u -> Param (TypeBase shape u)
forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
forall a. Monoid a => a
mempty VName
v (TypeBase shape u -> Param (TypeBase shape u))
-> TypeBase shape u -> Param (TypeBase shape u)
forall a b. (a -> b) -> a -> b
$ PrimType -> TypeBase shape u
forall shape u. PrimType -> TypeBase shape u
I.Prim PrimType
I.int64 | E.TypeParamDim VName
v SrcLoc
_ <- [TypeParam]
tparams]
      shape_subst :: Map VName [SubExp]
shape_subst = [(VName, [SubExp])] -> Map VName [SubExp]
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Param (TypeBase Any Any) -> VName
forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p, [VName -> SubExp
I.Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ Param (TypeBase Any Any) -> VName
forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p]) | Param (TypeBase Any Any)
p <- [Param (TypeBase Any Any)]
forall shape u. [Param (TypeBase shape u)]
shape_params]

  [(Ident, [AttrInfo VName])]
-> [TypeBase Shape Uniqueness]
-> ([[Param (TypeBase Shape Uniqueness)]] -> InternaliseM a)
-> InternaliseM a
forall t a.
Show t =>
[(Ident, [AttrInfo VName])]
-> [t] -> ([[Param t]] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident, [AttrInfo VName])]
pat_idents [TypeBase Shape Uniqueness]
pat_ts (([[Param (TypeBase Shape Uniqueness)]] -> InternaliseM a)
 -> InternaliseM a)
-> ([[Param (TypeBase Shape Uniqueness)]] -> InternaliseM a)
-> InternaliseM a
forall a b. (a -> b) -> a -> b
$ \[[Param (TypeBase Shape Uniqueness)]]
valueparams ->
    Scope SOACS -> InternaliseM a -> InternaliseM a
forall rep (m :: * -> *) a.
LocalScope rep m =>
Scope rep -> m a -> m a
I.localScope ([Param (TypeBase Shape Uniqueness)] -> Scope SOACS
forall rep dec. (FParamInfo rep ~ dec) => [Param dec] -> Scope rep
I.scopeOfFParams ([Param (TypeBase Shape Uniqueness)] -> Scope SOACS)
-> [Param (TypeBase Shape Uniqueness)] -> Scope SOACS
forall a b. (a -> b) -> a -> b
$ [Param (TypeBase Shape Uniqueness)]
forall shape u. [Param (TypeBase shape u)]
shape_params [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
-> [Param (TypeBase Shape Uniqueness)]
forall a. [a] -> [a] -> [a]
++ [[Param (TypeBase Shape Uniqueness)]]
-> [Param (TypeBase Shape Uniqueness)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Param (TypeBase Shape Uniqueness)]]
valueparams) (InternaliseM a -> InternaliseM a)
-> InternaliseM a -> InternaliseM a
forall a b. (a -> b) -> a -> b
$
      Map VName [SubExp] -> InternaliseM a -> InternaliseM a
forall a. Map VName [SubExp] -> InternaliseM a -> InternaliseM a
substitutingVars Map VName [SubExp]
shape_subst (InternaliseM a -> InternaliseM a)
-> InternaliseM a -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ [FParam] -> [FParam] -> InternaliseM a
m [FParam]
forall shape u. [Param (TypeBase shape u)]
shape_params ([FParam] -> InternaliseM a) -> [FParam] -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ [[Param (TypeBase Shape Uniqueness)]]
-> [Param (TypeBase Shape Uniqueness)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Param (TypeBase Shape Uniqueness)]]
valueparams

bindingLambdaParams ::
  [E.Pat] ->
  [I.Type] ->
  ([I.LParam] -> InternaliseM a) ->
  InternaliseM a
bindingLambdaParams :: [Pat] -> [Type] -> ([LParam] -> InternaliseM a) -> InternaliseM a
bindingLambdaParams [Pat]
params [Type]
ts [LParam] -> InternaliseM a
m = do
  [(Ident, [AttrInfo VName])]
params_idents <- [[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])])
-> InternaliseM [[(Ident, [AttrInfo VName])]]
-> InternaliseM [(Ident, [AttrInfo VName])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Pat -> InternaliseM [(Ident, [AttrInfo VName])])
-> [Pat] -> InternaliseM [[(Ident, [AttrInfo VName])]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pat -> InternaliseM [(Ident, [AttrInfo VName])]
forall (m :: * -> *).
MonadFreshNames m =>
Pat -> m [(Ident, [AttrInfo VName])]
flattenPat [Pat]
params

  [(Ident, [AttrInfo VName])]
-> [Type] -> ([[Param Type]] -> InternaliseM a) -> InternaliseM a
forall t a.
Show t =>
[(Ident, [AttrInfo VName])]
-> [t] -> ([[Param t]] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident, [AttrInfo VName])]
params_idents [Type]
ts (([[Param Type]] -> InternaliseM a) -> InternaliseM a)
-> ([[Param Type]] -> InternaliseM a) -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ \[[Param Type]]
params' ->
    Scope SOACS -> InternaliseM a -> InternaliseM a
forall rep (m :: * -> *) a.
LocalScope rep m =>
Scope rep -> m a -> m a
I.localScope ([Param Type] -> Scope SOACS
forall rep dec. (LParamInfo rep ~ dec) => [Param dec] -> Scope rep
I.scopeOfLParams ([Param Type] -> Scope SOACS) -> [Param Type] -> Scope SOACS
forall a b. (a -> b) -> a -> b
$ [[Param Type]] -> [Param Type]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Param Type]]
params') (InternaliseM a -> InternaliseM a)
-> InternaliseM a -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ [LParam] -> InternaliseM a
m ([LParam] -> InternaliseM a) -> [LParam] -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ [[Param Type]] -> [Param Type]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Param Type]]
params'

processFlatPat ::
  Show t =>
  [(E.Ident, [E.AttrInfo VName])] ->
  [t] ->
  InternaliseM ([[I.Param t]], VarSubsts)
processFlatPat :: [(Ident, [AttrInfo VName])]
-> [t] -> InternaliseM ([[Param t]], Map VName [SubExp])
processFlatPat [(Ident, [AttrInfo VName])]
x [t]
y = [([Param t], (VName, [SubExp]))]
-> [(Ident, [AttrInfo VName])]
-> [t]
-> InternaliseM ([[Param t]], Map VName [SubExp])
forall dec.
[([Param dec], (VName, [SubExp]))]
-> [(Ident, [AttrInfo VName])]
-> [dec]
-> InternaliseM ([[Param dec]], Map VName [SubExp])
processFlatPat' [] [(Ident, [AttrInfo VName])]
x [t]
y
  where
    processFlatPat' :: [([Param dec], (VName, [SubExp]))]
-> [(Ident, [AttrInfo VName])]
-> [dec]
-> InternaliseM ([[Param dec]], Map VName [SubExp])
processFlatPat' [([Param dec], (VName, [SubExp]))]
pat [] [dec]
_ = do
      let ([[Param dec]]
vs, [(VName, [SubExp])]
substs) = [([Param dec], (VName, [SubExp]))]
-> ([[Param dec]], [(VName, [SubExp])])
forall a b. [(a, b)] -> ([a], [b])
unzip [([Param dec], (VName, [SubExp]))]
pat
      ([[Param dec]], Map VName [SubExp])
-> InternaliseM ([[Param dec]], Map VName [SubExp])
forall (m :: * -> *) a. Monad m => a -> m a
return ([[Param dec]] -> [[Param dec]]
forall a. [a] -> [a]
reverse [[Param dec]]
vs, [(VName, [SubExp])] -> Map VName [SubExp]
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(VName, [SubExp])]
substs)
    processFlatPat' [([Param dec], (VName, [SubExp]))]
pat ((Ident
p, [AttrInfo VName]
attrs) : [(Ident, [AttrInfo VName])]
rest) [dec]
ts = do
      Attrs
attrs' <- [AttrInfo VName] -> InternaliseM Attrs
internaliseAttrs [AttrInfo VName]
attrs
      ([Param dec]
ps, [dec]
rest_ts) <- Attrs -> [dec] -> [VName] -> ([Param dec], [dec])
forall dec. Attrs -> [dec] -> [VName] -> ([Param dec], [dec])
handleMapping Attrs
attrs' [dec]
ts ([VName] -> ([Param dec], [dec]))
-> InternaliseM [VName] -> InternaliseM ([Param dec], [dec])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ident -> InternaliseM [VName]
internaliseBindee Ident
p
      [([Param dec], (VName, [SubExp]))]
-> [(Ident, [AttrInfo VName])]
-> [dec]
-> InternaliseM ([[Param dec]], Map VName [SubExp])
processFlatPat' (([Param dec]
ps, (Ident -> VName
forall (f :: * -> *) vn. IdentBase f vn -> vn
E.identName Ident
p, (Param dec -> SubExp) -> [Param dec] -> [SubExp]
forall a b. (a -> b) -> [a] -> [b]
map (VName -> SubExp
I.Var (VName -> SubExp) -> (Param dec -> VName) -> Param dec -> SubExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Param dec -> VName
forall dec. Param dec -> VName
I.paramName) [Param dec]
ps)) ([Param dec], (VName, [SubExp]))
-> [([Param dec], (VName, [SubExp]))]
-> [([Param dec], (VName, [SubExp]))]
forall a. a -> [a] -> [a]
: [([Param dec], (VName, [SubExp]))]
pat) [(Ident, [AttrInfo VName])]
rest [dec]
rest_ts

    handleMapping :: Attrs -> [dec] -> [VName] -> ([Param dec], [dec])
handleMapping Attrs
_ [dec]
ts [] =
      ([], [dec]
ts)
    handleMapping Attrs
attrs (dec
t : [dec]
ts) (VName
r : [VName]
rs) =
      let ([Param dec]
ps, [dec]
ts') = Attrs -> [dec] -> [VName] -> ([Param dec], [dec])
handleMapping Attrs
attrs [dec]
ts [VName]
rs
       in (Attrs -> VName -> dec -> Param dec
forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
attrs VName
r dec
t Param dec -> [Param dec] -> [Param dec]
forall a. a -> [a] -> [a]
: [Param dec]
ps, [dec]
ts')
    handleMapping Attrs
_ [] [VName]
_ =
      [Char] -> ([Param dec], [dec])
forall a. HasCallStack => [Char] -> a
error ([Char] -> ([Param dec], [dec])) -> [Char] -> ([Param dec], [dec])
forall a b. (a -> b) -> a -> b
$ [Char]
"handleMapping: insufficient identifiers in pattern." [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ([(Ident, [AttrInfo VName])], [t]) -> [Char]
forall a. Show a => a -> [Char]
show ([(Ident, [AttrInfo VName])]
x, [t]
y)

    internaliseBindee :: E.Ident -> InternaliseM [VName]
    internaliseBindee :: Ident -> InternaliseM [VName]
internaliseBindee Ident
bindee = do
      let name :: VName
name = Ident -> VName
forall (f :: * -> *) vn. IdentBase f vn -> vn
E.identName Ident
bindee
      Int
n <- TypeBase (DimDecl VName) Aliasing -> InternaliseM Int
forall als. TypeBase (DimDecl VName) als -> InternaliseM Int
internalisedTypeSize (TypeBase (DimDecl VName) Aliasing -> InternaliseM Int)
-> TypeBase (DimDecl VName) Aliasing -> InternaliseM Int
forall a b. (a -> b) -> a -> b
$ Info (TypeBase (DimDecl VName) Aliasing)
-> TypeBase (DimDecl VName) Aliasing
forall a. Info a -> a
E.unInfo (Info (TypeBase (DimDecl VName) Aliasing)
 -> TypeBase (DimDecl VName) Aliasing)
-> Info (TypeBase (DimDecl VName) Aliasing)
-> TypeBase (DimDecl VName) Aliasing
forall a b. (a -> b) -> a -> b
$ Ident -> Info (TypeBase (DimDecl VName) Aliasing)
forall (f :: * -> *) vn.
IdentBase f vn -> f (TypeBase (DimDecl VName) Aliasing)
E.identType Ident
bindee
      case Int
n of
        Int
1 -> [VName] -> InternaliseM [VName]
forall (m :: * -> *) a. Monad m => a -> m a
return [VName
name]
        Int
_ -> Int -> InternaliseM VName -> InternaliseM [VName]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (InternaliseM VName -> InternaliseM [VName])
-> InternaliseM VName -> InternaliseM [VName]
forall a b. (a -> b) -> a -> b
$ [Char] -> InternaliseM VName
forall (m :: * -> *). MonadFreshNames m => [Char] -> m VName
newVName ([Char] -> InternaliseM VName) -> [Char] -> InternaliseM VName
forall a b. (a -> b) -> a -> b
$ VName -> [Char]
baseString VName
name

bindingFlatPat ::
  Show t =>
  [(E.Ident, [E.AttrInfo VName])] ->
  [t] ->
  ([[I.Param t]] -> InternaliseM a) ->
  InternaliseM a
bindingFlatPat :: [(Ident, [AttrInfo VName])]
-> [t] -> ([[Param t]] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident, [AttrInfo VName])]
idents [t]
ts [[Param t]] -> InternaliseM a
m = do
  ([[Param t]]
ps, Map VName [SubExp]
substs) <- [(Ident, [AttrInfo VName])]
-> [t] -> InternaliseM ([[Param t]], Map VName [SubExp])
forall t.
Show t =>
[(Ident, [AttrInfo VName])]
-> [t] -> InternaliseM ([[Param t]], Map VName [SubExp])
processFlatPat [(Ident, [AttrInfo VName])]
idents [t]
ts
  (InternaliseEnv -> InternaliseEnv)
-> InternaliseM a -> InternaliseM a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\InternaliseEnv
env -> InternaliseEnv
env {envSubsts :: Map VName [SubExp]
envSubsts = Map VName [SubExp]
substs Map VName [SubExp] -> Map VName [SubExp] -> Map VName [SubExp]
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` InternaliseEnv -> Map VName [SubExp]
envSubsts InternaliseEnv
env}) (InternaliseM a -> InternaliseM a)
-> InternaliseM a -> InternaliseM a
forall a b. (a -> b) -> a -> b
$
    [[Param t]] -> InternaliseM a
m [[Param t]]
ps

-- | Flatten a pattern.  Returns a list of identifiers.
flattenPat :: MonadFreshNames m => E.Pat -> m [(E.Ident, [E.AttrInfo VName])]
flattenPat :: Pat -> m [(Ident, [AttrInfo VName])]
flattenPat = Pat -> m [(Ident, [AttrInfo VName])]
forall (m :: * -> *).
MonadFreshNames m =>
Pat -> m [(Ident, [AttrInfo VName])]
flattenPat'
  where
    flattenPat' :: Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' (E.PatParens Pat
p SrcLoc
_) =
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' Pat
p
    flattenPat' (E.PatAttr AttrInfo VName
attr Pat
p SrcLoc
_) =
      ((Ident, [AttrInfo VName]) -> (Ident, [AttrInfo VName]))
-> [(Ident, [AttrInfo VName])] -> [(Ident, [AttrInfo VName])]
forall a b. (a -> b) -> [a] -> [b]
map (([AttrInfo VName] -> [AttrInfo VName])
-> (Ident, [AttrInfo VName]) -> (Ident, [AttrInfo VName])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (AttrInfo VName
attr AttrInfo VName -> [AttrInfo VName] -> [AttrInfo VName]
forall a. a -> [a] -> [a]
:)) ([(Ident, [AttrInfo VName])] -> [(Ident, [AttrInfo VName])])
-> f [(Ident, [AttrInfo VName])] -> f [(Ident, [AttrInfo VName])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' Pat
p
    flattenPat' (E.Wildcard Info (TypeBase (DimDecl VName) Aliasing)
t SrcLoc
loc) = do
      VName
name <- [Char] -> f VName
forall (m :: * -> *). MonadFreshNames m => [Char] -> m VName
newVName [Char]
"nameless"
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' (Pat -> f [(Ident, [AttrInfo VName])])
-> Pat -> f [(Ident, [AttrInfo VName])]
forall a b. (a -> b) -> a -> b
$ VName -> Info (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> Pat
forall (f :: * -> *) vn.
vn
-> f (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> PatBase f vn
E.Id VName
name Info (TypeBase (DimDecl VName) Aliasing)
t SrcLoc
loc
    flattenPat' (E.Id VName
v (Info TypeBase (DimDecl VName) Aliasing
t) SrcLoc
loc) =
      [(Ident, [AttrInfo VName])] -> f [(Ident, [AttrInfo VName])]
forall (m :: * -> *) a. Monad m => a -> m a
return [(VName
-> Info (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> Ident
forall (f :: * -> *) vn.
vn
-> f (TypeBase (DimDecl VName) Aliasing)
-> SrcLoc
-> IdentBase f vn
E.Ident VName
v (TypeBase (DimDecl VName) Aliasing
-> Info (TypeBase (DimDecl VName) Aliasing)
forall a. a -> Info a
Info TypeBase (DimDecl VName) Aliasing
t) SrcLoc
loc, [AttrInfo VName]
forall a. Monoid a => a
mempty)]
    -- XXX: treat empty tuples and records as unit.
    flattenPat' (E.TuplePat [] SrcLoc
loc) =
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' (Info (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> Pat
forall (f :: * -> *) vn.
f (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> PatBase f vn
E.Wildcard (TypeBase (DimDecl VName) Aliasing
-> Info (TypeBase (DimDecl VName) Aliasing)
forall a. a -> Info a
Info (TypeBase (DimDecl VName) Aliasing
 -> Info (TypeBase (DimDecl VName) Aliasing))
-> TypeBase (DimDecl VName) Aliasing
-> Info (TypeBase (DimDecl VName) Aliasing)
forall a b. (a -> b) -> a -> b
$ ScalarTypeBase (DimDecl VName) Aliasing
-> TypeBase (DimDecl VName) Aliasing
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
E.Scalar (ScalarTypeBase (DimDecl VName) Aliasing
 -> TypeBase (DimDecl VName) Aliasing)
-> ScalarTypeBase (DimDecl VName) Aliasing
-> TypeBase (DimDecl VName) Aliasing
forall a b. (a -> b) -> a -> b
$ Map Name (TypeBase (DimDecl VName) Aliasing)
-> ScalarTypeBase (DimDecl VName) Aliasing
forall dim as. Map Name (TypeBase dim as) -> ScalarTypeBase dim as
E.Record Map Name (TypeBase (DimDecl VName) Aliasing)
forall a. Monoid a => a
mempty) SrcLoc
loc)
    flattenPat' (E.RecordPat [] SrcLoc
loc) =
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' (Info (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> Pat
forall (f :: * -> *) vn.
f (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> PatBase f vn
E.Wildcard (TypeBase (DimDecl VName) Aliasing
-> Info (TypeBase (DimDecl VName) Aliasing)
forall a. a -> Info a
Info (TypeBase (DimDecl VName) Aliasing
 -> Info (TypeBase (DimDecl VName) Aliasing))
-> TypeBase (DimDecl VName) Aliasing
-> Info (TypeBase (DimDecl VName) Aliasing)
forall a b. (a -> b) -> a -> b
$ ScalarTypeBase (DimDecl VName) Aliasing
-> TypeBase (DimDecl VName) Aliasing
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
E.Scalar (ScalarTypeBase (DimDecl VName) Aliasing
 -> TypeBase (DimDecl VName) Aliasing)
-> ScalarTypeBase (DimDecl VName) Aliasing
-> TypeBase (DimDecl VName) Aliasing
forall a b. (a -> b) -> a -> b
$ Map Name (TypeBase (DimDecl VName) Aliasing)
-> ScalarTypeBase (DimDecl VName) Aliasing
forall dim as. Map Name (TypeBase dim as) -> ScalarTypeBase dim as
E.Record Map Name (TypeBase (DimDecl VName) Aliasing)
forall a. Monoid a => a
mempty) SrcLoc
loc)
    flattenPat' (E.TuplePat [Pat]
pats SrcLoc
_) =
      [[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])])
-> f [[(Ident, [AttrInfo VName])]] -> f [(Ident, [AttrInfo VName])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Pat -> f [(Ident, [AttrInfo VName])])
-> [Pat] -> f [[(Ident, [AttrInfo VName])]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' [Pat]
pats
    flattenPat' (E.RecordPat [(Name, Pat)]
fs SrcLoc
loc) =
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' (Pat -> f [(Ident, [AttrInfo VName])])
-> Pat -> f [(Ident, [AttrInfo VName])]
forall a b. (a -> b) -> a -> b
$ [Pat] -> SrcLoc -> Pat
forall (f :: * -> *) vn. [PatBase f vn] -> SrcLoc -> PatBase f vn
E.TuplePat (((Name, Pat) -> Pat) -> [(Name, Pat)] -> [Pat]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Pat) -> Pat
forall a b. (a, b) -> b
snd ([(Name, Pat)] -> [Pat]) -> [(Name, Pat)] -> [Pat]
forall a b. (a -> b) -> a -> b
$ Map Name Pat -> [(Name, Pat)]
forall a. Map Name a -> [(Name, a)]
sortFields (Map Name Pat -> [(Name, Pat)]) -> Map Name Pat -> [(Name, Pat)]
forall a b. (a -> b) -> a -> b
$ [(Name, Pat)] -> Map Name Pat
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Name, Pat)]
fs) SrcLoc
loc
    flattenPat' (E.PatAscription Pat
p TypeDeclBase Info VName
_ SrcLoc
_) =
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' Pat
p
    flattenPat' (E.PatLit PatLit
_ Info (TypeBase (DimDecl VName) Aliasing)
t SrcLoc
loc) =
      Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' (Pat -> f [(Ident, [AttrInfo VName])])
-> Pat -> f [(Ident, [AttrInfo VName])]
forall a b. (a -> b) -> a -> b
$ Info (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> Pat
forall (f :: * -> *) vn.
f (TypeBase (DimDecl VName) Aliasing) -> SrcLoc -> PatBase f vn
E.Wildcard Info (TypeBase (DimDecl VName) Aliasing)
t SrcLoc
loc
    flattenPat' (E.PatConstr Name
_ Info (TypeBase (DimDecl VName) Aliasing)
_ [Pat]
ps SrcLoc
_) =
      [[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(Ident, [AttrInfo VName])]] -> [(Ident, [AttrInfo VName])])
-> f [[(Ident, [AttrInfo VName])]] -> f [(Ident, [AttrInfo VName])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Pat -> f [(Ident, [AttrInfo VName])])
-> [Pat] -> f [[(Ident, [AttrInfo VName])]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pat -> f [(Ident, [AttrInfo VName])]
flattenPat' [Pat]
ps

stmPat ::
  E.Pat ->
  [I.Type] ->
  ([VName] -> InternaliseM a) ->
  InternaliseM a
stmPat :: Pat -> [Type] -> ([VName] -> InternaliseM a) -> InternaliseM a
stmPat Pat
pat [Type]
ts [VName] -> InternaliseM a
m = do
  [(Ident, [AttrInfo VName])]
pat' <- Pat -> InternaliseM [(Ident, [AttrInfo VName])]
forall (m :: * -> *).
MonadFreshNames m =>
Pat -> m [(Ident, [AttrInfo VName])]
flattenPat Pat
pat
  [(Ident, [AttrInfo VName])]
-> [Type] -> ([[Param Type]] -> InternaliseM a) -> InternaliseM a
forall t a.
Show t =>
[(Ident, [AttrInfo VName])]
-> [t] -> ([[Param t]] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident, [AttrInfo VName])]
pat' [Type]
ts (([[Param Type]] -> InternaliseM a) -> InternaliseM a)
-> ([[Param Type]] -> InternaliseM a) -> InternaliseM a
forall a b. (a -> b) -> a -> b
$ [VName] -> InternaliseM a
m ([VName] -> InternaliseM a)
-> ([[Param Type]] -> [VName]) -> [[Param Type]] -> InternaliseM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Param Type -> VName) -> [Param Type] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param Type -> VName
forall dec. Param dec -> VName
I.paramName ([Param Type] -> [VName])
-> ([[Param Type]] -> [Param Type]) -> [[Param Type]] -> [VName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Param Type]] -> [Param Type]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat