swi-prolog 5.10.2
I have typed this prolog program in. However, when I run option a I get the following error.
go/0 Undefined Procedure save/1
I am sure that save is a proper keyword predicate, and the path to the file does exist as well. Not sure where I am going wrong here.
What does the /0 /1 mean in go and save?
Source code
/* Shopping list */
go:-reconsult('~/projects/prolog/chap7/shopping.pl'),
write('a: See list'), nl,
write('b: Add to list'), nl,
write('c: Delete from list'), nl,
read(Choice),
choice(Choice),
save('~/projects/prolog/chap7/shopping.pl').
/*
facts for shopping
*/
item(potatoes).
item(bread).
item(coffee).
/*
Rules for shopping list
*/
choice(a):-listing(item), nl.
choice(b):-write('Enter an item: '),
read(Item),
assert(item(Item)).
choice(c):-write('Item to delete: '),
read(Item),
retract(item(Item)).
choice(_):-write('Incorrect entry.'), nl.
Many thanks in advance,
The error message states that the predicate
go/0you defined makes use of a predicatesave/1that the system does not recognize and label as an undefined procedure. Predicates are usually identified by a predicate indicator in the formname/arity, where name is the name of the predicate, represented as an atom, and arity is a number representing how many parameters the predicate has been defined accepting: so, for example,go/0is the identifier for the predicatego, with no parameters, defined in your program; andsave/1is the identifier for the predicatesave, with one parameter, that you are trying to invoke but the system is unable to find. When a predicate has been defined through two or more overloaded versions, e.g. with a different arity for each definition, it may be referred by enclosing each arity between brackets, separated with a comma, e.g.p/[1,2]. Predicates are sometimes called procedures, the two terms being roughly equivalent.As it’s possible to see in older SWI-Prolog manuals (the link refers to version 2.9)
save/1indeed used to be defined in the standard library and generally available on those systems. Even in those old systems, it was suggested to usesave_program/[1,2]to create a new program instead ofsave/1, which was said to be exploited for debug purposes only. However, in modern SWI-Prolog systems, norsave/1neithersave_program/[1,2]are available. While it seems that there is no equivalent predicate forsave/1, the role ofsave_program/[1,2]has been taken byqsave_program/[1,2], which you can read about in the latest version of the manual. Be sure to read also about its limitations, just in case you’d want to expand your program way beyond the simple snippet you posted here.Having never used that kind of procedures, I must assume you know what you are doing. However, noticing the
reconsult/1call at the beginning of your program that uses the same file saved bysave/1later, I beg you to become aware that there’s a difference between a bunch of Prolog predicates in a text file, i.e. the format thatreconsult/1can read and understand, and the half-text half-binary format that at leastqsave_program(but I maintainsaveandsave_programtoo) uses to write the program on disk. Since, by reading your code, it seems you may be interested in storing the shopping list’s items, as an aside note I would like to add that you may save predicates of interest in a plain text file by using a combination oflisting/1with I/O procedures such asopen/3,set_output/1,current_output/1, and the like.