Create a Prolog database by typing the facts listed below in

Create a Prolog database by typing the facts listed below in a .pro file. Consult the file in Amzi Prolog. While in Amzi Prolog, type in the queries given below. You are to turn in the first response from Prolog. It is not necessary to show the responses after the ‘;’ is hit.

Facts:

Mickey is a toy.

Snoopy is a toy.

Jane plays with Snoopy.

Jane plays with Mickey.

Jane likes every toy she plays with.

Ann likes everything that Jane likes.

Queries:

Does Ann like Snoopy?

What does Jane like?

Who likes what?

Thanks a lot for your help!!!

Solution

The standard Prolog predicates for loading programs are \'consult\', \'reconsult\', and the bracket loader notation \'[ ...]\'. For example, the goal

?- consult(\'lists.pro\').

opens the file lists.pro and loads the clauses in that file into memory.

There are two main ways in which a prolog program can be deficient: either the source code has syntax errors, in which case there will be error messages upon loading, or else there is a logical error of some sort in the program, which the programmer discovers by testing the program. The current version of a prolog program is usually considered to be a prototype for the correct version in the future, and it is a common practice to edit the current version and reload and retest it. This rapid prototyping appraoch works nicely provided that the programmer has devoted sufficient time and effort to analyzing the problem at hand. Interestingly, if the rapid prototyping approach seems to be failing, this is an excellent signal to take up paper and pencil, rethink the requirements, and start over!

We could call our editor from within prolog ...

?- edit(\'lists.pro\'). %% User defined edit, see below ...

and then upon returning from the editor (and assuming that the new version of the file was resaved using the same file name), one could use the goal

? reconsult(\'lists.pro\').

to reload the program clauses into memory, automatically replacing the old definitions. If one had used \'consult\' rather than \'reconsult\' the old (and possibly incorrect) clauses would have remained in memory along with the new clauses (... this depends upon the Prolog system, actually).

If several files have been loaded into memory, and one needs to be reloaded, use \'reconsult\'. If the reloaded file defines predicates which are not defined in the remaining files then the reload will not disturb the clauses that were originally loaded from the other files.

The bracket notation is very handy. For example,

?- [\'file1.pro\',file2.pro\',file3.pro\'].

would load (effectively reconsult) all three files into prolog memory.

Many prolog systems rely upon the programmer to hava a favorite text editor for editing prolog programs. Here is a sample prolog program which calls TextEdit on the Mac(OSX). (This is just an illustration; we do not use TextEdit routinely for writing prolog programs.)

To use this editor, its source must be loaded (assume it\'s local to prolog session) ...

?- [edit].

yes

and then an \'edit\' goal can be used (again assume that the file to edit is local to the prolog session)...

?- edit(\'p.pl\').

{ TextEdit starts up with file loaded. Edit the program... Save the program using the same filename ...}

Create a Prolog database by typing the facts listed below in a .pro file. Consult the file in Amzi Prolog. While in Amzi Prolog, type in the queries given below
Create a Prolog database by typing the facts listed below in a .pro file. Consult the file in Amzi Prolog. While in Amzi Prolog, type in the queries given below

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site