1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
-- A program for extracting strongly connected components from a .dot
-- file created by auxprogs/gen-mdg.
-- How to use: one of the following:
-- compile to an exe: ghc -o dottoscc DotToScc.hs
-- and then ./dottoscc name_of_file.dot
-- or interpret with runhugs:
-- runhugs DotToScc.hs name_of_file.dot
-- or run within hugs:
-- hugs DotToScc.hs
-- Main> imain "name_of_file.dot"
module Main where
import System
import List ( sort, nub )
usage :: IO ()
usage = putStrLn "usage: dottoscc <name_of_file.dot>"
main :: IO ()
main = do args <- getArgs
if length args /= 1
then usage
else imain (head args)
imain :: String -> IO ()
imain dot_file_name
= do edges <- read_dot_file dot_file_name
let sccs = gen_sccs edges
let pretty = showPrettily sccs
putStrLn pretty
where
showPrettily :: [[String]] -> String
showPrettily = unlines . concatMap showScc
showScc elems
= let n = length elems
in
[""]
++ (if n > 1 then [" -- "
++ show n ++ " modules in cycle"]
else [])
++ map (" " ++) elems
-- Read a .dot file and return a list of edges
read_dot_file :: String{-filename-} -> IO [(String,String)]
read_dot_file dot_file_name
= do bytes <- readFile dot_file_name
let linez = lines bytes
let edges = [(s,d) | Just (s,d) <- map maybe_mk_edge linez]
return edges
where
-- identify lines of the form "text1 -> text2" and return
-- text1 and text2
maybe_mk_edge :: String -> Maybe (String, String)
maybe_mk_edge str
= case words str of
[text1, "->", text2] -> Just (text1, text2)
other -> Nothing
-- Take the list of edges and return a topologically sorted list of
-- sccs
gen_sccs :: [(String,String)] -> [[String]]
gen_sccs raw_edges
= let clean_edges = sort (nub raw_edges)
nodes = nub (concatMap (\(s,d) -> [s,d]) clean_edges)
ins v = [u | (u,w) <- clean_edges, v==w]
outs v = [w | (u,w) <- clean_edges, v==u]
components = map (sort.utSetToList) (deScc ins outs nodes)
in
components
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
-- Graph-theoretic stuff that does the interesting stuff.
-- ==========================================================--
--
deScc :: (Ord a) =>
(a -> [a]) -> -- The "ins" map
(a -> [a]) -> -- The "outs" map
[a] -> -- The root vertices
[Set a] -- The topologically sorted components
deScc ins outs
= spanning . depthFirst
where depthFirst = snd . deDepthFirstSearch outs (utSetEmpty, [])
spanning = snd . deSpanningSearch ins (utSetEmpty, [])
-- =========================================================--
--
deDepthFirstSearch :: (Ord a) =>
(a -> [a]) -> -- The map,
(Set a, [a]) -> -- state: visited set,
-- current sequence of vertices
[a] -> -- input vertices sequence
(Set a, [a]) -- final state
deDepthFirstSearch
= foldl . search
where
search relation (visited, sequence) vertex
| utSetElementOf vertex visited = (visited, sequence )
| otherwise = (visited', vertex: sequence')
where
(visited', sequence')
= deDepthFirstSearch relation
(utSetUnion visited (utSetSingleton vertex), sequence)
(relation vertex)
-- ==========================================================--
--
deSpanningSearch :: (Ord a) =>
(a -> [a]) -> -- The map
(Set a, [Set a]) -> -- Current state: visited set,
-- current sequence of vertice sets
[a] -> -- Input sequence of vertices
(Set a, [Set a]) -- Final state
deSpanningSearch
= foldl . search
where
search relation (visited, utSetSequence) vertex
| utSetElementOf vertex visited = (visited, utSetSequence )
| otherwise = (visited', utSetFromList (vertex: sequence): utSetSequence)
where
(visited', sequence)
= deDepthFirstSearch relation
(utSetUnion visited (utSetSingleton vertex), [])
(relation vertex)
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
-- Most of this set stuff isn't needed.
-- ====================================--
-- === set ===--
-- ====================================--
data Set e = MkSet [e]
-- ==========================================================--
--
unMkSet :: (Ord a) => Set a -> [a]
unMkSet (MkSet s) = s
-- ==========================================================--
--
utSetEmpty :: (Ord a) => Set a
utSetEmpty = MkSet []
-- ==========================================================--
--
utSetIsEmpty :: (Ord a) => Set a -> Bool
utSetIsEmpty (MkSet s) = s == []
-- ==========================================================--
--
utSetSingleton :: (Ord a) => a -> Set a
utSetSingleton x = MkSet [x]
-- ==========================================================--
--
utSetFromList :: (Ord a) => [a] -> Set a
utSetFromList x = (MkSet . rmdup . sort) x
where rmdup [] = []
rmdup [x] = [x]
rmdup (x:y:xs) | x==y = rmdup (y:xs)
| otherwise = x: rmdup (y:xs)
-- ==========================================================--
--
utSetToList :: (Ord a) => Set a -> [a]
utSetToList (MkSet xs) = xs
-- ==========================================================--
--
utSetUnion :: (Ord a) => Set a -> Set a -> Set a
utSetUnion (MkSet []) (MkSet []) = (MkSet [])
utSetUnion (MkSet []) (MkSet (b:bs)) = (MkSet (b:bs))
utSetUnion (MkSet (a:as)) (MkSet []) = (MkSet (a:as))
utSetUnion (MkSet (a:as)) (MkSet (b:bs))
| a < b = MkSet (a: (unMkSet (utSetUnion (MkSet as) (MkSet (b:bs)))))
| a == b = MkSet (a: (unMkSet (utSetUnion (MkSet as) (MkSet bs))))
| a > b = MkSet (b: (unMkSet (utSetUnion (MkSet (a:as)) (MkSet bs))))
-- ==========================================================--
--
utSetIntersection :: (Ord a) => Set a -> Set a -> Set a
utSetIntersection (MkSet []) (MkSet []) = (MkSet [])
utSetIntersection (MkSet []) (MkSet (b:bs)) = (MkSet [])
utSetIntersection (MkSet (a:as)) (MkSet []) = (MkSet [])
utSetIntersection (MkSet (a:as)) (MkSet (b:bs))
| a < b = utSetIntersection (MkSet as) (MkSet (b:bs))
| a == b = MkSet (a: (unMkSet (utSetIntersection (MkSet as) (MkSet bs))))
| a > b = utSetIntersection (MkSet (a:as)) (MkSet bs)
-- ==========================================================--
--
utSetSubtraction :: (Ord a) => Set a -> Set a -> Set a
utSetSubtraction (MkSet []) (MkSet []) = (MkSet [])
utSetSubtraction (MkSet []) (MkSet (b:bs)) = (MkSet [])
utSetSubtraction (MkSet (a:as)) (MkSet []) = (MkSet (a:as))
utSetSubtraction (MkSet (a:as)) (MkSet (b:bs))
| a < b = MkSet (a: (unMkSet (utSetSubtraction (MkSet as) (MkSet (b:bs)))))
| a == b = utSetSubtraction (MkSet as) (MkSet bs)
| a > b = utSetSubtraction (MkSet (a:as)) (MkSet bs)
-- ==========================================================--
--
utSetElementOf :: (Ord a) => a -> Set a -> Bool
utSetElementOf x (MkSet []) = False
utSetElementOf x (MkSet (y:ys)) = x==y || (x>y && utSetElementOf x (MkSet ys))
-- ==========================================================--
--
utSetSubsetOf :: (Ord a) => Set a -> Set a -> Bool
utSetSubsetOf (MkSet []) (MkSet bs) = True
utSetSubsetOf (MkSet (a:as)) (MkSet bs)
= utSetElementOf a (MkSet bs) && utSetSubsetOf (MkSet as) (MkSet bs)
-- ==========================================================--
--
utSetUnionList :: (Ord a) => [Set a] -> Set a
utSetUnionList setList = foldl utSetUnion utSetEmpty setList
|