VECTORISE pragmas for type classes and instances
[ghc.git] / compiler / vectorise / Vectorise / Monad / InstEnv.hs
1 module Vectorise.Monad.InstEnv
2 ( lookupInst
3 , lookupFamInst
4 )
5 where
6
7 import Vectorise.Monad.Global
8 import Vectorise.Monad.Base
9 import Vectorise.Env
10
11 import FamInstEnv
12 import InstEnv
13 import Class
14 import Type
15 import TyCon
16 import Outputable
17
18
19 #include "HsVersions.h"
20
21
22 -- Look up the dfun of a class instance.
23 --
24 -- The match must be unique —i.e., match exactly one instance— but the
25 -- type arguments used for matching may be more specific than those of
26 -- the class instance declaration. The found class instances must not have
27 -- any type variables in the instance context that do not appear in the
28 -- instances head (i.e., no flexi vars); for details for what this means,
29 -- see the docs at InstEnv.lookupInstEnv.
30 --
31 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
32 lookupInst cls tys
33 = do { instEnv <- readGEnv global_inst_env
34 ; case lookupUniqueInstEnv instEnv cls tys of
35 Right (inst, inst_tys) -> return (instanceDFunId inst, inst_tys)
36 Left err -> cantVectorise "Vectorise.Monad.InstEnv.lookupInst:" err
37 }
38
39 -- Look up the representation tycon of a family instance.
40 --
41 -- The match must be unique - ie, match exactly one instance - but the
42 -- type arguments used for matching may be more specific than those of
43 -- the family instance declaration.
44 --
45 -- Return the instance tycon and its type instance. For example, if we have
46 --
47 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
48 --
49 -- then we have a coercion (ie, type instance of family instance coercion)
50 --
51 -- :Co:R42T Int :: T [Int] ~ :R42T Int
52 --
53 -- which implies that :R42T was declared as 'data instance T [a]'.
54 --
55 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
56 lookupFamInst tycon tys
57 = ASSERT( isFamilyTyCon tycon )
58 do { instEnv <- readGEnv global_fam_inst_env
59 ; case lookupFamInstEnv instEnv tycon tys of
60 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
61 _other ->
62 cantVectorise "VectMonad.lookupFamInst: not found: "
63 (ppr $ mkTyConApp tycon tys)
64 }