Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First use of SWI-Prolog

Tags:

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)
like image 333
John R Avatar asked Mar 07 '11 16:03

John R


People also ask

Is SWI-Prolog a compiler?

Online Prolog (swi) compiler.

Is SWI-Prolog an IDE?

SWI-Prolog has a graphical IDE under construction based on XPCE, which is the graphical interface library SWI-Prolog has chosen for cross-platform development. Almost all programmer editors will supply syntax highlighting for Prolog, with the right definitions file installed.

Is SWI-Prolog open source?

SWI-Prolog offers a comprehensive free Prolog environment. Since its start in 1987, SWI-Prolog development has been driven by the needs of real world applications.


1 Answers

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.

like image 141
Fred Foo Avatar answered Nov 12 '22 12:11

Fred Foo