arnar (owner)

Revisions

gist: 16690 Download_button fork
public
Public Clone URL: git://gist.github.com/16690.git
Embed All Files: show embed
dependencyclosed.hs #
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
module Main where
 
import Data.Maybe (fromJust)
import Data.List (nub, intersect)
import Control.Monad.Writer
 
-- Modules in topological order, i.e.
-- a module must appear after all of its
-- dependencies
dependencies = [
 ("Base",[]),
 ("DateTime",["Base"]),
 ("Format",["Base"]),
 ("Iter",["Base"]),
 ("Async",["Base"]),
 ("DOM",["Base"]),
 ("Style",["DOM"]),
 ("Color",["Style"]),
 ("Logging",["Base"]),
 ("LoggingPane",["Logging"]),
 ("Selector",["Style"]),
 ("Signal",["Style"]),
 ("Visual",["Color"]),
 ("DragAndDrop",["Iter","Signal","Visual"]),
 ("Sortable",["DragAndDrop"])
 ]
 
modules = map fst dependencies
 
-- Simple transitive closure
closure :: [String] -> [String]
closure ms =
    let missing = nub $ filter (not . flip elem ms)
                  $ concatMap (fromJust . flip lookup dependencies) ms
    in
      if null missing
         then ms
         else closure (ms ++ missing)
 
antichains :: Writer [[String]] ()
antichains = antichains' [] modules
    where
      antichains' :: [String] -> [String] -> (Writer [[String]] ())
      antichains' ac [] = tell [ac]
      antichains' ac (candidate:rest) =
          do if ac `accepts` candidate
                then antichains' (candidate:ac) rest
                else return ()
             antichains' ac rest
      chain `accepts` candidate = null $ intersect (closure [candidate]) chain
 
main :: IO ()
main = do mapM_ (putStrLn . show . closure) $ snd $ runWriter antichains