Skip to content

Commit

Permalink
Bumping version
Browse files Browse the repository at this point in the history
  • Loading branch information
Liby99 committed Apr 18, 2023
1 parent 3be66f8 commit 3724ac6
Show file tree
Hide file tree
Showing 54 changed files with 1,092 additions and 268 deletions.
34 changes: 14 additions & 20 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
name: Rust
name: Cargo Build & Test

on:
push:
branches: [ "master" ]
branches: [ master ]
pull_request:
branches: [ "master" ]
branches: [ master ]

env:

env:
CARGO_TERM_COLOR: always

jobs:
build:

build_and_test:
name: Rust project - latest
runs-on: ubuntu-latest

strategy:
matrix:
toolchain:
- nightly
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ steps.component.outputs.toolchain }}
override: true
- id: component
uses: actions-rs/components-nightly@v1
with:
component: clippy
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo test --verbose --release
8 changes: 8 additions & 0 deletions core/src/common/expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter::FromIterator;

use super::binary_op::BinaryOp;
use super::tuple_access::TupleAccessor;
use super::unary_op::UnaryOp;
Expand Down Expand Up @@ -232,6 +234,12 @@ where
}
}

impl FromIterator<Expr> for Expr {
fn from_iter<I: IntoIterator<Item = Expr>>(iter: I) -> Self {
Expr::Tuple(iter.into_iter().collect())
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BinaryExpr {
pub op: BinaryOp,
Expand Down
4 changes: 2 additions & 2 deletions core/src/common/foreign_predicates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ use super::value_type::*;

mod float_eq;
mod range;
mod string_chars;
mod soft_cmp;
mod soft_eq;
mod soft_gt;
mod soft_lt;
mod soft_neq;
mod string_chars;

pub use float_eq::*;
pub use range::*;
pub use string_chars::*;
pub use soft_cmp::*;
pub use soft_eq::*;
pub use soft_gt::*;
pub use soft_lt::*;
pub use soft_neq::*;
pub use string_chars::*;
112 changes: 101 additions & 11 deletions core/src/compiler/back/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct Rule {

impl Rule {
pub fn head_predicate(&self) -> &String {
&self.head.predicate
&self.head.predicate()
}

pub fn body_literals(&self) -> impl Iterator<Item = &Literal> {
Expand All @@ -105,21 +105,95 @@ impl Rule {
}

#[derive(Clone, Debug, PartialEq)]
pub struct Head {
pub predicate: String,
pub args: Vec<Term>,
pub enum Head {
/// A simple atom as the head
Atom(Atom),

/// A disjunction of atoms as the head; all atoms should have the same predicate
Disjunction(Vec<Atom>),
}

impl Head {
pub fn new(predicate: String, args: Vec<Term>) -> Self {
Self { predicate, args }
pub fn atom(predicate: String, args: Vec<Term>) -> Self {
Self::Atom(Atom::new(predicate, args))
}

pub fn variable_args(&self) -> impl Iterator<Item = &Variable> {
self.args.iter().filter_map(|a| match a {
Term::Variable(v) => Some(v),
pub fn predicate(&self) -> &String {
match self {
Self::Atom(a) => &a.predicate,
Self::Disjunction(disj) => &disj[0].predicate,
}
}

pub fn get_atom(&self) -> Option<&Atom> {
match self {
Self::Atom(a) => Some(a),
_ => None,
})
}
}

pub fn variable_args(&self) -> Vec<&Variable> {
match self {
Self::Atom(a) => a.variable_args().collect(),
Self::Disjunction(disj) => disj.iter().flat_map(|a| a.variable_args()).collect(),
}
}

/// Substitute the atom's arguments with the given term rewrite function
pub fn substitute<F: Fn(&Term) -> Term + Copy>(&self, f: F) -> Self {
match self {
Self::Atom(a) => Self::Atom(a.substitute(f)),
Self::Disjunction(d) => Self::Disjunction(d.iter().map(|a| a.substitute(f)).collect()),
}
}

/// Get the variable patterns of the head
///
/// Atomic head has only one pattern;
/// Disjunctive head could have multiple patterns
pub fn has_multiple_patterns(&self) -> bool {
match self {
Self::Atom(_) => {
// Atomic head has only one pattern
false
},
Self::Disjunction(disj) => {
// Extract the pattern of the first atom in the disjunction
let first_pattern = disj[0]
.args
.iter()
.map(|t| match t {
Term::Variable(v) => v.name.clone(),
Term::Constant(_) => String::new(),
})
.collect::<Vec<_>>();

// Check if the first pattern is satisfied by all other atoms
for a in disj.iter().skip(1) {
let satisfies_pattern = a.args
.iter()
.enumerate()
.all(|(i, t)| {
if let Some(p) = first_pattern.get(i) {
match t {
Term::Variable(v) => p == &v.name,
Term::Constant(_) => p.is_empty(),
}
} else {
false
}
});

// If not satisfied, then the head has multiple patterns
if !satisfies_pattern {
return true;
}
}

// If all atoms satisfy the first pattern, then the head has only one pattern
false
},
}
}
}

Expand All @@ -130,7 +204,7 @@ pub struct Conjunction {
}

/// A term is the argument of a literal
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Term {
Variable(Variable),
Constant(Constant),
Expand Down Expand Up @@ -309,6 +383,14 @@ impl Atom {
self.args.iter().any(|a| a.is_constant())
}

/// Get the constant arguments
pub fn constant_args(&self) -> impl Iterator<Item = &Constant> {
self.args.iter().filter_map(|a| match a {
Term::Constant(c) => Some(c),
_ => None,
})
}

/// Create a partition of the atom's arguments into constant and variable
pub fn const_var_partition(&self) -> (Vec<(usize, &Constant)>, Vec<(usize, &Variable)>) {
let (constants, variables): (Vec<_>, Vec<_>) = self.args.iter().enumerate().partition(|(_, t)| t.is_constant());
Expand All @@ -328,6 +410,14 @@ impl Atom {
.collect();
(constants, variables)
}

/// Substitute the atom's arguments with the given term rewrite function
pub fn substitute<F: Fn(&Term) -> Term>(&self, f: F) -> Self {
Self {
predicate: self.predicate.clone(),
args: self.args.iter().map(|a| f(a)).collect(),
}
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down
Loading

0 comments on commit 3724ac6

Please sign in to comment.