Skip to content

Commit

Permalink
Merge pull request #835 from vsbogd/fix-evaluate-atom
Browse files Browse the repository at this point in the history
Fix evaluate atom
  • Loading branch information
Necr0x0Der authored Jan 15, 2025
2 parents d121d35 + 96d934c commit 69730e3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/src/metta/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ pub(crate) struct MettaContents {
/// An index, to find a loaded module from a ModuleDescriptor
module_descriptors: Mutex<HashMap<ModuleDescriptor, ModId>>,
/// A clone of the top module's Space, so we don't need to do any locking to access it,
/// to support the metta.space() public function
/// to support the metta.space() public function. Actual module space is an instance
/// of the [module::ModuleSpace]. This instance contains dependencies of the
/// top module. This space is an original space passed to the Metta constructor
/// thus it doesn't contain any dependencies.
top_mod_space: DynSpace,
/// A clone of the top module's Tokenizer
top_mod_tokenizer: Shared<Tokenizer>,
Expand Down Expand Up @@ -439,9 +442,9 @@ impl Metta {
let atom = if is_bare_minimal_interpreter(self) {
atom
} else {
wrap_atom_by_metta_interpreter(self.0.top_mod_space.clone(), atom)
wrap_atom_by_metta_interpreter(self.module_space(ModId::TOP), atom)
};
if self.type_check_is_enabled() && !validate_atom(self.0.top_mod_space.borrow().as_space(), &atom) {
if self.type_check_is_enabled() && !validate_atom(&self.module_space(ModId::TOP), &atom) {
Ok(vec![Atom::expr([ERROR_SYMBOL, atom, BAD_TYPE_SYMBOL])])
} else {
interpret(self.space(), &atom)
Expand Down
6 changes: 4 additions & 2 deletions lib/src/space/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub struct ModuleSpace {

impl Display for ModuleSpace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.main, f)
write!(f, "ModuleSpace({})", &self.main)
}
}

impl Debug for ModuleSpace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.main, f)
write!(f, "ModuleSpace({:?})", &self.main)
}
}

Expand All @@ -25,6 +25,7 @@ impl ModuleSpace {
}

pub fn query(&self, query: &Atom) -> BindingsSet {
log::debug!("ModuleSpace::query: {}", query);
let mut results = self.main.query(query);
for dep in &self.deps {
if let Some(space) = dep.borrow().as_any() {
Expand All @@ -41,6 +42,7 @@ impl ModuleSpace {
}

fn query_no_deps(&self, query: &Atom) -> BindingsSet {
log::debug!("ModuleSpace::query_no_deps: {}", query);
self.main.query(query)
}

Expand Down
12 changes: 12 additions & 0 deletions python/tests/test_metta.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ def test_match_with_rust_grounded_atom(self):

self.assertEqual([[]], result)

def test_metta_evaluate_atom_using_stdlib(self):
program = '''
(= (f) (let ($x $y) (A B) $x))
'''
runner = MeTTa(env_builder=Environment.test_env())
runner.run(program)

result = runner.run('!(f)')
self.assertEqual([[S('A')]], result)

result = runner.evaluate_atom(E(S('f')))
self.assertEqual([S('A')], result)

0 comments on commit 69730e3

Please sign in to comment.