Skip to content

stdlib

Char

Char(char)

Emulate Char type as in a traditional FPL

Source code in python/hyperon/stdlib.py
def __init__(self, char):
    if len(char) != 1:
        raise ValueError("A Char object must be initialized with a single character.")
    self.char = char

RegexMatchableObject

RegexMatchableObject(content, id=None)

Bases: MatchableObject

To match atoms with regular expressions

Source code in python/hyperon/stdlib.py
def __init__(self, content, id=None):
    super().__init__(content, id)

    self.content = self.content.replace("[[", "(").replace("]]", ")").replace("~", " ")

value property

value

Gets the value of the object, which is its content.

__eq__

__eq__(other)

Compares the equality of this ValueObject with another based on their content.

Source code in python/hyperon/atoms.py
def __eq__(self, other):
    """Compares the equality of this ValueObject with another based on their content."""
    # TODO: We need to hook this up the the Value-Bridging mechanism when it's designed and built
    # https://github.com/trueagi-io/hyperon-experimental/issues/351

    # TODO: ?typecheck for the contents
    return isinstance(other, ValueObject) and self.content == other.content

__repr__

__repr__()

Returns the object's ID if present, or a string representation of its content if not.

Source code in python/hyperon/atoms.py
def __repr__(self):
    """Returns the object's ID if present, or a string representation of
    its content if not."""
    # Overwrite Python default representation of a string to use
    # double quotes instead of single quotes.
    if isinstance(self.content, str):
        newstr = repr(self.content)[1:-1].translate(str.maketrans({'"' : r'\"'}))
        return f'"{newstr}"'

    # Use default representation for everything else
    return repr(self.content) if self.id is None else self.id

copy

copy()

Returns a copy of this GroundedObject instance.

Note: Currently, this method returns the original instance.

Source code in python/hyperon/atoms.py
def copy(self):
    """
    Returns a copy of this GroundedObject instance.

    Note: Currently, this method returns the original instance.
    """
    return self

serialize

serialize(serializer)

Serialize standard Python values. This implementation is used to pass Python values into the foreign runtime.

Source code in python/hyperon/atoms.py
def serialize(self, serializer):
    """
    Serialize standard Python values. This implementation is used to
    pass Python values into the foreign runtime.
    """
    if isinstance(self.content, bool):
        return serializer.serialize_bool(self.content)
    elif isinstance(self.content, int):
        return serializer.serialize_int(self.content)
    elif isinstance(self.content, float):
        return serializer.serialize_float(self.content)
    elif isinstance(self.content, str):
        return serializer.serialize_str(self.content)
    else:
        return SerialResult.NOT_SUPPORTED

text_ops

text_ops(metta)

Add text operators

repr: convert Atom to string. parse: convert String to Atom. stringToChars: convert String to tuple of Char. charsToString: convert tuple of Char to String.

see test_stdlib.py for examples.

Source code in python/hyperon/stdlib.py
@register_atoms(pass_metta=True)
def text_ops(metta):
    """Add text operators

    repr: convert Atom to string.
    parse: convert String to Atom.
    stringToChars: convert String to tuple of Char.
    charsToString: convert tuple of Char to String.

    see test_stdlib.py for examples.

    """

    reprAtom = OperationAtom('repr', lambda a: [ValueAtom(repr(a), 'String')],
                             ['Atom', 'String'], unwrap=False)
    parseAtom = OperationAtom('parse', lambda s: parseImpl(s, metta), ['String', 'Atom'], unwrap=False)
    stringToCharsAtom = OperationAtom('stringToChars', lambda s: [E(*[ValueAtom(Char(c)) for c in str(s)[1:-1]])],
                                      ['String', 'Atom'], unwrap=False)
    charsToStringAtom = OperationAtom('charsToString', lambda a: [ValueAtom("".join([str(c)[1:-1] for c in a.get_children()]))],
                                      ['Atom', 'String'], unwrap=False)
    return {
        r"repr": reprAtom,
        r"parse": parseAtom,
        r"stringToChars": stringToCharsAtom,
        r"charsToString": charsToStringAtom
    }