prologinstantiation-error

prolog Arguments are not sufficiently instantiated error


I have to write a prolog query to print family details of each family where children’s total income is more than their parents, I've written but I'm getting this error

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>

This is the base code:

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).

This is my code for the requirement:

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

And the query for the requirement:

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

can someone explain to me why it doesn't work?


Solution

  • There are two errors in your code.

    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.
    

    Query:

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

    The query fails because there is no family where the children earn more than their parents. However, removing that constraint, the query works properly and produces some answers.

    ?- 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 
    ...