{-# LANGUAGE Strict #-}

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

import Control.Monad
import Control.Monad.Free (Free (..))
import Control.Monad.Reader
import Data.Bifunctor
import Data.Foldable (toList)
import Data.Map.Strict qualified as M
import Data.Maybe
import Futhark.IR.SOACS qualified 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
_) =
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Name -> Attr
I.AttrName Name
v
internaliseAttr (E.AttrAtom (E.AtomInt Integer
x) SrcLoc
_) =
  forall (f :: * -> *) a. Applicative f => a -> f a
pure 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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Attr -> Attrs
I.oneAttr) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM AttrInfo VName -> InternaliseM Attr
internaliseAttr

treeLike :: Tree a -> [b] -> Tree b
treeLike :: forall a b. Tree a -> [b] -> Tree b
treeLike (Pure a
_) [b
b] = forall (f :: * -> *) a. a -> Free f a
Pure b
b
treeLike (Pure a
_) [b]
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"treeLike: invalid input"
treeLike (Free [Free [] a]
ls) [b]
bs = forall (f :: * -> *) a. f (Free f a) -> Free f a
Free forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a b. Tree a -> [b] -> Tree b
treeLike [Free [] a]
ls (forall a. [Int] -> [a] -> [[a]]
chunks (forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length [Free [] a]
ls) [b]
bs)

bindingFParams ::
  [E.TypeParam] ->
  [E.Pat E.ParamType] ->
  ([I.FParam I.SOACS] -> [[Tree (I.FParam I.SOACS)]] -> InternaliseM a) ->
  InternaliseM a
bindingFParams :: forall a.
[TypeParam]
-> [Pat ParamType]
-> ([FParam SOACS] -> [[Tree (FParam SOACS)]] -> InternaliseM a)
-> InternaliseM a
bindingFParams [TypeParam]
tparams [Pat ParamType]
params [FParam SOACS] -> [[Tree (FParam SOACS)]] -> InternaliseM a
m = do
  [[(Ident ParamType, [AttrInfo VName])]]
flattened_params <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall (m :: * -> *) u.
MonadFreshNames m =>
Pat (TypeBase Size u)
-> m [(Ident (TypeBase Size u), [AttrInfo VName])]
flattenPat [Pat ParamType]
params
  let params_idents :: [(Ident ParamType, [AttrInfo VName])]
params_idents = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Ident ParamType, [AttrInfo VName])]]
flattened_params
  [[Tree (TypeBase Shape Uniqueness)]]
params_ts <-
    [ParamType] -> InternaliseM [[Tree (TypeBase Shape Uniqueness)]]
internaliseParamTypes forall a b. (a -> b) -> a -> b
$
      forall a b. (a -> b) -> [a] -> [b]
map (forall a. Info a -> a
E.unInfo forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) vn (t :: k). IdentBase f vn t -> f t
E.identType forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Ident ParamType, [AttrInfo VName])]
params_idents
  let num_param_idents :: [Int]
num_param_idents = forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length [[(Ident ParamType, [AttrInfo VName])]]
flattened_params

  let shape_params :: [Param (TypeBase shape u)]
shape_params = [forall dec. Attrs -> VName -> dec -> Param dec
I.Param forall a. Monoid a => a
mempty VName
v forall a b. (a -> b) -> a -> b
$ 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 = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p, [VName -> SubExp
I.Var forall a b. (a -> b) -> a -> b
$ forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p]) | Param (TypeBase Any Any)
p <- forall {shape} {u}. [Param (TypeBase shape u)]
shape_params]
  forall t a.
Show t =>
[(Ident ParamType, [AttrInfo VName])]
-> [t] -> ([Params t] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident ParamType, [AttrInfo VName])]
params_idents (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall (t :: * -> *) a. Foldable t => t a -> [a]
toList) [[Tree (TypeBase Shape Uniqueness)]]
params_ts) forall a b. (a -> b) -> a -> b
$ \[Params (TypeBase Shape Uniqueness)]
valueparams -> do
    let ([Param (TypeBase shape u)]
certparams, [Params (TypeBase Shape Uniqueness)]
valueparams') =
          forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. [(a, b)] -> ([a], [b])
unzip forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {shape} {u} {shape} {u}.
[Param (TypeBase shape u)]
-> ([Param (TypeBase shape u)], [Param (TypeBase shape u)])
fixAccParams [Params (TypeBase Shape Uniqueness)]
valueparams
        all_params :: Params (TypeBase Shape Uniqueness)
all_params = forall {shape} {u}. [Param (TypeBase shape u)]
certparams forall a. [a] -> [a] -> [a]
++ forall {shape} {u}. [Param (TypeBase shape u)]
shape_params forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [Params (TypeBase Shape Uniqueness)]
valueparams'
    forall rep (m :: * -> *) a.
LocalScope rep m =>
Scope rep -> m a -> m a
I.localScope (forall rep dec. (FParamInfo rep ~ dec) => [Param dec] -> Scope rep
I.scopeOfFParams Params (TypeBase Shape Uniqueness)
all_params) forall a b. (a -> b) -> a -> b
$
      forall a. Map VName [SubExp] -> InternaliseM a -> InternaliseM a
substitutingVars Map VName [SubExp]
shape_subst forall a b. (a -> b) -> a -> b
$ do
        let values_grouped_by_params :: [[Params (TypeBase Shape Uniqueness)]]
values_grouped_by_params = forall a. [Int] -> [a] -> [[a]]
chunks [Int]
num_param_idents [Params (TypeBase Shape Uniqueness)]
valueparams'
            types_grouped_by_params :: [[[Tree (TypeBase Shape Uniqueness)]]]
types_grouped_by_params = forall a. [Int] -> [a] -> [[a]]
chunks [Int]
num_param_idents [[Tree (TypeBase Shape Uniqueness)]]
params_ts

        [FParam SOACS] -> [[Tree (FParam SOACS)]] -> InternaliseM a
m (forall {shape} {u}. [Param (TypeBase shape u)]
certparams forall a. [a] -> [a] -> [a]
++ forall {shape} {u}. [Param (TypeBase shape u)]
shape_params) forall a b. (a -> b) -> a -> b
$
          forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith [[Tree (TypeBase Shape Uniqueness)]]
-> [[FParam SOACS]] -> [Tree (FParam SOACS)]
chunkValues [[[Tree (TypeBase Shape Uniqueness)]]]
types_grouped_by_params [[Params (TypeBase Shape Uniqueness)]]
values_grouped_by_params
  where
    fixAccParams :: [Param (TypeBase shape u)]
-> ([Param (TypeBase shape u)], [Param (TypeBase shape u)])
fixAccParams [Param (TypeBase shape u)]
ps =
      forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a. [Maybe a] -> [a]
catMaybes forall a b. (a -> b) -> a -> b
$ forall a b. [(a, b)] -> ([a], [b])
unzip forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {shape} {u} {shape} {u}.
Param (TypeBase shape u)
-> (Maybe (Param (TypeBase shape u)), Param (TypeBase shape u))
fixAccParam [Param (TypeBase shape u)]
ps
    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)) =
      ( forall a. a -> Maybe a
Just (forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
attrs VName
acc forall a b. (a -> b) -> a -> b
$ forall shape u. PrimType -> TypeBase shape u
I.Prim PrimType
I.Unit),
        forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
attrs VName
pv (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 = (forall a. Maybe a
Nothing, Param (TypeBase shape u)
p)

    chunkValues ::
      [[Tree (I.TypeBase I.Shape Uniqueness)]] ->
      [[I.FParam I.SOACS]] ->
      [Tree (I.FParam I.SOACS)]
    chunkValues :: [[Tree (TypeBase Shape Uniqueness)]]
-> [[FParam SOACS]] -> [Tree (FParam SOACS)]
chunkValues [[Tree (TypeBase Shape Uniqueness)]]
tss [[FParam SOACS]]
vss =
      forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall {a} {b}. [Tree a] -> [b] -> [Tree b]
f [[Tree (TypeBase Shape Uniqueness)]]
tss [[FParam SOACS]]
vss
      where
        f :: [Tree a] -> [b] -> [Tree b]
f [Tree a]
ts [b]
vs = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a b. Tree a -> [b] -> Tree b
treeLike [Tree a]
ts (forall a. [Int] -> [a] -> [[a]]
chunks (forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length [Tree a]
ts) [b]
vs)

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

  let shape_params :: [Param (TypeBase shape u)]
shape_params = [forall dec. Attrs -> VName -> dec -> Param dec
I.Param forall a. Monoid a => a
mempty VName
v forall a b. (a -> b) -> a -> b
$ 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 = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p, [VName -> SubExp
I.Var forall a b. (a -> b) -> a -> b
$ forall dec. Param dec -> VName
I.paramName Param (TypeBase Any Any)
p]) | Param (TypeBase Any Any)
p <- forall {shape} {u}. [Param (TypeBase shape u)]
shape_params]

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

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

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

type Params t = [I.Param t]

processFlatPat ::
  Show t =>
  [(E.Ident ParamType, [E.AttrInfo VName])] ->
  [t] ->
  InternaliseM ([Params t], VarSubsts)
processFlatPat :: forall t.
Show t =>
[(Ident ParamType, [AttrInfo VName])]
-> [t] -> InternaliseM ([Params t], Map VName [SubExp])
processFlatPat [(Ident ParamType, [AttrInfo VName])]
x [t]
y = forall {dec}.
[([Param dec], (VName, [SubExp]))]
-> [(Ident ParamType, [AttrInfo VName])]
-> [dec]
-> InternaliseM ([[Param dec]], Map VName [SubExp])
processFlatPat' [] [(Ident ParamType, [AttrInfo VName])]
x [t]
y
  where
    processFlatPat' :: [([Param dec], (VName, [SubExp]))]
-> [(Ident ParamType, [AttrInfo VName])]
-> [dec]
-> InternaliseM ([[Param dec]], Map VName [SubExp])
processFlatPat' [([Param dec], (VName, [SubExp]))]
pat [] [dec]
_ = do
      let ([[Param dec]]
vs, [(VName, [SubExp])]
substs) = forall a b. [(a, b)] -> ([a], [b])
unzip [([Param dec], (VName, [SubExp]))]
pat
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. [a] -> [a]
reverse [[Param dec]]
vs, forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(VName, [SubExp])]
substs)
    processFlatPat' [([Param dec], (VName, [SubExp]))]
pat ((Ident ParamType
p, [AttrInfo VName]
attrs) : [(Ident ParamType, [AttrInfo VName])]
rest) [dec]
ts = do
      Attrs
attrs' <- [AttrInfo VName] -> InternaliseM Attrs
internaliseAttrs [AttrInfo VName]
attrs
      ([Param dec]
ps, [dec]
rest_ts) <- forall {dec}. Attrs -> [dec] -> [VName] -> ([Param dec], [dec])
handleMapping Attrs
attrs' [dec]
ts forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ident ParamType -> InternaliseM [VName]
internaliseBindee Ident ParamType
p
      [([Param dec], (VName, [SubExp]))]
-> [(Ident ParamType, [AttrInfo VName])]
-> [dec]
-> InternaliseM ([[Param dec]], Map VName [SubExp])
processFlatPat'
        (([Param dec]
ps, (forall {k} (f :: k -> *) vn (t :: k). IdentBase f vn t -> vn
E.identName Ident ParamType
p, forall a b. (a -> b) -> [a] -> [b]
map (VName -> SubExp
I.Var forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall dec. Param dec -> VName
I.paramName) [Param dec]
ps)) forall a. a -> [a] -> [a]
: [([Param dec], (VName, [SubExp]))]
pat)
        [(Ident ParamType, [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 (forall dec. Attrs -> VName -> dec -> Param dec
I.Param Attrs
attrs VName
r dec
t forall a. a -> [a] -> [a]
: [Param dec]
ps, [dec]
ts')
    handleMapping Attrs
_ [] [VName]
_ =
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"handleMapping: insufficient identifiers in pattern.\n" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show ([(Ident ParamType, [AttrInfo VName])]
x, [t]
y)

    internaliseBindee :: E.Ident E.ParamType -> InternaliseM [VName]
    internaliseBindee :: Ident ParamType -> InternaliseM [VName]
internaliseBindee Ident ParamType
bindee = do
      let name :: VName
name = forall {k} (f :: k -> *) vn (t :: k). IdentBase f vn t -> vn
E.identName Ident ParamType
bindee
      case forall als. TypeBase Size als -> Int
internalisedTypeSize forall a b. (a -> b) -> a -> b
$ forall a. Info a -> a
E.unInfo forall a b. (a -> b) -> a -> b
$ forall {k} (f :: k -> *) vn (t :: k). IdentBase f vn t -> f t
E.identType Ident ParamType
bindee of
        Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [VName
name]
        Int
n -> forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadFreshNames m => [Char] -> m VName
newVName forall a b. (a -> b) -> a -> b
$ VName -> [Char]
baseString VName
name

bindingFlatPat ::
  Show t =>
  [(E.Ident E.ParamType, [E.AttrInfo VName])] ->
  [t] ->
  ([Params t] -> InternaliseM a) ->
  InternaliseM a
bindingFlatPat :: forall t a.
Show t =>
[(Ident ParamType, [AttrInfo VName])]
-> [t] -> ([Params t] -> InternaliseM a) -> InternaliseM a
bindingFlatPat [(Ident ParamType, [AttrInfo VName])]
idents [t]
ts [Params t] -> InternaliseM a
m = do
  ([Params t]
ps, Map VName [SubExp]
substs) <- forall t.
Show t =>
[(Ident ParamType, [AttrInfo VName])]
-> [t] -> InternaliseM ([Params t], Map VName [SubExp])
processFlatPat [(Ident ParamType, [AttrInfo VName])]
idents [t]
ts
  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 forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` InternaliseEnv -> Map VName [SubExp]
envSubsts InternaliseEnv
env}) forall a b. (a -> b) -> a -> b
$
    [Params t] -> InternaliseM a
m [Params t]
ps

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

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