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???!!!
-
2Actually there are two ways of entering top level predicates. Both are explained here: http://www.swi-prolog.org/FAQ/ToplevelMode.html Also, found is recommendations on alternative usage. This question is also mirrored here: http://stackoverflow.com/questions/5404143/prolog-gives-error-undefined-procedure-when-trying-to-use – PLG Jun 23 '12 at 11:00
-
1See also https://de.wikipedia.org/wiki/Prolog_%28Programmiersprache%29#Laden_von_Prolog-Texten – Oct 06 '15 at 13:01
2 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.
-
2To 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. – frayser Nov 04 '10 at 11:37
-
3`assert/1` is a common but non-standard predicate. For portability, use instead either `asserta/1` or `assertz/1`. – Paulo Moura Feb 09 '15 at 10:12
You can create facts a prolog file and load them using consult function.
For example,
animals.pl
bigger(elephant, tiger).
bigger(tiger, rabbit).
bigger(rabbit, sparrow).
bigger(sparrow, ant).
You can also use assert function to define facts in prolog terminal.
1 ?- assert(bigger(elephant, rabbit)).
true.
Refer this link, to get more infromation.

- 3,658
- 1
- 36
- 57
-
assert is what I was looking for. It is strange that the prolog repl is in a different mode than prolog files, that's not usually the case for REPLs - a script executed by most other repl programs (for interpreted programming languages) behaves pretty much the same as piping that script on stdin, which is obviously not the case for (swi) prolog... – masterxilo Mar 30 '22 at 16:11