{-# OPTIONS_HADDOCK show-extensions #-}
module Debug.SimpleExpr.GraphUtils
(
exprToGraph,
plotExpr,
plotDGraphPng,
simpleExprToGraph,
appendNodeToGraph,
)
where
import Control.Concurrent (ThreadId)
import Data.Fix (Fix (..))
import Data.Graph.DGraph (DGraph, insertArc)
import Data.Graph.Types (Arc (..), empty, insertVertex, union)
import Data.Graph.VisualizeAlternative (plotDGraph, plotDGraphPng)
import Debug.SimpleExpr.Expr (Expr, SimpleExpr, SimpleExprF (..), content, dependencies)
import Prelude (IO, String, fmap, foldr, show, ($), (.))
simpleExprToGraph :: SimpleExpr -> DGraph String ()
simpleExprToGraph :: Fix SimpleExprF -> DGraph String ()
simpleExprToGraph (Fix SimpleExprF (Fix SimpleExprF)
e) = case SimpleExprF (Fix SimpleExprF)
e of
NumberF Integer
n -> String -> [String] -> DGraph String () -> DGraph String ()
appendNodeToGraph (forall a. Show a => a -> String
show Integer
n) [] DGraph String ()
graph
VariableF String
c -> String -> [String] -> DGraph String () -> DGraph String ()
appendNodeToGraph String
c [] DGraph String ()
graph
BinaryFuncF String
_ Fix SimpleExprF
a Fix SimpleExprF
b -> String -> [String] -> DGraph String () -> DGraph String ()
appendNodeToGraph (forall a. Show a => a -> String
show (forall (f :: * -> *). f (Fix f) -> Fix f
Fix SimpleExprF (Fix SimpleExprF)
e)) [forall a. Show a => a -> String
show Fix SimpleExprF
a, forall a. Show a => a -> String
show Fix SimpleExprF
b] DGraph String ()
graph
SymbolicFuncF String
_ [Fix SimpleExprF]
args' -> String -> [String] -> DGraph String () -> DGraph String ()
appendNodeToGraph (forall a. Show a => a -> String
show (forall (f :: * -> *). f (Fix f) -> Fix f
Fix SimpleExprF (Fix SimpleExprF)
e)) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Show a => a -> String
show [Fix SimpleExprF]
args') DGraph String ()
graph
where
graph :: DGraph String ()
graph = forall d. Expr d => d -> DGraph String ()
exprToGraph forall a b. (a -> b) -> a -> b
$ Fix SimpleExprF -> [Fix SimpleExprF]
dependencies (forall (f :: * -> *). f (Fix f) -> Fix f
Fix SimpleExprF (Fix SimpleExprF)
e)
appendNodeToGraph :: String -> [String] -> DGraph String () -> DGraph String ()
appendNodeToGraph :: String -> [String] -> DGraph String () -> DGraph String ()
appendNodeToGraph String
newNodeName [String]
depNodeNames DGraph String ()
graph = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr String -> DGraph String () -> DGraph String ()
addArc DGraph String ()
initGraph [String]
depNodeNames
where
addArc :: String -> DGraph String () -> DGraph String ()
addArc String
depName = forall v e.
(Hashable v, Eq v) =>
Arc v e -> DGraph v e -> DGraph v e
insertArc (forall v e. v -> v -> e -> Arc v e
Arc String
depName String
newNodeName ())
initGraph :: DGraph String ()
initGraph = forall (g :: * -> * -> *) v e.
(Graph g, Hashable v, Eq v) =>
v -> g v e -> g v e
insertVertex String
newNodeName DGraph String ()
graph
exprToGraph :: Expr d => d -> DGraph String ()
exprToGraph :: forall d. Expr d => d -> DGraph String ()
exprToGraph d
d = case forall inner outer. ListOf inner outer => outer -> [inner]
content d
d of
[] -> forall (g :: * -> * -> *) v e. (Graph g, Hashable v) => g v e
empty
[Fix SimpleExprF
v] -> Fix SimpleExprF -> DGraph String ()
simpleExprToGraph Fix SimpleExprF
v
(Fix SimpleExprF
v : [Fix SimpleExprF]
vs) -> Fix SimpleExprF -> DGraph String ()
simpleExprToGraph Fix SimpleExprF
v forall (g :: * -> * -> *) v e.
(Graph g, Hashable v, Eq v) =>
g v e -> g v e -> g v e
`union` forall d. Expr d => d -> DGraph String ()
exprToGraph [Fix SimpleExprF]
vs
plotExpr :: Expr d => d -> IO ThreadId
plotExpr :: forall d. Expr d => d -> IO ThreadId
plotExpr = forall v e.
(Hashable v, Ord v, PrintDot v, Show v, Show e) =>
DGraph v e -> IO ThreadId
plotDGraph forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Expr d => d -> DGraph String ()
exprToGraph