Skip to content

PyMeTTa handies [issue for ideas] #964

@Necr0x0Der

Description

@Necr0x0Der

There can be various automations and syntactic sugar to make Python-MeTTa integration more convenient and pretty.
A few ideas for starters:

  1. Introduce @grounded decorator for extensions. Currently, @register_tokens decorates a function, which returns a token-atom map. For simple cases, when we want to have a grounded function, which token and representation in MeTTa correspond to its name in Python, and it is a pure Python function with unwrap=True, we could decorate it directly, i.e.
@grounded
def sum_squares(xs):
  arr = jnp.array(xs)
  return int(jnp.sum(arr*arr))

instead of

def sum_squares(xs):
  arr = jnp.array(xs)
  return int(jnp.sum(arr*arr))


@register_atoms
def def_atoms():
   return {
       'sum_squares': OperationAtom('sum_squares', sum_squares)
   }

Not sure if it is useful only for extensions, or there can be some sense in having a similar decorator in the main Python script.

  1. It would be useful to make a backward import of MeTTa functions to Python. Something like
metta = MeTTa()
metta.run('''
(= (my-function $xs) ...)
'''
)
# metta.import2py('my-function')
metta.my_function(...)

That is, importing MeTTa functions to Python can be done by creating methods in metta object. Or, maybe, there are other ways to make calling MeTTa functions from Python more convenient.

  1. MyOperationAtom([1, 2, 3]) as a syntactic sugar for E(MyOperationAtom, ValueAtom([1, 2, 3])).

  2. Convenient access to grounded atoms defined in metta from Python. Currently, if the atom is not created in a Python script and put into a variable (MyOperationAtom = OperationAtom(...)), it is very inconvenient to call it from MeTTa. It typically requires parsing from textual representation with metta.run or metta.parse*. It would actually be nice to have them readily accessible as Python objects. Maybe, something like metta.stdlib.randint or whatever.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestpythonquestionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions