- ?- book(ISBN,_,_).
- ?- book(ISBN,Title,_),hates(Person,ISBN),author(Person,ISBN).
Notice that this shows the bindings to the variables Person and ISBN as
well. If we want to 'hide' those, we can define a new predicate and
query that predicate:
hated_book(Title):-
book(ISBN,Title,_),
hates(Person,ISBN),
author(Person,ISBN).
?- hated_book(Title).
- ?- author(Person,ISBN),book(ISBN,Title,_),write('The book '),write(Title),write(' is written by '),write(Person).
Notice that if you want to hide the variable-bindings here by introducing a new predicate (as in exercise 2)
print_info:-
author(Person,ISBN),
book(ISBN,Title,_),
write('The book '),
write(Title),
write(' is written by '),
write(Person).
prolog will only print 'Yes' when you ask the query ?- print_info..
Why? Since there are no variables in the query, prolog does not have to
show you all possible bindings. You ask prolog: 'can you print info?'
and since it can do this once it ansers 'Yes'. So it is not possible to
hide the variable-bindings here.
proud_author(Person):-
author(Person,Book),
owns(Person,Book).
- This is not possible with what you've learned so far!!
Everything you did processes individual books, one at a time. But to
count the number of books, you would have to consider 'all' books at
the same time. This is not possible until we will see meta-predicates
such as findall/3.
- The most important difference between 'books' and 'books in a
library' is that often a library has more than one copy of a specific
book. In other words, the difference between a book (the result of
intellectual work, which is unique) and a copy/print of a book (which
is usually not unique) becomes important. One possible representation
is to extend the book/3 predicate with an extra argument: a copy-number. A library with two copies of The art of Prolog will then look like this:
book(1,'The art of Prolog',400,1).
book(1,'The art of Prolog',400,2).
As you already see from this, the danger is the introduction of
redundancy, with all the problems it may cause (extra work to add data
and correct data, chances of introducing inconsistency, extra storage
space,...).
An alternative, cleaner representation is obtained by introducing a new predicate bookcopy/2. We then represent the same knowledge as follows:
book(1,'The art of Prolog',400).
bookcopy(1,1).
bookcopy(1,2).
This nicely seperates the concept of a book from the concept of a
copy/print of the book. This eliminates the redundancy, so this is
clearly the prefered representation.
- For Dutch it would look as follows:
raadpleeg(X):-consult(X).
stop:-halt.
schrijf(X):-write(X).
nieuwelijn:-nl.
voegtoe(X):-assert(X).