Skip to content

Commit

Permalink
Replace bool in Python stdlib by the Rust primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbogd committed Nov 22, 2024
1 parent e8a1ede commit 34d1175
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
11 changes: 11 additions & 0 deletions c/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::collections::HashSet;
use std::sync::atomic::{AtomicPtr, Ordering};

use hyperon::matcher::{Bindings, BindingsSet};
use hyperon::metta::runner::arithmetics::Bool;
use hyperon::metta::runner::arithmetics::Number;

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Expand Down Expand Up @@ -270,6 +271,16 @@ pub extern "C" fn atom_gnd(gnd: *mut gnd_t) -> atom_t {
Atom::gnd(CGrounded(AtomicPtr::new(gnd))).into()
}

/// @ingroup atom_group
/// @param[in] b boolean value
/// @return an `atom_t` for the Bool Grounded atom
/// @note The caller must take ownership responsibility for the returned `atom_t`
///
#[no_mangle]
pub extern "C" fn atom_bool(b: bool) -> atom_t {
Atom::gnd(Bool(b)).into()
}

/// @ingroup atom_group
/// @param[in] n integer number
/// @return an `atom_t` for the Number Grounded atom
Expand Down
7 changes: 5 additions & 2 deletions python/hyperon/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ def _priv_atom_gnd(obj, type):
catom = hp.atom_space(obj.cspace)
elif isinstance(obj, ValueObject):
value = obj.value
if isinstance(value, int) and not isinstance(value, bool):
if isinstance(value, bool):
# FIXME: add assert on type like for space
catom = hp.atom_bool(value)
elif isinstance(value, int):
# FIXME: add assert on type like for space
catom = hp.atom_int(value)
elif isinstance(value, float):
Expand Down Expand Up @@ -579,7 +582,7 @@ def PrimitiveAtom(value, type_name=None, atom_id=None):
converts Python primitives into MeTTa ones. This function is added to
override this rule if needed.
"""
PRIMITIVE_TYPES = (int, float)
PRIMITIVE_TYPES = (int, float, bool)
assert isinstance(value, PRIMITIVE_TYPES), f"Primitive value {PRIMITIVE_TYPES} is expected"
type = _type_sugar(type_name)
return GroundedAtom(hp.atom_py(ValueObject(value, atom_id), type.catom))
Expand Down
15 changes: 0 additions & 15 deletions python/hyperon/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ def __eq__(self, other):
return self.char == other.char
return False

@register_atoms
def bool_ops():
equalAtom = OperationAtom('==', lambda a, b: [ValueAtom(a == b, 'Bool')],
['$t', '$t', 'Bool'], unwrap=False)
orAtom = OperationAtom('or', lambda a, b: a or b, ['Bool', 'Bool', 'Bool'])
andAtom = OperationAtom('and', lambda a, b: a and b, ['Bool', 'Bool', 'Bool'])
notAtom = OperationAtom('not', lambda a: not a, ['Bool', 'Bool'])
return {
r"==": equalAtom,
r"or": orAtom,
r"and": andAtom,
r"not": notAtom
}

class RegexMatchableObject(MatchableObject):
''' To match atoms with regular expressions'''

Expand Down Expand Up @@ -95,7 +81,6 @@ def type_tokens():
return {
r"(?s)^\".*\"$": lambda token: ValueAtom(str(token[1:-1]), 'String'),
"\'[^\']\'": lambda token: ValueAtom(Char(token[1]), 'Char'),
r"True|False": lambda token: ValueAtom(token == 'True', 'Bool'),
r'regex:"[^"]*"': lambda token: G(RegexMatchableObject(token), AtomType.UNDEFINED)
}

Expand Down
3 changes: 3 additions & 0 deletions python/hyperonpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ PYBIND11_MODULE(hyperonpy, m) {
atom_t typ = atom_clone(ctyp.ptr());
return CAtom(atom_gnd(new GroundedObject(object, typ)));
}, "Create general grounded atom from Python object");
m.def("atom_bool", [](bool b) {
return CAtom(atom_bool(b));
}, "Create bool grounded atom");
m.def("atom_int", [](long long n) {
return CAtom(atom_int(n));
}, "Create int grounded atom");
Expand Down

0 comments on commit 34d1175

Please sign in to comment.