Outputting prolog in Mac

97 views Asked by At

I have a problem using prolog on a Mac, I figured out how to run it using SWI-Prolog but when I run it, it gives an error and does not give the expected output

Expected output: homer, bart

male(homer).
male(bart).
female(marge).
female(lisa).
female(maggie).

parent(homer, bart).
parent(homer, lisa).
parent(homer, maggie).
parent(marge, bart).
parent(marge, lisa).
parent(marge, maggie).

mother(X, Y) :- parent(X, Y), female(X).
father(X, Y) :- parent(X, Y), male(X).
son(X, Y) :- parent(Y, X), male(X).
daughter(X, Y) :- parent(Y, X), female(X).

?- male(X).

Here is the error I was speaking about earlier

Warning: /Users/[username]/Desktop/simpsons.pl:19:
Warning:    Singleton variables: [X]
true.

And instead of outputting homer, bart it outputs true

1

There are 1 answers

2
Isabelle Newbie On

When programming in Prolog, you put definitions and queries in different places. Definitions go into source files. Queries do not go into source files. You enter them while interacting with the Prolog system in something called the "toplevel" or "prompt" or "shell" or maybe "REPL" (read-eval-print loop).

For example, these are definitions:

male(homer).
male(bart).
female(marge).
female(lisa).
female(maggie).

You have put them in a source file called simpsons.pl. This is correct.

This is not a definition but a query:

?- male(X).

This does not go in a source file. Do not put it in simpsons.pl. Rather, you:

  1. start your Prolog system
  2. load the simpsons.pl file somehow
  3. enter the query male(X). and observe answers

This video: https://www.youtube.com/watch?v=t6L7O7KiE-Q shows these steps with SWI-Prolog on a Mac.

If you are comfortable using a command line, you might also be able to do this simpler. For example, on my (Linux) machine, I can start SWI-Prolog with a command line argument naming a file to be loaded:

$ swipl simpsons.pl 
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?-

See that ?-? That is the prompt meaning that the toplevel is now waiting for your input. Here is where you enter your male(X) query. You can use ; or Space to cycle through the various answers:

?- male(X).
X = homer ;
X = bart.