Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get simple Prolog example to work

Tags:

prolog

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?

like image 438
Uwe Ziegenhagen Avatar asked Mar 15 '14 08:03

Uwe Ziegenhagen


People also ask

How do you write a function in Prolog?

Prolog write is defined as, 'write' is an in-built predicate in prolog, it returns all the information that we required to show in the output, it gives clear output that means if we required output related to the program and with something friendly then we can use write predicate in the program, it uses terms to the ...

What are the rules in Prolog give example?

A rule in Prolog is a clause, normally with one or more variables in it. Normally, rules have a head, neck and body, as in: eats(Person, Thing) :- likes(Person, Thing), food(Thing). This says that a Person eats a Thing if the Person likes the Thing , and the Thing is food .


1 Answers

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.

like image 194
Paulo Moura Avatar answered Oct 09 '22 19:10

Paulo Moura