I haven’t dug too deep into graphs when it comes to programming. Never really needed to, I guess. I’ve taken some time to look up graphs and google up haskell’s equivalent… to see that most of the resources are old. Well, I mean, that’s already a thing, and it doesn’t mean they’re already bitrotted just because of that, but they also were a little unclear.
I’ve seen something like
data Node a = MkNode a [a]
-- where [Node a] is a list of adjacent nodes to the current node
type Graph a = [Node a]
But it’s been called to my attention that this is technically not a graph, but a tree with cycles. Which is precisely what a tree is: it’s a directed acyclic graph where every node is connected. This is also basically the implementation that containers uses. (Though for whatever reason, containers’ graphs might not be so good…)
However I’ve seen other things like
http://www.cs.tufts.edu/~nr/pubs/zipcfg.pdf
http://www.cs.tufts.edu/~nr/pubs/hoopl10.pdf
And other resources like stackoverflow etc feel like they get ahead of themselves, like
Which just creates a bunch of type aliases. I understand it’s for performance reasons, but it does not make understanding this subject any easier.
From what I’ve read besides the aforementioned representation, there is also
data Edge a = MkEdge a a
data Graph a = MkGraph [a] [Edge a]
Which stores nodes in the first list, and edges between them in the second. I understand that the types I’m making here are equivalent to tuples, but I feel like they make it easier to see the intent of what I’m writing.
I’ve also found a couple interesting explanations about why graphs are perhaps not as simple to represent as something like trees in algebraic data types. It’s because graphs are not algebraic (though, obviously algebraic-graphs has a representation that makes them such, but it doesn’t look different from the first representation I posted…), and ADTs are more suited for representing types in the form of trees. I’ve also found different answers that allude to tying the knot to make graphs as a recursive data type, but somehow, I’ve never seen such a type, only allusions to it.
I was a little disappointed that there wasn’t a neat resource that compiled different representations of graphs in Haskell and just showed the more easy to read version of their definition first. It feels like a lot of these resources start talking and showing other things before showing their representation in an understandable way. Also, probably a big reason I’m not understanding them this easily is because I’m probably a little rusty when it comes to Haskell, but I’m not sure.
So that’s the reason I’ve made this little thread. I’m interested in people’s responses to this, and I guess I’m kind of hoping someone else would do the work of compiling a list of interesting different representations of graphs. Also because I’m hoping Google will give this a higher priority than the more hodge-podge stackoverflow answer I got so that when other people (or when I forget in the future and end up looking it up again) there’s a more up-to-date resource that might be easier to understand.
I’ve renamed this post to reflect that I’m interested in any different functional representation of graphs, even if they’re in other languages… though, I’d be more interested in something that doesn’t really need dependent types to work!



