Simple Question

I’m using:

SWI-Prolog version online

I want the code to:

So I have predicates of a basketball games in which I save

(#game,HomeTeam,VisitorTeam,HomePointsOf1stQ,VisitorPointsOf1stQ,...

so on for the 4 quarters.

I want to see who wins by comparing the sum of each team quarters to get each team final result

But what I’m getting is:

just error

Edit: It’s my first prolog project so I’m a newbie

My code looks like this:

statsEquipa:-
    write('What team is it?'), read(Z),
    aggregate_all(count,jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD>FA+FB+FC+FD,VitoriasCasa),
    aggregate_all(count,jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD>FA+FB+FC+FD,VitoriasFora),
    aggregate_all(count,jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD<FA+FB+FC+FD,DerrotasCasa),
    aggregate_all(count,jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD<FA+FB+FC+FD,DerrotasFora),
    CountVitorias is VitoriasCasa+VitoriasFora,
    write(Z), format(": \n"),
    format('Número de Vitórias: '),write(CountVitorias),
    CountDerrotas is DerrotasCasa+DerrotasFora,
    format('\nNúmero de Derrotas: '),write(CountDerrotas).

Hi welcome.

You say you are using SWI-Prolog on line which I guess is SWISH.

If so can you post the link here and then we can run the code, see the error, etc.

Thanks.

I didn’t even get that though ahah thanks

https://swish.swi-prolog.org/p/Nba.pl

1 Like

I copied the source code to my computer, because it is more natural for me to use SWI-Prolog there.

In loading the source into SWI-Prolog using consult/1 I receive the following errors:

Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:529:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/13
          Use :- discontiguous jogo/11. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:597:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/9
          Use :- discontiguous jogo/11. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:919:
        Clauses of jogo/13 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:528
          Current predicate: jogo/11
          Use :- discontiguous jogo/13. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:920:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/13
          Use :- discontiguous jogo/11. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:1264:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/10
          Use :- discontiguous jogo/11. to suppress this message

The first error

Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:529:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/13
          Use :- discontiguous jogo/11. to suppress this message

Line 48 is the first data line

jogo(1,celtics,philadelphia,21,21,26,21,30,24,28,21).

and has 11 parameters.

Line 529 is

jogo(3,philadelphia,raptors,29,30,36,29,26,20,35,22).

and has 11 parameters.

but line 528 is

jogo(3,wizards,suns,33,29,22,26,28,24,26,30,40,37).

and has 13 parameters.

SWI-Prolog expects all predicates with the same name, e.g. (jogo) and arity to be listed in order in the code.

The cause of this error message is that the predicate is missing some parameters, or that the predicate has clauses with different arity and the code needs to rearranged to keep the clauses with the same name and arity together.


If I run the query

 ?- statsEquipa.
Qual a equipa?nets.

ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [15] ... + _7042+_7036> ... + _7054+_7048
ERROR:   [14] '$bags':findall_loop(jogo(_7100,nets,_7104,_7106,_7108,_7110,_7112,_7114,_7116,_7118,_7120)-1,user:(... > ...),_7088,[]) at c:/program files/swipl/boot/bags.pl:97
ERROR:   [13] setup_call_catcher_cleanup('$bags':'$new_findall_bag','$bags':findall_loop(...,...,_7182,[]),_7160,'$bags':'$destroy_findall_bag') at c:/program files/swipl/boot/init.pl:466
ERROR:    [9] aggregate:aggregate_all(count,jogo(_7230,nets,_7234,_7236,_7238,_7240,_7242,_7244,_7246,_7248,_7250),user:(... > ...),_7226) at c:/program files/swipl/library/aggregate.pl:258
ERROR:    [8] statsEquipa at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:1287
ERROR:    [7] <user>
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
^  Exception: (13) setup_call_catcher_cleanup('$bags':'$new_findall_bag', '$bags':findall_loop(jogo(_6104, nets, _6108, _6110, _6112, _6114, _6116, _6118, _6120, _6122, _6124)-1, user:(_6110+_6114+_6118+_6122>_6112+_6116+_6120+_6124), _6198, []), _7340, '$bags':'$destroy_findall_bag') ? creep
^  Call: (15) call('$bags':'$destroy_findall_bag') ?

I get that you are extracting the points for each team during each period and adding them for a total for the game, e.g. CA+CB+CC+CD but I don’t understand the purpose of >, e.g. CA+CB+CC+CD>FA+FB+FC+FD.

Please explain.

ohh thats a mistake, I wrote over 1000 lines of predicates so I would be afraid that would happen.

So in CA+CB+CC+CD>FA+FB+FC+FD I’m trying to compare the final result to see which team has won,
so CA is HomeTeam points in 1st quarter… and FA is the VisitorTeam points in the 1st quarter and so on, and if the points of CA+CB+CC+CD are greater then (>) FA+FB+FC+FD then Home Team has won and adds one to ‘VitoriasCasa’ variable

and the line 528 should be jogo(3,wizards,suns,33,29,22,26,28,24,66,67).

Thanks.

Your code was almost correct, mostly just added a few parenthesis in the right place was all.

statsEquipa:-
    write('Qual a equipa?'), read(Z),
    aggregate_all(count, (jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) > (FA+FB+FC+FD)),VitoriasCasa),  % Home victories
    aggregate_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) > (FA+FB+FC+FD)),VitoriasFora),  % Away victories
    aggregate_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) < (FA+FB+FC+FD)),DerrotasCasa),  % Home defeats
    aggregate_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) < (FA+FB+FC+FD)),DerrotasFora),  % Away defeats
    CountVitorias is VitoriasCasa+VitoriasFora,  % Victories
    CountDerrotas is DerrotasCasa+DerrotasFora,  % Defeats
    format('~w: ~nNúmero de Vitórias: ~w~nNúmero de Derrotas: ~w~n',[Z,CountVitorias,CountDerrotas]).

aggregrate_all/3 takes three arguements, but

jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD>FA+FB+FC+FD

is seen as more than one. It just needed some parenthesis to make it one argument.
Also added parenthesis around CA+CB+CC+CD so the compares worked correctly.

Lastly just used format correctly. I too had a lot of problems understanding format at first, but now it comes easy. :smiley:

?- statsEquipa.
Qual a equipa?nets.
nets: 
Número de Vitórias: 45
Número de Derrotas: 38
true.

Also I don’t know Spanish so hopefully the words are correct.

omg thank you so much!
It’s actually portuguese but yeah :stuck_out_tongue: