-
Notifications
You must be signed in to change notification settings - Fork 9
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
Proposal: Make the Yatima.Compiler.Graph a generic library #88
Comments
We'd love a PR! Thanks for the proposal and contribution. The scc code definitely should be generalized. Perhaps we can even take this opportunity to factor out a new generic Graph.lean library, that can eventually scale to something along the lines of fgl, graphs in Haskell or petgraph in Rust? I've created https://github.com/yatima-inc/Graph.lean as a template where we can do this in. @arthurpaulino and @winston-h-zhang, can you guys follow up with how to factor our code out and how to incorporate @NicolasRouquette's changes? |
These are great examples. For my internal Scala code base, I've been using this library for several years: http://scala-graph.org/ The petgraph library seems well-reviewed; however, it lacks the monadic style of the Haskell libs. |
Thanks for the contribution! We should definitely try and factor out any generic graph algorithms. Since John has already set up the
Ironically, purely lazy functional code is often not great for Lean, since Lean is strict and can do functional-but-in-place "pseudo-mutations." (For example, many pure functional graph algorithms in Haskell don't quite work because they take advantage of its laziness.) So that's why I'm hesitant on I also don't know much about Scala -- how different is the API style? One final thought is that I'd love to hear any thoughts about this! |
Thanks for the feedback. It seems that all of these options have some language-specific and API conventions that may not be a good fit for Lean. The scala-graph library API design is deliberately focused on blending w/ the Scala collection API, which is really nice. I see some similarities here and there with some of the proposed Yatima Std library operations. I understand that all of the above options involve biases due to the semantics of the languages involved (Haskell, Rust, Scala) and the API conventions they follow; we can get inspiration from these. If anything deserves to be called "monadic", the Yatima Graph DFS and strongly-connected component algorithms deserve that label:
and:
Both algorithms are implemented as reader monads over the structures mentioned above separately from the definition of the A good start might be to look at
with something similar to https://github.com/ekmett/graphs/blob/master/src/Data/Graph/Class.hs#L43:L50
I really like the https://github.com/ekmett/graphs/blob/master/src/Data/Graph/Algorithm.hs#L30:L37
The key difference is that Yatima's DFS and SCC algorithms are not currently written to a common interface like |
FYI: I also found that there is an SCC algorithm implemented in Lean: Interestingly, there is no graph data structure per se except for the information about vertices and their successors; e.g:
|
The monadic formulation of the strongly-connected component algorithm caught my attention; it's really elegant and concise. I hope that the https://github.com/leanprover/functional_programming_in_lean book will eventually explain the nifty Lean4 programming tricks involved!
I would like to use this library to formulate in Lean4 some algorithms I've been working on in Scala3; see: https://github.com/NicolasRouquette/scala3-bundle-closure
However, the current Graph library is tied to Lean4's symbols as graph vertices:
https://github.com/yatima-inc/yatima-lang/blob/ada78d298e08e495b307cb639f4305731f3af663/Yatima/Compiler/Graph.lean#L32-L34
So as a fun exercise to flex my new Lean4 programming brain, I extracted your library and parameterized it:
https://github.com/NicolasRouquette/YatimaGraphLib
To facilitate comparison w/ your original source, I've kept the same file/directory organization & names.
In writing a simple test, I noticed that the
buildG
function should add the edge target as a vertex in the map.See:
https://github.com/NicolasRouquette/YatimaGraphLib/blob/d74377ead019a05a43b11b0fff535abe3b3466dd/src/lean4/Yatima/Compiler/Graph.lean#L28:L36
If this idea makes sense, are you interested in a PR?
The text was updated successfully, but these errors were encountered: