1

I am now trying to use the assoc library in sw-prolog to create a relational prolog program. I need a dictionary that can store multiple values. This can be easily accomplished using a list as value.

However, i often need to look up across all keys, single values -- i.e. identify all keys that have included one particular value item. (However, the number of value is typically limited to at most tens of items).

If, i were to only use a association store that maps from keys to multiple values, then i would need to go through all keys, fetch the values and then check if a value is its member.

This is not efficient, nor does it seem scalable with large numbers of keys.

Is there a more efficient way to do this?

I could manage for each value item its own "inverse" key-value store, but this seems pretty inefficient. Also, if i were to use prolog fact store (with assert/ and retracts), to store each key value pair as an individual fact, i would probably get these lookup "indexed" for free.

What is the right design choice for a relational approach here?

[edit-1]: btw, one "solution" could be to allow in the assoc library to have multiple keys, in case values are then internally "indexed" -- but, the assoc library doesn't support this (as far as i know).

[edit-2]: look at additional libraries in swi-prolog i notice unweighted graphs. Perhaps using a graph structure would be more efficient for handing such a many-to-many relation between keys and values. And, in keeping with a relational approach.

[edit-3): or perhaps, ugraph, is not a good idea, since nodes (and links) are not typed. I would need to structurally indicate which node is a key and which are values.

thank you,

Daniel

user2193970
  • 345
  • 2
  • 10
  • Prolog is relational 'at heart'. I think you should follow its data model, recently extended by tabling. It's not an easy path, though... – CapelliC Dec 05 '17 at 08:02
  • You should use the Prolog database. A set of clauses with the same name and arity is a relation. You can have one-one or one-many mappings as you like, and collect into a list using `findall/3`. Maybe if you take one step back and describe the problem rather than a proposed obviously inappropriate solution, you will get better help. – Tomas By Dec 05 '17 at 22:17
  • Hi Tomas, I seek to implement many to many relationships that are dynamic -- i.e. the relationships get updated frequently. This is in contrast to say, relational information that is static -- such as a knowledge or a social graph that mostly includes relationship that came to exist and now persist. It seems that relations that are frequently updated are better treated using prolog relational data structures rather than the prolog database; since assert and retract and not well backtrackable (unless one has, i guess, transactional logic approach such as in XSB prolog). – user2193970 Dec 06 '17 at 10:00

0 Answers0