@@ -21,7 +21,7 @@ module Language.PureScript.TypeChecker.Entailment (
22import Data.Function (on)
22import Data.Function (on)
23import Data.List
23import Data.List
24−import Data.Maybe (maybeToList)
24+import Data.Maybe (fromMaybe, maybeToList)
25import Data.Foldable (foldMap)
25import Data.Foldable (foldMap)
26import qualified Data.Map as M
26import qualified Data.Map as M
@@ -47,17 +47,17 @@ newtype Work = Work Integer deriving (Show, Eq, Ord, Num)
47-- Check that the current set of type class dictionaries entail the specified type class goal, and, if so,
47-- Check that the current set of type class dictionaries entail the specified type class goal, and, if so,
48-- return a type class dictionary reference.
48-- return a type class dictionary reference.
50−entails :: Environment -> ModuleName -> [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
50+entails :: Environment -> ModuleName -> M.Map (Maybe ModuleName) [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
51−entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filter filterModule context))
51+entails env moduleName context = solve (sortedNubBy canonicalizeDictionary dictsInScope)
53 sortedNubBy :: (Ord k) => (v -> k) -> [v] -> [v]
53 sortedNubBy :: (Ord k) => (v -> k) -> [v] -> [v]
54 sortedNubBy f vs = M.elems (M.fromList (map (f &&& id) vs))
54 sortedNubBy f vs = M.elems (M.fromList (map (f &&& id) vs))
56− -- Filter out type dictionaries which are in scope in the current module
57− filterModule :: TypeClassDictionaryInScope -> Bool
56+ dictsInScope :: [TypeClassDictionaryInScope]
58− filterModule (TypeClassDictionaryInScope { tcdName = Qualified (Just mn) _ }) | mn == moduleName = True
57+ dictsInScope = findDicts Nothing ++ findDicts (Just moduleName)
59− filterModule (TypeClassDictionaryInScope { tcdName = Qualified Nothing _ }) = True
58+
60− filterModule _ = False
59+ findDicts :: Maybe ModuleName -> [TypeClassDictionaryInScope]
60+ findDicts = fromMaybe [] . flip M.lookup context
62 solve :: [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
62 solve :: [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
63 solve context' (className, tys) trySuperclasses = do
63 solve context' (className, tys) trySuperclasses = do