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???!!!
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 .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With