use super::support::*;
#[test]
fn safe_skolem_head_passes_validation() {
let head = Atom {
predicate: PredicateSymbol::new("hasDerivedKey"),
terms: vec![
var("x"),
Term::Skolem {
name: ConstantSymbol::new("example-key"),
args: vec![var("a"), var("b")],
},
],
};
let program = Program {
rules: vec![Rule::positive(
head,
vec![atom("pair", vec![var("x"), var("a"), var("b")])],
)],
};
assert!(program.validate().is_ok());
}
#[test]
fn unsafe_skolem_head_unbound_arg_fails_validation() {
let head = Atom {
predicate: PredicateSymbol::new("h"),
terms: vec![
var("x"),
Term::Skolem {
name: ConstantSymbol::new("example-key"),
args: vec![var("x"), var("orphan")],
},
],
};
let program = Program {
rules: vec![Rule::positive(head, vec![atom("anchor", vec![var("x")])])],
};
let err = program
.validate()
.expect_err("orphan SKOLEM arg should reject");
let ProgramValidationError::UnsafeRule { variables, .. } = err else {
panic!("expected UnsafeRule, got {err:?}");
};
assert!(
variables.contains(&VariableSymbol::new("orphan")),
"expected `orphan` in offending list, got {variables:?}"
);
}
#[test]
fn skolem_in_positive_body_atom_is_rejected() {
let body_atom = Atom {
predicate: PredicateSymbol::new("p"),
terms: vec![
var("x"),
Term::Skolem {
name: ConstantSymbol::new("example-key"),
args: vec![var("x")],
},
],
};
let program = Program {
rules: vec![Rule::positive(atom("h", vec![var("x")]), vec![body_atom])],
};
let err = program
.validate()
.expect_err("SKOLEM in body should reject");
assert_eq!(
err,
ProgramValidationError::SkolemInBodyAtom {
rule: rule0(),
literal: lit(0),
term: slot(1),
}
);
}
#[test]
fn skolem_in_negative_body_atom_is_rejected() {
let negated = Atom {
predicate: PredicateSymbol::new("p"),
terms: vec![
var("x"),
Term::Skolem {
name: ConstantSymbol::new("example-key"),
args: vec![var("x")],
},
],
};
let program = Program {
rules: vec![Rule::new(
atom("h", vec![var("x")]),
vec![
Literal::Positive(atom("anchor", vec![var("x")])),
Literal::Negative(negated),
],
)],
};
let err = program
.validate()
.expect_err("SKOLEM in negative body should reject");
assert_eq!(
err,
ProgramValidationError::SkolemInBodyAtom {
rule: rule0(),
literal: lit(1),
term: slot(1),
}
);
}
#[test]
fn skolem_in_aggregate_inner_body_is_rejected() {
let inner = Atom {
predicate: PredicateSymbol::new("salary"),
terms: vec![
var("e"),
var("dept"),
Term::Skolem {
name: ConstantSymbol::new("example-key"),
args: vec![var("e")],
},
],
};
let program = Program {
rules: vec![Rule::new(
atom("total", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(AggregateBody {
result_var: VariableSymbol::new("total"),
aggregator: Aggregator::Sum,
input_var: VariableSymbol::new("dept"),
group_by: vec![VariableSymbol::new("dept")],
inner_body: vec![Literal::Positive(inner)],
})],
)],
};
let err = program
.validate()
.expect_err("SKOLEM in aggregate inner should reject");
assert_eq!(
err,
ProgramValidationError::SkolemInBodyAtom {
rule: rule0(),
literal: lit(0).inner(LiteralIndex::new(0)),
term: slot(2),
}
);
}