-
Notifications
You must be signed in to change notification settings - Fork 67
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
Type class vs data type Graph #5
Comments
Oh, I just realised that putting it into So, maybe |
I prototyped a potential design here: https://gist.github.com/ndmitchell/800fb7bf65f5875a22e72b1b868e1442 |
@ndmitchell Thanks! I've just played with your code a little and realised that For undirected, transitive, etc. graphs, the result is not immediately unique and one would have to come up with some canonical way to implement And for instances like What about adding a class As a more vague comment: I find |
Another argument for turning Note: making As a side note, we still don't have any cool algorithms on the |
I didn't intend to imply that Can you give an example of a type that can implement Most of the graph algorithms you have written produce a polymorphic |
I think they should return the same result -- otherwise, they will leak implementation details. For example, at the moment we implement the I therefore think that the following should be a law of
Consider the following classic instance: newtype Size a = Size { getSize :: Int }
instance Graph (Size a) where
type Vertex (Size a) = a
empty = Size 0
vertex _ = Size 1
overlay x y = Size $ getSize x + getSize y
connect x y = Size $ getSize x + getSize y It's not inconceivable that we define something like this to calculate the size of graph expressions. You will find it tricky to implement |
The data type The type class |
At the moment we have type class
Graph
and data type calledBasic
that is its direct deep embedding (I also sometimes refer to it asExpr
). However, it would be really nice to call the data typeGraph
too, to add it toTree
,Forest
and the rest of the family of nice algebraic data types.So, I propose the following:
The data type
Graph
lives inAlgebra.Graph.Data
:Of course we need to convince GHC to add it toSee Type class vs data type Graph #5 (comment).containers
, but it's really very lightweight.The type class
Graph
lives inAlgebra.Graph
. It also defines theinstance Graph Data.Graph
.Presumably, if I'm implementing cool algorithms on the data type
Graph
, I don't care aboutAlgebra.Graph
and don't need to include it.Alternatively, if I'm coding polymorphic functions, I don't need
Algebra.Graph.Data
and don't include it.So, even though having the same name for the type class and data type sounds potentially confusing, they would probably rarely get mixed in the same file, and so can happily live in separate modules.
Having them in different modules also allows you to always be specific through qualified imports, like
Data.Graph
andClass.Graph
, which seems nicer than inventing names likeIGraph
orGraphLike
or what not for the type class.The text was updated successfully, but these errors were encountered: