Skip to content

Commit 4989ded

Browse files
committed
rework congruence_closure to use impl Trait and petgraph
1 parent 53828bd commit 4989ded

File tree

10 files changed

+19
-892
lines changed

10 files changed

+19
-892
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ homepage = "https://github.com/nikomatsakis/ena"
66
repository = "https://github.com/nikomatsakis/ena"
77
version = "0.5.0"
88
authors = ["Niko Matsakis <[email protected]>"]
9+
10+
[dependencies]
11+
log = "0.3"
12+
petgraph = "0.4.5"

src/cc/mod.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//! paper "Fast Decision Procedures Based on Congruence Closure" by Nelson
33
//! and Oppen, JACM 1980.
44
5-
use graph::{self, Graph, NodeIndex};
5+
use petgraph::Direction;
6+
use petgraph::graph::{Graph, NodeIndex};
67
use std::collections::HashMap;
78
use std::fmt::Debug;
89
use std::hash::Hash;
9-
use std::iter;
1010
use unify::{UnifyKey, UnifyValue, InfallibleUnifyValue, UnificationTable, UnionedKeys};
1111

1212
#[cfg(test)]
@@ -54,11 +54,11 @@ impl Token {
5454
}
5555

5656
fn from_node(node: NodeIndex) -> Token {
57-
Token { index: node.0 as u32 }
57+
Token { index: node.index() as u32 }
5858
}
5959

6060
fn node(&self) -> NodeIndex {
61-
NodeIndex(self.index as usize)
61+
NodeIndex::new(self.index as usize)
6262
}
6363
}
6464

@@ -134,7 +134,7 @@ impl<K: Key> CongruenceClosure<K> {
134134

135135
/// Return the key for a given token
136136
pub fn key(&self, token: Token) -> &K {
137-
self.graph.node_data(token.node())
137+
&self.graph[token.node()]
138138
}
139139

140140
/// Indicates they `key1` and `key2` are equivalent.
@@ -281,7 +281,7 @@ impl<'cc, K: Key> Iterator for MergedKeys<'cc, K> {
281281
fn next(&mut self) -> Option<Self::Item> {
282282
self.iterator
283283
.next()
284-
.map(|token| self.graph.node_data(token.node()).clone())
284+
.map(|token| self.graph[token.node()].clone())
285285
}
286286
}
287287

@@ -316,7 +316,7 @@ impl<'a, K: Key> Algorithm<'a, K> {
316316
let graph = self.graph;
317317
self.table
318318
.unioned_keys(u)
319-
.flat_map(|k| graph.predecessor_nodes(k.node()))
319+
.flat_map(|k| graph.neighbors_directed(k.node(), Direction::Incoming))
320320
.map(|i| Token::from_node(i))
321321
.collect()
322322
}
@@ -352,7 +352,7 @@ impl<'a, K: Key> Algorithm<'a, K> {
352352
}
353353

354354
fn key(&self, u: Token) -> &'a K {
355-
self.graph.node_data(u.node())
355+
&self.graph[u.node()]
356356
}
357357

358358
// Compare the local data, not considering successor nodes. So e.g
@@ -364,7 +364,7 @@ impl<'a, K: Key> Algorithm<'a, K> {
364364
}
365365

366366
fn token_kind(&self, u: Token) -> KeyKind {
367-
self.graph.node_data(u.node()).key_kind()
367+
self.graph[u.node()].key_kind()
368368
}
369369

370370
fn unioned(&mut self, u: Token, v: Token) -> bool {
@@ -409,28 +409,9 @@ impl<'a, K: Key> Algorithm<'a, K> {
409409
}
410410
}
411411

412-
fn successors(&self, token: Token) -> iter::Map<graph::AdjacentTargets<'a, K, ()>,
413-
fn(NodeIndex) -> Token> {
412+
fn successors(&self, token: Token) -> impl Iterator<Item = Token> + 'a {
414413
self.graph
415-
.successor_nodes(token.node())
414+
.neighbors_directed(token.node(), Direction::Outgoing)
416415
.map(Token::from_node)
417416
}
418-
419-
fn predecessors(&self, token: Token) -> iter::Map<graph::AdjacentSources<'a, K, ()>,
420-
fn(NodeIndex) -> Token> {
421-
self.graph
422-
.predecessor_nodes(token.node())
423-
.map(Token::from_node)
424-
}
425-
426-
/// If `token` has been unioned with something generative, returns
427-
/// `Ok(u)` where `u` is the generative token. Otherwise, returns
428-
/// `Err(v)` where `v` is the root of `token`.
429-
fn normalize_to_generative(&mut self, token: Token) -> Result<Token, Token> {
430-
let token = self.table.find(token);
431-
match self.token_kind(token) {
432-
Generative => Ok(token),
433-
Applicative => Err(token),
434-
}
435-
}
436417
}

src/cc/test.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ fn integer() -> Type {
7070
Box::new(Nominal("integer", vec![]))
7171
}
7272

73-
fn character() -> Type {
74-
Box::new(Nominal("char", vec![]))
75-
}
76-
7773
fn vec(t: Type) -> Type {
7874
Box::new(Nominal("Vec", vec![t]))
7975
}

src/constraint/mod.rs

Lines changed: 0 additions & 160 deletions
This file was deleted.

src/constraint/test.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/debug.rs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)