yap prolog read predicate - prolog

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?

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

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).

Related

Prolog what's the difference between \+ and \=

What is the difference \= and \+?
because
?- 15\=14.
?- \+ 15=14.<--- this gives an error while the above does not.
Why?Aren't they the same?
Edit: here's the error:
Compiling the file:
D:\Program Files\Strawberry Prolog Beta\Games\WarCraft.pro
Warning 4: The string \+ is not an operator. (line 1, before the first clause)
Error 16: Instead of the integer 15 what is expected here is something like an infix operator or a full stop. (line 1, before the first clause)
1 error, 1 warning.
Also I'm using Strawberry prolog I also tried it on SWI prolog still the same.
I think you are putting queries into Prolog source files. That is not where they should go:
predicate definitions go into Prolog source files
queries are typed into the interactive Prolog toplevel
Try running the SWI-Prolog program without an input file. You should get a window with some informational messages about the SWI-Prolog version and then a prompt ?-. That is the toplevel. Try typing your query there. All queries should go there.
I don't know about Strawberry Prolog, but I suspect it's the same there.

Prolog, getting right answer in terminal, but wrong answer when running a program

Typing "prolog" in terminal gets:
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?-
Typing:
| ?- member(2, [1,2,3]).
Gets:
true ?
Then pressing enter gets:
yes
Typing:
| ?- member(4, [1,2,3]).
gets:
no
When i write a file; test.pl consisting of this:
:- member(4, [1,2,3]), nl, halt.
And then write in the terminal:
| ?- [test2].
I get:
compiling /path/test.pl for byte code...
/path/test.pl:1: warning: unknown directive (',')/2 - maybe use initialization/1 - directive ignored
/path/test.pl compiled, 1 lines read - 139 bytes written, 11 ms
yes
Shouldnt the answer here be no? What am i doing wrong. Also, how would you do this in prolog:
if (testInPrologTerminal(member(4, [1,2,3])) { do this; }
I.e, i want to send queries to the prolog top level, and get an answer
When you type the query member(2, [1,2,3]), GNU Prolog prompts you for a possible additional solution (hence the true ? prompt) as only by backtracking (and looking to the last element in the list, 3) it could check for it. When you press enter, you're telling the top-level interpreter that you are satisfied with the current solution (the element 2 in the list second position). The second query, member(4, [1,2,3]), have no solutions so you get a no.
To execute a query when a file is loaded, the standard and portable way of doing it, is to use the standard initialization/1 directive. In this case, you would write:
:- initialization((member(4, [1,2,3]), nl, halt)).
Note the ()'s surrounding the query, otherwise you may get a warning about an unknown initialization/3 standard, built-in, control construct. If you have more complex queries to be executed when a file is loaded, then define a predicate that makes the queries a call this predicate from the initialization/1 directive. For example:
main :-
( member(4, [1,2,3]) ->
write('Query succeeded!'), nl
; write('Query failed!'), nl
).
:- initialization(main).
Writing arbitrary queries as directives in a source file is legacy practice and thus accepted by several Prolog implementations but using the initialization/1 directive is the more clean, standard, and portable alternative.

Constraint Programming library or syntax issue in SWI-Prolog

I'm just trying to figure out constraint programming in SWI-Prolog, looking at this tutorial : http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming
However I seem to be falling at the first hurdle.
?- use_module(library(clpfd)).
true.
?- X #> Y, X in 1..3, Y=2.
ERROR: Syntax error: Operator expected
ERROR: X
ERROR: ** here **
ERROR: #> Y, X in 1..3, Y=2 .
?-
What's going wrong here? I seem to have included the library, but the first example line from the tutorial throws a syntax error.
All the tutorials I can find seem to use operators like #=, #< etc. But my SWI-Prolog baulks at them. Are they an extra syntax which comes with that constraint library? (And am I failing to load it?)
Or am I misreading the tutorial examples?
Update : Trying to understand things from Horsh's reply below. I can get this to work if I use the library and run the line in the interactive terminal. But if I try to import the library and use these operators in a source file, then it throws the error again. What am I not understanding?
Update 2 :
OK. If, in my source file, I invoke the library and then write a rule which contains a #>. Then I try to consult it from the command-line. It will throw an error and the #> syntax is un-recognised. If import the library to the command line before trying to consult the program, it works. Can this be right?
Building on Horsh's answer, you should be importing the library in your source code, remembering to put ?- at the beginning of the line like so:
?- use_module(library(clpfd)).
The ?- tells SWI-Prolog to execute the line as if it were typed into the interpreter directly, instead of trying to declare it as a predicate in your program.
Don't be concerned about SWI-Prolog importing the library more than once, it knows to check if the library was modified and only reloads it if the library was changed since the last time it was loaded.
For anyone else that finds this in the future, if you want to import a library in an SWI-Prolog source file, the following will also work:
:- use_module(library(clpfd)).
Note the :- and not ?-.
The is all in the manual here and there.
?- [library(clpfd)].
% library(error) compiled into error 0.00 sec, 10,128 bytes
% library(apply) compiled into apply 0.00 sec, 16,840 bytes
% library(assoc) compiled into assoc 0.00 sec, 13,132 bytes
% library(lists) compiled into lists 0.00 sec, 14,332 bytes
% library(pairs) compiled into pairs 0.00 sec, 5,372 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 392,604 bytes
true.
?- X #> Y, X in 1..3, Y=2.
X = 3,
Y = 2.

First use of SWI-Prolog

I'm brand new to Prolog. I am simply trying to get some output from Prolog on Windows Vista.
I have downloaded and installed Prolog 5.1; I chose the .pro file extension when installing (not to confuse with Perl files).
I created a file called test.pro.
Inside this file I put the following:
inside(tom).
?-inside(tom).
I double clicked the file and a command line interface popped up. On this interface (after a bunch of generic Prolog version/copyright info) the only output is:
1 ?-
OK, for starters, I did not expect it to ask a question; I expected it to answer a question (something along the line of 'yes').
Anyway, I tried to respond to the query with the following:
In the command line I re-inserted 'inside(tom).', so the whole line looks like:
1 ?- inside(tom).
I pressed Enter and got an error message:
ERROR: toplevel: Undefined procedure: inside/1 (DWIM could not correct goal)
Prolog doesn't answer questions if you haven't told it facts. (Except for some built-in facts such as member(1, [1,2,3]).)
You can tell it who is inside by (comment follow a %):
1 ?- [user]. % get facts and rules from user input
|: inside(mary). % Mary and John are explicitly inside
|: inside(john).
|: inside(X) :- location(X, house). % rule: anyone in the house is inside
|: inside(X) :- location(X, office). % (variables start with a capital letter)
|:
|: location(tom, house).
|: location(bernard, house).
|: location(anne, office).
|: % type Ctrl+D
% user://1 compiled 0.00 sec, 1,220 bytes
true.
2 ?- inside(tom). % Prolog deduces that Tom is inside
true .
If you want to learn Prolog, Learn Prolog Now is a good, free tutorial.
You need to compile this first (also called "consult" in prolog). If I knew which version of prolog you have I could find out the exact key entry for this command (Ctrl-L may work). By the way, welcome to the wonderful world of prolog- I love it :) As soon as you're over this hurdle, it gets a lot better. :)
Quick and dirty. What was missing was 'compiling' the file, known as consult
and the syntax is as follows, all characters in the line are relevant.
?- [filename].
then you can ask questions and do other things with what the database.(the source code in prolog)
http://www.swi-prolog.org/pldoc/man?section=quickstart

Do SWI-Prolog files need start and end characters?

I'm new to SWI-Prolog and am trying some tutorials. Every file I try to load through the command line, however, gets 2 error messages - one at the start (Operator expected) and one at the end (Unexpected end of file). Files are saved in the same directory as the one I'm working in.
For example, I have this file saved as kb2.pl
listensToMusic(mia).
happy(yolanda).
playsAirGuitar(mia) :- listensToMusic(mia).
playsAirGuitar(yolanda) :- listensToMusic(yolanda).
listensToMusic(yolanda):- happy(yolanda).
From the start of opening Prolog (through /opt/local/bin/swipl), my command line looks like this:
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,928 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.2)
Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
help, use ?- help(Topic). or ?- apropos(Word).
?- [kb2].
ERROR: /Users/name/Desktop/kb2.pl:1:0: Syntax error: Operator expected
ERROR: /Users/name/Desktop/kb2.pl:10:52: Syntax error: Unexpected end of file
% kb2 compiled 0.00 sec, 2,584 bytes
true.
?-
When I ask for the listing of the compiled file, I get:
\happy(yolanda).
\playsAirGuitar(mia) :- listensToMusic(mia).
\playsAirGuitar(yolanda) :- listensToMusic(yolanda).
true.
So it's cutting out the first and last lines of the file.
I've searched for these error messages online and found a lot of useful hints about formatting for Prolog, but none that address this situation. Is there some sort of special character or formatting that should be used for the start and end of a file for Prolog?
Which operating system are you on? Are you sure the file is ASCII (and not UTF-16)? Does it have the native line ending for your platform? Can you post the whole file (raw) so people can check it?
Many Prolog systems, such as SWI-Prolog, Jekejeke Prolog, allow to detect a BOM as the first character of a text file. The BOM is some white space, that indicates the encoding of the text file:
254 255 UTF-16BE
255 254 UTF-16LE
239 187 191 UTF-8
So there is no need to restrict yourself to ASCII. Many text editors will place the BOM for you. And SWI-Prolog, Jekejeke Prolog, by default probe the BOM.

Resources