{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE UndecidableInstances #-}

-- | This module provides facilities for transforming Futhark programs
-- such that names are unique, via the 'renameProg' function.
module Futhark.Transform.Rename
  ( -- * Renaming programs
    renameProg,

    -- * Renaming parts of a program.

    --
    -- These all require execution in a 'MonadFreshNames' environment.
    renameExp,
    renameStm,
    renameBody,
    renameLambda,
    renamePattern,
    renameSomething,

    -- * Renaming annotations
    RenameM,
    substituteRename,
    renamingStms,
    Rename (..),
    Renameable,
  )
where

import Control.Monad.Reader
import Control.Monad.State
import qualified Data.Map.Strict as M
import Data.Maybe
import Futhark.FreshNames hiding (newName)
import Futhark.IR.Prop.Names
import Futhark.IR.Prop.Patterns
import Futhark.IR.Syntax
import Futhark.IR.Traversals
import Futhark.MonadFreshNames (MonadFreshNames (..), modifyNameSource, newName)
import Futhark.Transform.Substitute

runRenamer :: RenameM a -> VNameSource -> (a, VNameSource)
runRenamer :: forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM StateT VNameSource (Reader RenameEnv) a
m) VNameSource
src = Reader RenameEnv (a, VNameSource) -> RenameEnv -> (a, VNameSource)
forall r a. Reader r a -> r -> a
runReader (StateT VNameSource (Reader RenameEnv) a
-> VNameSource -> Reader RenameEnv (a, VNameSource)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT VNameSource (Reader RenameEnv) a
m VNameSource
src) RenameEnv
env
  where
    env :: RenameEnv
env = Map VName VName -> RenameEnv
RenameEnv Map VName VName
forall k a. Map k a
M.empty

-- | Rename variables such that each is unique.  The semantics of the
-- program are unaffected, under the assumption that the program was
-- correct to begin with.  In particular, the renaming may make an
-- invalid program valid.
renameProg ::
  (Renameable lore, MonadFreshNames m) =>
  Prog lore ->
  m (Prog lore)
renameProg :: forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Prog lore -> m (Prog lore)
renameProg Prog lore
prog = (VNameSource -> (Prog lore, VNameSource)) -> m (Prog lore)
forall (m :: * -> *) a.
MonadFreshNames m =>
(VNameSource -> (a, VNameSource)) -> m a
modifyNameSource ((VNameSource -> (Prog lore, VNameSource)) -> m (Prog lore))
-> (VNameSource -> (Prog lore, VNameSource)) -> m (Prog lore)
forall a b. (a -> b) -> a -> b
$
  RenameM (Prog lore) -> VNameSource -> (Prog lore, VNameSource)
forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM (Prog lore) -> VNameSource -> (Prog lore, VNameSource))
-> RenameM (Prog lore) -> VNameSource -> (Prog lore, VNameSource)
forall a b. (a -> b) -> a -> b
$
    Stms lore
-> (Stms lore -> RenameM (Prog lore)) -> RenameM (Prog lore)
forall lore a.
Renameable lore =>
Stms lore -> (Stms lore -> RenameM a) -> RenameM a
renamingStms (Prog lore -> Stms lore
forall lore. Prog lore -> Stms lore
progConsts Prog lore
prog) ((Stms lore -> RenameM (Prog lore)) -> RenameM (Prog lore))
-> (Stms lore -> RenameM (Prog lore)) -> RenameM (Prog lore)
forall a b. (a -> b) -> a -> b
$ \Stms lore
consts -> do
      [FunDef lore]
funs <- (FunDef lore -> RenameM (FunDef lore))
-> [FunDef lore] -> RenameM [FunDef lore]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FunDef lore -> RenameM (FunDef lore)
forall a. Rename a => a -> RenameM a
rename (Prog lore -> [FunDef lore]
forall lore. Prog lore -> [FunDef lore]
progFuns Prog lore
prog)
      Prog lore -> RenameM (Prog lore)
forall (m :: * -> *) a. Monad m => a -> m a
return Prog lore
prog {progConsts :: Stms lore
progConsts = Stms lore
consts, progFuns :: [FunDef lore]
progFuns = [FunDef lore]
funs}

-- | Rename bound variables such that each is unique.  The semantics
-- of the expression is unaffected, under the assumption that the
-- expression was correct to begin with.  Any free variables are left
-- untouched.
renameExp ::
  (Renameable lore, MonadFreshNames m) =>
  Exp lore ->
  m (Exp lore)
renameExp :: forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Exp lore -> m (Exp lore)
renameExp = (VNameSource -> (Exp lore, VNameSource)) -> m (Exp lore)
forall (m :: * -> *) a.
MonadFreshNames m =>
(VNameSource -> (a, VNameSource)) -> m a
modifyNameSource ((VNameSource -> (Exp lore, VNameSource)) -> m (Exp lore))
-> (Exp lore -> VNameSource -> (Exp lore, VNameSource))
-> Exp lore
-> m (Exp lore)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameM (Exp lore) -> VNameSource -> (Exp lore, VNameSource)
forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM (Exp lore) -> VNameSource -> (Exp lore, VNameSource))
-> (Exp lore -> RenameM (Exp lore))
-> Exp lore
-> VNameSource
-> (Exp lore, VNameSource)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exp lore -> RenameM (Exp lore)
forall a. Rename a => a -> RenameM a
rename

-- | Rename bound variables such that each is unique.  The semantics
-- of the binding is unaffected, under the assumption that the
-- binding was correct to begin with.  Any free variables are left
-- untouched, as are the names in the pattern of the binding.
renameStm ::
  (Renameable lore, MonadFreshNames m) =>
  Stm lore ->
  m (Stm lore)
renameStm :: forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Stm lore -> m (Stm lore)
renameStm Stm lore
binding = do
  Exp lore
e <- Exp lore -> m (Exp lore)
forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Exp lore -> m (Exp lore)
renameExp (Exp lore -> m (Exp lore)) -> Exp lore -> m (Exp lore)
forall a b. (a -> b) -> a -> b
$ Stm lore -> Exp lore
forall lore. Stm lore -> Exp lore
stmExp Stm lore
binding
  Stm lore -> m (Stm lore)
forall (m :: * -> *) a. Monad m => a -> m a
return Stm lore
binding {stmExp :: Exp lore
stmExp = Exp lore
e}

-- | Rename bound variables such that each is unique.  The semantics
-- of the body is unaffected, under the assumption that the body was
-- correct to begin with.  Any free variables are left untouched.
renameBody ::
  (Renameable lore, MonadFreshNames m) =>
  Body lore ->
  m (Body lore)
renameBody :: forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Body lore -> m (Body lore)
renameBody = (VNameSource -> (Body lore, VNameSource)) -> m (Body lore)
forall (m :: * -> *) a.
MonadFreshNames m =>
(VNameSource -> (a, VNameSource)) -> m a
modifyNameSource ((VNameSource -> (Body lore, VNameSource)) -> m (Body lore))
-> (Body lore -> VNameSource -> (Body lore, VNameSource))
-> Body lore
-> m (Body lore)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameM (Body lore) -> VNameSource -> (Body lore, VNameSource)
forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM (Body lore) -> VNameSource -> (Body lore, VNameSource))
-> (Body lore -> RenameM (Body lore))
-> Body lore
-> VNameSource
-> (Body lore, VNameSource)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Body lore -> RenameM (Body lore)
forall a. Rename a => a -> RenameM a
rename

-- | Rename bound variables such that each is unique.  The semantics
-- of the lambda is unaffected, under the assumption that the body was
-- correct to begin with.  Any free variables are left untouched.
-- Note in particular that the parameters of the lambda are renamed.
renameLambda ::
  (Renameable lore, MonadFreshNames m) =>
  Lambda lore ->
  m (Lambda lore)
renameLambda :: forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Lambda lore -> m (Lambda lore)
renameLambda = (VNameSource -> (Lambda lore, VNameSource)) -> m (Lambda lore)
forall (m :: * -> *) a.
MonadFreshNames m =>
(VNameSource -> (a, VNameSource)) -> m a
modifyNameSource ((VNameSource -> (Lambda lore, VNameSource)) -> m (Lambda lore))
-> (Lambda lore -> VNameSource -> (Lambda lore, VNameSource))
-> Lambda lore
-> m (Lambda lore)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameM (Lambda lore) -> VNameSource -> (Lambda lore, VNameSource)
forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM (Lambda lore)
 -> VNameSource -> (Lambda lore, VNameSource))
-> (Lambda lore -> RenameM (Lambda lore))
-> Lambda lore
-> VNameSource
-> (Lambda lore, VNameSource)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lambda lore -> RenameM (Lambda lore)
forall a. Rename a => a -> RenameM a
rename

-- | Produce an equivalent pattern but with each pattern element given
-- a new name.
renamePattern ::
  (Rename dec, MonadFreshNames m) =>
  PatternT dec ->
  m (PatternT dec)
renamePattern :: forall dec (m :: * -> *).
(Rename dec, MonadFreshNames m) =>
PatternT dec -> m (PatternT dec)
renamePattern = (VNameSource -> (PatternT dec, VNameSource)) -> m (PatternT dec)
forall (m :: * -> *) a.
MonadFreshNames m =>
(VNameSource -> (a, VNameSource)) -> m a
modifyNameSource ((VNameSource -> (PatternT dec, VNameSource)) -> m (PatternT dec))
-> (PatternT dec -> VNameSource -> (PatternT dec, VNameSource))
-> PatternT dec
-> m (PatternT dec)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameM (PatternT dec)
-> VNameSource -> (PatternT dec, VNameSource)
forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM (PatternT dec)
 -> VNameSource -> (PatternT dec, VNameSource))
-> (PatternT dec -> RenameM (PatternT dec))
-> PatternT dec
-> VNameSource
-> (PatternT dec, VNameSource)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatternT dec -> RenameM (PatternT dec)
forall {dec}. Rename dec => PatternT dec -> RenameM (PatternT dec)
rename'
  where
    rename' :: PatternT dec -> RenameM (PatternT dec)
rename' PatternT dec
pat = [VName] -> RenameM (PatternT dec) -> RenameM (PatternT dec)
forall a. [VName] -> RenameM a -> RenameM a
bind (PatternT dec -> [VName]
forall dec. PatternT dec -> [VName]
patternNames PatternT dec
pat) (RenameM (PatternT dec) -> RenameM (PatternT dec))
-> RenameM (PatternT dec) -> RenameM (PatternT dec)
forall a b. (a -> b) -> a -> b
$ PatternT dec -> RenameM (PatternT dec)
forall a. Rename a => a -> RenameM a
rename PatternT dec
pat

-- | Rename the bound variables in something (does not affect free variables).
renameSomething ::
  (Rename a, MonadFreshNames m) =>
  a ->
  m a
renameSomething :: forall a (m :: * -> *). (Rename a, MonadFreshNames m) => a -> m a
renameSomething = (VNameSource -> (a, VNameSource)) -> m a
forall (m :: * -> *) a.
MonadFreshNames m =>
(VNameSource -> (a, VNameSource)) -> m a
modifyNameSource ((VNameSource -> (a, VNameSource)) -> m a)
-> (a -> VNameSource -> (a, VNameSource)) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameM a -> VNameSource -> (a, VNameSource)
forall a. RenameM a -> VNameSource -> (a, VNameSource)
runRenamer (RenameM a -> VNameSource -> (a, VNameSource))
-> (a -> RenameM a) -> a -> VNameSource -> (a, VNameSource)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> RenameM a
forall a. Rename a => a -> RenameM a
rename

newtype RenameEnv = RenameEnv {RenameEnv -> Map VName VName
envNameMap :: M.Map VName VName}

-- | The monad in which renaming is performed.
newtype RenameM a = RenameM (StateT VNameSource (Reader RenameEnv) a)
  deriving
    ( (forall a b. (a -> b) -> RenameM a -> RenameM b)
-> (forall a b. a -> RenameM b -> RenameM a) -> Functor RenameM
forall a b. a -> RenameM b -> RenameM a
forall a b. (a -> b) -> RenameM a -> RenameM 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 -> RenameM b -> RenameM a
$c<$ :: forall a b. a -> RenameM b -> RenameM a
fmap :: forall a b. (a -> b) -> RenameM a -> RenameM b
$cfmap :: forall a b. (a -> b) -> RenameM a -> RenameM b
Functor,
      Functor RenameM
Functor RenameM
-> (forall a. a -> RenameM a)
-> (forall a b. RenameM (a -> b) -> RenameM a -> RenameM b)
-> (forall a b c.
    (a -> b -> c) -> RenameM a -> RenameM b -> RenameM c)
-> (forall a b. RenameM a -> RenameM b -> RenameM b)
-> (forall a b. RenameM a -> RenameM b -> RenameM a)
-> Applicative RenameM
forall a. a -> RenameM a
forall a b. RenameM a -> RenameM b -> RenameM a
forall a b. RenameM a -> RenameM b -> RenameM b
forall a b. RenameM (a -> b) -> RenameM a -> RenameM b
forall a b c. (a -> b -> c) -> RenameM a -> RenameM b -> RenameM 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. RenameM a -> RenameM b -> RenameM a
$c<* :: forall a b. RenameM a -> RenameM b -> RenameM a
*> :: forall a b. RenameM a -> RenameM b -> RenameM b
$c*> :: forall a b. RenameM a -> RenameM b -> RenameM b
liftA2 :: forall a b c. (a -> b -> c) -> RenameM a -> RenameM b -> RenameM c
$cliftA2 :: forall a b c. (a -> b -> c) -> RenameM a -> RenameM b -> RenameM c
<*> :: forall a b. RenameM (a -> b) -> RenameM a -> RenameM b
$c<*> :: forall a b. RenameM (a -> b) -> RenameM a -> RenameM b
pure :: forall a. a -> RenameM a
$cpure :: forall a. a -> RenameM a
Applicative,
      Applicative RenameM
Applicative RenameM
-> (forall a b. RenameM a -> (a -> RenameM b) -> RenameM b)
-> (forall a b. RenameM a -> RenameM b -> RenameM b)
-> (forall a. a -> RenameM a)
-> Monad RenameM
forall a. a -> RenameM a
forall a b. RenameM a -> RenameM b -> RenameM b
forall a b. RenameM a -> (a -> RenameM b) -> RenameM 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 -> RenameM a
$creturn :: forall a. a -> RenameM a
>> :: forall a b. RenameM a -> RenameM b -> RenameM b
$c>> :: forall a b. RenameM a -> RenameM b -> RenameM b
>>= :: forall a b. RenameM a -> (a -> RenameM b) -> RenameM b
$c>>= :: forall a b. RenameM a -> (a -> RenameM b) -> RenameM b
Monad,
      Monad RenameM
Applicative RenameM
RenameM VNameSource
Applicative RenameM
-> Monad RenameM
-> RenameM VNameSource
-> (VNameSource -> RenameM ())
-> MonadFreshNames RenameM
VNameSource -> RenameM ()
forall (m :: * -> *).
Applicative m
-> Monad m
-> m VNameSource
-> (VNameSource -> m ())
-> MonadFreshNames m
putNameSource :: VNameSource -> RenameM ()
$cputNameSource :: VNameSource -> RenameM ()
getNameSource :: RenameM VNameSource
$cgetNameSource :: RenameM VNameSource
MonadFreshNames,
      MonadReader RenameEnv
    )

-- | Produce a map of the substitutions that should be performed by
-- the renamer.
renamerSubstitutions :: RenameM Substitutions
renamerSubstitutions :: RenameM (Map VName VName)
renamerSubstitutions = (RenameEnv -> Map VName VName) -> RenameM (Map VName VName)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RenameEnv -> Map VName VName
envNameMap

-- | Perform a renaming using the 'Substitute' instance.  This only
-- works if the argument does not itself perform any name binding, but
-- it can save on boilerplate for simple types.
substituteRename :: Substitute a => a -> RenameM a
substituteRename :: forall a. Substitute a => a -> RenameM a
substituteRename a
x = do
  Map VName VName
substs <- RenameM (Map VName VName)
renamerSubstitutions
  a -> RenameM a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> RenameM a) -> a -> RenameM a
forall a b. (a -> b) -> a -> b
$ Map VName VName -> a -> a
forall a. Substitute a => Map VName VName -> a -> a
substituteNames Map VName VName
substs a
x

-- | Members of class 'Rename' can be uniquely renamed.
class Rename a where
  -- | Rename the given value such that it does not contain shadowing,
  -- and has incorporated any substitutions present in the 'RenameM'
  -- environment.
  rename :: a -> RenameM a

instance Rename VName where
  rename :: VName -> RenameM VName
rename VName
name = (RenameEnv -> VName) -> RenameM VName
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (VName -> Maybe VName -> VName
forall a. a -> Maybe a -> a
fromMaybe VName
name (Maybe VName -> VName)
-> (RenameEnv -> Maybe VName) -> RenameEnv -> VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VName -> Map VName VName -> Maybe VName
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup VName
name (Map VName VName -> Maybe VName)
-> (RenameEnv -> Map VName VName) -> RenameEnv -> Maybe VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameEnv -> Map VName VName
envNameMap)

instance Rename a => Rename [a] where
  rename :: [a] -> RenameM [a]
rename = (a -> RenameM a) -> [a] -> RenameM [a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM a -> RenameM a
forall a. Rename a => a -> RenameM a
rename

instance (Rename a, Rename b) => Rename (a, b) where
  rename :: (a, b) -> RenameM (a, b)
rename (a
a, b
b) = (,) (a -> b -> (a, b)) -> RenameM a -> RenameM (b -> (a, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> RenameM a
forall a. Rename a => a -> RenameM a
rename a
a RenameM (b -> (a, b)) -> RenameM b -> RenameM (a, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> b -> RenameM b
forall a. Rename a => a -> RenameM a
rename b
b

instance (Rename a, Rename b, Rename c) => Rename (a, b, c) where
  rename :: (a, b, c) -> RenameM (a, b, c)
rename (a
a, b
b, c
c) = do
    a
a' <- a -> RenameM a
forall a. Rename a => a -> RenameM a
rename a
a
    b
b' <- b -> RenameM b
forall a. Rename a => a -> RenameM a
rename b
b
    c
c' <- c -> RenameM c
forall a. Rename a => a -> RenameM a
rename c
c
    (a, b, c) -> RenameM (a, b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a', b
b', c
c')

instance Rename a => Rename (Maybe a) where
  rename :: Maybe a -> RenameM (Maybe a)
rename = RenameM (Maybe a)
-> (a -> RenameM (Maybe a)) -> Maybe a -> RenameM (Maybe a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe a -> RenameM (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) ((a -> Maybe a) -> RenameM a -> RenameM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (RenameM a -> RenameM (Maybe a))
-> (a -> RenameM a) -> a -> RenameM (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> RenameM a
forall a. Rename a => a -> RenameM a
rename)

instance Rename Bool where
  rename :: Bool -> RenameM Bool
rename = Bool -> RenameM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return

instance Rename Ident where
  rename :: Ident -> RenameM Ident
rename (Ident VName
name Type
tp) = do
    VName
name' <- VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename VName
name
    Type
tp' <- Type -> RenameM Type
forall a. Rename a => a -> RenameM a
rename Type
tp
    Ident -> RenameM Ident
forall (m :: * -> *) a. Monad m => a -> m a
return (Ident -> RenameM Ident) -> Ident -> RenameM Ident
forall a b. (a -> b) -> a -> b
$ VName -> Type -> Ident
Ident VName
name' Type
tp'

bind :: [VName] -> RenameM a -> RenameM a
bind :: forall a. [VName] -> RenameM a -> RenameM a
bind [VName]
vars RenameM a
body = do
  [VName]
vars' <- (VName -> RenameM VName) -> [VName] -> RenameM [VName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM VName -> RenameM VName
forall (m :: * -> *). MonadFreshNames m => VName -> m VName
newName [VName]
vars
  -- This works because map union prefers elements from left
  -- operand.
  (RenameEnv -> RenameEnv) -> RenameM a -> RenameM a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ([VName] -> RenameEnv -> RenameEnv
bind' [VName]
vars') RenameM a
body
  where
    bind' :: [VName] -> RenameEnv -> RenameEnv
bind' [VName]
vars' RenameEnv
env =
      RenameEnv
env
        { envNameMap :: Map VName VName
envNameMap =
            [(VName, VName)] -> Map VName VName
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([VName] -> [VName] -> [(VName, VName)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VName]
vars [VName]
vars')
              Map VName VName -> Map VName VName -> Map VName VName
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` RenameEnv -> Map VName VName
envNameMap RenameEnv
env
        }

-- | Rename some statements, then execute an action with the name
-- substitutions induced by the statements active.
renamingStms :: Renameable lore => Stms lore -> (Stms lore -> RenameM a) -> RenameM a
renamingStms :: forall lore a.
Renameable lore =>
Stms lore -> (Stms lore -> RenameM a) -> RenameM a
renamingStms Stms lore
stms Stms lore -> RenameM a
m = Stms lore -> Stms lore -> RenameM a
descend Stms lore
forall a. Monoid a => a
mempty Stms lore
stms
  where
    descend :: Stms lore -> Stms lore -> RenameM a
descend Stms lore
stms' Stms lore
rem_stms = case Stms lore -> Maybe (Stm lore, Stms lore)
forall lore. Stms lore -> Maybe (Stm lore, Stms lore)
stmsHead Stms lore
rem_stms of
      Maybe (Stm lore, Stms lore)
Nothing -> Stms lore -> RenameM a
m Stms lore
stms'
      Just (Stm lore
stm, Stms lore
rem_stms') -> [VName] -> RenameM a -> RenameM a
forall a. [VName] -> RenameM a -> RenameM a
bind (PatternT (LetDec lore) -> [VName]
forall dec. PatternT dec -> [VName]
patternNames (PatternT (LetDec lore) -> [VName])
-> PatternT (LetDec lore) -> [VName]
forall a b. (a -> b) -> a -> b
$ Stm lore -> PatternT (LetDec lore)
forall lore. Stm lore -> Pattern lore
stmPattern Stm lore
stm) (RenameM a -> RenameM a) -> RenameM a -> RenameM a
forall a b. (a -> b) -> a -> b
$ do
        Stm lore
stm' <- Stm lore -> RenameM (Stm lore)
forall a. Rename a => a -> RenameM a
rename Stm lore
stm
        Stms lore -> Stms lore -> RenameM a
descend (Stms lore
stms' Stms lore -> Stms lore -> Stms lore
forall a. Semigroup a => a -> a -> a
<> Stm lore -> Stms lore
forall lore. Stm lore -> Stms lore
oneStm Stm lore
stm') Stms lore
rem_stms'

instance Renameable lore => Rename (FunDef lore) where
  rename :: FunDef lore -> RenameM (FunDef lore)
rename (FunDef Maybe EntryPoint
entry Attrs
attrs Name
fname [RetType lore]
ret [Param (FParamInfo lore)]
params BodyT lore
body) =
    [VName] -> RenameM (FunDef lore) -> RenameM (FunDef lore)
forall a. [VName] -> RenameM a -> RenameM a
bind ((Param (FParamInfo lore) -> VName)
-> [Param (FParamInfo lore)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param (FParamInfo lore) -> VName
forall dec. Param dec -> VName
paramName [Param (FParamInfo lore)]
params) (RenameM (FunDef lore) -> RenameM (FunDef lore))
-> RenameM (FunDef lore) -> RenameM (FunDef lore)
forall a b. (a -> b) -> a -> b
$ do
      [Param (FParamInfo lore)]
params' <- (Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore)))
-> [Param (FParamInfo lore)] -> RenameM [Param (FParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (FParamInfo lore)]
params
      BodyT lore
body' <- BodyT lore -> RenameM (BodyT lore)
forall a. Rename a => a -> RenameM a
rename BodyT lore
body
      [RetType lore]
ret' <- [RetType lore] -> RenameM [RetType lore]
forall a. Rename a => a -> RenameM a
rename [RetType lore]
ret
      FunDef lore -> RenameM (FunDef lore)
forall (m :: * -> *) a. Monad m => a -> m a
return (FunDef lore -> RenameM (FunDef lore))
-> FunDef lore -> RenameM (FunDef lore)
forall a b. (a -> b) -> a -> b
$ Maybe EntryPoint
-> Attrs
-> Name
-> [RetType lore]
-> [Param (FParamInfo lore)]
-> BodyT lore
-> FunDef lore
forall lore.
Maybe EntryPoint
-> Attrs
-> Name
-> [RetType lore]
-> [FParam lore]
-> BodyT lore
-> FunDef lore
FunDef Maybe EntryPoint
entry Attrs
attrs Name
fname [RetType lore]
ret' [Param (FParamInfo lore)]
params' BodyT lore
body'

instance Rename SubExp where
  rename :: SubExp -> RenameM SubExp
rename (Var VName
v) = VName -> SubExp
Var (VName -> SubExp) -> RenameM VName -> RenameM SubExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename VName
v
  rename (Constant PrimValue
v) = SubExp -> RenameM SubExp
forall (m :: * -> *) a. Monad m => a -> m a
return (SubExp -> RenameM SubExp) -> SubExp -> RenameM SubExp
forall a b. (a -> b) -> a -> b
$ PrimValue -> SubExp
Constant PrimValue
v

instance Rename dec => Rename (Param dec) where
  rename :: Param dec -> RenameM (Param dec)
rename (Param VName
name dec
dec) = VName -> dec -> Param dec
forall dec. VName -> dec -> Param dec
Param (VName -> dec -> Param dec)
-> RenameM VName -> RenameM (dec -> Param dec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename VName
name RenameM (dec -> Param dec) -> RenameM dec -> RenameM (Param dec)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> dec -> RenameM dec
forall a. Rename a => a -> RenameM a
rename dec
dec

instance Rename dec => Rename (PatternT dec) where
  rename :: PatternT dec -> RenameM (PatternT dec)
rename (Pattern [PatElemT dec]
context [PatElemT dec]
values) = [PatElemT dec] -> [PatElemT dec] -> PatternT dec
forall dec. [PatElemT dec] -> [PatElemT dec] -> PatternT dec
Pattern ([PatElemT dec] -> [PatElemT dec] -> PatternT dec)
-> RenameM [PatElemT dec]
-> RenameM ([PatElemT dec] -> PatternT dec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [PatElemT dec] -> RenameM [PatElemT dec]
forall a. Rename a => a -> RenameM a
rename [PatElemT dec]
context RenameM ([PatElemT dec] -> PatternT dec)
-> RenameM [PatElemT dec] -> RenameM (PatternT dec)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [PatElemT dec] -> RenameM [PatElemT dec]
forall a. Rename a => a -> RenameM a
rename [PatElemT dec]
values

instance Rename dec => Rename (PatElemT dec) where
  rename :: PatElemT dec -> RenameM (PatElemT dec)
rename (PatElem VName
ident dec
dec) = VName -> dec -> PatElemT dec
forall dec. VName -> dec -> PatElemT dec
PatElem (VName -> dec -> PatElemT dec)
-> RenameM VName -> RenameM (dec -> PatElemT dec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename VName
ident RenameM (dec -> PatElemT dec)
-> RenameM dec -> RenameM (PatElemT dec)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> dec -> RenameM dec
forall a. Rename a => a -> RenameM a
rename dec
dec

instance Rename Certificates where
  rename :: Certificates -> RenameM Certificates
rename (Certificates [VName]
cs) = [VName] -> Certificates
Certificates ([VName] -> Certificates)
-> RenameM [VName] -> RenameM Certificates
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VName] -> RenameM [VName]
forall a. Rename a => a -> RenameM a
rename [VName]
cs

instance Rename Attrs where
  rename :: Attrs -> RenameM Attrs
rename = Attrs -> RenameM Attrs
forall (f :: * -> *) a. Applicative f => a -> f a
pure

instance Rename dec => Rename (StmAux dec) where
  rename :: StmAux dec -> RenameM (StmAux dec)
rename (StmAux Certificates
cs Attrs
attrs dec
dec) =
    Certificates -> Attrs -> dec -> StmAux dec
forall dec. Certificates -> Attrs -> dec -> StmAux dec
StmAux (Certificates -> Attrs -> dec -> StmAux dec)
-> RenameM Certificates -> RenameM (Attrs -> dec -> StmAux dec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Certificates -> RenameM Certificates
forall a. Rename a => a -> RenameM a
rename Certificates
cs RenameM (Attrs -> dec -> StmAux dec)
-> RenameM Attrs -> RenameM (dec -> StmAux dec)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Attrs -> RenameM Attrs
forall a. Rename a => a -> RenameM a
rename Attrs
attrs RenameM (dec -> StmAux dec) -> RenameM dec -> RenameM (StmAux dec)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> dec -> RenameM dec
forall a. Rename a => a -> RenameM a
rename dec
dec

instance Renameable lore => Rename (Body lore) where
  rename :: Body lore -> RenameM (Body lore)
rename (Body BodyDec lore
dec Stms lore
stms Result
res) = do
    BodyDec lore
dec' <- BodyDec lore -> RenameM (BodyDec lore)
forall a. Rename a => a -> RenameM a
rename BodyDec lore
dec
    Stms lore
-> (Stms lore -> RenameM (Body lore)) -> RenameM (Body lore)
forall lore a.
Renameable lore =>
Stms lore -> (Stms lore -> RenameM a) -> RenameM a
renamingStms Stms lore
stms ((Stms lore -> RenameM (Body lore)) -> RenameM (Body lore))
-> (Stms lore -> RenameM (Body lore)) -> RenameM (Body lore)
forall a b. (a -> b) -> a -> b
$ \Stms lore
stms' ->
      BodyDec lore -> Stms lore -> Result -> Body lore
forall lore. BodyDec lore -> Stms lore -> Result -> BodyT lore
Body BodyDec lore
dec' Stms lore
stms' (Result -> Body lore) -> RenameM Result -> RenameM (Body lore)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Result -> RenameM Result
forall a. Rename a => a -> RenameM a
rename Result
res

instance Renameable lore => Rename (Stm lore) where
  rename :: Stm lore -> RenameM (Stm lore)
rename (Let PatternT (LetDec lore)
pat StmAux (ExpDec lore)
elore Exp lore
e) = PatternT (LetDec lore)
-> StmAux (ExpDec lore) -> Exp lore -> Stm lore
forall lore.
Pattern lore -> StmAux (ExpDec lore) -> Exp lore -> Stm lore
Let (PatternT (LetDec lore)
 -> StmAux (ExpDec lore) -> Exp lore -> Stm lore)
-> RenameM (PatternT (LetDec lore))
-> RenameM (StmAux (ExpDec lore) -> Exp lore -> Stm lore)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PatternT (LetDec lore) -> RenameM (PatternT (LetDec lore))
forall a. Rename a => a -> RenameM a
rename PatternT (LetDec lore)
pat RenameM (StmAux (ExpDec lore) -> Exp lore -> Stm lore)
-> RenameM (StmAux (ExpDec lore)) -> RenameM (Exp lore -> Stm lore)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> StmAux (ExpDec lore) -> RenameM (StmAux (ExpDec lore))
forall a. Rename a => a -> RenameM a
rename StmAux (ExpDec lore)
elore RenameM (Exp lore -> Stm lore)
-> RenameM (Exp lore) -> RenameM (Stm lore)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Exp lore -> RenameM (Exp lore)
forall a. Rename a => a -> RenameM a
rename Exp lore
e

instance Renameable lore => Rename (Exp lore) where
  rename :: Exp lore -> RenameM (Exp lore)
rename (DoLoop [(Param (FParamInfo lore), SubExp)]
ctx [(Param (FParamInfo lore), SubExp)]
val LoopForm lore
form BodyT lore
loopbody) = do
    let ([Param (FParamInfo lore)]
ctxparams, Result
ctxinit) = [(Param (FParamInfo lore), SubExp)]
-> ([Param (FParamInfo lore)], Result)
forall a b. [(a, b)] -> ([a], [b])
unzip [(Param (FParamInfo lore), SubExp)]
ctx
        ([Param (FParamInfo lore)]
valparams, Result
valinit) = [(Param (FParamInfo lore), SubExp)]
-> ([Param (FParamInfo lore)], Result)
forall a b. [(a, b)] -> ([a], [b])
unzip [(Param (FParamInfo lore), SubExp)]
val
    Result
ctxinit' <- (SubExp -> RenameM SubExp) -> Result -> RenameM Result
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM SubExp -> RenameM SubExp
forall a. Rename a => a -> RenameM a
rename Result
ctxinit
    Result
valinit' <- (SubExp -> RenameM SubExp) -> Result -> RenameM Result
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM SubExp -> RenameM SubExp
forall a. Rename a => a -> RenameM a
rename Result
valinit
    case LoopForm lore
form of
      -- It is important that 'i' is renamed before the loop_vars, as
      -- 'i' may be used in the annotations for loop_vars (e.g. index
      -- functions).
      ForLoop VName
i IntType
it SubExp
boundexp [(Param (LParamInfo lore), VName)]
loop_vars -> [VName] -> RenameM (Exp lore) -> RenameM (Exp lore)
forall a. [VName] -> RenameM a -> RenameM a
bind [VName
i] (RenameM (Exp lore) -> RenameM (Exp lore))
-> RenameM (Exp lore) -> RenameM (Exp lore)
forall a b. (a -> b) -> a -> b
$ do
        let ([Param (LParamInfo lore)]
loop_params, [VName]
loop_arrs) = [(Param (LParamInfo lore), VName)]
-> ([Param (LParamInfo lore)], [VName])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Param (LParamInfo lore), VName)]
loop_vars
        SubExp
boundexp' <- SubExp -> RenameM SubExp
forall a. Rename a => a -> RenameM a
rename SubExp
boundexp
        [VName]
loop_arrs' <- [VName] -> RenameM [VName]
forall a. Rename a => a -> RenameM a
rename [VName]
loop_arrs
        [VName] -> RenameM (Exp lore) -> RenameM (Exp lore)
forall a. [VName] -> RenameM a -> RenameM a
bind
          ( (Param (FParamInfo lore) -> VName)
-> [Param (FParamInfo lore)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param (FParamInfo lore) -> VName
forall dec. Param dec -> VName
paramName ([Param (FParamInfo lore)]
ctxparams [Param (FParamInfo lore)]
-> [Param (FParamInfo lore)] -> [Param (FParamInfo lore)]
forall a. [a] -> [a] -> [a]
++ [Param (FParamInfo lore)]
valparams)
              [VName] -> [VName] -> [VName]
forall a. [a] -> [a] -> [a]
++ (Param (LParamInfo lore) -> VName)
-> [Param (LParamInfo lore)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param (LParamInfo lore) -> VName
forall dec. Param dec -> VName
paramName [Param (LParamInfo lore)]
loop_params
          )
          (RenameM (Exp lore) -> RenameM (Exp lore))
-> RenameM (Exp lore) -> RenameM (Exp lore)
forall a b. (a -> b) -> a -> b
$ do
            [Param (FParamInfo lore)]
ctxparams' <- (Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore)))
-> [Param (FParamInfo lore)] -> RenameM [Param (FParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (FParamInfo lore)]
ctxparams
            [Param (FParamInfo lore)]
valparams' <- (Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore)))
-> [Param (FParamInfo lore)] -> RenameM [Param (FParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (FParamInfo lore)]
valparams
            [Param (LParamInfo lore)]
loop_params' <- (Param (LParamInfo lore) -> RenameM (Param (LParamInfo lore)))
-> [Param (LParamInfo lore)] -> RenameM [Param (LParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (LParamInfo lore) -> RenameM (Param (LParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (LParamInfo lore)]
loop_params
            VName
i' <- VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename VName
i
            BodyT lore
loopbody' <- BodyT lore -> RenameM (BodyT lore)
forall a. Rename a => a -> RenameM a
rename BodyT lore
loopbody
            Exp lore -> RenameM (Exp lore)
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp lore -> RenameM (Exp lore)) -> Exp lore -> RenameM (Exp lore)
forall a b. (a -> b) -> a -> b
$
              [(Param (FParamInfo lore), SubExp)]
-> [(Param (FParamInfo lore), SubExp)]
-> LoopForm lore
-> BodyT lore
-> Exp lore
forall lore.
[(FParam lore, SubExp)]
-> [(FParam lore, SubExp)]
-> LoopForm lore
-> BodyT lore
-> ExpT lore
DoLoop
                ([Param (FParamInfo lore)]
-> Result -> [(Param (FParamInfo lore), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (FParamInfo lore)]
ctxparams' Result
ctxinit')
                ([Param (FParamInfo lore)]
-> Result -> [(Param (FParamInfo lore), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (FParamInfo lore)]
valparams' Result
valinit')
                ( VName
-> IntType
-> SubExp
-> [(Param (LParamInfo lore), VName)]
-> LoopForm lore
forall lore.
VName
-> IntType -> SubExp -> [(LParam lore, VName)] -> LoopForm lore
ForLoop VName
i' IntType
it SubExp
boundexp' ([(Param (LParamInfo lore), VName)] -> LoopForm lore)
-> [(Param (LParamInfo lore), VName)] -> LoopForm lore
forall a b. (a -> b) -> a -> b
$
                    [Param (LParamInfo lore)]
-> [VName] -> [(Param (LParamInfo lore), VName)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (LParamInfo lore)]
loop_params' [VName]
loop_arrs'
                )
                BodyT lore
loopbody'
      WhileLoop VName
cond ->
        [VName] -> RenameM (Exp lore) -> RenameM (Exp lore)
forall a. [VName] -> RenameM a -> RenameM a
bind ((Param (FParamInfo lore) -> VName)
-> [Param (FParamInfo lore)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param (FParamInfo lore) -> VName
forall dec. Param dec -> VName
paramName ([Param (FParamInfo lore)] -> [VName])
-> [Param (FParamInfo lore)] -> [VName]
forall a b. (a -> b) -> a -> b
$ [Param (FParamInfo lore)]
ctxparams [Param (FParamInfo lore)]
-> [Param (FParamInfo lore)] -> [Param (FParamInfo lore)]
forall a. [a] -> [a] -> [a]
++ [Param (FParamInfo lore)]
valparams) (RenameM (Exp lore) -> RenameM (Exp lore))
-> RenameM (Exp lore) -> RenameM (Exp lore)
forall a b. (a -> b) -> a -> b
$ do
          [Param (FParamInfo lore)]
ctxparams' <- (Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore)))
-> [Param (FParamInfo lore)] -> RenameM [Param (FParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (FParamInfo lore)]
ctxparams
          [Param (FParamInfo lore)]
valparams' <- (Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore)))
-> [Param (FParamInfo lore)] -> RenameM [Param (FParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (FParamInfo lore)]
valparams
          BodyT lore
loopbody' <- BodyT lore -> RenameM (BodyT lore)
forall a. Rename a => a -> RenameM a
rename BodyT lore
loopbody
          VName
cond' <- VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename VName
cond
          Exp lore -> RenameM (Exp lore)
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp lore -> RenameM (Exp lore)) -> Exp lore -> RenameM (Exp lore)
forall a b. (a -> b) -> a -> b
$
            [(Param (FParamInfo lore), SubExp)]
-> [(Param (FParamInfo lore), SubExp)]
-> LoopForm lore
-> BodyT lore
-> Exp lore
forall lore.
[(FParam lore, SubExp)]
-> [(FParam lore, SubExp)]
-> LoopForm lore
-> BodyT lore
-> ExpT lore
DoLoop
              ([Param (FParamInfo lore)]
-> Result -> [(Param (FParamInfo lore), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (FParamInfo lore)]
ctxparams' Result
ctxinit')
              ([Param (FParamInfo lore)]
-> Result -> [(Param (FParamInfo lore), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (FParamInfo lore)]
valparams' Result
valinit')
              (VName -> LoopForm lore
forall lore. VName -> LoopForm lore
WhileLoop VName
cond')
              BodyT lore
loopbody'
  rename Exp lore
e = Mapper lore lore RenameM -> Exp lore -> RenameM (Exp lore)
forall (m :: * -> *) flore tlore.
(Applicative m, Monad m) =>
Mapper flore tlore m -> Exp flore -> m (Exp tlore)
mapExpM Mapper lore lore RenameM
mapper Exp lore
e
    where
      mapper :: Mapper lore lore RenameM
mapper =
        Mapper :: forall flore tlore (m :: * -> *).
(SubExp -> m SubExp)
-> (Scope tlore -> Body flore -> m (Body tlore))
-> (VName -> m VName)
-> (RetType flore -> m (RetType tlore))
-> (BranchType flore -> m (BranchType tlore))
-> (FParam flore -> m (FParam tlore))
-> (LParam flore -> m (LParam tlore))
-> (Op flore -> m (Op tlore))
-> Mapper flore tlore m
Mapper
          { mapOnBody :: Scope lore -> BodyT lore -> RenameM (BodyT lore)
mapOnBody = (BodyT lore -> RenameM (BodyT lore))
-> Scope lore -> BodyT lore -> RenameM (BodyT lore)
forall a b. a -> b -> a
const BodyT lore -> RenameM (BodyT lore)
forall a. Rename a => a -> RenameM a
rename,
            mapOnSubExp :: SubExp -> RenameM SubExp
mapOnSubExp = SubExp -> RenameM SubExp
forall a. Rename a => a -> RenameM a
rename,
            mapOnVName :: VName -> RenameM VName
mapOnVName = VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename,
            mapOnRetType :: RetType lore -> RenameM (RetType lore)
mapOnRetType = RetType lore -> RenameM (RetType lore)
forall a. Rename a => a -> RenameM a
rename,
            mapOnBranchType :: BranchType lore -> RenameM (BranchType lore)
mapOnBranchType = BranchType lore -> RenameM (BranchType lore)
forall a. Rename a => a -> RenameM a
rename,
            mapOnFParam :: Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
mapOnFParam = Param (FParamInfo lore) -> RenameM (Param (FParamInfo lore))
forall a. Rename a => a -> RenameM a
rename,
            mapOnLParam :: Param (LParamInfo lore) -> RenameM (Param (LParamInfo lore))
mapOnLParam = Param (LParamInfo lore) -> RenameM (Param (LParamInfo lore))
forall a. Rename a => a -> RenameM a
rename,
            mapOnOp :: Op lore -> RenameM (Op lore)
mapOnOp = Op lore -> RenameM (Op lore)
forall a. Rename a => a -> RenameM a
rename
          }

instance
  Rename shape =>
  Rename (TypeBase shape u)
  where
  rename :: TypeBase shape u -> RenameM (TypeBase shape u)
rename (Array PrimType
et shape
size u
u) = do
    shape
size' <- shape -> RenameM shape
forall a. Rename a => a -> RenameM a
rename shape
size
    TypeBase shape u -> RenameM (TypeBase shape u)
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeBase shape u -> RenameM (TypeBase shape u))
-> TypeBase shape u -> RenameM (TypeBase shape u)
forall a b. (a -> b) -> a -> b
$ PrimType -> shape -> u -> TypeBase shape u
forall shape u. PrimType -> shape -> u -> TypeBase shape u
Array PrimType
et shape
size' u
u
  rename (Prim PrimType
et) = TypeBase shape u -> RenameM (TypeBase shape u)
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeBase shape u -> RenameM (TypeBase shape u))
-> TypeBase shape u -> RenameM (TypeBase shape u)
forall a b. (a -> b) -> a -> b
$ PrimType -> TypeBase shape u
forall shape u. PrimType -> TypeBase shape u
Prim PrimType
et
  rename (Mem Space
space) = TypeBase shape u -> RenameM (TypeBase shape u)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TypeBase shape u -> RenameM (TypeBase shape u))
-> TypeBase shape u -> RenameM (TypeBase shape u)
forall a b. (a -> b) -> a -> b
$ Space -> TypeBase shape u
forall shape u. Space -> TypeBase shape u
Mem Space
space

instance Renameable lore => Rename (Lambda lore) where
  rename :: Lambda lore -> RenameM (Lambda lore)
rename (Lambda [Param (LParamInfo lore)]
params BodyT lore
body [Type]
ret) =
    [VName] -> RenameM (Lambda lore) -> RenameM (Lambda lore)
forall a. [VName] -> RenameM a -> RenameM a
bind ((Param (LParamInfo lore) -> VName)
-> [Param (LParamInfo lore)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param (LParamInfo lore) -> VName
forall dec. Param dec -> VName
paramName [Param (LParamInfo lore)]
params) (RenameM (Lambda lore) -> RenameM (Lambda lore))
-> RenameM (Lambda lore) -> RenameM (Lambda lore)
forall a b. (a -> b) -> a -> b
$ do
      [Param (LParamInfo lore)]
params' <- (Param (LParamInfo lore) -> RenameM (Param (LParamInfo lore)))
-> [Param (LParamInfo lore)] -> RenameM [Param (LParamInfo lore)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Param (LParamInfo lore) -> RenameM (Param (LParamInfo lore))
forall a. Rename a => a -> RenameM a
rename [Param (LParamInfo lore)]
params
      BodyT lore
body' <- BodyT lore -> RenameM (BodyT lore)
forall a. Rename a => a -> RenameM a
rename BodyT lore
body
      [Type]
ret' <- (Type -> RenameM Type) -> [Type] -> RenameM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> RenameM Type
forall a. Rename a => a -> RenameM a
rename [Type]
ret
      Lambda lore -> RenameM (Lambda lore)
forall (m :: * -> *) a. Monad m => a -> m a
return (Lambda lore -> RenameM (Lambda lore))
-> Lambda lore -> RenameM (Lambda lore)
forall a b. (a -> b) -> a -> b
$ [Param (LParamInfo lore)] -> BodyT lore -> [Type] -> Lambda lore
forall lore. [LParam lore] -> BodyT lore -> [Type] -> LambdaT lore
Lambda [Param (LParamInfo lore)]
params' BodyT lore
body' [Type]
ret'

instance Rename Names where
  rename :: Names -> RenameM Names
rename = ([VName] -> Names) -> RenameM [VName] -> RenameM Names
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [VName] -> Names
namesFromList (RenameM [VName] -> RenameM Names)
-> (Names -> RenameM [VName]) -> Names -> RenameM Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName -> RenameM VName) -> [VName] -> RenameM [VName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM VName -> RenameM VName
forall a. Rename a => a -> RenameM a
rename ([VName] -> RenameM [VName])
-> (Names -> [VName]) -> Names -> RenameM [VName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Names -> [VName]
namesToList

instance Rename Rank where
  rename :: Rank -> RenameM Rank
rename = Rank -> RenameM Rank
forall (m :: * -> *) a. Monad m => a -> m a
return

instance Rename d => Rename (ShapeBase d) where
  rename :: ShapeBase d -> RenameM (ShapeBase d)
rename (Shape [d]
l) = [d] -> ShapeBase d
forall d. [d] -> ShapeBase d
Shape ([d] -> ShapeBase d) -> RenameM [d] -> RenameM (ShapeBase d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (d -> RenameM d) -> [d] -> RenameM [d]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM d -> RenameM d
forall a. Rename a => a -> RenameM a
rename [d]
l

instance Rename ExtSize where
  rename :: ExtSize -> RenameM ExtSize
rename (Free SubExp
se) = SubExp -> ExtSize
forall a. a -> Ext a
Free (SubExp -> ExtSize) -> RenameM SubExp -> RenameM ExtSize
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SubExp -> RenameM SubExp
forall a. Rename a => a -> RenameM a
rename SubExp
se
  rename (Ext Int
x) = ExtSize -> RenameM ExtSize
forall (m :: * -> *) a. Monad m => a -> m a
return (ExtSize -> RenameM ExtSize) -> ExtSize -> RenameM ExtSize
forall a b. (a -> b) -> a -> b
$ Int -> ExtSize
forall a. Int -> Ext a
Ext Int
x

instance Rename () where
  rename :: () -> RenameM ()
rename = () -> RenameM ()
forall (m :: * -> *) a. Monad m => a -> m a
return

instance Rename d => Rename (DimIndex d) where
  rename :: DimIndex d -> RenameM (DimIndex d)
rename (DimFix d
i) = d -> DimIndex d
forall d. d -> DimIndex d
DimFix (d -> DimIndex d) -> RenameM d -> RenameM (DimIndex d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> d -> RenameM d
forall a. Rename a => a -> RenameM a
rename d
i
  rename (DimSlice d
i d
n d
s) = d -> d -> d -> DimIndex d
forall d. d -> d -> d -> DimIndex d
DimSlice (d -> d -> d -> DimIndex d)
-> RenameM d -> RenameM (d -> d -> DimIndex d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> d -> RenameM d
forall a. Rename a => a -> RenameM a
rename d
i RenameM (d -> d -> DimIndex d)
-> RenameM d -> RenameM (d -> DimIndex d)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> d -> RenameM d
forall a. Rename a => a -> RenameM a
rename d
n RenameM (d -> DimIndex d) -> RenameM d -> RenameM (DimIndex d)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> d -> RenameM d
forall a. Rename a => a -> RenameM a
rename d
s

-- | Lores in which all annotations are renameable.
type Renameable lore =
  ( Rename (LetDec lore),
    Rename (ExpDec lore),
    Rename (BodyDec lore),
    Rename (FParamInfo lore),
    Rename (LParamInfo lore),
    Rename (RetType lore),
    Rename (BranchType lore),
    Rename (Op lore)
  )