Finish breaking up VectBuiltIn and VectMonad, and add comments
[ghc-hetmet.git] / compiler / vectorise / Vectorise / Monad / InstEnv.hs
1
2 module Vectorise.Monad.InstEnv (
3         lookupInst,
4         lookupFamInst
5 ) where
6 import Vectorise.Monad.Global
7 import Vectorise.Monad.Base
8 import Vectorise.Env
9
10 import FamInstEnv
11 import InstEnv
12 import Class
13 import Type
14 import TyCon
15 import Outputable
16
17
18 #include "HsVersions.h"
19
20
21 getInstEnv :: VM (InstEnv, InstEnv)
22 getInstEnv = readGEnv global_inst_env
23
24 getFamInstEnv :: VM FamInstEnvs
25 getFamInstEnv = readGEnv global_fam_inst_env
26
27
28 -- Look up the dfun of a class instance.
29 --
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.
36 --
37 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
38 lookupInst cls tys
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)
45              where
46                inst_tys'  = [ty | Right ty <- inst_tys]
47                noFlexiVar = all isRight inst_tys
48            _other         ->
49              pprPanic "VectMonad.lookupInst: not found " (ppr cls <+> ppr tys)
50        }
51   where
52     isRight (Left  _) = False
53     isRight (Right _) = True
54
55 -- Look up the representation tycon of a family instance.
56 --
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.
60 --
61 -- Return the instance tycon and its type instance.  For example, if we have
62 --
63 --  lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
64 --
65 -- then we have a coercion (ie, type instance of family instance coercion)
66 --
67 --  :Co:R42T Int :: T [Int] ~ :R42T Int
68 --
69 -- which implies that :R42T was declared as 'data instance T [a]'.
70 --
71 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
72 lookupFamInst tycon tys
73   = ASSERT( isOpenTyCon tycon )
74     do { instEnv <- getFamInstEnv
75        ; case lookupFamInstEnv instEnv tycon tys of
76            [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
77            _other                -> 
78              pprPanic "VectMonad.lookupFamInst: not found: " 
79                       (ppr $ mkTyConApp tycon tys)
80        }