首页 > 解决方案 > 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.

标签: prolog

解决方案


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.

推荐阅读