1

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

false
  • 10,264
  • 13
  • 101
  • 209
cbotnav
  • 21
  • 4
  • 1
    You need to enter `male(X).` interactively. – false Oct 21 '20 at 09:04
  • @false, isn't that done? `?- male(X).` – cbotnav Oct 21 '20 at 09:08
  • 1
    The `?- ` is the top level's prompt. And you type `male(X).` – false Oct 21 '20 at 09:14
  • @false so like this: `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).` ? – cbotnav Oct 21 '20 at 14:08
  • You compile the rest (as you did before) and then loaded, you pose the query. – false Oct 21 '20 at 14:10

1 Answers1

1

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.
Isabelle Newbie
  • 9,258
  • 1
  • 20
  • 32
  • BTW, where does this coloring come from? – false Oct 21 '20 at 15:27
  • 1
    StackOverflow recently changed to some new "smart" syntax highlighting that thinks everything is JavaScript, or something. It automatically colors code blocks (i.e., normal indented-by-four-spaces blocks) without having to do anything. And without providing an easy way to turn it off, as far as I can tell. – Isabelle Newbie Oct 21 '20 at 15:33