InstEnv.hs 2.48 KB
Newer Older
1
module Vectorise.Monad.InstEnv 
2 3
  ( existsInst
  , lookupInst
4 5 6
  , lookupFamInst
  ) 
where
7 8 9 10 11

import Vectorise.Monad.Global
import Vectorise.Monad.Base
import Vectorise.Env

Ian Lynagh's avatar
Ian Lynagh committed
12
import DynFlags
13 14 15 16 17 18
import FamInstEnv
import InstEnv
import Class
import Type
import TyCon
import Outputable
19
import Util
20 21 22 23 24


#include "HsVersions.h"


25 26 27 28 29 30 31 32
-- Check whether a unique class instance for a given class and type arguments exists.
--
existsInst :: Class -> [Type] -> VM Bool
existsInst cls tys
  = do { instEnv <- readGEnv global_inst_env
       ; return $ either (const False) (const True) (lookupUniqueInstEnv instEnv cls tys)
       }

33 34
-- Look up the dfun of a class instance.
--
35
-- The match must be unique —i.e., match exactly one instance— but the 
36 37 38 39 40 41 42 43
-- type arguments used for matching may be more specific than those of 
-- the class instance declaration.  The found class instances must not have
-- any type variables in the instance context that do not appear in the
-- instances head (i.e., no flexi vars); for details for what this means,
-- see the docs at InstEnv.lookupInstEnv.
--
lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
lookupInst cls tys
44 45 46
  = do { instEnv <- readGEnv global_inst_env
       ; case lookupUniqueInstEnv instEnv cls tys of
           Right (inst, inst_tys) -> return (instanceDFunId inst, inst_tys)
Ian Lynagh's avatar
Ian Lynagh committed
47 48 49
           Left  err              ->
               do dflags <- getDynFlags
                  cantVectorise dflags "Vectorise.Monad.InstEnv.lookupInst:" err
50 51
       }

52
-- Look up a family instance.
53 54 55 56 57
--
-- The match must be unique - ie, match exactly one instance - but the 
-- type arguments used for matching may be more specific than those of 
-- the family instance declaration.
--
58
-- Return the family instance and its type instance.  For example, if we have
59 60 61 62 63 64 65 66 67
--
--  lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
--
-- then we have a coercion (ie, type instance of family instance coercion)
--
--  :Co:R42T Int :: T [Int] ~ :R42T Int
--
-- which implies that :R42T was declared as 'data instance T [a]'.
--
68
lookupFamInst :: TyCon -> [Type] -> VM (FamInst, [Type])
69
lookupFamInst tycon tys
70
  = ASSERT( isFamilyTyCon tycon )
71
    do { instEnv <- readGEnv global_fam_inst_env
72
       ; case lookupFamInstEnv instEnv tycon tys of
73
           [(fam_inst, rep_tys)] -> return ( fam_inst, rep_tys)
74
           _other                -> 
Ian Lynagh's avatar
Ian Lynagh committed
75
             do dflags <- getDynFlags
76
                cantVectorise dflags "Vectorise.Monad.InstEnv.lookupFamInst: not found: "
77
                           (ppr $ mkTyConApp tycon tys)
78
       }