Skip to content

Commit 2021aa0

Browse files
committed
Merge pull request purescript#1161 from puffnfresh/perf/entails-filtermodule
Remove poor performing entails.filterModule
2 parents c4b2e41 + 40ad1cf commit 2021aa0

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/Language/PureScript/AST/Declarations.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
module Language.PureScript.AST.Declarations where
1818

1919
import qualified Data.Data as D
20+
import qualified Data.Map as M
2021

2122
import Language.PureScript.AST.Binders
2223
import Language.PureScript.AST.Operators
@@ -360,7 +361,7 @@ data Expr
360361
-- at superclass implementations when searching for a dictionary, the type class name and
361362
-- instance type, and the type class dictionaries in scope.
362363
--
363-
| TypeClassDictionary Bool Constraint [TypeClassDictionaryInScope]
364+
| TypeClassDictionary Bool Constraint (M.Map (Maybe ModuleName) [TypeClassDictionaryInScope])
364365
-- |
365366
-- A typeclass dictionary accessor, the implementation is left unspecified until CoreFn desugaring.
366367
--

src/Language/PureScript/TypeChecker/Entailment.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Language.PureScript.TypeChecker.Entailment (
2121

2222
import Data.Function (on)
2323
import Data.List
24-
import Data.Maybe (maybeToList)
24+
import Data.Maybe (fromMaybe, maybeToList)
2525
import Data.Foldable (foldMap)
2626
import qualified Data.Map as M
2727

@@ -47,17 +47,17 @@ newtype Work = Work Integer deriving (Show, Eq, Ord, Num)
4747
-- Check that the current set of type class dictionaries entail the specified type class goal, and, if so,
4848
-- return a type class dictionary reference.
4949
--
50-
entails :: Environment -> ModuleName -> [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
51-
entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filter filterModule context))
50+
entails :: Environment -> ModuleName -> M.Map (Maybe ModuleName) [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
51+
entails env moduleName context = solve (sortedNubBy canonicalizeDictionary dictsInScope)
5252
where
5353
sortedNubBy :: (Ord k) => (v -> k) -> [v] -> [v]
5454
sortedNubBy f vs = M.elems (M.fromList (map (f &&& id) vs))
5555

56-
-- Filter out type dictionaries which are in scope in the current module
57-
filterModule :: TypeClassDictionaryInScope -> Bool
58-
filterModule (TypeClassDictionaryInScope { tcdName = Qualified (Just mn) _ }) | mn == moduleName = True
59-
filterModule (TypeClassDictionaryInScope { tcdName = Qualified Nothing _ }) = True
60-
filterModule _ = False
56+
dictsInScope :: [TypeClassDictionaryInScope]
57+
dictsInScope = findDicts Nothing ++ findDicts (Just moduleName)
58+
59+
findDicts :: Maybe ModuleName -> [TypeClassDictionaryInScope]
60+
findDicts = fromMaybe [] . flip M.lookup context
6161

6262
solve :: [TypeClassDictionaryInScope] -> Constraint -> Bool -> Check Expr
6363
solve context' (className, tys) trySuperclasses = do

src/Language/PureScript/TypeChecker/Monad.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ withTypeClassDictionaries entries action = do
7676
return a
7777

7878
-- |
79-
-- Get the currently available list of type class dictionaries
79+
-- Get the currently available map of type class dictionaries
8080
--
81-
getTypeClassDictionaries :: (Functor m, MonadState CheckState m) => m [TypeClassDictionaryInScope]
82-
getTypeClassDictionaries = (>>= M.elems) . M.elems . typeClassDictionaries . checkEnv <$> get
81+
getTypeClassDictionaries :: (Functor m, MonadState CheckState m) => m (M.Map (Maybe ModuleName) [TypeClassDictionaryInScope])
82+
getTypeClassDictionaries = fmap M.elems . typeClassDictionaries . checkEnv <$> get
8383

8484
-- |
8585
-- Lookup type class dictionaries in a module.

0 commit comments

Comments
 (0)