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

support wildcard terms #409

Merged
merged 4 commits into from
Nov 9, 2023
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
15 changes: 15 additions & 0 deletions nemo/src/io/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ pub struct RuleParser<'a> {
sources: RefCell<Vec<DataSourceDeclaration>>,
/// Declarations of predicates with their types.
predicate_declarations: RefCell<HashMap<Identifier, Vec<PrimitiveType>>>,
/// Number counting up for generating distinct wildcards.
wildcard_generator: RefCell<usize>,
}

impl<'a> RuleParser<'a> {
Expand Down Expand Up @@ -1003,6 +1005,7 @@ impl<'a> RuleParser<'a> {
self.parse_parenthesised_term(),
self.parse_function_term(),
self.parse_aggregate(),
self.parse_wildcard(),
)),
multispace_or_comment0,
)(input)
Expand All @@ -1012,6 +1015,18 @@ impl<'a> RuleParser<'a> {
)
}

/// Parse a wildcard variable.
pub fn parse_wildcard(&'a self) -> impl FnMut(Span<'a>) -> IntermediateResult<Term> {
traced(
"parse_wildcard",
map_res(space_delimited_token("_"), |_| {
let wildcard = Variable::create_wildcard(*self.wildcard_generator.borrow());
*self.wildcard_generator.borrow_mut() += 1;
Ok::<_, ParseError>(Term::Primitive(PrimitiveTerm::Variable(wildcard)))
}),
)
}

/// Parse a parenthesised term tree.
pub fn parse_parenthesised_term(&'a self) -> impl FnMut(Span<'a>) -> IntermediateResult<Term> {
traced(
Expand Down
3 changes: 3 additions & 0 deletions nemo/src/io/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ pub enum ParseError {
/// An existentially quantified variable occurs in the body of a rule.
#[error(r#"Variable "{0}" occurs existentially quantified in the rule body."#)]
BodyExistential(String),
/// A wildcard pattern was used inside of the rule head.
#[error(r#"The head of a rule must not contain any wildcard patterns."#)]
WildcardInHead,
/// The universal variable does not occur in a positive body literal.
#[error(r#"The universal variable "{0}" does not occur in a positive body literal."#)]
UnsafeHeadVariable(String),
Expand Down
4 changes: 4 additions & 0 deletions nemo/src/model/rule_model/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ impl Rule {

// Head atoms may only use variables that are either bound or derived
for variable in head.iter().flat_map(|a| a.variables()) {
if variable.is_wildcard() {
return Err(ParseError::WildcardInHead);
}

if variable.is_universal()
&& !safe_variables.contains(variable)
&& !derived_variables.contains(variable)
Expand Down
15 changes: 15 additions & 0 deletions nemo/src/model/rule_model/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum Variable {
}

impl Variable {
const WILDCARD_PREFIX: &'static str = "__WILDCARD";

/// Return the name of the variable.
pub fn name(&self) -> String {
match self {
Expand All @@ -43,6 +45,19 @@ impl Variable {
pub fn is_existential(&self) -> bool {
matches!(self, Variable::Existential(_))
}

/// Return whether this variable was generated by a wildcard pattern.
pub fn is_wildcard(&self) -> bool {
match self {
Variable::Universal(ident) => ident.0.starts_with(Self::WILDCARD_PREFIX),
Variable::Existential(_) => false,
}
}

/// Make a wildcard variable with the given unique index.
pub fn create_wildcard(index: usize) -> Variable {
Variable::Universal(format!("{}{}", Self::WILDCARD_PREFIX, index).into())
}
}

impl Display for Variable {
Expand Down
5 changes: 5 additions & 0 deletions resources/testcases/regression/wildcards/regression.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
issue: 408
title: writing wildcard instead of a variable name
tracker: https://github.com/knowsys/nemo/issues/408
type: feature

3 changes: 3 additions & 0 deletions resources/testcases/regression/wildcards/run.rls
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@source input[3]: load-csv("sources/main.csv").

result(?x) :- input(?x, _, _).
4 changes: 4 additions & 0 deletions resources/testcases/regression/wildcards/run/result.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2
4
6
8
13 changes: 13 additions & 0 deletions resources/testcases/regression/wildcards/sources/main.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
2,0,0
2,0,1
2,1,0
2,5,2
2,5,3
4,0,0
4,2,1
4,5,1
6,2,0
6,9,2
8,1,2
8,4,2
8,5,1