How to choose between bagof, setof and findall in Prolog

自闭症网瘾萝莉.ら 提交于 2020-12-08 06:46:33

问题


How does one choose between bagof, setof and findall? Are there any important differences? Which is most commonly used and which is the safest? Thanks for your comments/answers.

I checked the SWI-Prolog manual page on findall/3 and found them to be very similar.


回答1:


That's a great question!

I am not attempting to give an exhaustive answer, but I would like to suggest a few lines of thought that can help you to understand these predicates better:

  • Think about which of these predicates are more fundamental in the sense of what can be expressed in terms of what. For example, can you express findall/3 in terms of setof/3? And what about the other way around? This helps to see what you need to provide at least to implement all these predicates.
  • Think about which declarative properties are preserved by these predicates. For example, does the order in which the solutions are found influence the results? For which of these predicates precisely? Can any of these predicates fail? In which cases precisely?
  • Think about the space and time complexity of each of these predicates. Which of these predicates, if any, can be implemented and used more efficiently than others? And at what cost and tradeoffs?

In addition, I recommend you read Richard O'Keefe's book The Craft of Prolog for valuable information about these predicates.




回答2:


an important difference I found between findall/3 and bagof/3, it's the latter doesn't copies terms it accumulates in list. This can be fundamental, for instance, when your list will collect attributed variables, like to ones you use when modelling a problem with library(clpfd).



来源:https://stackoverflow.com/questions/38172507/how-to-choose-between-bagof-setof-and-findall-in-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!