prolog

Get simple Prolog example to work


I am trying to run a simple gprolog to run on my Linux machine, GNU Prolog was installed from the Ubuntu Software Center.

From the GNU Prolog Intro I got the following example, stored in HelloWorld.pl.

parent(hank,ben).
parent(hank,denise).
parent(irene,ben).
parent(irene,denise).
parent(alice,carl).
parent(ben,carl).
parent(denise,frank).
parent(denise,gary).
parent(earl,frank).
parent(earl,gary).
grandparent(X,Z):-parent(X,Y),parent(Y,Z).
ancestor(X,Y):-parent(X,Y).
ancestor(X,Y):-parent(Z,Y),ancestor(X,Z).

I start gprolog, enter [HelloProlog]. and get the following error:

| ?- [HelloProlog].
uncaught exception: error(instantiation_error,consult/1)

Even if I do not load the code from a file but run it interactively I get an error:

uwe@z11:~/desktop$ gprolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- parent(Luke,Anakin).
uncaught exception: error(existence_error(procedure,parent/2),top_level/0)
| ?- 

Is my installation broken or what am I doing wrong?


Solution

  • In Prolog, variables start with an upper case letter (or with an underscore), hence the instantiation error you got with the goal [HelloProlog]. Simply use instead ['HelloProlog']. I.e. represent the file path as a Prolog atom, which require single quotes when they start with an upper case letter.

    The existence error you got is simply due to querying a predicate that it's not defined. You need to load the HelloWorld.pl file first.