【问题标题】:prolog Arguments are not sufficiently instantiated errorprolog 参数没有充分实例化错误
【发布时间】:2023-04-02 20:09:02
【问题描述】:

我必须编写一个 prolog 查询来打印每个孩子的总收入超过父母的家庭的家庭详细信息,我已经写过但我收到了这个错误

Arguments are not sufficiently instantiated
In:
   [3] _1770 is _1776+0
   [2] totalx([person(_1844,_1846,_1848,...)],_1834) at  line 50
   [1] '<meta-call>'((...,...)) <foreign>

这是基本代码:

family(person( john, cohen, date(17,may,1990), unemployed), person( lily, cohen, 
date(9,may,1990), unemployed),[ ] ).
family(person( john, armstrong, date(7,may,1988), unemployed), person( lily, Armstrong, 
date(29,may,1961), unemployed), [ ] ).
family(person( eric, baily, date(7,may,1963), works( bbc, 2200)), person( grace, baily, 
date(9,may,1965), works( ntu, 1000)), [person( louie, baily, date(25,may,1983), unemployed) ] ).
family(person( eric, baily, date(7,may,1963), works( acc, 21200)), person( grace, baily, 
date(9,may,1965), works( ntnu, 12000)), [person( louie, baily, date(25,may,1983), unemployed) ] ).
family(person( eric, fox, date(27,may,1970), works( bbc, 25200)), person( grace, fox, 
date(9,may,1971), works( ntbu, 13000)), [person( louie, fox, date(5,may,1993), unemployed) ] ).

    husband(X) :- family(X, _, _).
    wife(X) :- family(_, X, _).
    child(X) :- family(_, _, Children), member(X, Children).

    salary(person(_, _, _, works(_, S)), S).
    salary(person(_, _, _, unemployed), 0).

这是我的要求代码:

    48-totalx([],0).
    49-totalx([Person|L],Sum):-salary(Person,S),totalx(L,Rest),
    50-Sum is S+Rest

需求查询:

family(Husband,Wife,Child),totalx(Children,IChildren),totalx([Husband],IHusband), 
totalx([Wife],IWife),IChildren > IHusband+IWife.

有人可以向我解释为什么它不起作用吗?

【问题讨论】:

  • 您收到警告了吗?喜欢变量Armstrong?
  • 单独尝试`totalx(Children,IChildren)´。
  • 不,我不只是参数没有充分实例化错误
  • 我得到参数没有充分实例化在:[2] _1352 is _1358+0 [1] totalx([person(_1426,_1428,_1430,...)],_1416) 在第 51 行

标签: prolog instantiation-error


【解决方案1】:

您的代码中有两个错误。

  • 第一个是原子armstrong,在谓词family/3的第二个子句中,必须以小写字母开头。
  • 第二个是变量Child,在查询的第一个目标中,必须是Children(实例化错误是因为变量Children没有在第二个目标中实例化查询)。
family(person(john, cohen, date(17,may,1990), unemployed),
       person(lily, cohen, date( 9,may,1990), unemployed),
       []).

family(person(john, armstrong, date( 7,may,1988), unemployed),
       person(lily, armstrong, date(29,may,1961), unemployed), % lowercase
       []).

family(person(eric,  baily, date(7,may,1963), works( bbc, 2200)),
       person(grace, baily, date(9,may,1965), works( ntu, 1000)),
       [person( louie, baily, date(25,may,1983), unemployed)]).

family(person(eric,  baily, date(7,may,1963), works( acc, 21200)),
       person(grace, baily, date(9,may,1965), works( ntnu, 12000)),
       [person(louie, baily, date(25,may,1983), unemployed)] ).

family(person(eric,  fox, date(27,may,1970), works( bbc, 25200)),
       person(grace, fox, date( 9,may,1971), works( ntbu, 13000)),
       [person(louie, fox, date(5,may,1993), unemployed)] ).


husband(X) :- family(X, _, _).
wife(X) :- family(_, X, _).
child(X) :- family(_, _, Children), member(X, Children).

salary(person(_, _, _, works(_, S)), S).
salary(person(_, _, _, unemployed), 0).


totalx([], 0).
totalx([Person|L],Sum) :-
    salary(Person, S),
    totalx(L, Rest),
    Sum is S + Rest.

查询:

?- family(Husband,Wife,Children), totalx(Children,IChildren),totalx([Husband],IHusband),totalx([Wife],IWife),IChildren > IHusband+IWife.
false.

查询失败,因为没有一个家庭的孩子收入比父母多。但是,删除该约束后,查询可以正常工作并产生一些答案。

?- family(Husband,Wife,Children), totalx(Children,IChildren),totalx([Husband],IHusband),totalx([Wife],IWife).
Husband = person(john, cohen, date(17, may, 1990), unemployed),
Wife = person(lily, cohen, date(9, may, 1990), unemployed),
Children = [],
IChildren = IHusband, IHusband = IWife, IWife = 0 ;

Husband = person(john, armstrong, date(7, may, 1988), unemployed),
Wife = person(lily, armstrong, date(29, may, 1961), unemployed),
Children = [],
IChildren = IHusband, IHusband = IWife, IWife = 0 
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多