prolog - How to create condition for functor's argument in a rule - PROLOG
问题描述
I'm currently learning Prolog and I want to create a specific rule which will check if a person can watch a film. The condition for true should be age of person equal of higher than required age for film.
So I have something like that:
person("John",19).
person("Kate",14).
person("Carl",8).
film("Shining",18,"Horror").
film("Little Agents",13,"Family").
film("Frozen",7,"Animation").
can_borrow(film(_,Age1,_),person(_,Age2)):-Age2>=Age1.
And if I ask i.e.
?- can_borrow(film("Shining",18,"Horror"),person("John",19)).
It works and returns true.
But when I ask to show me all possible combinations (all films which every person can watch)
?- can_borrow(X,Y).
I have an error:
ERROR: Arguments are not sufficiently instantiated
How to write the rule properly, so it would work as I've written above?
Thanks in advance.
解决方案
The film(_, Age1, _)
and person(_, Age2)
in can_borrow(film(_, Age1, _), person(_, Age2))
are just terms with a functor that happens to be the same as a predicate. But Prolog does not attach special meanings to it. You should here call predicates to unify the terms. For example:
can_borrow(film(Title, Age1, Genre), person(Name, Age2)) :-
film(Title, Age1, Genre),
person(Name, Age2),
Age1 =< Age2.
推荐阅读
- node.js - Sequelize 交易因外键而失败
- javascript - 在选择中更改选项元素的背景颜色
- powershell - 使用 Powershell 安装给定文件夹中的所有文件
- javascript - 在 chrome 浏览器中面临混合内容的问题
- django - 如何使用 python 将 ogg 文件转换为 mp3?
- java - 从 Java 1.8 迁移到 11 后,Jaxb 不使用绑定适配器
- python-3.x - ruamel.yaml 如何使用roundtripdumper
- reactjs - 我如何控制放大 DataStore 同步
- fluid-framework - (FluidFrameworkv0.44.1])如何获取Container clientId
- kubernetes - 使用自定义列或 jsonpath 在 kubectl 中组合多列输出