Skip to content
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

Add custom serde (de)serialization for compact hugr #1

Merged
merged 4 commits into from
Apr 13, 2023

Conversation

ss2165
Copy link
Member

@ss2165 ss2165 commented Apr 5, 2023

Serialization implemented via an intermediate struct which maps (graph, hierarchy) to (nodes, edges) as this is a more natural encoding, and more useful for front-ends.

A key limitation of this is that it requires nodes to be compacted beforehand, so serialisation would invalidate node indices. In most use cases though, this should be sufficient.

If more direct serialisation of the datastructures is desirable in future (e.g. for storing intermediate data to disk), we can return to using serde derive directives directly, and expose the intermediate SerHugr structure (requiring explicit conversion in and out of this for "ergonomic" serialised format).

If that is not required, we can instead implement Serialize and Deserialize directly without going via the intermediate.

The current state is left as a reasonable performance compromise which leaves both future options open.

@ss2165 ss2165 requested a review from aborgna-q April 5, 2023 10:04
@ss2165 ss2165 changed the base branch from main to feature/new-module April 5, 2023 13:03
@ss2165 ss2165 force-pushed the feature/new-module branch from 69e90d7 to 7ced3f2 Compare April 5, 2023 13:04
@ss2165 ss2165 force-pushed the feature/serialize branch from c68d034 to 6088e3a Compare April 5, 2023 13:07
@aborgna-q aborgna-q added the enhancement New feature or request label Apr 11, 2023
@ss2165 ss2165 force-pushed the feature/new-module branch from 7ced3f2 to 53b1a23 Compare April 11, 2023 15:43
@ss2165 ss2165 force-pushed the feature/serialize branch from 6088e3a to 4a00e51 Compare April 12, 2023 09:44
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

src/hugr/serialize.rs Show resolved Hide resolved
src/hugr/serialize.rs Outdated Show resolved Hide resolved
@ss2165 ss2165 force-pushed the feature/new-module branch from e9eeafb to 0508cc0 Compare April 13, 2023 09:45
Base automatically changed from feature/new-module to main April 13, 2023 09:49
@ss2165 ss2165 force-pushed the feature/serialize branch from 4597cf4 to 4551ca0 Compare April 13, 2023 09:51
@ss2165 ss2165 requested a review from aborgna-q April 13, 2023 09:51
@ss2165 ss2165 force-pushed the feature/serialize branch from b52693d to 23bae83 Compare April 13, 2023 10:05
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ss2165 ss2165 merged commit 5729fa3 into main Apr 13, 2023
@ss2165 ss2165 deleted the feature/serialize branch April 13, 2023 12:32
github-merge-queue bot pushed a commit that referenced this pull request Dec 17, 2024
Updates the `Display` implementation of poly functions, functions,
types, type arguments, and type params so its easier to read.

Comparison below. I added the newlines manually.

Before:
```swift
forall Any _0.map(|i|i.to_string()).unwrap_or("-".to_string()).
    [array([
        Variable { v: TypeArgVariable { idx: 1, cached_decl: BoundedNat { bound: UpperBound(None) } } },
        Type { ty: TypeBase(Sum(General { rows: [TypeRowBase { types: [] }, TypeRowBase { types: [TypeBase(Variable(0, Any), Any)] }] }), Any) }
    ])]
 -> [[]][[
    array([
            Variable { v: TypeArgVariable { idx: 1, cached_decl: BoundedNat { bound: UpperBound(None) } } },
            Type { ty: TypeBase(Sum(General { rows: [TypeRowBase { types: [] }, TypeRowBase { types: [TypeBase(Variable(0, Any), Any)] }] }), Any) }
        ]),
        int([BoundedNat { n: 6 }])
    ]]
```
(yes, that is some hardcoded rust source)

After:
```swift
∀ (var#0 : Type) (var#1 : Nat). [array(var#1, Type([]+[var#0]))]
                             -> [[array(var#1, Type([]+[var#0])), int(6)]]
```

That is, a generic function on a type var and an unbounded natural that
takes an array of (nat, option type) and returns a tuple with the array
and an int.

It may be better if the tuple display used `()` instead of `[]`, but
that can be discussed later.

Update after review suggestions:
```swift
∀ (#0 : Type) (#1 : Nat). [array(#1, []+[#0])] -> [[array(#1, []+[#0]), int(6)]]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants