-- | Topological sort of recursive functions.
recTopoSort :: (Eq a, Ord a) => (a -> [a]) -> [a] -> [[a]]
recTopoSort callees = filter (not . null) . f [] . sortBy (compare `on` length) . map (allDependencies callees)
  where
  f sofar a = case a of
    [] -> []
    a : b -> [ a | a <- a, not $ elem a sofar ] : f (nub $ a ++ sofar) b

-- | All the dependencies for a given functions, including that function.
recTopoSort :: (Eq a, Ord a) => (a -> [a]) -> [a] -> [[a]]
recTopoSort callees = filter (not . null) . f [] . sortBy (compare `on` length) . map (allDependencies callees)
  where
  f sofar a = case a of
    [] -> []
    a : b -> [ a | a <- a, not $ elem a sofar ] : f (nub $ a ++ sofar) b

-- | All the dependencies for a given functions, including that function.
recTopoSort callees = filter (not . null) . f [] . sortBy (compare `on` length) . map (allDependencies callees)
  where
  f sofar a = case a of
    [] -> []
    a : b -> [ a | a <- a, not $ elem a sofar ] : f (nub $ a ++ sofar) b

-- | All the dependencies for a given functions, including that function.