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

What is the most idiomatic way for "filtering" tuples in Metta? #52

Open
CICS-Oleg opened this issue Sep 19, 2024 · 12 comments
Open

What is the most idiomatic way for "filtering" tuples in Metta? #52

CICS-Oleg opened this issue Sep 19, 2024 · 12 comments
Labels
help wanted Extra attention is needed question Further information is requested tutorial Tutorial question

Comments

@CICS-Oleg
Copy link
Contributor

CICS-Oleg commented Sep 19, 2024

Suppose we have a tupe of atoms (alpha beta gamma delta delta). How to filter out delta?
Now suppose we have a tupe of strings ("alpha" "beta" "gamma" "delta" "delta"). How to filter out 'delta' most effectively in this case?
Lets consider nested scenarios: (alpha beta gamma (delta (delta))) and ("alpha" "beta" "gamma" ("delta" ("delta"))). What should be done in this case?

I'm afraid the ways to solve this I could imagine right away are more "functional" and not effective like recursive functions from SICP examples.

I suspect it can be done by a matching technique but don't get how to correctly formulate desired output for match . Needs some sql's 'EXCEPT' analog or whatever.

!(add-atom &self (alpha beta gamma (delta (delta))))
!(add-atom &self ("alpha" "beta" "gamma" ("delta" ("delta"))))
!(match &self (delta) (all the rest elements in tuple besides deltas))
@CICS-Oleg CICS-Oleg added help wanted Extra attention is needed question Further information is requested tutorial Tutorial question labels Sep 19, 2024
@DaddyWesker
Copy link
Contributor

Unfortunately I can't come with non-functional variant either. Probably @Necr0x0Der or @vsbogd could help out with your question.

@vsbogd
Copy link
Contributor

vsbogd commented Sep 19, 2024

I suspect it can be done by a matching technique but don't get how to correctly formulate desired output for match . Needs some sql's 'EXCEPT' analog or whatever.

It is not possible to use match to filter out something from the tuple.

I am not sure I understand your question correctly though. Do you mean if (alpha beta gamma delta delta) is given you need to get (alpha beta gamma)? Does order of other atoms inside tuple matters?

@Necr0x0Der
Copy link
Contributor

More context would be appreciated. Maybe, the initial representation is just inconvenient. For example, instead of (Friends-of Alpha Beta Gamma Delta) one could use (Friend-of Alpha Beta), (Friend-of Alpha Gamma), (Friend-of Alpha Delta).
However, if one wants a generic way to traverse a hypergraph starting with a certain atom, then the current atomspace for the interpreter is not very suitable for this (since it doesn't do deduplication of atoms).But I don't think it is what you need in any case

@CICS-Oleg
Copy link
Contributor Author

@vsbogd

Do you mean if (alpha beta gamma delta delta) is given you need to get (alpha beta gamma)?

Yes.

Does order of other atoms inside tuple matters?

Let's suppose that it doesn't matter.

@Necr0x0Der
Copy link
Contributor

Let's suppose that it doesn't matter.

Your usecase is still not clear, but what's about something like this?

(member my-set alpha)
(member my-set beta)
(member my-set gamma)
(member my-set delta)
!(match &self (member $set delta)
   (match &self (member $x $member) $member))

@CICS-Oleg
Copy link
Contributor Author

(member my-set alpha)
(member my-set beta)
(member my-set gamma)
(member my-set delta)
(member aint-my-set delta)

Taking the example above, how to make a set of entities, which are in my-set, but not in aint-my-set?
And speaking about operations on sets, how to get intersection of sets for the example above?

@CICS-Oleg
Copy link
Contributor Author

(member my-set alpha)
(member my-set beta)
(member my-set gamma)
(member my-set delta)
!(match &self (member $set delta)
   (match &self (member $x $member) $member))

Is it a filtering example? If it's so then it gives the same result as
!(match &self (member $x $member) $member))
with no filtering.

@Necr0x0Der
Copy link
Contributor

Is it a filtering example?

there is a typo; it should be

!(match &self (member $set delta)
   (match &self (member $set $member) $member))

Although, if delta is in several sets, then the result will be their union

@Necr0x0Der
Copy link
Contributor

how to make a set of entities, which are in my-set, but not in aint-my-set?

Maybe, this is the right way

!(match &self (member my-set $member)
   (unify (member aint-my-set $member) &self Empty $member))

@Necr0x0Der
Copy link
Contributor

Necr0x0Der commented Oct 3, 2024

And speaking about operations on sets, how to get intersection of sets for the example above?

!(match &self
    (, (member my-set $member)
       (member aint-my-set $member))
    $member)

@DaddyWesker
Copy link
Contributor

Is it a filtering example?

there is a typo; it should be

!(match &self (member $set delta)
   (match &self (member $set $member) $member))

Although, if delta is in several sets, then the result will be their union

For me this code still gives [gamma, delta, beta, alpha], so all members of set which contains delta if I read this code right.

@Necr0x0Der
Copy link
Contributor

Well, this code is definitely for getting all the member of all the sets, which include delta as an element. It's definitely not for filtering delta out. If was possibly not a filtering example, but an example of how to represent unordered tuples for them to be amenable for match. As per filtering out the result, it is not possible to do this by match directly (in the interpreter space), but it can easily be done by post-processing:

(member my-set alpha)
(member my-set beta)
(member my-set gamma)
(member my-set delta)
(= (all-but-delta $set)
     (match &self (member $set $x)
            (case $x
                ((delta Empty)
                 ($_ $x)))))
! (all-but-delta my-set)

OTOH, one can write something like:

(= (all-but-delta $set)
   (collapse
      (let $x (superpose $set)
        (if (== $x delta) Empty $x))))

! (all-but-delta (alpha beta gamma delta delta))

The nested scenario is more complex. So, the question was if it is necessary to work with tuples or another representation of sets can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested tutorial Tutorial question
Projects
None yet
Development

No branches or pull requests

4 participants