module Gibbon.NewL2.FromOldL2 ( fromOldL2, toOldL2, toOldL2Exp ) where

import qualified Data.Map as M
import qualified Data.Set as S

import           Gibbon.L2.Syntax
import qualified Gibbon.NewL2.Syntax as New
import           Gibbon.Common

--------------------------------------------------------------------------------

-- Maps a location to a region
type LocEnv = M.Map LocVar New.LocArg

fromOldL2 :: Prog2 -> PassM New.Prog2
fromOldL2 :: Prog2 -> PassM Prog2
fromOldL2 Prog{DDefs (TyOf Exp2)
ddefs :: DDefs (TyOf Exp2)
ddefs :: forall ex. Prog ex -> DDefs (TyOf ex)
ddefs,FunDefs Exp2
fundefs :: FunDefs Exp2
fundefs :: forall ex. Prog ex -> FunDefs ex
fundefs,Maybe (Exp2, TyOf Exp2)
mainExp :: Maybe (Exp2, TyOf Exp2)
mainExp :: forall ex. Prog ex -> Maybe (ex, TyOf ex)
mainExp} = do
  let ddefs' :: Map Var (DDef Ty2)
ddefs' = (DDef (UrTy Var) -> DDef Ty2)
-> Map Var (DDef (UrTy Var)) -> Map Var (DDef Ty2)
forall a b k. (a -> b) -> Map k a -> Map k b
M.map ((UrTy Var -> Ty2) -> DDef (UrTy Var) -> DDef Ty2
forall a b. (a -> b) -> DDef a -> DDef b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UrTy Var -> Ty2
New.MkTy2) DDefs (TyOf Exp2)
Map Var (DDef (UrTy Var))
ddefs
  [FunDef2]
fds' <- (FunDef2 -> PassM FunDef2) -> [FunDef2] -> PassM [FunDef2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Map Var (DDef (UrTy Var))
-> FunDefs Exp2 -> FunDef2 -> PassM FunDef2
fromOldL2Fn DDefs (TyOf Exp2)
Map Var (DDef (UrTy Var))
ddefs FunDefs Exp2
fundefs) ([FunDef2] -> PassM [FunDef2]) -> [FunDef2] -> PassM [FunDef2]
forall a b. (a -> b) -> a -> b
$ FunDefs Exp2 -> [FunDef2]
forall k a. Map k a -> [a]
M.elems FunDefs Exp2
fundefs
  let fundefs' :: Map Var FunDef2
fundefs' = [(Var, FunDef2)] -> Map Var FunDef2
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Var, FunDef2)] -> Map Var FunDef2)
-> [(Var, FunDef2)] -> Map Var FunDef2
forall a b. (a -> b) -> a -> b
$ (FunDef2 -> (Var, FunDef2)) -> [FunDef2] -> [(Var, FunDef2)]
forall a b. (a -> b) -> [a] -> [b]
map (\FunDef2
f -> (FunDef2 -> Var
forall ex. FunDef ex -> Var
funName FunDef2
f,FunDef2
f)) [FunDef2]
fds'
      env2 :: Env2 (UrTy Var)
env2 = TyEnv (UrTy Var) -> TyEnv (ArrowTy (UrTy Var)) -> Env2 (UrTy Var)
forall a. TyEnv a -> TyEnv (ArrowTy a) -> Env2 a
Env2 TyEnv (UrTy Var)
forall k a. Map k a
M.empty (FunDefs Exp2 -> TyEnv (ArrowTy (TyOf Exp2))
forall a. FunDefs a -> TyEnv (ArrowTy (TyOf a))
initFunEnv FunDefs Exp2
fundefs)
  Maybe (Exp2, Ty2)
mainExp' <- case Maybe (Exp2, TyOf Exp2)
mainExp of
                Maybe (Exp2, TyOf Exp2)
Nothing -> Maybe (Exp2, Ty2) -> PassM (Maybe (Exp2, Ty2))
forall a. a -> PassM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Exp2, Ty2)
forall a. Maybe a
Nothing
                Just (Exp2
mn, TyOf Exp2
ty) -> do
                  let ty' :: Ty2
ty' = UrTy Var -> Ty2
New.MkTy2 TyOf Exp2
UrTy Var
ty
                  Exp2
mn' <- Map Var (DDef (UrTy Var))
-> FunDefs Exp2 -> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
fromOldL2Exp DDefs (TyOf Exp2)
Map Var (DDef (UrTy Var))
ddefs FunDefs Exp2
fundefs LocEnv
forall k a. Map k a
M.empty Env2 (UrTy Var)
env2 Exp2
mn
                  Maybe (Exp2, Ty2) -> PassM (Maybe (Exp2, Ty2))
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Exp2, Ty2) -> PassM (Maybe (Exp2, Ty2)))
-> Maybe (Exp2, Ty2) -> PassM (Maybe (Exp2, Ty2))
forall a b. (a -> b) -> a -> b
$ (Exp2, Ty2) -> Maybe (Exp2, Ty2)
forall a. a -> Maybe a
Just (Exp2
mn', Ty2
ty')
  Prog2 -> PassM Prog2
forall a. a -> PassM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Prog2 -> PassM Prog2) -> Prog2 -> PassM Prog2
forall a b. (a -> b) -> a -> b
$ DDefs (TyOf Exp2)
-> Map Var FunDef2 -> Maybe (Exp2, TyOf Exp2) -> Prog2
forall ex.
DDefs (TyOf ex) -> FunDefs ex -> Maybe (ex, TyOf ex) -> Prog ex
Prog DDefs (TyOf Exp2)
Map Var (DDef Ty2)
ddefs' Map Var FunDef2
fundefs' Maybe (Exp2, TyOf Exp2)
Maybe (Exp2, Ty2)
mainExp'


fromOldL2Fn :: DDefs Ty2 -> FunDefs2 -> FunDef2 -> PassM New.FunDef2
fromOldL2Fn :: Map Var (DDef (UrTy Var))
-> FunDefs Exp2 -> FunDef2 -> PassM FunDef2
fromOldL2Fn Map Var (DDef (UrTy Var))
ddefs FunDefs Exp2
fundefs f :: FunDef2
f@FunDef{[Var]
funArgs :: [Var]
funArgs :: forall ex. FunDef ex -> [Var]
funArgs,ArrowTy (TyOf Exp2)
funTy :: ArrowTy (TyOf Exp2)
funTy :: forall ex. FunDef ex -> ArrowTy (TyOf ex)
funTy,Exp2
funBody :: Exp2
funBody :: forall ex. FunDef ex -> ex
funBody} = do
  let initTyEnv :: TyEnv (UrTy Var)
initTyEnv  = [(Var, UrTy Var)] -> TyEnv (UrTy Var)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Var, UrTy Var)] -> TyEnv (UrTy Var))
-> [(Var, UrTy Var)] -> TyEnv (UrTy Var)
forall a b. (a -> b) -> a -> b
$ [Var] -> [UrTy Var] -> [(Var, UrTy Var)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Var]
funArgs (ArrowTy2 (UrTy Var) -> [UrTy Var]
forall ty2. ArrowTy2 ty2 -> [ty2]
arrIns ArrowTy (TyOf Exp2)
ArrowTy2 (UrTy Var)
funTy)
      env2 :: Env2 (UrTy Var)
env2 = TyEnv (UrTy Var) -> TyEnv (ArrowTy (UrTy Var)) -> Env2 (UrTy Var)
forall a. TyEnv a -> TyEnv (ArrowTy a) -> Env2 a
Env2 TyEnv (UrTy Var)
initTyEnv (FunDefs Exp2 -> TyEnv (ArrowTy (TyOf Exp2))
forall a. FunDefs a -> TyEnv (ArrowTy (TyOf a))
initFunEnv FunDefs Exp2
fundefs)
      initLocEnv :: LocEnv
initLocEnv = [(Var, LocArg)] -> LocEnv
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Var, LocArg)] -> LocEnv) -> [(Var, LocArg)] -> LocEnv
forall a b. (a -> b) -> a -> b
$ (LRM -> (Var, LocArg)) -> [LRM] -> [(Var, LocArg)]
forall a b. (a -> b) -> [a] -> [b]
map (\LRM
lrm -> (LRM -> Var
lrmLoc LRM
lrm, LREM -> LocArg
New.Loc (LRM -> LREM
New.fromLRM LRM
lrm))) (ArrowTy2 (UrTy Var) -> [LRM]
forall ty2. ArrowTy2 ty2 -> [LRM]
locVars ArrowTy (TyOf Exp2)
ArrowTy2 (UrTy Var)
funTy)
  Exp2
bod' <- Map Var (DDef (UrTy Var))
-> FunDefs Exp2 -> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
fromOldL2Exp Map Var (DDef (UrTy Var))
ddefs FunDefs Exp2
fundefs LocEnv
initLocEnv Env2 (UrTy Var)
env2 Exp2
funBody
  FunDef2 -> PassM FunDef2
forall a. a -> PassM a
forall (m :: * -> *) a. Monad m => a -> m a
return (FunDef2 -> PassM FunDef2) -> FunDef2 -> PassM FunDef2
forall a b. (a -> b) -> a -> b
$ FunDef2
f { funBody :: Exp2
funBody = Exp2
bod', funTy :: ArrowTy (TyOf Exp2)
funTy = (UrTy Var -> Ty2) -> ArrowTy2 (UrTy Var) -> ArrowTy2 Ty2
forall a b. (a -> b) -> ArrowTy2 a -> ArrowTy2 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UrTy Var -> Ty2
New.MkTy2 ArrowTy (TyOf Exp2)
ArrowTy2 (UrTy Var)
funTy }


fromOldL2Exp :: DDefs Ty2 -> FunDefs2 -> LocEnv -> Env2 Ty2 -> Exp2 -> PassM New.Exp2
fromOldL2Exp :: Map Var (DDef (UrTy Var))
-> FunDefs Exp2 -> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
fromOldL2Exp Map Var (DDef (UrTy Var))
ddefs FunDefs Exp2
fundefs LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
ex =
  case Exp2
ex of
    AppE Var
f [Var]
locs [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2) [Exp2]
args
      let locargs :: [LocArg]
locargs = (Var -> LocArg) -> [Var] -> [LocArg]
forall a b. (a -> b) -> [a] -> [b]
map (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# ) [Var]
locs
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [LocArg] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> [loc] -> [PreExp ext loc dec] -> PreExp ext loc dec
AppE Var
f [LocArg]
locargs [Exp2]
args'

    SpawnE Var
f [Var]
locs [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2) [Exp2]
args
      let locargs :: [LocArg]
locargs = (Var -> LocArg) -> [Var] -> [LocArg]
forall a b. (a -> b) -> [a] -> [b]
map (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# ) [Var]
locs
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [LocArg] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> [loc] -> [PreExp ext loc dec] -> PreExp ext loc dec
SpawnE Var
f [LocArg]
locargs [Exp2]
args'

    PrimAppE Prim (UrTy Var)
pr [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2) [Exp2]
args
      let pr' :: Prim Ty2
pr' = (UrTy Var -> Ty2) -> Prim (UrTy Var) -> Prim Ty2
forall a b. (a -> b) -> Prim a -> Prim b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UrTy Var -> Ty2
New.MkTy2 Prim (UrTy Var)
pr
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Prim Ty2 -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Prim dec -> [PreExp ext loc dec] -> PreExp ext loc dec
PrimAppE Prim Ty2
pr' [Exp2]
args'

    LetE (Var
v, [Var]
ewitnesses, UrTy Var
ty, Exp2
rhs) Exp2
bod

      | SpawnE Var
f [Var]
applocs [Exp2]
args <- Exp2
rhs
      , Bool -> Bool
not ([Var] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Var]
ewitnesses) ->
          do let e :: Exp2
e = (Var, [Var], UrTy Var, Exp2) -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
(Var, [loc], dec, PreExp ext loc dec)
-> PreExp ext loc dec -> PreExp ext loc dec
LetE (Var
v, [Var]
ewitnesses, UrTy Var
ty, Var -> [Var] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> [loc] -> [PreExp ext loc dec] -> PreExp ext loc dec
AppE Var
f [Var]
applocs [Exp2]
args) Exp2
bod
             (LetE (Var
v', [LocArg]
ewitnesses', Ty2
ty', AppE Var
f' [LocArg]
applocs' [Exp2]
args') Exp2
bod') <-
               LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
e
             Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ (Var, [LocArg], Ty2, Exp2) -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
(Var, [loc], dec, PreExp ext loc dec)
-> PreExp ext loc dec -> PreExp ext loc dec
LetE (Var
v', [LocArg]
ewitnesses', Ty2
ty', Var -> [LocArg] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> [loc] -> [PreExp ext loc dec] -> PreExp ext loc dec
SpawnE Var
f' [LocArg]
applocs' [Exp2]
args') Exp2
bod'

      | AppE Var
f [Var]
_applocs [Exp2]
args <- Exp2
rhs
      , Bool -> Bool
not ([Var] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Var]
ewitnesses) ->
          do let fty :: ArrowTy (UrTy Var)
fty = Var -> Env2 (UrTy Var) -> ArrowTy (UrTy Var)
forall a. Out (ArrowTy a) => Var -> Env2 a -> ArrowTy a
lookupFEnv Var
f Env2 (UrTy Var)
env2
                 effs :: Set Effect
effs = ArrowTy2 (UrTy Var) -> Set Effect
forall ty2. ArrowTy2 ty2 -> Set Effect
arrEffs ArrowTy (UrTy Var)
ArrowTy2 (UrTy Var)
fty
                 intys :: [UrTy Var]
intys = ArrowTy2 (UrTy Var) -> [UrTy Var]
forall ty2. ArrowTy2 ty2 -> [ty2]
arrIns ArrowTy (UrTy Var)
ArrowTy2 (UrTy Var)
fty
                 traversed_arg_pos :: [Int]
traversed_arg_pos =
                   ((Int, UrTy Var) -> Int) -> [(Int, UrTy Var)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, UrTy Var) -> Int
forall a b. (a, b) -> a
fst ([(Int, UrTy Var)] -> [Int]) -> [(Int, UrTy Var)] -> [Int]
forall a b. (a -> b) -> a -> b
$
                   ((Int, UrTy Var) -> Bool) -> [(Int, UrTy Var)] -> [(Int, UrTy Var)]
forall a. (a -> Bool) -> [a] -> [a]
filter
                     (\(Int
_i,UrTy Var
inty) ->
                        case UrTy Var
inty of
                          PackedTy [Char]
_ Var
loc -> Effect -> Set Effect -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member (Var -> Effect
Traverse Var
loc) Set Effect
effs
                          UrTy Var
_ -> Bool
False)
                     ([Int] -> [UrTy Var] -> [(Int, UrTy Var)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [UrTy Var]
intys)
                 traversed_args :: [Exp2]
traversed_args = (Int -> Exp2) -> [Int] -> [Exp2]
forall a b. (a -> b) -> [a] -> [b]
map ([Exp2]
args [Exp2] -> Int -> Exp2
forall a. (Out a, HasCallStack) => [a] -> Int -> a
!!!) [Int]
traversed_arg_pos
                 traversed_locs :: [Var]
traversed_locs = (Exp2 -> [Var] -> [Var]) -> [Var] -> [Exp2] -> [Var]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
                                      (\Exp2
arg [Var]
acc ->
                                         case Exp2
arg of
                                           VarE Var
w ->
                                             case Var -> Env2 (UrTy Var) -> UrTy Var
forall a. Out a => Var -> Env2 a -> a
lookupVEnv Var
w Env2 (UrTy Var)
env2 of
                                               PackedTy [Char]
_ Var
loc -> (Var
locVar -> [Var] -> [Var]
forall a. a -> [a] -> [a]
:[Var]
acc)
                                               -- For indirection/redirection pointers.
                                               UrTy Var
CursorTy -> (Var
wVar -> [Var] -> [Var]
forall a. a -> [a] -> [a]
:[Var]
acc)
                                               UrTy Var
_ -> [Var]
acc
                                           Exp2
_ -> [Var]
acc)
                                      []
                                      [Exp2]
traversed_args
                 locenv' :: LocEnv
locenv' = UrTy Var -> LocEnv -> LocEnv
updModality UrTy Var
ty LocEnv
locenv
                 ([LocArg]
ewitnesses', LocEnv
locenv'') =
                        ((Var, Var) -> ([LocArg], LocEnv) -> ([LocArg], LocEnv))
-> ([LocArg], LocEnv) -> [(Var, Var)] -> ([LocArg], LocEnv)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
                          (\(Var
witloc, Var
tloc) ([LocArg]
wits, LocEnv
env) ->
                             let (New.Loc LREM
lrem) = (LocEnv
env LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
tloc)
                                 wit' :: LocArg
wit' = LREM -> Var -> LocArg
New.EndWitness LREM
lrem Var
witloc
                                 env' :: LocEnv
env' = Var -> LocArg -> LocEnv -> LocEnv
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Var
witloc LocArg
wit' LocEnv
env
                             in (LocArg
wit' LocArg -> [LocArg] -> [LocArg]
forall a. a -> [a] -> [a]
: [LocArg]
wits, LocEnv
env'))
                          ([], LocEnv
locenv')
                          ([Var] -> [Var] -> [(Var, Var)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Var]
ewitnesses [Var]
traversed_locs)
             Exp2
rhs' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
rhs
             Exp2
bod' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv'' (Var -> UrTy Var -> Env2 (UrTy Var) -> Env2 (UrTy Var)
forall a. Var -> a -> Env2 a -> Env2 a
extendVEnv Var
v UrTy Var
ty Env2 (UrTy Var)
env2) Exp2
bod
             let ty' :: Ty2
ty' = UrTy Var -> Ty2
New.MkTy2 UrTy Var
ty
             (Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ (Var, [LocArg], Ty2, Exp2) -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
(Var, [loc], dec, PreExp ext loc dec)
-> PreExp ext loc dec -> PreExp ext loc dec
LetE (Var
v, [LocArg]
ewitnesses', Ty2
ty', Exp2
rhs') Exp2
bod')

      | CaseE Exp2
_scrt [([Char], [(Var, Var)], Exp2)]
_brs <- Exp2
rhs
      , Bool -> Bool
not ([Var] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Var]
ewitnesses) ->
          [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error [Char]
"todo case"

      | [Var] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Var]
ewitnesses ->
          do let ty' :: Ty2
ty' = UrTy Var -> Ty2
New.MkTy2 UrTy Var
ty
             Exp2
rhs' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
rhs
             let locenv' :: LocEnv
locenv' = UrTy Var -> LocEnv -> LocEnv
updModality UrTy Var
ty LocEnv
locenv
             Exp2
bod' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv' (Var -> UrTy Var -> Env2 (UrTy Var) -> Env2 (UrTy Var)
forall a. Var -> a -> Env2 a -> Env2 a
extendVEnv Var
v UrTy Var
ty Env2 (UrTy Var)
env2) Exp2
bod
             Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ (Var, [LocArg], Ty2, Exp2) -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
(Var, [loc], dec, PreExp ext loc dec)
-> PreExp ext loc dec -> PreExp ext loc dec
LetE (Var
v, [], Ty2
ty', Exp2
rhs') Exp2
bod'

      | Bool
otherwise -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error ([Char] -> PassM Exp2) -> [Char] -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ [Char]
"fromOldL2Exp: got" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Exp2 -> [Char]
forall a. Out a => a -> [Char]
sdoc Exp2
ex


    CaseE Exp2
scrt [([Char], [(Var, Var)], Exp2)]
brs
      | VarE Var
v <- Exp2
scrt ->
          do let (PackedTy [Char]
_ Var
scrt_loc) = Var -> Env2 (UrTy Var) -> UrTy Var
forall a. Out a => Var -> Env2 a -> a
lookupVEnv Var
v Env2 (UrTy Var)
env2
                 (New.Loc LREM
lrem) = LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
scrt_loc

                 docase :: ([Char], [(Var, Var)], Exp2)
-> PassM ([Char], [(Var, LocArg)], Exp2)
docase ([Char]
dcon, [(Var, Var)]
vlocs, Exp2
rhs) = do

                   let mkLocArg :: Var -> LocArg
mkLocArg Var
loc = LREM -> LocArg
New.Loc (LREM -> LocArg) -> LREM -> LocArg
forall a b. (a -> b) -> a -> b
$ LREM
lrem { lremLoc :: Var
New.lremLoc = Var
loc }
                   let ([Var]
vars,[Var]
locs) = [(Var, Var)] -> ([Var], [Var])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Var, Var)]
vlocs
                       locargs :: [LocArg]
locargs = (Var -> LocArg) -> [Var] -> [LocArg]
forall a b. (a -> b) -> [a] -> [b]
map Var -> LocArg
mkLocArg [Var]
locs
                       vlocs' :: [(Var, LocArg)]
vlocs' = [Var] -> [LocArg] -> [(Var, LocArg)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Var]
vars [LocArg]
locargs
                       locenv' :: LocEnv
locenv' = (LocArg -> LocEnv -> LocEnv) -> LocEnv -> [LocArg] -> LocEnv
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
                                   (\(New.Loc LREM
lrem') LocEnv
acc -> Var -> LocArg -> LocEnv -> LocEnv
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (LREM -> Var
New.lremLoc LREM
lrem') (LREM -> LocArg
New.Loc LREM
lrem') LocEnv
acc)
                                   LocEnv
locenv [LocArg]
locargs
                       env2' :: Env2 (UrTy Var)
env2' = HasCallStack =>
[Char]
-> Map Var (DDef (UrTy Var))
-> [Var]
-> [Var]
-> Env2 (UrTy Var)
-> Env2 (UrTy Var)
[Char]
-> Map Var (DDef (UrTy Var))
-> [Var]
-> [Var]
-> Env2 (UrTy Var)
-> Env2 (UrTy Var)
extendPatternMatchEnv [Char]
dcon Map Var (DDef (UrTy Var))
ddefs [Var]
vars [Var]
locs Env2 (UrTy Var)
env2
                       locenv'' :: LocEnv
locenv'' = if [Char] -> Bool
isRedirectionTag [Char]
dcon Bool -> Bool -> Bool
|| [Char] -> Bool
isIndirectionTag [Char]
dcon
                                  then let ptr :: Var
ptr = [Var] -> Var
forall a. HasCallStack => [a] -> a
head [Var]
vars
                                       in Var -> LocArg -> LocEnv -> LocEnv
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Var
ptr (Var -> LocArg
mkLocArg Var
ptr) LocEnv
locenv'
                                  else LocEnv
locenv'
                   Exp2
rhs' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv'' Env2 (UrTy Var)
env2' Exp2
rhs
                   ([Char], [(Var, LocArg)], Exp2)
-> PassM ([Char], [(Var, LocArg)], Exp2)
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Char], [(Var, LocArg)], Exp2)
 -> PassM ([Char], [(Var, LocArg)], Exp2))
-> ([Char], [(Var, LocArg)], Exp2)
-> PassM ([Char], [(Var, LocArg)], Exp2)
forall a b. (a -> b) -> a -> b
$ ([Char]
dcon, [(Var, LocArg)]
vlocs', Exp2
rhs')

             (Exp2 -> [([Char], [(Var, LocArg)], Exp2)] -> Exp2
forall (ext :: * -> * -> *) loc dec.
PreExp ext loc dec
-> [([Char], [(Var, loc)], PreExp ext loc dec)]
-> PreExp ext loc dec
CaseE (Var -> Exp2
forall (ext :: * -> * -> *) loc dec. Var -> PreExp ext loc dec
VarE Var
v)) ([([Char], [(Var, LocArg)], Exp2)] -> Exp2)
-> PassM [([Char], [(Var, LocArg)], Exp2)] -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (([Char], [(Var, Var)], Exp2)
 -> PassM ([Char], [(Var, LocArg)], Exp2))
-> [([Char], [(Var, Var)], Exp2)]
-> PassM [([Char], [(Var, LocArg)], Exp2)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Char], [(Var, Var)], Exp2)
-> PassM ([Char], [(Var, LocArg)], Exp2)
docase [([Char], [(Var, Var)], Exp2)]
brs

      | Bool
otherwise -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error ([Char] -> PassM Exp2) -> [Char] -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ [Char]
"fromOldL2Exp: CaseE " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Exp2 -> [Char]
forall a. Out a => a -> [Char]
sdoc Exp2
scrt


    DataConE Var
loc [Char]
dcon [Exp2]
args -> do
      let locarg :: LocArg
locarg = LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
loc
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2) [Exp2]
args
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ LocArg -> [Char] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
loc -> [Char] -> [PreExp ext loc dec] -> PreExp ext loc dec
DataConE LocArg
locarg [Char]
dcon [Exp2]
args'

    Ext E2Ext Var (UrTy Var)
ext ->
      case E2Ext Var (UrTy Var)
ext of
        LetRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod -> do
          Exp2
bod' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
bod
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Region
-> RegionSize -> Maybe RegionType -> Exp2 -> E2Ext LocArg Ty2
forall loc dec.
Region
-> RegionSize -> Maybe RegionType -> E2 loc dec -> E2Ext loc dec
LetRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod'

        LetParRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod -> do
          Exp2
bod' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
bod
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Region
-> RegionSize -> Maybe RegionType -> Exp2 -> E2Ext LocArg Ty2
forall loc dec.
Region
-> RegionSize -> Maybe RegionType -> E2 loc dec -> E2Ext loc dec
LetRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod'

        LetLocE Var
loc PreLocExp Var
rhs Exp2
bod -> do
          let rhs' :: PreLocExp LocArg
rhs' = (Var -> LocArg) -> PreLocExp Var -> PreLocExp LocArg
forall a b. (a -> b) -> PreLocExp a -> PreLocExp b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
#) PreLocExp Var
rhs
              locarg :: LocArg
locarg = Var -> PreLocExp Var -> LocEnv -> LocArg
toLocArg Var
loc PreLocExp Var
rhs LocEnv
locenv
          Exp2
bod' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go (Var -> LocArg -> LocEnv -> LocEnv
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Var
loc LocArg
locarg LocEnv
locenv) Env2 (UrTy Var)
env2 Exp2
bod
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> PreLocExp LocArg -> Exp2 -> E2Ext LocArg Ty2
forall loc dec. Var -> PreLocExp loc -> E2 loc dec -> E2Ext loc dec
LetLocE Var
loc PreLocExp LocArg
rhs' Exp2
bod'

        RetE [Var]
locs Var
v -> do
          let locargs :: [LocArg]
locargs = (Var -> LocArg) -> [Var] -> [LocArg]
forall a b. (a -> b) -> [a] -> [b]
map (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
#) [Var]
locs
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ [LocArg] -> Var -> E2Ext LocArg Ty2
forall loc dec. [loc] -> Var -> E2Ext loc dec
RetE [LocArg]
locargs Var
v

        FromEndE Var
loc -> E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2)
-> (LocArg -> E2Ext LocArg Ty2) -> LocArg -> Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LocArg -> E2Ext LocArg Ty2
forall loc dec. loc -> E2Ext loc dec
FromEndE (LocArg -> Exp2) -> PassM LocArg -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LocArg -> PassM LocArg
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
loc)

        BoundsCheck Int
i Var
reg Var
loc -> do
         let reg' :: LocArg
reg' = Var -> Modality -> LocArg
New.Reg Var
reg Modality
Output
             loc' :: LocArg
loc' = LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
loc
         Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Int -> LocArg -> LocArg -> E2Ext LocArg Ty2
forall loc dec. Int -> loc -> loc -> E2Ext loc dec
BoundsCheck Int
i LocArg
reg' LocArg
loc'

        AddFixed Var
v Int
i -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Int -> E2Ext LocArg Ty2
forall loc dec. Var -> Int -> E2Ext loc dec
AddFixed Var
v Int
i

        TagCursor Var
a Var
b -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Var -> E2Ext LocArg Ty2
forall loc dec. Var -> Var -> E2Ext loc dec
TagCursor Var
a Var
b

        StartOfPkdCursor Var
cur -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> E2Ext LocArg Ty2
forall loc dec. Var -> E2Ext loc dec
StartOfPkdCursor Var
cur

        IndirectionE [Char]
tycon [Char]
dcon (Var
from,Var
from_reg) (Var
to,Var
to_reg) Exp2
e -> do
          Exp2
e' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
e
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$
            [Char]
-> [Char]
-> (LocArg, LocArg)
-> (LocArg, LocArg)
-> Exp2
-> E2Ext LocArg Ty2
forall loc dec.
[Char]
-> [Char]
-> (loc, loc)
-> (loc, loc)
-> E2 loc dec
-> E2Ext loc dec
IndirectionE
              [Char]
tycon
              [Char]
dcon
              (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
from, Var -> Modality -> Var -> LocArg
New.EndOfReg Var
from_reg Modality
Output (Var -> Var
toEndV Var
from_reg))
              (LocEnv
locenv LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
to, Var -> Modality -> Var -> LocArg
New.EndOfReg Var
to_reg Modality
Input (Var -> Var
toEndV Var
to_reg))
              Exp2
e'
              -- (locenv # from, New.Reg (VarR from_reg) Output)
              -- (locenv # to, New.Reg (VarR to_reg) Input)

        E2Ext Var (UrTy Var)
GetCilkWorkerNum -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext E2Ext LocArg Ty2
forall loc dec. E2Ext loc dec
GetCilkWorkerNum

        LetAvail [Var]
avail Exp2
rhs -> do
          Exp2
rhs' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
rhs
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ [Var] -> Exp2 -> E2Ext LocArg Ty2
forall loc dec. [Var] -> E2 loc dec -> E2Ext loc dec
LetAvail [Var]
avail Exp2
rhs'

        AllocateTagHere Var
loc [Char]
tycon -> do
          -- let locarg = locenv # loc
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [Char] -> E2Ext LocArg Ty2
forall loc dec. Var -> [Char] -> E2Ext loc dec
AllocateTagHere Var
loc [Char]
tycon

        AllocateScalarsHere Var
loc -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> E2Ext LocArg Ty2
forall loc dec. Var -> E2Ext loc dec
AllocateScalarsHere Var
loc

        SSPush SSModality
mode Var
loc Var
end_loc [Char]
tycon -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ SSModality -> Var -> Var -> [Char] -> E2Ext LocArg Ty2
forall loc dec. SSModality -> Var -> Var -> [Char] -> E2Ext loc dec
SSPush SSModality
mode Var
loc Var
end_loc [Char]
tycon

        SSPop SSModality
mode Var
loc Var
end_loc -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext LocArg Ty2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext LocArg Ty2 -> Exp2) -> E2Ext LocArg Ty2 -> Exp2
forall a b. (a -> b) -> a -> b
$ SSModality -> Var -> Var -> E2Ext LocArg Ty2
forall loc dec. SSModality -> Var -> Var -> E2Ext loc dec
SSPop SSModality
mode Var
loc Var
end_loc

    -- straightforward recursion
    VarE Var
v -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Exp2
forall (ext :: * -> * -> *) loc dec. Var -> PreExp ext loc dec
VarE Var
v
    LitE Int
i -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Int -> Exp2
forall (ext :: * -> * -> *) loc dec. Int -> PreExp ext loc dec
LitE Int
i
    CharE Char
i -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Char -> Exp2
forall (ext :: * -> * -> *) loc dec. Char -> PreExp ext loc dec
CharE Char
i
    FloatE Double
i  -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Double -> Exp2
forall (ext :: * -> * -> *) loc dec. Double -> PreExp ext loc dec
FloatE Double
i
    LitSymE Var
s -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Exp2
forall (ext :: * -> * -> *) loc dec. Var -> PreExp ext loc dec
LitSymE Var
s
    IfE Exp2
a Exp2
b Exp2
c -> Exp2 -> Exp2 -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
PreExp ext loc dec
-> PreExp ext loc dec -> PreExp ext loc dec -> PreExp ext loc dec
IfE (Exp2 -> Exp2 -> Exp2 -> Exp2)
-> PassM Exp2 -> PassM (Exp2 -> Exp2 -> Exp2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
a PassM (Exp2 -> Exp2 -> Exp2) -> PassM Exp2 -> PassM (Exp2 -> Exp2)
forall a b. PassM (a -> b) -> PassM a -> PassM b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
b PassM (Exp2 -> Exp2) -> PassM Exp2 -> PassM Exp2
forall a b. PassM (a -> b) -> PassM a -> PassM b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
c
    MkProdE [Exp2]
args  -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
[PreExp ext loc dec] -> PreExp ext loc dec
MkProdE ([Exp2] -> Exp2) -> PassM [Exp2] -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2) [Exp2]
args
    ProjE Int
i Exp2
e     -> (Int -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
Int -> PreExp ext loc dec -> PreExp ext loc dec
ProjE Int
i) (Exp2 -> Exp2) -> PassM Exp2 -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
e
    TimeIt Exp2
e UrTy Var
ty Bool
b -> do Exp2
e' <- LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
e
                        let ty' :: Ty2
ty' = UrTy Var -> Ty2
New.MkTy2 UrTy Var
ty
                        Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Exp2 -> Ty2 -> Bool -> Exp2
forall (ext :: * -> * -> *) loc dec.
PreExp ext loc dec -> dec -> Bool -> PreExp ext loc dec
TimeIt Exp2
e' Ty2
ty' Bool
b
    WithArenaE Var
v Exp2
rhs -> (Var -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> PreExp ext loc dec -> PreExp ext loc dec
WithArenaE Var
v) (Exp2 -> Exp2) -> PassM Exp2 -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go LocEnv
locenv Env2 (UrTy Var)
env2 Exp2
rhs
    Exp2
SyncE   -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp2
forall (ext :: * -> * -> *) loc dec. PreExp ext loc dec
SyncE
    MapE{}  -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error [Char]
"MapE"
    FoldE{} -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error [Char]
"FoldE"
 where
  go :: LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
go = Map Var (DDef (UrTy Var))
-> FunDefs Exp2 -> LocEnv -> Env2 (UrTy Var) -> Exp2 -> PassM Exp2
fromOldL2Exp Map Var (DDef (UrTy Var))
ddefs FunDefs Exp2
fundefs

  toLocArg :: LocVar -> LocExp -> LocEnv -> New.LocArg
  toLocArg :: Var -> PreLocExp Var -> LocEnv -> LocArg
toLocArg Var
loc PreLocExp Var
locexp LocEnv
locenv0 =
    case PreLocExp Var
locexp of
      StartOfRegionLE Region
reg -> LREM -> LocArg
New.Loc (Var -> Var -> Var -> Modality -> LREM
New.LREM Var
loc (Region -> Var
regionToVar Region
reg) (Var -> Var
toEndV (Region -> Var
regionToVar Region
reg)) Modality
Output)
      AfterConstantLE Int
_ Var
loc2 ->
        let (New.Loc LREM
lrem) = LocEnv
locenv0 LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
loc2
        in LREM -> LocArg
New.Loc (Var -> Var -> Var -> Modality -> LREM
New.LREM Var
loc (LREM -> Var
New.lremReg LREM
lrem) (LREM -> Var
New.lremEndReg LREM
lrem) Modality
Output)
      AfterVariableLE Var
_ Var
loc2 Bool
_ ->
        let (New.Loc LREM
lrem) = LocEnv
locenv0 LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
loc2
        in LREM -> LocArg
New.Loc (Var -> Var -> Var -> Modality -> LREM
New.LREM Var
loc (LREM -> Var
New.lremReg LREM
lrem) (LREM -> Var
New.lremEndReg LREM
lrem) Modality
Output)
      InRegionLE Region
reg ->
        LREM -> LocArg
New.Loc (Var -> Var -> Var -> Modality -> LREM
New.LREM Var
loc (Region -> Var
regionToVar Region
reg) (Var -> Var
toEndV (Region -> Var
regionToVar Region
reg)) Modality
Output)
      PreLocExp Var
FreeLE ->
        LREM -> LocArg
New.Loc (Var -> Var -> Var -> Modality -> LREM
New.LREM Var
loc Var
"FREE_REG" Var
"end_FREE_REG" Modality
Output)
      FromEndLE Var
loc2 ->
        case (LocEnv
locenv0 LocEnv -> Var -> LocArg
forall a b.
(Ord a, Out a, Out b, Show a, HasCallStack) =>
Map a b -> a -> b
# Var
loc2) of
          New.Loc LREM
lrem -> LREM -> LocArg
New.Loc (LREM
lrem { lremLoc :: Var
New.lremLoc = Var
loc })
          New.EndWitness LREM
lrem Var
_ -> LREM -> LocArg
New.Loc ( LREM
lrem { lremLoc :: Var
New.lremLoc = Var
loc } )
          LocArg
oth -> [Char] -> LocArg
forall a. HasCallStack => [Char] -> a
error ([Char] -> LocArg) -> [Char] -> LocArg
forall a b. (a -> b) -> a -> b
$ [Char]
"toLocArg: got" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ LocArg -> [Char]
forall a. Out a => a -> [Char]
sdoc LocArg
oth


  updModality :: Ty2 -> LocEnv -> LocEnv
  updModality :: UrTy Var -> LocEnv -> LocEnv
updModality UrTy Var
ty LocEnv
env =
    let locs :: [Var]
locs = UrTy Var -> [Var]
locsInTy UrTy Var
ty in
      (Var -> LocEnv -> LocEnv) -> LocEnv -> [Var] -> LocEnv
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Var
loc LocEnv
acc -> (LocArg -> LocArg) -> Var -> LocEnv -> LocEnv
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
M.adjust (\LocArg
locarg ->
                                     case LocArg
locarg of
                                       New.Loc LREM
lrem -> LREM -> LocArg
New.Loc (LREM
lrem { lremMode :: Modality
New.lremMode = Modality
Input })
                                       LocArg
_ -> LocArg
locarg)
                         Var
loc LocEnv
acc)
      LocEnv
env [Var]
locs

--------------------------------------------------------------------------------


toOldL2 :: New.Prog2 -> PassM Prog2
toOldL2 :: Prog2 -> PassM Prog2
toOldL2 Prog{DDefs (TyOf Exp2)
ddefs :: forall ex. Prog ex -> DDefs (TyOf ex)
ddefs :: DDefs (TyOf Exp2)
ddefs,Map Var FunDef2
fundefs :: forall ex. Prog ex -> FunDefs ex
fundefs :: Map Var FunDef2
fundefs,Maybe (Exp2, TyOf Exp2)
mainExp :: forall ex. Prog ex -> Maybe (ex, TyOf ex)
mainExp :: Maybe (Exp2, TyOf Exp2)
mainExp} = do
  let ddefs' :: Map Var (DDef (UrTy Var))
ddefs' = (DDef Ty2 -> DDef (UrTy Var))
-> Map Var (DDef Ty2) -> Map Var (DDef (UrTy Var))
forall a b k. (a -> b) -> Map k a -> Map k b
M.map ((Ty2 -> UrTy Var) -> DDef Ty2 -> DDef (UrTy Var)
forall a b. (a -> b) -> DDef a -> DDef b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ty2 -> UrTy Var
New.unTy2) DDefs (TyOf Exp2)
Map Var (DDef Ty2)
ddefs
  [FunDef2]
fds' <- (FunDef2 -> PassM FunDef2) -> [FunDef2] -> PassM [FunDef2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM FunDef2 -> PassM FunDef2
toOldL2Fn ([FunDef2] -> PassM [FunDef2]) -> [FunDef2] -> PassM [FunDef2]
forall a b. (a -> b) -> a -> b
$ Map Var FunDef2 -> [FunDef2]
forall k a. Map k a -> [a]
M.elems Map Var FunDef2
fundefs
  let fundefs' :: FunDefs Exp2
fundefs' = [(Var, FunDef2)] -> FunDefs Exp2
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Var, FunDef2)] -> FunDefs Exp2)
-> [(Var, FunDef2)] -> FunDefs Exp2
forall a b. (a -> b) -> a -> b
$ (FunDef2 -> (Var, FunDef2)) -> [FunDef2] -> [(Var, FunDef2)]
forall a b. (a -> b) -> [a] -> [b]
map (\FunDef2
f -> (FunDef2 -> Var
forall ex. FunDef ex -> Var
funName FunDef2
f,FunDef2
f)) [FunDef2]
fds'
  Maybe (Exp2, UrTy Var)
mainExp' <- case Maybe (Exp2, TyOf Exp2)
mainExp of
                Maybe (Exp2, TyOf Exp2)
Nothing -> Maybe (Exp2, UrTy Var) -> PassM (Maybe (Exp2, UrTy Var))
forall a. a -> PassM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Exp2, UrTy Var)
forall a. Maybe a
Nothing
                Just (Exp2
mn, TyOf Exp2
ty) -> do
                  let ty' :: UrTy Var
ty' = Ty2 -> UrTy Var
New.unTy2 TyOf Exp2
Ty2
ty
                  Exp2
mn' <- Exp2 -> PassM Exp2
toOldL2Exp Exp2
mn
                  Maybe (Exp2, UrTy Var) -> PassM (Maybe (Exp2, UrTy Var))
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Exp2, UrTy Var) -> PassM (Maybe (Exp2, UrTy Var)))
-> Maybe (Exp2, UrTy Var) -> PassM (Maybe (Exp2, UrTy Var))
forall a b. (a -> b) -> a -> b
$ (Exp2, UrTy Var) -> Maybe (Exp2, UrTy Var)
forall a. a -> Maybe a
Just (Exp2
mn', UrTy Var
ty')
  Prog2 -> PassM Prog2
forall a. a -> PassM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Prog2 -> PassM Prog2) -> Prog2 -> PassM Prog2
forall a b. (a -> b) -> a -> b
$ DDefs (TyOf Exp2)
-> FunDefs Exp2 -> Maybe (Exp2, TyOf Exp2) -> Prog2
forall ex.
DDefs (TyOf ex) -> FunDefs ex -> Maybe (ex, TyOf ex) -> Prog ex
Prog DDefs (TyOf Exp2)
Map Var (DDef (UrTy Var))
ddefs' FunDefs Exp2
fundefs' Maybe (Exp2, TyOf Exp2)
Maybe (Exp2, UrTy Var)
mainExp'


toOldL2Fn :: New.FunDef2 -> PassM FunDef2
toOldL2Fn :: FunDef2 -> PassM FunDef2
toOldL2Fn f :: FunDef2
f@FunDef{ArrowTy (TyOf Exp2)
funTy :: forall ex. FunDef ex -> ArrowTy (TyOf ex)
funTy :: ArrowTy (TyOf Exp2)
funTy,Exp2
funBody :: forall ex. FunDef ex -> ex
funBody :: Exp2
funBody} = do
  Exp2
bod' <- Exp2 -> PassM Exp2
toOldL2Exp Exp2
funBody
  FunDef2 -> PassM FunDef2
forall a. a -> PassM a
forall (m :: * -> *) a. Monad m => a -> m a
return (FunDef2 -> PassM FunDef2) -> FunDef2 -> PassM FunDef2
forall a b. (a -> b) -> a -> b
$ FunDef2
f { funBody :: Exp2
funBody = Exp2
bod', funTy :: ArrowTy (TyOf Exp2)
funTy = (Ty2 -> UrTy Var) -> ArrowTy2 Ty2 -> ArrowTy2 (UrTy Var)
forall a b. (a -> b) -> ArrowTy2 a -> ArrowTy2 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ty2 -> UrTy Var
New.unTy2 ArrowTy (TyOf Exp2)
ArrowTy2 Ty2
funTy }


toOldL2Exp :: New.Exp2 -> PassM Exp2
toOldL2Exp :: Exp2 -> PassM Exp2
toOldL2Exp Exp2
ex =
  case Exp2
ex of
    AppE Var
f [LocArg]
locs [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Exp2 -> PassM Exp2
go [Exp2]
args
      let locargs :: [Var]
locargs = (LocArg -> Var) -> [LocArg] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map LocArg -> Var
New.toLocVar [LocArg]
locs
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [Var] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> [loc] -> [PreExp ext loc dec] -> PreExp ext loc dec
AppE Var
f [Var]
locargs [Exp2]
args'

    SpawnE Var
f [LocArg]
locs [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Exp2 -> PassM Exp2
go [Exp2]
args
      let locargs :: [Var]
locargs = (LocArg -> Var) -> [LocArg] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map LocArg -> Var
New.toLocVar [LocArg]
locs
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [Var] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> [loc] -> [PreExp ext loc dec] -> PreExp ext loc dec
SpawnE Var
f [Var]
locargs [Exp2]
args'

    PrimAppE Prim Ty2
pr [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Exp2 -> PassM Exp2
go [Exp2]
args
      let pr' :: Prim (UrTy Var)
pr' = (Ty2 -> UrTy Var) -> Prim Ty2 -> Prim (UrTy Var)
forall a b. (a -> b) -> Prim a -> Prim b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ty2 -> UrTy Var
New.unTy2 Prim Ty2
pr
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Prim (UrTy Var) -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
Prim dec -> [PreExp ext loc dec] -> PreExp ext loc dec
PrimAppE Prim (UrTy Var)
pr' [Exp2]
args'

    LetE (Var
v, [LocArg]
_ewitnesses, Ty2
ty, Exp2
rhs) Exp2
bod -> do
             let ty' :: UrTy Var
ty' = Ty2 -> UrTy Var
New.unTy2 Ty2
ty
             Exp2
rhs' <- Exp2 -> PassM Exp2
go Exp2
rhs
             Exp2
bod' <- Exp2 -> PassM Exp2
go Exp2
bod
             Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ (Var, [Var], UrTy Var, Exp2) -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
(Var, [loc], dec, PreExp ext loc dec)
-> PreExp ext loc dec -> PreExp ext loc dec
LetE (Var
v, [], UrTy Var
ty', Exp2
rhs') Exp2
bod'

    CaseE Exp2
scrt [([Char], [(Var, LocArg)], Exp2)]
brs
      | VarE Var
v <- Exp2
scrt ->
          do let docase :: (a, [(a, LocArg)], Exp2) -> PassM (a, [(a, Var)], Exp2)
docase (a
dcon, [(a, LocArg)]
vlocs, Exp2
rhs) = do
                   let ([a]
vars,[LocArg]
locs) = [(a, LocArg)] -> ([a], [LocArg])
forall a b. [(a, b)] -> ([a], [b])
unzip [(a, LocArg)]
vlocs
                       locargs :: [Var]
locargs = (LocArg -> Var) -> [LocArg] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map LocArg -> Var
New.toLocVar [LocArg]
locs
                       vlocs' :: [(a, Var)]
vlocs' = [a] -> [Var] -> [(a, Var)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
vars [Var]
locargs
                   Exp2
rhs' <- Exp2 -> PassM Exp2
go Exp2
rhs
                   (a, [(a, Var)], Exp2) -> PassM (a, [(a, Var)], Exp2)
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, [(a, Var)], Exp2) -> PassM (a, [(a, Var)], Exp2))
-> (a, [(a, Var)], Exp2) -> PassM (a, [(a, Var)], Exp2)
forall a b. (a -> b) -> a -> b
$ (a
dcon, [(a, Var)]
vlocs', Exp2
rhs')

             (Exp2 -> [([Char], [(Var, Var)], Exp2)] -> Exp2
forall (ext :: * -> * -> *) loc dec.
PreExp ext loc dec
-> [([Char], [(Var, loc)], PreExp ext loc dec)]
-> PreExp ext loc dec
CaseE (Var -> Exp2
forall (ext :: * -> * -> *) loc dec. Var -> PreExp ext loc dec
VarE Var
v)) ([([Char], [(Var, Var)], Exp2)] -> Exp2)
-> PassM [([Char], [(Var, Var)], Exp2)] -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (([Char], [(Var, LocArg)], Exp2)
 -> PassM ([Char], [(Var, Var)], Exp2))
-> [([Char], [(Var, LocArg)], Exp2)]
-> PassM [([Char], [(Var, Var)], Exp2)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Char], [(Var, LocArg)], Exp2)
-> PassM ([Char], [(Var, Var)], Exp2)
forall {a} {a}.
(a, [(a, LocArg)], Exp2) -> PassM (a, [(a, Var)], Exp2)
docase [([Char], [(Var, LocArg)], Exp2)]
brs

      | Bool
otherwise -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error ([Char] -> PassM Exp2) -> [Char] -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ [Char]
"toOldL2Exp: CaseE " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Exp2 -> [Char]
forall a. Out a => a -> [Char]
sdoc Exp2
scrt


    DataConE LocArg
loc [Char]
dcon [Exp2]
args -> do
      [Exp2]
args' <- (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Exp2 -> PassM Exp2
go [Exp2]
args
      Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [Char] -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
loc -> [Char] -> [PreExp ext loc dec] -> PreExp ext loc dec
DataConE (LocArg -> Var
New.toLocVar LocArg
loc) [Char]
dcon [Exp2]
args'

    Ext E2Ext LocArg Ty2
ext ->
      case E2Ext LocArg Ty2
ext of
        LetRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod -> do
          Exp2
bod' <- Exp2 -> PassM Exp2
go Exp2
bod
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Region
-> RegionSize -> Maybe RegionType -> Exp2 -> E2Ext Var (UrTy Var)
forall loc dec.
Region
-> RegionSize -> Maybe RegionType -> E2 loc dec -> E2Ext loc dec
LetRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod'

        LetParRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod -> do
          Exp2
bod' <- Exp2 -> PassM Exp2
go Exp2
bod
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Region
-> RegionSize -> Maybe RegionType -> Exp2 -> E2Ext Var (UrTy Var)
forall loc dec.
Region
-> RegionSize -> Maybe RegionType -> E2 loc dec -> E2Ext loc dec
LetRegionE Region
reg RegionSize
reg_size Maybe RegionType
mb_ty Exp2
bod'

        LetLocE Var
loc PreLocExp LocArg
rhs Exp2
bod -> do
          let rhs' :: PreLocExp Var
rhs' = (LocArg -> Var) -> PreLocExp LocArg -> PreLocExp Var
forall a b. (a -> b) -> PreLocExp a -> PreLocExp b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LocArg -> Var
New.toLocVar PreLocExp LocArg
rhs
          Exp2
bod' <- Exp2 -> PassM Exp2
go Exp2
bod
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> PreLocExp Var -> Exp2 -> E2Ext Var (UrTy Var)
forall loc dec. Var -> PreLocExp loc -> E2 loc dec -> E2Ext loc dec
LetLocE Var
loc PreLocExp Var
rhs' Exp2
bod'

        StartOfPkdCursor Var
cur -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> E2Ext Var (UrTy Var)
forall loc dec. Var -> E2Ext loc dec
StartOfPkdCursor Var
cur

        TagCursor Var
a Var
b -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Var -> E2Ext Var (UrTy Var)
forall loc dec. Var -> Var -> E2Ext loc dec
TagCursor Var
a Var
b

        RetE [LocArg]
locs Var
v -> do
          let locargs :: [Var]
locargs = (LocArg -> Var) -> [LocArg] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map LocArg -> Var
New.toLocVar [LocArg]
locs
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ [Var] -> Var -> E2Ext Var (UrTy Var)
forall loc dec. [loc] -> Var -> E2Ext loc dec
RetE [Var]
locargs Var
v

        FromEndE LocArg
loc -> E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2)
-> (Var -> E2Ext Var (UrTy Var)) -> Var -> Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Var -> E2Ext Var (UrTy Var)
forall loc dec. loc -> E2Ext loc dec
FromEndE (Var -> Exp2) -> PassM Var -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Var -> PassM Var
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LocArg -> Var
New.toLocVar LocArg
loc)

        BoundsCheck Int
i LocArg
reg LocArg
loc -> do
         Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Int -> Var -> Var -> E2Ext Var (UrTy Var)
forall loc dec. Int -> loc -> loc -> E2Ext loc dec
BoundsCheck Int
i (LocArg -> Var
New.toLocVar LocArg
reg) (LocArg -> Var
New.toLocVar LocArg
loc)

        AddFixed Var
v Int
i -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Int -> E2Ext Var (UrTy Var)
forall loc dec. Var -> Int -> E2Ext loc dec
AddFixed Var
v Int
i

        IndirectionE [Char]
tycon [Char]
dcon (LocArg
from,LocArg
from_reg) (LocArg
to,LocArg
to_reg) Exp2
e -> do
          Exp2
e' <- Exp2 -> PassM Exp2
go Exp2
e
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$
            [Char]
-> [Char]
-> (Var, Var)
-> (Var, Var)
-> Exp2
-> E2Ext Var (UrTy Var)
forall loc dec.
[Char]
-> [Char]
-> (loc, loc)
-> (loc, loc)
-> E2 loc dec
-> E2Ext loc dec
IndirectionE
              [Char]
tycon
              [Char]
dcon
              (LocArg -> Var
New.toLocVar LocArg
from, LocArg -> Var
New.toLocVar LocArg
from_reg)
              (LocArg -> Var
New.toLocVar LocArg
to, LocArg -> Var
New.toLocVar LocArg
to_reg)
              Exp2
e'

        E2Ext LocArg Ty2
GetCilkWorkerNum -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext E2Ext Var (UrTy Var)
forall loc dec. E2Ext loc dec
GetCilkWorkerNum

        LetAvail [Var]
avail Exp2
rhs -> do
          Exp2
rhs' <- Exp2 -> PassM Exp2
go Exp2
rhs
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ [Var] -> Exp2 -> E2Ext Var (UrTy Var)
forall loc dec. [Var] -> E2 loc dec -> E2Ext loc dec
LetAvail [Var]
avail Exp2
rhs'

        AllocateTagHere Var
loc [Char]
tycon -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> [Char] -> E2Ext Var (UrTy Var)
forall loc dec. Var -> [Char] -> E2Ext loc dec
AllocateTagHere Var
loc [Char]
tycon

        AllocateScalarsHere Var
loc -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ Var -> E2Ext Var (UrTy Var)
forall loc dec. Var -> E2Ext loc dec
AllocateScalarsHere Var
loc

        SSPush SSModality
mode Var
loc Var
end_loc [Char]
tycon -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ SSModality -> Var -> Var -> [Char] -> E2Ext Var (UrTy Var)
forall loc dec. SSModality -> Var -> Var -> [Char] -> E2Ext loc dec
SSPush SSModality
mode Var
loc Var
end_loc [Char]
tycon

        SSPop SSModality
mode Var
loc Var
end_loc -> do
          Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ E2Ext Var (UrTy Var) -> Exp2
forall (ext :: * -> * -> *) loc dec.
ext loc dec -> PreExp ext loc dec
Ext (E2Ext Var (UrTy Var) -> Exp2) -> E2Ext Var (UrTy Var) -> Exp2
forall a b. (a -> b) -> a -> b
$ SSModality -> Var -> Var -> E2Ext Var (UrTy Var)
forall loc dec. SSModality -> Var -> Var -> E2Ext loc dec
SSPop SSModality
mode Var
loc Var
end_loc

    -- straightforward recursion
    VarE Var
v -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Exp2
forall (ext :: * -> * -> *) loc dec. Var -> PreExp ext loc dec
VarE Var
v
    LitE Int
i -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Int -> Exp2
forall (ext :: * -> * -> *) loc dec. Int -> PreExp ext loc dec
LitE Int
i
    CharE Char
i -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Char -> Exp2
forall (ext :: * -> * -> *) loc dec. Char -> PreExp ext loc dec
CharE Char
i
    FloatE Double
i  -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Double -> Exp2
forall (ext :: * -> * -> *) loc dec. Double -> PreExp ext loc dec
FloatE Double
i
    LitSymE Var
s -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Var -> Exp2
forall (ext :: * -> * -> *) loc dec. Var -> PreExp ext loc dec
LitSymE Var
s
    IfE Exp2
a Exp2
b Exp2
c -> Exp2 -> Exp2 -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
PreExp ext loc dec
-> PreExp ext loc dec -> PreExp ext loc dec -> PreExp ext loc dec
IfE (Exp2 -> Exp2 -> Exp2 -> Exp2)
-> PassM Exp2 -> PassM (Exp2 -> Exp2 -> Exp2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Exp2 -> PassM Exp2
go Exp2
a PassM (Exp2 -> Exp2 -> Exp2) -> PassM Exp2 -> PassM (Exp2 -> Exp2)
forall a b. PassM (a -> b) -> PassM a -> PassM b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Exp2 -> PassM Exp2
go Exp2
b PassM (Exp2 -> Exp2) -> PassM Exp2 -> PassM Exp2
forall a b. PassM (a -> b) -> PassM a -> PassM b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Exp2 -> PassM Exp2
go Exp2
c
    MkProdE [Exp2]
args  -> [Exp2] -> Exp2
forall (ext :: * -> * -> *) loc dec.
[PreExp ext loc dec] -> PreExp ext loc dec
MkProdE ([Exp2] -> Exp2) -> PassM [Exp2] -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Exp2 -> PassM Exp2) -> [Exp2] -> PassM [Exp2]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Exp2 -> PassM Exp2
go [Exp2]
args
    ProjE Int
i Exp2
e     -> (Int -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
Int -> PreExp ext loc dec -> PreExp ext loc dec
ProjE Int
i) (Exp2 -> Exp2) -> PassM Exp2 -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Exp2 -> PassM Exp2
go Exp2
e
    TimeIt Exp2
e Ty2
ty Bool
b -> do Exp2
e' <- Exp2 -> PassM Exp2
go Exp2
e
                        let ty' :: UrTy Var
ty' = Ty2 -> UrTy Var
New.unTy2 Ty2
ty
                        Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp2 -> PassM Exp2) -> Exp2 -> PassM Exp2
forall a b. (a -> b) -> a -> b
$ Exp2 -> UrTy Var -> Bool -> Exp2
forall (ext :: * -> * -> *) loc dec.
PreExp ext loc dec -> dec -> Bool -> PreExp ext loc dec
TimeIt Exp2
e' UrTy Var
ty' Bool
b
    WithArenaE Var
v Exp2
rhs -> (Var -> Exp2 -> Exp2
forall (ext :: * -> * -> *) loc dec.
Var -> PreExp ext loc dec -> PreExp ext loc dec
WithArenaE Var
v) (Exp2 -> Exp2) -> PassM Exp2 -> PassM Exp2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Exp2 -> PassM Exp2
go Exp2
rhs
    Exp2
SyncE   -> Exp2 -> PassM Exp2
forall a. a -> PassM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp2
forall (ext :: * -> * -> *) loc dec. PreExp ext loc dec
SyncE
    MapE{}  -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error [Char]
"MapE"
    FoldE{} -> [Char] -> PassM Exp2
forall a. HasCallStack => [Char] -> a
error [Char]
"FoldE"
 where
  go :: Exp2 -> PassM Exp2
go = Exp2 -> PassM Exp2
toOldL2Exp