2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
4 mkPADictType, mkPArrayType,
5 paDictArgType, paDictOfType,
6 paMethod, lengthPA, replicatePA,
11 #include "HsVersions.h"
26 import Control.Monad ( liftM )
28 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
29 collectAnnTypeArgs expr = go expr []
31 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
34 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
35 collectAnnTypeBinders expr = go [] expr
37 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
38 go bs e = (reverse bs, e)
40 isAnnTypeArg :: AnnExpr b ann -> Bool
41 isAnnTypeArg (_, AnnType t) = True
42 isAnnTypeArg _ = False
44 isClosureTyCon :: TyCon -> Bool
45 isClosureTyCon tc = tyConUnique tc == closureTyConKey
47 splitClosureTy :: Type -> (Type, Type)
49 | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
53 | otherwise = pprPanic "splitClosureTy" (ppr ty)
55 mkPADictType :: Type -> VM Type
58 tc <- builtin paDictTyCon
59 return $ TyConApp tc [ty]
61 mkPArrayType :: Type -> VM Type
64 tc <- builtin parrayTyCon
65 return $ TyConApp tc [ty]
67 paDictArgType :: TyVar -> VM (Maybe Type)
68 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
70 go ty k | Just k' <- kindView k = go ty k'
73 tv <- newTyVar FSLIT("a") k1
74 mty1 <- go (TyVarTy tv) k1
77 mty2 <- go (AppTy ty (TyVarTy tv)) k2
78 return $ fmap (ForAllTy tv . FunTy ty1) mty2
83 = liftM Just (mkPADictType ty)
85 go ty k = return Nothing
87 paDictOfType :: Type -> VM CoreExpr
88 paDictOfType ty = paDictOfTyApp ty_fn ty_args
90 (ty_fn, ty_args) = splitAppTys ty
92 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
93 paDictOfTyApp ty_fn ty_args
94 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
95 paDictOfTyApp (TyVarTy tv) ty_args
97 dfun <- maybeV (lookupTyVarPA tv)
98 paDFunApply dfun ty_args
99 paDictOfTyApp (TyConApp tc _) ty_args
101 pa_class <- builtin paClass
102 (dfun, ty_args') <- lookupInst pa_class [TyConApp tc ty_args]
103 paDFunApply (Var dfun) ty_args'
104 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
106 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
109 dicts <- mapM paDictOfType tys
110 return $ mkApps (mkTyApps dfun tys) dicts
112 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
116 dict <- paDictOfType ty
117 return $ mkApps (Var fn) [Type ty, dict]
119 lengthPA :: CoreExpr -> VM CoreExpr
120 lengthPA x = liftM (`App` x) (paMethod lengthPAVar (exprType x))
122 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
123 replicatePA len x = liftM (`mkApps` [len,x])
124 (paMethod replicatePAVar (exprType x))
126 lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
127 lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
129 hoistExpr :: FastString -> CoreExpr -> VM Var
132 var <- newLocalVar fs (exprType expr)
134 env { global_bindings = (var, expr) : global_bindings env }
137 takeHoisted :: VM [(Var, CoreExpr)]
141 setGEnv $ env { global_bindings = [] }
142 return $ global_bindings env