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

Issues with foldl-atom #857

Open
rTreutlein opened this issue Feb 21, 2025 · 3 comments
Open

Issues with foldl-atom #857

rTreutlein opened this issue Feb 21, 2025 · 3 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@rTreutlein
Copy link

Describe the bug
foldl-atom works when provided with a function but if the function is substituted by it's body it breaks

To Reproduce

(= (remove $list $elem)
   (if-decons-expr $list $head $tail
      (unify $elem $head ($head $tail) (let ($res $ntail) (remove $tail $elem) ($res (cons-atom $head $ntail))))
      (() $list)
))

;;Helper function for overlap
(= (overlap_ff ($left $intersection $right) $elem)
   (let ($res $nright) (remove $right $elem)
     (if (== $res ())
       ((cons-atom $elem $left) $intersection $right)
       ($left (cons-atom $res $intersection) $nright))))

;;Calculate the overlap of 2 sets
;;Retruns (($list1 - $list2) ($list1 intersection $list2) ($list2 - $list1))
;;Variables are treated as matchichg any set
(= (overlap $list1 $list2)
   (foldl-atom $list1 (() () $list2) $accum $elem
    (overlap_ff $accum $elem)))

!(overlap (a b c) (b c d))
;[((a) (c b) (d))] this works

(= (overlap2 $list1 $list2)
   (foldl-atom $list1 (() () $list2) $accum $elem
    (let ($left $intersection $right) $accum
     (let ($res $nright) (remove $right $elem)
      (if (== $res ())
       ((cons-atom $elem $left) $intersection $right)
       ($left (cons-atom $res $intersection) $nright))))))

!(overlap2 (a b c) (b c d))
;[] this doesn't

Expected behavior
both functions should output the same

@vsbogd vsbogd added the bug Something isn't working label Feb 24, 2025
@vsbogd
Copy link
Collaborator

vsbogd commented Feb 24, 2025

The problem is that operation variables like $left, $intersection and $right are not sealed and they keep their values from item to item. This means that on second item they already have values: { $left <- (), $intersection <- (), $right <- (b c d) } and it is not matchable with the current state of the accum ((a) () (b c d)).

I was trying to use sealed to fix it but I found that sealed cannot seal all variables in a expression. You always need to point which variables to seal. This prevents function to seal expression with variables passed from the outer context because function doesn't know which variables to seal. Thus I think we need to change this behaviour and seal all variables by default except variables in the ignorance set passed by caller. Then it will be possible to use sealed inside fold to make it work as expected.

@vsbogd vsbogd self-assigned this Feb 24, 2025
@vsbogd
Copy link
Collaborator

vsbogd commented Feb 24, 2025

Another case of sealed usage is the lambda case. For example here https://github.com/trueagi-io/metta-examples/blob/4e8b3eacec04acda693ba8a66f7b53b28a668a84/SICP_book/chapter_1_3.metta#L29-L31 sealed is used by lambda to make its parameters unique for each call.

Thus sealed is applied to:

  • local variables
  • parameters

Lambda case also is covered by sealed op with list of ignored variables, but it requires adding additional parameter to lambda: list of such variables. Variables which are not in this list and in list of parameters is considered to be local.

@vsbogd
Copy link
Collaborator

vsbogd commented Feb 24, 2025

Summary. This fix this issue one should do the following:

  • change sealed to have list of ignores variables instead of the list of variables to be sealed
  • fix metta-examples and other code using sealed
  • fix foldl-atom to use sealed on each iteration or may be even introduce lambda in the standard metta-library and make foldl-atom get lambda as an argument

@vsbogd vsbogd added the good first issue Good for newcomers label Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants