@@ -11,12 +11,12 @@ module CheapReduction
1111 ( CheaplyReducibleE (.. ), cheapReduce , cheapReduceWithDecls , cheapNormalize
1212 , normalizeProj , asNaryProj , normalizeNaryProj
1313 , depPairLeftTy , instantiateTyConDef
14- , dataDefRep , instantiateDepPairTy , unwrapNewtypeType , repValAtom
14+ , dataDefRep , unwrapNewtypeType , repValAtom
1515 , unwrapLeadingNewtypesType , wrapNewtypesData , liftSimpAtom , liftSimpType
1616 , liftSimpFun , makeStructRepVal , NonAtomRenamer (.. ), Visitor (.. ), VisitGeneric (.. )
1717 , visitAtomPartial , visitTypePartial , visitAtomDefault , visitTypeDefault , Visitor2
18- , visitBinders , visitPiDefault , visitAlt , toAtomVar , instantiatePiTy , instantiateTabPiTy
19- , bindersToVars , bindersToAtoms )
18+ , visitBinders , visitPiDefault , visitAlt , toAtomVar , instantiate , withInstantiated
19+ , bindersToVars , bindersToAtoms , instantiateNames , withInstantiatedNames , assumeConst )
2020 where
2121
2222import Control.Applicative
@@ -242,7 +242,7 @@ cheapReduceDictExpr resultTy d = case d of
242242 args' <- mapM cheapReduceE args
243243 InstanceDef _ _ bs _ body <- lookupInstanceDef instanceName
244244 let InstanceBody superclasses _ = body
245- applySubst (bs @@> ( SubstVal <$> args')) (superclasses !! superclassIx)
245+ instantiate ( Abs bs (superclasses !! superclassIx)) args'
246246 child' -> return $ DictCon resultTy $ SuperclassProj child' superclassIx
247247 InstantiatedGiven f xs ->
248248 reduceApp <|> justSubst
@@ -285,19 +285,16 @@ instance IRRep r => CheaplyReducibleE r (Expr r) (Atom r) where
285285 cheapReduceE dict >>= \ case
286286 DictCon _ (InstanceDict instanceName args) -> dropSubst do
287287 args' <- mapM cheapReduceE args
288- InstanceDef _ _ bs _ (InstanceBody _ methods) <- lookupInstanceDef instanceName
289- let method = methods !! i
290- extendSubst (bs@@> (SubstVal <$> args')) do
291- method' <- cheapReduceE method
288+ def <- lookupInstanceDef instanceName
289+ withInstantiated def args' \ (PairE _ (InstanceBody _ methods)) -> do
290+ method' <- cheapReduceE $ methods !! i
292291 cheapReduceApp method' explicitArgs'
293292 _ -> empty
294293 _ -> empty
295294
296295cheapReduceApp :: CAtom o -> [CAtom o ] -> CheapReducerM CoreIR i o (CAtom o )
297296cheapReduceApp f xs = case f of
298- Lam (CoreLamExpr _ (LamExpr bs body)) -> do
299- let subst = bs @@> fmap SubstVal xs
300- dropSubst $ extendSubst subst $ cheapReduceE body
297+ Lam lam -> dropSubst $ withInstantiated lam xs \ body -> cheapReduceE body
301298 _ -> empty
302299
303300instance IRRep r => CheaplyReducibleE r (IxType r ) (IxType r ) where
@@ -450,7 +447,7 @@ projType i ty x = case ty of
450447 DepPairTy t | i == 0 -> return $ depPairLeftTy t
451448 DepPairTy t | i == 1 -> do
452449 xFst <- normalizeProj (ProjectProduct 0 ) x
453- instantiateDepPairTy t xFst
450+ instantiate t [ xFst]
454451 _ -> error $ " Can't project type: " ++ pprint ty
455452
456453unwrapLeadingNewtypesType :: EnvReader m => CType n -> m n ([NewtypeCon n ], CType n )
@@ -470,13 +467,39 @@ instantiateTyConDef (TyConDef _ _ bs conDefs) (TyConParams _ xs) = do
470467 applySubst (bs @@> (SubstVal <$> xs)) conDefs
471468{-# INLINE instantiateTyConDef #-}
472469
473- instantiatePiTy :: (EnvReader m , IRRep r ) => PiType r n -> [Atom r n ] -> m n (EffTy r n )
474- instantiatePiTy (PiType bs effTy) xs = do
475- applySubst (bs @@> (SubstVal <$> xs)) effTy
476-
477- instantiateTabPiTy :: (EnvReader m , IRRep r ) => TabPiType r n -> Atom r n -> m n (Type r n )
478- instantiateTabPiTy (TabPiType _ b resultTy) x = do
479- applySubst (b @> SubstVal x) resultTy
470+ assumeConst
471+ :: (IRRep r , HoistableE body , SinkableE body , ToBindersAbs e body r ) => e n -> body n
472+ assumeConst e = case toAbs e of Abs bs body -> ignoreHoistFailure $ hoist bs body
473+
474+ instantiate
475+ :: (EnvReader m , IRRep r , SubstE (SubstVal Atom ) body , SinkableE body , ToBindersAbs e body r )
476+ => e n -> [Atom r n ] -> m n (body n )
477+ instantiate e xs = case toAbs e of
478+ Abs bs body -> applySubst (bs @@> (SubstVal <$> xs)) body
479+
480+ -- "lazy" subst-extending version of `instantiate`
481+ withInstantiated
482+ :: (SubstReader AtomSubstVal m , IRRep r , SubstE (SubstVal Atom ) body , SinkableE body , ToBindersAbs e body r )
483+ => e i -> [Atom r o ]
484+ -> (forall i' . body i' -> m i' o a )
485+ -> m i o a
486+ withInstantiated e xs cont = case toAbs e of
487+ Abs bs body -> extendSubst (bs @@> (SubstVal <$> xs)) $ cont body
488+
489+ instantiateNames
490+ :: (EnvReader m , IRRep r , RenameE body , SinkableE body , ToBindersAbs e body r )
491+ => e n -> [AtomName r n ] -> m n (body n )
492+ instantiateNames e vs = case toAbs e of
493+ Abs bs body -> applyRename (bs @@> vs) body
494+
495+ -- "lazy" subst-extending version of `instantiateNames`
496+ withInstantiatedNames
497+ :: (SubstReader Name m , IRRep r , RenameE body , SinkableE body , ToBindersAbs e body r )
498+ => e i -> [AtomName r o ]
499+ -> (forall i' . body i' -> m i' o a )
500+ -> m i o a
501+ withInstantiatedNames e vs cont = case toAbs e of
502+ Abs bs body -> extendRenamer (bs @@> vs) $ cont body
480503
481504-- Returns a representation type (type of an TypeCon-typed Newtype payload)
482505-- given a list of instantiated DataConDefs.
@@ -498,10 +521,6 @@ makeStructRepVal tyConName args = do
498521 _ -> error " wrong number of args"
499522 _ -> return $ ProdVal args
500523
501- instantiateDepPairTy :: (IRRep r , EnvReader m ) => DepPairType r n -> Atom r n -> m n (Type r n )
502- instantiateDepPairTy (DepPairType _ b rhsTy) x = applyAbs (Abs b rhsTy) (SubstVal x)
503- {-# INLINE instantiateDepPairTy #-}
504-
505524-- === traversable terms ===
506525
507526class Monad m => NonAtomRenamer m i o | m -> i , m -> o where
0 commit comments