2 module Vectorise.Monad.InstEnv (
6 import Vectorise.Monad.Global
7 import Vectorise.Monad.Base
18 #include "HsVersions.h"
21 getInstEnv :: VM (InstEnv, InstEnv)
22 getInstEnv = readGEnv global_inst_env
24 getFamInstEnv :: VM FamInstEnvs
25 getFamInstEnv = readGEnv global_fam_inst_env
28 -- Look up the dfun of a class instance.
30 -- The match must be unique - ie, match exactly one instance - but the
31 -- type arguments used for matching may be more specific than those of
32 -- the class instance declaration. The found class instances must not have
33 -- any type variables in the instance context that do not appear in the
34 -- instances head (i.e., no flexi vars); for details for what this means,
35 -- see the docs at InstEnv.lookupInstEnv.
37 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
39 = do { instEnv <- getInstEnv
40 ; case lookupInstEnv instEnv cls tys of
41 ([(inst, inst_tys)], _)
42 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
43 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
44 (ppr $ mkTyConApp (classTyCon cls) tys)
46 inst_tys' = [ty | Right ty <- inst_tys]
47 noFlexiVar = all isRight inst_tys
49 pprPanic "VectMonad.lookupInst: not found " (ppr cls <+> ppr tys)
52 isRight (Left _) = False
53 isRight (Right _) = True
55 -- Look up the representation tycon of a family instance.
57 -- The match must be unique - ie, match exactly one instance - but the
58 -- type arguments used for matching may be more specific than those of
59 -- the family instance declaration.
61 -- Return the instance tycon and its type instance. For example, if we have
63 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
65 -- then we have a coercion (ie, type instance of family instance coercion)
67 -- :Co:R42T Int :: T [Int] ~ :R42T Int
69 -- which implies that :R42T was declared as 'data instance T [a]'.
71 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
72 lookupFamInst tycon tys
73 = ASSERT( isFamilyTyCon tycon )
74 do { instEnv <- getFamInstEnv
75 ; case lookupFamInstEnv instEnv tycon tys of
76 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
78 pprPanic "VectMonad.lookupFamInst: not found: "
79 (ppr $ mkTyConApp tycon tys)