@@ -26,6 +26,8 @@ import Data.List (nub, intersect)
2626import Data.Maybe (isJust, mapMaybe)
2727import Control.Applicative ((<$>), (<*>), pure)
2828
29+import qualified Data.Set as S
30+
2931import Language.PureScript.Declarations
3032import Language.PureScript.Names
3133import Language.PureScript.Types
@@ -91,13 +93,19 @@ collapseBindingGroupsForValue other = other
9193
9294usedIdents :: ModuleName -> Declaration -> [Ident]
9395usedIdents moduleName =
94− let (f, _, _, _, _) = everythingOnValues (++) (const []) usedNames (const []) (const []) (const [])
96+ let (f, _, _, _, _) = everythingWithContextOnValues S.empty [] (++) def usedNamesE usedNamesB def def
9597 in nub . f
9698 where
97− usedNames :: Expr -> [Ident]
98− usedNames (Var (Qualified Nothing name)) = [name]
99− usedNames (Var (Qualified (Just moduleName') name)) | moduleName == moduleName' = [name]
100− usedNames _ = []
99+ def s _ = (s, [])
100+
101+ usedNamesE :: S.Set Ident -> Expr -> (S.Set Ident, [Ident])
102+ usedNamesE scope (Var (Qualified Nothing name)) | name `S.notMember` scope = (scope, [name])
103+ usedNamesE scope (Var (Qualified (Just moduleName') name)) | moduleName == moduleName' && name `S.notMember` scope = (scope, [name])
104+ usedNamesE scope (Abs (Left name) _) = (name `S.insert` scope, [])
105+ usedNamesE scope _ = (scope, [])
106+
107+ usedNamesB :: S.Set Ident -> Binder -> (S.Set Ident, [Ident])
108+ usedNamesB scope binder = (scope `S.union` S.fromList (binderNames binder), [])
101109
102110usedProperNames :: ModuleName -> Declaration -> [ProperName]
103111usedProperNames moduleName =