{-# LANGUAGE OverloadedStrings #-}
module Algebra.Graph.Export (
Doc, isEmpty, literal, render,
(<+>), brackets, doubleQuotes, indent, unlines,
export
) where
import Data.Foldable (fold)
import Data.String hiding (unlines)
import Prelude hiding (unlines)
import Algebra.Graph.ToGraph (ToGraph, ToVertex, toAdjacencyMap)
import Algebra.Graph.AdjacencyMap (vertexList, edgeList)
import Algebra.Graph.Internal
newtype Doc s = Doc (List s) deriving (Semigroup (Doc s)
Doc s
Semigroup (Doc s)
-> Doc s
-> (Doc s -> Doc s -> Doc s)
-> ([Doc s] -> Doc s)
-> Monoid (Doc s)
[Doc s] -> Doc s
Doc s -> Doc s -> Doc s
forall s. Semigroup (Doc s)
forall s. Doc s
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall s. [Doc s] -> Doc s
forall s. Doc s -> Doc s -> Doc s
mconcat :: [Doc s] -> Doc s
$cmconcat :: forall s. [Doc s] -> Doc s
mappend :: Doc s -> Doc s -> Doc s
$cmappend :: forall s. Doc s -> Doc s -> Doc s
mempty :: Doc s
$cmempty :: forall s. Doc s
$cp1Monoid :: forall s. Semigroup (Doc s)
Monoid, b -> Doc s -> Doc s
NonEmpty (Doc s) -> Doc s
Doc s -> Doc s -> Doc s
(Doc s -> Doc s -> Doc s)
-> (NonEmpty (Doc s) -> Doc s)
-> (forall b. Integral b => b -> Doc s -> Doc s)
-> Semigroup (Doc s)
forall b. Integral b => b -> Doc s -> Doc s
forall s. NonEmpty (Doc s) -> Doc s
forall s. Doc s -> Doc s -> Doc s
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall s b. Integral b => b -> Doc s -> Doc s
stimes :: b -> Doc s -> Doc s
$cstimes :: forall s b. Integral b => b -> Doc s -> Doc s
sconcat :: NonEmpty (Doc s) -> Doc s
$csconcat :: forall s. NonEmpty (Doc s) -> Doc s
<> :: Doc s -> Doc s -> Doc s
$c<> :: forall s. Doc s -> Doc s -> Doc s
Semigroup)
instance (Monoid s, Show s) => Show (Doc s) where
show :: Doc s -> String
show = s -> String
forall a. Show a => a -> String
show (s -> String) -> (Doc s -> s) -> Doc s -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc s -> s
forall s. Monoid s => Doc s -> s
render
instance (Monoid s, Eq s) => Eq (Doc s) where
Doc s
x == :: Doc s -> Doc s -> Bool
== Doc s
y | Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
x = Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
y
| Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
y = Bool
False
| Bool
otherwise = Doc s -> s
forall s. Monoid s => Doc s -> s
render Doc s
x s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== Doc s -> s
forall s. Monoid s => Doc s -> s
render Doc s
y
instance (Monoid s, Ord s) => Ord (Doc s) where
compare :: Doc s -> Doc s -> Ordering
compare Doc s
x Doc s
y | Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
x = if Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
y then Ordering
EQ else Ordering
LT
| Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
y = Ordering
GT
| Bool
otherwise = s -> s -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Doc s -> s
forall s. Monoid s => Doc s -> s
render Doc s
x) (Doc s -> s
forall s. Monoid s => Doc s -> s
render Doc s
y)
instance IsString s => IsString (Doc s) where
fromString :: String -> Doc s
fromString = s -> Doc s
forall s. s -> Doc s
literal (s -> Doc s) -> (String -> s) -> String -> Doc s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> s
forall a. IsString a => String -> a
fromString
isEmpty :: Doc s -> Bool
isEmpty :: Doc s -> Bool
isEmpty (Doc List s
xs) = List s -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null List s
xs
literal :: s -> Doc s
literal :: s -> Doc s
literal = List s -> Doc s
forall s. List s -> Doc s
Doc (List s -> Doc s) -> (s -> List s) -> s -> Doc s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> List s
forall (f :: * -> *) a. Applicative f => a -> f a
pure
render :: Monoid s => Doc s -> s
render :: Doc s -> s
render (Doc List s
x) = List s -> s
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold List s
x
(<+>) :: IsString s => Doc s -> Doc s -> Doc s
Doc s
x <+> :: Doc s -> Doc s -> Doc s
<+> Doc s
y | Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
x = Doc s
y
| Doc s -> Bool
forall s. Doc s -> Bool
isEmpty Doc s
y = Doc s
x
| Bool
otherwise = Doc s
x Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
" " Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
y
infixl 7 <+>
brackets :: IsString s => Doc s -> Doc s
brackets :: Doc s -> Doc s
brackets Doc s
x = Doc s
"[" Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
x Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
"]"
doubleQuotes :: IsString s => Doc s -> Doc s
doubleQuotes :: Doc s -> Doc s
doubleQuotes Doc s
x = Doc s
"\"" Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
x Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
"\""
indent :: IsString s => Int -> Doc s -> Doc s
indent :: Int -> Doc s -> Doc s
indent Int
spaces Doc s
x = String -> Doc s
forall a. IsString a => String -> a
fromString (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
spaces Char
' ') Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
x
unlines :: IsString s => [Doc s] -> Doc s
unlines :: [Doc s] -> Doc s
unlines [] = Doc s
forall a. Monoid a => a
mempty
unlines (Doc s
x:[Doc s]
xs) = Doc s
x Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
"\n" Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> [Doc s] -> Doc s
forall s. IsString s => [Doc s] -> Doc s
unlines [Doc s]
xs
export :: (Ord a, ToGraph g, ToVertex g ~ a) => (a -> Doc s) -> (a -> a -> Doc s) -> g -> Doc s
export :: (a -> Doc s) -> (a -> a -> Doc s) -> g -> Doc s
export a -> Doc s
v a -> a -> Doc s
e g
g = Doc s
vDoc Doc s -> Doc s -> Doc s
forall a. Semigroup a => a -> a -> a
<> Doc s
eDoc
where
vDoc :: Doc s
vDoc = [Doc s] -> Doc s
forall a. Monoid a => [a] -> a
mconcat ([Doc s] -> Doc s) -> [Doc s] -> Doc s
forall a b. (a -> b) -> a -> b
$ (a -> Doc s) -> [a] -> [Doc s]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc s
v (AdjacencyMap a -> [a]
forall a. AdjacencyMap a -> [a]
vertexList AdjacencyMap a
AdjacencyMap (ToVertex g)
adjMap)
eDoc :: Doc s
eDoc = [Doc s] -> Doc s
forall a. Monoid a => [a] -> a
mconcat ([Doc s] -> Doc s) -> [Doc s] -> Doc s
forall a b. (a -> b) -> a -> b
$ ((a, a) -> Doc s) -> [(a, a)] -> [Doc s]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> a -> Doc s) -> (a, a) -> Doc s
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> a -> Doc s
e) (AdjacencyMap a -> [(a, a)]
forall a. AdjacencyMap a -> [(a, a)]
edgeList AdjacencyMap a
AdjacencyMap (ToVertex g)
adjMap)
adjMap :: AdjacencyMap (ToVertex g)
adjMap = g -> AdjacencyMap (ToVertex g)
forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
toAdjacencyMap g
g