Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod ast;
pub mod parser;
pub mod typechecker;

pub use parser::parse;
5 changes: 5 additions & 0 deletions src/typechecker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod path_type;
pub mod variable_type;

pub use path_type::{Direction, PathType};
pub use variable_type::{EdgeKind, EdgeType, NodeType, Schema, VariableType};
202 changes: 202 additions & 0 deletions src/typechecker/path_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
use super::variable_type::{EdgeKind, EdgeType, Schema, VariableType};

/// Edge direction in a path.
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum Direction {
Right,
Left,
Undirected,
Any,
}

/// Path types representing sequences of nodes and edges.
#[derive(PartialEq, Clone, Debug)]
pub enum PathType {
/// A single node in the path.
Node(VariableType),
/// An edge connecting a path to a node: path - node.
Edge {
path: Box<PathType>,
node: VariableType,
},
/// Union of two path types.
Union(Box<PathType>, Box<PathType>),
/// Bottom type (empty/inconsistent path).
Zero,
}

impl Default for PathType {
fn default() -> Self {
PathType::Node(VariableType::node())
}
}

impl PathType {
/// Creates a node path type.
pub fn node(n: VariableType) -> Self {
PathType::Node(n)
}

/// Creates an edge path type.
pub fn edge(path: PathType, node: VariableType) -> Self {
PathType::Edge {
path: Box::new(path),
node,
}
}

/// Returns the length of the path (number of edges).
pub fn len(&self) -> usize {
match self {
PathType::Node(_) => 0,
PathType::Edge { path, .. } => path.len() + 1,
PathType::Union(p1, p2) => p1.len().min(p2.len()),
PathType::Zero => 0,
}
}

/// Returns true if the path has no edges (length 0).
pub fn is_empty(&self) -> bool {
match self {
PathType::Node(_) | PathType::Zero => true,
PathType::Edge { .. } => false,
PathType::Union(p1, p2) => p1.is_empty() || p2.is_empty(),
}
}

/// Computes the union of two path types.
pub fn union(p1: PathType, p2: PathType) -> PathType {
match (&p1, &p2) {
(PathType::Zero, _) => p2,
(_, PathType::Zero) => p1,
_ if p1 == p2 => p1,
_ => PathType::Union(Box::new(p1), Box::new(p2)),
}
}

/// Constructs a union from a list of path types.
pub fn union_from_list(paths: Vec<PathType>) -> PathType {
if paths.is_empty() {
return PathType::Zero;
}
paths
.into_iter()
.reduce(PathType::union)
.unwrap_or(PathType::Zero)
}

/// Converts a VariableType to a PathType given a direction.
pub fn to_path_type(t: &VariableType, direction: Direction) -> PathType {
match t {
VariableType::Node(_) => PathType::Node(t.clone()),
VariableType::Edge(edge) => match edge.kind {
EdgeKind::Directed => match direction {
Direction::Right => PathType::Edge {
path: Box::new(PathType::Node(edge.left.clone().into())),
node: edge.right.clone().into(),
},
Direction::Left => {
let flipped = VariableType::Edge(EdgeType::directed(
edge.descriptor.clone(),
edge.right.clone(),
edge.left.clone(),
));
PathType::to_path_type(&flipped, Direction::Right)
}
Direction::Any => PathType::union(
PathType::to_path_type(t, Direction::Right),
PathType::to_path_type(t, Direction::Left),
),
Direction::Undirected => PathType::to_path_type(t, Direction::Any),
},
EdgeKind::Undirected => {
let as_directed = VariableType::Edge(EdgeType::directed(
edge.descriptor.clone(),
edge.left.clone(),
edge.right.clone(),
));
PathType::to_path_type(&as_directed, Direction::Any)
}
},
VariableType::Union(t1, t2) => PathType::union(
PathType::to_path_type(t1, direction),
PathType::to_path_type(t2, direction),
),
VariableType::Zero => PathType::Zero,
VariableType::List(_) => PathType::Zero,
}
}

/// Computes the meet (greatest lower bound) of two path types.
pub fn meet(schema: &Schema, p1: &PathType, p2: &PathType) -> PathType {
match (p1, p2) {
(PathType::Zero, _) | (_, PathType::Zero) => PathType::Zero,

(PathType::Node(n1), PathType::Node(n2)) => match VariableType::meet(n1, n2) {
Ok(met) => PathType::Node(VariableType::refine(schema, &met)),
Err(_) => PathType::Zero,
},

(
PathType::Edge {
path: p1_path,
node: p1_node,
},
PathType::Node(n2),
) => match VariableType::meet(p1_node, n2) {
Ok(met) => PathType::Edge {
path: p1_path.clone(),
node: VariableType::refine(schema, &met),
},
Err(_) => PathType::Zero,
},

(
_,
PathType::Edge {
path: p2_path,
node: p2_node,
},
) => PathType::Edge {
path: Box::new(PathType::meet(schema, p1, p2_path)),
node: p2_node.clone(),
},

(_, PathType::Union(u1, u2)) => {
let m1 = PathType::meet(schema, p1, u1);
let m2 = PathType::meet(schema, p1, u2);
if m1.is_unsatisfiable() {
return m2;
}
if m2.is_unsatisfiable() {
return m1;
}
PathType::union(m1, m2)
}

(PathType::Union(u1, u2), _) => {
let m1 = PathType::meet(schema, u1, p2);
let m2 = PathType::meet(schema, u2, p2);
if m1.is_unsatisfiable() {
return m2;
}
if m2.is_unsatisfiable() {
return m1;
}
PathType::union(m1, m2)
}
}
}

/// Determines if a path type is unsatisfiable (inconsistent/bottom).
pub fn is_unsatisfiable(&self) -> bool {
match self {
PathType::Zero => true,
PathType::Node(n) => VariableType::is_empty(n),
PathType::Edge { path, node } => {
path.is_unsatisfiable() || VariableType::is_empty(node)
}
PathType::Union(p1, p2) => p1.is_unsatisfiable() && p2.is_unsatisfiable(),
}
}
}
Loading
Loading