Skip to content

Commit

Permalink
assertAlphaEqual added
Browse files Browse the repository at this point in the history
  • Loading branch information
Innokenty committed Dec 11, 2024
1 parent 49c4ab0 commit c1177e9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/src/atom/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,9 @@ mod test {

#[test]
fn test_atoms_are_equivalent() {
assert!(atoms_are_equivalent(&expr!(x "b" {"c"}), &expr!(x "b" {"c"})));
assert!(atoms_are_equivalent(&expr!(x "b" x), &expr!(x "b" x)));
assert!(atoms_are_equivalent(&expr!(a a "b" {"c"}), &expr!(x x "b" {"c"})));
assert!(atoms_are_equivalent(&expr!(a "b" {"c"}), &expr!(x "b" {"c"})));
assert!(atoms_are_equivalent(&expr!(a b), &expr!(c d)));
assert!(!atoms_are_equivalent(&expr!(a "b" {"c"}), &expr!(a "x" {"c"})));
Expand Down
55 changes: 54 additions & 1 deletion lib/src/metta/runner/stdlib/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::metta::*;
use crate::metta::text::Tokenizer;
use crate::space::*;
use crate::common::assert::vec_eq_no_order;

use crate::atom::matcher::atoms_are_equivalent;
use crate::metta::runner::stdlib::{grounded_op, atom_to_string, regex, interpret_no_error, unit_result};

use std::convert::TryInto;
Expand All @@ -18,6 +18,15 @@ fn assert_results_equal(actual: &Vec<Atom>, expected: &Vec<Atom>, atom: &Atom) -
}
}

fn assert_alpha_equal(actual: &Vec<Atom>, expected: &Vec<Atom>, atom: &Atom) -> Result<Vec<Atom>, ExecError> {
log::debug!("assert_alpha_equal: actual: {:?}, expected: {:?}, actual atom: {:?}", actual, expected, atom);
let report = format!("\nExpected: {:?}\nGot: {:?}", expected, actual);
match atoms_are_equivalent(actual.get(0).unwrap(), expected.get(0).unwrap()) {
true => unit_result(),
false => Err(ExecError::Runtime(format!("{}", report)))
}
}

/// Implement trace! built-in.
///
/// It is equivalent to Idris or Haskell Trace, that is, it prints a
Expand Down Expand Up @@ -141,6 +150,42 @@ impl CustomExecute for AssertEqualOp {
}
}

#[derive(Clone, Debug)]
pub struct AssertAlphaEqualOp {
space: DynSpace,
}

grounded_op!(AssertAlphaEqualOp, "assertAlphaEqual");

impl AssertAlphaEqualOp {
pub fn new(space: DynSpace) -> Self {
Self{ space }
}
}

impl Grounded for AssertAlphaEqualOp {
fn type_(&self) -> Atom {
Atom::expr([ARROW_SYMBOL, ATOM_TYPE_ATOM, ATOM_TYPE_ATOM, ATOM_TYPE_ATOM])
}

fn as_execute(&self) -> Option<&dyn CustomExecute> {
Some(self)
}
}

impl CustomExecute for AssertAlphaEqualOp {
fn execute(&self, args: &[Atom]) -> Result<Vec<Atom>, ExecError> {
log::debug!("AssertAlphaEqualOp::execute: {:?}", args);
let arg_error = || ExecError::from("assertAlphaEqual expects two atoms as arguments: actual and expected");
let actual_atom = args.get(0).ok_or_else(arg_error)?;
let expected_atom = args.get(1).ok_or_else(arg_error)?;

let actual = interpret_no_error(self.space.clone(), actual_atom)?;
let expected = interpret_no_error(self.space.clone(), expected_atom)?;
assert_alpha_equal(&actual, &expected, actual_atom)
}
}

#[derive(Clone, Debug)]
pub struct AssertEqualToResultOp {
space: DynSpace,
Expand Down Expand Up @@ -191,13 +236,16 @@ pub fn register_runner_tokens(tref: &mut Tokenizer, space: &DynSpace) {
tref.register_token(regex(r"assertEqual"), move |_| { assert_equal_op.clone() });
let assert_equal_to_result_op = Atom::gnd(AssertEqualToResultOp::new(space.clone()));
tref.register_token(regex(r"assertEqualToResult"), move |_| { assert_equal_to_result_op.clone() });
let assert_alpha_equal_op = Atom::gnd(AssertAlphaEqualOp::new(space.clone()));
tref.register_token(regex(r"assertAlphaEqual"), move |_| { assert_alpha_equal_op.clone() });
}

#[cfg(test)]
mod tests {
use super::*;
use crate::metta::runner::{Metta, EnvBuilder, SExprParser};
use crate::common::test_utils::metta_space;
use crate::metta::runner::stdlib::tests::run_program;

use regex::Regex;

Expand Down Expand Up @@ -229,6 +277,11 @@ mod tests {
]));
}

#[test]
fn metta_assert_alpha_equal_op() {
assert_eq!(run_program(&format!("(= (foo) (R $x $y)) (= (bar) (R $z $x)) !(assertAlphaEqual ((foo) (bar)) ((R $x $y) (R $z $a)))")), Ok(vec![vec![expr!(())]]));
}

#[test]
fn metta_assert_equal_to_result_op() {
let metta = Metta::new(Some(EnvBuilder::test_env()));
Expand Down

0 comments on commit c1177e9

Please sign in to comment.