Skip to content

Commit

Permalink
Add serialization support (#171)
Browse files Browse the repository at this point in the history
* Add serialization

* Add tests and fix a few bugs

* fmt

* Add documentation for using insta

* Try adding all snapshot files

Asked by @oflatt
https://egraphs.zulipchat.com/#narrow/stream/328972-general/topic/e-graph.20serialization.20format/near/376503331

* Remove snapshot tests for now
  • Loading branch information
saulshanabrook authored Jul 19, 2023
1 parent 7e56187 commit c65d3d6
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 42 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ name = "files"
[features]
default = ["bin"]

bin = ["dep:clap", "dep:env_logger"]
bin = ["dep:clap", "dep:env_logger", "egraph-serialize/serde", "dep:serde_json"]
wasm-bindgen = ["instant/wasm-bindgen"]

[dependencies]
Expand All @@ -33,6 +33,9 @@ num-traits = "0.2.15"
smallvec = "1.11"
symbolic_expressions = {git = "https://github.com/oflatt/symbolic-expressions", rev = "4c0ea5ca008f972450b2af72387e64d2c1c6a791"}

egraph-serialize = {git = "https://github.com/egraphs-good/egraph-serialize", rev = "54b1a4f1e2f2135846b084edcb495cd159839540", default-features = false}
serde_json ={optional=true, version = "1.0.100", features = ["preserve_order"]}

lalrpop-util = {version = "0.20", features = ["lexer"]}
regex = "1"

Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is repo for the `egglog` tool accompanying the paper
"Better Together: Unifying Datalog and Equality Saturation"
([ACM DL](https://dl.acm.org/doi/10.1145/3591239), [arXiv](https://arxiv.org/abs/2304.04332)).

If you use this work, please use [this citation](./CITATION.bib).
If you use this work, please use [this citation](./CITATION.bib).

See also the Python binding, which provides a bit more documentation:
https://egg-smol-python.readthedocs.io/en/latest/
Expand All @@ -30,7 +30,7 @@ make all
## Usage

```
cargo run [-f fact-path] [-naive] <files.egg>
cargo run [-f fact-path] [-naive] [--to-json] <files.egg>
```

or just
Expand Down Expand Up @@ -63,6 +63,10 @@ npm install -g @vscode/vsce

Run `vsce package` in the `vscode/egglog-1.0.0` folder to reconstruct the .vsix file and install it manually.

## Development

To run the tests use `make test`.

# Syntax

The syntax of the .egg files is defined in `src/ast/parse.lalrpop`.
Expand Down Expand Up @@ -138,7 +142,7 @@ If you define a `:merge` expression, you can update specific values in the funct
(set (KeepMax 1) 1)
(set (KeepMax 1) 2) ; we redefine 1 to be 2
(set (KeepMax 1) 0) ; this does not change since we use max
(extract (KeepMax 1)) ; this is 2
(extract (KeepMax 1)) ; this is 2
```

### `relation` command
Expand Down Expand Up @@ -240,7 +244,7 @@ Example:
(Add b a))
```

declares a rule that a `Add` variant is commutative.
declares a rule that a `Add` variant is commutative.

```
(birewrite (* (* a b) c) (* a (* b c)))
Expand Down Expand Up @@ -326,7 +330,7 @@ Union only works on variants, not sorts.
### Name

```
[ <Ident> ]
[ <Ident> ]
```

### Facts
Expand Down
2 changes: 1 addition & 1 deletion src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use smallvec::SmallVec;

mod binary_search;
pub mod index;
mod table;
pub(crate) mod table;

pub type ValueVec = SmallVec<[Value; 3]>;

Expand Down
10 changes: 5 additions & 5 deletions src/function/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) struct Table {
max_ts: u32,
n_stale: usize,
table: RawTable<TableOffset>,
vals: Vec<(Input, TupleOutput)>,
pub(crate) vals: Vec<(Input, TupleOutput)>,
}

/// Used for the RawTable probe sequence.
Expand Down Expand Up @@ -299,7 +299,7 @@ impl Table {
}
}

fn hash_values(vs: &[Value]) -> u64 {
pub(crate) fn hash_values(vs: &[Value]) -> u64 {
// Just hash the bits: all inputs to the same function should have matching
// column types.
let mut hasher = BH::default().build_hasher();
Expand All @@ -310,8 +310,8 @@ fn hash_values(vs: &[Value]) -> u64 {
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct Input {
data: ValueVec,
pub(crate) struct Input {
pub(crate) data: ValueVec,
/// The timestamp at which the given input became "stale"
stale_at: u32,
}
Expand All @@ -328,7 +328,7 @@ impl Input {
self.data.as_slice()
}

fn live(&self) -> bool {
pub(crate) fn live(&self) -> bool {
self.stale_at == u32::MAX
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod extract;
mod function;
mod gj;
mod proofs;
mod serialize;
pub mod sort;
mod typecheck;
mod typechecking;
Expand All @@ -13,6 +14,7 @@ mod value;
use hashbrown::hash_map::Entry;
use index::ColumnIndex;
use instant::{Duration, Instant};
pub use serialize::SerializeConfig;
use sort::*;
use thiserror::Error;

Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use egglog::EGraph;
use egglog::{EGraph, SerializeConfig};
use std::io::{self, BufRead, BufReader};
use std::path::PathBuf;

Expand All @@ -12,6 +12,8 @@ struct Args {
inputs: Vec<PathBuf>,
#[clap(long)]
proofs: bool,
#[clap(long)]
to_json: bool,
}

fn main() {
Expand Down Expand Up @@ -75,6 +77,12 @@ fn main() {
}
}

if args.to_json {
let json_path = input.with_extension("json");
let serialized = egraph.serialize(SerializeConfig::default());
serialized.to_json_file(json_path).unwrap();
}

// no need to drop the egraph if we are going to exit
if idx == args.inputs.len() - 1 {
std::mem::forget(egraph)
Expand Down
Loading

0 comments on commit c65d3d6

Please sign in to comment.