Remove redundant indirection

This commit is contained in:
Patrick Lühne 2020-04-10 23:02:14 +02:00
parent 02cf3f552b
commit 80980e4e11
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
5 changed files with 26 additions and 30 deletions

View File

@ -178,12 +178,12 @@ impl BinaryOperation
pub struct Function
{
pub declaration: std::rc::Rc<FunctionDeclaration>,
pub arguments: Vec<Box<Term>>,
pub arguments: Terms,
}
impl Function
{
pub fn new(declaration: std::rc::Rc<FunctionDeclaration>, arguments: Vec<Box<Term>>) -> Self
pub fn new(declaration: std::rc::Rc<FunctionDeclaration>, arguments: Terms) -> Self
{
assert_eq!(declaration.arity, arguments.len(),
"function has a different number of arguments then declared");
@ -307,12 +307,12 @@ impl Implies
pub struct Predicate
{
pub declaration: std::rc::Rc<PredicateDeclaration>,
pub arguments: Vec<Box<Term>>,
pub arguments: Terms,
}
impl Predicate
{
pub fn new(declaration: std::rc::Rc<PredicateDeclaration>, arguments: Vec<Box<Term>>) -> Self
pub fn new(declaration: std::rc::Rc<PredicateDeclaration>, arguments: Terms) -> Self
{
assert_eq!(declaration.arity, arguments.len(),
"predicate has a different number of arguments then declared");
@ -340,7 +340,7 @@ pub enum Term
Variable(Variable),
}
pub type Terms = Vec<Box<Term>>;
pub type Terms = Vec<Term>;
impl Term
{
@ -379,8 +379,7 @@ impl Term
Self::boolean(false)
}
pub fn function(declaration: std::rc::Rc<FunctionDeclaration>, arguments: Vec<Box<Term>>)
-> Self
pub fn function(declaration: std::rc::Rc<FunctionDeclaration>, arguments: Terms) -> Self
{
Self::Function(Function::new(declaration, arguments))
}
@ -546,8 +545,7 @@ impl Formula
Self::Or(arguments)
}
pub fn predicate(declaration: std::rc::Rc<PredicateDeclaration>, arguments: Vec<Box<Term>>)
-> Self
pub fn predicate(declaration: std::rc::Rc<PredicateDeclaration>, arguments: Terms) -> Self
{
Self::Predicate(Predicate::new(declaration, arguments))
}

View File

@ -483,9 +483,10 @@ mod tests
Box::new(Formula::or(arguments.into_iter().map(|x| *x).collect()))
}
fn predicate(name: &str, arguments: Terms) -> Box<Formula>
fn predicate(name: &str, arguments: Vec<Box<Term>>) -> Box<Formula>
{
Box::new(Formula::predicate(predicate_declaration(name, arguments.len()), arguments))
Box::new(Formula::predicate(predicate_declaration(name, arguments.len()),
arguments.into_iter().map(|x| *x).collect()))
}
fn predicate_declaration(name: &str, arity: usize) -> std::rc::Rc<PredicateDeclaration>

View File

@ -316,7 +316,8 @@ pub(crate) mod tests
pub(crate) fn function(name: &str, arguments: Vec<Box<Term>>) -> Box<Term>
{
Box::new(Term::function(function_declaration(name, arguments.len()), arguments))
Box::new(Term::function(function_declaration(name, arguments.len()),
arguments.into_iter().map(|x| *x).collect()))
}
pub(crate) fn function_declaration(name: &str, arity: usize) -> std::rc::Rc<FunctionDeclaration>
@ -404,17 +405,17 @@ pub(crate) mod tests
constant("e")
}
pub(crate) fn abc() -> Terms
pub(crate) fn abc() -> Vec<Box<Term>>
{
vec![a(), b(), c()]
}
pub(crate) fn a1b1c1() -> Terms
pub(crate) fn a1b1c1() -> Vec<Box<Term>>
{
vec![constant("a1"), constant("b1"), constant("c1")]
}
pub(crate) fn a2b2c2() -> Terms
pub(crate) fn a2b2c2() -> Vec<Box<Term>>
{
vec![constant("a2"), constant("b2"), constant("c2")]
}
@ -434,7 +435,7 @@ pub(crate) mod tests
variable("Z")
}
pub(crate) fn xyz() -> Terms
pub(crate) fn xyz() -> Vec<Box<Term>>
{
vec![x(), y(), z()]
}

View File

@ -642,9 +642,9 @@ mod tests
assert_eq!(predicate_remainder("s ( )"), "");
assert_eq!(predicate("s ( 1 , 2 , 3 )").declaration.name, "s");
assert_eq!(predicate("s ( 1 , 2 , 3 )").declaration.arity, 3);
assert_eq!(*predicate("s ( 1 , 2 , 3 )").arguments.remove(0), Term::integer(1));
assert_eq!(*predicate("s ( 1 , 2 , 3 )").arguments.remove(1), Term::integer(2));
assert_eq!(*predicate("s ( 1 , 2 , 3 )").arguments.remove(2), Term::integer(3));
assert_eq!(predicate("s ( 1 , 2 , 3 )").arguments.remove(0), Term::integer(1));
assert_eq!(predicate("s ( 1 , 2 , 3 )").arguments.remove(1), Term::integer(2));
assert_eq!(predicate("s ( 1 , 2 , 3 )").arguments.remove(2), Term::integer(3));
assert_eq!(predicate_remainder("s ( 1 , 2 , 3 )"), "");
assert_eq!(predicate_remainder("s ( 1 , 2 , 3 )"), "");
assert_eq!(predicate("s ( ), rest").declaration.name, "s");

View File

@ -59,7 +59,7 @@ fn absolute_value<'i>(i: &'i str, d: &Declarations) -> IResult<&'i str, crate::T
}
pub(crate) fn function_or_predicate<'i>(i: &'i str, d: &Declarations)
-> IResult<&'i str, (&'i str, Option<Vec<Box<crate::Term>>>)>
-> IResult<&'i str, (&'i str, Option<crate::Terms>)>
{
pair
(
@ -82,11 +82,7 @@ pub(crate) fn function_or_predicate<'i>(i: &'i str, d: &Declarations)
tag(","),
multispace0,
),
map
(
|i| term(i, d),
Box::new,
),
|i| term(i, d),
),
preceded
(
@ -485,8 +481,8 @@ mod tests
assert_eq!(term_as_function("s").declaration.arity, 0);
assert_eq!(term_as_function("s(1, 2, 3)").declaration.name, "s");
assert_eq!(term_as_function("s(1, 2, 3)").declaration.arity, 3);
assert_eq!(*term_as_function("s(1, 2, 3)").arguments.remove(0), Term::integer(1));
assert_eq!(*term_as_function("s(1, 2, 3)").arguments.remove(2), Term::integer(3));
assert_eq!(term_as_function("s(1, 2, 3)").arguments.remove(0), Term::integer(1));
assert_eq!(term_as_function("s(1, 2, 3)").arguments.remove(2), Term::integer(3));
}
#[test]
@ -715,9 +711,9 @@ mod tests
assert_eq!(function_remainder("s ( )"), "");
assert_eq!(function("s ( 1 , 2 , 3 )").declaration.name, "s");
assert_eq!(function("s ( 1 , 2 , 3 )").declaration.arity, 3);
assert_eq!(*function("s ( 1 , 2 , 3 )").arguments.remove(0), Term::integer(1));
assert_eq!(*function("s ( 1 , 2 , 3 )").arguments.remove(1), Term::integer(2));
assert_eq!(*function("s ( 1 , 2 , 3 )").arguments.remove(2), Term::integer(3));
assert_eq!(function("s ( 1 , 2 , 3 )").arguments.remove(0), Term::integer(1));
assert_eq!(function("s ( 1 , 2 , 3 )").arguments.remove(1), Term::integer(2));
assert_eq!(function("s ( 1 , 2 , 3 )").arguments.remove(2), Term::integer(3));
assert_eq!(function_remainder("s ( 1 , 2 , 3 )"), "");
assert_eq!(function("s ( ), rest").declaration.name, "s");
assert_eq!(function("s ( ), rest").declaration.arity, 0);