Skip to content

Commit 69730e3

Browse files
authored
Merge pull request #835 from vsbogd/fix-evaluate-atom
Fix evaluate atom
2 parents d121d35 + 96d934c commit 69730e3

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/src/metta/runner/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ pub(crate) struct MettaContents {
124124
/// An index, to find a loaded module from a ModuleDescriptor
125125
module_descriptors: Mutex<HashMap<ModuleDescriptor, ModId>>,
126126
/// A clone of the top module's Space, so we don't need to do any locking to access it,
127-
/// to support the metta.space() public function
127+
/// to support the metta.space() public function. Actual module space is an instance
128+
/// of the [module::ModuleSpace]. This instance contains dependencies of the
129+
/// top module. This space is an original space passed to the Metta constructor
130+
/// thus it doesn't contain any dependencies.
128131
top_mod_space: DynSpace,
129132
/// A clone of the top module's Tokenizer
130133
top_mod_tokenizer: Shared<Tokenizer>,
@@ -439,9 +442,9 @@ impl Metta {
439442
let atom = if is_bare_minimal_interpreter(self) {
440443
atom
441444
} else {
442-
wrap_atom_by_metta_interpreter(self.0.top_mod_space.clone(), atom)
445+
wrap_atom_by_metta_interpreter(self.module_space(ModId::TOP), atom)
443446
};
444-
if self.type_check_is_enabled() && !validate_atom(self.0.top_mod_space.borrow().as_space(), &atom) {
447+
if self.type_check_is_enabled() && !validate_atom(&self.module_space(ModId::TOP), &atom) {
445448
Ok(vec![Atom::expr([ERROR_SYMBOL, atom, BAD_TYPE_SYMBOL])])
446449
} else {
447450
interpret(self.space(), &atom)

lib/src/space/module.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ pub struct ModuleSpace {
99

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

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

@@ -25,6 +25,7 @@ impl ModuleSpace {
2525
}
2626

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

4344
fn query_no_deps(&self, query: &Atom) -> BindingsSet {
45+
log::debug!("ModuleSpace::query_no_deps: {}", query);
4446
self.main.query(query)
4547
}
4648

python/tests/test_metta.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ def test_match_with_rust_grounded_atom(self):
9898

9999
self.assertEqual([[]], result)
100100

101+
def test_metta_evaluate_atom_using_stdlib(self):
102+
program = '''
103+
(= (f) (let ($x $y) (A B) $x))
104+
'''
105+
runner = MeTTa(env_builder=Environment.test_env())
106+
runner.run(program)
107+
108+
result = runner.run('!(f)')
109+
self.assertEqual([[S('A')]], result)
110+
111+
result = runner.evaluate_atom(E(S('f')))
112+
self.assertEqual([S('A')], result)

0 commit comments

Comments
 (0)