trueagi-io / hyperon-experimental

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

Have assertEqual support alpha equivalence #744

Open ngeiswei opened 4 months ago

ngeiswei commented 4 months ago

Describe the bug Probably not a bug, but it would be very convenient if assertEqual and assertEqualToResult could support alpha-equivalence. Either it could be the default (maybe undesirable though), or new assert operators, such as assertAlphaEqual and assertAlphaEqualToResult could be added.

To Reproduce Steps to reproduce the behavior:

  1. Run the following MeTTa script
    (= (foo) (R $x $y))
    !(assertEqual (foo) (R $x $y))
  2. See output

Expected behavior If assertEqual supported alpha-equivalence it would output [()].

Actual behavior Instead it outputs

[(Error (assertEqual (foo) (R $x $y)) 
Expected: [(R $x $y)]
Got: [(R $x#31 $y#32)]
Missed result: (R $x $y))]

Additional context

vsbogd commented 4 months ago

We have a Rust function which checks two atoms for being alpha-equivalent: https://github.com/trueagi-io/hyperon-experimental/blob/9c126b107f8a5c4ea95db33f7c805e70692d8e9c/lib/src/atom/matcher.rs#L1253 Thus it is not a problem to add couple new operations actually.

ngeiswei commented 4 months ago

Awesome, would you do it? Or would you rather have me do it? I'll be a bit slower but it should be within my reach.

ngeiswei commented 4 months ago

Also, there are at least two ways to do it:

  1. Expose atoms_are_equivalent to MeTTa, then implement assertAlphaEqual in MeTTa (add it in stdlib.metta I suppose).
  2. Implement assertAlphaEqual in Rust, exposed to MeTTa.

Even if we go with 2, I think it would be useful to expose atoms_are_equivalent to MeTTa. I did find it useful in my own code, and I assume the Rust implementation is far more efficient than my MeTTa implementation.

ngeiswei commented 4 months ago

Also, how would you name in MeTTa

ngeiswei commented 4 months ago

Also, a different issue that I'm bringing only because we are discussing names. I don't understand the name assertEqualToResult. Didn't you mean assertEqualToResults, ending with an s? But anyway, that certainly is for another issue.