如何模拟“外连接”在核心逻辑中?
我刚刚开始使用 core.logic,为了解决它,我试图实现一些简单的东西,类似于我目前正在专业处理的问题。然而,问题的一部分让我难住了......
作为我的示例的简化,如果我有一个项目目录,其中一些仅在某些国家/地区可用,而有些则在特定国家/地区不可用。我希望能够指定项目列表和例外情况,例如:
(defrel items Name Color)
(defrel restricted-to Country Name)
(defrel not-allowed-in Country Name)
(facts items [['Purse 'Blue]
['Car 'Red]
['Banana 'Yellow]])
(facts restricted-to [['US 'Car]])
(facts not-allowed-in [['UK 'Banana]
['France 'Purse]])
如果可能,我宁愿不指定所有国家/地区的允许入境,因为受限制的项目集相对较小,并且我'我希望能够进行一次更改以允许/排除给定国家/地区的某个项目。
如何编写一个规则来给出一个国家/地区的项目/颜色列表,并具有以下约束:
- 该项目必须位于项目列表中
- 国家/项目不得位于“不允许进入”列表中
- 任何一个:
- 该商品的限制列表中没有国家/地区
- 该国家/地区/商品对位于限制列表中
有某种方法可以做到这一点吗?我是否以完全错误的方式思考事情?
I've just started playing with core.logic, and to work on it I'm trying to implement something simple that is similar to a problem that I am currently working on professionally. However, one part of the problem has me stumped...
As a simplification of my example, if I have a catalog of items, and some of them are only available in certain countries, and some are not available in specific countries. I'd like to be able specify the list of items, and the exceptions, something like:
(defrel items Name Color)
(defrel restricted-to Country Name)
(defrel not-allowed-in Country Name)
(facts items [['Purse 'Blue]
['Car 'Red]
['Banana 'Yellow]])
(facts restricted-to [['US 'Car]])
(facts not-allowed-in [['UK 'Banana]
['France 'Purse]])
If possible, I'd rather not specify allowed-in for all countries, as the set of items with restrictions is relatively small, and I'd like to be able to make a single change to allow/exclude for an item for a given country.
How can I write a rule that gives the list of items/colors for a country, with the following constraints:
- The item must be in the list of items
- The country/item must be not be in the 'not-allowed-in' list
- Either:
- There is no country in the restricted-to list for that item
- The country/item pair is in the restricted-to list
Is there some way to do this? Am I thinking about things in entirely the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常当你开始否定逻辑编程的目标时,你需要达到非关系操作(Prolog 中的 cut,core.logic 中的 conda)。
该解决方案只能使用基本参数来调用。
完整解决方案
Usually when you start negating goals in logic programming, you need to reach for non-relational operations (cut in Prolog, conda in core.logic).
This solution should only be called with ground arguments.
Full solution
Conda 可能会使代码复杂化,使用 nafc,如果需要,您可以更轻松地重新排序目标。
这仍然是非关系性的! :)
有关更多示例: https://gist.github.com/ahoy-jon/cd0f025276234de464d5
Conda may complexifies the code, using nafc, you can more easily reorder goals if you want.
This is still non-relational ! :)
For more examples : https://gist.github.com/ahoy-jon/cd0f025276234de464d5