Skip to content

Commit

Permalink
feat: Draft for hugr-model with export, import, parsing and pretty …
Browse files Browse the repository at this point in the history
…printing (#1542)

This PR defines a draft of the model format, and the following parts
that operate on the format:

- **Import**: A module in the model format can be imported as a core
hugr module.
 - **Export**: A core hugr module can be exported into the model format.
- **Parsing**: A model hugr can be parsed from an s-expression based
text format.
- **Pretty printing:**: A model hugr can be printed into the
s-expression format, with readable indentation.

This PR is very big, and yet it does not present a completed feature.
The model and core format differ in several aspects. Where they differ,
this is mostly intentional: The model is kept flexible enough to
incorporate features that we know that we want in the future but are
currently hard or impossible to express in the core. Import and export
perform conversions where possible, but do not yet cover everything.

To prevent this PR from becoming even bigger and to allow for core and
model to converge incrementally from both directions, I suggest the
following plan: We merge this PR into main with the model related code
being feature gated and considered experimental. We then perform tweaks
to model and core in smaller PRs where necessary until they are
sufficiently close.

---------

Co-authored-by: Agustín Borgna <[email protected]>
Co-authored-by: Seyon Sivarajah <[email protected]>
  • Loading branch information
3 people authored Oct 7, 2024
1 parent fc3efe5 commit a2e92e8
Show file tree
Hide file tree
Showing 35 changed files with 4,510 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/change-filters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ rust: &rust
- "hugr-cli/**"
- "hugr-core/**"
- "hugr-passes/**"
- "hugr-model/**"
- "Cargo.toml"
- "specification/schema/**"
- ".github/workflows/ci-rs.yml"
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ lto = "thin"

[workspace]
resolver = "2"
members = ["hugr", "hugr-core", "hugr-passes", "hugr-cli"]
members = ["hugr", "hugr-core", "hugr-passes", "hugr-cli", "hugr-model"]

[workspace.package]
rust-version = "1.75"
Expand Down Expand Up @@ -62,6 +62,9 @@ clap-verbosity-flag = "2.2.0"
assert_cmd = "2.0.14"
assert_fs = "1.1.1"
predicates = "3.1.0"
indexmap = "2.3.0"
fxhash = "0.2.1"
bumpalo = { version = "3.16.0" }

[profile.dev.package]
insta.opt-level = 3
Expand Down
1 change: 1 addition & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ in
# cargo-llvm-cov is currently marked broken on nixpkgs unstable
pkgs-stable.cargo-llvm-cov
pkgs.graphviz
pkgs.cargo-insta
] ++ lib.optionals
pkgs.stdenv.isDarwin
(with pkgs.darwin.apple_sdk; [
Expand Down
12 changes: 11 additions & 1 deletion hugr-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ workspace = true
[features]
extension_inference = []
declarative = ["serde_yaml"]
model_unstable = ["hugr-model"]

[[test]]
name = "model"
required-features = ["model_unstable"]

[dependencies]
portgraph = { workspace = true, features = ["serde", "petgraph"] }
Expand All @@ -32,7 +37,7 @@ serde = { workspace = true, features = ["derive", "rc"] }
serde_yaml = { workspace = true, optional = true }
typetag = { workspace = true }
smol_str = { workspace = true, features = ["serde"] }
derive_more = { workspace = true, features=["display", "from"]}
derive_more = { workspace = true, features = ["display", "from"] }
itertools = { workspace = true }
html-escape = { workspace = true }
bitvec = { workspace = true, features = ["serde"] }
Expand All @@ -46,6 +51,10 @@ paste = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
semver = { version = "1.0.23", features = ["serde"] }
hugr-model = { path = "../hugr-model", optional = true }
indexmap.workspace = true
fxhash.workspace = true
bumpalo = { workspace = true, features = ["collections"] }

[dev-dependencies]
rstest = { workspace = true }
Expand All @@ -61,3 +70,4 @@ regex-syntax = { workspace = true }

# Required for documentation examples
hugr = { path = "../hugr" }
serde_yaml = "0.9.34"
3 changes: 3 additions & 0 deletions hugr-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Please read the [API documentation here][].
Not enabled by default.
- `declarative`:
Experimental support for declaring extensions in YAML files, support is limited.
- `model_unstable`
Import and export from the representation defined in the `hugr-model` crate.
Unstable and subject to change. Not enabled by default.

## Recent Changes

Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub(crate) mod test {
dataflow_builder.finish_with_outputs(w)
}

pub(super) fn build_main(
pub(crate) fn build_main(
signature: PolyFuncType,
f: impl FnOnce(FunctionBuilder<&mut Hugr>) -> Result<BuildHandle<FuncID<true>>, BuildError>,
) -> Result<Hugr, BuildError> {
Expand Down
Loading

0 comments on commit a2e92e8

Please sign in to comment.