5
votes

I am just starting to use Prolog, and already I've run into problem with a seemingly simple example. Here is my .pl file:

hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).

Then, after I load the program into swipl, I test it with this line at the prompt

writesgoodcode(jeff).

I thought it would display true, but I get this error:

?- hacker(jeff).
ERROR: hacker/1: Undefined procedure: mountaindew/1
   Exception: (7) hacker(jeff) ? 

This program works fine, but this doesn't solve my problems:

hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).

$ swipl -s dumb.pl
% dumb.pl compiled 0.00 sec, 1,112 bytes

?- hacker(jeff).
true.

Can anyone explain why my original program doesn't work? From my understanding, Prolog should "skip" the first statement since it doesn't have enough information, and check the next line. It does have enough info for that second line, and thus it should evaluate true. Any help or a point in the right direction would be great. Thanks.

3

3 Answers

4
votes

As the error message says, you have an undefined procedure mountaindew/1. To make your code return true, your options are:

  1. Define this predicate
  2. Declare that this predicate is dynamic: dynamic(mountaindew/1)
  3. Declare that all unknown predicates should fail (not recommended): set_prolog_flag(unknown, fail)
0
votes

you could also change the order of the predicates (cannot be done always ofc) but mostly what Kaarel said.

in the end there is not really a point in writing something that will always fail, even if you are still developing the code

0
votes

This works but as I am a beginner I can't say why. The word "uninstantiated" may apply. Despite not knowing why it works, I think it's helpful to show one way that works.

hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
mountaindew(john).
doesntsleep(john).
writesgoodcode(jeff).