| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
UVMHS.Lib.Graph
Synopsis
- type Graph a = a ⇰ 𝑃 a
- graphTranspose :: Ord a => Graph a -> Graph a
- kosaraju :: Ord a => Graph a -> a ⇰ a
- sccGroups :: Ord a => (a ⇰ 𝑃 a) -> (a ⇰ a) ∧ (a ⇰ (𝑃 a ∧ 𝑃 a))
- sccEachGroupM :: forall a b m. (Ord a, Monad m) => 𝐼 a -> (a ⇰ 𝑃 a) -> (𝔹 -> 𝐼 a -> m (𝐼 b)) -> m (𝐼 b)
- sccEachGroup :: Ord a => 𝐼 a -> (a ⇰ 𝑃 a) -> (𝔹 -> 𝐼 a -> 𝐼 b) -> 𝐼 b
Documentation
type Graph a = a ⇰ 𝑃 a Source #
A Graph has node elements a, and is just a relation between nodes.
Notation:
kvss : Graph a
(same as general dictionaries)
graphTranspose :: Ord a => Graph a -> Graph a Source #
Property:
(v ↦ ks) ∈ graph-transpose(kvss) ⟺ ∀ (k ↦ vs) ∈ kvss. k ∈ ks ⟺ v ∈ vs
kosaraju :: Ord a => Graph a -> a ⇰ a Source #
Run the Kosaraju algorithm, which computes strongly connected components. The algorithm computes a canonical node for each strongly connected component, and maps each node to the canonical node for strongly connected component in which it lives.
Definition:
strongly-connected ∈ ℘(Graph) strongly-connected(kvss) ≜ ∀ k₁,k₂ ∈ nodes(kvss). ∃ path(k₁,k₂) strongly-connected-components ∈ ℘(℘(Graph)) strongly-connected-components(kvsss) ≜ (∀ kvss ∈ kvsss. strongly-connected(kvss)) ∧ (∀ kvss₁,kvss₂ ∈ kvsss. ¬strongly-connected(kvss₁ ∪kvss₂))
Property:
∀ kvss. strongly-connected-components( values( graph-transpose( { k ↦ {v} | (k ↦ v) ∈ kosaraju(kvss) }
sccGroups :: Ord a => (a ⇰ 𝑃 a) -> (a ⇰ a) ∧ (a ⇰ (𝑃 a ∧ 𝑃 a)) Source #
In the context of this function, we treat the element a like variables
that have a definition/use structure. The first argument deps encodes
dependencies from definitions of things to uses of things.
From a dependency relation, compute two elements: 1. The strongly connected components for the dependency relation, represented as a map from variables to a canonical SCC elements for its dependency group 2. A map from canonical SCC elements to: (a). All variables in its dependency group (i.e., the inverse of (1)) (b). All dependencies of all variables in the dependnecy group
- is computed using
kosaraju, but with an additional bit of information in the node representation that encodes def/use information, following these two semantic ideas: - *Use* of any variablexdepends on the *definition* of that variablex- If `{x ↦ y}` appears indeps, this means that *definition* of variablexdepends on *use* of variabley.
sccEachGroupM :: forall a b m. (Ord a, Monad m) => 𝐼 a -> (a ⇰ 𝑃 a) -> (𝔹 -> 𝐼 a -> m (𝐼 b)) -> m (𝐼 b) Source #
Compute strongly connected components following a dependency map, and process each component with a monadic action.
sccEachGroupM xs deps f
xsrepresents a desired ordering in which to process variables (e.g., if definitionsxandydo not depend on each other, andxcomes beforeyinxs, then callfon the SCC group forxbefore doing the same fory.)depsrepresents dependency information. SeesccGroupsfor how these are processed.- `f b xs′` represents an action to perform on each SCC group. If the group
is non-recursive, then
bwill beFalseand `|xs′| = 1`. If the group is recursive, thenbwill beTrueand `|xs′| ≥ 1`.