Changeset 12775
- Timestamp:
- 03.11.2009 15:03:01 (3 weeks ago)
- Files:
-
- 1 modified
-
trunk/Common/Lib/Rel.hs (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/Lib/Rel.hs
r12774 r12775 128 128 -- | make relation irreflexive 129 129 irreflex :: Ord a => Rel a -> Rel a 130 irreflex (Rel m) = Rel $ rmNull $ Map.mapWithKey (Set.delete)m130 irreflex (Rel m) = Rel $ rmNull $ Map.mapWithKey Set.delete m 131 131 132 132 -- | compute strongly connected components for a transitively closed relation … … 146 146 147 147 setToMap :: Ord a => Set.Set a -> Map.Map a a 148 setToMap s = Map.fromDistinctAscList $ 149 List.map (\ a -> (a, a)) $ Set.toList s 148 setToMap = Map.fromDistinctAscList . List.map (\ a -> (a, a)) . Set.toList 150 149 151 150 {- | transitive reduction (minimal relation with the same transitive closure) … … 188 187 map :: (Ord a, Ord b) => (a -> b) -> Rel a -> Rel b 189 188 map f (Rel m) = Rel $ Map.foldWithKey 190 ( \ a v -> Map.insertWith Set.union (f a) $ Set.map f v) Map.empty m189 ( \ a -> Map.insertWith Set.union (f a) . Set.map f) Map.empty m 191 190 192 191 -- | Restriction of a relation under a set … … 204 203 -- | convert a set of ordered pairs to a relation 205 204 fromSet :: (Ord a) => Set.Set (a, a) -> Rel a 206 fromSet s = fromAscList $ Set.toList s205 fromSet = fromAscList . Set.toList 207 206 208 207 -- | convert a sorted list of ordered pairs to a relation … … 223 222 between 1 and the second value that is output. -} 224 223 toPrecMap :: Ord a => Rel a -> (Map.Map a Int, Int) 225 toPrecMap r= foldl ( \ (m1, c) s -> let n = c + 1 in224 toPrecMap = foldl ( \ (m1, c) s -> let n = c + 1 in 226 225 (Set.fold ( \ i -> Map.insert i n) m1 s, n)) 227 (Map.empty, 0) $ topSort r226 (Map.empty, 0) . topSort 228 227 229 228 topSortDAG :: Ord a => Rel a -> [Set.Set a] … … 254 253 else let Rel im = irreflex r 255 254 mr = elemsSet im Set.\\ Map.keysSet im 256 in if Set.null mr then Map.keysSet $ Map.filterWithKey (\ k v ->257 Set.singleton k == v) m255 in if Set.null mr then Map.keysSet $ Map.filterWithKey 256 ((==) . Set.singleton) m 258 257 else mr 259 258 … … 287 286 let (a, b) = Set.deleteFindMin c 288 287 (m, d) = Set.deleteFindMax c 289 in insert m a $ foldr ( \ (x, y) -> insert x y) (delete a a r) $288 in insert m a $ foldr (uncurry insert) (delete a a r) $ 290 289 zip (Set.toList d) (Set.toList b) 291 290 … … 296 295 haveCommonLeftElem :: (Ord a) => a -> a -> Rel a -> Bool 297 296 haveCommonLeftElem t1 t2 = 298 Map.fold(\ e rs -> rs || (t1 `Set.member` e && 299 t2 `Set.member` e)) False . toMap 297 Map.fold(\ e -> (|| Set.member t1 e && Set.member t2 e)) False . toMap 300 298 301 299 -- | partitions a set into a list of disjoint non-empty subsets … … 327 325 where iso x y = member x y rel && member y x rel 328 326 check s = Set.null s || 329 Set.fold (\ y rs -> rs &&330 not (haveCommonLeftElem x y rel)) True s'327 Set.fold (\ y -> 328 (&& not (haveCommonLeftElem x y rel))) True s' 331 329 && check s' 332 330 where (x, s') = Set.deleteFindMin s