@@ -5,10 +5,11 @@ module DFAMin (minimizeDFA) where
55import AbsSyn
66
77import Control.Monad (guard )
8- import Data.Foldable (fold )
8+ import Data.Bifunctor (second )
9+ -- import Data.Foldable (fold)
910import Data.IntMap (IntMap )
1011import Data.IntSet (IntSet )
11- import Data.Map (Map )
12+ -- import Data.Map (Map)
1213
1314import qualified Data.IntMap as IntMap
1415import 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