Saturday, January 9, 2010

Brief Overview on Prolog and Installation Procedure of SWI-Prolog on Linux

  • Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics.
  •  
  • First Prolog system was developed in 1972 by Alain Colmerauer and Phillipe Roussel.

  • SWI-Prolog is an open source implementation of the programming language Prolog, commonly used for teaching and semantic web applications.

  • SWI-Prolog has been under continuous development since 1987. Its main author is Jan Wielemaker. The name SWI is derived from Sociaal-Wetenschappelijke Informatica ("Social Science Informatics"), the former name of the group at the University of Amsterdam, where Wielemaker is employ.

  • The first implementations of Prolog were interpreters, however, David H. D. Warren created the Warren Abstract Machine, an early and influential Prolog compiler which came to define the "Edinburgh Prolog" dialect which served as the basis for the syntax of most modern implementations.
For more details follow wikipedia.


How to Install SWI-Prolog on Fedora Core

Login as root (type su, provide password and hit enter).

Apply following commands.

# yum -y install pl-devel
Loaded plugins: presto, refresh-packagekit Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package pl-devel.i686 0:5.7.11-5.fc12 set to be updated --> Processing Dependency: pl = 5.7.11-5.fc12 for package: pl-devel-5.7.11-5.fc12.i686 --> Processing Dependency: libpl.so.5.7.11 for package: pl-devel-5.7.11-5.fc12.i686 --> Running transaction check ---> Package pl.i686 0:5.7.11-5.fc12 set to be updated --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: pl-devel i686 5.7.11-5.fc12 fedora 65 k Installing for dependencies: pl i686 5.7.11-5.fc12 fedora 5.8 M Transaction Summary ================================================================================ Install 2 Package(s) Upgrade 0 Package(s) Total download size: 5.9 M Downloading Packages: Setting up and reading Presto delta metadata Processing delta metadata Package(s) data still to download: 5.9 M (1/2): pl-5.7.11-5.fc12.i686.rpm | 5.8 MB 04:21 (2/2): pl-devel-5.7.11-5.fc12.i686.rpm | 65 kB 00:05 -------------------------------------------------------------------------------- Total 22 kB/s | 5.9 MB 04:29 Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : pl-5.7.11-5.fc12.i686 1/2 Installing : pl-devel-5.7.11-5.fc12.i686 2/2 Installed: pl-devel.i686 0:5.7.11-5.fc12 Dependency Installed: pl.i686 0:5.7.11-5.fc12 Complete! # pl -v SWI-Prolog version 5.7.11 for i386-linux

How to Install SWI-Prolog on Ubuntu
Apply this command to install SWI-Prolog on Ubuntu.

# sudo apt-get install gprolog swi-prolog

Prolog with Netbeans
Prolog can be used with Netbeans IDE. Who are interested navigate to following links.

Working with Prolog
After installing prolog you can provide pl command to run pl interpreter. To exit the interpreter press Ctrl + d. Or type 'halt.'.

To print something like hello world use write command.

?- write('hello world').
hello world
true.
 
?-
To write a prolog program you can use any editor. For example, to create a program called parent.pl you can use the following command(to open a file with gnome editor).

# gedit parent.pl&


Contents of parent.pl below:

/* upper case used for variables, lower case for constants.
   Terminate each clause by a period.  A clause can be written
   on multiple lines. Do not leave any blank space. */

/* these are rules */

parent(X,Y):-mother(X,Y).
parent(X,Y):-father(X,Y).
grandparent(X,Y):-parent(X,Z),parent(Z,Y).

/* and these facts */
mother(sonja,mary).
mother(sonja,jane).
father(john,jim).
father(john,bob).
father(bob,bill).
father(bob,dan).

/* and these are goals 

Find all X that are grandparents of Y.  Type ; after the first answer
is returned to see more answers.

grandparent(X,Y).

Find who John is a granparent of

grandparent(john,X).
*/

Now run prolog

# pl
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 5.7.11)
Copyright (c) 1990-2009 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).
Now we will compile the program parent.pl We can use either compile or consult command. Or can simply type ['parent.pl'] to compile the program.

# consult('/path_to_file/file_name')

Syntax of compile and consult command are same.

Put a period afte every command. Period means end of command.

?- ['parent.pl'].
% parent.pl compiled 0.00 sec, 1,632 bytes true. ?-

Now run queries.

# ?- grandparent(X,Y).
X = john, Y = bill ; X = john, Y = dan ; false. ?- grandparent(john,X). X = bill ; X = dan. ?- parent(X,Y). X = sonja, Y = mary ; X = sonja, Y = jane ; X = john, Y = jim ; X = john, Y = bob ; X = bob, Y = bill ; X = bob, Y = dan. ?- listing. father(john, jim). father(john, bob). father(bob, bill). father(bob, dan). mother(sonja, mary). mother(sonja, jane). parent(A, B) :- mother(A, B). parent(A, B) :- father(A, B). grandparent(A, C) :- parent(A, B), parent(B, C). % Foreign: rl_read_history/1 % Foreign: rl_write_history/1 % Foreign: rl_add_history/1 % Foreign: rl_read_init_file/1 true. ?- halt. #

Acknowledgement and Related Links:

6 comments:

  1. Thanks for dropping a comment.

    ReplyDelete
  2. thanks it helped me

    ReplyDelete
  3. Glad to know that. Thanks for dropping by..

    ReplyDelete
  4. Nowhere on SWI Prolog site was the clear, obvious instruction 'yum -y install pl-devel' given! Only circular links and lots of mumbo-jumbo about RPMs. This was so much less painful than what's on there, for someone trying to install Prolog, thank you!

    ReplyDelete
  5. Hey, thank you. I appreciate that.

    ReplyDelete