2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
7 TyConRepr(..), mkTyConRepr,
8 mkToPRepr, mkToArrPRepr, mkFromPRepr, mkFromArrPRepr,
9 mkPADictType, mkPArrayType, mkPReprType,
11 parrayCoerce, parrayReprTyCon, parrayReprDataCon, mkVScrut,
12 prDictOfType, prCoerce,
13 paDictArgType, paDictOfType, paDFunType,
14 paMethod, lengthPA, replicatePA, emptyPA, liftPA,
15 polyAbstract, polyApply, polyVApply,
16 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
17 buildClosure, buildClosures,
21 #include "HsVersions.h"
35 import Id ( mkWildId )
36 import MkId ( unwrapFamInstScrut )
40 import BasicTypes ( Boxity(..) )
45 import Data.List ( zipWith4 )
46 import Control.Monad ( liftM, liftM2, zipWithM_ )
48 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
49 collectAnnTypeArgs expr = go expr []
51 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
54 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
55 collectAnnTypeBinders expr = go [] expr
57 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
58 go bs e = (reverse bs, e)
60 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
61 collectAnnValBinders expr = go [] expr
63 go bs (_, AnnLam b e) | isId b = go (b:bs) e
64 go bs e = (reverse bs, e)
66 isAnnTypeArg :: AnnExpr b ann -> Bool
67 isAnnTypeArg (_, AnnType t) = True
68 isAnnTypeArg _ = False
70 mkDataConTag :: DataCon -> CoreExpr
71 mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]
73 splitUnTy :: String -> Name -> Type -> Type
75 | Just (tc, [ty']) <- splitTyConApp_maybe ty
76 , tyConName tc == name
79 | otherwise = pprPanic s (ppr ty)
81 splitBinTy :: String -> Name -> Type -> (Type, Type)
83 | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
84 , tyConName tc == name
87 | otherwise = pprPanic s (ppr ty)
89 splitFixedTyConApp :: TyCon -> Type -> [Type]
90 splitFixedTyConApp tc ty
91 | Just (tc', tys) <- splitTyConApp_maybe ty
95 | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty)
97 splitClosureTy :: Type -> (Type, Type)
98 splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
100 splitPArrayTy :: Type -> Type
101 splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
103 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
104 mkBuiltinTyConApp get_tc tys
107 return $ mkTyConApp tc tys
109 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
110 mkBuiltinTyConApps get_tc tys ty
113 return $ foldr (mk tc) ty tys
115 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
117 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
118 mkBuiltinTyConApps1 get_tc dft [] = return dft
119 mkBuiltinTyConApps1 get_tc dft tys
123 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
124 _ -> return $ foldr1 (mk tc) tys
126 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
128 data TyConRepr = TyConRepr {
129 repr_tyvars :: [TyVar]
130 , repr_tys :: [[Type]]
132 , repr_prod_tycons :: [Maybe TyCon]
133 , repr_prod_tys :: [Type]
134 , repr_sum_tycon :: Maybe TyCon
138 mkTyConRepr :: TyCon -> VM TyConRepr
141 prod_tycons <- mapM (mk_tycon prodTyCon) rep_tys
142 let prod_tys = zipWith mk_tc_app_maybe prod_tycons rep_tys
143 sum_tycon <- mk_tycon sumTyCon prod_tys
150 , repr_prod_tycons = prod_tycons
151 , repr_prod_tys = prod_tys
152 , repr_sum_tycon = sum_tycon
153 , repr_type = mk_tc_app_maybe sum_tycon prod_tys
156 tyvars = tyConTyVars vect_tc
157 data_cons = tyConDataCons vect_tc
158 rep_tys = map dataConRepArgTys data_cons
161 | n > 1 = builtin (Just . get_tc n)
162 | otherwise = return Nothing
165 mk_tc_app_maybe Nothing [] = unitTy
166 mk_tc_app_maybe Nothing [ty] = ty
167 mk_tc_app_maybe (Just tc) tys = mkTyConApp tc tys
170 mkPRepr :: [[Type]] -> VM Type
173 embed_tc <- builtin embedTyCon
174 sum_tcs <- builtins sumTyCon
175 prod_tcs <- builtins prodTyCon
177 let mk_sum [] = unitTy
179 mk_sum tys = mkTyConApp (sum_tcs $ length tys) tys
183 mk_prod tys = mkTyConApp (prod_tcs $ length tys) tys
185 mk_embed ty = mkTyConApp embed_tc [ty]
188 . map (mk_prod . map mk_embed)
192 mkToPRepr :: [[CoreExpr]] -> VM ([CoreExpr], Type)
195 sum_tcs <- builtins sumTyCon
196 prod_tcs <- builtins prodTyCon
198 let mk_sum [] = ([Var unitDataConId], unitTy)
199 mk_sum [(expr, ty)] = ([expr], ty)
200 mk_sum es = (zipWith mk_alt (tyConDataCons sum_tc) exprs,
201 mkTyConApp sum_tc tys)
203 (exprs, tys) = unzip es
204 sum_tc = sum_tcs (length es)
205 mk_alt dc expr = mkConApp dc (map Type tys ++ [expr])
207 mk_prod [] = (Var unitDataConId, unitTy)
208 mk_prod [expr] = (expr, exprType expr)
209 mk_prod exprs = (mkConApp prod_dc (map Type tys ++ exprs),
210 mkTyConApp prod_tc tys)
212 tys = map exprType exprs
213 prod_tc = prod_tcs (length exprs)
214 [prod_dc] = tyConDataCons prod_tc
216 return . mk_sum . map mk_prod $ ess
218 mkToArrPRepr :: CoreExpr -> CoreExpr -> [[CoreExpr]] -> VM CoreExpr
219 mkToArrPRepr len sel ess
221 let mk_sum [(expr, ty)] = return (expr, ty)
224 sum_tc <- builtin . sumTyCon $ length es
225 (sum_rtc, _) <- parrayReprTyCon (mkTyConApp sum_tc tys)
226 let [sum_rdc] = tyConDataCons sum_rtc
228 return (mkConApp sum_rdc (map Type tys ++ (len : sel : exprs)),
229 mkTyConApp sum_tc tys)
231 (exprs, tys) = unzip es
233 mk_prod [expr] = return (expr, splitPArrayTy (exprType expr))
236 prod_tc <- builtin . prodTyCon $ length exprs
237 (prod_rtc, _) <- parrayReprTyCon (mkTyConApp prod_tc tys)
238 let [prod_rdc] = tyConDataCons prod_rtc
240 return (mkConApp prod_rdc (map Type tys ++ (len : exprs)),
241 mkTyConApp prod_tc tys)
243 tys = map (splitPArrayTy . exprType) exprs
245 liftM fst (mk_sum =<< mapM mk_prod ess)
247 mkFromPRepr :: CoreExpr -> Type -> [([Var], CoreExpr)] -> VM CoreExpr
248 mkFromPRepr scrut res_ty alts
250 sum_tcs <- builtins sumTyCon
251 prod_tcs <- builtins prodTyCon
253 let un_sum expr ty [(vars, res)] = un_prod expr ty vars res
256 ps <- mapM (newLocalVar FSLIT("p")) tys
258 $ zipWith4 un_prod (map Var ps) tys vars rs
259 return . Case expr (mkWildId ty) res_ty
260 $ zipWith3 mk_alt sum_dcs ps bodies
262 (vars, rs) = unzip bs
263 tys = splitFixedTyConApp sum_tc ty
264 sum_tc = sum_tcs $ length bs
265 sum_dcs = tyConDataCons sum_tc
267 mk_alt dc p body = (DataAlt dc, [p], body)
269 un_prod expr ty [] r = return r
270 un_prod expr ty [var] r = return $ Let (NonRec var expr) r
271 un_prod expr ty vars r
272 = return $ Case expr (mkWildId ty) res_ty
273 [(DataAlt prod_dc, vars, r)]
275 prod_tc = prod_tcs $ length vars
276 [prod_dc] = tyConDataCons prod_tc
278 un_sum scrut (exprType scrut) alts
280 mkFromArrPRepr :: CoreExpr -> Type -> Var -> Var -> [[Var]] -> CoreExpr
282 mkFromArrPRepr scrut res_ty len sel vars res
283 = return (Var unitDataConId)
285 mkClosureType :: Type -> Type -> VM Type
286 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
288 mkClosureTypes :: [Type] -> Type -> VM Type
289 mkClosureTypes = mkBuiltinTyConApps closureTyCon
291 mkPReprType :: Type -> VM Type
292 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
294 mkPADictType :: Type -> VM Type
295 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
297 mkPArrayType :: Type -> VM Type
298 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
300 parrayCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr
301 parrayCoerce repr_tc args expr
302 | Just arg_co <- tyConFamilyCoercion_maybe repr_tc
304 parray <- builtin parrayTyCon
306 let co = mkAppCoercion (mkTyConApp parray [])
307 (mkSymCoercion (mkTyConApp arg_co args))
309 return $ mkCoerce co expr
311 parrayReprTyCon :: Type -> VM (TyCon, [Type])
312 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
314 parrayReprDataCon :: Type -> VM (DataCon, [Type])
317 (tc, arg_tys) <- parrayReprTyCon ty
318 let [dc] = tyConDataCons tc
321 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
324 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
325 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
327 prDictOfType :: Type -> VM CoreExpr
329 | Just (tycon, ty_args) <- splitTyConApp_maybe orig_ty
331 dfun <- traceMaybeV "prDictOfType" (ppr tycon) (lookupTyConPR tycon)
332 prDFunApply (Var dfun) ty_args
334 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
337 args <- mapM mkDFunArg arg_tys
338 return $ mkApps mono_dfun args
340 mono_dfun = mkTyApps dfun tys
341 (arg_tys, _) = splitFunTys (exprType mono_dfun)
343 mkDFunArg :: Type -> VM CoreExpr
345 | Just (tycon, [arg]) <- splitTyConApp_maybe ty
347 = let name = tyConName tycon
349 get_dict | name == paTyConName = paDictOfType
350 | name == prTyConName = prDictOfType
351 | otherwise = pprPanic "mkDFunArg" (ppr ty)
355 mkDFunArg ty = pprPanic "mkDFunArg" (ppr ty)
357 prCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr
358 prCoerce repr_tc args expr
359 | Just arg_co <- tyConFamilyCoercion_maybe repr_tc
361 pr_tc <- builtin prTyCon
363 let co = mkAppCoercion (mkTyConApp pr_tc [])
364 (mkSymCoercion (mkTyConApp arg_co args))
366 return $ mkCoerce co expr
368 paDictArgType :: TyVar -> VM (Maybe Type)
369 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
371 go ty k | Just k' <- kindView k = go ty k'
374 tv <- newTyVar FSLIT("a") k1
375 mty1 <- go (TyVarTy tv) k1
378 mty2 <- go (AppTy ty (TyVarTy tv)) k2
379 return $ fmap (ForAllTy tv . FunTy ty1) mty2
384 = liftM Just (mkPADictType ty)
386 go ty k = return Nothing
388 paDictOfType :: Type -> VM CoreExpr
389 paDictOfType ty = paDictOfTyApp ty_fn ty_args
391 (ty_fn, ty_args) = splitAppTys ty
393 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
394 paDictOfTyApp ty_fn ty_args
395 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
396 paDictOfTyApp (TyVarTy tv) ty_args
398 dfun <- maybeV (lookupTyVarPA tv)
399 paDFunApply dfun ty_args
400 paDictOfTyApp (TyConApp tc _) ty_args
402 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
403 paDFunApply (Var dfun) ty_args
404 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
406 paDFunType :: TyCon -> VM Type
409 margs <- mapM paDictArgType tvs
410 res <- mkPADictType (mkTyConApp tc arg_tys)
411 return . mkForAllTys tvs
412 $ mkFunTys [arg | Just arg <- margs] res
415 arg_tys = mkTyVarTys tvs
417 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
420 dicts <- mapM paDictOfType tys
421 return $ mkApps (mkTyApps dfun tys) dicts
423 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
427 dict <- paDictOfType ty
428 return $ mkApps (Var fn) [Type ty, dict]
430 mkPR :: Type -> VM CoreExpr
431 mkPR = paMethod mkPRVar
433 lengthPA :: CoreExpr -> VM CoreExpr
434 lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
436 ty = splitPArrayTy (exprType x)
438 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
439 replicatePA len x = liftM (`mkApps` [len,x])
440 (paMethod replicatePAVar (exprType x))
442 emptyPA :: Type -> VM CoreExpr
443 emptyPA = paMethod emptyPAVar
445 liftPA :: CoreExpr -> VM CoreExpr
448 lc <- builtin liftingContext
449 replicatePA (Var lc) x
451 newLocalVVar :: FastString -> Type -> VM VVar
454 lty <- mkPArrayType vty
455 vv <- newLocalVar fs vty
456 lv <- newLocalVar fs lty
459 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
463 mdicts <- mapM mk_dict_var tvs
464 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
468 r <- paDictArgType tv
470 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
471 Nothing -> return Nothing
473 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
475 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
478 dicts <- mapM paDictOfType tys
479 return $ expr `mkTyApps` tys `mkApps` dicts
481 polyVApply :: VExpr -> [Type] -> VM VExpr
484 dicts <- mapM paDictOfType tys
485 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
487 hoistBinding :: Var -> CoreExpr -> VM ()
488 hoistBinding v e = updGEnv $ \env ->
489 env { global_bindings = (v,e) : global_bindings env }
491 hoistExpr :: FastString -> CoreExpr -> VM Var
494 var <- newLocalVar fs (exprType expr)
495 hoistBinding var expr
498 hoistVExpr :: VExpr -> VM VVar
502 vv <- hoistExpr ('v' `consFS` fs) ve
503 lv <- hoistExpr ('l' `consFS` fs) le
506 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
509 expr <- closedV . polyAbstract tvs $ \abstract ->
510 liftM (mapVect abstract) p
511 fn <- hoistVExpr expr
512 polyVApply (vVar fn) (mkTyVarTys tvs)
514 takeHoisted :: VM [(Var, CoreExpr)]
518 setGEnv $ env { global_bindings = [] }
519 return $ global_bindings env
521 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
522 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
524 dict <- paDictOfType env_ty
525 mkv <- builtin mkClosureVar
526 mkl <- builtin mkClosurePVar
527 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
528 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
530 mkClosureApp :: VExpr -> VExpr -> VM VExpr
531 mkClosureApp (vclo, lclo) (varg, larg)
533 vapply <- builtin applyClosureVar
534 lapply <- builtin applyClosurePVar
535 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
536 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
538 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
540 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
541 buildClosures tvs vars [] res_ty mk_body
543 buildClosures tvs vars [arg_ty] res_ty mk_body
544 = buildClosure tvs vars arg_ty res_ty mk_body
545 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
547 res_ty' <- mkClosureTypes arg_tys res_ty
548 arg <- newLocalVVar FSLIT("x") arg_ty
549 buildClosure tvs vars arg_ty res_ty'
552 lc <- builtin liftingContext
553 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
554 return $ vLams lc (vars ++ [arg]) clo
556 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
558 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
559 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
561 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
562 buildClosure tvs vars arg_ty res_ty mk_body
564 (env_ty, env, bind) <- buildEnv vars
565 env_bndr <- newLocalVVar FSLIT("env") env_ty
566 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
568 fn <- hoistPolyVExpr tvs
570 lc <- builtin liftingContext
572 body' <- bind (vVar env_bndr)
573 (vVarApps lc body (vars ++ [arg_bndr]))
574 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
576 mkClosure arg_ty res_ty env_ty fn env
578 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
581 lc <- builtin liftingContext
582 let (ty, venv, vbind) = mkVectEnv tys vs
583 (lenv, lbind) <- mkLiftEnv lc tys ls
584 return (ty, (venv, lenv),
585 \(venv,lenv) (vbody,lbody) ->
587 let vbody' = vbind venv vbody
588 lbody' <- lbind lenv lbody
589 return (vbody', lbody'))
594 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
595 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
596 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
597 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
598 \env body -> Case env (mkWildId ty) (exprType body)
599 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
603 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
604 mkLiftEnv lc [ty] [v]
605 = return (Var v, \env body ->
607 len <- lengthPA (Var v)
608 return . Let (NonRec v env)
609 $ Case len lc (exprType body) [(DEFAULT, [], body)])
611 -- NOTE: this transparently deals with empty environments
614 (env_tc, env_tyargs) <- parrayReprTyCon vty
615 let [env_con] = tyConDataCons env_tc
617 env = Var (dataConWrapId env_con)
618 `mkTyApps` env_tyargs
619 `mkVarApps` (lc : vs)
621 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
623 return $ Case scrut (mkWildId (exprType scrut))
625 [(DataAlt env_con, lc : bndrs, body)]
628 vty = mkCoreTupTy tys
630 bndrs | null vs = [mkWildId unitTy]