Skip to content

Commit 5c25ab6

Browse files
committed
Suggestion: Use a plain filter instead of restrictKeys
1 parent 855913f commit 5c25ab6

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/DFAMin.hs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ module DFAMin (minimizeDFA) where
55
import AbsSyn
66

77
import Control.Monad (guard)
8-
import Data.Foldable (fold)
8+
import Data.Bifunctor (second)
9+
-- import Data.Foldable (fold)
910
import Data.IntMap (IntMap)
1011
import Data.IntSet (IntSet)
11-
import Data.Map (Map)
12+
-- import Data.Map (Map)
1213

1314
import qualified Data.IntMap as IntMap
1415
import qualified Data.IntSet as IntSet
@@ -202,18 +203,18 @@ generateReverseTransitionCache dfa = IntMap.elems $
202203
pure (token, IntMap.singleton targetState (IntSet.singleton sourceState))
203204

204205

205-
-- | Given an IntMap and an IntSet, restrict the IntMap to the keys that are
206-
-- within the IntSet.
207-
--
208-
-- This function is a simple wrapper around 'IntMap.restrictKeys', provided for
209-
-- compatibility with older versions of @containers@.
210-
restrictKeys :: forall a. IntMap a -> IntSet -> IntMap a
211-
restrictKeys m s =
212-
#if MIN_VERSION_containers(0,6,0)
213-
IntMap.restrictKeys m s
214-
#else
215-
IntMap.filterWithKey (\k _ -> k `IntSet.member` s) m
216-
#endif
206+
-- -- | Given an IntMap and an IntSet, restrict the IntMap to the keys that are
207+
-- -- within the IntSet.
208+
-- --
209+
-- -- This function is a simple wrapper around 'IntMap.restrictKeys', provided for
210+
-- -- compatibility with older versions of @containers@.
211+
-- restrictKeys :: forall a. IntMap a -> IntSet -> IntMap a
212+
-- restrictKeys m s =
213+
-- #if MIN_VERSION_containers(0,6,0)
214+
-- IntMap.restrictKeys m s
215+
-- #else
216+
-- IntMap.filterWithKey (\k _ -> k `IntSet.member` s) m
217+
-- #endif
217218

218219

219220
-- | Given two sets X and Y, compute their intersection and difference.
@@ -245,8 +246,12 @@ groupEquivalentStates dfa = outerLoop ([], initialSubsets dfa)
245246
outerLoop :: ([EquivalenceClass], [EquivalenceClass]) -> [EquivalenceClass]
246247
outerLoop (r, []) = r
247248
outerLoop (r, a:w) = outerLoop $ List.foldl' refineWithX (a:r,w) $ do
248-
allPreviousStates <- reverseTransitionCache
249-
let x = fold $ restrictKeys allPreviousStates a
249+
allPreviousStates :: IntMap EquivalenceClass <- reverseTransitionCache
250+
-- let x = fold $ restrictKeys allPreviousStates a
251+
-- Is 'restrictKeys' here really better than the following simpler filter?
252+
-- The given asymptotics does look better, but aren't there overheads to create a balanced 'IntMap'
253+
-- just to destroy it immediately with a 'fold'?
254+
let x = IntSet.unions $ map (\ (tgt, srcs) -> if tgt `IntSet.member` a then srcs else IntSet.empty) $ IntMap.toList allPreviousStates
250255
guard $ not $ IntSet.null x
251256
pure x
252257

0 commit comments

Comments
 (0)