Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have assertEqual support alpha equivalence #744

Closed
ngeiswei opened this issue Jul 18, 2024 · 10 comments · Fixed by #814
Closed

Have assertEqual support alpha equivalence #744

ngeiswei opened this issue Jul 18, 2024 · 10 comments · Fixed by #814
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@ngeiswei
Copy link
Contributor

ngeiswei commented Jul 18, 2024

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))
  1. 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

@ngeiswei ngeiswei added the enhancement New feature or request label Jul 18, 2024
@vsbogd
Copy link
Collaborator

vsbogd commented Jul 23, 2024

We have a Rust function which checks two atoms for being alpha-equivalent:

pub fn atoms_are_equivalent(left: &Atom, right: &Atom) -> bool {

Thus it is not a problem to add couple new operations actually.

@vsbogd vsbogd added the good first issue Good for newcomers label Jul 23, 2024
@ngeiswei
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

Also, how would you name in MeTTa

  • atoms_are_equivalent. I have used =alpha in my code for instance.
  • assertEqual up to alpha-equivalence? I had thought of assertAlphaEqual but they are other possibilities like assertEquivalent, etc.

@ngeiswei
Copy link
Contributor Author

ngeiswei commented Jul 24, 2024

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.

@vsbogd
Copy link
Collaborator

vsbogd commented Dec 11, 2024

As @DaddyWesker started working on it I am adding my thought on it.

I think adding =alfa function into stdlib (which is call to

pub fn atoms_are_equivalent(left: &Atom, right: &Atom) -> bool {
) is useful.

I would vote for reimplementing assertEqual using =alfa in pure MeTTa and don't introduce special assertAlfaEqual, but I would also check it with @Necr0x0Der. I would also use atoms_are_equivalent inside assertEqualToResult if we agree on changing assertEqual.

I believe name of the assertEqualToResult is not quite important. Result of the atom evaluation can contain many atoms. We can say it is a single result or many results. It is some language ambiguity. Thus I don't see reasons to prefer results over result and as assertEqualToResult is already widely used I would keep it as is.

@ngeiswei
Copy link
Contributor Author

After more thoughts, I think it is better to have assertEqual support non-alpha equivalence by default, and add an extra assertAlphaEqual for alpha equivalence, because equality with alpha equivalence is strictly more permissive than equality without alpha equivalence. And in the context of unit tests I think you want the default to be as restrictive as possibly to capture more errors.

@ngeiswei
Copy link
Contributor Author

ngeiswei commented Dec 11, 2024

I want to add that I have many unit tests that are purposely testing equality without alpha equivalence.

@vsbogd
Copy link
Collaborator

vsbogd commented Dec 11, 2024

equality with alpha equivalence is strictly more permissive than equality without alpha equivalence. And in the context of unit tests I think you want the default to be as restrictive as possibly to capture more errors.

In general I agree. But in practice there are many situations when user's expectation of names are more or less arbitrary and interpreter can choose another naming (see #127 (comment) for example). Anyway I don't object adding separate alpha equivalent assert.

@ngeiswei
Copy link
Contributor Author

That makes sense too, let's see what @Necr0x0Der has to say.

@vsbogd vsbogd linked a pull request Dec 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants