Basic of Prolog


Prolog is  a first logic programing language. Prolog is using for Artificial Intelligence and computational linguistics programing.

Prolog is declarative: the program logic is expressed in terms of relations, represented as facts and rules. A computation is initiated by running a query over these relations.

The language was first conceived by a group around  “Alain Colmerauer” in Marseille, France, in the early 1970s and the first Prolog system was developed in 1972 by Colmerauer with Philippe Roussel.

Above all things are just a  copy paste from wiki…. 😀

Now, For the main spotlight comes… How to deals with prolog?

Software that you need : swi-prolog  / gprolog / turbo prolog / visual prolog / etc… [But here, we are dealing with swi-prolog & gprolog].

gprolog [The GNU Prolog] : GNU Prolog is a free Prolog compiler with constraint solving over finite domains developed by Daniel Diaz.

You can find more thing here : http://www.gprolog.org/

swi-prolog : SWI-Prolog offers a comprehensive free Prolog environment. Since its start in 1987.

You can find more thing here : http://www.swi-prolog.org/

How to install gprolog ?

Open terminal n type this command :

% sudo apt-get install swi-prolog

How to install swi-prolog ?

Open terminal n type this command :

% sudo apt-add-repository ppa:swi-prolog/stable
% sudo apt-get update
% sudo apt-get install swi-prolog

Now, We are starting very first basic program for understand this software how to use and which type program we will help us for kick startup.

Some Impotent My Things :  You must some basic knowledge regarding “Converting English to Prolog Facts And Rules”.

  • English : The cakes are delicious.
    • In Prolog : delicious(cakes).
  • English : The pickles are delicious.
    • In Prolog : delicious(pickles).
  • English : The Manges are sweet.
    • In Prolog : sweet(mangoes).
  • English : Shivang likes food if it is delicious.
    • In Prolog : likes(shivang,food) :- delicious(food).
  • English : Shivang likes food if it is spicy and delicious.
    • In Ptolog : likes(shivang,food) :- spicy(food), delicious(food).

Notes : ” if ” replaced by ” :- ” and the ” end ” by ” , ” (commas). And every facts terminated by ” . ” (period).

You think what is this ? and how it will help us !!

So answer is, It’s a facts / sentences / rules. That are indicates weather it’s  a true or false or some kind of indication or some kind of facts.

One thing you must know, We are also doing same things. First we are decided the facts / sentences / rules are true of false for certain condition and then after we are doing action performs or take a decision based on that facts / sentences / rules.

On the first site it’s a  some kind of complication, but if u think parallel…

As a New Born Baby !———> As  a Fresh Computer System with No Program !

As a Small Child  !————–> As  a Some kind of OS n with some Programs… !

As a Gentleman Like You !—-> As a Prolog Program !

You think like, New born baby have no any kind of things and thoughts… And when baby learn or start to storing  ” facts / sentences / rules ” in mind (Very Powerful System ), and based on that facts/sentences/rules baby give the response or perform the action.

With the help of prolog programing language, we are try to achieve same things. And It will help us to achieve the Artificial Intelligence (AI).

Aim : Check for Entered Name of People are Human or not !

First make file with extension ” .pl “. Like “human.pl”. In that file, define the facts / sentences / rules. and save it.

human.pl

human.pl

for the Comments :

% single line comments.  /* Multi Line Comments */

Now, Open swi-prolog or gprolog. For Linux user go where the .pl file is stored. and open gprolog/swi-prolog. For swi-prolog type “swipl” and for gprolog type “gprolog” on the terminal. You can see this kind of screen…. I am using gprolog because i have some issue related to swi-prolog (actually it’s  can’t exit properly or some bugs issue ).

gprolog

gprolog

Before starting anything else, We know some basic things like help, exit terminate the execution etc….

For Any Kind of Help or Action ” Ctrl + c “.  and then type action option. If u are using Very First time then type ” h  “. It will show you the options. You can see here…

Prolog Terminal

Prolog Terminal

Now, How to compile the “human.pl” prolog code ?

?- [human].

Type enter. For successful compilation it will return ” true “. otherwise it will display error.

After successful compilation we can ask the equation to prolog terminal…

  • English : shivang is Human ?
    • In Prolog : human(shivang).
  • English : krunal is Human ?
    • In Prolog : human(krunal).
  • English :harshit is Human ?
    • In Prolog : human(harshit).

It entered name stored as  a human then it will say/return “yes. “, otherwise “no/false.”

Who is Human ?

Who is Human ? 😀

That’s It ! Program Complete !

Notes : It’s a very basic level things and prolog program. Just for starter kick…If Any kind of Mistake of Anything Not True then comment Here…

Thank u !

For More Info You can Read AI books….

You can find same post Here : https://mylinuxsys.wordpress.com/2014/02/17/basic-of-prolog/