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

Querying connected sub-spaces #816

Open
nasser opened this issue Dec 11, 2024 · 2 comments
Open

Querying connected sub-spaces #816

nasser opened this issue Dec 11, 2024 · 2 comments

Comments

@nasser
Copy link

nasser commented Dec 11, 2024

I am in a situation where I need to query connected sub-spaces. My intuition is that I will have to write a custom Space implementation in Rust but I was wondering if there was a way to do it directly in Metta.

I need to group atoms into arbitrary "zones". Zones can be connected to other zones, forming a DAG. Querying a zone should factor in all of its atoms and the atoms of the transitive walk of its connections at the same time, and it should support compound queries that use (, ...).

I have prototypes that tag atoms in a zone, but because querying bottoms out in unify and considers each atom in turn, I cannot support compound queries. Here it is as an example of my thinking, eventually modeling a "cousin" relationship as (, (uncle me $u) (child $u $c)):

(= (zone:connections $zone)
   (match &self (zone:connect $zone $x) $x))
(= (zone:connections $zone)
   (zone:connections (match &self (zone:connect $zone $x) $x)))

(= (zone:get $zone)
   (let $p (zone:connections $zone)
        (match &self (zone $p $a) $a)))
(= (zone:get $zone)
   (match &self (zone $zone $a) $a))

(= (? $zone $query $success)
   (let $atom (zone:get $zone)
        (unify $atom $query $success (empty))))

(zone foo (atom of interest))
(zone bar (atom of note))
(zone foo (uncle me fadi))
(zone foo (child fadi karim))
(zone bar (child fadi sarah))
(zone:connect bar foo)

! (? bar (atom of $x) $x)
;; [interest, note]
! (? foo (atom of $x) $x)
;; [interest]
! (? bar (, (uncle me $u) (child $u $c)) $c)
; []
! (? foo (, (uncle me $u) (child $u $c)) $c)
; []

Another experiment was to leverage MeTTa spaces themselves. This enables match and compound queries, but it is still not ideal because compound queries for atoms that span spaces would not work.

(= (zone:connections $zone)
   (match &self (zone:connect $zone $x) $x))
(= (zone:connections $zone)
   (zone:connections (match &self (zone:connect $zone $x) $x)))

(= (? $zone $query $pattern)
   (let $space (zone:connections $zone)
        (match $space $query $pattern)))
(= (? $zone $query $pattern)
   (match $zone $query $pattern))

! (bind! &foo (new-space))
! (bind! &bar (new-space))

! (add-atom &foo (atom of interest))
! (add-atom &bar (atom of note))
! (add-atom &foo (uncle me fadi))
! (add-atom &foo (child fadi karim))
! (add-atom &bar (child fadi sarah))

(zone:connect &bar &foo)

! (? &bar (atom of $x) $x)
; [interest, interest, note]
! (? &foo (atom of $x) $x)
; [interest]
! (? &bar (, (uncle me $u) (child $u $c)) $c)
; [karim, karim]
! (? &foo (, (uncle me $u) (child $u $c)) $c)
; [karim]

Note this this prototype catches karim but not sarah as a cousin, because karim exists in the same zone/space as the uncle fadi but sarah does not.

Is there a way to achieve these semantics in MeTTa directly? Or is it best to start investigating custom space implementations?

@Necr0x0Der
Copy link
Collaborator

Well, there is an option to put a space into another space, like (add-atom &parent &child). In this case, (match &parent ...) will also walk through &child. However, this will work for trees, but not for DAGs, I guess. So, it's indeed needed to be handled manually, but it depends on how you want to handle multiple inheritances in your case. One option would be to put all the children with deduplication temporarily for matching. OTOH, I don't think that matching in spaces inserted into other spaces is optimized. Maybe, putting everything in one space and tagging could be more efficient.

@nasser
Copy link
Author

nasser commented Dec 15, 2024

nested spaces are great! this will probably address our needs -- thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants