@@ -28,13 +28,9 @@ import Data.IntMap (IntMap)
28-- UseMap maps names to the set of used (reachable) argument positions.
28-- UseMap maps names to the set of used (reachable) argument positions.
29type UseMap = Map Name IntSet
29type UseMap = Map Name IntSet
31−-- Deps augmented with cached info.
33−-- snd = all names that the function depends on
34−type Deps' = (Deps, Set Name)
36−-- DepMap maps functions (only functions) to their Deps'.
31+-- DepMap maps functions (only functions) to their Deps.
37−type DepMap = Map Name Deps'
32+type DepMap = Map Name Deps
33+type FlatMap = Map (Name, Int) (Set (Ctors, Cond))
39-- Deps of a function is a map from argument index
35-- Deps of a function is a map from argument index
40-- to all usages of the argument, including conditions
36-- to all usages of the argument, including conditions
@@ -58,8 +54,21 @@ type Vars = Map Name Var
58type PatVar = Ctors -> Var
54type PatVar = Ctors -> Var
59type PatVars = Map Name PatVar
55type PatVars = Map Name PatVar
58+-- Find the minimal consistent usage by forward chaining.
61minimalUsage :: DepMap -> UseMap
59minimalUsage :: DepMap -> UseMap
62−minimalUsage dmap = undefined
60+minimalUsage dmap =
62+ flatten :: DepMap -> FlatMap
66+ trivials = M.mapMaybeWithKey isTrivial dmap
68+ isTrivial :: Name -> (Deps, Set Name) -> Maybe (Deps, Set Name)
69+ isTrivial n (deps, ns)
64-- Build the dependency graph,
73-- Build the dependency graph,
65-- starting the depth-first search from a list of Names.
74-- starting the depth-first search from a list of Names.
@@ -73,7 +82,7 @@ buildDepMap ctx ns = dfs M.empty ns
73 dfs dmap [] = dmap
82 dfs dmap [] = dmap
74 dfs dmap (n : ns)
83 dfs dmap (n : ns)
75 | n `M.member` dmap = dfs dmap ns
84 | n `M.member` dmap = dfs dmap ns
76− | otherwise = dfs (M.insert n (deps, depn) dmap) ns
85+ | otherwise = dfs (M.insert n deps dmap) ns
78 next = [n | n <- S.toList depn, n `M.notMember` dmap]
87 next = [n | n <- S.toList depn, n `M.notMember` dmap]
79 depn = S.delete n (depNames deps)
88 depn = S.delete n (depNames deps)