2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
5 mkPADictType, mkPArrayType,
6 paDictArgType, paDictOfType, paDFunType,
7 paMethod, lengthPA, replicatePA, emptyPA, liftPA,
8 polyAbstract, polyApply, polyVApply,
10 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
11 buildClosure, buildClosures,
15 #include "HsVersions.h"
26 import DataCon ( dataConWrapId )
28 import Id ( mkWildId )
29 import MkId ( unwrapFamInstScrut )
32 import BasicTypes ( Boxity(..) )
37 import Control.Monad ( liftM, zipWithM_ )
39 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
40 collectAnnTypeArgs expr = go expr []
42 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
45 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
46 collectAnnTypeBinders expr = go [] expr
48 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
49 go bs e = (reverse bs, e)
51 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
52 collectAnnValBinders expr = go [] expr
54 go bs (_, AnnLam b e) | isId b = go (b:bs) e
55 go bs e = (reverse bs, e)
57 isAnnTypeArg :: AnnExpr b ann -> Bool
58 isAnnTypeArg (_, AnnType t) = True
59 isAnnTypeArg _ = False
61 isClosureTyCon :: TyCon -> Bool
62 isClosureTyCon tc = tyConName tc == closureTyConName
64 splitClosureTy :: Type -> (Type, Type)
66 | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
70 | otherwise = pprPanic "splitClosureTy" (ppr ty)
72 isPArrayTyCon :: TyCon -> Bool
73 isPArrayTyCon tc = tyConName tc == parrayTyConName
75 splitPArrayTy :: Type -> Type
77 | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty
81 | otherwise = pprPanic "splitPArrayTy" (ppr ty)
83 mkClosureType :: Type -> Type -> VM Type
84 mkClosureType arg_ty res_ty
86 tc <- builtin closureTyCon
87 return $ mkTyConApp tc [arg_ty, res_ty]
89 mkClosureTypes :: [Type] -> Type -> VM Type
90 mkClosureTypes arg_tys res_ty
92 tc <- builtin closureTyCon
93 return $ foldr (mk tc) res_ty arg_tys
95 mk tc arg_ty res_ty = mkTyConApp tc [arg_ty, res_ty]
97 mkPADictType :: Type -> VM Type
100 tc <- builtin paTyCon
101 return $ TyConApp tc [ty]
103 mkPArrayType :: Type -> VM Type
106 tc <- builtin parrayTyCon
107 return $ TyConApp tc [ty]
109 paDictArgType :: TyVar -> VM (Maybe Type)
110 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
112 go ty k | Just k' <- kindView k = go ty k'
115 tv <- newTyVar FSLIT("a") k1
116 mty1 <- go (TyVarTy tv) k1
119 mty2 <- go (AppTy ty (TyVarTy tv)) k2
120 return $ fmap (ForAllTy tv . FunTy ty1) mty2
125 = liftM Just (mkPADictType ty)
127 go ty k = return Nothing
129 paDictOfType :: Type -> VM CoreExpr
130 paDictOfType ty = paDictOfTyApp ty_fn ty_args
132 (ty_fn, ty_args) = splitAppTys ty
134 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
135 paDictOfTyApp ty_fn ty_args
136 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
137 paDictOfTyApp (TyVarTy tv) ty_args
139 dfun <- maybeV (lookupTyVarPA tv)
140 paDFunApply dfun ty_args
141 paDictOfTyApp (TyConApp tc _) ty_args
143 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
144 paDFunApply (Var dfun) ty_args
145 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
147 paDFunType :: TyCon -> VM Type
150 margs <- mapM paDictArgType tvs
151 res <- mkPADictType (mkTyConApp tc arg_tys)
152 return . mkForAllTys tvs
153 $ mkFunTys [arg | Just arg <- margs] res
156 arg_tys = mkTyVarTys tvs
158 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
161 dicts <- mapM paDictOfType tys
162 return $ mkApps (mkTyApps dfun tys) dicts
164 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
168 dict <- paDictOfType ty
169 return $ mkApps (Var fn) [Type ty, dict]
171 lengthPA :: CoreExpr -> VM CoreExpr
172 lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
174 ty = splitPArrayTy (exprType x)
176 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
177 replicatePA len x = liftM (`mkApps` [len,x])
178 (paMethod replicatePAVar (exprType x))
180 emptyPA :: Type -> VM CoreExpr
181 emptyPA = paMethod emptyPAVar
183 liftPA :: CoreExpr -> VM CoreExpr
186 lc <- builtin liftingContext
187 replicatePA (Var lc) x
189 newLocalVVar :: FastString -> Type -> VM VVar
192 lty <- mkPArrayType vty
193 vv <- newLocalVar fs vty
194 lv <- newLocalVar fs lty
197 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
201 mdicts <- mapM mk_dict_var tvs
202 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
206 r <- paDictArgType tv
208 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
209 Nothing -> return Nothing
211 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
213 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
216 dicts <- mapM paDictOfType tys
217 return $ expr `mkTyApps` tys `mkApps` dicts
219 polyVApply :: VExpr -> [Type] -> VM VExpr
222 dicts <- mapM paDictOfType tys
223 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
225 lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
226 lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
228 hoistBinding :: Var -> CoreExpr -> VM ()
229 hoistBinding v e = updGEnv $ \env ->
230 env { global_bindings = (v,e) : global_bindings env }
232 hoistExpr :: FastString -> CoreExpr -> VM Var
235 var <- newLocalVar fs (exprType expr)
236 hoistBinding var expr
239 hoistVExpr :: VExpr -> VM VVar
243 vv <- hoistExpr ('v' `consFS` fs) ve
244 lv <- hoistExpr ('l' `consFS` fs) le
247 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
250 expr <- closedV . polyAbstract tvs $ \abstract ->
251 liftM (mapVect abstract) p
252 fn <- hoistVExpr expr
253 polyVApply (vVar fn) (mkTyVarTys tvs)
255 takeHoisted :: VM [(Var, CoreExpr)]
259 setGEnv $ env { global_bindings = [] }
260 return $ global_bindings env
262 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
263 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
265 dict <- paDictOfType env_ty
266 mkv <- builtin mkClosureVar
267 mkl <- builtin mkClosurePVar
268 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
269 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
271 mkClosureApp :: VExpr -> VExpr -> VM VExpr
272 mkClosureApp (vclo, lclo) (varg, larg)
274 vapply <- builtin applyClosureVar
275 lapply <- builtin applyClosurePVar
276 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
277 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
279 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
281 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
282 buildClosures tvs vars [arg_ty] res_ty mk_body
283 = buildClosure tvs vars arg_ty res_ty mk_body
284 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
286 res_ty' <- mkClosureTypes arg_tys res_ty
287 arg <- newLocalVVar FSLIT("x") arg_ty
288 buildClosure tvs vars arg_ty res_ty'
291 lc <- builtin liftingContext
292 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
293 return $ vLams lc (vars ++ [arg]) clo
295 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
297 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
298 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
300 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
301 buildClosure tvs vars arg_ty res_ty mk_body
303 (env_ty, env, bind) <- buildEnv vars
304 env_bndr <- newLocalVVar FSLIT("env") env_ty
305 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
307 fn <- hoistPolyVExpr tvs
309 lc <- builtin liftingContext
311 body' <- bind (vVar env_bndr)
312 (vVarApps lc body (vars ++ [arg_bndr]))
313 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
315 mkClosure arg_ty res_ty env_ty fn env
317 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
320 lc <- builtin liftingContext
321 let (ty, venv, vbind) = mkVectEnv tys vs
322 (lenv, lbind) <- mkLiftEnv lc tys ls
323 return (ty, (venv, lenv),
324 \(venv,lenv) (vbody,lbody) ->
326 let vbody' = vbind venv vbody
327 lbody' <- lbind lenv lbody
328 return (vbody', lbody'))
333 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
334 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
335 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
336 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
337 \env body -> Case env (mkWildId ty) (exprType body)
338 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
342 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
343 mkLiftEnv lc [ty] [v]
344 = return (Var v, \env body ->
346 len <- lengthPA (Var v)
347 return . Let (NonRec v env)
348 $ Case len lc (exprType body) [(DEFAULT, [], body)])
350 -- NOTE: this transparently deals with empty environments
353 (env_tc, env_tyargs) <- lookupPArrayFamInst vty
354 let [env_con] = tyConDataCons env_tc
356 env = Var (dataConWrapId env_con)
357 `mkTyApps` env_tyargs
358 `mkVarApps` (lc : vs)
360 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
362 return $ Case scrut (mkWildId (exprType scrut))
364 [(DataAlt env_con, lc : bndrs, body)]
367 vty = mkCoreTupTy tys
369 bndrs | null vs = [mkWildId unitTy]