Skip to content

Commit 8a31fd7

Browse files
committed
Update for new version of generics-rep
closes #2
1 parent 2dba1a0 commit 8a31fd7

File tree

4 files changed

+61
-48
lines changed

4 files changed

+61
-48
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"purescript-prelude": "^3.1.1",
1616
"purescript-console": "^3.0.0",
17-
"purescript-generics-rep": "^5.0.0",
17+
"purescript-generics-rep": "^6.1.1",
1818
"viz.js": "^1.8.0",
1919
"purescript-dotlang": "^1.1.0",
2020
"purescript-graphviz": "^1.0.0"

src/Data/GenericGraph.purs

+29-14
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ module Data.GenericGraph where
22

33
import Control.Semigroupoid ((>>>))
44
import Data.Array (concat, foldr, (!!), (:))
5-
import Data.DotLang (Attr(..), Edge(..), EdgeType(..), FillStyle(..), Graph, Node(..), graphFromElements, changeNodeId, nodeId)
6-
import Data.Generic.Rep (class Generic, Argument(..), Constructor(..), Field(..), NoArguments, NoConstructors, Product(..), Rec(..), Sum(..), from)
5+
import Data.DotLang (Edge(..), EdgeType(..), Graph, Node(..), graphFromElements, changeNodeId, nodeId)
6+
import Data.DotLang.Attr (FillStyle(..))
7+
import Data.DotLang.Attr.Edge as E
8+
import Data.DotLang.Attr.Node as N
9+
import Data.Generic.Rep (class Generic, Argument(..), Constructor(..), NoArguments, NoConstructors, Product(..), Sum(..), from)
710
import Data.Maybe (Maybe(..), fromMaybe)
811
import Data.String (joinWith)
912
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
1013
import Data.Tuple (Tuple(..), fst)
11-
import Prelude (class Show, id, show, ($), (+), (<$>), (<>))
12-
14+
import Prelude (class Show, identity, show, ($), (+), (<$>), (<>))
15+
import Prim.Row (class Cons, class Lacks)
16+
import Prim.RowList (class RowToList, Nil, Cons)
17+
import Record as Record
18+
import Type.Data.RowList (RLProxy(..))
19+
import Type.RowList (class ListToRow)
1320

1421
-- Tree type
1522
data Tree a = Root a (Array (Tree a))
@@ -67,14 +74,22 @@ instance arrayEdges :: Edges a => Edges (Array a) where
6774
instance genericReprArgument :: Edges a => GenericEdges (Argument a) where
6875
genericEdges' (Argument a) = edges a
6976

77+
class (RowToList r rl) <= GenericEdgesRowList r rl | rl -> r where
78+
rlEdges :: RLProxy rl -> Record r -> Array (Tree (Maybe Node))
7079

71-
instance genericEdgesRec :: GenericEdges a => GenericEdges (Rec a) where
72-
genericEdges' (Rec a) = genericEdges' a
80+
instance emptyRowToEdge :: (RowToList r Nil) => GenericEdgesRowList r Nil where
81+
rlEdges _ _ = []
7382

74-
instance genericEdgesField :: (Edges a, IsSymbol name) => GenericEdges (Field name a) where
75-
genericEdges' (Field a) = Root (Just $ Node fieldName []) [ edges a ]
83+
instance consRowToEdge :: (Edges ty, Lacks name tailRow, GenericEdgesRowList tailRow tail, Cons name ty tailRow r, IsSymbol name, RowToList r (Cons name ty tail)) => GenericEdgesRowList r (Cons name ty tail) where
84+
rlEdges _ r = Root (Just $ Node fieldName []) [edges fieldValue] : rlEdges (RLProxy :: RLProxy tail) (Record.delete fieldSymbol r)
7685
where
77-
fieldName = reflectSymbol (SProxy :: SProxy name)
86+
fieldSymbol = SProxy :: SProxy name
87+
fieldName = reflectSymbol fieldSymbol
88+
fieldValue :: ty
89+
fieldValue = Record.get fieldSymbol r
90+
91+
instance genericEdgesRec :: (RowToList r rl, GenericEdgesRowList r rl) => Edges (Record r) where
92+
edges r = Root (Just $ Node "root" []) (rlEdges (RLProxy :: RLProxy rl) r)
7893

7994
-- | A `Generic` implementation of the `eq` member from the `Eq` type class.
8095
genericEdges :: forall a rep. Generic a rep => GenericEdges rep => a -> Tree (Maybe Node)
@@ -101,7 +116,7 @@ uniqueNodes :: Tree Node -> Tree Node
101116
uniqueNodes = (uniqueNodes' 0) >>> fst
102117

103118
extractEdges :: Node -> Tree Node -> Array Edge
104-
extractEdges parent (Root node children) = [Edge Forward (nodeId parent) (nodeId node)] <>
119+
extractEdges parent (Root node children) = [Edge Forward (nodeId parent) (nodeId node) []] <>
105120
(concat $
106121
(extractEdges node) <$> children)
107122

@@ -111,8 +126,8 @@ extractNodes (Root node children) = node : (concat $ extractNodes <$> children)
111126
-- | genenric version of toGraph not renaming nodes.
112127
genericToGraphUnique a. Edges a => a -> Graph
113128
genericToGraphUnique e
114-
= id
115-
$ (\f -> graphFromElements ((Node "root" [Style Invis]) : extractNodes f) (extractEdges (Node "root" []) f))
129+
= identity
130+
$ (\f -> graphFromElements ((Node "root" [N.Style Invis]) : extractNodes f) (extractEdges (Node "root" []) f))
116131
$ fromMaybe (Root (Node "" []) [])
117132
$ (\a -> a !! 0)
118133
$ eliminateNothings
@@ -121,8 +136,8 @@ genericToGraphUnique e
121136
-- | generic version of toGraph. Renaming Nodes to make them unique
122137
genericToGraph :: a. Edges a => a -> Graph
123138
genericToGraph e
124-
= id
125-
$ (\f -> graphFromElements ((Node "root" [Style Invis]) : extractNodes f) (extractEdges (Node "root" []) f))
139+
= identity
140+
$ (\f -> graphFromElements ((Node "root" [N.Style Invis]) : extractNodes f) (extractEdges (Node "root" []) f))
126141
$ uniqueNodes
127142
$ fromMaybe (Root (Node "" []) [])
128143
$ (\a -> a !! 0)

test/Example.purs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Test.Example where
22

33
import Data.DotLang (class GraphRepr, toGraph)
4+
import Data.DotLang.Class (toText)
45
import Data.Function (($))
56
import Data.Generic.Rep (class Generic)
67
import Data.GenericGraph (class Edges, genericEdges, genericToGraph)
7-
import Graphics.Graphviz (Engine(..), renderToSvg)
8+
import Graphics.Graphviz (renderToText, Engine(..), Format(..))
89

910
data Tree' a = Leaf' | Node' (Tree' a) a (Tree' a)
1011

@@ -13,5 +14,5 @@ derive instance treeGeneric :: Generic (Tree' a) _
1314
instance treeEdges :: Edges a => Edges (Tree' a) where edges x = genericEdges x
1415
instance treeDotRepr :: Edges a => GraphRepr (Tree' a) where toGraph = genericToGraph
1516

16-
example = renderToSvg Dot $ toGraph $
17+
example = renderToText Dot Svg $ toText $ toGraph $
1718
Node' Leaf' 3 (Node' (Node' Leaf' 5 Leaf') 4 Leaf')

test/Main.purs

+28-31
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@ module Test.Main where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.AVar (AVAR)
7-
import Control.Monad.Eff.Console (CONSOLE, log)
8-
import Data.DotLang (class GraphRepr, toGraph, toText)
5+
import Data.DotLang (class GraphRepr, toGraph)
6+
import Data.DotLang.Class (toText)
97
import Data.Foldable (foldr)
108
import Data.Generic.Rep (class Generic)
119
import Data.Generic.Rep.Show (genericShow)
1210
import Data.GenericGraph (class Edges, genericEdges, genericToGraph)
11+
import Effect (Effect)
12+
import Effect.Aff (Aff)
13+
import Effect.Aff.Class (liftAff)
14+
import Effect.Class (liftEffect)
15+
import Effect.Class.Console (logShow)
16+
import Test.Example (example)
1317
import Test.Unit (suite, test)
1418
import Test.Unit.Assert (equal)
15-
import Test.Unit.Console (TESTOUTPUT)
19+
import Test.Unit.Console (log)
1620
import Test.Unit.Main (runTest)
17-
import Test.Example (example)
1821

1922
--Simple
2023
data Simple = A | B
@@ -64,42 +67,36 @@ newtype Todo = Todo
6467

6568
derive instance genericTodo :: Generic Todo _
6669
instance showTodo :: Show Todo where show = genericShow
70+
6771
instance graphReprTodo :: GraphRepr Todo where toGraph = genericToGraph
6872
instance egdesTodo :: Edges Todo where edges x = genericEdges x
6973

70-
main :: Eff
71-
( console :: CONSOLE
72-
, avar :: AVAR
73-
, testOutput :: TESTOUTPUT
74-
)
75-
Unit
76-
main = do
77-
-- log $ toText $ genericToGraph $ fromArray [1, 2, 3, 4, 7]
78-
-- log $ toText $ toGraph $ (Node' Leaf' 3 (Node' (Node' Leaf' 5 Leaf') 4 Leaf'))
79-
log $ example
80-
main'
81-
82-
83-
main' ::
84-
Eff
85-
( console :: CONSOLE
86-
, testOutput :: TESTOUTPUT
87-
, avar :: AVAR
88-
)
89-
Unit
90-
main' = runTest do
74+
-- main = do
75+
-- log $ toText $ genericToGraph $ fromArray [1, 2, 3, 4, 7]
76+
-- log $ toText $ toGraph $ (Node' Leaf' 3 (Node' (Node' Leaf' 5 Leaf') 4 Leaf'))
77+
-- val <- example
78+
-- main'
79+
80+
81+
main :: Effect Unit
82+
main = runTest do
9183
suite "GenericGraph" do
9284
test "simple" do
93-
equal "digraph {root [style=invis]; 0 [label=\"A\"]; root -> 0; }" (toText $ toGraph A)
85+
equal "digraph {root [style=invis]; 0 [label=\"A\"]; root -> 0 []; }" (toText $ toGraph A)
9486
test "recursive" do
9587
equal
96-
"digraph {root [style=invis]; 0 [label=\"Node\"]; 1 [label=\"Node\"]; 2 [label=\"Leaf\"]; root -> 0; 0 -> 1; 1 -> 2; }"
88+
"digraph {root [style=invis]; 0 [label=\"Node\"]; 1 [label=\"Node\"]; 2 [label=\"Leaf\"]; root -> 0 []; 0 -> 1 []; 1 -> 2 []; }"
9789
(toText $ toGraph $ Node (Node Leaf))
9890
test "list" do
9991
equal
100-
"digraph {root [style=invis]; 0 [label=\"Cons'\"]; 4 [label=\"1\"]; 1 [label=\"Cons'\"]; 3 [label=\"2\"]; 2 [label=\"Nil\"]; root -> 0; 0 -> 4; 0 -> 1; 1 -> 3; 1 -> 2; }"
92+
"digraph {root [style=invis]; 0 [label=\"Cons'\"]; 4 [label=\"1\"]; 1 [label=\"Cons'\"]; 3 [label=\"2\"]; 2 [label=\"Nil\"]; root -> 0 []; 0 -> 4 []; 0 -> 1 []; 1 -> 3 []; 1 -> 2 []; }"
10193
(toText $ toGraph $ Cons' 1 (Cons' 2 Nil))
10294
test "tree" do
10395
equal
104-
"digraph {root [style=invis]; 0 [label=\"Node'\"]; 9 [label=\"Leaf'\"]; 8 [label=\"3\"]; 1 [label=\"Node'\"]; 4 [label=\"Node'\"]; 7 [label=\"Leaf'\"]; 6 [label=\"5\"]; 5 [label=\"Leaf'\"]; 3 [label=\"4\"]; 2 [label=\"Leaf'\"]; root -> 0; 0 -> 9; 0 -> 8; 0 -> 1; 1 -> 4; 4 -> 7; 4 -> 6; 4 -> 5; 1 -> 3; 1 -> 2; }"
96+
"digraph {root [style=invis]; 0 [label=\"Node'\"]; 9 [label=\"Leaf'\"]; 8 [label=\"3\"]; 1 [label=\"Node'\"]; 4 [label=\"Node'\"]; 7 [label=\"Leaf'\"]; 6 [label=\"5\"]; 5 [label=\"Leaf'\"]; 3 [label=\"4\"]; 2 [label=\"Leaf'\"]; root -> 0 []; 0 -> 9 []; 0 -> 8 []; 0 -> 1 []; 1 -> 4 []; 4 -> 7 []; 4 -> 6 []; 4 -> 5 []; 1 -> 3 []; 1 -> 2 []; }"
10597
(toText $ toGraph $ Node' Leaf' 3 (Node' (Node' Leaf' 5 Leaf') 4 Leaf'))
98+
test "record" do
99+
let example = toText $ toGraph $ Todo {id: 1, text: "asd", newText: "asd", completed: true, editing: true }
100+
equal
101+
"digraph {root [style=invis]; 0 [label=\"Todo\"]; 1 [label=\"root\"]; 10 [label=\"completed\"]; 11 [label=\"true\"]; 8 [label=\"editing\"]; 9 [label=\"true\"]; 6 [label=\"id\"]; 7 [label=\"1\"]; 4 [label=\"newText\"]; 5 [label=\"\\\"asd\\\"\"]; 2 [label=\"text\"]; 3 [label=\"\\\"asd\\\"\"]; root -> 0 []; 0 -> 1 []; 1 -> 10 []; 10 -> 11 []; 1 -> 8 []; 8 -> 9 []; 1 -> 6 []; 6 -> 7 []; 1 -> 4 []; 4 -> 5 []; 1 -> 2 []; 2 -> 3 []; }"
102+
example

0 commit comments

Comments
 (0)