atoms ¶
The Python wrapper for Hyperon Atom Rust types
Atom ¶
AtomType ¶
Defines all Atom types
Bindings ¶
Bindings(bindings: Union[CBindings, None] = None)
Interface for working with atom matching and variable-to-atom binding.
Source code in python/hyperon/atoms.py
__deepcopy__ ¶
__del__ ¶
__enter__ ¶
__eq__ ¶
__exit__ ¶
__repr__ ¶
add_var_binding ¶
add_var_binding(var: VariableAtom, atom: Atom) -> bool
clone ¶
iterator ¶
Returns an iterator over the variable-atom pairs in the bindings
merge ¶
merge(other: Bindings) -> BindingsSet
narrow_vars ¶
Keeps only specific variable associations.
Source code in python/hyperon/atoms.py
BindingsSet ¶
Represents a set of Bindings frames, potentially expressing all possible matches produced by a match operation.
Source code in python/hyperon/atoms.py
__deepcopy__ ¶
__del__ ¶
__enter__ ¶
__eq__ ¶
__exit__ ¶
__getitem__ ¶
Gets a Bindings frame by index
Source code in python/hyperon/atoms.py
__repr__ ¶
add_var_binding ¶
add_var_binding(var: VariableAtom, value: Atom) -> bool
Adds a new variable to atom association to every Bindings frame in a BindingsSet.
Source code in python/hyperon/atoms.py
add_var_equality ¶
Asserts equality between two Variable atoms in a BindingsSet.
clone ¶
empty ¶
Creates a new BindingsSet without any Bindings frames. Conceptually, this means no valid matches exist.
is_single ¶
is_single() -> bool
Checks if a Bindings set contains a frame with no associations, and thus allows variables to take any value.
iterator ¶
merge_into ¶
merge_into(input: Union[BindingsSet, Bindings])
Merges the contents of another BindingsSet or Bindings frame.
Source code in python/hyperon/atoms.py
ExpressionAtom ¶
GroundedAtom ¶
Bases: Atom
A GroundedAtom represents sub-symbolic knowledge. At the API level, it allows keeping data and behaviour inside an Atom. There are three aspects of a GroundedAtom which can be customized:
- the type of GroundedAtom is provided by the Atom itself;
- the matching algorithm used by the Atom;
- an Atom can be made executable, and used to apply sub-symbolic
operations to other Atoms as arguments.
Source code in python/hyperon/atoms.py
__del__ ¶
__eq__ ¶
__repr__ ¶
get_grounded_type ¶
get_metatype ¶
get_object ¶
Returns the GroundedAtom object, or the Space wrapped inside a GroundedAtom, or convert supported Rust grounded objects into corresponding ValueObjects. Function raises TypeError when grounded atom cannot be converted to Python object. Uncatched Python exception can lead to Rust panic in some contexts, for example when exception is thrown from atomspace query unification procedure. It is highly recommended to call get_object method inside try-except block to catch TypeError.
Source code in python/hyperon/atoms.py
iterate ¶
Performs a depth-first exhaustive iteration of an Atom and all its children recursively.
GroundedObject ¶
A GroundedObject holds some content and, optionally, an identifier.
Source code in python/hyperon/atoms.py
__repr__ ¶
Returns the object's ID if present, or a string representation of its content if not.
Source code in python/hyperon/atoms.py
copy ¶
Returns a copy of this GroundedObject instance.
Note: Currently, this method returns the original instance.
IncorrectArgumentError ¶
Bases: Exception
Argument is not recognized by function implementation. It can be argument of incorrect type or in incorrect format. Interpreter handles this error similarly to the situation when pure function definition is not matched.
MatchableObject ¶
Bases: ValueObject
Represents an object that can be involved in a matching operation with an Atom.
This class is meant to be subclassed by objects that define specific matching behavior with an Atom. It provides a stub method for the matching operation that raises a RuntimeError when called, which must be overridden by subclasses.
Inherits
ValueObject: The parent class that provides basic value-based equality and representation.
Methods:
-
match_–A stub method for matching the object with an Atom.
Example
class MyMatchableObject(MatchableObject): def match_(self, atom): # Implement the matching logic here pass
my_obj = MyMatchableObject("some_value") my_obj.match_(some_atom) # Should not raise RuntimeError
Raises:
-
RuntimeError–Raised when the match_ method is called without being overridden by a subclass.
Source code in python/hyperon/atoms.py
__eq__ ¶
Compares the equality of this ValueObject with another based on their content.
Source code in python/hyperon/atoms.py
__repr__ ¶
Returns the object's ID if present, or a string representation of its content if not.
Source code in python/hyperon/atoms.py
copy ¶
Returns a copy of this GroundedObject instance.
Note: Currently, this method returns the original instance.
match_ ¶
A stub method for matching the object with an Atom.
This method is intended to be overridden by subclasses to provide specific matching behavior with an Atom.
Parameters:
-
atom(Atom) –An Atom object to match against.
Raises:
-
RuntimeError–Raised when this method is called without being overridden in a subclass.
Source code in python/hyperon/atoms.py
serialize ¶
Serialize standard Python values. This implementation is used to pass Python values into the foreign runtime.
Source code in python/hyperon/atoms.py
MettaError ¶
Bases: Exception
Custom exception; raised when a error should be returned from OperationAtom, , but we don't want to output Python error stack.
NoReduceError ¶
Bases: Exception
Custom exception; raised when a reduction operation cannot be performed.
OperationObject ¶
Bases: GroundedObject
An OperationObject represents an operation as a grounded object, allowing for more advanced logic like lazy evaluation, type-checking, and more.
Inherits
GroundedObject: The parent class that provides the basic wrapper around content.
Attributes:
-
unwrap(bool) –Determines whether to unwrap the content of GroundedAtoms when passed as arguments to the operation.
Properties
op: Returns the operation function. name: Returns the identifier name for this operation object.
Methods:
-
execute–Executes the operation with the provided arguments.
-
__eq__–Compares the equality of this OperationObject instance with another.
Example
def add(a, b): return a + b
op_obj = OperationObject("addition", add) result = op_obj.execute(3, 4)
Parameters: name (str): The identifier for this operation. op (function): The function representing the operation. unwrap (bool, optional): Whether to unwrap GroundedAtom content when applying the operation. Defaults to True.
Source code in python/hyperon/atoms.py
__eq__ ¶
Compares the equality of this OperationObject with another based on their names.
Parameters:
-
other(OperationObject) –Another OperationObject instance to compare.
Returns:
- –
True if both OperationObjects have the same name; False otherwise.
Source code in python/hyperon/atoms.py
__repr__ ¶
Returns the object's ID if present, or a string representation of its content if not.
Source code in python/hyperon/atoms.py
copy ¶
Returns a copy of this GroundedObject instance.
Note: Currently, this method returns the original instance.
execute ¶
Executes the operation with the provided arguments.
Parameters:
-
*args–Arguments to pass to the operation function.
-
res_typ(AtomType, default:UNDEFINED) –The expected result type. Defaults to AtomType.UNDEFINED.
Returns:
- –
The result of the operation.
Raises:
-
NoReduceError–Raised when
unwrap=Trueand a non-GroundedAtom argument is provided. -
RuntimeError–Raised when the result of the operation is not a list.
Note
Depending on the unwrap attribute, this method will either unwrap GroundedAtoms before passing them to the operation or pass them as is.
Source code in python/hyperon/atoms.py
SymbolAtom ¶
Bases: Atom
A SymbolAtom represents a single concept, identified by name. If two symbols have the same name, they reference the same concept.
Source code in python/hyperon/atoms.py
ValueObject ¶
Bases: GroundedObject
A ValueObject is a specialized form of GroundedObject, which treats its content as a value. It allows for equality comparison between the content of two ValueObjects.
Example
obj1 = ValueObject(5) obj2 = ValueObject(5) obj3 = ValueObject(6)
print(obj1 == obj2) # True print(obj1 == obj3) # False
Source code in python/hyperon/atoms.py
__eq__ ¶
Compares the equality of this ValueObject with another based on their content.
Source code in python/hyperon/atoms.py
__repr__ ¶
Returns the object's ID if present, or a string representation of its content if not.
Source code in python/hyperon/atoms.py
copy ¶
Returns a copy of this GroundedObject instance.
Note: Currently, this method returns the original instance.
serialize ¶
Serialize standard Python values. This implementation is used to pass Python values into the foreign runtime.
Source code in python/hyperon/atoms.py
VariableAtom ¶
Bases: Atom
A VariableAtom represents a variable in an expression. It serves as a placeholder that can be matched with, or bound to other Atoms.
Source code in python/hyperon/atoms.py
E ¶
G ¶
MatchableAtom ¶
Creates a Grounded Atom that wraps a matchable value, optionally specifying its type and identifier.
OperationAtom ¶
An OperationAtom wraps an operation with optional type information into a GroundedAtom and associates a name with it. Useful for registering custom operations that can be executed in an Atom-based computational environment.
Source code in python/hyperon/atoms.py
PrimitiveAtom ¶
Creates a GroundedAtom that wraps a given Python primitive value without converting it into the MeTTa primitive. By default ValueAtom function converts Python primitives into MeTTa ones. This function is added to override this rule if needed.
Source code in python/hyperon/atoms.py
S ¶
V ¶
ValueAtom ¶
Creates a GroundedAtom that wraps a given value, optionally specifying its type and identifier. It has special processing for the objects which have cspace attribute and for ValueObject instances of primitive types. Spaces usually should be treated by a special way. Primitive atoms are converted into the MeTTa primitives.