-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Graph datatype abstract, remove awkward instances #95
Comments
So I think the right way to go is using pattern synonyms. It allows to "hide" the real constructor, but still allows pattern matching (even if it can be really bad in performances).
So I think it can cover almost all possible representations :)
I agree that this instance is awkward, but sadly, for example, |
The whole point of hiding constructors is to avoid leaking any information about the structure of the graph: by providing pattern synonyms you will allow the user to distinguish
That's true, but I don't think it is possible to implement Note that we could add unorderedVertexList :: Graph a -> [a]
unorderedVertexList = foldg [] pure (++) (++)
Are you sure they are genuinely useful? My impression is that pretty much any use of them is a bug waiting to happen.
This will lead to orphan instances, I would prefer to avoid that. |
Ah I forgot about impotence ! But you are right, goodbye poor little constructors ^^
Yes, that is true.
Certainly ^^. Moreover, everything is definable for anyone using For sure, all of this will need some explanations somewhere ! Note that we will also have to change |
I think everything from the
Yes, you are right. |
Is traverse f = foldg (pure Empty) (fmap Vertex . f) (liftA2 Overlay) (liftA2 Connect) suitable ? But if by Once you have |
@nobrakal Indeed, users will be able to implement
By "safe" I meant the user is supposed to follow the laws specified in the https://github.com/snowleopard/alga/blob/master/src/Algebra/Graph/ToGraph.hs#L54-L57
In case of your |
@snowleopard So is there anything else to do here ? About |
@nobrakal Yes, let's start by removing problematic instances. And then the next step will be to make |
Just realized that we will have a problem with with
We could require |
@nobrakal Perhaps, we should just drop these functions from the API. I think all of them will be available via the |
See #95. * Remove `Foldable` and `Traversable` instances, and associated comments. * Drop `isEmpty`, `hasVertex`, `vertexCount`, `vertexList`, `vertexSet`, `vertexIntSet` and `box` from `Algebra.Graph.HigherKinded.Class` because of the removal of the `Foldable` superclass; `mesh` and `torus` were rewritten without `box`. * Add a comment explaining why the `Foldable` instance is problematic.
I am currently modeling electrical networks using graphs from the I don't understand why What is it about this definition of traverse that can lead to bugs? |
Actually I was bit by unexpected behavior relating to Consider this code : import Algebra.Graph.Labelled
graph :: Graph (Capacity Int) Char
graph = edges [(0, 'a', 'b'), (0, 'b', 'c'), (0, 'c', 'a')] If The previous instances for type NodeID = Int
data Network e a
= MkNetwork { networkTopology :: Graph (Capacity e) NodeID
, networkNodeMap :: Bimap NodeID a
} where Using a data structure like this -- separating the graph nodes from the connection topology -- allows you to write intuitive Hopefully this can help others |
@LaurentRDC Yes, both Foldable and Traversable make it possible to observe the inner structure of algebraic graph expressions in a way that violates the laws of the algebra. I don't know of a good solution at the moment. P.S. I'm curious about your application (modeling electrical networks). Where can I read more about it? |
This is not public work yet. Hopefully I'll be able to share details in the coming months. I'll try to remember to ping back then. |
We currently expose the implementation of the
Graph
datatype:This is problematic:
Users can observe different graph structures even though underlying graphs are equal according to the laws of the algebra, e.g.
Empty == Overlay Empty Empty
.Other representations may be more efficient (e.g. see Replacing Algebra.Graph by Maybe (Algebra.Graph.NonEmpty) #90) but we can't experiment with them without potentially breaking user code.
While doing this, we should probably dump awkward instance such as
Foldable
, since one can also use it to reveal internal structure, e.g.toList [1] == [1]
buttoList [1 + 1] == [1, 1]
. We have an equivalent of theFoldable
type class tailored for graphs, which we callToGraph
, which relies on the user to provide correct folding functions to avoid such issues.The text was updated successfully, but these errors were encountered: