views:

47

answers:

2

I am experimenting with prolog, reading "Programming in prolog using the ISO standard, fith edition". I have installed yap (yet another prolog) on my ubuntu 10.10 Maverick RC system, installed using synaptic. I am running prolog from within emacs23 using prolog-mode.

The following code (from chapter five of book) does not give results as in book:

/*   FILE   history_base.pl   */                                                    
use_module(library(lists)) /* to use member/2    */                   
event(1505,['Euclid',translated,into,'Latin']).                        
event(1510,['Reuchlin-Pfefferkorn',controversy]).                      
event(1523,['Christian','II',flies,from,'Denmark']).                        

mywhen(X,Y):-event(Y,Z),member(X,Z).

% Restoring file /usr/lib/Yap/startup
YAP version Yap-5.1.3

< reading the above file into yap>

  ?- mywhen("Denmark",D).
no

which is not what the book gives!

Now adding to the file above the line (from the book):

hello1(Event):- read(Date), event(Date,Event).

Gives this error when reading the file into yap

(using "consult buffer" in the prolog menu in emacs):

  ?-  % reconsulting /tmp/prolcomp14814QRf.pl...    
     SYNTAX ERROR at /tmp/prolcomp14814QRf.pl, near line 3:   
 use_module( library( lists ) )                                                    
<    ==== HERE ====>                              
 event( 1505 , [ Euclid , translated , into
 , Latin ] ).
 % reconsulted /tmp/prolcomp14814QRf.pl in module user, 0 msec 752 bytes
yes
   ?- 

¿Any comments?

+2  A: 

Perhaps you should terminate the use_module(library(lists)) statement with a . and declare it as a directive, i.e.:

:- use_module(library(lists)).

sharky
Thanks for the answer. I did as you said, and now prolog accepts the file without problems. But still it do not work as expected:
kjetil b halvorsen
The above terminated too soon!. It should have continued:
kjetil b halvorsen
% reconsulted /tmp/prolcomp14814QRf.pl in module user, 0 msec 8064 bytesyes ?- mywhen("Denmark",D).no ?-
kjetil b halvorsen
A: 

You have to write Denmark between single quotes instead of double quotes, i.e.:

?- mywhen('Denmark', D).

When you put Denmark between double quotes, the prolog interprets it as a list of character codes instead of an atom, but in the definition of event it is written as an atom (between single quotes).

gusbro
Yep, atoms are not strings in Prolog, so `'Denmark'` is not the same as `"Denmark"`; the latter is syntactic sugar for `[68, 101, 110, 109, 97, 114, 107]` (a list of character codes, as gusbro has said).
sharky