Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a fact in SWI-Prolog?

Tags:

prolog

I just want to create something like: like(x,y). I've been trying for a long time and am really frustrated, could anyone please tell me how to do it???!!!

like image 226
Hope7 Avatar asked Nov 03 '10 05:11

Hope7


People also ask

How do you write facts in SWI-Prolog?

To have Prolog accept the predicate without asserting one, enter dynamic(like/2) . Then you'll get false instead of an Undefined procedure exception*, until you assert a like/2 fact. assert/1 is a common but non-standard predicate. For portability, use instead either asserta/1 or assertz/1 .

How do you write a fact in Prolog?

Facts have some simple rules of syntax. Facts should always begin with a lowercase letter and end with a full stop. The facts themselves can consist of any letter or number combination, as well as the underscore _ character.

How facts and rules are implemented in Prolog?

A Prolog program consists of a number of clauses. Each clause is either a fact or a rule. After a Prolog program is loaded (or consulted) in a Prolog interpreter, users can submit goals or queries, and the Prolog intepreter will give results (answers) according to the facts and rules.

How do you use SWI in Prolog?

Open a terminal (Ctrl+Alt+T) and navigate to the directory where you stored your program. Open SWI-Prolog by invoking swipl . In SWI-Prolog, type [program] to load the program, i.e. the file name in brackets, but without the ending. In order to query the loaded program, type goals and watch the output.


1 Answers

I'm assuming you are using swi interactively and trying to enter the fact gives you an error like so:

1 ?- like(x, y). ERROR: toplevel: Undefined procedure: like/2 (DWIM could not correct goal) 

Since the fact does not exist in the database. If this is the case, try asserting the fact first:

2 ?- assert(like(x,y)). true. 

Then you can try:

3 ?- like(x, y). true. 

This time the query succeeds because the fact exists in the database.

A better approach might be to write your clauses into a file & then consult them. Swi prolog has an emacs-like editor that you can bring up by typing

emacs. 

at the prompt. Or use your own editor & then consult the file. Swi prolog comes with a lot of graphical tools that might be of help; look at the manual for more details.

like image 126
Roman Avatar answered Oct 05 '22 22:10

Roman