-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Library and utiltity for processing cabal's plan.json file
--   
--   This package provides a library for decoding <tt>plan.json</tt> files
--   as well as the simple tool <tt>cabal-plan</tt> for extracting and
--   pretty printing the information contained in the <tt>plan.json</tt>
--   file.
--   
--   <tt>plan.json</tt> files are generated by <a>cabal</a>'s <a>nix-style
--   local builds</a> and contain detailed information about the
--   build/install plan computed by the cabal solver.
@package cabal-plan
@version 0.4.0.0


-- | Tools to work with Directed Acyclic Graphs, by taking advantage of
--   topological sorting.
--   
--   <ul>
--   <li>SPDX-License-Id: BSD-3-Clause</li>
--   <li>Author: Oleg Grenrus</li>
--   </ul>
module Topograph

-- | Graph representation.
data G v a
G :: [a] -> a -> v -> v -> Maybe a -> a -> [a] -> a -> a -> Int -> Int -> a -> Int -> G v a

-- | all vertices, in topological order.
[gVertices] :: G v a -> [a]

-- | retrieve original vertex data. <i>O(1)</i>
[gFromVertex] :: G v a -> a -> v

-- | <i>O(log n)</i>
[gToVertex] :: G v a -> v -> Maybe a

-- | Outgoing edges.
[gEdges] :: G v a -> a -> [a]

-- | Upper bound of the path length. Negative if there aren't path.
--   <i>O(1)</i>
[gDiff] :: G v a -> a -> a -> Int
[gVerticeCount] :: G v a -> Int
[gToInt] :: G v a -> a -> Int

-- | Run action on topologically sorted representation of the graph.
--   
--   <h3><b>Examples</b></h3>
--   
--   <h4>Topological sorting</h4>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \G {..} -&gt; map gFromVertex gVertices
--   Right "axbde"
--   </pre>
--   
--   Vertices are sorted
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \G {..} -&gt; map gFromVertex $ sort gVertices
--   Right "axbde"
--   </pre>
--   
--   <h4>Outgoing edges</h4>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \G {..} -&gt; map (map gFromVertex . gEdges) gVertices
--   Right ["xbde","de","d","e",""]
--   </pre>
--   
--   Note: edges are always larger than source vertex:
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \G {..} -&gt; getAll $ foldMap (\a -&gt; foldMap (\b -&gt; All (a &lt; b)) (gEdges a)) gVertices
--   Right True
--   </pre>
--   
--   <h4>Not DAG</h4>
--   
--   <pre>
--   &gt;&gt;&gt; let loop = M.map S.fromList $ M.fromList [('a', "bx"), ('b', "cx"), ('c', "ax"), ('x', "")]
--   
--   &gt;&gt;&gt; runG loop $ \G {..} -&gt; map gFromVertex gVertices
--   Left "abc"
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; runG (M.singleton 'a' (S.singleton 'a')) $ \G {..} -&gt; map gFromVertex gVertices
--   Left "aa"
--   </pre>
runG :: forall v r. Ord v => Map v (Set v) -> (forall i. Ord i => G v i -> r) -> Either [v] r

-- | Like <a>runG</a> but returns <a>Maybe</a>
runG' :: forall v r. Ord v => Map v (Set v) -> (forall i. Ord i => G v i -> r) -> Maybe r

-- | All paths from <tt>a</tt> to <tt>b</tt>. Note that every path has at
--   least 2 elements, start and end. Use <a>allPaths'</a> for the
--   intermediate steps only.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; fmap3 gFromVertex $ allPaths g &lt;$&gt; gToVertex 'a' &lt;*&gt; gToVertex 'e'
--   Right (Just ["axde","axe","abde","ade","ae"])
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; fmap3 gFromVertex $ allPaths g &lt;$&gt; gToVertex 'a' &lt;*&gt; gToVertex 'a'
--   Right (Just [])
--   </pre>
allPaths :: forall v a. Ord a => G v a -> a -> a -> [[a]]

-- | <a>allPaths</a> without begin and end elements.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; fmap3 gFromVertex $ allPaths' g &lt;$&gt; gToVertex 'a' &lt;*&gt; gToVertex 'e' &lt;*&gt; pure []
--   Right (Just ["xd","x","bd","d",""])
--   </pre>
allPaths' :: forall v a. Ord a => G v a -> a -> a -> [a] -> [[a]]

-- | Like <a>allPaths</a> but return a <a>Tree</a>.
--   
--   <pre>
--   &gt;&gt;&gt; let t = runG example $ \g@G{..} -&gt; fmap3 gFromVertex $ allPathsTree g &lt;$&gt; gToVertex 'a' &lt;*&gt; gToVertex 'e'
--   
--   &gt;&gt;&gt; fmap3 (T.foldTree $ \a bs -&gt; if null bs then [[a]] else concatMap (map (a:)) bs) t
--   Right (Just (Just ["axde","axe","abde","ade","ae"]))
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; fmap3 (S.fromList . treePairs) t
--   Right (Just (Just (fromList [('a','b'),('a','d'),('a','e'),('a','x'),('b','d'),('d','e'),('x','d'),('x','e')])))
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; let ls = runG example $ \g@G{..} -&gt; fmap3 gFromVertex $ allPaths g &lt;$&gt; gToVertex 'a' &lt;*&gt; gToVertex 'e'
--   
--   &gt;&gt;&gt; fmap2 (S.fromList . concatMap pairs) ls
--   Right (Just (fromList [('a','b'),('a','d'),('a','e'),('a','x'),('b','d'),('d','e'),('x','d'),('x','e')]))
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; traverse3_ dispTree t
--   'a'
--     'x'
--       'd'
--         'e'
--       'e'
--     'b'
--       'd'
--         'e'
--     'd'
--       'e'
--     'e'
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; traverse3_ (putStrLn . T.drawTree . fmap show) t
--   'a'
--   |
--   +- 'x'
--   |  |
--   |  +- 'd'
--   |  |  |
--   |  |  `- 'e'
--   |  |
--   |  `- 'e'
--   ...
--   </pre>
allPathsTree :: forall v a. Ord a => G v a -> a -> a -> Maybe (Tree a)

-- | Depth-first paths starting at a vertex.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; fmap3 gFromVertex $ dfs g &lt;$&gt; gToVertex 'x'
--   Right (Just ["xde","xe"])
--   </pre>
dfs :: forall v a. Ord a => G v a -> a -> [[a]]

-- | like <a>dfs</a> but returns a <a>Tree</a>.
--   
--   <pre>
--   &gt;&gt;&gt; traverse2_ dispTree $ runG example $ \g@G{..} -&gt; fmap2 gFromVertex $ dfsTree g &lt;$&gt; gToVertex 'x'
--   'x'
--     'd'
--       'e'
--     'e'
--   </pre>
dfsTree :: forall v a. Ord a => G v a -> a -> Tree a

-- | Longest paths lengths starting from a vertex.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; longestPathLengths g &lt;$&gt; gToVertex 'a'
--   Right (Just [0,1,1,2,3])
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \G {..} -&gt; map gFromVertex gVertices
--   Right "axbde"
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; longestPathLengths g &lt;$&gt; gToVertex 'b'
--   Right (Just [0,0,0,1,2])
--   </pre>
longestPathLengths :: Ord a => G v a -> a -> [Int]

-- | Graph with all edges reversed.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ adjacencyList . transpose
--   Right [('a',""),('b',"a"),('d',"abx"),('e',"adx"),('x',"a")]
--   </pre>
--   
--   <h3><b>Properties</b></h3>
--   
--   Commutes with <a>closure</a>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ adjacencyList . closure . transpose
--   Right [('a',""),('b',"a"),('d',"abx"),('e',"abdx"),('x',"a")]
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ adjacencyList . transpose . closure
--   Right [('a',""),('b',"a"),('d',"abx"),('e',"abdx"),('x',"a")]
--   </pre>
--   
--   Commutes with <a>reduction</a>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ adjacencyList . reduction . transpose
--   Right [('a',""),('b',"a"),('d',"bx"),('e',"d"),('x',"a")]
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ adjacencyList . transpose . reduction
--   Right [('a',""),('b',"a"),('d',"bx"),('e',"d"),('x',"a")]
--   </pre>
transpose :: forall v a. Ord a => G v a -> G v (Down a)

-- | Transitive reduction.
--   
--   Smallest graph, such that if there is a path from <i>u</i> to <i>v</i>
--   in the original graph, then there is also such a path in the
--   reduction.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g -&gt; adjacencyList $ reduction g
--   Right [('a',"bx"),('b',"d"),('d',"e"),('e',""),('x',"d")]
--   </pre>
--   
--   Taking closure first doesn't matter:
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g -&gt; adjacencyList $ reduction $ closure g
--   Right [('a',"bx"),('b',"d"),('d',"e"),('e',""),('x',"d")]
--   </pre>
reduction :: Ord a => G v a -> G v a

-- | Transitive closure.
--   
--   A graph, such that if there is a path from <i>u</i> to <i>v</i> in the
--   original graph, then there is an edge from <i>u</i> to <i>v</i> in the
--   closure.
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g -&gt; adjacencyList $ closure g
--   Right [('a',"bdex"),('b',"de"),('d',"e"),('e',""),('x',"de")]
--   </pre>
--   
--   Taking reduction first, doesn't matter:
--   
--   <pre>
--   &gt;&gt;&gt; runG example $ \g -&gt; adjacencyList $ closure $ reduction g
--   Right [('a',"bdex"),('b',"de"),('d',"e"),('e',""),('x',"de")]
--   </pre>
closure :: Ord a => G v a -> G v a

-- | <pre>
--   &gt;&gt;&gt; runG example $ \g@G{..} -&gt; map (\(a,b) -&gt; [gFromVertex a, gFromVertex b]) $  S.toList $ edgesSet g
--   Right ["ax","ab","ad","ae","xd","xe","bd","de"]
--   </pre>
edgesSet :: Ord a => G v a -> Set (a, a)

-- | Recover adjacency map representation from the <a>G</a>.
--   
--   <pre>
--   &gt;&gt;&gt; runG example adjacencyMap
--   Right (fromList [('a',fromList "bdex"),('b',fromList "d"),('d',fromList "e"),('e',fromList ""),('x',fromList "de")])
--   </pre>
adjacencyMap :: Ord v => G v a -> Map v (Set v)

-- | Adjacency list representation of <a>G</a>.
--   
--   <pre>
--   &gt;&gt;&gt; runG example adjacencyList
--   Right [('a',"bdex"),('b',"d"),('d',"e"),('e',""),('x',"de")]
--   </pre>
adjacencyList :: Ord v => G v a -> [(v, [v])]

-- | Like <a>pairs</a> but for <a>Tree</a>.
treePairs :: Tree a -> [(a, a)]

-- | Consequtive pairs.
--   
--   <pre>
--   &gt;&gt;&gt; pairs [1..10]
--   [(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10)]
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; pairs []
--   []
--   </pre>
pairs :: [a] -> [(a, a)]

-- | Unwrap <a>Down</a>.
getDown :: Down a -> a
