swipl gives an error on encountering variables in lists

Viewed 45

Code:

[mia, vincent, jules, yolanda]. 
[mia, [vincent, jules], [butch, girlfriend(butch)]].

% uncommenting the below line gives an error: 
% [mia, vincent, X]. 

According to my book and different sites on the internet, it's perfectly fine to include variables in prolog lists yet on loading the above knowledge base I get the following error:

ERROR: /home/user/Prolog/det/lists.pl:7:
        Arguments are not sufficiently instantiated
ERROR: /home/user/Prolog/det/lists.pl:7:
        Arguments are not sufficiently instantiated
        In:
   Arguments are not sufficiently instantiated
   Arguments are not sufficiently instantiated
        In:
          [31] throw(error(instantiation_error,_4850))

 [27] '$load_file'('/home/user/Prolog/det/lists.pl','/home/user/Prolog/det/lists.pl',_4878,[expand(false),...]) at /usr/lib/swi-prolog/boot/init.pl:2507
          [26] setup_call_catcher_cleanup(system:true,system:'$load_file'('/home/user/Prolog/det/lists.pl','/home/user/Prolog/lab4/lists.pl',_4946,...),_4924,system:'$end_consult'(...,user)) at /usr/lib/swi-prolog/boot/init.pl:443
          [22] '$do_load_file_2'(lists,'/home/user/Prolog/lab4/lists.pl',user,compiled,[expand(false),...]) at /usr/lib/swi-prolog/boot/init.pl:2124
          [19] '$mt_do_load'(<clause>(0x55a555ed3400),lists,'/home/user/Prolog/lab4/lists.pl',user,[expand(false),...]) at /usr/lib/swi-prolog/boot/init.pl:2070
          [18] setup_call_catcher_cleanup(system:with_mutex('$load_file',...),system:'$mt_do_load'(<clause>(0x55a555ed3400),lists,'/home/user/Prolog/det/lists.pl',user,...),_5084,system:'$mt_end_load'(<clause>(0x55a555ed3400))) at /usr/lib/swi-prolog/boot/init.pl:443
           [7] <user>
1 Answers

It certainly is fine to include variables in lists, but the lists need to be put in a context.

Just writing [mia, vincent, jules, yolanda]. is actually a shortcut to load files, as in consult/1.

You have to use those lists as the argument of a term for example:

list_of_people([mia, vincent, jules, yolanda]).

Maybe better written

person(mia).
person(vincent).
person(jules).
person(yolanda).

and then collected

?- bagof(P,person(P),PersonList).
PersonList = [mia,vincent,jules,yolanda].
Related