trueagi-io / hyperon-experimental

MeTTa programming language implementation
https://metta-lang.dev
MIT License
148 stars 49 forks source link

Custom match for operations with arguments #546

Open Necr0x0Der opened 9 months ago

Necr0x0Der commented 9 months ago

Currently, grounded atoms can have custom match_, but it works for atoms themselves, while it would be useful for operations to handle their arguments in match_ as well. For example, (+ $x 1) could handle matching against 5 inferring a binding $x <- 4. Similarly, cons-atom could be used for deconstruction, e.g. (cons-atom $car $cdr) matched against (a b c) would yield bindings $car <- a, $cdr <- (b c), so one could write

(case $x
  ((cons-atom $car $cdr) ...)

instead of

(let ($car $cdr) ((car-atom $x) (cdr-atom $x)) ...

in recursive analysis of expressions.

It is generally not useful to have a custom match for operations without arguments.

Necr0x0Der commented 9 months ago

This might even be usable in function definitions, e.g.

(= (is-closed (cons-atom $x $xs)) (and (is-closed $x) (is-closed $xs)))