1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.DeadCodeElimination
4-- Copyright : (c) 2014 Phil Freeman
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15module Language.PureScript.DeadCodeElimination (
23import Data.Maybe (mapMaybe)
25import Language.PureScript.Names
26import Language.PureScript.Values
27import Language.PureScript.Declarations
30-- Eliminate all declarations which are not a transitive dependency of the entry point module
32eliminateDeadCode :: [ModuleName] -> [Module] -> [Module]
33eliminateDeadCode entryPoints ms =
34 let declarations = concatMap declarationsByModule ms
35 (graph, _, vertexFor) = graphFromEdges $ map (\(key, deps) -> (key, key, deps)) declarations
36 entryPointVertices = mapMaybe (vertexFor . fst) . filter (\((mn, _), _) -> mn `elem` entryPoints) $ declarations
37 in flip map ms $ \(Module moduleName ds exps) -> Module moduleName (filter (isUsed moduleName graph vertexFor entryPointVertices) ds) exps
39type Key = (ModuleName, Either Ident ProperName)
41declarationsByModule :: Module -> [(Key, [Key])]
42declarationsByModule (Module moduleName ds _) = concatMap go ds
44 go :: Declaration -> [(Key, [Key])]
45 go d@(ValueDeclaration name _ _ _ _) = [((moduleName, Left name), dependencies moduleName d)]
46 go (DataDeclaration _ _ dctors) = map (\(name, _) -> ((moduleName, Right name), [])) dctors
47 go (ExternDeclaration _ name _ _) = [((moduleName, Left name), [])]
48 go d@(BindingGroupDeclaration names') = map (\(name, _, _) -> ((moduleName, Left name), dependencies moduleName d)) names'
49 go (DataBindingGroupDeclaration ds') = concatMap go ds'
52dependencies :: (Data d) => ModuleName -> d -> [Key]
53dependencies moduleName = nub . everything (++) (mkQ [] values)
55 values :: Value -> [Key]
56 values (Var ident) = let (mn, name) = qualify moduleName ident in [(mn, Left name)]
57 values (Constructor (Qualified (Just mn) name)) = [(mn, Right name)]
58 values (Constructor (Qualified Nothing _)) = error "Found unqualified data constructor"
61isUsed :: ModuleName -> Graph -> (Key -> Maybe Vertex) -> [Vertex] -> Declaration -> Bool
62isUsed moduleName graph vertexFor entryPointVertices (ValueDeclaration name _ _ _ _) =
63 let Just v' = vertexFor (moduleName, Left name)
64 in any (\v -> path graph v v') entryPointVertices
65isUsed moduleName graph vertexFor entryPointVertices (DataDeclaration _ _ dctors) =
66 any (\(pn, _) -> let Just v' = vertexFor (moduleName, Right pn)
67 in any (\v -> path graph v v') entryPointVertices) dctors
68isUsed moduleName graph vertexFor entryPointVertices (ExternDeclaration _ name _ _) =
69 let Just v' = vertexFor (moduleName, Left name)
70 in any (\v -> path graph v v') entryPointVertices
71isUsed moduleName graph vertexFor entryPointVertices (BindingGroupDeclaration ds) =
72 any (\(name, _, _) -> let Just v' = vertexFor (moduleName, Left name)
73 in any (\v -> path graph v v') entryPointVertices) ds
74isUsed moduleName graph vertexFor entryPointVertices (DataBindingGroupDeclaration ds) =
75 any (isUsed moduleName graph vertexFor entryPointVertices) ds
76isUsed _ _ _ _ _ = True
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list